ublox: new 'AT+UBMCONF?' response parser
This commit is contained in:
@@ -81,3 +81,64 @@ mm_ublox_parse_uusbconf_response (const gchar *response,
|
||||
*out_profile = profile;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
/* UBMCONF? response parser */
|
||||
|
||||
gboolean
|
||||
mm_ublox_parse_ubmconf_response (const gchar *response,
|
||||
MMUbloxNetworkingMode *out_mode,
|
||||
GError **error)
|
||||
{
|
||||
GRegex *r;
|
||||
GMatchInfo *match_info;
|
||||
GError *inner_error = NULL;
|
||||
MMUbloxNetworkingMode mode = MM_UBLOX_NETWORKING_MODE_UNKNOWN;
|
||||
|
||||
g_assert (out_mode != NULL);
|
||||
|
||||
/* Response may be e.g.:
|
||||
* +UBMCONF: 1
|
||||
* +UBMCONF: 2
|
||||
*/
|
||||
r = g_regex_new ("\\+UBMCONF: (\\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 mode_id = 0;
|
||||
|
||||
if (mm_get_uint_from_match_info (match_info, 1, &mode_id)) {
|
||||
switch (mode_id) {
|
||||
case 1:
|
||||
mode = MM_UBLOX_NETWORKING_MODE_ROUTER;
|
||||
break;
|
||||
case 2:
|
||||
mode = MM_UBLOX_NETWORKING_MODE_BRIDGE;
|
||||
break;
|
||||
default:
|
||||
inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_UNSUPPORTED,
|
||||
"Unknown mode id: '%u'", mode_id);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 (mode == MM_UBLOX_NETWORKING_MODE_UNKNOWN) {
|
||||
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
|
||||
"Couldn't parse networking mode response");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
*out_mode = mode;
|
||||
return TRUE;
|
||||
}
|
||||
|
@@ -32,4 +32,17 @@ gboolean mm_ublox_parse_uusbconf_response (const gchar *response,
|
||||
MMUbloxUsbProfile *out_profile,
|
||||
GError **error);
|
||||
|
||||
/*****************************************************************************/
|
||||
/* UBMCONF? response parser */
|
||||
|
||||
typedef enum {
|
||||
MM_UBLOX_NETWORKING_MODE_UNKNOWN,
|
||||
MM_UBLOX_NETWORKING_MODE_ROUTER,
|
||||
MM_UBLOX_NETWORKING_MODE_BRIDGE,
|
||||
} MMUbloxNetworkingMode;
|
||||
|
||||
gboolean mm_ublox_parse_ubmconf_response (const gchar *response,
|
||||
MMUbloxNetworkingMode *out_mode,
|
||||
GError **error);
|
||||
|
||||
#endif /* MM_MODEM_HELPERS_UBLOX_H */
|
||||
|
@@ -66,6 +66,42 @@ test_uusbconf_response (void)
|
||||
}
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
/* Test UBMCONF? responses */
|
||||
|
||||
typedef struct {
|
||||
const gchar *str;
|
||||
MMUbloxNetworkingMode mode;
|
||||
} UbmconfResponseTest;
|
||||
|
||||
static const UbmconfResponseTest ubmconf_response_tests[] = {
|
||||
{
|
||||
.str = "+UBMCONF: 1\r\n",
|
||||
.mode = MM_UBLOX_NETWORKING_MODE_ROUTER
|
||||
},
|
||||
{
|
||||
.str = "+UBMCONF: 2\r\n",
|
||||
.mode = MM_UBLOX_NETWORKING_MODE_BRIDGE
|
||||
},
|
||||
};
|
||||
|
||||
static void
|
||||
test_ubmconf_response (void)
|
||||
{
|
||||
guint i;
|
||||
|
||||
for (i = 0; i < G_N_ELEMENTS (ubmconf_response_tests); i++) {
|
||||
MMUbloxNetworkingMode mode = MM_UBLOX_NETWORKING_MODE_UNKNOWN;
|
||||
GError *error = NULL;
|
||||
gboolean success;
|
||||
|
||||
success = mm_ublox_parse_ubmconf_response (ubmconf_response_tests[i].str, &mode, &error);
|
||||
g_assert_no_error (error);
|
||||
g_assert (success);
|
||||
g_assert_cmpuint (ubmconf_response_tests[i].mode, ==, mode);
|
||||
}
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
void
|
||||
@@ -96,6 +132,7 @@ int main (int argc, char **argv)
|
||||
g_test_init (&argc, &argv, NULL);
|
||||
|
||||
g_test_add_func ("/MM/ublox/uusbconf/response", test_uusbconf_response);
|
||||
g_test_add_func ("/MM/ublox/ubmconf/response", test_ubmconf_response);
|
||||
|
||||
return g_test_run ();
|
||||
}
|
||||
|
Reference in New Issue
Block a user