huawei: handle unquoted strings in ^SYSINFOEX response
The original sysinfoex_parse() in MMBroadbandModemHuawei does not handle unquoted <sysmode_name> and <submode_name> fields in ^SYSINFOEX responses, which are sen on some Huawei modems (e.g. E303). This patch moves the ^SYSINFOEX parsing code to mm-modem-helpers-huawei.c, fixes the regex for handling unquoted strings in ^SYSINFOEX responses, and adds unit tests.
This commit is contained in:

committed by
Aleksander Morgado

parent
49d4163a2b
commit
18053540ea
@@ -113,66 +113,6 @@ struct _MMBroadbandModemHuaweiPrivate {
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
static gboolean
|
||||
sysinfoex_parse (const char *reply,
|
||||
guint *out_srv_status,
|
||||
guint *out_srv_domain,
|
||||
guint *out_roam_status,
|
||||
guint *out_sim_state,
|
||||
guint *out_sys_mode,
|
||||
guint *out_sys_submode,
|
||||
GError **error)
|
||||
{
|
||||
gboolean matched;
|
||||
GRegex *r;
|
||||
GMatchInfo *match_info = NULL;
|
||||
GError *match_error = NULL;
|
||||
|
||||
g_assert (out_srv_status != NULL);
|
||||
g_assert (out_srv_domain != NULL);
|
||||
g_assert (out_roam_status != NULL);
|
||||
g_assert (out_sim_state != NULL);
|
||||
g_assert (out_sys_mode != NULL);
|
||||
g_assert (out_sys_submode != NULL);
|
||||
|
||||
/* Format:
|
||||
*
|
||||
* ^SYSINFOEX: <srv_status>,<srv_domain>,<roam_status>,<sim_state>,<reserved>,<sysmode>,<sysmode_name>,<submode>,<submode_name>
|
||||
*/
|
||||
|
||||
/* ^SYSINFOEX:2,3,0,1,,3,"WCDMA",41,"HSPA+" */
|
||||
|
||||
r = g_regex_new ("\\^SYSINFOEX:\\s*(\\d+),(\\d+),(\\d+),(\\d+),?(\\d*),(\\d+),\"(.*)\",(\\d+),\"(.*)\"$", 0, 0, NULL);
|
||||
g_assert (r != NULL);
|
||||
|
||||
matched = g_regex_match_full (r, reply, -1, 0, 0, &match_info, &match_error);
|
||||
if (!matched) {
|
||||
if (match_error) {
|
||||
g_propagate_error (error, match_error);
|
||||
g_prefix_error (error, "Could not parse ^SYSINFOEX results: ");
|
||||
} else {
|
||||
g_set_error_literal (error,
|
||||
MM_CORE_ERROR,
|
||||
MM_CORE_ERROR_FAILED,
|
||||
"Couldn't match ^SYSINFOEX reply");
|
||||
}
|
||||
} else {
|
||||
mm_get_uint_from_match_info (match_info, 1, out_srv_status);
|
||||
mm_get_uint_from_match_info (match_info, 2, out_srv_domain);
|
||||
mm_get_uint_from_match_info (match_info, 3, out_roam_status);
|
||||
mm_get_uint_from_match_info (match_info, 4, out_sim_state);
|
||||
|
||||
/* We just ignore the sysmode and submode name strings */
|
||||
mm_get_uint_from_match_info (match_info, 6, out_sys_mode);
|
||||
mm_get_uint_from_match_info (match_info, 8, out_sys_submode);
|
||||
}
|
||||
|
||||
if (match_info)
|
||||
g_match_info_free (match_info);
|
||||
g_regex_unref (r);
|
||||
return matched;
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
gboolean extended;
|
||||
guint srv_status;
|
||||
@@ -314,14 +254,14 @@ run_sysinfoex_ready (MMBaseModem *_self,
|
||||
|
||||
result = g_new0 (SysinfoResult, 1);
|
||||
result->extended = TRUE;
|
||||
if (!sysinfoex_parse (response,
|
||||
&result->srv_status,
|
||||
&result->srv_domain,
|
||||
&result->roam_status,
|
||||
&result->sim_state,
|
||||
&result->sys_mode,
|
||||
&result->sys_submode,
|
||||
&error)) {
|
||||
if (!mm_huawei_parse_sysinfoex_response (response,
|
||||
&result->srv_status,
|
||||
&result->srv_domain,
|
||||
&result->roam_status,
|
||||
&result->sim_state,
|
||||
&result->sys_mode,
|
||||
&result->sys_submode,
|
||||
&error)) {
|
||||
mm_dbg ("^SYSINFOEX parsing failed: %s", error->message);
|
||||
g_simple_async_result_take_error (simple, error);
|
||||
g_simple_async_result_complete (simple);
|
||||
|
@@ -179,3 +179,68 @@ mm_huawei_parse_sysinfo_response (const char *reply,
|
||||
g_regex_unref (r);
|
||||
return matched;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
/* ^SYSINFOEX response parser */
|
||||
|
||||
gboolean
|
||||
mm_huawei_parse_sysinfoex_response (const char *reply,
|
||||
guint *out_srv_status,
|
||||
guint *out_srv_domain,
|
||||
guint *out_roam_status,
|
||||
guint *out_sim_state,
|
||||
guint *out_sys_mode,
|
||||
guint *out_sys_submode,
|
||||
GError **error)
|
||||
{
|
||||
gboolean matched;
|
||||
GRegex *r;
|
||||
GMatchInfo *match_info = NULL;
|
||||
GError *match_error = NULL;
|
||||
|
||||
g_assert (out_srv_status != NULL);
|
||||
g_assert (out_srv_domain != NULL);
|
||||
g_assert (out_roam_status != NULL);
|
||||
g_assert (out_sim_state != NULL);
|
||||
g_assert (out_sys_mode != NULL);
|
||||
g_assert (out_sys_submode != NULL);
|
||||
|
||||
/* Format:
|
||||
*
|
||||
* ^SYSINFOEX: <srv_status>,<srv_domain>,<roam_status>,<sim_state>,<reserved>,<sysmode>,<sysmode_name>,<submode>,<submode_name>
|
||||
*
|
||||
* <sysmode_name> and <submode_name> may not be quoted on some Huawei modems (e.g. E303).
|
||||
*/
|
||||
|
||||
/* ^SYSINFOEX:2,3,0,1,,3,"WCDMA",41,"HSPA+" */
|
||||
|
||||
r = g_regex_new ("\\^SYSINFOEX:\\s*(\\d+),(\\d+),(\\d+),(\\d+),?(\\d*),(\\d+),\"?([^\"]*)\"?,(\\d+),\"?([^\"]*)\"?$", 0, 0, NULL);
|
||||
g_assert (r != NULL);
|
||||
|
||||
matched = g_regex_match_full (r, reply, -1, 0, 0, &match_info, &match_error);
|
||||
if (!matched) {
|
||||
if (match_error) {
|
||||
g_propagate_error (error, match_error);
|
||||
g_prefix_error (error, "Could not parse ^SYSINFOEX results: ");
|
||||
} else {
|
||||
g_set_error_literal (error,
|
||||
MM_CORE_ERROR,
|
||||
MM_CORE_ERROR_FAILED,
|
||||
"Couldn't match ^SYSINFOEX reply");
|
||||
}
|
||||
} else {
|
||||
mm_get_uint_from_match_info (match_info, 1, out_srv_status);
|
||||
mm_get_uint_from_match_info (match_info, 2, out_srv_domain);
|
||||
mm_get_uint_from_match_info (match_info, 3, out_roam_status);
|
||||
mm_get_uint_from_match_info (match_info, 4, out_sim_state);
|
||||
|
||||
/* We just ignore the sysmode and submode name strings */
|
||||
mm_get_uint_from_match_info (match_info, 6, out_sys_mode);
|
||||
mm_get_uint_from_match_info (match_info, 8, out_sys_submode);
|
||||
}
|
||||
|
||||
if (match_info)
|
||||
g_match_info_free (match_info);
|
||||
g_regex_unref (r);
|
||||
return matched;
|
||||
}
|
||||
|
@@ -38,4 +38,14 @@ gboolean mm_huawei_parse_sysinfo_response (const char *reply,
|
||||
guint *out_sys_submode,
|
||||
GError **error);
|
||||
|
||||
/* ^SYSINFOEX response parser */
|
||||
gboolean mm_huawei_parse_sysinfoex_response (const char *reply,
|
||||
guint *out_srv_status,
|
||||
guint *out_srv_domain,
|
||||
guint *out_roam_status,
|
||||
guint *out_sim_state,
|
||||
guint *out_sys_mode,
|
||||
guint *out_sys_submode,
|
||||
GError **error);
|
||||
|
||||
#endif /* MM_MODEM_HELPERS_HUAWEI_H */
|
||||
|
@@ -148,9 +148,9 @@ static const SysinfoTest sysinfo_tests[] = {
|
||||
{ "^SYSINFO:2,4,5,3,1,,", 2, 4, 5, 3, 1, FALSE, 0 },
|
||||
{ "^SYSINFO:2,4,5,3,1,6", 2, 4, 5, 3, 1, FALSE, 6 },
|
||||
{ "^SYSINFO:2,4,5,3,1,6,", 2, 4, 5, 3, 1, FALSE, 6 },
|
||||
{ "^SYSINFO:2,4,5,3,1,,3", 2, 4, 5, 3, 1, TRUE, 3 },
|
||||
{ "^SYSINFO:2,4,5,3,1,0,3", 2, 4, 5, 3, 1, TRUE, 3 },
|
||||
{ "^SYSINFO: 2,4,5,3,1,0,3", 2, 4, 5, 3, 1, TRUE, 3 },
|
||||
{ "^SYSINFO:2,4,5,3,1,,6", 2, 4, 5, 3, 1, TRUE, 6 },
|
||||
{ "^SYSINFO:2,4,5,3,1,0,6", 2, 4, 5, 3, 1, TRUE, 6 },
|
||||
{ "^SYSINFO: 2,4,5,3,1,0,6", 2, 4, 5, 3, 1, TRUE, 6 },
|
||||
{ NULL, 0, 0, 0, 0, 0, FALSE, 0 }
|
||||
};
|
||||
|
||||
@@ -191,6 +191,59 @@ test_sysinfo (void)
|
||||
}
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
/* Test ^SYSINFOEX responses */
|
||||
|
||||
typedef struct {
|
||||
const gchar *str;
|
||||
guint expected_srv_status;
|
||||
guint expected_srv_domain;
|
||||
guint expected_roam_status;
|
||||
guint expected_sim_state;
|
||||
guint expected_sys_mode;
|
||||
guint expected_sys_submode;
|
||||
} SysinfoexTest;
|
||||
|
||||
static const SysinfoexTest sysinfoex_tests[] = {
|
||||
{ "^SYSINFOEX:2,4,5,1,,3,WCDMA,41,HSPA+", 2, 4, 5, 1, 3, 41 },
|
||||
{ "^SYSINFOEX:2,4,5,1,,3,\"WCDMA\",41,\"HSPA+\"", 2, 4, 5, 1, 3, 41 },
|
||||
{ "^SYSINFOEX: 2,4,5,1,0,3,\"WCDMA\",41,\"HSPA+\"", 2, 4, 5, 1, 3, 41 },
|
||||
{ NULL, 0, 0, 0, 0, 0, 0 }
|
||||
};
|
||||
|
||||
static void
|
||||
test_sysinfoex (void)
|
||||
{
|
||||
guint i;
|
||||
|
||||
for (i = 0; sysinfoex_tests[i].str; i++) {
|
||||
GError *error = NULL;
|
||||
guint srv_status = 0;
|
||||
guint srv_domain = 0;
|
||||
guint roam_status = 0;
|
||||
guint sim_state = 0;
|
||||
guint sys_mode = 0;
|
||||
guint sys_submode = 0;
|
||||
|
||||
g_assert (mm_huawei_parse_sysinfoex_response (sysinfoex_tests[i].str,
|
||||
&srv_status,
|
||||
&srv_domain,
|
||||
&roam_status,
|
||||
&sim_state,
|
||||
&sys_mode,
|
||||
&sys_submode,
|
||||
&error) == TRUE);
|
||||
g_assert_no_error (error);
|
||||
|
||||
g_assert (srv_status == sysinfoex_tests[i].expected_srv_status);
|
||||
g_assert (srv_domain == sysinfoex_tests[i].expected_srv_domain);
|
||||
g_assert (roam_status == sysinfoex_tests[i].expected_roam_status);
|
||||
g_assert (sim_state == sysinfoex_tests[i].expected_sim_state);
|
||||
g_assert (sys_mode == sysinfoex_tests[i].expected_sys_mode);
|
||||
g_assert (sys_submode == sysinfoex_tests[i].expected_sys_submode);
|
||||
}
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
int main (int argc, char **argv)
|
||||
@@ -202,6 +255,7 @@ int main (int argc, char **argv)
|
||||
|
||||
g_test_add_func ("/MM/huawei/ndisstatqry", test_ndisstatqry);
|
||||
g_test_add_func ("/MM/huawei/sysinfo", test_sysinfo);
|
||||
g_test_add_func ("/MM/huawei/sysinfoex", test_sysinfoex);
|
||||
|
||||
return g_test_run ();
|
||||
}
|
||||
|
Reference in New Issue
Block a user