diff options
author | Aleksander Morgado <aleksander@aleksander.es> | 2016-08-09 17:31:04 +0200 |
---|---|---|
committer | Aleksander Morgado <aleksander@aleksander.es> | 2016-10-12 13:24:08 +0200 |
commit | 29517c99ba9fee164b788a1f7cff43773e0b58be (patch) | |
tree | 69a50f3183584bec1cc84bc10b1b6fd1d2a9e16c | |
parent | fe460b2f54b6a035a207815252fe87ee3a06351c (diff) |
ublox: new +UPINCNT response parser
-rw-r--r-- | plugins/ublox/mm-modem-helpers-ublox.c | 80 | ||||
-rw-r--r-- | plugins/ublox/mm-modem-helpers-ublox.h | 10 | ||||
-rw-r--r-- | plugins/ublox/tests/test-modem-helpers-ublox.c | 60 |
3 files changed, 149 insertions, 1 deletions
diff --git a/plugins/ublox/mm-modem-helpers-ublox.c b/plugins/ublox/mm-modem-helpers-ublox.c index 855e097a..01172de4 100644 --- a/plugins/ublox/mm-modem-helpers-ublox.c +++ b/plugins/ublox/mm-modem-helpers-ublox.c @@ -21,6 +21,86 @@ #include "mm-modem-helpers-ublox.h" /*****************************************************************************/ +/* +UPINCNT response parser */ + +gboolean +mm_ublox_parse_upincnt_response (const gchar *response, + guint *out_pin_attempts, + guint *out_pin2_attempts, + guint *out_puk_attempts, + guint *out_puk2_attempts, + GError **error) +{ + GRegex *r; + GMatchInfo *match_info; + GError *inner_error = NULL; + guint pin_attempts = 0; + guint pin2_attempts = 0; + guint puk_attempts = 0; + guint puk2_attempts = 0; + gboolean success = TRUE; + + g_assert (out_pin_attempts); + g_assert (out_pin2_attempts); + g_assert (out_puk_attempts); + g_assert (out_puk2_attempts); + + /* Response may be e.g.: + * +UPINCNT: 3,3,10,10 + */ + r = g_regex_new ("\\+UPINCNT: (\\d+),(\\d+),(\\d+),(\\d+)(?:\\r\\n)?", 0, 0, NULL); + g_assert (r != NULL); + + g_regex_match_full (r, response, strlen (response), 0, 0, &match_info, &inner_error); + if (!inner_error && g_match_info_matches (match_info)) { + if (!mm_get_uint_from_match_info (match_info, 1, &pin_attempts)) { + inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_UNSUPPORTED, + "Couldn't parse PIN attempts"); + goto out; + } + if (!mm_get_uint_from_match_info (match_info, 2, &pin2_attempts)) { + inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_UNSUPPORTED, + "Couldn't parse PIN2 attempts"); + goto out; + } + if (!mm_get_uint_from_match_info (match_info, 3, &puk_attempts)) { + inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_UNSUPPORTED, + "Couldn't parse PUK attempts"); + goto out; + } + if (!mm_get_uint_from_match_info (match_info, 4, &puk2_attempts)) { + inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_UNSUPPORTED, + "Couldn't parse PUK2 attempts"); + goto out; + } + success = TRUE; + } + +out: + + if (match_info) + g_match_info_free (match_info); + g_regex_unref (r); + + if (inner_error) { + g_propagate_error (error, inner_error); + return FALSE; + } + + if (!success) { + g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED, + "Couldn't parse +UPINCNT response: '%s'", response); + return FALSE; + } + + *out_pin_attempts = pin_attempts; + *out_pin2_attempts = pin2_attempts; + *out_puk_attempts = puk_attempts; + *out_puk2_attempts = puk2_attempts; + return TRUE; +} + +/*****************************************************************************/ /* UUSBCONF? response parser */ gboolean diff --git a/plugins/ublox/mm-modem-helpers-ublox.h b/plugins/ublox/mm-modem-helpers-ublox.h index e8f09ff9..ba264a39 100644 --- a/plugins/ublox/mm-modem-helpers-ublox.h +++ b/plugins/ublox/mm-modem-helpers-ublox.h @@ -20,6 +20,16 @@ #include <ModemManager.h> /*****************************************************************************/ +/* +UPINCNT response parser */ + +gboolean mm_ublox_parse_upincnt_response (const gchar *response, + guint *out_pin_attempts, + guint *out_pin2_attempts, + guint *out_puk_attempts, + guint *out_puk2_attempts, + GError **error); + +/*****************************************************************************/ /* UUSBCONF? response parser */ typedef enum { /*< underscore_name=mm_ublox_usb_profile >*/ diff --git a/plugins/ublox/tests/test-modem-helpers-ublox.c b/plugins/ublox/tests/test-modem-helpers-ublox.c index 7aa091f9..2f3994c3 100644 --- a/plugins/ublox/tests/test-modem-helpers-ublox.c +++ b/plugins/ublox/tests/test-modem-helpers-ublox.c @@ -27,6 +27,64 @@ #include "mm-modem-helpers-ublox.h" /*****************************************************************************/ +/* Test +UPINCNT responses */ + +typedef struct { + const gchar *str; + guint pin_attempts; + guint pin2_attempts; + guint puk_attempts; + guint puk2_attempts; +} UpinCntResponseTest; + +static const UpinCntResponseTest upincnt_response_tests[] = { + { .str = "+UPINCNT: 3,3,10,10\r\n", + .pin_attempts = 3, + .pin2_attempts = 3, + .puk_attempts = 10, + .puk2_attempts = 10 + }, + { .str = "+UPINCNT: 0,3,5,5\r\n", + .pin_attempts = 0, + .pin2_attempts = 3, + .puk_attempts = 5, + .puk2_attempts = 5 + }, + { .str = "+UPINCNT: 0,0,0,0\r\n", + .pin_attempts = 0, + .pin2_attempts = 0, + .puk_attempts = 0, + .puk2_attempts = 0 + }, +}; + +static void +test_upincnt_response (void) +{ + guint i; + + for (i = 0; i < G_N_ELEMENTS (upincnt_response_tests); i++) { + GError *error = NULL; + gboolean success; + guint pin_attempts = G_MAXUINT; + guint pin2_attempts = G_MAXUINT; + guint puk_attempts = G_MAXUINT; + guint puk2_attempts = G_MAXUINT; + + success = mm_ublox_parse_upincnt_response (upincnt_response_tests[i].str, + &pin_attempts, &pin2_attempts, + &puk_attempts, &puk2_attempts, + &error); + g_assert_no_error (error); + g_assert (success); + g_assert_cmpuint (upincnt_response_tests[i].pin_attempts, ==, pin_attempts); + g_assert_cmpuint (upincnt_response_tests[i].pin2_attempts, ==, pin2_attempts); + g_assert_cmpuint (upincnt_response_tests[i].puk_attempts, ==, puk_attempts); + g_assert_cmpuint (upincnt_response_tests[i].puk2_attempts, ==, puk2_attempts); + } +} + +/*****************************************************************************/ /* Test UUSBCONF? responses */ typedef struct { @@ -454,7 +512,7 @@ int main (int argc, char **argv) g_type_init (); g_test_init (&argc, &argv, NULL); - + g_test_add_func ("/MM/ublox/upincnt/response", test_upincnt_response); g_test_add_func ("/MM/ublox/uusbconf/response", test_uusbconf_response); g_test_add_func ("/MM/ublox/ubmconf/response", test_ubmconf_response); g_test_add_func ("/MM/ublox/uipaddr/response", test_uipaddr_response); |