helpers: new '+CLCC' test response parser

This commit is contained in:
Aleksander Morgado
2019-06-16 11:57:08 +02:00
parent 67cc999266
commit 1cc31df425
3 changed files with 236 additions and 0 deletions

View File

@@ -575,6 +575,121 @@ mm_voice_ccwa_regex_get (void)
NULL);
}
static void
call_info_free (MMCallInfo *info)
{
if (!info)
return;
g_free (info->number);
g_slice_free (MMCallInfo, info);
}
gboolean
mm_3gpp_parse_clcc_response (const gchar *str,
GList **out_list,
GError **error)
{
GRegex *r;
GList *list = NULL;
GError *inner_error = NULL;
GMatchInfo *match_info = NULL;
static const MMCallDirection call_direction[] = {
[0] = MM_CALL_DIRECTION_OUTGOING,
[1] = MM_CALL_DIRECTION_INCOMING,
};
static const MMCallState call_state[] = {
[0] = MM_CALL_STATE_ACTIVE,
[1] = MM_CALL_STATE_HELD,
[2] = MM_CALL_STATE_DIALING, /* Dialing (MOC) */
[3] = MM_CALL_STATE_RINGING_OUT, /* Alerting (MOC) */
[4] = MM_CALL_STATE_RINGING_IN, /* Incoming (MTC) */
[5] = MM_CALL_STATE_WAITING, /* Waiting (MTC) */
};
g_assert (out_list);
/*
* 1 2 3 4 5 6 7 8 9 10
* +CLCC: <idx>,<dir>,<stat>,<mode>,<mpty>[,<number>,<type>[,<alpha>[,<priority>[,<CLI validity>]]]]
* +CLCC: <idx>,<dir>,<stat>,<mode>,<mpty>[,<number>,<type>[,<alpha>[,<priority>[,<CLI validity>]]]]
* ...
*/
r = g_regex_new ("\\+CLCC:\\s*(\\d+),\\s*(\\d+),\\s*(\\d+),\\s*(\\d+),\\s*(\\d+)" /* mandatory fields */
"(?:,\\s*([^,]*),\\s*(\\d+)" /* number and type */
"(?:,\\s*([^,]*)" /* alpha */
"(?:,\\s*(\\d*)" /* priority */
"(?:,\\s*(\\d*)" /* CLI validity */
")?)?)?)?$",
G_REGEX_RAW | G_REGEX_MULTILINE | G_REGEX_NEWLINE_CRLF,
G_REGEX_MATCH_NEWLINE_CRLF,
NULL);
g_assert (r != NULL);
g_regex_match_full (r, str, strlen (str), 0, 0, &match_info, &inner_error);
if (inner_error)
goto out;
/* Parse the results */
while (g_match_info_matches (match_info)) {
MMCallInfo *call_info;
guint aux;
call_info = g_slice_new0 (MMCallInfo);
if (!mm_get_uint_from_match_info (match_info, 1, &call_info->index)) {
mm_warn ("couldn't parse call index from +CLCC line");
goto next;
}
if (!mm_get_uint_from_match_info (match_info, 2, &aux) ||
(aux >= G_N_ELEMENTS (call_direction))) {
mm_warn ("couldn't parse call direction from +CLCC line");
goto next;
}
call_info->direction = call_direction[aux];
if (!mm_get_uint_from_match_info (match_info, 3, &aux) ||
(aux >= G_N_ELEMENTS (call_state))) {
mm_warn ("couldn't parse call state from +CLCC line");
goto next;
}
call_info->state = call_state[aux];
if (g_match_info_get_match_count (match_info) >= 7)
call_info->number = mm_get_string_unquoted_from_match_info (match_info, 6);
list = g_list_append (list, call_info);
call_info = NULL;
next:
call_info_free (call_info);
g_match_info_next (match_info, NULL);
}
out:
g_clear_pointer (&match_info, g_match_info_free);
g_regex_unref (r);
if (inner_error) {
mm_3gpp_call_info_list_free (list);
g_propagate_error (error, inner_error);
return FALSE;
}
*out_list = list;
return TRUE;
}
void
mm_3gpp_call_info_list_free (GList *call_info_list)
{
g_list_free_full (call_info_list, (GDestroyNotify) call_info_free);
}
/*************************************************************************/
static MMFlowControl

View File

@@ -99,6 +99,18 @@ GRegex *mm_voice_cring_regex_get (void);
GRegex *mm_voice_clip_regex_get (void);
GRegex *mm_voice_ccwa_regex_get (void);
/* +CLCC response parser */
typedef struct {
guint index;
MMCallDirection direction;
MMCallState state;
gchar *number; /* optional */
} MMCallInfo;
gboolean mm_3gpp_parse_clcc_response (const gchar *str,
GList **out_list,
GError **error);
void mm_3gpp_call_info_list_free (GList *call_info_list);
/*****************************************************************************/
/* SERIAL specific helpers and utilities */

