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
|
/* -*- 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) 2018 Aleksander Morgado <aleksander@aleksander.es>
*/
#include "mm-log.h"
#include "mm-modem-helpers.h"
#include "mm-modem-helpers-xmm.h"
/*****************************************************************************/
/* XACT common config */
typedef struct {
guint num;
MMModemBand band;
} XactBandConfig;
static const XactBandConfig xact_band_config[] = {
/* GSM bands */
{ .num = 900, .band = MM_MODEM_BAND_EGSM },
{ .num = 1800, .band = MM_MODEM_BAND_DCS },
{ .num = 1900, .band = MM_MODEM_BAND_PCS },
{ .num = 850, .band = MM_MODEM_BAND_G850 },
{ .num = 450, .band = MM_MODEM_BAND_G450 },
{ .num = 480, .band = MM_MODEM_BAND_G480 },
{ .num = 750, .band = MM_MODEM_BAND_G750 },
{ .num = 380, .band = MM_MODEM_BAND_G380 },
{ .num = 410, .band = MM_MODEM_BAND_G410 },
{ .num = 710, .band = MM_MODEM_BAND_G710 },
{ .num = 810, .band = MM_MODEM_BAND_G810 },
/* UMTS bands */
{ .num = 1, .band = MM_MODEM_BAND_UTRAN_1 },
{ .num = 2, .band = MM_MODEM_BAND_UTRAN_2 },
{ .num = 3, .band = MM_MODEM_BAND_UTRAN_3 },
{ .num = 4, .band = MM_MODEM_BAND_UTRAN_4 },
{ .num = 5, .band = MM_MODEM_BAND_UTRAN_5 },
{ .num = 6, .band = MM_MODEM_BAND_UTRAN_6 },
{ .num = 7, .band = MM_MODEM_BAND_UTRAN_7 },
{ .num = 8, .band = MM_MODEM_BAND_UTRAN_8 },
{ .num = 9, .band = MM_MODEM_BAND_UTRAN_9 },
{ .num = 10, .band = MM_MODEM_BAND_UTRAN_10 },
{ .num = 11, .band = MM_MODEM_BAND_UTRAN_11 },
{ .num = 12, .band = MM_MODEM_BAND_UTRAN_12 },
{ .num = 13, .band = MM_MODEM_BAND_UTRAN_13 },
{ .num = 14, .band = MM_MODEM_BAND_UTRAN_14 },
{ .num = 19, .band = MM_MODEM_BAND_UTRAN_19 },
{ .num = 20, .band = MM_MODEM_BAND_UTRAN_20 },
{ .num = 21, .band = MM_MODEM_BAND_UTRAN_21 },
{ .num = 22, .band = MM_MODEM_BAND_UTRAN_22 },
{ .num = 25, .band = MM_MODEM_BAND_UTRAN_25 },
/* LTE bands */
{ .num = 101, .band = MM_MODEM_BAND_EUTRAN_1 },
{ .num = 102, .band = MM_MODEM_BAND_EUTRAN_2 },
{ .num = 103, .band = MM_MODEM_BAND_EUTRAN_3 },
{ .num = 104, .band = MM_MODEM_BAND_EUTRAN_4 },
{ .num = 105, .band = MM_MODEM_BAND_EUTRAN_5 },
{ .num = 106, .band = MM_MODEM_BAND_EUTRAN_6 },
{ .num = 107, .band = MM_MODEM_BAND_EUTRAN_7 },
{ .num = 108, .band = MM_MODEM_BAND_EUTRAN_8 },
{ .num = 109, .band = MM_MODEM_BAND_EUTRAN_9 },
{ .num = 110, .band = MM_MODEM_BAND_EUTRAN_10 },
{ .num = 111, .band = MM_MODEM_BAND_EUTRAN_11 },
{ .num = 112, .band = MM_MODEM_BAND_EUTRAN_12 },
{ .num = 113, .band = MM_MODEM_BAND_EUTRAN_13 },
{ .num = 114, .band = MM_MODEM_BAND_EUTRAN_14 },
{ .num = 117, .band = MM_MODEM_BAND_EUTRAN_17 },
{ .num = 118, .band = MM_MODEM_BAND_EUTRAN_18 },
{ .num = 119, .band = MM_MODEM_BAND_EUTRAN_19 },
{ .num = 120, .band = MM_MODEM_BAND_EUTRAN_20 },
{ .num = 121, .band = MM_MODEM_BAND_EUTRAN_21 },
{ .num = 122, .band = MM_MODEM_BAND_EUTRAN_22 },
{ .num = 123, .band = MM_MODEM_BAND_EUTRAN_23 },
{ .num = 124, .band = MM_MODEM_BAND_EUTRAN_24 },
{ .num = 125, .band = MM_MODEM_BAND_EUTRAN_25 },
{ .num = 126, .band = MM_MODEM_BAND_EUTRAN_26 },
{ .num = 127, .band = MM_MODEM_BAND_EUTRAN_27 },
{ .num = 128, .band = MM_MODEM_BAND_EUTRAN_28 },
{ .num = 129, .band = MM_MODEM_BAND_EUTRAN_29 },
{ .num = 130, .band = MM_MODEM_BAND_EUTRAN_30 },
{ .num = 131, .band = MM_MODEM_BAND_EUTRAN_31 },
{ .num = 132, .band = MM_MODEM_BAND_EUTRAN_32 },
{ .num = 133, .band = MM_MODEM_BAND_EUTRAN_33 },
{ .num = 134, .band = MM_MODEM_BAND_EUTRAN_34 },
{ .num = 135, .band = MM_MODEM_BAND_EUTRAN_35 },
{ .num = 136, .band = MM_MODEM_BAND_EUTRAN_36 },
{ .num = 137, .band = MM_MODEM_BAND_EUTRAN_37 },
{ .num = 138, .band = MM_MODEM_BAND_EUTRAN_38 },
{ .num = 139, .band = MM_MODEM_BAND_EUTRAN_39 },
{ .num = 140, .band = MM_MODEM_BAND_EUTRAN_40 },
{ .num = 141, .band = MM_MODEM_BAND_EUTRAN_41 },
{ .num = 142, .band = MM_MODEM_BAND_EUTRAN_42 },
{ .num = 143, .band = MM_MODEM_BAND_EUTRAN_43 },
{ .num = 144, .band = MM_MODEM_BAND_EUTRAN_44 },
{ .num = 145, .band = MM_MODEM_BAND_EUTRAN_45 },
{ .num = 146, .band = MM_MODEM_BAND_EUTRAN_46 },
{ .num = 147, .band = MM_MODEM_BAND_EUTRAN_47 },
{ .num = 148, .band = MM_MODEM_BAND_EUTRAN_48 },
{ .num = 149, .band = MM_MODEM_BAND_EUTRAN_49 },
{ .num = 150, .band = MM_MODEM_BAND_EUTRAN_50 },
{ .num = 151, .band = MM_MODEM_BAND_EUTRAN_51 },
{ .num = 152, .band = MM_MODEM_BAND_EUTRAN_52 },
{ .num = 153, .band = MM_MODEM_BAND_EUTRAN_53 },
{ .num = 154, .band = MM_MODEM_BAND_EUTRAN_54 },
{ .num = 155, .band = MM_MODEM_BAND_EUTRAN_55 },
{ .num = 156, .band = MM_MODEM_BAND_EUTRAN_56 },
{ .num = 157, .band = MM_MODEM_BAND_EUTRAN_57 },
{ .num = 158, .band = MM_MODEM_BAND_EUTRAN_58 },
{ .num = 159, .band = MM_MODEM_BAND_EUTRAN_59 },
{ .num = 160, .band = MM_MODEM_BAND_EUTRAN_60 },
{ .num = 161, .band = MM_MODEM_BAND_EUTRAN_61 },
{ .num = 162, .band = MM_MODEM_BAND_EUTRAN_62 },
{ .num = 163, .band = MM_MODEM_BAND_EUTRAN_63 },
{ .num = 164, .band = MM_MODEM_BAND_EUTRAN_64 },
{ .num = 165, .band = MM_MODEM_BAND_EUTRAN_65 },
{ .num = 166, .band = MM_MODEM_BAND_EUTRAN_66 },
};
#define XACT_NUM_IS_BAND_2G(num) (num > 300)
#define XACT_NUM_IS_BAND_3G(num) (num < 100)
#define XACT_NUM_IS_BAND_4G(num) (num > 100 && num < 300)
static MMModemBand
xact_num_to_band (guint num)
{
guint i;
for (i = 0; i < G_N_ELEMENTS (xact_band_config); i++) {
if (num == xact_band_config[i].num)
return xact_band_config[i].band;
}
return MM_MODEM_BAND_UNKNOWN;
}
/*****************************************************************************/
/* XACT=? response parser */
/* Index of the array is the XMM-specific value */
static const MMModemMode xmm_modes[] = {
( MM_MODEM_MODE_2G ),
( MM_MODEM_MODE_3G ),
( MM_MODEM_MODE_4G ),
( MM_MODEM_MODE_2G | MM_MODEM_MODE_3G ),
( MM_MODEM_MODE_3G | MM_MODEM_MODE_4G ),
( MM_MODEM_MODE_2G | MM_MODEM_MODE_4G ),
( MM_MODEM_MODE_2G | MM_MODEM_MODE_3G | MM_MODEM_MODE_4G ),
};
gboolean
mm_xmm_parse_xact_test_response (const gchar *response,
GArray **modes_out,
GArray **bands_out,
GError **error)
{
GError *inner_error = NULL;
GArray *modes = NULL;
GArray *all_modes = NULL;
GArray *filtered = NULL;
GArray *supported = NULL;
GArray *preferred = NULL;
GArray *bands = NULL;
gchar **split = NULL;
guint i;
MMModemModeCombination all = {
.allowed = MM_MODEM_MODE_NONE,
.preferred = MM_MODEM_MODE_NONE
};
g_assert (modes_out && bands_out);
/*
* AT+XACT=?
* +XACT: (0-6),(0-2),0,1,2,4,5,8,101,102,103,104,105,107,108,111,...
*/
response = mm_strip_tag (response, "+XACT:");
split = mm_split_string_groups (response);
if (g_strv_length (split) < 3) {
inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED, "Missing fields");
goto out;
}
/* First group is list of supported modes */
supported = mm_parse_uint_list (split[0], &inner_error);
if (inner_error)
goto out;
if (!supported) {
inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED, "Missing modes");
goto out;
}
/* Second group is list of possible preferred modes.
* For our purposes, the preferred list may be empty */
preferred = mm_parse_uint_list (split[1], &inner_error);
if (inner_error)
goto out;
/* Build array of modes */
modes = g_array_new (FALSE, FALSE, sizeof (MMModemModeCombination));
for (i = 0; i < supported->len; i++) {
guint supported_value;
MMModemModeCombination combination;
guint j;
supported_value = g_array_index (supported, guint, i);
if (supported_value >= G_N_ELEMENTS (xmm_modes)) {
mm_warn ("Unexpected AcT supported value: %u", supported_value);
continue;
}
/* Combination without any preferred */
combination.allowed = xmm_modes[supported_value];
combination.preferred = MM_MODEM_MODE_NONE;
g_array_append_val (modes, combination);
if (mm_count_bits_set (combination.allowed) == 1)
continue;
if (!preferred)
continue;
for (j = 0; j < preferred->len; j++) {
guint preferred_value;
preferred_value = g_array_index (preferred, guint, j);
if (preferred_value >= G_N_ELEMENTS (xmm_modes)) {
mm_warn ("Unexpected AcT preferred value: %u", preferred_value);
continue;
}
combination.preferred = xmm_modes[preferred_value];
if (mm_count_bits_set (combination.preferred) != 1) {
mm_warn ("AcT preferred value should be a single AcT: %u", preferred_value);
continue;
}
if (!(combination.allowed & combination.preferred))
continue;
g_array_append_val (modes, combination);
}
}
if (modes->len == 0) {
inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"No modes list built from +XACT=? response");
goto out;
}
/* Build array of bands */
bands = g_array_new (FALSE, FALSE, sizeof (MMModemBand));
/*
* The next element at index 2 may be '0'. We will just treat that field as
* any other band field as '0' isn't a supported band, we'll just ignore it.
*/
for (i = 2; split[i]; i++) {
MMModemBand band;
guint num;
if (!mm_get_uint_from_str (split[i], &num)) {
mm_warn ("Unexpected band value: %s", split[i]);
continue;
}
if (num == 0)
continue;
band = xact_num_to_band (num);
if (band == MM_MODEM_BAND_UNKNOWN) {
mm_warn ("Unsupported band value: %s", split[i]);
continue;
}
g_array_append_val (bands, band);
if (XACT_NUM_IS_BAND_2G (num))
all.allowed |= MM_MODEM_MODE_2G;
if (XACT_NUM_IS_BAND_3G (num))
all.allowed |= MM_MODEM_MODE_3G;
if (XACT_NUM_IS_BAND_4G (num))
all.allowed |= MM_MODEM_MODE_4G;
}
if (bands->len == 0) {
inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"No bands list built from +XACT=? response");
goto out;
}
/* AT+XACT lies about the supported modes, e.g. it may report 2G supported
* for 3G+4G only devices. So, filter out unsupported modes based on the
* supported bands */
all_modes = g_array_sized_new (FALSE, FALSE, sizeof (MMModemModeCombination), 1);
g_array_append_val (all_modes, all);
filtered = mm_filter_supported_modes (all_modes, modes);
if (!filtered || filtered->len == 0) {
inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"Empty supported mode list after frequency band filtering");
goto out;
}
/* success */
out:
if (modes)
g_array_unref (modes);
if (all_modes)
g_array_unref (all_modes);
if (supported)
g_array_unref (supported);
if (preferred)
g_array_unref (preferred);
g_strfreev (split);
if (inner_error) {
if (filtered)
g_array_unref (filtered);
if (bands)
g_array_unref (bands);
g_propagate_error (error, inner_error);
return FALSE;
}
g_assert (filtered);
*modes_out = filtered;
g_assert (bands);
*bands_out = bands;
return TRUE;
}
/*****************************************************************************/
/* AT+XACT? response parser */
gboolean
mm_xmm_parse_xact_query_response (const gchar *response,
MMModemModeCombination *mode_out,
GArray **bands_out,
GError **error)
{
GRegex *r;
GMatchInfo *match_info;
GError *inner_error = NULL;
GArray *bands = NULL;
guint i;
MMModemModeCombination mode = {
.allowed = MM_MODEM_MODE_NONE,
.preferred = MM_MODEM_MODE_NONE,
};
/* At least one */
g_assert (mode_out || bands_out);
/*
* AT+XACT?
* +XACT: 4,1,2,1,2,4,5,8,101,102,103,104,105,107,108,111,...
*
* Note: the first 3 fields corresponde to allowed and preferred modes. Only the
* first one of those 3 first fields is mandatory, the other two may be empty.
*/
r = g_regex_new ("\\+XACT: (\\d+),([^,]*),([^,]*),(.*)(?:\\r\\n)?",
G_REGEX_DOLLAR_ENDONLY | G_REGEX_RAW, 0, NULL);
g_assert (r != NULL);
g_regex_match_full (r, response, strlen (response), 0, 0, &match_info, &inner_error);
if (!inner_error && g_match_info_matches (match_info)) {
if (mode_out) {
guint xmm_mode;
/* Number at index 1 */
mm_get_uint_from_match_info (match_info, 1, &xmm_mode);
if (xmm_mode >= G_N_ELEMENTS (xmm_modes)) {
inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED, "Unsupported XACT AcT value: %u", xmm_mode);
goto out;
}
mode.allowed = xmm_modes[xmm_mode];
/* Number at index 2 */
if (mm_count_bits_set (mode.allowed) > 1 && mm_get_uint_from_match_info (match_info, 2, &xmm_mode)) {
if (xmm_mode >= G_N_ELEMENTS (xmm_modes)) {
inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED, "Unsupported XACT preferred AcT value: %u", xmm_mode);
goto out;
}
mode.preferred = xmm_modes[xmm_mode];
}
/* Number at index 3: ignored */
}
if (bands_out) {
gchar *bandstr;
GArray *nums;
/* Bands start at index 4 */
bandstr = mm_get_string_unquoted_from_match_info (match_info, 4);
nums = mm_parse_uint_list (bandstr, &inner_error);
g_free (bandstr);
if (inner_error)
goto out;
if (!nums) {
inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED, "Invalid XACT? response");
goto out;
}
bands = g_array_sized_new (FALSE, FALSE, sizeof (MMModemBand), nums->len);
for (i = 0; i < nums->len; i++) {
MMModemBand band;
band = xact_num_to_band (g_array_index (nums, guint, i));
if (band != MM_MODEM_BAND_UNKNOWN)
g_array_append_val (bands, band);
}
g_array_unref (nums);
if (bands->len == 0) {
inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED, "Missing current band list");
goto out;
}
}
}
/* success */
out:
if (match_info)
g_match_info_free (match_info);
g_regex_unref (r);
if (inner_error) {
if (bands)
g_array_unref (bands);
g_propagate_error (error, inner_error);
return FALSE;
}
if (mode_out) {
g_assert (mode.allowed != MM_MODEM_MODE_NONE);
mode_out->allowed = mode.allowed;
mode_out->preferred = mode.preferred;
}
if (bands_out) {
g_assert (bands);
*bands_out = bands;
}
return TRUE;
}
|