<div dir="ltr"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Pushed to git master. I also pushed a follow up commit to use<br>
mm_get_uint_from_hex_str():<br>
<a href="https://cgit.freedesktop.org/ModemManager/ModemManager/commit/?id=d09bc8baaa9fe93a72bb715530b1403a7a81c891" rel="noreferrer" target="_blank">https://cgit.freedesktop.org/<wbr>ModemManager/ModemManager/<wbr>commit/?id=<wbr>d09bc8baaa9fe93a72bb715530b140<wbr>3a7a81c891</a><br>
<br>
Thanks!</div></blockquote><div><br><div style="font-family:arial,helvetica,sans-serif;display:inline" class="gmail_default">​Awesome, thanks to you!​</div> </div></div><div class="gmail_extra"><br><div class="gmail_quote">On 19 April 2017 at 10:46, Aleksander Morgado <span dir="ltr"><<a href="mailto:aleksander@aleksander.es" target="_blank">aleksander@aleksander.es</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 19/04/17 10:00, Carlo Lobrano wrote:<br>
> - Refactored mm_telit_parse_csim_response in order to correctly<br>
>   recognize the following +CSIM error codes:<br>
>     * 6300 - Verification failed<br>
>     * 6983 - Authentication method blocked<br>
>     * 6984 - Reference data invalidated<br>
>     * 6A86 - Incorrect parameters<br>
>     * 6A88 - Reference data not found<br>
><br>
> - Updated correspondent tests.<br>
> - Finally, some minor changes in other files for better error logging<br>
><br>
> Fixes: <a href="https://bugs.freedesktop.org/show_bug.cgi?id=100374" rel="noreferrer" target="_blank">https://bugs.freedesktop.org/<wbr>show_bug.cgi?id=100374</a><br>
<br>
</span>Pushed to git master. I also pushed a follow up commit to use<br>
mm_get_uint_from_hex_str():<br>
<a href="https://cgit.freedesktop.org/ModemManager/ModemManager/commit/?id=d09bc8baaa9fe93a72bb715530b1403a7a81c891" rel="noreferrer" target="_blank">https://cgit.freedesktop.org/<wbr>ModemManager/ModemManager/<wbr>commit/?id=<wbr>d09bc8baaa9fe93a72bb715530b140<wbr>3a7a81c891</a><br>
<br>
Thanks!<br>
<div><div class="h5"><br>
> ---<br>
>  plugins/telit/mm-broadband-<wbr>modem-telit.c          |  6 +-<br>
>  plugins/telit/mm-modem-<wbr>helpers-telit.c            | 98 ++++++++++++++++-------<br>
>  plugins/telit/mm-modem-<wbr>helpers-telit.h            |  3 +-<br>
>  plugins/telit/tests/test-mm-<wbr>modem-helpers-telit.c | 73 ++++++++---------<br>
>  4 files changed, 104 insertions(+), 76 deletions(-)<br>
><br>
> diff --git a/plugins/telit/mm-broadband-<wbr>modem-telit.c b/plugins/telit/mm-broadband-<wbr>modem-telit.c<br>
> index 8b87310..d154fc6 100644<br>
> --- a/plugins/telit/mm-broadband-<wbr>modem-telit.c<br>
> +++ b/plugins/telit/mm-broadband-<wbr>modem-telit.c<br>
> @@ -541,13 +541,13 @@ csim_query_ready (MMBaseModem *self,<br>
>      response = mm_base_modem_at_command_<wbr>finish (self, res, &error);<br>
><br>
>      if (!response) {<br>
> -        mm_warn ("No respose for step %d: %s", ctx->step, error->message);<br>
> +        mm_warn ("load unlock retries: no respose for step %d: %s", ctx->step, error->message);<br>
>          g_error_free (error);<br>
>          goto next_step;<br>
>      }<br>
><br>
> -    if ( (unlock_retries = mm_telit_parse_csim_response (ctx->step, response, &error)) < 0) {<br>
> -        mm_warn ("Parse error in step %d: %s.", ctx->step, error->message);<br>
> +    if ( (unlock_retries = mm_telit_parse_csim_response (response, &error)) < 0) {<br>
> +        mm_warn ("load unlock retries: parse error in step %d: %s.", ctx->step, error->message);<br>
>          g_error_free (error);<br>
>          goto next_step;<br>
>      }<br>
> diff --git a/plugins/telit/mm-modem-<wbr>helpers-telit.c b/plugins/telit/mm-modem-<wbr>helpers-telit.c<br>
> index c8c1f4b..1dd2542 100644<br>
> --- a/plugins/telit/mm-modem-<wbr>helpers-telit.c<br>
> +++ b/plugins/telit/mm-modem-<wbr>helpers-telit.c<br>
> @@ -17,6 +17,7 @@<br>
>  #include <stdlib.h><br>
>  #include <string.h><br>
>  #include <stdio.h><br>
> +#include <errno.h><br>
><br>
>  #include <ModemManager.h><br>
>  #define _LIBMM_INSIDE_MMCLI<br>
> @@ -113,54 +114,91 @@ mm_telit_get_band_flag (GArray *bands_array,<br>
><br>
>  /*****************************<wbr>******************************<wbr>******************/<br>
>  /* +CSIM response parser */<br>
> +#define MM_TELIT_MIN_SIM_RETRY_HEX 0x63C0<br>
> +#define MM_TELIT_MAX_SIM_RETRY_HEX 0x63CF<br>
><br>
>  gint<br>
> -mm_telit_parse_csim_response (const guint step,<br>
> -                              const gchar *response,<br>
> +mm_telit_parse_csim_response (const gchar *response,<br>
>                                GError **error)<br>
>  {<br>
> -    GRegex *r = NULL;<br>
>      GMatchInfo *match_info = NULL;<br>
> -    gchar *retries_hex_str;<br>
> -    guint retries;<br>
> +    GRegex *r = NULL;<br>
> +    gchar *str_code = NULL;<br>
> +    gint retries = -1;<br>
> +    guint64 hex_code = 0x0;<br>
> +    GError *inner_error = NULL;<br>
><br>
> -    r = g_regex_new ("\\+CSIM:\\s*[0-9]+,\\s*.*<wbr>63C(.*)\"", G_REGEX_RAW, 0, NULL);<br>
> +    r = g_regex_new ("\\+CSIM:\\s*[0-9]+,\\s*\".*(<wbr>[0-9a-fA-F]{4})\"", G_REGEX_RAW, 0, NULL);<br>
> +    g_regex_match (r, response, 0, &match_info);<br>
><br>
> -    if (!g_regex_match (r, response, 0, &match_info)) {<br>
> -        g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,<br>
> -                     "Could not parse reponse '%s'", response);<br>
> -        g_match_info_free (match_info);<br>
> -        g_regex_unref (r);<br>
> -        return -1;<br>
> +    if (!g_match_info_matches (match_info)) {<br>
> +        inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED,<br>
> +                                   "Could not recognize +CSIM response '%s'", response);<br>
> +        goto out;<br>
>      }<br>
><br>
> -    if (!g_match_info_matches (match_info)) {<br>
> -        g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,<br>
> -                     "Could not find matches in response '%s'", response);<br>
> -        g_match_info_free (match_info);<br>
> -        g_regex_unref (r);<br>
> -        return -1;<br>
> +    str_code = mm_get_string_unquoted_from_<wbr>match_info (match_info, 1);<br>
> +    if (str_code == NULL) {<br>
> +        inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED,<br>
> +                                   "Could not find expected string code in response '%s'", response);<br>
> +        goto out;<br>
>      }<br>
><br>
> -    retries_hex_str = mm_get_string_unquoted_from_<wbr>match_info (match_info, 1);<br>
> -    g_assert (NULL != retries_hex_str);<br>
> +    errno = 0;<br>
> +    hex_code = g_ascii_strtoull (str_code, NULL, 16);<br>
> +    if (hex_code == G_MAXUINT64 && errno == ERANGE) {<br>
> +        inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED,<br>
> +                                   "Could not recognize expected hex code in response '%s'", response);<br>
> +        goto out;<br>
> +    }<br>
><br>
> -    /* convert hex value to uint */<br>
> -    if (sscanf (retries_hex_str, "%x", &retries) != 1) {<br>
> -         g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,<br>
> -                     "Could not get retry value from match '%s'",<br>
> -                     retries_hex_str);<br>
> -        g_match_info_free (match_info);<br>
> -        g_regex_unref (r);<br>
> -        return -1;<br>
> +    switch (hex_code) {<br>
> +        case 0x6300:<br>
> +            inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED,<br>
> +                                       "SIM verification failed");<br>
> +            goto out;<br>
> +        case 0x6983:<br>
> +            inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED,<br>
> +                                       "SIM authentication method blocked");<br>
> +            goto out;<br>
> +        case 0x6984:<br>
> +            inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED,<br>
> +                                       "SIM reference data invalidated");<br>
> +            goto out;<br>
> +        case 0x6A86:<br>
> +            inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED,<br>
> +                                       "Incorrect parameters in SIM request");<br>
> +            goto out;<br>
> +        case 0x6A88:<br>
> +            inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED,<br>
> +                                       "SIM reference data not found");<br>
> +            goto out;<br>
> +        default:<br>
> +            break;<br>
>      }<br>
><br>
> -    g_free (retries_hex_str);<br>
> -    g_match_info_free (match_info);<br>
> +    if (hex_code < MM_TELIT_MIN_SIM_RETRY_HEX || hex_code > MM_TELIT_MAX_SIM_RETRY_HEX) {<br>
> +        inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED,<br>
> +                                   "Unknown error returned '0x%04lx'", hex_code);<br>
> +        goto out;<br>
> +    }<br>
> +<br>
> +    retries = (gint)(hex_code - MM_TELIT_MIN_SIM_RETRY_HEX);<br>
> +<br>
> +out:<br>
>      g_regex_unref (r);<br>
> +    g_match_info_free (match_info);<br>
> +    g_free (str_code);<br>
> +<br>
> +    if (inner_error) {<br>
> +        g_propagate_error (error, inner_error);<br>
> +        return -1;<br>
> +    }<br>
><br>
> +    g_assert (retries >= 0);<br>
>      return retries;<br>
>  }<br>
> +<br>
>  #define SUPP_BAND_RESPONSE_REGEX          "#BND:\\s*\\((?P<Bands2G>[0-9\<wbr>\-,]*)\\)(,\\s*\\((?P<Bands3G><wbr>[0-9\\-,]*)\\))?(,\\s*\\((?P<<wbr>Bands4G>[0-9\\-,]*)\\))?"<br>
>  #define CURR_BAND_RESPONSE_REGEX          "#BND:\\s*(?P<Bands2G>\\d+)(,\<wbr>\s*(?P<Bands3G>\\d+))?(,\\s*(?<wbr>P<Bands4G>\\d+))?"<br>
><br>
> diff --git a/plugins/telit/mm-modem-<wbr>helpers-telit.h b/plugins/telit/mm-modem-<wbr>helpers-telit.h<br>
> index 5f0e880..b693732 100644<br>
> --- a/plugins/telit/mm-modem-<wbr>helpers-telit.h<br>
> +++ b/plugins/telit/mm-modem-<wbr>helpers-telit.h<br>
> @@ -61,8 +61,7 @@ typedef struct {<br>
>  } TelitToMMBandMap;<br>
><br>
>  /* +CSIM response parser */<br>
> -gint mm_telit_parse_csim_response (const guint step,<br>
> -                                   const gchar *response,<br>
> +gint mm_telit_parse_csim_response (const gchar *response,<br>
>                                     GError **error);<br>
><br>
>  typedef enum {<br>
> diff --git a/plugins/telit/tests/test-mm-<wbr>modem-helpers-telit.c b/plugins/telit/tests/test-mm-<wbr>modem-helpers-telit.c<br>
> index 40e3628..3008010 100644<br>
> --- a/plugins/telit/tests/test-mm-<wbr>modem-helpers-telit.c<br>
> +++ b/plugins/telit/tests/test-mm-<wbr>modem-helpers-telit.c<br>
> @@ -28,66 +28,57 @@<br>
>  typedef struct {<br>
>      gchar *response;<br>
>      gint result;<br>
> +    gchar *error_message;<br>
>  } CSIMResponseTest;<br>
><br>
> -static CSIMResponseTest valid_csim_response_test_list [] = {<br>
> +static CSIMResponseTest csim_response_test_list [] = {<br>
>      /* The parser expects that 2nd arg contains<br>
>       * substring "63Cx" where x is an HEX string<br>
>       * representing the retry value */<br>
> -    {"+CSIM:8,\"xxxx63C1\"", 1},<br>
> -    {"+CSIM:8,\"xxxx63CA\"", 10},<br>
> -    {"+CSIM:8,\"xxxx63CF\"", 15},<br>
> +    {"+CSIM:8,\"000063C1\"", 1, NULL},<br>
> +    {"+CSIM:8,\"000063CA\"", 10, NULL},<br>
> +    {"+CSIM:8,\"000063CF\"", 15, NULL},<br>
>      /* The parser accepts spaces */<br>
> -    {"+CSIM:8,\"xxxx63C1\"", 1},<br>
> -    {"+CSIM:8, \"xxxx63C1\"", 1},<br>
> -    {"+CSIM: 8, \"xxxx63C1\"", 1},<br>
> -    {"+CSIM:  8, \"xxxx63C1\"", 1},<br>
> +    {"+CSIM:8, \"000063C1\"", 1, NULL},<br>
> +    {"+CSIM: 8, \"000063C1\"", 1, NULL},<br>
> +    {"+CSIM:  8, \"000063C1\"", 1, NULL},<br>
>      /* the parser expects an int as first argument (2nd arg's length),<br>
>       * but does not check if it is correct */<br>
> -    {"+CSIM: 10, \"63CF\"", 15},<br>
> -    /* the parser expect a quotation mark at the end<br>
> -     * of the response, but not at the begin */<br>
> -    {"+CSIM: 10, 63CF\"", 15},<br>
> -    { NULL, -1}<br>
> -};<br>
> -<br>
> -static CSIMResponseTest invalid_csim_response_test_<wbr>list [] = {<br>
> -    /* Test missing final quotation mark */<br>
> -    {"+CSIM: 8, xxxx63CF", -1},<br>
> -    /* Negative test for substring "63Cx" */<br>
> -    {"+CSIM: 4, xxxx73CF\"", -1},<br>
> -    /* Test missing first argument */<br>
> -    {"+CSIM:xxxx63CF\"", -1},<br>
> -    { NULL, -1}<br>
> +    {"+CSIM: 10, \"63CF\"", 15, NULL},<br>
> +    /* Valid +CSIM Error codes */<br>
> +    {"+CSIM: 4, \"6300\"", -1, "SIM verification failed"},<br>
> +    {"+CSIM: 4, \"6983\"", -1, "SIM authentication method blocked"},<br>
> +    {"+CSIM: 4, \"6984\"", -1, "SIM reference data invalidated"},<br>
> +    {"+CSIM: 4, \"6A86\"", -1, "Incorrect parameters in SIM request"},<br>
> +    {"+CSIM: 4, \"6A88\"", -1, "SIM reference data not found"},<br>
> +    /* Test error: missing first argument */<br>
> +    {"+CSIM:000063CF\"", -1, "Could not recognize +CSIM response '+CSIM:000063CF\"'"},<br>
> +    /* Test error: missing quotation mark */<br>
> +    {"+CSIM: 8, 000063CF", -1, "Could not recognize +CSIM response '+CSIM: 8, 000063CF'"},<br>
> +    /* Test generic error */<br>
> +    {"+CSIM: 4, \"63BF\"", -1, "Unknown error returned '0x63bf'"},<br>
> +    {"+CSIM: 4, \"63D0\"", -1, "Unknown error returned '0x63d0'"}<br>
>  };<br>
><br>
>  static void<br>
>  test_mm_telit_parse_csim_<wbr>response (void)<br>
>  {<br>
> -    const gint step = 1;<br>
>      guint i;<br>
>      gint res;<br>
>      GError* error = NULL;<br>
><br>
> -    /* Test valid responses */<br>
> -    for (i = 0; valid_csim_response_test_list[<wbr>i].response != NULL; i++) {<br>
> -        res = mm_telit_parse_csim_response (step, valid_csim_response_test_list[<wbr>i].response, &error);<br>
> -<br>
> -        g_assert_no_error (error);<br>
> -        g_assert_cmpint (res, ==, valid_csim_response_test_list[<wbr>i].result);<br>
> -    }<br>
> -<br>
> -    /* Test invalid responses */<br>
> -    for (i = 0; invalid_csim_response_test_<wbr>list[i].response != NULL; i++) {<br>
> -        res = mm_telit_parse_csim_response (step, invalid_csim_response_test_<wbr>list[i].response, &error);<br>
> +    for (i = 0; i < G_N_ELEMENTS (csim_response_test_list); i++) {<br>
> +        res = mm_telit_parse_csim_response (csim_response_test_list[i].<wbr>response, &error);<br>
><br>
> -        g_assert_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED);<br>
> -        g_assert_cmpint (res, ==, invalid_csim_response_test_<wbr>list[i].result);<br>
> -<br>
> -        if (NULL != error) {<br>
> -            g_error_free (error);<br>
> -            error = NULL;<br>
> +        if (csim_response_test_list[i].<wbr>error_message == NULL) {<br>
> +            g_assert_no_error (error);<br>
> +        } else {<br>
> +            g_assert_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED);<br>
> +            g_assert_cmpstr (error->message, ==, csim_response_test_list[i].<wbr>error_message);<br>
> +            g_clear_error (&error);<br>
>          }<br>
> +<br>
> +        g_assert_cmpint (res, ==, csim_response_test_list[i].<wbr>result);<br>
>      }<br>
>  }<br>
><br>
><br>
<br>
<br>
--<br>
</div></div>Aleksander<br>
<a href="https://aleksander.es" rel="noreferrer" target="_blank">https://aleksander.es</a><br>
</blockquote></div><br></div>