View File

@@ -4018,6 +4018,110 @@ test_ccwa_indication (void)
g_regex_unref (r);
}
/*****************************************************************************/
/* Test +CLCC URCs */
static void
common_test_clcc_response (const gchar *str,
const MMCallInfo *expected_call_info_list,
guint expected_call_info_list_size)
{
GError *error = NULL;
gboolean result;
GList *call_info_list = NULL;
GList *l;
result = mm_3gpp_parse_clcc_response (str, &call_info_list, &error);
g_assert_no_error (error);
g_assert (result);
g_print ("found %u calls\n", g_list_length (call_info_list));
if (expected_call_info_list) {
g_assert (call_info_list);
g_assert_cmpuint (g_list_length (call_info_list), ==, expected_call_info_list_size);
} else
g_assert (!call_info_list);
for (l = call_info_list; l; l = g_list_next (l)) {
const MMCallInfo *call_info = (const MMCallInfo *)(l->data);
gboolean found = FALSE;
guint i;
g_print ("call at index %u: direction %s, state %s, number %s\n",
call_info->index,
mm_call_direction_get_string (call_info->direction),
mm_call_state_get_string (call_info->state),
call_info->number ? call_info->number : "n/a");
for (i = 0; !found && i < expected_call_info_list_size; i++)
found = ((call_info->index == expected_call_info_list[i].index) &&
(call_info->direction == expected_call_info_list[i].direction) &&
(call_info->state == expected_call_info_list[i].state) &&
(g_strcmp0 (call_info->number, expected_call_info_list[i].number) == 0));
g_assert (found);
}
mm_3gpp_call_info_list_free (call_info_list);
}
static void
test_clcc_response_empty (void)
{
const gchar *response = "";
common_test_clcc_response (response, NULL, 0);
}
static void
test_clcc_response_single (void)
{
static const MMCallInfo expected_call_info_list[] = {
{ 1, MM_CALL_DIRECTION_INCOMING, MM_CALL_STATE_ACTIVE, "123456789" }
};
const gchar *response =
"+CLCC: 1,1,0,0,0,\"123456789\",161";
common_test_clcc_response (response, expected_call_info_list, G_N_ELEMENTS (expected_call_info_list));
}
static void
test_clcc_response_single_long (void)
{
static const MMCallInfo expected_call_info_list[] = {
{ 1, MM_CALL_DIRECTION_INCOMING, MM_CALL_STATE_RINGING_IN, "123456789" }
};
/* NOTE: priority field is EMPTY */
const gchar *response =
"+CLCC: 1,1,4,0,0,\"123456789\",129,\"\",,0";
common_test_clcc_response (response, expected_call_info_list, G_N_ELEMENTS (expected_call_info_list));
}
static void
test_clcc_response_multiple (void)
{
static const MMCallInfo expected_call_info_list[] = {
{ 1, MM_CALL_DIRECTION_INCOMING, MM_CALL_STATE_ACTIVE, NULL },
{ 2, MM_CALL_DIRECTION_INCOMING, MM_CALL_STATE_ACTIVE, "123456789" },
{ 3, MM_CALL_DIRECTION_INCOMING, MM_CALL_STATE_ACTIVE, "987654321" },
{ 4, MM_CALL_DIRECTION_INCOMING, MM_CALL_STATE_ACTIVE, "000000000" },
{ 5, MM_CALL_DIRECTION_INCOMING, MM_CALL_STATE_WAITING, "555555555" },
};
const gchar *response =
"+CLCC: 1,1,0,0,1\r\n" /* number unknown */
"+CLCC: 2,1,0,0,1,\"123456789\",161\r\n"
"+CLCC: 3,1,0,0,1,\"987654321\",161,\"Alice\"\r\n"
"+CLCC: 4,1,0,0,1,\"000000000\",161,\"Bob\",1\r\n"
"+CLCC: 5,1,5,0,0,\"555555555\",161,\"Mallory\",2,0\r\n";
common_test_clcc_response (response, expected_call_info_list, G_N_ELEMENTS (expected_call_info_list));
}
/*****************************************************************************/
typedef struct {
@@ -4332,6 +4436,11 @@ int main (int argc, char **argv)
g_test_suite_add (suite, TESTCASE (test_clip_indication, NULL));
g_test_suite_add (suite, TESTCASE (test_ccwa_indication, NULL));
g_test_suite_add (suite, TESTCASE (test_clcc_response_empty, NULL));
g_test_suite_add (suite, TESTCASE (test_clcc_response_single, NULL));
g_test_suite_add (suite, TESTCASE (test_clcc_response_single_long, NULL));
g_test_suite_add (suite, TESTCASE (test_clcc_response_multiple, NULL));
g_test_suite_add (suite, TESTCASE (test_parse_uint_list, NULL));
g_test_suite_add (suite, TESTCASE (test_bcd_to_string, NULL));