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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
|
/* -*- 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) 2014 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.h"
#include "mm-modem-helpers.h"
#include "mm-modem-helpers-cinterion.h"
static gint
sort_band (MMModemBand a, MMModemBand b)
{
return a - b;
}
/*****************************************************************************/
/* Test ^SCFG test responses */
static void
common_test_scfg (const gchar *response,
GArray *expected_bands)
{
GArray *bands = NULL;
gchar *expected_bands_str;
gchar *bands_str;
GError *error = NULL;
gboolean res;
res = mm_cinterion_parse_scfg_test (response,
MM_MODEM_CHARSET_UNKNOWN,
&bands,
&error);
g_assert_no_error (error);
g_assert (res == TRUE);
g_assert (bands != NULL);
g_array_sort (bands, (GCompareFunc)sort_band);
g_array_sort (expected_bands, (GCompareFunc)sort_band);
expected_bands_str = mm_common_build_bands_string ((const MMModemBand *)expected_bands->data,
expected_bands->len);
bands_str = mm_common_build_bands_string ((const MMModemBand *)bands->data,
bands->len);
/* Instead of comparing the array one by one, compare the strings built from the mask
* (we get a nicer error if it fails) */
g_assert_cmpstr (bands_str, ==, expected_bands_str);
g_free (bands_str);
g_free (expected_bands_str);
g_array_unref (bands);
}
static void
test_scfg (void)
{
GArray *expected_bands;
MMModemBand single;
const gchar *response =
"^SCFG: \"Audio/Loop\",(\"0\",\"1\")\r\n"
"^SCFG: \"Call/ECC\",(\"0\"-\"255\")\r\n"
"^SCFG: \"Call/Speech/Codec\",(\"0\",\"1\")\r\n"
"^SCFG: \"GPRS/Auth\",(\"0\",\"1\",\"2\")\r\n"
"^SCFG: \"GPRS/AutoAttach\",(\"disabled\",\"enabled\")\r\n"
"^SCFG: \"GPRS/MaxDataRate/HSDPA\",(\"0\",\"1\")\r\n"
"^SCFG: \"GPRS/MaxDataRate/HSUPA\",(\"0\",\"1\")\r\n"
"^SCFG: \"Ident/Manufacturer\",(25)\r\n"
"^SCFG: \"Ident/Product\",(25)\r\n"
"^SCFG: \"MEopMode/Airplane\",(\"off\",\"on\")\r\n"
"^SCFG: \"MEopMode/CregRoam\",(\"0\",\"1\")\r\n"
"^SCFG: \"MEopMode/CFUN\",(\"0\",\"1\")\r\n"
"^SCFG: \"MEopMode/PowerMgmt/LCI\",(\"disabled\",\"enabled\")\r\n"
"^SCFG: \"MEopMode/PowerMgmt/VExt\",(\"high\",\"low\")\r\n"
"^SCFG: \"MEopMode/PwrSave\",(\"disabled\",\"enabled\"),(\"0-600\"),(\"1-36000\")\r\n"
"^SCFG: \"MEopMode/RingOnData\",(\"on\",\"off\")\r\n"
"^SCFG: \"MEopMode/RingUrcOnCall\",(\"on\",\"off\")\r\n"
"^SCFG: \"MEShutdown/OnIgnition\",(\"on\",\"off\")\r\n"
"^SCFG: \"Radio/Band\",(\"1-511\",\"0-1\")\r\n"
"^SCFG: \"Radio/NWSM\",(\"0\",\"1\",\"2\")\r\n"
"^SCFG: \"Radio/OutputPowerReduction\",(\"4\"-\"8\")\r\n"
"^SCFG: \"Serial/USB/DDD\",(\"0\",\"1\"),(\"0\"),(4),(4),(4),(63),(63),(4)\r\n"
"^SCFG: \"URC/DstIfc\",(\"mdm\",\"app\")\r\n"
"^SCFG: \"URC/Datamode/Ringline\",(\"off\",\"on\")\r\n"
"^SCFG: \"URC/Ringline\",(\"off\",\"local\",\"asc0\",\"wakeup\")\r\n"
"^SCFG: \"URC/Ringline/ActiveTime\",(\"0\",\"1\",\"2\",\"keep\")\r\n";
expected_bands = g_array_sized_new (FALSE, FALSE, sizeof (MMModemBand), 9);
single = MM_MODEM_BAND_EGSM, g_array_append_val (expected_bands, single);
single = MM_MODEM_BAND_DCS, g_array_append_val (expected_bands, single);
single = MM_MODEM_BAND_PCS, g_array_append_val (expected_bands, single);
single = MM_MODEM_BAND_G850, g_array_append_val (expected_bands, single);
single = MM_MODEM_BAND_U2100, g_array_append_val (expected_bands, single);
single = MM_MODEM_BAND_U1900, g_array_append_val (expected_bands, single);
single = MM_MODEM_BAND_U850, g_array_append_val (expected_bands, single);
single = MM_MODEM_BAND_U900, g_array_append_val (expected_bands, single);
single = MM_MODEM_BAND_U800, g_array_append_val (expected_bands, single);
common_test_scfg (response, expected_bands);
g_array_unref (expected_bands);
}
/*****************************************************************************/
/* Test ^SCFG responses */
static void
common_test_scfg_response (const gchar *response,
MMModemCharset charset,
GArray *expected_bands)
{
GArray *bands = NULL;
gchar *expected_bands_str;
gchar *bands_str;
GError *error = NULL;
gboolean res;
res = mm_cinterion_parse_scfg_response (response, charset, &bands, &error);
g_assert_no_error (error);
g_assert (res == TRUE);
g_assert (bands != NULL);
g_array_sort (bands, (GCompareFunc)sort_band);
g_array_sort (expected_bands, (GCompareFunc)sort_band);
expected_bands_str = mm_common_build_bands_string ((const MMModemBand *)expected_bands->data,
expected_bands->len);
bands_str = mm_common_build_bands_string ((const MMModemBand *)bands->data,
bands->len);
/* Instead of comparing the array one by one, compare the strings built from the mask
* (we get a nicer error if it fails) */
g_assert_cmpstr (bands_str, ==, expected_bands_str);
g_free (bands_str);
g_free (expected_bands_str);
g_array_unref (bands);
}
static void
test_scfg_response_2g (void)
{
GArray *expected_bands;
MMModemBand single;
const gchar *response =
"^SCFG: \"Radio/Band\",\"3\",\"3\"\r\n"
"\r\n";
expected_bands = g_array_sized_new (FALSE, FALSE, sizeof (MMModemBand), 9);
single = MM_MODEM_BAND_EGSM, g_array_append_val (expected_bands, single);
single = MM_MODEM_BAND_DCS, g_array_append_val (expected_bands, single);
common_test_scfg_response (response, MM_MODEM_CHARSET_UNKNOWN, expected_bands);
g_array_unref (expected_bands);
}
static void
test_scfg_response_2g_ucs2 (void)
{
GArray *expected_bands;
MMModemBand single;
const gchar *response =
"^SCFG: \"Radio/Band\",\"0031\",\"0031\"\r\n"
"\r\n";
expected_bands = g_array_sized_new (FALSE, FALSE, sizeof (MMModemBand), 9);
single = MM_MODEM_BAND_EGSM, g_array_append_val (expected_bands, single);
common_test_scfg_response (response, MM_MODEM_CHARSET_UCS2, expected_bands);
g_array_unref (expected_bands);
}
static void
test_scfg_response_3g (void)
{
GArray *expected_bands;
MMModemBand single;
const gchar *response =
"^SCFG: \"Radio/Band\",127\r\n"
"\r\n";
expected_bands = g_array_sized_new (FALSE, FALSE, sizeof (MMModemBand), 9);
single = MM_MODEM_BAND_EGSM, g_array_append_val (expected_bands, single);
single = MM_MODEM_BAND_DCS, g_array_append_val (expected_bands, single);
single = MM_MODEM_BAND_PCS, g_array_append_val (expected_bands, single);
single = MM_MODEM_BAND_G850, g_array_append_val (expected_bands, single);
single = MM_MODEM_BAND_U2100, g_array_append_val (expected_bands, single);
single = MM_MODEM_BAND_U1900, g_array_append_val (expected_bands, single);
single = MM_MODEM_BAND_U850, g_array_append_val (expected_bands, single);
common_test_scfg_response (response, MM_MODEM_CHARSET_UNKNOWN, expected_bands);
g_array_unref (expected_bands);
}
/*****************************************************************************/
/* Test ^SCFG test */
static void
compare_arrays (const GArray *supported,
const GArray *expected)
{
guint i;
g_assert_cmpuint (supported->len, ==, expected->len);
for (i = 0; i < supported->len; i++) {
gboolean found = FALSE;
guint j;
for (j = 0; j < expected->len && !found; j++) {
if (g_array_index (supported, guint, i) == g_array_index (expected, guint, j))
found = TRUE;
}
g_assert (found);
}
}
static void
common_test_cnmi (const gchar *response,
const GArray *expected_mode,
const GArray *expected_mt,
const GArray *expected_bm,
const GArray *expected_ds,
const GArray *expected_bfr)
{
GArray *supported_mode = NULL;
GArray *supported_mt = NULL;
GArray *supported_bm = NULL;
GArray *supported_ds = NULL;
GArray *supported_bfr = NULL;
GError *error = NULL;
gboolean res;
g_assert (expected_mode != NULL);
g_assert (expected_mt != NULL);
g_assert (expected_bm != NULL);
g_assert (expected_ds != NULL);
g_assert (expected_bfr != NULL);
res = mm_cinterion_parse_cnmi_test (response,
&supported_mode,
&supported_mt,
&supported_bm,
&supported_ds,
&supported_bfr,
&error);
g_assert_no_error (error);
g_assert (res == TRUE);
g_assert (supported_mode != NULL);
g_assert (supported_mt != NULL);
g_assert (supported_bm != NULL);
g_assert (supported_ds != NULL);
g_assert (supported_bfr != NULL);
compare_arrays (supported_mode, expected_mode);
compare_arrays (supported_mt, expected_mt);
compare_arrays (supported_bm, expected_bm);
compare_arrays (supported_ds, expected_ds);
compare_arrays (supported_bfr, expected_bfr);
g_array_unref (supported_mode);
g_array_unref (supported_mt);
g_array_unref (supported_bm);
g_array_unref (supported_ds);
g_array_unref (supported_bfr);
}
static void
test_cnmi_phs8 (void)
{
GArray *expected_mode;
GArray *expected_mt;
GArray *expected_bm;
GArray *expected_ds;
GArray *expected_bfr;
guint val;
const gchar *response =
"+CNMI: (0,1,2),(0,1),(0,2),(0),(1)\r\n"
"\r\n";
expected_mode = g_array_sized_new (FALSE, FALSE, sizeof (guint), 3);
val = 0, g_array_append_val (expected_mode, val);
val = 1, g_array_append_val (expected_mode, val);
val = 2, g_array_append_val (expected_mode, val);
expected_mt = g_array_sized_new (FALSE, FALSE, sizeof (guint), 2);
val = 0, g_array_append_val (expected_mt, val);
val = 1, g_array_append_val (expected_mt, val);
expected_bm = g_array_sized_new (FALSE, FALSE, sizeof (guint), 2);
val = 0, g_array_append_val (expected_bm, val);
val = 2, g_array_append_val (expected_bm, val);
expected_ds = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
val = 0, g_array_append_val (expected_ds, val);
expected_bfr = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
val = 1, g_array_append_val (expected_bfr, val);
common_test_cnmi (response,
expected_mode,
expected_mt,
expected_bm,
expected_ds,
expected_bfr);
g_array_unref (expected_mode);
g_array_unref (expected_mt);
g_array_unref (expected_bm);
g_array_unref (expected_ds);
g_array_unref (expected_bfr);
}
/*****************************************************************************/
/* Test ^SWWAN read */
#define SWWAN_TEST_MAX_CIDS 2
typedef struct {
guint cid;
MMBearerConnectionStatus state;
} PdpContextState;
typedef struct {
const gchar *response;
PdpContextState expected_items[SWWAN_TEST_MAX_CIDS];
gboolean skip_test_other_cids;
} SwwanTest;
/* Note: all tests are based on checking CIDs 2 and 3 */
static const SwwanTest swwan_tests[] = {
/* No active PDP context reported (all disconnected) */
{
.response = "",
.expected_items = {
{ .cid = 2, .state = MM_BEARER_CONNECTION_STATUS_DISCONNECTED },
{ .cid = 3, .state = MM_BEARER_CONNECTION_STATUS_DISCONNECTED }
},
/* Don't test other CIDs because for those we would also return
* DISCONNECTED, not UNKNOWN. */
.skip_test_other_cids = TRUE
},
/* Single PDP context active (short version without interface index) */
{
.response = "^SWWAN: 3,1\r\n",
.expected_items = {
{ .cid = 2, .state = MM_BEARER_CONNECTION_STATUS_UNKNOWN },
{ .cid = 3, .state = MM_BEARER_CONNECTION_STATUS_CONNECTED }
}
},
/* Single PDP context active (long version with interface index) */
{
.response = "^SWWAN: 3,1,1\r\n",
.expected_items = {
{ .cid = 2, .state = MM_BEARER_CONNECTION_STATUS_UNKNOWN },
{ .cid = 3, .state = MM_BEARER_CONNECTION_STATUS_CONNECTED }
}
},
/* Single PDP context inactive (short version without interface index) */
{
.response = "^SWWAN: 3,0\r\n",
.expected_items = {
{ .cid = 2, .state = MM_BEARER_CONNECTION_STATUS_UNKNOWN },
{ .cid = 3, .state = MM_BEARER_CONNECTION_STATUS_DISCONNECTED }
}
},
/* Single PDP context inactive (long version with interface index) */
{
.response = "^SWWAN: 3,0,1\r\n",
.expected_items = {
{ .cid = 2, .state = MM_BEARER_CONNECTION_STATUS_UNKNOWN },
{ .cid = 3, .state = MM_BEARER_CONNECTION_STATUS_DISCONNECTED }
}
},
/* Multiple PDP contexts active (short version without interface index) */
{
.response = "^SWWAN: 2,1\r\n^SWWAN: 3,1\r\n",
.expected_items = {
{ .cid = 2, .state = MM_BEARER_CONNECTION_STATUS_CONNECTED },
{ .cid = 3, .state = MM_BEARER_CONNECTION_STATUS_CONNECTED }
}
},
/* Multiple PDP contexts active (long version with interface index) */
{
.response = "^SWWAN: 2,1,3\r\n^SWWAN: 3,1,1\r\n",
.expected_items = {
{ .cid = 2, .state = MM_BEARER_CONNECTION_STATUS_CONNECTED },
{ .cid = 3, .state = MM_BEARER_CONNECTION_STATUS_CONNECTED }
}
},
/* Multiple PDP contexts inactive (short version without interface index) */
{
.response = "^SWWAN: 2,0\r\n^SWWAN: 3,0\r\n",
.expected_items = {
{ .cid = 2, .state = MM_BEARER_CONNECTION_STATUS_DISCONNECTED },
{ .cid = 3, .state = MM_BEARER_CONNECTION_STATUS_DISCONNECTED }
}
},
/* Multiple PDP contexts inactive (long version with interface index) */
{
.response = "^SWWAN: 2,0,3\r\n^SWWAN: 3,0,1\r\n",
.expected_items = {
{ .cid = 2, .state = MM_BEARER_CONNECTION_STATUS_DISCONNECTED },
{ .cid = 3, .state = MM_BEARER_CONNECTION_STATUS_DISCONNECTED }
}
},
/* Multiple PDP contexts active/inactive (short version without interface index) */
{
.response = "^SWWAN: 2,0\r\n^SWWAN: 3,1\r\n",
.expected_items = {
{ .cid = 2, .state = MM_BEARER_CONNECTION_STATUS_DISCONNECTED },
{ .cid = 3, .state = MM_BEARER_CONNECTION_STATUS_CONNECTED }
}
},
/* Multiple PDP contexts active/inactive (long version with interface index) */
{
.response = "^SWWAN: 2,0,3\r\n^SWWAN: 3,1,1\r\n",
.expected_items = {
{ .cid = 2, .state = MM_BEARER_CONNECTION_STATUS_DISCONNECTED },
{ .cid = 3, .state = MM_BEARER_CONNECTION_STATUS_CONNECTED }
}
}
};
static void
test_swwan_pls8 (void)
{
MMBearerConnectionStatus read_state;
GError *error = NULL;
guint i;
/* Base tests for successful responses */
for (i = 0; i < G_N_ELEMENTS (swwan_tests); i++) {
guint j;
/* Query for the expected items (CIDs 2 and 3) */
for (j = 0; j < SWWAN_TEST_MAX_CIDS; j++) {
read_state = mm_cinterion_parse_swwan_response (swwan_tests[i].response, swwan_tests[i].expected_items[j].cid, &error);
if (swwan_tests[i].expected_items[j].state == MM_BEARER_CONNECTION_STATUS_UNKNOWN) {
g_assert_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED);
g_clear_error (&error);
} else
g_assert_no_error (error);
g_assert_cmpint (read_state, ==, swwan_tests[i].expected_items[j].state);
}
/* Query for a CID which isn't replied (e.g. 12) */
if (!swwan_tests[i].skip_test_other_cids) {
read_state = mm_cinterion_parse_swwan_response (swwan_tests[i].response, 12, &error);
g_assert_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED);
g_assert_cmpint (read_state, ==, MM_BEARER_CONNECTION_STATUS_UNKNOWN);
g_clear_error (&error);
}
}
/* Additional tests for errors */
read_state = mm_cinterion_parse_swwan_response ("^GARBAGE", 2, &error);
g_assert_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED);
g_assert_cmpint (read_state, ==, MM_BEARER_CONNECTION_STATUS_UNKNOWN);
g_clear_error (&error);
}
/*****************************************************************************/
/* Test ^SIND responses */
static void
common_test_sind_response (const gchar *response,
const gchar *expected_description,
guint expected_mode,
guint expected_value)
{
GError *error = NULL;
gboolean res;
gchar *description;
guint mode;
guint value;
res = mm_cinterion_parse_sind_response (response,
&description,
&mode,
&value,
&error);
g_assert_no_error (error);
g_assert (res == TRUE);
g_assert_cmpstr (description, ==, expected_description);
g_assert_cmpuint (mode, ==, expected_mode);
g_assert_cmpuint (value, ==, expected_value);
g_free (description);
}
static void
test_sind_response_simstatus (void)
{
common_test_sind_response ("^SIND: simstatus,1,5", "simstatus", 1, 5);
}
/*****************************************************************************/
void
_mm_log (const char *loc,
const char *func,
guint32 level,
const char *fmt,
...)
{
#if defined ENABLE_TEST_MESSAGE_TRACES
/* Dummy log function */
va_list args;
gchar *msg;
va_start (args, fmt);
msg = g_strdup_vprintf (fmt, args);
va_end (args);
g_print ("%s\n", msg);
g_free (msg);
#endif
}
int main (int argc, char **argv)
{
setlocale (LC_ALL, "");
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/MM/cinterion/scfg", test_scfg);
g_test_add_func ("/MM/cinterion/scfg/response/3g", test_scfg_response_3g);
g_test_add_func ("/MM/cinterion/scfg/response/2g", test_scfg_response_2g);
g_test_add_func ("/MM/cinterion/scfg/response/2g/ucs2", test_scfg_response_2g_ucs2);
g_test_add_func ("/MM/cinterion/cnmi/phs8", test_cnmi_phs8);
g_test_add_func ("/MM/cinterion/swwan/pls8", test_swwan_pls8);
g_test_add_func ("/MM/cinterion/sind/response/simstatus", test_sind_response_simstatus);
return g_test_run ();
}
|