<div dir="ltr">Hi Aleksander,<div><br></div><div>see below my reply<br><br>Il giovedì 6 aprile 2017, Aleksander Morgado <<a href="mailto:aleksander@aleksander.es" target="_blank">aleksander@aleksander.es</a>> ha scritto:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hey Carlo,<br>
<br>
On 04/04/17 14:55, Carlo Lobrano wrote:<br>
> - Refactored mm_telit_parse_csim_response in order to correctly recognize the<br>
> following +CSIM error codes:<br>
><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" target="_blank">https://bugs.freedesktop.org/s<wbr>how_bug.cgi?id=100374</a><br>
><br>
> ---<br>
><br>
> As a side note, I observed that sometimes the modem replies with error<br>
> code<br>
><br>
> 6A86 - Incorrect parameters<br>
><br>
> when #QSS: 3 has not been received yet. This seems to be a modem's bug<br>
> because the very same request is accepted as correct when issued later,<br>
> namely when the SIM is ready.<br>
><br>
<br>
See comments inline below.<br>
<br>
> ---<br>
> plugins/telit/mm-broadband-mod<wbr>em-telit.c | 6 +-<br>
> plugins/telit/mm-modem-helpers<wbr>-telit.c | 93 ++++++++++++++++-------<br>
> plugins/telit/mm-modem-helpers<wbr>-telit.h | 3 +-<br>
> plugins/telit/tests/test-mm-mo<wbr>dem-helpers-telit.c | 72 +++++++++---------<br>
> 4 files changed, 105 insertions(+), 69 deletions(-)<br>
><br>
> diff --git a/plugins/telit/mm-broadband-m<wbr>odem-telit.c b/plugins/telit/mm-broadband-m<wbr>odem-telit.c<br>
> index cce0229..f316e30 100644<br>
> --- a/plugins/telit/mm-broadband-m<wbr>odem-telit.c<br>
> +++ b/plugins/telit/mm-broadband-m<wbr>odem-telit.c<br>
> @@ -541,13 +541,13 @@ csim_query_ready (MMBaseModem *self,<br>
> response = mm_base_modem_at_command_finis<wbr>h (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>
<br>
I don't think we should be printing the "step number", especially not<br>
via warning. This information would be useful if instead we gave the<br>
actual lock name associated to the step, though.<br>
<br>
As the csim_query_ready() is being reused for multiple locks, we could<br>
have an array of strings specifying which lock is being processed at<br>
each step, e.g.:<br>
<br>
static const gchar *step_lock_names[LOAD_UNLOCK_R<wbr>ETRIES_STEP_LAST] = {<br>
[LOAD_UNLOCK_RETRIES_STEP_PIN] = "PIN",<br>
[LOAD_UNLOCK_RETRIES_STEP_PUK] = "PUK",<br>
[LOAD_UNLOCK_RETRIES_STEP_PIN2<wbr>] = "PIN2",<br>
[LOAD_UNLOCK_RETRIES_STEP_PUK2<wbr>] = "PUK2",<br>
};<br>
<br>
What do you think? I know this issue was already there, but just spotted<br>
it :) Maybe handled in a separate new patch better?<br>
<br></blockquote><div><br></div><div>I agree, the "step" thing is just for developer (myself, specifically :D), and I prefer the idea to use a separate patch.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<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>
<br>
Same here with the step number.<br></blockquote><div><br></div><div>right</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
> g_error_free (error);<br>
> goto next_step;<br>
> }<br>
> diff --git a/plugins/telit/mm-modem-helpe<wbr>rs-telit.c b/plugins/telit/mm-modem-helpe<wbr>rs-telit.c<br>
> index c8c1f4b..cb3ff24 100644<br>
> --- a/plugins/telit/mm-modem-helpe<wbr>rs-telit.c<br>
> +++ b/plugins/telit/mm-modem-helpe<wbr>rs-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,92 @@ 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>
> -<br>
> - r = g_regex_new ("\\+CSIM:\\s*[0-9]+,\\s*.*63C<wbr>(.*)\"", G_REGEX_RAW, 0, NULL);<br>
> + GRegex *r = NULL;<br>
> + gchar *str_code = NULL;<br>
> + gint retries = -1;<br>
> + guint64 hex_code = 0x0;<br>
><br>
> + r = g_regex_new ("\\+CSIM:\\s*[0-9]+,\\s*.*([0<wbr>-9a-fA-F]{4})\"", G_REGEX_RAW, 0, NULL);<br>
<br>
The regex is matching the trailing double-quotes; why not the leading<br>
ones as well? E.g.:<br>
<br>
"\\+CSIM:\\s*[0-9]+,\\s*\".*([<wbr>0-9a-fA-F]{4})\""<br></blockquote><div><br></div><div>I'm not sure it's really necessary. I'm still accepting everything (.*) until a string with an hex appears ([0-9...]) and I need the trailing double-quotes just to stop the parser, but I think it does not hurt to specify the beginning of the string :)</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<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>
> + "Could not recognize response '%s'", response);<br>
> + goto out;<br>
> }<br>
><br>
> - if (!g_match_info_matches (match_info)) {<br>
> + if (match_info == NULL || !g_match_info_matches (match_info)) {<br>
<br>
match_info is always created by g_regex_match(), so no need to check if<br>
it is == NULL here. Actually, you could also ignore the g_regex_match()<br>
return value all together if you're afterwards running<br>
g_match_info_matches().<br></blockquote><div><br></div><div>I see. Ok, I can simplify it a bit</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<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>
> + "Could not parse response '%s'", response);<br>
> + goto out;<br>
> + }<br>
> +<br>
> + str_code = mm_get_string_unquoted_from_ma<wbr>tch_info (match_info, 1);<br>
> + if (str_code == NULL) {<br>
> + g_set_error (error, 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_ma<wbr>tch_info (match_info, 1);<br>
> - g_assert (NULL != retries_hex_str);<br>
<br>
This is a good change; mm_get_string_unquoted_from_ma<wbr>tch_info() may be<br>
returning NULL if the matched string is empty, so the g_assert() was a<br>
bit too much.<br>
<br>
> + errno = 0;<br>
> + hex_code = g_ascii_strtoull (str_code, NULL, 16);<br>
> + if (hex_code == G_MAXUINT64 && errno == ERANGE) {<br>
> + g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,<br>
> + "Could not recognize expected hex code in response '%s'", response);<br>
> + goto out;<br>
> + }<br>
<br>
And this one may be too much, as we really just matched a 4-digit hex<br>
string with the regex, so the chances of a failure here are 0. But<br>
anyway, it's safer in this way in case the regex changes in the future,<br>
so good.</blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<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>
<br>
A whitespace before the "(" is missing.<br></blockquote><div><br></div><div>Damn :/</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
> + case 0x6300:<br>
> + g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,<br>
> + "SIM verification failed");<br>
> + break;<br>
<br>
Instead of "break;" just "goto out;" is perfectly fine here and in all<br>
the other cases.<br>
<br>
> + case 0x6983:<br>
> + g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,<br>
> + "SIM authentication method blocked");<br>
> + break;<br>
> + case 0x6984:<br>
> + g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,<br>
> + "SIM reference data invalidated");<br>
> + break;<br>
> + case 0x6A86:<br>
> + g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,<br>
> + "Incorrect parameters in SIM request");<br>
> + break;<br>
> + case 0x6A88:<br>
> + g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,<br>
> + "SIM reference data not found");<br>
> + break;<br>
> + default:<br>
> + break;<br>
> + }<br>
> +<br>
> + if (g_error_matches (*error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED)) {<br>
> + goto out;<br>
> + }<br>
<br>
This isn't good practice, you shouldn't assume that the caller passes a<br>
valid GError address, the method should also accept NULL as GError<br>
address so that the caller ignores it.<br>
<br>
A better approach would be to have a "GError *inner_error;" declared by<br>
yourself inside the method, use g_error_new() to create the GError, and<br>
then, before returning -1 in the method, do something like:<br>
<br>
...<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></blockquote><div><br></div><div>Indeed, I'll fix that</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
> +<br>
> + if (hex_code < MM_TELIT_MIN_SIM_RETRY_HEX || hex_code > MM_TELIT_MAX_SIM_RETRY_HEX) {<br>
> + g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,<br>
> + "SIM retries out of bound in response '%s'", response);<br>
<br>
If we get a value out of bounds, wouldn't it really be another error we<br>
don't know about (and therefore not handled in the switch above)? Maybe<br>
we could just:<br>
g_set_error (..., "Unknown error returned: 0x%04x", hex_code);<br>
<br>
i.e. instead of saying "out of bounds".<br></blockquote><div><br></div><div>Ah, yes, that could happen.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
> + } else {<br>
> + retries = (gint)(hex_code - MM_TELIT_MIN_SIM_RETRY_HEX);<br>
> }<br>
><br>
> - g_free (retries_hex_str);<br>
> +out:<br>
> + if (str_code != NULL)<br>
> + g_free (str_code);<br>
<br>
No need to check the pointer being != NULL before calling g_free();<br>
g_free() (as also does free()) accepts the NULL pointer and will just do </blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">nothing.<br></blockquote><div><br></div><div>Ok</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
> g_match_info_free (match_info);<br>
> g_regex_unref (r);<br>
><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<B<wbr>ands4G>[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-helpe<wbr>rs-telit.h b/plugins/telit/mm-modem-helpe<wbr>rs-telit.h<br>
> index 5f0e880..b693732 100644<br>
> --- a/plugins/telit/mm-modem-helpe<wbr>rs-telit.h<br>
> +++ b/plugins/telit/mm-modem-helpe<wbr>rs-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..8b7a808 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,64 @@<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,\"xxxx63C1\"", 1, NULL},<br>
> + {"+CSIM:8,\"xxxx63CA\"", 10, NULL},<br>
> + {"+CSIM:8,\"xxxx63CF\"", 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,\"xxxx63C1\"", 1, NULL},<br>
> + {"+CSIM:8, \"xxxx63C1\"", 1, NULL},<br>
> + {"+CSIM: 8, \"xxxx63C1\"", 1, NULL},<br>
> + {"+CSIM: 8, \"xxxx63C1\"", 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>
> + {"+CSIM: 10, \"63CF\"", 15, NULL},<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_lis<wbr>t [] = {<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>
> + * of the response, but not at the beginning */<br>
> + {"+CSIM: 4, 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: out of bound */<br>
> + {"+CSIM: 4, \"63BF\"", -1, "SIM retries out of bound in response '+CSIM: 4, \"63BF\"'"},<br>
> + {"+CSIM: 4, \"63D0\"", -1, "SIM retries out of bound in response '+CSIM: 4, \"63D0\"'"},<br>
> + /* Test error: missing final quotation mark */<br>
> + {"+CSIM: 8, xxxx63CF", -1, "Could not recognize response '+CSIM: 8, xxxx63CF'"},<br>
> + /* Test error: missing first argument */<br>
> + {"+CSIM:xxxx63CF\"", -1, "Could not recognize response '+CSIM:xxxx63CF\"'"},<br>
> { NULL, -1}<br>
<br>
I know this wasn't part of the patch, but I would remove this last<br>
{NULL, -1} element, and then use G_N_ELEMENTS() when iterating below.<br>
Given that you're merging two arrays together, this may be a good moment<br>
to do that change as well.<br>
<br>
> };<br>
><br>
> static void<br>
> test_mm_telit_parse_csim_respo<wbr>nse (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_lis<wbr>t[i].response != NULL; i++) {<br>
> - res = mm_telit_parse_csim_response (step, invalid_csim_response_test_lis<wbr>t[i].response, &error);<br>
> -<br>
> - g_assert_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED);<br>
> - g_assert_cmpint (res, ==, invalid_csim_response_test_lis<wbr>t[i].result);<br>
> -<br>
> - if (NULL != error) {<br>
> + for (i = 0; csim_response_test_list[i].res<wbr>ponse != NULL; i++) {<br>
<br>
<br>
E.g.:<br>
<br>
for (i = 0; i < G_N_ELEMENTS (csim_response_test_list); i++) {<br>
...<br>
<br>
> + res = mm_telit_parse_csim_response (csim_response_test_list[i].re<wbr>sponse, &error);<br>
> +<br>
> + if (csim_response_test_list[i].er<wbr>ror_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].err<wbr>or_message);<br>
> g_error_free (error);<br>
> error = NULL;<br>
<br>
Also not part of the changes you're doing but let's use this to free the<br>
error and reset the pointer to NULL at the same time:<br>
g_clear_error (&error);<br>
<br>
> }<br>
> +<br>
> + g_assert_cmpint (res, ==, csim_response_test_list[i].res<wbr>ult);<br>
> }<br>
> }<br>
><br>
><br></blockquote><div><br><div style="font-family:arial,helvetica,sans-serif;display:inline" class="gmail_default">Ok, I'll be back with all the changes<br><br></div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<br>
--<br>
Aleksander<br>
<a href="https://aleksander.es" target="_blank">https://aleksander.es</a><br>
</blockquote></div>
</div>