aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksander Morgado <aleksander@aleksander.es>2014-02-26 20:42:15 +0100
committerAleksander Morgado <aleksander@aleksander.es>2014-02-28 16:35:18 +0100
commitc1e2a3a5bc686f310a705724e806d058f3cee68c (patch)
tree0041add5ebe34cd062dff9c889307dd8a416b57c
parent0877f68a8481c5d011652831cfe523f7a629604e (diff)
cinterion: consolidate current bands loading for 2G and 3G devices
-rw-r--r--plugins/cinterion/mm-broadband-modem-cinterion.c101
-rw-r--r--plugins/cinterion/mm-modem-helpers-cinterion.c53
-rw-r--r--plugins/cinterion/mm-modem-helpers-cinterion.h7
-rw-r--r--plugins/cinterion/tests/test-modem-helpers-cinterion.c48
4 files changed, 92 insertions, 117 deletions
diff --git a/plugins/cinterion/mm-broadband-modem-cinterion.c b/plugins/cinterion/mm-broadband-modem-cinterion.c
index e2d97591..d3d7ff11 100644
--- a/plugins/cinterion/mm-broadband-modem-cinterion.c
+++ b/plugins/cinterion/mm-broadband-modem-cinterion.c
@@ -972,7 +972,7 @@ load_supported_bands (MMIfaceModem *self,
}
/*****************************************************************************/
-/* CURRENT BANDS */
+/* Load current bands (Modem interface) */
static GArray *
load_current_bands_finish (MMIfaceModem *self,
@@ -987,91 +987,9 @@ load_current_bands_finish (MMIfaceModem *self,
}
static void
-get_2g_band_ready (MMBroadbandModemCinterion *self,
- GAsyncResult *res,
- GSimpleAsyncResult *operation_result)
-{
- const gchar *response;
- GError *error = NULL;
- GArray *bands_array = NULL;
- GRegex *regex;
- GMatchInfo *match_info = NULL;
-
- response = mm_base_modem_at_command_finish (MM_BASE_MODEM (self), res, &error);
- if (!response) {
- /* Let the error be critical. */
- g_simple_async_result_take_error (operation_result, error);
- g_simple_async_result_complete (operation_result);
- g_object_unref (operation_result);
- return;
- }
-
- /* The AT^SCFG? command replies a list of several different config
- * values. We will only look for 'Radio/Band".
- *
- * AT+SCFG="Radio/Band"
- * ^SCFG: "Radio/Band","0031","0031"
- *
- * Note that "0031" is a UCS2-encoded string, as we configured UCS2 as
- * character set to use.
- */
- regex = g_regex_new ("\\^SCFG:\\s*\"Radio/Band\",\\s*\"(.*)\",\\s*\"(.*)\"", 0, 0, NULL);
- g_assert (regex != NULL);
-
- if (g_regex_match_full (regex, response, strlen (response), 0, 0, &match_info, NULL)) {
- gchar *current;
-
- /* The first number given is the current band configuration, the
- * second number given is the allowed band configuration, which we
- * don't really need to get here. */
- current = g_match_info_fetch (match_info, 1);
- if (current) {
- guint i;
-
- /* If in UCS2, convert to UTF-8 */
- current = mm_broadband_modem_take_and_convert_to_utf8 (MM_BROADBAND_MODEM (self),
- current);
-
- for (i = 0; i < G_N_ELEMENTS (bands_2g); i++) {
- if (strcmp (bands_2g[i].cinterion_band, current) == 0) {
- guint j;
-
- if (G_UNLIKELY (!bands_array))
- bands_array = g_array_new (FALSE, FALSE, sizeof (MMModemBand));
-
- for (j = 0; j < bands_2g[i].n_mm_bands; j++)
- g_array_append_val (bands_array, bands_2g[i].mm_bands[j]);
-
- break;
- }
- }
-
- g_free (current);
- }
- }
-
- if (match_info)
- g_match_info_free (match_info);
- g_regex_unref (regex);
-
- if (!bands_array)
- g_simple_async_result_set_error (operation_result,
- MM_CORE_ERROR,
- MM_CORE_ERROR_FAILED,
- "Couldn't parse current bands reply");
- else
- g_simple_async_result_set_op_res_gpointer (operation_result,
- bands_array,
- (GDestroyNotify)g_array_unref);
-
- g_simple_async_result_complete (operation_result);
- g_object_unref (operation_result);
-}
-
-static void
-get_3g_band_ready (MMBroadbandModemCinterion *self,
- GAsyncResult *res,
- GSimpleAsyncResult *simple)
+get_band_ready (MMBroadbandModemCinterion *self,
+ GAsyncResult *res,
+ GSimpleAsyncResult *simple)
{
const gchar *response;
GError *error = NULL;
@@ -1080,7 +998,10 @@ get_3g_band_ready (MMBroadbandModemCinterion *self,
response = mm_base_modem_at_command_finish (MM_BASE_MODEM (self), res, &error);
if (!response)
g_simple_async_result_take_error (simple, error);
- else if (!mm_cinterion_parse_scfg_3g_response (response, &bands, &error))
+ else if (!mm_cinterion_parse_scfg_response (response,
+ mm_broadband_modem_get_current_charset (MM_BROADBAND_MODEM (self)),
+ &bands,
+ &error))
g_simple_async_result_take_error (simple, error);
else
g_simple_async_result_set_op_res_gpointer (simple, bands, (GDestroyNotify)g_array_unref);
@@ -1101,15 +1022,11 @@ load_current_bands (MMIfaceModem *self,
user_data,
load_current_bands);
- /* Query the currently used Radio/Band. The query command is the same for
- * both 2G and 3G devices, but the reply reader is different. */
mm_base_modem_at_command (MM_BASE_MODEM (self),
"AT^SCFG=\"Radio/Band\"",
3,
FALSE,
- (GAsyncReadyCallback)(mm_iface_modem_is_3g (self) ?
- get_3g_band_ready :
- get_2g_band_ready),
+ (GAsyncReadyCallback)get_band_ready,
result);
}
diff --git a/plugins/cinterion/mm-modem-helpers-cinterion.c b/plugins/cinterion/mm-modem-helpers-cinterion.c
index ad3112f2..a7d1b86d 100644
--- a/plugins/cinterion/mm-modem-helpers-cinterion.c
+++ b/plugins/cinterion/mm-modem-helpers-cinterion.c
@@ -136,17 +136,26 @@ mm_cinterion_parse_scfg_test (const gchar *response,
}
/*****************************************************************************/
-/* ^SCFG (3G) response parser
+/* ^SCFG response parser
*
- * Example:
+ * Example (3G):
* AT^SCFG="Radio/Band"
* ^SCFG: "Radio/Band",127
+ *
+ * Example (2G, UCS-2):
+ * AT+SCFG="Radio/Band"
+ * ^SCFG: "Radio/Band","0031","0031"
+ *
+ * Example (2G):
+ * AT+SCFG="Radio/Band"
+ * ^SCFG: "Radio/Band","3","3"
*/
gboolean
-mm_cinterion_parse_scfg_3g_response (const gchar *response,
- GArray **current_bands,
- GError **error)
+mm_cinterion_parse_scfg_response (const gchar *response,
+ MMModemCharset charset,
+ GArray **current_bands,
+ GError **error)
{
GRegex *r;
GMatchInfo *match_info;
@@ -158,29 +167,39 @@ mm_cinterion_parse_scfg_3g_response (const gchar *response,
return FALSE;
}
- r = g_regex_new ("\\^SCFG:\\s*\"Radio/Band\",\\s*(\\d*)", 0, 0, NULL);
+ r = g_regex_new ("\\^SCFG:\\s*\"Radio/Band\",\\s*\"?([0-9a-fA-F]*)\"?", 0, 0, NULL);
g_assert (r != NULL);
if (g_regex_match_full (r, response, strlen (response), 0, 0, &match_info, NULL)) {
- gchar *current;
+ gchar *currentstr;
+ guint current = 0;
- current = g_match_info_fetch (match_info, 1);
- if (current) {
- guint32 current_int;
- guint i;
+ currentstr = mm_get_string_unquoted_from_match_info (match_info, 1);
+ if (currentstr) {
+ /* Handle charset conversion if the number is given in UCS2 */
+ if (charset != MM_MODEM_CHARSET_UNKNOWN)
+ currentstr = mm_charset_take_and_convert_to_utf8 (currentstr, charset);
+
+ mm_get_uint_from_str (currentstr, &current);
+ }
- current_int = (guint32) atoi (current);
+ if (current == 0) {
+ inner_error = g_error_new (MM_CORE_ERROR,
+ MM_CORE_ERROR_FAILED,
+ "Couldn't parse ^SCFG response");
+ } else {
+ guint i;
for (i = 0; i < G_N_ELEMENTS (cinterion_bands); i++) {
- if (current_int & cinterion_bands[i].cinterion_band_flag) {
+ if (current & cinterion_bands[i].cinterion_band_flag) {
if (G_UNLIKELY (!bands))
- bands = g_array_sized_new (FALSE, FALSE, sizeof (MMModemBand), 4);
+ bands = g_array_sized_new (FALSE, FALSE, sizeof (MMModemBand), 9);
g_array_append_val (bands, cinterion_bands[i].mm_band);
}
}
-
- g_free (current);
}
+
+ g_free (currentstr);
}
if (match_info)
@@ -190,7 +209,7 @@ mm_cinterion_parse_scfg_3g_response (const gchar *response,
if (!bands)
inner_error = g_error_new (MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
- "No valid bands found in ^SCFG=? response");
+ "No valid bands found in ^SCFG response");
if (inner_error) {
g_propagate_error (error, inner_error);
diff --git a/plugins/cinterion/mm-modem-helpers-cinterion.h b/plugins/cinterion/mm-modem-helpers-cinterion.h
index c4d8eff5..43b8ba84 100644
--- a/plugins/cinterion/mm-modem-helpers-cinterion.h
+++ b/plugins/cinterion/mm-modem-helpers-cinterion.h
@@ -29,9 +29,10 @@ gboolean mm_cinterion_parse_scfg_test (const gchar *response,
/*****************************************************************************/
/* ^SCFG response parser */
-gboolean mm_cinterion_parse_scfg_3g_response (const gchar *response,
- GArray **bands,
- GError **error);
+gboolean mm_cinterion_parse_scfg_response (const gchar *response,
+ MMModemCharset charset,
+ GArray **bands,
+ GError **error);
/*****************************************************************************/
/* Build Cinterion-specific band value */
diff --git a/plugins/cinterion/tests/test-modem-helpers-cinterion.c b/plugins/cinterion/tests/test-modem-helpers-cinterion.c
index 1a813b02..1ee71c9b 100644
--- a/plugins/cinterion/tests/test-modem-helpers-cinterion.c
+++ b/plugins/cinterion/tests/test-modem-helpers-cinterion.c
@@ -122,6 +122,7 @@ test_scfg (void)
static void
common_test_scfg_response (const gchar *response,
+ MMModemCharset charset,
GArray *expected_bands)
{
GArray *bands = NULL;
@@ -130,7 +131,7 @@ common_test_scfg_response (const gchar *response,
GError *error = NULL;
gboolean res;
- res = mm_cinterion_parse_scfg_3g_response (response, &bands, &error);
+ res = mm_cinterion_parse_scfg_response (response, charset, &bands, &error);
g_assert_no_error (error);
g_assert (res == TRUE);
g_assert (bands != NULL);
@@ -152,7 +153,42 @@ common_test_scfg_response (const gchar *response,
}
static void
-test_scfg_response (void)
+test_scfg_response_2g (void)
+{
+ GArray *expected_bands;
+ MMModemBand single;
+ const gchar *response =
+ "^SCFG: \"Radio/Band\",\"3\",\"3\"\r\n"
+ "\r\n";
+
+ expected_bands = g_array_sized_new (FALSE, FALSE, sizeof (MMModemBand), 9);
+ single = MM_MODEM_BAND_EGSM, g_array_append_val (expected_bands, single);
+ single = MM_MODEM_BAND_DCS, g_array_append_val (expected_bands, single);
+
+ common_test_scfg_response (response, MM_MODEM_CHARSET_UNKNOWN, expected_bands);
+
+ g_array_unref (expected_bands);
+}
+
+static void
+test_scfg_response_2g_ucs2 (void)
+{
+ GArray *expected_bands;
+ MMModemBand single;
+ const gchar *response =
+ "^SCFG: \"Radio/Band\",\"0031\",\"0031\"\r\n"
+ "\r\n";
+
+ expected_bands = g_array_sized_new (FALSE, FALSE, sizeof (MMModemBand), 9);
+ single = MM_MODEM_BAND_EGSM, g_array_append_val (expected_bands, single);
+
+ common_test_scfg_response (response, MM_MODEM_CHARSET_UCS2, expected_bands);
+
+ g_array_unref (expected_bands);
+}
+
+static void
+test_scfg_response_3g (void)
{
GArray *expected_bands;
MMModemBand single;
@@ -169,7 +205,7 @@ test_scfg_response (void)
single = MM_MODEM_BAND_U1900, g_array_append_val (expected_bands, single);
single = MM_MODEM_BAND_U850, g_array_append_val (expected_bands, single);
- common_test_scfg_response (response, expected_bands);
+ common_test_scfg_response (response, MM_MODEM_CHARSET_UNKNOWN, expected_bands);
g_array_unref (expected_bands);
}
@@ -203,8 +239,10 @@ int main (int argc, char **argv)
g_type_init ();
g_test_init (&argc, &argv, NULL);
- g_test_add_func ("/MM/cinterion/scfg", test_scfg);
- g_test_add_func ("/MM/cinterion/scfg/response", test_scfg_response);
+ g_test_add_func ("/MM/cinterion/scfg", test_scfg);
+ g_test_add_func ("/MM/cinterion/scfg/response/3g", test_scfg_response_3g);
+ g_test_add_func ("/MM/cinterion/scfg/response/2g", test_scfg_response_2g);
+ g_test_add_func ("/MM/cinterion/scfg/response/2g/ucs2", test_scfg_response_2g_ucs2);
return g_test_run ();
}