ublox: new +URAT? response parser

This commit is contained in:
Aleksander Morgado
2016-08-09 08:43:56 +02:00
parent 29ace8b120
commit bde9c4795a
3 changed files with 156 additions and 0 deletions

View File

@@ -426,3 +426,97 @@ mm_ublox_filter_supported_modes (const gchar *model,
return filtered; return filtered;
} }
/*****************************************************************************/
/* URAT? response parser */
gboolean
mm_ublox_parse_urat_read_response (const gchar *response,
MMModemMode *out_allowed,
MMModemMode *out_preferred,
GError **error)
{
GRegex *r;
GMatchInfo *match_info;
GError *inner_error = NULL;
MMModemMode allowed = MM_MODEM_MODE_NONE;
MMModemMode preferred = MM_MODEM_MODE_NONE;
gchar *allowed_str = NULL;
gchar *preferred_str = NULL;
g_assert (out_allowed != NULL && out_preferred != NULL);
/* Response may be e.g.:
* +URAT: 1,2
* +URAT: 1
*/
r = g_regex_new ("\\+URAT: (\\d+)(?:,(\\d+))?(?:\\r\\n)?", 0, 0, NULL);
g_assert (r != NULL);
g_regex_match_full (r, response, strlen (response), 0, 0, &match_info, &inner_error);
if (!inner_error && g_match_info_matches (match_info)) {
guint value = 0;
/* Selected item is mandatory */
if (!mm_get_uint_from_match_info (match_info, 1, &value)) {
inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"Couldn't read AcT selected value");
goto out;
}
if (value >= G_N_ELEMENTS (ublox_combinations)) {
inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"Unexpected AcT selected value: %u", value);
goto out;
}
allowed = ublox_combinations[value];
allowed_str = mm_modem_mode_build_string_from_mask (allowed);
mm_dbg ("current allowed modes retrieved: %s", allowed_str);
/* Preferred item is optional */
if (mm_get_uint_from_match_info (match_info, 2, &value)) {
if (value >= G_N_ELEMENTS (ublox_combinations)) {
inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"Unexpected AcT preferred value: %u", value);
goto out;
}
preferred = ublox_combinations[value];
preferred_str = mm_modem_mode_build_string_from_mask (preferred);
mm_dbg ("current preferred modes retrieved: %s", preferred_str);
if (mm_count_bits_set (preferred) != 1) {
inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"AcT preferred value should be a single AcT: %s", preferred_str);
goto out;
}
if (!(allowed & preferred)) {
inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"AcT preferred value (%s) not a subset of the allowed value (%s)",
preferred_str, allowed_str);
goto out;
}
}
}
out:
g_free (allowed_str);
g_free (preferred_str);
if (match_info)
g_match_info_free (match_info);
g_regex_unref (r);
if (inner_error) {
g_propagate_error (error, inner_error);
return FALSE;
}
if (allowed == MM_MODEM_MODE_NONE) {
inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"Couldn't parse +URAT response: %s", response);
return FALSE;
}
*out_allowed = allowed;
*out_preferred = preferred;
return TRUE;
}

View File

@@ -17,6 +17,7 @@
#define MM_MODEM_HELPERS_UBLOX_H #define MM_MODEM_HELPERS_UBLOX_H
#include <glib.h> #include <glib.h>
#include <ModemManager.h>
/*****************************************************************************/ /*****************************************************************************/
/* UUSBCONF? response parser */ /* UUSBCONF? response parser */
@@ -70,5 +71,13 @@ GArray *mm_ublox_filter_supported_modes (const gchar *model,
GArray *combinations, GArray *combinations,
GError **error); GError **error);
/*****************************************************************************/
/* URAT? response parser */
gboolean mm_ublox_parse_urat_read_response (const gchar *response,
MMModemMode *out_allowed,
MMModemMode *out_preferred,
GError **error);
#endif /* MM_MODEM_HELPERS_UBLOX_H */ #endif /* MM_MODEM_HELPERS_UBLOX_H */

View File

@@ -317,6 +317,58 @@ test_mode_filtering_sara_u280 (void)
compare_combinations ("+URAT: (0-6),(0,2,3)", "SARA-U280", expected_combinations, G_N_ELEMENTS (expected_combinations)); compare_combinations ("+URAT: (0-6),(0,2,3)", "SARA-U280", expected_combinations, G_N_ELEMENTS (expected_combinations));
} }
/*****************************************************************************/
/* URAT? response parser */
typedef struct {
const gchar *str;
MMModemMode allowed;
MMModemMode preferred;
} UratReadResponseTest;
static const UratReadResponseTest urat_read_response_tests[] = {
{
.str = "+URAT: 1,2\r\n",
.allowed = (MM_MODEM_MODE_2G | MM_MODEM_MODE_3G),
.preferred = MM_MODEM_MODE_3G,
},
{
.str = "+URAT: 4,0\r\n",
.allowed = (MM_MODEM_MODE_2G | MM_MODEM_MODE_3G | MM_MODEM_MODE_4G),
.preferred = MM_MODEM_MODE_2G,
},
{
.str = "+URAT: 0\r\n",
.allowed = MM_MODEM_MODE_2G,
.preferred = MM_MODEM_MODE_NONE,
},
{
.str = "+URAT: 6\r\n",
.allowed = (MM_MODEM_MODE_3G | MM_MODEM_MODE_4G),
.preferred = MM_MODEM_MODE_NONE,
},
};
static void
test_urat_read_response (void)
{
guint i;
for (i = 0; i < G_N_ELEMENTS (urat_read_response_tests); i++) {
MMModemMode allowed = MM_MODEM_MODE_NONE;
MMModemMode preferred = MM_MODEM_MODE_NONE;
GError *error = NULL;
gboolean success;
success = mm_ublox_parse_urat_read_response (urat_read_response_tests[i].str,
&allowed, &preferred, &error);
g_assert_no_error (error);
g_assert (success);
g_assert_cmpuint (urat_read_response_tests[i].allowed, ==, allowed);
g_assert_cmpuint (urat_read_response_tests[i].preferred, ==, preferred);
}
}
/*****************************************************************************/ /*****************************************************************************/
void void
@@ -355,6 +407,7 @@ int main (int argc, char **argv)
g_test_add_func ("/MM/ublox/urat/test/response/toby-l201", test_mode_filtering_toby_l201); g_test_add_func ("/MM/ublox/urat/test/response/toby-l201", test_mode_filtering_toby_l201);
g_test_add_func ("/MM/ublox/urat/test/response/lisa-u200", test_mode_filtering_lisa_u200); g_test_add_func ("/MM/ublox/urat/test/response/lisa-u200", test_mode_filtering_lisa_u200);
g_test_add_func ("/MM/ublox/urat/test/response/sara-u280", test_mode_filtering_sara_u280); g_test_add_func ("/MM/ublox/urat/test/response/sara-u280", test_mode_filtering_sara_u280);
g_test_add_func ("/MM/ublox/urat/read/response", test_urat_read_response);
return g_test_run (); return g_test_run ();
} }