diff options
author | Aleksander Morgado <aleksander@aleksander.es> | 2020-09-09 10:55:33 +0200 |
---|---|---|
committer | Aleksander Morgado <aleksander@aleksander.es> | 2020-11-04 09:50:13 +0100 |
commit | f280573f6ded8da447f8f3bc934c4d3204a27f1c (patch) | |
tree | 44c081ab4dcebe8baf9719819516b075ad64f598 | |
parent | eb338c967f613b4de8ef2933d496e1bc0936f037 (diff) |
cinterion: setup SGAUTH response parser as a helper method
-rw-r--r-- | plugins/cinterion/mm-broadband-modem-cinterion.c | 44 | ||||
-rw-r--r-- | plugins/cinterion/mm-modem-helpers-cinterion.c | 45 | ||||
-rw-r--r-- | plugins/cinterion/mm-modem-helpers-cinterion.h | 9 | ||||
-rw-r--r-- | plugins/cinterion/tests/test-modem-helpers-cinterion.c | 51 |
4 files changed, 116 insertions, 33 deletions
diff --git a/plugins/cinterion/mm-broadband-modem-cinterion.c b/plugins/cinterion/mm-broadband-modem-cinterion.c index 2abd5d73..2df65340 100644 --- a/plugins/cinterion/mm-broadband-modem-cinterion.c +++ b/plugins/cinterion/mm-broadband-modem-cinterion.c @@ -1360,13 +1360,6 @@ common_load_initial_eps_bearer_finish (MMIfaceModem3gpp *self, static void common_load_initial_eps_step (GTask *task); -/* at^sgauth? - * ^SGAUTH: 1,2,"vf" - * ^SGAUTH: 3,0,"" - * ^SGAUTH: 4,0 - * - * OK - */ static void common_load_initial_eps_auth_ready (MMBaseModem *self, GAsyncResult *res, @@ -1374,35 +1367,20 @@ common_load_initial_eps_auth_ready (MMBaseModem *self, { const gchar *response; CommonLoadInitialEpsContext *ctx; + g_autoptr(GError) error = NULL; + MMBearerAllowedAuth auth = MM_BEARER_ALLOWED_AUTH_UNKNOWN; + g_autofree gchar *username = NULL; ctx = (CommonLoadInitialEpsContext *) g_task_get_task_data (task); - response = mm_base_modem_at_command_finish (self, res, NULL); - - /* in case of error, skip */ - if (response) { - g_autoptr(GRegex) r = NULL; - g_autoptr(GMatchInfo) match_info = NULL; - - r = g_regex_new ("\\^SGAUTH:\\s*(\\d+),(\\d+),?\"?([a-zA-Z0-9_-]+)?\"?", 0, 0, NULL); - g_assert (r != NULL); - - g_regex_match_full (r, response, strlen (response), 0, 0, &match_info, NULL); - while (g_match_info_matches (match_info)) { - guint cid = 0; - guint cinterion_auth_type = 0; - g_autofree gchar *username = NULL; - - mm_get_uint_from_match_info (match_info, 1, &cid); - mm_get_uint_from_match_info (match_info, 2, &cinterion_auth_type); - username = mm_get_string_unquoted_from_match_info (match_info, 3); - if (cid == ctx->cid) { - mm_bearer_properties_set_allowed_auth (ctx->properties, mm_auth_type_from_cinterion_auth_type (cinterion_auth_type)); - if (username) - mm_bearer_properties_set_user (ctx->properties, username); - } - g_match_info_next (match_info, NULL); - } + response = mm_base_modem_at_command_finish (self, res, &error); + if (!response) + mm_obj_dbg (self, "couldn't load context %d auth settings: %s", ctx->cid, error->message); + else if (!mm_cinterion_parse_sgauth_response (response, ctx->cid, &auth, &username, &error)) + mm_obj_dbg (self, "couldn't parse context %d auth settings: %s", ctx->cid, error->message); + else { + mm_bearer_properties_set_allowed_auth (ctx->properties, auth); + mm_bearer_properties_set_user (ctx->properties, username); } /* Go to next step */ diff --git a/plugins/cinterion/mm-modem-helpers-cinterion.c b/plugins/cinterion/mm-modem-helpers-cinterion.c index bb449505..7bdc8c9e 100644 --- a/plugins/cinterion/mm-modem-helpers-cinterion.c +++ b/plugins/cinterion/mm-modem-helpers-cinterion.c @@ -847,6 +847,51 @@ mm_cinterion_parse_swwan_response (const gchar *response, } /*****************************************************************************/ +/* ^SGAUTH response parser */ + +/* at^sgauth? + * ^SGAUTH: 1,2,"vf" + * ^SGAUTH: 3,0,"" + * ^SGAUTH: 4,0 + * + * OK + */ + +gboolean +mm_cinterion_parse_sgauth_response (const gchar *response, + guint cid, + MMBearerAllowedAuth *out_auth, + gchar **out_username, + GError **error) +{ + g_autoptr(GRegex) r = NULL; + g_autoptr(GMatchInfo) match_info = NULL; + + r = g_regex_new ("\\^SGAUTH:\\s*(\\d+),(\\d+),?\"?([a-zA-Z0-9_-]+)?\"?", 0, 0, NULL); + g_assert (r != NULL); + + g_regex_match_full (r, response, strlen (response), 0, 0, &match_info, NULL); + while (g_match_info_matches (match_info)) { + guint sgauth_cid = 0; + + if (mm_get_uint_from_match_info (match_info, 1, &sgauth_cid) && + (sgauth_cid == cid)) { + guint cinterion_auth_type = 0; + + mm_get_uint_from_match_info (match_info, 2, &cinterion_auth_type); + *out_auth = mm_auth_type_from_cinterion_auth_type (cinterion_auth_type); + *out_username = mm_get_string_unquoted_from_match_info (match_info, 3); + return TRUE; + } + g_match_info_next (match_info, NULL); + } + + g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_NOT_FOUND, + "Auth settings for context %u not found", cid); + return FALSE; +} + +/*****************************************************************************/ /* ^SMONG response parser */ static MMModemAccessTechnology diff --git a/plugins/cinterion/mm-modem-helpers-cinterion.h b/plugins/cinterion/mm-modem-helpers-cinterion.h index 35221a66..533c50de 100644 --- a/plugins/cinterion/mm-modem-helpers-cinterion.h +++ b/plugins/cinterion/mm-modem-helpers-cinterion.h @@ -112,6 +112,15 @@ MMBearerConnectionStatus mm_cinterion_parse_swwan_response (const gchar *respon GError **error); /*****************************************************************************/ +/* ^SGAUTH response parser */ + +gboolean mm_cinterion_parse_sgauth_response (const gchar *response, + guint cid, + MMBearerAllowedAuth *out_auth, + gchar **out_username, + GError **error); + +/*****************************************************************************/ /* ^SMONG response parser */ gboolean mm_cinterion_parse_smong_response (const gchar *response, diff --git a/plugins/cinterion/tests/test-modem-helpers-cinterion.c b/plugins/cinterion/tests/test-modem-helpers-cinterion.c index 14ce4c38..bb27c6e7 100644 --- a/plugins/cinterion/tests/test-modem-helpers-cinterion.c +++ b/plugins/cinterion/tests/test-modem-helpers-cinterion.c @@ -1663,6 +1663,56 @@ test_provcfg_response (void) } /*****************************************************************************/ +/* Test ^SGAUTH responses */ + +static void +test_sgauth_response (void) +{ + gboolean result; + MMBearerAllowedAuth auth = MM_BEARER_ALLOWED_AUTH_UNKNOWN; + gchar *username = NULL; + GError *error = NULL; + + const gchar *response = + "^SGAUTH: 1,2,\"vf\"\r\n" + "^SGAUTH: 2,1,\"\"\r\n" + "^SGAUTH: 3,0\r\n"; + + /* CID 1 */ + result = mm_cinterion_parse_sgauth_response (response, 1, &auth, &username, &error); + g_assert_no_error (error); + g_assert (result); + g_assert_cmpuint (auth, ==, MM_BEARER_ALLOWED_AUTH_CHAP); + g_assert_cmpstr (username, ==, "vf"); + + auth = MM_BEARER_ALLOWED_AUTH_UNKNOWN; + g_clear_pointer (&username, g_free); + + /* CID 2 */ + result = mm_cinterion_parse_sgauth_response (response, 2, &auth, &username, &error); + g_assert_no_error (error); + g_assert (result); + g_assert_cmpuint (auth, ==, MM_BEARER_ALLOWED_AUTH_PAP); + g_assert_null (username); + + auth = MM_BEARER_ALLOWED_AUTH_UNKNOWN; + + /* CID 3 */ + result = mm_cinterion_parse_sgauth_response (response, 3, &auth, &username, &error); + g_assert_no_error (error); + g_assert (result); + g_assert_cmpuint (auth, ==, MM_BEARER_ALLOWED_AUTH_NONE); + g_assert_null (username); + + auth = MM_BEARER_ALLOWED_AUTH_UNKNOWN; + + /* CID 4 */ + result = mm_cinterion_parse_sgauth_response (response, 4, &auth, &username, &error); + g_assert_error (error, MM_CORE_ERROR, MM_CORE_ERROR_NOT_FOUND); + g_assert (!result); +} + +/*****************************************************************************/ int main (int argc, char **argv) { @@ -1695,6 +1745,7 @@ int main (int argc, char **argv) g_test_add_func ("/MM/cinterion/smoni/query_response", test_smoni_response); g_test_add_func ("/MM/cinterion/smoni/query_response_to_signal", test_smoni_response_to_signal); g_test_add_func ("/MM/cinterion/scfg/provcfg", test_provcfg_response); + g_test_add_func ("/MM/cinterion/sgauth", test_sgauth_response); return g_test_run (); } |