summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksander Morgado <aleksander@aleksander.es>2016-08-12 11:36:48 +0200
committerAleksander Morgado <aleksander@aleksander.es>2016-10-12 13:24:09 +0200
commit09c9ca5749f2fc59e5d1adf84eb3a0e0c9a58cff (patch)
tree382f908c5f2dfa31a0a8f6689cb9464304f95b6a
parent4632836d40af6831835ecea2561be53676c578bd (diff)
downloadModemManager-09c9ca5749f2fc59e5d1adf84eb3a0e0c9a58cff.tar.gz
ublox: new +UBANDSEL? response parser
-rw-r--r--plugins/ublox/mm-modem-helpers-ublox.c87
-rw-r--r--plugins/ublox/mm-modem-helpers-ublox.h6
-rw-r--r--plugins/ublox/tests/test-modem-helpers-ublox.c83
3 files changed, 176 insertions, 0 deletions
diff --git a/plugins/ublox/mm-modem-helpers-ublox.c b/plugins/ublox/mm-modem-helpers-ublox.c
index ece45fe5a..1190cd68b 100644
--- a/plugins/ublox/mm-modem-helpers-ublox.c
+++ b/plugins/ublox/mm-modem-helpers-ublox.c
@@ -645,6 +645,93 @@ mm_ublox_get_supported_bands (const gchar *model,
}
/*****************************************************************************/
+/* +UBANDSEL? response parser */
+
+static void
+append_bands (GArray *bands,
+ guint ubandsel_value)
+{
+ guint i;
+
+ for (i = 0; i < G_N_ELEMENTS (band_configuration); i++)
+ if (ubandsel_value == band_configuration[i].ubandsel_value)
+ break;
+
+ if (i == G_N_ELEMENTS (band_configuration)) {
+ mm_warn ("Unknown band configuration value given: %u", ubandsel_value);
+ return;
+ }
+
+ /* Note: we don't care if the device doesn't support one of these modes;
+ * the generic logic will filter out all bands not supported before
+ * exposing them in the DBus property */
+
+ if (band_configuration[i].bands_2g[0]) {
+ g_array_append_val (bands, band_configuration[i].bands_2g[0]);
+ if (band_configuration[i].bands_2g[1])
+ g_array_append_val (bands, band_configuration[i].bands_2g[1]);
+ }
+
+ if (band_configuration[i].bands_3g[0]) {
+ g_array_append_val (bands, band_configuration[i].bands_3g[0]);
+ if (band_configuration[i].bands_3g[1])
+ g_array_append_val (bands, band_configuration[i].bands_3g[1]);
+ }
+
+ if (band_configuration[i].bands_4g[0]) {
+ g_array_append_val (bands, band_configuration[i].bands_4g[0]);
+ if (band_configuration[i].bands_4g[1])
+ g_array_append_val (bands, band_configuration[i].bands_4g[1]);
+ }
+}
+
+GArray *
+mm_ublox_parse_ubandsel_response (const gchar *response,
+ GError **error)
+{
+ GArray *array_values = NULL;
+ GArray *array = NULL;
+ gchar *dupstr = NULL;
+ GError *inner_error = NULL;
+ guint i;
+
+ if (!g_str_has_prefix (response, "+UBANDSEL")) {
+ inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
+ "Couldn't parse +UBANDSEL response: '%s'", response);
+ goto out;
+ }
+
+ /* Response may be e.g.:
+ * +UBANDSEL: 850,900,1800,1900
+ */
+ dupstr = g_strchomp (g_strdup (mm_strip_tag (response, "+UBANDSEL:")));
+
+ array_values = mm_parse_uint_list (dupstr, &inner_error);
+ if (!array_values)
+ goto out;
+
+ /* Convert list of ubandsel numbers to MMModemBand values */
+ array = g_array_new (FALSE, FALSE, sizeof (MMModemBand));
+ for (i = 0; i < array_values->len; i++)
+ append_bands (array, g_array_index (array_values, guint, i));
+
+ if (!array->len) {
+ inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
+ "No known band selection values matched in +UBANDSEL response: '%s'", response);
+ goto out;
+ }
+
+out:
+ if (inner_error) {
+ g_propagate_error (error, inner_error);
+ g_clear_pointer (&array, g_array_unref);
+ }
+ g_clear_pointer (&array_values, g_array_unref);
+ g_free (dupstr);
+ return array;
+}
+
+/*****************************************************************************/
/* Get mode to apply when ANY */
MMModemMode
diff --git a/plugins/ublox/mm-modem-helpers-ublox.h b/plugins/ublox/mm-modem-helpers-ublox.h
index 97a0c7977..bd4f5e2b1 100644
--- a/plugins/ublox/mm-modem-helpers-ublox.h
+++ b/plugins/ublox/mm-modem-helpers-ublox.h
@@ -95,6 +95,12 @@ GArray *mm_ublox_get_supported_bands (const gchar *model,
GError **error);
/*****************************************************************************/
+/* UBANDSEL? response parser */
+
+GArray *mm_ublox_parse_ubandsel_response (const gchar *response,
+ GError **error);
+
+/*****************************************************************************/
/* Get mode to apply when ANY */
MMModemMode mm_ublox_get_modem_mode_any (const GArray *combinations);
diff --git a/plugins/ublox/tests/test-modem-helpers-ublox.c b/plugins/ublox/tests/test-modem-helpers-ublox.c
index 497b56f76..917385827 100644
--- a/plugins/ublox/tests/test-modem-helpers-ublox.c
+++ b/plugins/ublox/tests/test-modem-helpers-ublox.c
@@ -592,6 +592,84 @@ test_supported_bands_sara_u280 (void)
}
/*****************************************************************************/
+/* Test +UBANDSEL? response parser */
+
+static void
+common_validate_ubandsel_response (const gchar *str,
+ const MMModemBand *expected_bands,
+ guint n_expected_bands)
+{
+ GError *error = NULL;
+ GArray *bands;
+ gchar *bands_str;
+ GArray *expected_bands_array;
+ gchar *expected_bands_str;
+
+ bands = mm_ublox_parse_ubandsel_response (str, &error);
+ g_assert_no_error (error);
+ g_assert (bands);
+ mm_common_bands_garray_sort (bands);
+ bands_str = mm_common_build_bands_string ((MMModemBand *)(bands->data), bands->len);
+ g_array_unref (bands);
+
+ expected_bands_array = g_array_sized_new (FALSE, FALSE, sizeof (MMModemBand), n_expected_bands);
+ g_array_append_vals (expected_bands_array, expected_bands, n_expected_bands);
+ mm_common_bands_garray_sort (expected_bands_array);
+ expected_bands_str = mm_common_build_bands_string ((MMModemBand *)(expected_bands_array->data), expected_bands_array->len);
+ g_array_unref (expected_bands_array);
+
+ g_assert_cmpstr (bands_str, ==, expected_bands_str);
+ g_free (bands_str);
+ g_free (expected_bands_str);
+}
+
+static void
+test_ubandsel_response_four (void)
+{
+ const MMModemBand expected_bands[] = {
+ /* 850 */ MM_MODEM_BAND_G850, MM_MODEM_BAND_U850, MM_MODEM_BAND_EUTRAN_V,
+ /* 900 */ MM_MODEM_BAND_EGSM, MM_MODEM_BAND_U900, MM_MODEM_BAND_EUTRAN_VIII,
+ /* 1800 */ MM_MODEM_BAND_DCS, MM_MODEM_BAND_U1800, MM_MODEM_BAND_EUTRAN_III,
+ /* 1900 */ MM_MODEM_BAND_PCS, MM_MODEM_BAND_U1900, MM_MODEM_BAND_EUTRAN_II,
+ };
+
+ common_validate_ubandsel_response ("+UBANDSEL: 850,900,1800,1900\r\n", expected_bands, G_N_ELEMENTS (expected_bands));
+}
+
+static void
+test_ubandsel_response_three (void)
+{
+ const MMModemBand expected_bands[] = {
+ /* 850 */ MM_MODEM_BAND_G850, MM_MODEM_BAND_U850, MM_MODEM_BAND_EUTRAN_V,
+ /* 900 */ MM_MODEM_BAND_EGSM, MM_MODEM_BAND_U900, MM_MODEM_BAND_EUTRAN_VIII,
+ /* 1800 */ MM_MODEM_BAND_DCS, MM_MODEM_BAND_U1800, MM_MODEM_BAND_EUTRAN_III,
+ };
+
+ common_validate_ubandsel_response ("+UBANDSEL: 850,900,1800\r\n", expected_bands, G_N_ELEMENTS (expected_bands));
+}
+
+static void
+test_ubandsel_response_two (void)
+{
+ const MMModemBand expected_bands[] = {
+ /* 850 */ MM_MODEM_BAND_G850, MM_MODEM_BAND_U850, MM_MODEM_BAND_EUTRAN_V,
+ /* 900 */ MM_MODEM_BAND_EGSM, MM_MODEM_BAND_U900, MM_MODEM_BAND_EUTRAN_VIII,
+ };
+
+ common_validate_ubandsel_response ("+UBANDSEL: 850,900\r\n", expected_bands, G_N_ELEMENTS (expected_bands));
+}
+
+static void
+test_ubandsel_response_one (void)
+{
+ const MMModemBand expected_bands[] = {
+ /* 850 */ MM_MODEM_BAND_G850, MM_MODEM_BAND_U850, MM_MODEM_BAND_EUTRAN_V,
+ };
+
+ common_validate_ubandsel_response ("+UBANDSEL: 850\r\n", expected_bands, G_N_ELEMENTS (expected_bands));
+}
+
+/*****************************************************************************/
void
_mm_log (const char *loc,
@@ -638,5 +716,10 @@ int main (int argc, char **argv)
g_test_add_func ("/MM/ublox/supported-bands/lisa-u200", test_supported_bands_lisa_u200);
g_test_add_func ("/MM/ublox/supported-bands/sara-u280", test_supported_bands_sara_u280);
+ g_test_add_func ("/MM/ublox/ubandsel/response/one", test_ubandsel_response_one);
+ g_test_add_func ("/MM/ublox/ubandsel/response/two", test_ubandsel_response_two);
+ g_test_add_func ("/MM/ublox/ubandsel/response/three", test_ubandsel_response_three);
+ g_test_add_func ("/MM/ublox/ubandsel/response/four", test_ubandsel_response_four);
+
return g_test_run ();
}