From 07ff953c8fe896524fa8fb703da6169d813c5b3e Mon Sep 17 00:00:00 2001 From: Aleksander Morgado Date: Mon, 27 Sep 2021 16:07:09 +0200 Subject: mbimcli: setup common helper to get enum values from strings --- src/mbimcli/mbimcli-basic-connect.c | 8 +++++++- src/mbimcli/mbimcli-helpers.c | 40 ++++++++++++++++++++----------------- src/mbimcli/mbimcli-helpers.h | 10 +++++++++- 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/src/mbimcli/mbimcli-basic-connect.c b/src/mbimcli/mbimcli-basic-connect.c index 3c47915..c8b6a0d 100644 --- a/src/mbimcli/mbimcli-basic-connect.c +++ b/src/mbimcli/mbimcli-basic-connect.c @@ -678,7 +678,13 @@ set_pin_input_parse (const gchar *str, } if (pin_type && (g_strv_length (split) == n_max)) { - new_pin_type = mbimcli_read_pintype_from_string (split[n++]); + const gchar *pin_type_str; + + pin_type_str = split[n++]; + if (!mbimcli_read_pin_type_from_string (pin_type_str, &new_pin_type)) { + g_printerr ("error: couldn't parse input pin-type: %s\n", pin_type_str); + return FALSE; + } if (new_pin_type == MBIM_PIN_TYPE_UNKNOWN || (*pin_type == MBIM_PIN_TYPE_PIN1 && new_pin_type >= MBIM_PIN_TYPE_PUK1) || (*pin_type == MBIM_PIN_TYPE_PUK1 && new_pin_type < MBIM_PIN_TYPE_PUK1)) { diff --git a/src/mbimcli/mbimcli-helpers.c b/src/mbimcli/mbimcli-helpers.c index 2b2ae69..614c267 100644 --- a/src/mbimcli/mbimcli-helpers.c +++ b/src/mbimcli/mbimcli-helpers.c @@ -345,22 +345,26 @@ mbimcli_parse_key_value_string (const gchar *str, return TRUE; } -MbimPinType -mbimcli_read_pintype_from_string (const gchar *str) -{ - const gchar *feature; - gint i; - - if (str == NULL) - return MBIM_PIN_TYPE_UNKNOWN; - - /* Compare string to nicknames from mbim_pin_type_values */ - i = MBIM_PIN_TYPE_CUSTOM; - while (NULL != (feature = mbim_pin_type_get_string (i))) { - if (g_str_equal (feature, str)) - return i; - i++; +#define MBIMCLI_ENUM_LIST_ITEM(TYPE,TYPE_UNDERSCORE,DESCR) \ + gboolean \ + mbimcli_read_## TYPE_UNDERSCORE ##_from_string (const gchar *str, \ + TYPE *out) \ + { \ + GType type; \ + GEnumClass *enum_class; \ + GEnumValue *enum_value; \ + \ + type = mbim_## TYPE_UNDERSCORE ##_get_type (); \ + enum_class = G_ENUM_CLASS (g_type_class_ref (type)); \ + enum_value = g_enum_get_value_by_nick (enum_class, str); \ + \ + if (enum_value) \ + *out = (TYPE)enum_value->value; \ + else \ + g_printerr ("error: invalid " DESCR " value given: '%s'\n", str); \ + \ + g_type_class_unref (enum_class); \ + return !!enum_value; \ } - - return MBIM_PIN_TYPE_UNKNOWN; -} +MBIMCLI_ENUM_LIST +#undef MBIMCLI_ENUM_LIST_ITEM diff --git a/src/mbimcli/mbimcli-helpers.h b/src/mbimcli/mbimcli-helpers.h index 8abab69..2ad9c50 100644 --- a/src/mbimcli/mbimcli-helpers.h +++ b/src/mbimcli/mbimcli-helpers.h @@ -33,6 +33,14 @@ gboolean mbimcli_parse_key_value_string (const gchar *str, MbimParseKeyValueForeachFn callback, gpointer user_data); -MbimPinType mbimcli_read_pintype_from_string (const gchar *str); + +/* Common helpers to read enums from strings */ +#define MBIMCLI_ENUM_LIST \ + MBIMCLI_ENUM_LIST_ITEM (MbimPinType, pin_type, "pin type") + +#define MBIMCLI_ENUM_LIST_ITEM(TYPE,TYPE_UNDERSCORE,DESCR) \ + gboolean mbimcli_read_## TYPE_UNDERSCORE ##_from_string (const gchar *str, TYPE *out); +MBIMCLI_ENUM_LIST +#undef MBIMCLI_ENUM_LIST_ITEM #endif /* __MBIMCLI_H__ */ -- cgit v1.2.1