modem-helpers: new method to validate and/or parse MCC/MNC operator ID string
This commit is contained in:
@@ -1465,6 +1465,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,
|
||||
|
@@ -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);
|
||||
|
||||
|
@@ -1406,6 +1406,77 @@ test_cnum_response_generic_multiple_numbers (void *f, gpointer d)
|
||||
test_cnum_results ("Generic, multiple numbers", reply, (GStrv)expected);
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
/* 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
|
||||
@@ -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);
|
||||
|
Reference in New Issue
Block a user