From 3988f53d2e9b9aae7eb4f6fb8c46dca55373ba6f Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Tue, 5 Jan 2010 14:57:30 -0600 Subject: gsm: correctly parse Nokia N80 +COPS response --- src/mm-modem-helpers.c | 108 ++++++++++++++++++++++++++++++++--------- src/tests/test-modem-helpers.c | 46 ++++++------------ 2 files changed, 100 insertions(+), 54 deletions(-) (limited to 'src') diff --git a/src/mm-modem-helpers.c b/src/mm-modem-helpers.c index 809eab9a..dbf04d01 100644 --- a/src/mm-modem-helpers.c +++ b/src/mm-modem-helpers.c @@ -16,10 +16,40 @@ #include #include +#include #include "mm-errors.h" #include "mm-modem-helpers.h" +static void +save_scan_value (GHashTable *hash, const char *key, GMatchInfo *info, guint32 num) +{ + char *quoted; + size_t len; + + g_return_if_fail (info != NULL); + + quoted = g_match_info_fetch (info, num); + if (!quoted) + return; + + len = strlen (quoted); + + /* Unquote the item if needed */ + if ((len >= 2) && (quoted[0] == '"') && (quoted[len - 1] == '"')) { + quoted[0] = ' '; + quoted[len - 1] = ' '; + quoted = g_strstrip (quoted); + } + + if (!strlen (quoted)) { + g_free (quoted); + return; + } + + g_hash_table_insert (hash, g_strdup (key), quoted); +} + GPtrArray * mm_gsm_parse_scan_response (const char *reply, GError **error) { @@ -44,20 +74,20 @@ mm_gsm_parse_scan_response (const char *reply, GError **error) reply = strstr (reply, "+COPS: ") + 7; /* Cell access technology (GSM, UTRAN, etc) got added later and not all - * modems implement it. Some modesm have quirks that make it hard to - * use one regular experession for matching both pre-UMTS and UMTS - * responses. So try UMTS-format first and fall back to pre-UMTS if - * we get no UMTS-formst matches. - */ + * modems implement it. Some modesm have quirks that make it hard to + * use one regular experession for matching both pre-UMTS and UMTS + * responses. So try UMTS-format first and fall back to pre-UMTS if + * we get no UMTS-formst matches. + */ /* Quirk: Sony-Ericsson TM-506 sometimes includes a stray ')' like so, - * which is what makes it hard to match both pre-UMTS and UMTS in - * the same regex: - * - * +COPS: (2,"","T-Mobile","31026",0),(1,"AT&T","AT&T","310410"),0) - */ + * which is what makes it hard to match both pre-UMTS and UMTS in + * the same regex: + * + * +COPS: (2,"","T-Mobile","31026",0),(1,"AT&T","AT&T","310410"),0) + */ - r = g_regex_new ("\\((\\d),\"(.*)\",\"(.*)\",\"(.*)\"[\\)]?,(\\d)\\)", G_REGEX_UNGREEDY, 0, &err); + r = g_regex_new ("\\((\\d),([^,\\)]*),([^,\\)]*),([^,\\)]*)[\\)]?,(\\d)\\)", G_REGEX_UNGREEDY, 0, NULL); if (err) { g_error ("Invalid regular expression: %s", err->message); g_error_free (err); @@ -76,13 +106,19 @@ mm_gsm_parse_scan_response (const char *reply, GError **error) } /* Pre-UMTS format doesn't include the cell access technology after - * the numeric operator element. - * - * Ex: Motorola C-series (BUSlink SCWi275u) like so: - * - * +COPS: (2,"T-Mobile","","310260"),(0,"Cingular Wireless","","310410") - */ - r = g_regex_new ("\\((\\d),\"(.*)\",\"(.*)\",\"(.*)\"\\)", G_REGEX_UNGREEDY, 0, &err); + * the numeric operator element. + * + * Ex: Motorola C-series (BUSlink SCWi275u) like so: + * + * +COPS: (2,"T-Mobile","","310260"),(0,"Cingular Wireless","","310410") + */ + + /* Quirk: Some Nokia phones (N80) don't send the quotes for empty values: + * + * +COPS: (2,"T - Mobile",,"31026"),(1,"Einstein PCS",,"31064"),(1,"Cingular",,"31041"),,(0,1,3),(0,2) + */ + + r = g_regex_new ("\\((\\d),([^,\\)]*),([^,\\)]*),([^\\)]*)\\)", G_REGEX_UNGREEDY, 0, NULL); if (err) { g_error ("Invalid regular expression: %s", err->message); g_error_free (err); @@ -101,24 +137,48 @@ mm_gsm_parse_scan_response (const char *reply, GError **error) while (g_match_info_matches (match_info)) { GHashTable *hash; char *access_tech = NULL; + const char *tmp; + gboolean valid = FALSE; hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); - g_hash_table_insert (hash, g_strdup (MM_SCAN_TAG_STATUS), g_match_info_fetch (match_info, 1)); - g_hash_table_insert (hash, g_strdup (MM_SCAN_TAG_OPER_LONG), g_match_info_fetch (match_info, 2)); - g_hash_table_insert (hash, g_strdup (MM_SCAN_TAG_OPER_SHORT), g_match_info_fetch (match_info, 3)); - g_hash_table_insert (hash, g_strdup (MM_SCAN_TAG_OPER_NUM), g_match_info_fetch (match_info, 4)); + + save_scan_value (hash, MM_SCAN_TAG_STATUS, match_info, 1); + save_scan_value (hash, MM_SCAN_TAG_OPER_LONG, match_info, 2); + save_scan_value (hash, MM_SCAN_TAG_OPER_SHORT, match_info, 3); + save_scan_value (hash, MM_SCAN_TAG_OPER_NUM, match_info, 4); /* Only try for access technology with UMTS-format matches */ if (umts_format) access_tech = g_match_info_fetch (match_info, 5); if (access_tech && (strlen (access_tech) == 1)) { /* Recognized access technologies are between '0' and '6' inclusive... */ - if ((access_tech[0] >= 48) && (access_tech[0] <= 54)) + if ((access_tech[0] >= '0') && (access_tech[0] <= '6')) g_hash_table_insert (hash, g_strdup (MM_SCAN_TAG_ACCESS_TECH), access_tech); } else g_free (access_tech); - g_ptr_array_add (results, hash); + /* If the operator number isn't valid (ie, at least 5 digits), + * ignore the scan result; it's probably the parameter stuff at the + * end of the +COPS response. The regex will sometimes catch this + * but there's no good way to ignore it. + */ + tmp = g_hash_table_lookup (hash, MM_SCAN_TAG_OPER_NUM); + if (tmp && (strlen (tmp) >= 5)) { + valid = TRUE; + while (*tmp) { + if (!isdigit (*tmp) && (*tmp != '-')) { + valid = FALSE; + break; + } + tmp++; + } + + if (valid) + g_ptr_array_add (results, hash); + } + + if (!valid) + g_hash_table_destroy (hash); g_match_info_next (match_info, NULL); } diff --git a/src/tests/test-modem-helpers.c b/src/tests/test-modem-helpers.c index e2cbf103..8ae27e54 100644 --- a/src/tests/test-modem-helpers.c +++ b/src/tests/test-modem-helpers.c @@ -76,11 +76,9 @@ test_results (const char *desc, g_assert (value == NULL); value = g_hash_table_lookup (entry, MM_SCAN_TAG_OPER_NUM); - if (expected->oper_num) { - g_assert (value); - g_assert (strcmp (value, expected->oper_num) == 0); - } else - g_assert (value == NULL); + g_assert (expected->oper_num); + g_assert (value); + g_assert (strcmp (value, expected->oper_num) == 0); value = g_hash_table_lookup (entry, MM_SCAN_TAG_ACCESS_TECH); if (expected->tech) { @@ -98,7 +96,7 @@ test_cops_response_tm506 (void *f, gpointer d) { const char *reply = "+COPS: (2,\"\",\"T-Mobile\",\"31026\",0),(2,\"T - Mobile\",\"T - Mobile\",\"310260\"),2),(1,\"AT&T\",\"AT&T\",\"310410\"),0)"; static OperEntry expected[] = { - { "2", "", "T-Mobile", "31026", "0" }, + { "2", NULL, "T-Mobile", "31026", "0" }, { "2", "T - Mobile", "T - Mobile", "310260", "2" }, { "1", "AT&T", "AT&T", "310410", "0" } }; @@ -175,8 +173,8 @@ test_cops_response_motoc (void *f, gpointer d) { const char *reply = "+COPS: (2,\"T-Mobile\",\"\",\"310260\"),(0,\"Cingular Wireless\",\"\",\"310410\")"; static OperEntry expected[] = { - { "2", "T-Mobile", "", "310260", NULL }, - { "0", "Cingular Wireless", "", "310410", NULL }, + { "2", "T-Mobile", NULL, "310260", NULL }, + { "0", "Cingular Wireless", NULL, "310410", NULL }, }; test_results ("BUSlink SCWi275u (Motorola C-series)", reply, &expected[0], ARRAY_LEN (expected)); @@ -200,7 +198,7 @@ test_cops_response_mf627b (void *f, gpointer d) const char *reply = "+COPS: (2,\"AT&Tp\",\"AT&T@\",\"310410\",0),(3,\"\",\"\",\"31026\",0),"; static OperEntry expected[] = { { "2", "AT&Tp", "AT&T@", "310410", "0" }, - { "3", "", "", "31026", "0" }, + { "3", NULL, NULL, "31026", "0" }, }; test_results ("ZTE MF627 (B)", reply, &expected[0], ARRAY_LEN (expected)); @@ -223,7 +221,7 @@ test_cops_response_mercury (void *f, gpointer d) { const char *reply = "+COPS: (2,\"\",\"\",\"310410\",2),(1,\"AT&T\",\"AT&T\",\"310410\",0),(1,\"T-Mobile\",\"TMO\",\"31026\",0),,(0,1,2,3,4),(0,1,2)"; static OperEntry expected[] = { - { "2", "", "", "310410", "2" }, + { "2", NULL, NULL, "310410", "2" }, { "1", "AT&T", "AT&T", "310410", "0" }, { "1", "T-Mobile", "TMO", "31026", "0" }, }; @@ -236,9 +234,9 @@ test_cops_response_quicksilver (void *f, gpointer d) { const char *reply = "+COPS: (2,\"AT&T\",\"\",\"310410\",0),(2,\"\",\"\",\"3104100\",2),(1,\"AT&T\",\"\",\"310260\",0),,(0-4),(0-2)"; static OperEntry expected[] = { - { "2", "AT&T", "", "310410", "0" }, - { "2", "", "", "3104100", "2" }, - { "1", "AT&T", "", "310260", "0" }, + { "2", "AT&T", NULL, "310410", "0" }, + { "2", NULL, NULL, "3104100", "2" }, + { "1", "AT&T", NULL, "310260", "0" }, }; test_results ("Option AT&T Quicksilver", reply, &expected[0], ARRAY_LEN (expected)); @@ -309,29 +307,19 @@ test_cops_response_mc8775 (void *f, gpointer d) test_results ("Sierra MC8775", reply, &expected[0], ARRAY_LEN (expected)); } -#if 0 static void test_cops_response_n80 (void *f, gpointer d) { const char *reply = "+COPS: (2,\"T - Mobile\",,\"31026\"),(1,\"Einstein PCS\",,\"31064\"),(1,\"Cingular\",,\"31041\"),,(0,1,3),(0,2)"; static OperEntry expected[] = { - { "2", "T - Mobile", "", "31026", NULL }, - { "1", "Einstein PCS", "", "31064", NULL }, - { "1", "Cingular", "", "31041", NULL }, + { "2", "T - Mobile", NULL, "31026", NULL }, + { "1", "Einstein PCS", NULL, "31064", NULL }, + { "1", "Cingular", NULL, "31041", NULL }, }; - GError *error = NULL; - GPtrArray *results; - results = mm_gsm_parse_scan_response (reply, &error); - g_assert (results); - g_assert (error == NULL); - - g_assert (results->len == ARRAY_LEN (expected)); - test_results ("Nokia N80", results, &expected[0]); - - mm_gsm_destroy_scan_data (results); + test_results ("Nokia N80", reply, &expected[0], ARRAY_LEN (expected)); } -#endif + typedef void (*TCFunc)(void); @@ -362,9 +350,7 @@ int main (int argc, char **argv) g_test_suite_add (suite, TESTCASE (test_cops_response_f3507g, NULL)); g_test_suite_add (suite, TESTCASE (test_cops_response_f3607gw, NULL)); g_test_suite_add (suite, TESTCASE (test_cops_response_mc8775, NULL)); -#if 0 g_test_suite_add (suite, TESTCASE (test_cops_response_n80, NULL)); -#endif return g_test_run (); } -- cgit v1.2.3-70-g09d2