[PATCH v2 06/13] ublox: new AT+UACT=? test parser
Aleksander Morgado
aleksander at aleksander.es
Fri Sep 15 05:01:02 UTC 2017
---
plugins/ublox/mm-modem-helpers-ublox.c | 118 +++++++++++++++++++++++++
plugins/ublox/mm-modem-helpers-ublox.h | 9 ++
plugins/ublox/tests/test-modem-helpers-ublox.c | 98 ++++++++++++++++++++
3 files changed, 225 insertions(+)
diff --git a/plugins/ublox/mm-modem-helpers-ublox.c b/plugins/ublox/mm-modem-helpers-ublox.c
index f60a1459..cf7a295f 100644
--- a/plugins/ublox/mm-modem-helpers-ublox.c
+++ b/plugins/ublox/mm-modem-helpers-ublox.c
@@ -988,6 +988,124 @@ mm_ublox_parse_uact_response (const gchar *response,
return bands;
}
+/*****************************************************************************/
+/* UACT=? response parser */
+
+static GArray *
+parse_bands_from_string (const gchar *str,
+ const gchar *group)
+{
+ GArray *bands = NULL;
+ GError *inner_error = NULL;
+ GArray *nums;
+
+ nums = mm_parse_uint_list (str, &inner_error);
+ if (nums) {
+ gchar *tmpstr;
+
+ bands = uact_num_array_to_band_array (nums);
+ tmpstr = mm_common_build_bands_string ((MMModemBand *)(bands->data), bands->len);
+ mm_dbg ("modem reports support for %s bands: %s", group, tmpstr);
+ g_free (tmpstr);
+
+ g_array_unref (nums);
+ } else if (inner_error) {
+ mm_warn ("couldn't parse list of supported %s bands: %s", group, inner_error->message);
+ g_clear_error (&inner_error);
+ }
+
+ return bands;
+}
+
+gboolean
+mm_ublox_parse_uact_test (const gchar *response,
+ GArray **bands2g_out,
+ GArray **bands3g_out,
+ GArray **bands4g_out,
+ GError **error)
+{
+ GRegex *r;
+ GMatchInfo *match_info;
+ GError *inner_error = NULL;
+ const gchar *bands2g_str = NULL;
+ const gchar *bands3g_str = NULL;
+ const gchar *bands4g_str = NULL;
+ GArray *bands2g = NULL;
+ GArray *bands3g = NULL;
+ GArray *bands4g = NULL;
+ gchar **split = NULL;
+
+ g_assert (bands2g_out && bands3g_out && bands4g_out);
+
+ /*
+ * AT+UACT=?
+ * +UACT: ,,,(900,1800),(1,8),(101,103,107,108,120),(138)
+ */
+ r = g_regex_new ("\\+UACT: ([^,]*),([^,]*),([^,]*),(.*)(?:\\r\\n)?",
+ G_REGEX_DOLLAR_ENDONLY | G_REGEX_RAW, 0, NULL);
+ g_assert (r != NULL);
+
+ g_regex_match_full (r, response, strlen (response), 0, 0, &match_info, &inner_error);
+ if (inner_error)
+ goto out;
+
+ if (g_match_info_matches (match_info)) {
+ gchar *aux;
+ guint n_groups;
+
+ aux = mm_get_string_unquoted_from_match_info (match_info, 4);
+ split = mm_split_string_groups (aux);
+ n_groups = g_strv_length (split);
+ if (n_groups >= 1)
+ bands2g_str = split[0];
+ if (n_groups >= 2)
+ bands3g_str = split[1];
+ if (n_groups >= 3)
+ bands4g_str = split[2];
+ g_free (aux);
+ }
+
+ if (!bands2g_str && !bands3g_str && !bands4g_str) {
+ inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
+ "frequency groups not found: %s", response);
+ goto out;
+ }
+
+ bands2g = parse_bands_from_string (bands2g_str, "2G");
+ bands3g = parse_bands_from_string (bands3g_str, "3G");
+ bands4g = parse_bands_from_string (bands4g_str, "4G");
+
+ if (!bands2g->len && !bands3g->len && !bands4g->len) {
+ inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
+ "no supported frequencies reported: %s", response);
+ goto out;
+ }
+
+ /* success */
+
+out:
+ g_strfreev (split);
+ if (match_info)
+ g_match_info_free (match_info);
+ g_regex_unref (r);
+
+ if (inner_error) {
+ if (bands2g)
+ g_array_unref (bands2g);
+ if (bands3g)
+ g_array_unref (bands3g);
+ if (bands4g)
+ g_array_unref (bands4g);
+ g_propagate_error (error, inner_error);
+ return FALSE;
+ }
+
+ *bands2g_out = bands2g;
+ *bands3g_out = bands3g;
+ *bands4g_out = bands4g;
+ return TRUE;
+}
+
/*****************************************************************************/
/* URAT? response parser */
diff --git a/plugins/ublox/mm-modem-helpers-ublox.h b/plugins/ublox/mm-modem-helpers-ublox.h
index cac11ade..516e49f6 100644
--- a/plugins/ublox/mm-modem-helpers-ublox.h
+++ b/plugins/ublox/mm-modem-helpers-ublox.h
@@ -112,6 +112,15 @@ gchar *mm_ublox_build_ubandsel_set_command (GArray *bands,
GArray *mm_ublox_parse_uact_response (const gchar *response,
GError **error);
+/*****************************************************************************/
+/* UACT=? test parser */
+
+gboolean mm_ublox_parse_uact_test (const gchar *response,
+ GArray **bands_2g,
+ GArray **bands_3g,
+ GArray **bands_4g,
+ GError **error);
+
/*****************************************************************************/
/* Get mode to apply when ANY */
diff --git a/plugins/ublox/tests/test-modem-helpers-ublox.c b/plugins/ublox/tests/test-modem-helpers-ublox.c
index 587b5a38..bb54cc17 100644
--- a/plugins/ublox/tests/test-modem-helpers-ublox.c
+++ b/plugins/ublox/tests/test-modem-helpers-ublox.c
@@ -791,6 +791,100 @@ test_uact_response_2g3g4g (void)
expected_bands, G_N_ELEMENTS (expected_bands));
}
+/*****************************************************************************/
+/* Test +UACT=? test parser */
+
+static void
+common_validate_uact_test (const gchar *str,
+ const MMModemBand *expected_bands_2g,
+ guint n_expected_bands_2g,
+ const MMModemBand *expected_bands_3g,
+ guint n_expected_bands_3g,
+ const MMModemBand *expected_bands_4g,
+ guint n_expected_bands_4g)
+{
+ GError *error = NULL;
+ gboolean result;
+ GArray *bands_2g = NULL;
+ GArray *bands_3g = NULL;
+ GArray *bands_4g = NULL;
+
+ result = mm_ublox_parse_uact_test (str, &bands_2g, &bands_3g, &bands_4g, &error);
+ g_assert_no_error (error);
+ g_assert (result);
+
+ common_compare_bands (bands_2g, expected_bands_2g, n_expected_bands_2g);
+ common_compare_bands (bands_3g, expected_bands_3g, n_expected_bands_3g);
+ common_compare_bands (bands_4g, expected_bands_4g, n_expected_bands_4g);
+}
+
+static void
+test_uact_test_2g (void)
+{
+ const MMModemBand expected_bands_2g[] = {
+ MM_MODEM_BAND_EGSM, MM_MODEM_BAND_DCS
+ };
+
+ common_validate_uact_test ("+UACT: ,,,(900,1800)\r\n",
+ expected_bands_2g, G_N_ELEMENTS (expected_bands_2g),
+ NULL, 0,
+ NULL, 0);
+}
+
+static void
+test_uact_test_2g3g (void)
+{
+ const MMModemBand expected_bands_2g[] = {
+ MM_MODEM_BAND_EGSM, MM_MODEM_BAND_DCS
+ };
+ const MMModemBand expected_bands_3g[] = {
+ MM_MODEM_BAND_UTRAN_1, MM_MODEM_BAND_UTRAN_8
+ };
+
+ common_validate_uact_test ("+UACT: ,,,(900,1800),(1,8)\r\n",
+ expected_bands_2g, G_N_ELEMENTS (expected_bands_2g),
+ expected_bands_3g, G_N_ELEMENTS (expected_bands_3g),
+ NULL, 0);
+}
+
+static void
+test_uact_test_2g3g4g (void)
+{
+ const MMModemBand expected_bands_2g[] = {
+ MM_MODEM_BAND_EGSM, MM_MODEM_BAND_DCS
+ };
+ const MMModemBand expected_bands_3g[] = {
+ MM_MODEM_BAND_UTRAN_1, MM_MODEM_BAND_UTRAN_8
+ };
+ const MMModemBand expected_bands_4g[] = {
+ MM_MODEM_BAND_EUTRAN_1, MM_MODEM_BAND_EUTRAN_3, MM_MODEM_BAND_EUTRAN_7, MM_MODEM_BAND_EUTRAN_8, MM_MODEM_BAND_EUTRAN_20
+ };
+
+ common_validate_uact_test ("+UACT: ,,,(900,1800),(1,8),(101,103,107,108,120)\r\n",
+ expected_bands_2g, G_N_ELEMENTS (expected_bands_2g),
+ expected_bands_3g, G_N_ELEMENTS (expected_bands_3g),
+ expected_bands_4g, G_N_ELEMENTS (expected_bands_4g));
+}
+
+static void
+test_uact_test_2g3g4g_2 (void)
+{
+ const MMModemBand expected_bands_2g[] = {
+ MM_MODEM_BAND_EGSM, MM_MODEM_BAND_DCS
+ };
+ const MMModemBand expected_bands_3g[] = {
+ MM_MODEM_BAND_UTRAN_1, MM_MODEM_BAND_UTRAN_8
+ };
+ const MMModemBand expected_bands_4g[] = {
+ MM_MODEM_BAND_EUTRAN_1, MM_MODEM_BAND_EUTRAN_3, MM_MODEM_BAND_EUTRAN_7, MM_MODEM_BAND_EUTRAN_8, MM_MODEM_BAND_EUTRAN_20
+ };
+
+ common_validate_uact_test ("+UACT: ,,,(900,1800),(1,8),(101,103,107,108,120),(138)\r\n",
+ expected_bands_2g, G_N_ELEMENTS (expected_bands_2g),
+ expected_bands_3g, G_N_ELEMENTS (expected_bands_3g),
+ expected_bands_4g, G_N_ELEMENTS (expected_bands_4g));
+}
+
/*****************************************************************************/
/* Test +UGCNTRD responses */
@@ -925,6 +1019,10 @@ int main (int argc, char **argv)
g_test_add_func ("/MM/ublox/uact/response/2g", test_uact_response_2g);
g_test_add_func ("/MM/ublox/uact/response/2g3g", test_uact_response_2g3g);
g_test_add_func ("/MM/ublox/uact/response/2g3g4g", test_uact_response_2g3g4g);
+ g_test_add_func ("/MM/ublox/uact/test/2g", test_uact_test_2g);
+ g_test_add_func ("/MM/ublox/uact/test/2g3g", test_uact_test_2g3g);
+ g_test_add_func ("/MM/ublox/uact/test/2g3g4g", test_uact_test_2g3g4g);
+ g_test_add_func ("/MM/ublox/uact/test/2g3g4g/2", test_uact_test_2g3g4g_2);
g_test_add_func ("/MM/ublox/ugcntrd/response", test_ugcntrd_response);
return g_test_run ();
--
2.14.1
More information about the ModemManager-devel
mailing list