ublox: new +UPINCNT response parser

This commit is contained in:
Aleksander Morgado
2016-08-09 17:31:04 +02:00
parent fe460b2f54
commit 29517c99ba
3 changed files with 149 additions and 1 deletions

View File

@@ -20,6 +20,86 @@
#include "mm-modem-helpers.h" #include "mm-modem-helpers.h"
#include "mm-modem-helpers-ublox.h" #include "mm-modem-helpers-ublox.h"
/*****************************************************************************/
/* +UPINCNT response parser */
gboolean
mm_ublox_parse_upincnt_response (const gchar *response,
guint *out_pin_attempts,
guint *out_pin2_attempts,
guint *out_puk_attempts,
guint *out_puk2_attempts,
GError **error)
{
GRegex *r;
GMatchInfo *match_info;
GError *inner_error = NULL;
guint pin_attempts = 0;
guint pin2_attempts = 0;
guint puk_attempts = 0;
guint puk2_attempts = 0;
gboolean success = TRUE;
g_assert (out_pin_attempts);
g_assert (out_pin2_attempts);
g_assert (out_puk_attempts);
g_assert (out_puk2_attempts);
/* Response may be e.g.:
* +UPINCNT: 3,3,10,10
*/
r = g_regex_new ("\\+UPINCNT: (\\d+),(\\d+),(\\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)) {
if (!mm_get_uint_from_match_info (match_info, 1, &pin_attempts)) {
inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_UNSUPPORTED,
"Couldn't parse PIN attempts");
goto out;
}
if (!mm_get_uint_from_match_info (match_info, 2, &pin2_attempts)) {
inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_UNSUPPORTED,
"Couldn't parse PIN2 attempts");
goto out;
}
if (!mm_get_uint_from_match_info (match_info, 3, &puk_attempts)) {
inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_UNSUPPORTED,
"Couldn't parse PUK attempts");
goto out;
}
if (!mm_get_uint_from_match_info (match_info, 4, &puk2_attempts)) {
inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_UNSUPPORTED,
"Couldn't parse PUK2 attempts");
goto out;
}
success = TRUE;
}
out:
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 (!success) {
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"Couldn't parse +UPINCNT response: '%s'", response);
return FALSE;
}
*out_pin_attempts = pin_attempts;
*out_pin2_attempts = pin2_attempts;
*out_puk_attempts = puk_attempts;
*out_puk2_attempts = puk2_attempts;
return TRUE;
}
/*****************************************************************************/ /*****************************************************************************/
/* UUSBCONF? response parser */ /* UUSBCONF? response parser */

View File

@@ -19,6 +19,16 @@
#include <glib.h> #include <glib.h>
#include <ModemManager.h> #include <ModemManager.h>
/*****************************************************************************/
/* +UPINCNT response parser */
gboolean mm_ublox_parse_upincnt_response (const gchar *response,
guint *out_pin_attempts,
guint *out_pin2_attempts,
guint *out_puk_attempts,
guint *out_puk2_attempts,
GError **error);
/*****************************************************************************/ /*****************************************************************************/
/* UUSBCONF? response parser */ /* UUSBCONF? response parser */

View File

@@ -26,6 +26,64 @@
#include "mm-modem-helpers.h" #include "mm-modem-helpers.h"
#include "mm-modem-helpers-ublox.h" #include "mm-modem-helpers-ublox.h"
/*****************************************************************************/
/* Test +UPINCNT responses */
typedef struct {
const gchar *str;
guint pin_attempts;
guint pin2_attempts;
guint puk_attempts;
guint puk2_attempts;
} UpinCntResponseTest;
static const UpinCntResponseTest upincnt_response_tests[] = {
{ .str = "+UPINCNT: 3,3,10,10\r\n",
.pin_attempts = 3,
.pin2_attempts = 3,
.puk_attempts = 10,
.puk2_attempts = 10
},
{ .str = "+UPINCNT: 0,3,5,5\r\n",
.pin_attempts = 0,
.pin2_attempts = 3,
.puk_attempts = 5,
.puk2_attempts = 5
},
{ .str = "+UPINCNT: 0,0,0,0\r\n",
.pin_attempts = 0,
.pin2_attempts = 0,
.puk_attempts = 0,
.puk2_attempts = 0
},
};
static void
test_upincnt_response (void)
{
guint i;
for (i = 0; i < G_N_ELEMENTS (upincnt_response_tests); i++) {
GError *error = NULL;
gboolean success;
guint pin_attempts = G_MAXUINT;
guint pin2_attempts = G_MAXUINT;
guint puk_attempts = G_MAXUINT;
guint puk2_attempts = G_MAXUINT;
success = mm_ublox_parse_upincnt_response (upincnt_response_tests[i].str,
&pin_attempts, &pin2_attempts,
&puk_attempts, &puk2_attempts,
&error);
g_assert_no_error (error);
g_assert (success);
g_assert_cmpuint (upincnt_response_tests[i].pin_attempts, ==, pin_attempts);
g_assert_cmpuint (upincnt_response_tests[i].pin2_attempts, ==, pin2_attempts);
g_assert_cmpuint (upincnt_response_tests[i].puk_attempts, ==, puk_attempts);
g_assert_cmpuint (upincnt_response_tests[i].puk2_attempts, ==, puk2_attempts);
}
}
/*****************************************************************************/ /*****************************************************************************/
/* Test UUSBCONF? responses */ /* Test UUSBCONF? responses */
@@ -454,7 +512,7 @@ int main (int argc, char **argv)
g_type_init (); g_type_init ();
g_test_init (&argc, &argv, NULL); g_test_init (&argc, &argv, NULL);
g_test_add_func ("/MM/ublox/upincnt/response", test_upincnt_response);
g_test_add_func ("/MM/ublox/uusbconf/response", test_uusbconf_response); g_test_add_func ("/MM/ublox/uusbconf/response", test_uusbconf_response);
g_test_add_func ("/MM/ublox/ubmconf/response", test_ubmconf_response); g_test_add_func ("/MM/ublox/ubmconf/response", test_ubmconf_response);
g_test_add_func ("/MM/ublox/uipaddr/response", test_uipaddr_response); g_test_add_func ("/MM/ublox/uipaddr/response", test_uipaddr_response);