gsm: correctly parse Nokia N80 +COPS response

This commit is contained in:
Dan Williams
2010-01-05 14:57:30 -06:00
parent 216e49f8d8
commit 3988f53d2e
2 changed files with 100 additions and 54 deletions

View File

@@ -16,10 +16,40 @@
#include <glib.h>
#include <string.h>
#include <ctype.h>
#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);
}