modem-helpers: new +CNUM response parser and tests

This commit is contained in:
Aleksander Morgado
2012-03-06 13:09:34 +01:00
parent 4a52a19c41
commit 51c28d0f79
3 changed files with 145 additions and 0 deletions

View File

@@ -1820,3 +1820,40 @@ mm_count_bits_set (gulong number)
number &= number - 1;
return c;
}
GStrv
mm_3gpp_parse_cnum_response (const gchar *reply,
GError **error)
{
GArray *array = NULL;
GRegex *r;
GMatchInfo *match_info;
/* Empty strings also return NULL list */
if (!reply || !reply[0])
return NULL;
r = g_regex_new ("\\+CNUM:\\s*\"?\\S*\"?,\"(\\S+)\",\\d", G_REGEX_UNGREEDY, 0, NULL);
g_assert (r != NULL);
g_regex_match (r, reply, 0, &match_info);
while (g_match_info_matches (match_info)) {
gchar *number;
number = g_match_info_fetch (match_info, 1);
if (number && number[0]) {
if (!array)
array = g_array_new (TRUE, TRUE, sizeof (gchar *));
g_array_append_val (array, number);
} else
g_free (number);
g_match_info_next (match_info, NULL);
}
g_match_info_free (match_info);
g_regex_unref (r);
return (array ? (GStrv) g_array_free (array, FALSE) : NULL);
}

View File

@@ -19,6 +19,7 @@
#include <ModemManager.h>
#include "glib-object.h"
#include "mm-charsets.h"
#define MM_MODEM_CAPABILITY_3GPP_LTE \
@@ -114,6 +115,8 @@ gboolean mm_3gpp_parse_clck_response (const gchar *reply,
gchar *mm_3gpp_parse_operator (const gchar *reply,
MMModemCharset cur_charset);
GStrv mm_3gpp_parse_cnum_response (const gchar *reply,
GError **error);
MMModemAccessTechnology mm_3gpp_string_to_access_tech (const gchar *string);

View File

@@ -1307,6 +1307,105 @@ test_cpms_response_cinterion (void *f, gpointer d)
g_assert (is_storage_supported (mem3, MM_SMS_STORAGE_MT));
}
/*****************************************************************************/
/* Test CNUM responses */
static void
test_cnum_results (const gchar *desc,
const gchar *reply,
const GStrv expected)
{
GStrv results;
GError *error = NULL;
guint i;
g_print ("\nTesting +CNUM response (%s)...\n", desc);
results = mm_3gpp_parse_cnum_response (reply, &error);
g_assert (results);
g_assert_no_error (error);
g_assert_cmpuint (g_strv_length (results), ==, g_strv_length (expected));
for (i = 0; results[i]; i++) {
guint j;
for (j = 0; expected[j]; j++) {
if (g_str_equal (results[i], expected[j]))
break;
}
/* Ensure the result is found in the expected list */
g_assert (expected[j]);
}
g_strfreev (results);
}
static void
test_cnum_response_generic (void *f, gpointer d)
{
const gchar *reply = "+CNUM: \"something\",\"1234567890\",161";
const gchar *expected[] = {
"1234567890",
NULL
};
test_cnum_results ("Generic", reply, (GStrv)expected);
}
static void
test_cnum_response_generic_without_detail (void *f, gpointer d)
{
const gchar *reply = "+CNUM: ,\"1234567890\",161";
const gchar *expected[] = {
"1234567890",
NULL
};
test_cnum_results ("Generic, without detail", reply, (GStrv)expected);
}
static void
test_cnum_response_generic_detail_unquoted (void *f, gpointer d)
{
const gchar *reply = "+CNUM: something,\"1234567890\",161";
const gchar *expected[] = {
"1234567890",
NULL
};
test_cnum_results ("Generic, detail unquoted", reply, (GStrv)expected);
}
static void
test_cnum_response_generic_international_number (void *f, gpointer d)
{
const gchar *reply = "+CNUM: something,\"+34600000001\",145";
const gchar *expected[] = {
"+34600000001",
NULL
};
test_cnum_results ("Generic, international number", reply, (GStrv)expected);
}
static void
test_cnum_response_generic_multiple_numbers (void *f, gpointer d)
{
const gchar *reply =
"+CNUM: something,\"+34600000001\",145\r\n"
"+CNUM: ,\"+34600000002\",145\r\n"
"+CNUM: \"another\",\"1234567890\",161";
const gchar *expected[] = {
"+34600000001",
"+34600000002",
"1234567890",
NULL
};
test_cnum_results ("Generic, multiple numbers", reply, (GStrv)expected);
}
/*****************************************************************************/
void
@@ -1408,6 +1507,12 @@ int main (int argc, char **argv)
g_test_suite_add (suite, TESTCASE (test_cgdcont_response_nokia, NULL));
g_test_suite_add (suite, TESTCASE (test_cnum_response_generic, NULL));
g_test_suite_add (suite, TESTCASE (test_cnum_response_generic_without_detail, NULL));
g_test_suite_add (suite, TESTCASE (test_cnum_response_generic_detail_unquoted, NULL));
g_test_suite_add (suite, TESTCASE (test_cnum_response_generic_international_number, NULL));
g_test_suite_add (suite, TESTCASE (test_cnum_response_generic_multiple_numbers, NULL));
result = g_test_run ();
reg_test_data_free (reg_data);