Move the WSC Primary Device Type parsing from p2p.c and eap-wsc.c to a common function in wscutil.c supporting both formats so that it can be used in ap.c too. --- src/eap-wsc.c | 9 +-------- src/p2p.c | 43 ++++++------------------------------------- src/wscutil.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/wscutil.h | 2 ++ 4 files changed, 54 insertions(+), 45 deletions(-) diff --git a/src/eap-wsc.c b/src/eap-wsc.c index 78a52b4e..bbf32ed3 100644 --- a/src/eap-wsc.c +++ b/src/eap-wsc.c @@ -1742,19 +1742,12 @@ static bool load_primary_device_type(struct l_settings *settings, struct wsc_primary_device_type *pdt) { const char *v; - int r; v = l_settings_get_value(settings, "WSC", "PrimaryDeviceType"); if (!v) return false; - r = sscanf(v, "%hx-%2hhx%2hhx%2hhx%2hhx-%2hx", &pdt->category, - &pdt->oui[0], &pdt->oui[1], &pdt->oui[2], - &pdt->oui_type, &pdt->subcategory); - if (r != 6) - return false; - - return true; + return wsc_device_type_from_setting_str(v, pdt); } static bool load_constrained_string(struct l_settings *settings, diff --git a/src/p2p.c b/src/p2p.c index f0324d31..89d8c4ac 100644 --- a/src/p2p.c +++ b/src/p2p.c @@ -4355,43 +4355,12 @@ struct p2p_device *p2p_device_update_from_genl(struct l_genl_msg *msg, str = l_settings_get_string(iwd_get_config(), "P2P", "DeviceType"); - /* - * Standard WSC subcategories are unique and more specific than - * categories so there's no point for the user to specify the - * category if they choose to use the string format. - * - * As an example our default value (Computer - PC) can be - * encoded as either of: - * - * DeviceType=pc - * DeviceType=0x00010050f2040001 - */ - if (str && !wsc_device_type_from_subcategory_str( - &dev->device_info.primary_device_type, - str)) { - unsigned long long u; - char *endp; - - u = strtoull(str, &endp, 0); - - /* - * Accept any custom category, OUI and subcategory values but - * require non-zero category as a sanity check. - */ - if (*endp != '\0' || (u & 0xffff000000000000ll) == 0) - l_error("[P2P].DeviceType must be a subcategory string " - "or a 64-bit integer encoding the full Primary" - " Device Type attribute: " - "|||"); - else { - dev->device_info.primary_device_type.category = u >> 48; - dev->device_info.primary_device_type.oui[0] = u >> 40; - dev->device_info.primary_device_type.oui[1] = u >> 32; - dev->device_info.primary_device_type.oui[2] = u >> 24; - dev->device_info.primary_device_type.oui_type = u >> 16; - dev->device_info.primary_device_type.subcategory = u; - } - } + if (str && !wsc_device_type_from_setting_str(str, + &dev->device_info.primary_device_type)) + l_error("[P2P].DeviceType must be a subcategory string " + "or a 64-bit integer encoding the full Primary" + " Device Type attribute: " + "|||"); l_free(str); diff --git a/src/wscutil.c b/src/wscutil.c index 49e73d7f..a48d2aa5 100644 --- a/src/wscutil.c +++ b/src/wscutil.c @@ -3050,3 +3050,48 @@ bool wsc_device_type_from_subcategory_str(struct wsc_primary_device_type *out, return false; } + +bool wsc_device_type_from_setting_str(const char *value, + struct wsc_primary_device_type *out) +{ + unsigned long long u; + char *endp; + + if (!value) + return false; + + /* + * Standard WSC subcategories are unique and more specific than + * categories so there's no point for the user to specify the + * category if they choose to use the string format. + * + * As an example our default value (Computer - PC) can be + * encoded as either of: + * + * DeviceType=pc + * DeviceType=0x00010050f2040001 + * DeviceType=1-0050f204-1 + */ + if (wsc_device_type_from_subcategory_str(out, value)) + true; + + u = strtoull(value, &endp, 0); + + /* + * Accept any custom category, OUI and subcategory values but require + * non-zero category as a sanity check. + */ + if (*endp == '\0' && (u & 0xffff000000000000ll) != 0) { + out->category = u >> 48; + out->oui[0] = u >> 40; + out->oui[1] = u >> 32; + out->oui[2] = u >> 24; + out->oui_type = u >> 16; + out->subcategory = u; + return true; + } + + return sscanf(value, "%hx-%2hhx%2hhx%2hhx%2hhx-%2hx", &out->category, + &out->oui[0], &out->oui[1], &out->oui[2], + &out->oui_type, &out->subcategory) == 6; +} diff --git a/src/wscutil.h b/src/wscutil.h index 5bb26d84..1d603d8f 100644 --- a/src/wscutil.h +++ b/src/wscutil.h @@ -681,3 +681,5 @@ bool wsc_device_type_to_dbus_str(const struct wsc_primary_device_type *val, const char **subcategory_str); bool wsc_device_type_from_subcategory_str(struct wsc_primary_device_type *out, const char *subcategory_str); +bool wsc_device_type_from_setting_str(const char *value, + struct wsc_primary_device_type *out); -- 2.27.0