aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksander Morgado <aleksander@lanedo.com>2012-08-08 10:12:43 +0200
committerAleksander Morgado <aleksander@lanedo.com>2012-08-23 19:10:54 +0200
commit75b63dcfac7b4589243b9ee9bdd833a2aa2c8e4d (patch)
tree73c9b7c06e222cd95a52b700ee1c1cbe0a6ad18e
parenta95e5dd3244e72cecd518b55bc2ef872963b15d1 (diff)
modem-helpers: new method to validate and/or parse MCC/MNC operator ID string
-rw-r--r--src/mm-modem-helpers.c60
-rw-r--r--src/mm-modem-helpers.h5
-rw-r--r--src/tests/test-modem-helpers.c73
3 files changed, 138 insertions, 0 deletions
diff --git a/src/mm-modem-helpers.c b/src/mm-modem-helpers.c
index 3eb953e5..4c93045b 100644
--- a/src/mm-modem-helpers.c
+++ b/src/mm-modem-helpers.c
@@ -1466,6 +1466,66 @@ mm_3gpp_get_ip_family_from_pdp_type (const gchar *pdp_type)
/*************************************************************************/
gboolean
+mm_3gpp_parse_operator_id (const gchar *operator_id,
+ guint16 *mcc,
+ guint16 *mnc,
+ GError **error)
+{
+ guint len;
+ guint i;
+ gchar aux[4];
+ guint16 tmp;
+
+ g_assert (operator_id != NULL);
+
+ len = strlen (operator_id);
+ if (len != 5 && len != 6) {
+ g_set_error (error,
+ MM_CORE_ERROR,
+ MM_CORE_ERROR_FAILED,
+ "Operator ID must have 5 or 6 digits");
+ return FALSE;
+ }
+
+ for (i = 0; i < len; i++) {
+ if (!g_ascii_isdigit (operator_id[i])) {
+ g_set_error (error,
+ MM_CORE_ERROR,
+ MM_CORE_ERROR_FAILED,
+ "Operator ID must only contain digits");
+ return FALSE;
+ }
+ }
+
+ memcpy (&aux[0], operator_id, 3);
+ aux[3] = '\0';
+ tmp = atoi (aux);
+ if (tmp == 0) {
+ g_set_error (error,
+ MM_CORE_ERROR,
+ MM_CORE_ERROR_FAILED,
+ "MCC must not be zero");
+ return FALSE;
+ }
+
+ if (mcc)
+ *mcc = tmp;
+
+ if (mnc) {
+ if (len == 5) {
+ memcpy (&aux[0], &operator_id[3], 2);
+ aux[2] = '\0';
+ } else
+ memcpy (&aux[0], &operator_id[3], 3);
+ *mnc = atoi (aux);
+ }
+
+ return TRUE;
+}
+
+/*************************************************************************/
+
+gboolean
mm_cdma_parse_spservice_read_response (const gchar *reply,
MMModemCdmaRegistrationState *out_cdma_1x_state,
MMModemCdmaRegistrationState *out_evdo_state)
diff --git a/src/mm-modem-helpers.h b/src/mm-modem-helpers.h
index 0743c35d..4754e7f9 100644
--- a/src/mm-modem-helpers.h
+++ b/src/mm-modem-helpers.h
@@ -152,6 +152,11 @@ MMModemAccessTechnology mm_string_to_access_tech (const gchar *string);
gchar *mm_3gpp_parse_operator (const gchar *reply,
MMModemCharset cur_charset);
+gboolean mm_3gpp_parse_operator_id (const gchar *operator_id,
+ guint16 *mcc,
+ guint16 *mnc,
+ GError **error);
+
const gchar *mm_3gpp_get_pdp_type_from_ip_family (MMBearerIpFamily family);
MMBearerIpFamily mm_3gpp_get_ip_family_from_pdp_type (const gchar *pdp_type);
diff --git a/src/tests/test-modem-helpers.c b/src/tests/test-modem-helpers.c
index 12904a8e..8c8bdf01 100644
--- a/src/tests/test-modem-helpers.c
+++ b/src/tests/test-modem-helpers.c
@@ -1407,6 +1407,77 @@ test_cnum_response_generic_multiple_numbers (void *f, gpointer d)
}
/*****************************************************************************/
+/* Test operator ID parsing */
+
+static void
+common_parse_operator_id (const gchar *operator_id,
+ gboolean expected_success,
+ guint16 expected_mcc,
+ guint16 expected_mnc)
+{
+ guint16 mcc;
+ guint16 mnc;
+ gboolean result;
+ GError *error = NULL;
+
+ if (expected_mcc) {
+ g_print ("Parsing Operator ID '%s' "
+ "(%" G_GUINT16_FORMAT ", %" G_GUINT16_FORMAT ")...\n",
+ operator_id, expected_mcc, expected_mnc);
+ result = mm_3gpp_parse_operator_id (operator_id, &mcc, &mnc, &error);
+ } else {
+ g_print ("Validating Operator ID '%s'...\n", operator_id);
+ result = mm_3gpp_parse_operator_id (operator_id, NULL, NULL, &error);
+ }
+
+ if (error)
+ g_printerr ("\tGot %s error: %s...\n",
+ expected_success ? "unexpected" : "expected",
+ error->message);
+
+ g_assert (result == expected_success);
+
+ if (expected_success) {
+ g_assert_no_error (error);
+ if (expected_mcc) {
+ g_assert_cmpuint (expected_mcc, ==, mcc);
+ g_assert_cmpuint (expected_mnc, ==, mnc);
+ }
+ } else {
+ g_assert (error != NULL);
+ g_error_free (error);
+ }
+}
+
+
+static void
+test_parse_operator_id (void *f, gpointer d)
+{
+ g_print ("\n");
+ /* Valid MCC+MNC(2) */
+ common_parse_operator_id ("41201", TRUE, 412, 1);
+ common_parse_operator_id ("41201", TRUE, 0, 0);
+ /* Valid MCC+MNC(3) */
+ common_parse_operator_id ("342600", TRUE, 342, 600);
+ common_parse_operator_id ("342600", TRUE, 0, 0);
+ /* Valid MCC+MNC(2, == 0) */
+ common_parse_operator_id ("72400", TRUE, 724, 0);
+ common_parse_operator_id ("72400", TRUE, 0, 0);
+ /* Valid MCC+MNC(3, == 0) */
+ common_parse_operator_id ("724000", TRUE, 724, 0);
+ common_parse_operator_id ("724000", TRUE, 0, 0);
+
+ /* Invalid MCC=0 */
+ common_parse_operator_id ("000600", FALSE, 0, 0);
+ /* Invalid, non-digits */
+ common_parse_operator_id ("000Z00", FALSE, 0, 0);
+ /* Invalid, short */
+ common_parse_operator_id ("123", FALSE, 0, 0);
+ /* Invalid, long */
+ common_parse_operator_id ("1234567", FALSE, 0, 0);
+}
+
+/*****************************************************************************/
void
_mm_log (const char *loc,
@@ -1513,6 +1584,8 @@ int main (int argc, char **argv)
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));
+ g_test_suite_add (suite, TESTCASE (test_parse_operator_id, NULL));
+
result = g_test_run ();
reg_test_data_free (reg_data);