diff options
author | Denis Kenzior <denkenz@gmail.com> | 2024-03-22 10:17:26 -0500 |
---|---|---|
committer | Denis Kenzior <denkenz@gmail.com> | 2024-04-17 11:57:33 -0500 |
commit | 7de3a65b33ad51804002d28c6ca09d69543de5eb (patch) | |
tree | 8a4c1412b3a0ab51c3e12539cb81be5dd9f5358f | |
parent | 1d064a14ce457ac1ddf123ddff3a44dff904ec48 (diff) | |
download | connman-7de3a65b33ad51804002d28c6ca09d69543de5eb.tar.gz |
ofono: Add generic method to convert a list of strings to flags
oFono D-Bus APIs are string based, with several string list based
properties that convey information that might require tracking. For
example:
- Interfaces -> list of interfaces supported
- Features -> list of features supported
- Capabilities -> modem capabilities
Introduce a new convenience method that can be used to convert such
string lists to a set of flags (bitmap). Make extract_interfaces()
use it. While here, tighten up error checking and do not attempt to
parse non-string list signatures.
-rw-r--r-- | plugins/ofono.c | 49 |
1 files changed, 40 insertions, 9 deletions
diff --git a/plugins/ofono.c b/plugins/ofono.c index 65a722fd8..190684333 100644 --- a/plugins/ofono.c +++ b/plugins/ofono.c @@ -685,29 +685,60 @@ static bool has_interface(uint8_t interfaces, return false; } -static uint8_t extract_interfaces(DBusMessageIter *array) +struct flag_map { + const char *value; + uint32_t flag; +}; + +static int string_list_to_flags(DBusMessageIter *array, + const struct flag_map *map, size_t map_len, + uint32_t *out_flags) { DBusMessageIter entry; - uint8_t interfaces = 0; + uint32_t flags = 0; + + if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY) + return -EINVAL; + + if (dbus_message_iter_get_element_type(array) != DBUS_TYPE_STRING) + return -EINVAL; dbus_message_iter_recurse(array, &entry); while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) { + size_t i; const char *name; dbus_message_iter_get_basic(&entry, &name); - if (g_str_equal(name, OFONO_SIM_INTERFACE)) - interfaces |= OFONO_API_SIM; - else if (g_str_equal(name, OFONO_NETREG_INTERFACE)) - interfaces |= OFONO_API_NETREG; - else if (g_str_equal(name, OFONO_CM_INTERFACE)) - interfaces |= OFONO_API_CM; + for (i = 0; i < map_len; i++) + if (!strcmp(name, map[i].value)) + flags |= map[i].flag; dbus_message_iter_next(&entry); } - return interfaces; + if (out_flags) + *out_flags = flags; + + return 0; +} + +static uint8_t extract_interfaces(DBusMessageIter *array) +{ + static const struct flag_map interfaces_map[] = { + { .value = OFONO_SIM_INTERFACE, .flag = OFONO_API_SIM }, + { .value = OFONO_NETREG_INTERFACE, .flag = OFONO_API_NETREG }, + { .value = OFONO_CM_INTERFACE, .flag = OFONO_API_CM }, + }; + uint32_t flags; + + if (string_list_to_flags(array, interfaces_map, + G_N_ELEMENTS(interfaces_map), + &flags) < 0) + return 0; + + return flags; } static char *extract_nameservers(DBusMessageIter *array) |