cinterion: setup SGAUTH response parser as a helper method

This commit is contained in:
Aleksander Morgado
2020-09-09 10:55:33 +02:00
parent eb338c967f
commit f280573f6d
4 changed files with 116 additions and 33 deletions

View File

@@ -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 */

View File

@@ -846,6 +846,51 @@ mm_cinterion_parse_swwan_response (const gchar *response,
return status;
}
/*****************************************************************************/
/* ^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 */

View File

@@ -111,6 +111,15 @@ MMBearerConnectionStatus mm_cinterion_parse_swwan_response (const gchar *respon
gpointer log_object,
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 */

View File

@@ -1662,6 +1662,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 ();
}