1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
*
* Copyright (C) 2019 Aleksander Morgado <aleksander@aleksander.es>
*/
#include <glib.h>
#include <glib-object.h>
#include <locale.h>
#include <ModemManager.h>
#define _LIBMM_INSIDE_MM
#include <libmm-glib.h>
#include "mm-log-test.h"
#include "mm-modem-helpers.h"
#include "mm-modem-helpers-simtech.h"
/*****************************************************************************/
/* Test +CLCC URCs */
static void
common_test_clcc_urc (const gchar *urc,
const MMCallInfo *expected_call_info_list,
guint expected_call_info_list_size)
{
g_autoptr(GRegex) clcc_regex = NULL;
g_autoptr(GMatchInfo) match_info = NULL;
g_autofree gchar *str = NULL;
GError *error = NULL;
GList *call_info_list = NULL;
GList *l;
gboolean result;
clcc_regex = mm_simtech_get_clcc_urc_regex ();
/* Same matching logic as done in MMSerialPortAt when processing URCs! */
result = g_regex_match_full (clcc_regex, urc, -1, 0, 0, &match_info, &error);
g_assert_no_error (error);
g_assert (result);
/* read full matched content */
str = g_match_info_fetch (match_info, 0);
g_assert (str);
result = mm_simtech_parse_clcc_list (str, NULL, &call_info_list, &error);
g_assert_no_error (error);
g_assert (result);
g_debug ("found %u calls", 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_debug ("call at index %u: direction %s, state %s, number %s",
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_simtech_call_info_list_free (call_info_list);
}
static void
test_clcc_urc_single (void)
{
static const MMCallInfo expected_call_info_list[] = {
{ 1, MM_CALL_DIRECTION_INCOMING, MM_CALL_STATE_ACTIVE, (gchar *) "123456789" }
};
const gchar *urc =
"\r\n+CLCC: 1,1,0,0,0,\"123456789\",161"
"\r\n";
common_test_clcc_urc (urc, expected_call_info_list, G_N_ELEMENTS (expected_call_info_list));
}
static void
test_clcc_urc_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, (gchar *) "123456789" },
{ 3, MM_CALL_DIRECTION_INCOMING, MM_CALL_STATE_ACTIVE, (gchar *) "987654321" },
};
const gchar *urc =
"\r\n+CLCC: 1,1,0,0,0" /* number unknown */
"\r\n+CLCC: 2,1,0,0,0,\"123456789\",161"
"\r\n+CLCC: 3,1,0,0,0,\"987654321\",161,\"Alice\""
"\r\n";
common_test_clcc_urc (urc, expected_call_info_list, G_N_ELEMENTS (expected_call_info_list));
}
static void
test_clcc_urc_complex (void)
{
static const MMCallInfo expected_call_info_list[] = {
{ 1, MM_CALL_DIRECTION_INCOMING, MM_CALL_STATE_ACTIVE, (gchar *) "123456789" },
{ 2, MM_CALL_DIRECTION_INCOMING, MM_CALL_STATE_WAITING, (gchar *) "987654321" },
};
const gchar *urc =
"\r\n^CIEV: 1,0" /* some different URC before our match */
"\r\n+CLCC: 1,1,0,0,0,\"123456789\",161"
"\r\n+CLCC: 2,1,5,0,0,\"987654321\",161"
"\r\n^CIEV: 1,0" /* some different URC after our match */
"\r\n";
common_test_clcc_urc (urc, expected_call_info_list, G_N_ELEMENTS (expected_call_info_list));
}
/*****************************************************************************/
static void
common_test_voice_call_urc (const gchar *urc,
gboolean expected_start_or_stop,
guint expected_duration)
{
g_autoptr(GRegex) voice_call_regex = NULL;
g_autoptr(GMatchInfo) match_info = NULL;
GError *error = NULL;
gboolean start_or_stop = FALSE; /* start = TRUE, stop = FALSE */
guint duration = 0;
gboolean result;
voice_call_regex = mm_simtech_get_voice_call_urc_regex ();
/* Same matching logic as done in MMSerialPortAt when processing URCs! */
result = g_regex_match_full (voice_call_regex, urc, -1, 0, 0, &match_info, &error);
g_assert_no_error (error);
g_assert (result);
result = mm_simtech_parse_voice_call_urc (match_info, &start_or_stop, &duration, &error);
g_assert_no_error (error);
g_assert (result);
g_assert_cmpuint (expected_start_or_stop, ==, start_or_stop);
g_assert_cmpuint (expected_duration, ==, duration);
}
static void
test_voice_call_begin_urc (void)
{
common_test_voice_call_urc ("\r\nVOICE CALL: BEGIN\r\n", TRUE, 0);
}
static void
test_voice_call_end_urc (void)
{
common_test_voice_call_urc ("\r\nVOICE CALL: END\r\n", FALSE, 0);
}
static void
test_voice_call_end_duration_urc (void)
{
common_test_voice_call_urc ("\r\nVOICE CALL: END: 000041\r\n", FALSE, 41);
}
/*****************************************************************************/
static void
common_test_missed_call_urc (const gchar *urc,
const gchar *expected_details)
{
g_autoptr(GRegex) missed_call_regex = NULL;
g_autoptr(GMatchInfo) match_info = NULL;
g_autofree gchar *details = NULL;
GError *error = NULL;
gboolean result;
missed_call_regex = mm_simtech_get_missed_call_urc_regex ();
/* Same matching logic as done in MMSerialPortAt when processing URCs! */
result = g_regex_match_full (missed_call_regex, urc, -1, 0, 0, &match_info, &error);
g_assert_no_error (error);
g_assert (result);
result = mm_simtech_parse_missed_call_urc (match_info, &details, &error);
g_assert_no_error (error);
g_assert (result);
g_assert_cmpstr (expected_details, ==, details);
}
static void
test_missed_call_urc (void)
{
common_test_missed_call_urc ("\r\nMISSED_CALL: 11:01AM 07712345678\r\n", "11:01AM 07712345678");
}
/*****************************************************************************/
static void
common_test_cring_urc (const gchar *urc,
const gchar *expected_type)
{
g_autoptr(GRegex) cring_regex = NULL;
g_autoptr(GMatchInfo) match_info = NULL;
g_autofree gchar *type = NULL;
GError *error = NULL;
gboolean result;
cring_regex = mm_simtech_get_cring_urc_regex ();
/* Same matching logic as done in MMSerialPortAt when processing URCs! */
result = g_regex_match_full (cring_regex, urc, -1, 0, 0, &match_info, &error);
g_assert_no_error (error);
g_assert (result);
type = g_match_info_fetch (match_info, 1);
g_assert (type);
g_assert_cmpstr (type, ==, expected_type);
}
static void
test_cring_urc_two_crs (void)
{
common_test_cring_urc ("\r\r\n+CRING: VOICE\r\r\n", "VOICE");
}
static void
test_cring_urc_one_cr (void)
{
common_test_cring_urc ("\r\n+CRING: VOICE\r\n", "VOICE");
}
/*****************************************************************************/
static void
common_test_rxdtmf_urc (const gchar *urc,
const gchar *expected_str)
{
g_autoptr(GRegex) rxdtmf_regex = NULL;
g_autoptr(GMatchInfo) match_info = NULL;
g_autofree gchar *type = NULL;
GError *error = NULL;
gboolean result;
rxdtmf_regex = mm_simtech_get_rxdtmf_urc_regex ();
/* Same matching logic as done in MMSerialPortAt when processing URCs! */
result = g_regex_match_full (rxdtmf_regex, urc, -1, 0, 0, &match_info, &error);
g_assert_no_error (error);
g_assert (result);
type = g_match_info_fetch (match_info, 1);
g_assert (type);
g_assert_cmpstr (type, ==, expected_str);
}
static void
test_rxdtmf_urc_two_crs (void)
{
common_test_rxdtmf_urc ("\r\r\n+RXDTMF: 8\r\r\n", "8");
common_test_rxdtmf_urc ("\r\r\n+RXDTMF: *\r\r\n", "*");
common_test_rxdtmf_urc ("\r\r\n+RXDTMF: #\r\r\n", "#");
common_test_rxdtmf_urc ("\r\r\n+RXDTMF: A\r\r\n", "A");
}
static void
test_rxdtmf_urc_one_cr (void)
{
common_test_rxdtmf_urc ("\r\n+RXDTMF: 8\r\n", "8");
common_test_rxdtmf_urc ("\r\n+RXDTMF: *\r\n", "*");
common_test_rxdtmf_urc ("\r\n+RXDTMF: #\r\n", "#");
common_test_rxdtmf_urc ("\r\n+RXDTMF: A\r\n", "A");
}
/*****************************************************************************/
int main (int argc, char **argv)
{
setlocale (LC_ALL, "");
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/MM/simtech/clcc/urc/single", test_clcc_urc_single);
g_test_add_func ("/MM/simtech/clcc/urc/multiple", test_clcc_urc_multiple);
g_test_add_func ("/MM/simtech/clcc/urc/complex", test_clcc_urc_complex);
g_test_add_func ("/MM/simtech/voicecall/urc/begin", test_voice_call_begin_urc);
g_test_add_func ("/MM/simtech/voicecall/urc/end", test_voice_call_end_urc);
g_test_add_func ("/MM/simtech/voicecall/urc/end-duration", test_voice_call_end_duration_urc);
g_test_add_func ("/MM/simtech/missedcall/urc", test_missed_call_urc);
g_test_add_func ("/MM/simtech/cring/urc/two-crs", test_cring_urc_two_crs);
g_test_add_func ("/MM/simtech/cring/urc/one-cr", test_cring_urc_one_cr);
g_test_add_func ("/MM/simtech/rxdtmf/urc/two-crs", test_rxdtmf_urc_two_crs);
g_test_add_func ("/MM/simtech/rxdtmf/urc/one-cr", test_rxdtmf_urc_one_cr);
return g_test_run ();
}
|