core: process SMS +CGMR response with regex
Variability in the response style from certain modems causes the parsing of the +CGMR response to fail. For example, the Telit HE910 inserts an empty string ("") in the second field of the response, causing the sscanf implementation to fail. This patch converts the parsing of the CGMR response to a regex that allows for more flexibility and robustness, and adds unit tests around the parsing call. Signed-off-by: Nick Stevens <Nick.Stevens@digi.com>
This commit is contained in:

committed by
Aleksander Morgado

parent
0ca68657fb
commit
eaf65ed98d
@@ -1201,6 +1201,77 @@ mm_3gpp_parse_cmgf_test_response (const gchar *reply,
|
||||
|
||||
/*************************************************************************/
|
||||
|
||||
MM3gppPduInfo *
|
||||
mm_3gpp_parse_cmgr_read_response (const gchar *reply,
|
||||
guint index,
|
||||
GError **error)
|
||||
{
|
||||
GRegex *r;
|
||||
GMatchInfo *match_info;
|
||||
gint count;
|
||||
gint status;
|
||||
gchar *pdu;
|
||||
MM3gppPduInfo *info = NULL;
|
||||
|
||||
/* +CMGR: <stat>,<alpha>,<length>(whitespace)<pdu> */
|
||||
/* The <alpha> and <length> fields are matched, but not currently used */
|
||||
r = g_regex_new ("\\+CMGR:\\s*(\\d+)\\s*,([^,]*),\\s*(\\d+)\\s*([^\\r\\n]*)", 0, 0, NULL);
|
||||
g_assert (r);
|
||||
|
||||
if (!g_regex_match_full (r, reply, strlen (reply), 0, 0, &match_info, NULL)) {
|
||||
g_set_error (error,
|
||||
MM_CORE_ERROR,
|
||||
MM_CORE_ERROR_FAILED,
|
||||
"Failed to parse CMGR read result: response didn't match '%s'",
|
||||
reply);
|
||||
goto done;
|
||||
}
|
||||
|
||||
/* g_match_info_get_match_count includes match #0 */
|
||||
if ((count = g_match_info_get_match_count (match_info)) != 5) {
|
||||
g_set_error (error,
|
||||
MM_CORE_ERROR,
|
||||
MM_CORE_ERROR_FAILED,
|
||||
"Failed to match CMGR fields (matched %d) '%s'",
|
||||
count,
|
||||
reply);
|
||||
goto done;
|
||||
}
|
||||
|
||||
if (!mm_get_int_from_match_info (match_info, 1, &status)) {
|
||||
g_set_error (error,
|
||||
MM_CORE_ERROR,
|
||||
MM_CORE_ERROR_FAILED,
|
||||
"Failed to extract CMGR status field '%s'",
|
||||
reply);
|
||||
goto done;
|
||||
}
|
||||
|
||||
|
||||
pdu = mm_get_string_unquoted_from_match_info (match_info, 4);
|
||||
if (!pdu) {
|
||||
g_set_error (error,
|
||||
MM_CORE_ERROR,
|
||||
MM_CORE_ERROR_FAILED,
|
||||
"Failed to extract CMGR pdu field '%s'",
|
||||
reply);
|
||||
goto done;
|
||||
}
|
||||
|
||||
info = g_new0 (MM3gppPduInfo, 1);
|
||||
info->index = index;
|
||||
info->status = status;
|
||||
info->pdu = pdu;
|
||||
|
||||
done:
|
||||
g_match_info_free (match_info);
|
||||
g_regex_unref (r);
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
/*************************************************************************/
|
||||
|
||||
static MMSmsStorage
|
||||
storage_from_str (const gchar *str)
|
||||
{
|
||||
@@ -1698,7 +1769,7 @@ done:
|
||||
|
||||
/*************************************************************************/
|
||||
|
||||
static void
|
||||
void
|
||||
mm_3gpp_pdu_info_free (MM3gppPduInfo *info)
|
||||
{
|
||||
g_free (info->pdu);
|
||||
|
Reference in New Issue
Block a user