-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathmm-modem-helpers.c
More file actions
5938 lines (5041 loc) · 190 KB
/
Copy pathmm-modem-helpers.c
File metadata and controls
5938 lines (5041 loc) · 190 KB
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- 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) 2008 - 2009 Novell, Inc.
* Copyright (C) 2009 - 2012 Red Hat, Inc.
* Copyright (C) 2012 Google, Inc.
* Copyright (c) 2021 Qualcomm Innovation Center, Inc.
* Copyright (C) 2024 JUCR GmbH
*/
#include <config.h>
#include <stdio.h>
#include <ctype.h>
#include <glib.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <ModemManager.h>
#define _LIBMM_INSIDE_MM
#include <libmm-glib.h>
#include "mm-sms-part.h"
#include "mm-common-helpers.h"
#include "mm-modem-helpers.h"
#include "mm-helper-enums-types.h"
#include "mm-log-object.h"
/*****************************************************************************/
gchar *
mm_strip_quotes (gchar *str)
{
gsize len;
if (!str)
return NULL;
len = strlen (str);
if ((len >= 2) && (str[0] == '"') && (str[len - 1] == '"')) {
str[0] = ' ';
str[len - 1] = ' ';
}
return g_strstrip (str);
}
const gchar *
mm_strip_tag (const gchar *str, const gchar *cmd)
{
const gchar *p = str;
if (p) {
if (!strncmp (p, cmd, strlen (cmd)))
p += strlen (cmd);
while (isspace (*p))
p++;
}
return p;
}
/*****************************************************************************/
gchar **
mm_split_string_groups (const gchar *str)
{
GPtrArray *array;
const gchar *start;
const gchar *next;
array = g_ptr_array_new ();
/*
* Manually parse splitting groups. Groups may be single elements, or otherwise
* lists given between parenthesis, e.g.:
*
* ("SM","ME"),("SM","ME"),("SM","ME")
* "SM","SM","SM"
* "SM",("SM","ME"),("SM","ME")
*/
/* Iterate string splitting groups */
for (start = str; start; start = next) {
gchar *item;
gssize len = -1;
/* skip leading whitespaces */
while (*start == ' ')
start++;
if (*start == '(') {
start++;
next = strchr (start, ')');
if (next) {
len = next - start;
next = strchr (next, ',');
if (next)
next++;
}
} else {
next = strchr (start, ',');
if (next) {
len = next - start;
next++;
}
}
if (len < 0)
item = g_strdup (start);
else
item = g_strndup (start, len);
g_ptr_array_add (array, item);
}
if (array->len > 0) {
g_ptr_array_add (array, NULL);
return (gchar **) g_ptr_array_free (array, FALSE);
}
g_ptr_array_unref (array);
return NULL;
}
/*****************************************************************************/
static int uint_compare_func (gconstpointer a, gconstpointer b)
{
return (*(guint *)a - *(guint *)b);
}
GArray *
mm_parse_uint_list (const gchar *str,
GError **error)
{
GArray *array;
gchar *dupstr;
gchar *aux;
GError *inner_error = NULL;
if (!str || !str[0])
return NULL;
/* Parses into a GArray of guints, the list of numbers given in the string,
* also supporting number intervals.
* E.g.:
* 1-6 --> 1,2,3,4,5,6
* 1,2,4-6 --> 1,2,4,5,6
*/
array = g_array_new (FALSE, FALSE, sizeof (guint));
aux = dupstr = g_strdup (str);
while (aux) {
gchar *next;
gchar *interval;
next = strchr (aux, ',');
if (next) {
*next = '\0';
next++;
}
interval = strchr (aux, '-');
if (interval) {
guint start = 0;
guint stop = 0;
*interval = '\0';
interval++;
if (!mm_get_uint_from_str (aux, &start)) {
inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"couldn't parse interval start integer: '%s'", aux);
goto out;
}
if (!mm_get_uint_from_str (interval, &stop)) {
inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"couldn't parse interval stop integer: '%s'", interval);
goto out;
}
if (start > stop) {
inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"interval start (%u) cannot be bigger than interval stop (%u)", start, stop);
goto out;
}
for (; start <= stop; start++)
g_array_append_val (array, start);
} else {
guint num;
if (!mm_get_uint_from_str (aux, &num)) {
inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"couldn't parse integer: '%s'", aux);
goto out;
}
g_array_append_val (array, num);
}
aux = next;
}
if (!array->len)
inner_error = g_error_new (MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"couldn't parse list of integers: '%s'", str);
else
g_array_sort (array, uint_compare_func);
out:
g_free (dupstr);
if (inner_error) {
g_propagate_error (error, inner_error);
g_array_unref (array);
return NULL;
}
return array;
}
/*****************************************************************************/
guint
mm_count_bits_set (gulong number)
{
guint c;
for (c = 0; number; c++)
number &= number - 1;
return c;
}
guint
mm_find_bit_set (gulong number)
{
guint c = 0;
for (c = 0; !(number & 0x1); c++)
number >>= 1;
return c;
}
/*****************************************************************************/
gchar *
mm_create_device_identifier (guint vid,
guint pid,
gpointer log_object,
const gchar *ati,
const gchar *ati1,
const gchar *gsn,
const gchar *revision,
const gchar *model,
const gchar *manf)
{
g_autoptr(GString) devid = NULL;
g_autoptr(GString) msg = NULL;
g_autoptr(GChecksum) sum = NULL;
const gchar *ret;
gchar *p = NULL;
gchar str_vid[10], str_pid[10];
/* Build up the device identifier */
devid = g_string_sized_new (50);
if (ati)
g_string_append (devid, ati);
if (ati1) {
/* Only append "ATI1" if it's different than "ATI" */
if (!ati || (strcmp (ati, ati1) != 0))
g_string_append (devid, ati1);
}
if (gsn)
g_string_append (devid, gsn);
if (revision)
g_string_append (devid, revision);
if (model)
g_string_append (devid, model);
if (manf)
g_string_append (devid, manf);
if (!strlen (devid->str))
return NULL;
p = devid->str;
msg = g_string_sized_new (strlen (devid->str) + 17);
sum = g_checksum_new (G_CHECKSUM_SHA1);
if (vid) {
snprintf (str_vid, sizeof (str_vid) - 1, "%08x", vid);
g_checksum_update (sum, (const guchar *) &str_vid[0], strlen (str_vid));
g_string_append_printf (msg, "%08x", vid);
}
if (pid) {
snprintf (str_pid, sizeof (str_pid) - 1, "%08x", pid);
g_checksum_update (sum, (const guchar *) &str_pid[0], strlen (str_pid));
g_string_append_printf (msg, "%08x", pid);
}
while (*p) {
/* Strip spaces and linebreaks */
if (!isblank (*p) && !isspace (*p) && isascii (*p)) {
g_checksum_update (sum, (const guchar *) p, 1);
g_string_append_c (msg, *p);
}
p++;
}
ret = g_checksum_get_string (sum);
mm_obj_dbg (log_object, "device identifier built: %s -> %s", msg->str, ret);
return g_strdup (ret);
}
/*****************************************************************************/
guint
mm_netmask_to_cidr (const gchar *netmask)
{
guint32 num = 0;
inet_pton (AF_INET, netmask, &num);
return mm_count_bits_set (num);
}
/*****************************************************************************/
GArray *
mm_filter_current_bands (const GArray *supported_bands,
const GArray *current_bands)
{
/* We will assure that the list given in 'current' bands maps the list
* given in 'supported' bands, unless 'UNKNOWN' or 'ANY' is given, of
* course */
guint i;
GArray *filtered;
if (!supported_bands ||
supported_bands->len == 0 ||
!current_bands ||
current_bands->len == 0)
return NULL;
if (supported_bands->len == 1 &&
(g_array_index (supported_bands, MMModemBand, 0) == MM_MODEM_BAND_UNKNOWN ||
g_array_index (supported_bands, MMModemBand, 0) == MM_MODEM_BAND_ANY))
return NULL;
if (current_bands->len == 1 &&
(g_array_index (current_bands, MMModemBand, 0) == MM_MODEM_BAND_UNKNOWN ||
g_array_index (current_bands, MMModemBand, 0) == MM_MODEM_BAND_ANY))
return NULL;
filtered = g_array_sized_new (FALSE, FALSE, sizeof (MMModemBand), current_bands->len);
for (i = 0; i < current_bands->len; i++) {
guint j;
for (j = 0; j < supported_bands->len; j++) {
if (g_array_index (supported_bands, MMModemBand, j) == g_array_index (current_bands, MMModemBand, i)) {
g_array_append_val (filtered, g_array_index (current_bands, MMModemBand, i));
/* Found */
break;
}
}
}
if (filtered->len == 0) {
g_array_unref (filtered);
return NULL;
}
return filtered;
}
/*****************************************************************************/
GArray *
mm_filter_supported_modes (const GArray *all,
const GArray *supported_combinations,
gpointer log_object)
{
MMModemModeCombination all_item;
guint i;
GArray *filtered_combinations;
g_return_val_if_fail (all != NULL, NULL);
g_return_val_if_fail (all->len == 1, NULL);
g_return_val_if_fail (supported_combinations != NULL, NULL);
mm_obj_dbg (log_object, "filtering %u supported mode combinations with %u modes",
supported_combinations->len, all->len);
all_item = g_array_index (all, MMModemModeCombination, 0);
g_return_val_if_fail (all_item.allowed != MM_MODEM_MODE_NONE, NULL);
/* We will filter out all combinations which have modes not listed in 'all' */
filtered_combinations = g_array_sized_new (FALSE, FALSE, sizeof (MMModemModeCombination), supported_combinations->len);
for (i = 0; i < supported_combinations->len; i++) {
MMModemModeCombination *mode;
mode = &g_array_index (supported_combinations, MMModemModeCombination, i);
if (!(mode->allowed & ~all_item.allowed)) {
/* Compare only 'allowed', *not* preferred. If there is at least one item with allowed
* containing all supported modes, we're already good to go. This allows us to have a
* default with preferred != NONE (e.g. Wavecom 2G modem with allowed=CS+2G and
* preferred=2G */
g_array_append_val (filtered_combinations, *mode);
}
}
if (filtered_combinations->len == 0)
mm_obj_warn (log_object, "all supported mode combinations were filtered out");
mm_obj_dbg (log_object, "device supports %u different mode combinations",
filtered_combinations->len);
return filtered_combinations;
}
/*****************************************************************************/
static const gchar bcd_chars[] = "0123456789\0\0\0\0\0\0";
gchar *
mm_bcd_to_string (const guint8 *bcd, gsize bcd_len, gboolean low_nybble_first)
{
GString *str;
gsize i;
g_return_val_if_fail (bcd != NULL, NULL);
str = g_string_sized_new (bcd_len * 2 + 1);
for (i = 0 ; i < bcd_len; i++) {
if (low_nybble_first)
str = g_string_append_c (str, bcd_chars[bcd[i] & 0xF]);
str = g_string_append_c (str, bcd_chars[(bcd[i] >> 4) & 0xF]);
if (!low_nybble_first)
str = g_string_append_c (str, bcd_chars[bcd[i] & 0xF]);
}
return g_string_free (str, FALSE);
}
/*****************************************************************************/
gchar *
mm_at_quote_string (const gchar *input)
{
GString *str;
gsize input_len;
input_len = input ? strlen (input) : 0;
str = g_string_sized_new (3 + 3 * input_len); /* worst case */
g_string_append_c (str, '"');
if (input) {
gsize i, len;
len = strlen (input);
for (i = 0 ; i < len; i++) {
if (input[i] < 0x20 || input[i] == '"' || input[i] == '\\')
g_string_append_printf (str, "\\%02X", input[i]);
else
g_string_append_c (str, input[i]);
}
}
g_string_append_c (str, '"');
return g_string_free (str, FALSE);
}
/*****************************************************************************/
GRegex *
mm_call_end_regex_get (void)
{
/* Example:
* <CR><LF>NO ANSWER<CR><LF>
* <LF>NO CARRIER<CR><LF>
* <CR><LF>BUSY<CR><LF>
*
* Some Sierra devices omit the leading <CR> for in-call responses.
*/
return g_regex_new ("(\\r)?\\n(NO CARRIER)|(BUSY)|(NO ANSWER)|(NO DIALTONE)\\r\\n",
G_REGEX_DOLLAR_ENDONLY | G_REGEX_RAW | G_REGEX_OPTIMIZE,
0,
NULL);
}
/*****************************************************************************/
GRegex *
mm_voice_ring_regex_get (void)
{
/* Example:
* <CR><LF>RING<CR><LF>
*/
return g_regex_new ("\\r\\nRING(?:\\r)?\\r\\n",
G_REGEX_RAW | G_REGEX_OPTIMIZE,
0,
NULL);
}
GRegex *
mm_voice_cring_regex_get (void)
{
/* Example:
* <CR><LF>+CRING: VOICE<CR><LF>
* <CR><LF>+CRING: DATA<CR><LF>
*/
return g_regex_new ("\\r\\n\\+CRING:\\s*(\\S+)\\r\\n",
G_REGEX_RAW | G_REGEX_OPTIMIZE,
0,
NULL);
}
GRegex *
mm_voice_clip_regex_get (void)
{
/*
* Only first 2 fields are mandatory:
* +CLIP: <number>,<type>[,<subaddr>,<satype>[,[<alpha>][,<CLI_validity>]]]
*
* Example:
* <CR><LF>+CLIP: "+393351391306",145,,,,0<CR><LF>
* \_ Number \_ Type
*/
return g_regex_new ("\\r\\n\\+CLIP:\\s*([^,\\s]*)\\s*,\\s*(\\d+)\\s*,?(.*)\\r\\n",
G_REGEX_RAW | G_REGEX_OPTIMIZE,
0,
NULL);
}
GRegex *
mm_voice_ccwa_regex_get (void)
{
/*
* Only first 3 fields are mandatory, but we read only the first one
* +CCWA: <number>,<type>,<class>,[<alpha>][,<CLI_validity>[,<subaddr>,<satype>[,<priority>]]]
*
* Example:
* <CR><LF>+CCWA: "+393351391306",145,1
* \_ Number \_ Type
*/
return g_regex_new ("\\r\\n\\+CCWA:\\s*([^,\\s]*)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,?(.*)\\r\\n",
G_REGEX_RAW | G_REGEX_OPTIMIZE,
0,
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,
gpointer log_object,
GList **out_list,
GError **error)
{
g_autoptr(GRegex) r = NULL;
g_autoptr(GMatchInfo) match_info = NULL;
GList *list = NULL;
GError *inner_error = 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) */
/* This next call state number isn't defined by 3GPP, because it
* doesn't make sense to have it when reporting a full list of calls
* via +CLCC (i.e. the absence of the call would mean it's terminated).
* But, this value may be used by other implementations (e.g. SimTech
* plugin) to report that a call is terminated even when the full
* call list isn't being reported. So, let's support it in the generic,
* parser, even if not strictly standard. */
[6] = MM_CALL_STATE_TERMINATED,
};
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_obj_warn (log_object, "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_obj_warn (log_object, "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_obj_warn (log_object, "couldn't parse call state from +CLCC line");
goto next;
}
call_info->state = call_state[aux];
if (!mm_get_uint_from_match_info (match_info, 4, &aux)) {
mm_obj_warn (log_object, "couldn't parse mode from +CLCC line");
goto next;
}
/*
* Skip calls in Fax-only and DATA-only mode (3GPP TS 27.007):
* 0: Voice
* 1: Data
* 2: Fax
* 3: Voice followed by data, voice mode
* 4: Alternating voice/data, voice mode
* 5: Alternating voice/fax, voice mode
* 6: Voice followed by data, data mode
* 7: Alternating voice/data, data mode
* 8: Alternating voice/fax, fax mode
* 9: unknown
*/
if (aux != 0 && aux != 3 && aux != 4 && aux != 5) {
mm_obj_dbg (log_object, "+CLCC line is not a voice call, skipping.");
goto next;
}
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:
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
flow_control_array_to_mask (GArray *array,
const gchar *item,
gpointer log_object)
{
MMFlowControl mask = MM_FLOW_CONTROL_UNKNOWN;
guint i;
for (i = 0; i < array->len; i++) {
guint mode;
mode = g_array_index (array, guint, i);
switch (mode) {
case 0:
mm_obj_dbg (log_object, "%s supports no flow control", item);
mask |= MM_FLOW_CONTROL_NONE;
break;
case 1:
mm_obj_dbg (log_object, "%s supports XON/XOFF flow control", item);
mask |= MM_FLOW_CONTROL_XON_XOFF;
break;
case 2:
mm_obj_dbg (log_object, "%s supports RTS/CTS flow control", item);
mask |= MM_FLOW_CONTROL_RTS_CTS;
break;
default:
break;
}
}
return mask;
}
static MMFlowControl
flow_control_match_info_to_mask (GMatchInfo *match_info,
guint index,
const gchar *item,
gpointer log_object,
GError **error)
{
MMFlowControl mask = MM_FLOW_CONTROL_UNKNOWN;
gchar *aux = NULL;
GArray *array = NULL;
if (!(aux = mm_get_string_unquoted_from_match_info (match_info, index))) {
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"Error retrieving list of supported %s flow control methods", item);
goto out;
}
if (!(array = mm_parse_uint_list (aux, error))) {
g_prefix_error (error, "Error parsing list of supported %s flow control methods: ", item);
goto out;
}
if ((mask = flow_control_array_to_mask (array, item, log_object)) == MM_FLOW_CONTROL_UNKNOWN) {
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"No known %s flow control method given", item);
goto out;
}
out:
g_clear_pointer (&aux, g_free);
g_clear_pointer (&array, g_array_unref);
return mask;
}
MMFlowControl
mm_parse_ifc_test_response (const gchar *response,
gpointer log_object,
GError **error)
{
g_autoptr(GRegex) r = NULL;
g_autoptr(GMatchInfo) match_info = NULL;
GError *inner_error = NULL;
MMFlowControl te_mask = MM_FLOW_CONTROL_UNKNOWN;
MMFlowControl ta_mask = MM_FLOW_CONTROL_UNKNOWN;
MMFlowControl mask = MM_FLOW_CONTROL_UNKNOWN;
r = g_regex_new ("(?:\\+IFC:)?\\s*\\((.*)\\),\\((.*)\\)(?:\\r\\n)?", 0, 0, NULL);
g_assert (r != NULL);
g_regex_match_full (r, response, strlen (response), 0, 0, &match_info, &inner_error);
if (inner_error)
goto out;
if (!g_match_info_matches (match_info)) {
inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED, "Couldn't match response");
goto out;
}
/* Parse TE flow control methods */
if ((te_mask = flow_control_match_info_to_mask (match_info, 1, "TE", log_object, &inner_error)) == MM_FLOW_CONTROL_UNKNOWN)
goto out;
/* Parse TA flow control methods */
if ((ta_mask = flow_control_match_info_to_mask (match_info, 2, "TA", log_object, &inner_error)) == MM_FLOW_CONTROL_UNKNOWN)
goto out;
/* Only those methods in both TA and TE will be the ones we report */
mask = te_mask & ta_mask;
out:
if (inner_error)
g_propagate_error (error, inner_error);
return mask;
}
MMFlowControl
mm_flow_control_from_string (const gchar *str,
GError **error)
{
GFlagsClass *flags_class;
guint value;
guint i;
flags_class = G_FLAGS_CLASS (g_type_class_ref (MM_TYPE_FLOW_CONTROL));
for (i = 0; flags_class->values[i].value_nick; i++) {
if (!g_ascii_strcasecmp (str, flags_class->values[i].value_nick)) {
value = flags_class->values[i].value;
g_type_class_unref (flags_class);
return value;
}
}
g_type_class_unref (flags_class);
g_set_error (error,
MM_CORE_ERROR,
MM_CORE_ERROR_INVALID_ARGS,
"Couldn't match '%s' with a valid MMFlowControl value",
str);
return MM_FLOW_CONTROL_UNKNOWN;
}
/*************************************************************************/
gboolean
mm_modem_3gpp_registration_state_is_registered (MMModem3gppRegistrationState state)
{
switch (state) {
case MM_MODEM_3GPP_REGISTRATION_STATE_HOME:
case MM_MODEM_3GPP_REGISTRATION_STATE_ROAMING:
case MM_MODEM_3GPP_REGISTRATION_STATE_HOME_SMS_ONLY:
case MM_MODEM_3GPP_REGISTRATION_STATE_ROAMING_SMS_ONLY:
case MM_MODEM_3GPP_REGISTRATION_STATE_EMERGENCY_ONLY:
case MM_MODEM_3GPP_REGISTRATION_STATE_HOME_CSFB_NOT_PREFERRED:
case MM_MODEM_3GPP_REGISTRATION_STATE_ROAMING_CSFB_NOT_PREFERRED:
case MM_MODEM_3GPP_REGISTRATION_STATE_ATTACHED_RLOS:
return TRUE;
case MM_MODEM_3GPP_REGISTRATION_STATE_IDLE:
case MM_MODEM_3GPP_REGISTRATION_STATE_SEARCHING:
case MM_MODEM_3GPP_REGISTRATION_STATE_DENIED:
case MM_MODEM_3GPP_REGISTRATION_STATE_UNKNOWN:
default:
return FALSE;
}
}
/*************************************************************************/
static const gchar *creg_regex[] = {
/* +CREG: <stat> (GSM 07.07 CREG=1 unsolicited) */
[0] = "\\+(CREG|CGREG|CEREG|C5GREG):\\s*0*([0-9]+)",
/* +CREG: <n>,<stat> (GSM 07.07 CREG=1 solicited) */
[1] = "\\+(CREG|CGREG|CEREG|C5GREG):\\s*0*([0-9]),\\s*0*([0-9]+)",
/* +CREG: <stat>,<lac>,<ci> (GSM 07.07 CREG=2 unsolicited) */
[2] = "\\+(CREG|CGREG|CEREG):\\s*0*([0-9]+),\\s*([^,\\s]*)\\s*,\\s*([^,\\s]*)",
/* +CREG: <n>,<stat>,<lac>,<ci> (GSM 07.07 solicited and some CREG=2 unsolicited) */
[3] = "\\+(CREG|CGREG|CEREG):\\s*([0-9]),\\s*([0-9]|[1-9][0-9]+)\\s*,\\s*([^,]*)\\s*,\\s*([^,\\s]*)",
[4] = "\\+(CREG|CGREG|CEREG):\\s*0*([0-9]),\\s*0*([0-9]+)\\s*,\\s*(\"[^,]*\")\\s*,\\s*(\"[^,\\s]*\")",
/* +CREG: <stat>,<lac>,<ci>,<AcT> (ETSI 27.007 CREG=2 unsolicited) */
[5] = "\\+(CREG|CGREG|CEREG):\\s*([0-9]|[1-9][0-9]+)\\s*,\\s*([^,\\s]*)\\s*,\\s*([^,\\s]*)\\s*,\\s*([0-9])",
[6] = "\\+(CREG|CGREG|CEREG):\\s*0*([0-9]+)\\s*,\\s*(\"[^,\\s]*\")\\s*,\\s*(\"[^,\\s]*\")\\s*,\\s*0*([0-9])",
/* +CREG: <n>,<stat>,<lac>,<ci>,<AcT> (ETSI 27.007 solicited and some CREG=2 unsolicited) */
[7] = "\\+(CREG|CGREG|CEREG):\\s*0*([0-9]),\\s*0*([0-9]+)\\s*,\\s*([^,\\s]*)\\s*,\\s*([^,\\s]*)\\s*,\\s*0*([0-9])",
/* +CREG: <n>,<stat>,<lac>,<ci>,<AcT?>,<something> (Samsung Wave S8500) */
/* '<CR><LF>+CREG: 2,1,000B,2816, B, C2816<CR><LF><CR><LF>OK<CR><LF>' */
[8] = "\\+(CREG|CGREG):\\s*0*([0-9]),\\s*0*([0-9]+)\\s*,\\s*([^,\\s]*)\\s*,\\s*([^,\\s]*)\\s*,\\s*([^,\\s]*)\\s*,\\s*[^,\\s]*",
/* +CREG: <stat>,<lac>,<ci>,<AcT>,<RAC> (ETSI 27.007 v9.20 CREG=2 unsolicited with RAC) */
[9] = "\\+(CREG|CGREG):\\s*0*([0-9]+)\\s*,\\s*([^,\\s]*)\\s*,\\s*([^,\\s]*)\\s*,\\s*0*([0-9])\\s*,\\s*([^,\\s]*)",
/* +CEREG: <stat>,<lac>,<rac>,<ci>,<AcT> (ETSI 27.007 v8.6 CREG=2 unsolicited with RAC) */
[10] = "\\+(CEREG):\\s*0*([0-9]+)\\s*,\\s*([^,\\s]*)\\s*,\\s*([^,\\s]*)\\s*,\\s*([^,\\s]*)\\s*,\\s*0*([0-9])",
/* +CEREG: <n>,<stat>,<lac>,<rac>,<ci>,<AcT> (ETSI 27.007 v8.6 CREG=2 solicited with RAC) */
[11] = "\\+(CEREG):\\s*0*([0-9]),\\s*0*([0-9]+)\\s*,\\s*([^,\\s]*)\\s*,\\s*([^,\\s]*)\\s*,\\s*([^,\\s]*)\\s*,\\s*0*([0-9])",
/* +C5GREG: <stat>,<lac>,<ci>,<AcT>,<Allowed_NSSAI_length>,<Allowed_NSSAI> (ETSI 27.007 CREG=2 unsolicited) */
[12] = "\\+(C5GREG):\\s*([0-9]+)\\s*,\\s*([^,\\s]*)\\s*,\\s*([^,\\s]*)\\s*,\\s*([0-9]+)\\s*,\\s*([^,\\s]*)\\s*,\\s*([^,\\s]*)",
/* +C5GREG: <n>,<stat>,<lac>,<ci>,<AcT>,<Allowed_NSSAI_length>,<Allowed_NSSAI> (ETSI 27.007 solicited) */
[13] = "\\+(C5GREG):\\s*([0-9]+)\\s*,\\s*([0-9+])\\s*,\\s*([^,\\s]*)\\s*,\\s*([^,\\s]*)\\s*,\\s*([0-9]+)\\s*,\\s*([^,\\s]*)\\s*,\\s*([^,\\s]*)",
};
GPtrArray *
mm_3gpp_creg_regex_get (gboolean solicited)
{
GPtrArray *array;
guint i;
array = g_ptr_array_sized_new (G_N_ELEMENTS (creg_regex));
for (i = 0; i < G_N_ELEMENTS (creg_regex); i++) {
GRegex *regex;
g_autofree gchar *pattern = NULL;
if (solicited) {
pattern = g_strdup_printf ("%s$", creg_regex[i]);
regex = g_regex_new (pattern, G_REGEX_RAW | G_REGEX_OPTIMIZE, 0, NULL);
} else {
pattern = g_strdup_printf ("\\r\\n%s\\r\\n", creg_regex[i]);
regex = g_regex_new (pattern, G_REGEX_RAW | G_REGEX_OPTIMIZE, 0, NULL);
}
g_assert (regex);
g_ptr_array_add (array, regex);
}
return array;
}
void
mm_3gpp_creg_regex_destroy (GPtrArray *array)
{
g_ptr_array_foreach (array, (GFunc) g_regex_unref, NULL);
g_ptr_array_free (array, TRUE);
}
/*************************************************************************/
GRegex *
mm_3gpp_ciev_regex_get (void)
{
return g_regex_new ("\\r\\n\\+CIEV: (.*),(\\d)\\r\\n",
G_REGEX_RAW | G_REGEX_OPTIMIZE,
0,
NULL);
}
/*************************************************************************/
GRegex *
mm_3gpp_cgev_regex_get (void)
{
return g_regex_new ("\\r\\n\\+CGEV:\\s*(.*)\\r\\n",
G_REGEX_RAW | G_REGEX_OPTIMIZE,
0,
NULL);
}
/*************************************************************************/
GRegex *
mm_3gpp_cusd_regex_get (void)
{
return g_regex_new ("\\r\\n\\+CUSD:\\s*(.*)\\r\\n",
G_REGEX_RAW | G_REGEX_OPTIMIZE,
0,
NULL);
}
/*************************************************************************/
GRegex *
mm_3gpp_cmti_regex_get (void)
{
return g_regex_new ("\\r\\n\\+CMTI:\\s*\"(\\S+)\",\\s*(\\d+)\\r\\n",
G_REGEX_RAW | G_REGEX_OPTIMIZE,
0,
NULL);
}
GRegex *
mm_3gpp_cds_regex_get (void)
{
/* Example:
* <CR><LF>+CDS: 24<CR><LF>07914356060013F10659098136395339F6219011707193802190117071938030<CR><LF>
*/
return g_regex_new ("\\r\\n\\+CDS:\\s*(\\d+)\\r\\n(.*)\\r\\n",
G_REGEX_RAW | G_REGEX_OPTIMIZE,
0,
NULL);
}
GRegex *
mm_3gpp_cbm_regex_get (void)
{
/* +CBM: <length><CR><LF><PDU> */
return g_regex_new ("\\r\\n\\+CBM:\\s*(\\d+)\\r\\n(.*)\\r\\n",
G_REGEX_RAW | G_REGEX_OPTIMIZE,
0,
NULL);
}
/*************************************************************************/