Debian Codegraph @

src/qmicli/qmicli-helpers.c

raw
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * qmicli -- Command line interface to control QMI devices
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * Copyright (C) 2015 Velocloud Inc.
19 * Copyright (C) 2012-2019 Aleksander Morgado <[email protected]>
20 */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26
27 #include "qmicli-helpers.h"
28
29 #define QMICLI_ENUM_LIST_ITEM(TYPE,TYPE_UNDERSCORE,DESCR) \
30 gboolean \
31 qmicli_read_## TYPE_UNDERSCORE ##_from_string (const gchar *str, \
32 TYPE *out) \
33 { \
34 GType type; \
35 GEnumClass *enum_class; \
36 GEnumValue *enum_value; \
37 \
38 type = qmi_## TYPE_UNDERSCORE ##_get_type (); \
39 enum_class = G_ENUM_CLASS (g_type_class_ref (type)); \
40 enum_value = g_enum_get_value_by_nick (enum_class, str); \
41 \
42 if (enum_value) \
43 *out = (TYPE)enum_value->value; \
44 else \
45 g_printerr ("error: invalid " DESCR " value given: '%s'\n", str); \
46 \
47 g_type_class_unref (enum_class); \
48 return !!enum_value; \
49 }
50 QMICLI_ENUM_LIST
51 #undef QMICLI_ENUM_LIST_ITEM
52
53 #define QMICLI_FLAGS_LIST_ITEM(TYPE,TYPE_UNDERSCORE,DESCR) \
54 gboolean \
55 qmicli_read_## TYPE_UNDERSCORE ##_from_string (const gchar *str, \
56 TYPE *out) \
57 { \
58 GType type; \
59 GFlagsClass *flags_class; \
60 GFlagsValue *flags_value; \
61 gchar **items, **iter; \
62 gboolean success = TRUE; \
63 \
64 type = qmi_## TYPE_UNDERSCORE ##_get_type (); \
65 flags_class = G_FLAGS_CLASS (g_type_class_ref (type)); \
66 \
67 *out = 0; \
68 items = g_strsplit_set (str, "|", 0); \
69 for (iter = items; iter && *iter && success; iter++) { \
70 g_strstrip (*iter); \
71 if (!*iter[0]) \
72 continue; \
73 \
74 flags_value = g_flags_get_value_by_nick (flags_class, *iter); \
75 if (flags_value) { \
76 *out |= (TYPE)flags_value->value; \
77 } else { \
78 g_printerr ("error: unknown " DESCR " value given: '%s'\n", *iter); \
79 success = FALSE; \
80 } \
81 } \
82 \
83 g_strfreev (items); \
84 g_type_class_unref (flags_class); \
85 return success; \
86 }
87 QMICLI_FLAGS_LIST
88 #undef QMICLI_FLAGS_LIST_ITEM
89
90
91 /* For 64-bit flags we don't have GFlagsClass as they're aren't registered in the
92 * type system. Instead, we create a temporary array with all known flag names, and
93 * use it to match the input values given */
94 #define QMICLI_FLAGS64_LIST_ITEM(TYPE,TYPE_UNDERSCORE,DESCR) \
95 gboolean \
96 qmicli_read_## TYPE_UNDERSCORE ##_from_string (const gchar *str, \
97 TYPE *out) \
98 { \
99 gchar *flag_names[64]; \
100 gchar **items, **iter; \
101 guint i; \
102 gboolean success = TRUE; \
103 \
104 for (i = 0; i < G_N_ELEMENTS (flag_names); i++) \
105 flag_names[i] = qmi_ ## TYPE_UNDERSCORE ## _build_string_from_mask (((guint64)1) << i); \
106 \
107 *out = 0; \
108 items = g_strsplit_set (str, "|", 0); \
109 for (iter = items; iter && *iter && success; iter++) { \
110 g_strstrip (*iter); \
111 if (!*iter[0]) \
112 continue; \
113 \
114 for (i = 0; i < G_N_ELEMENTS (flag_names); i++) { \
115 if (g_strcmp0 (*iter, flag_names[i]) == 0) { \
116 *out |= (TYPE)(((guint64)1) << i); \
117 break; \
118 } \
119 } \
120 if (i == G_N_ELEMENTS (flag_names)) { \
121 g_printerr ("error: unknown " DESCR " value given: '%s'\n", *iter); \
122 success = FALSE; \
123 } \
124 } \
125 \
126 for (i = 0; i < G_N_ELEMENTS (flag_names); i++) \
127 g_free (flag_names[i]); \
128 g_strfreev (items); \
129 return success; \
130 }
131 QMICLI_FLAGS64_LIST
132 #undef QMICLI_FLAGS64_LIST_ITEM
133
134 gchar *
135 qmicli_get_raw_data_printable (const GArray *data,
136 gsize max_line_length,
137 const gchar *line_prefix)
138 {
139 gsize i;
140 gsize j;
141 gsize k;
142 gsize new_str_length;
143 gchar *new_str;
144 gsize prefix_len;
145 guint n_lines;
146 gboolean is_new_line;
147
148 g_return_val_if_fail (max_line_length >= 3, NULL);
149
150 if (!data)
151 return g_strdup ("");
152
153 /* Get new string length. If input string has N bytes, we need:
154 * - 1 byte for last NUL char
155 * - 2N bytes for hexadecimal char representation of each byte...
156 * - N-1 bytes for the separator ':'
157 * So... a total of (1+2N+N-1) = 3N bytes are needed... */
158 new_str_length = 3 * data->len;
159
160 /* Effective max line length needs to be multiple of 3, we don't want to
161 * split in half a given byte representation */
162 while (max_line_length % 3 != 0)
163 max_line_length--;
164
165 /* Prefix len includes the newline character plus the length of the input
166 * prefix */
167 prefix_len = strlen (line_prefix) + 1;
168 /* We don't consider the last NUL byte when counting lines to generate */
169 n_lines = (new_str_length - 1) / max_line_length;
170 if ((new_str_length - 1) % max_line_length != 0)
171 n_lines++;
172
173 /* Build new str length expected when we prefix the string and we limit the
174 * line length */
175 new_str_length += (n_lines * prefix_len);
176
177 /* Allocate memory for new array and initialize contents to NUL */
178 new_str = g_malloc0 (new_str_length);
179
180 /* Print hexadecimal representation of each byte... */
181 is_new_line = TRUE;
182 for (i = 0, j = 0, k = 0; i < data->len; i++) {
183 if (is_new_line) {
184 strcpy (&new_str[j], line_prefix);
185 j += strlen (line_prefix);
186 is_new_line = FALSE;
187 }
188
189 /* Print character in output string... */
190 snprintf (&new_str[j], 3, "%02X", g_array_index (data, guint8, i));
191 j+=2;
192 k+=2;
193
194 if (i != (data->len - 1) ) {
195 new_str[j] = ':';
196 j++;
197 k++;
198 }
199
200 if (k % max_line_length == 0 ||
201 i == (data->len -1)) {
202 new_str[j] = '\n';
203 j++;
204 is_new_line = TRUE;
205 }
206 }
207
208 /* Set output string */
209 return new_str;
210 }
211
212 gchar *
213 qmicli_get_firmware_image_unique_id_printable (const GArray *unique_id)
214 {
215 gchar *unique_id_str;
216 guint i;
217 guint n_ascii = 0;
218 gboolean end = FALSE;
219
220 #define UNIQUE_ID_LEN 16
221
222 g_warn_if_fail (unique_id->len <= UNIQUE_ID_LEN);
223 unique_id_str = g_malloc0 (UNIQUE_ID_LEN + 1);
224 memcpy (unique_id_str, unique_id->data, UNIQUE_ID_LEN);
225
226 /* We want an ASCII string that, if finished before the 16 bytes,
227 * is suffixed with NUL bytes. */
228 for (i = 0; i < UNIQUE_ID_LEN; i++) {
229 /* If a byte isn't ASCII, stop */
230 if (unique_id_str[i] & 0x80)
231 break;
232 /* If string isn't finished yet... */
233 if (!end) {
234 /* String finished now */
235 if (unique_id_str[i] == '\0')
236 end = TRUE;
237 else
238 n_ascii++;
239 } else {
240 /* String finished but we then got
241 * another ASCII byte? not possible */
242 if (unique_id_str[i] != '\0')
243 break;
244 }
245 }
246
247 if (i == UNIQUE_ID_LEN && n_ascii > 0)
248 return unique_id_str;
249
250 #undef UNIQUE_ID_LEN
251
252 g_free (unique_id_str);
253
254 /* Get a raw hex string otherwise */
255 unique_id_str = qmicli_get_raw_data_printable (unique_id, 80, "");
256 unique_id_str[strlen (unique_id_str) - 1] = '\0'; /* remove EOL */
257
258 return unique_id_str;
259 }
260
261 gboolean
262 qmicli_read_dms_uim_pin_id_from_string (const gchar *str,
263 QmiDmsUimPinId *out)
264 {
265 if (!str || str[0] == '\0') {
266 g_printerr ("error: expected 'PIN' or 'PIN2', got: none\n");
267 return FALSE;
268 }
269
270 if (g_str_equal (str, "PIN")) {
271 *out = QMI_DMS_UIM_PIN_ID_PIN;
272 return TRUE;
273 }
274
275 if (g_str_equal (str, "PIN2")) {
276 *out = QMI_DMS_UIM_PIN_ID_PIN2;
277 return TRUE;
278 }
279
280 g_printerr ("error: expected 'PIN' or 'PIN2', got: '%s'\n", str);
281 return FALSE;
282 }
283
284 gboolean
285 qmicli_read_uim_pin_id_from_string (const gchar *str,
286 QmiUimPinId *out)
287 {
288 if (!str || str[0] == '\0') {
289 g_printerr ("error: expected 'PIN1', 'PIN2' or 'UPIN', got: none\n");
290 return FALSE;
291 }
292
293 if (g_str_equal (str, "PIN1")) {
294 *out = QMI_UIM_PIN_ID_PIN1;
295 return TRUE;
296 }
297
298 if (g_str_equal (str, "PIN2")) {
299 *out = QMI_UIM_PIN_ID_PIN2;
300 return TRUE;
301 }
302 if (g_str_equal (str, "UPIN")) {
303 *out = QMI_UIM_PIN_ID_UPIN;
304 return TRUE;
305 }
306
307 g_printerr ("error: expected 'PIN1', 'PIN2' or 'UPIN', got: '%s'\n", str);
308 return FALSE;
309 }
310
311 gboolean
312 qmicli_read_ssp_rat_options_from_string (const gchar *str,
313 QmiNasRatModePreference *out_mode_preference,
314 GArray **out_acquisition_order)
315 {
316 GType rat_mode_preference_type;
317 GFlagsClass *rat_mode_preference_flags_class;
318 GFlagsValue *rat_mode_preference_flags_value;
319 gboolean mode_preference_set = FALSE;
320 GType radio_interface_type;
321 GEnumClass *radio_interface_enum_class;
322 GEnumValue *radio_interface_enum_value;
323 gboolean acquisition_order_set = FALSE;
324 gboolean success = TRUE;
325 char **items, **iter;
326
327 rat_mode_preference_type = qmi_nas_rat_mode_preference_get_type ();
328 rat_mode_preference_flags_class = G_FLAGS_CLASS (g_type_class_ref (rat_mode_preference_type));
329 radio_interface_type = qmi_nas_radio_interface_get_type ();
330 radio_interface_enum_class = G_ENUM_CLASS (g_type_class_ref (radio_interface_type));
331
332 *out_mode_preference = 0;
333 *out_acquisition_order = g_array_new (FALSE, FALSE, sizeof (QmiNasRadioInterface));
334
335 items = g_strsplit_set (str, "|", 0);
336 for (iter = items; iter && *iter && success; iter++) {
337 if (!*iter[0])
338 continue;
339
340 /* Note: we can use the same nick names both for mode preference flags
341 * and acquistion order enums, which is very fortunate */
342
343 rat_mode_preference_flags_value = g_flags_get_value_by_nick (rat_mode_preference_flags_class, *iter);
344 if (rat_mode_preference_flags_value) {
345 *out_mode_preference |= (QmiNasRatModePreference)rat_mode_preference_flags_value->value;
346 mode_preference_set = TRUE;
347 } else {
348 g_printerr ("error: invalid rat mode pref value given: '%s'\n", *iter);
349 success = FALSE;
350 }
351
352 radio_interface_enum_value = g_enum_get_value_by_nick (radio_interface_enum_class, *iter);
353 if (radio_interface_enum_value) {
354 QmiNasRadioInterface value;
355
356 value = (QmiNasRadioInterface)(radio_interface_enum_value->value);
357 g_array_append_val (*out_acquisition_order, value);
358 acquisition_order_set = TRUE;
359 } else {
360 g_printerr ("error: invalid radio interface value given: '%s'\n", *iter);
361 success = FALSE;
362 }
363 }
364
365 if (!mode_preference_set)
366 g_printerr ("error: invalid rat mode pref input given: '%s'\n", str);
367 if (!acquisition_order_set)
368 g_printerr ("error: invalid rat mode pref input given: '%s'\n", str);
369
370 if (items)
371 g_strfreev (items);
372 g_type_class_unref (rat_mode_preference_flags_class);
373 g_type_class_unref (radio_interface_enum_class);
374 return success && (mode_preference_set || acquisition_order_set);;
375 }
376
377 static gboolean
378 parse_3gpp_mcc_mnc (const gchar *str,
379 guint16 *out_mcc,
380 guint16 *out_mnc,
381 gboolean *out_pcs_digit)
382 {
383 guint len;
384 guint i;
385 gchar aux[4];
386 guint16 tmp;
387
388 len = strlen (str);
389 if (len != 5 && len != 6)
390 return FALSE;
391 for (i = 0; i < len; i++) {
392 if (!g_ascii_isdigit (str[i]))
393 return FALSE;
394 }
395
396 memcpy (&aux[0], str, 3);
397 aux[3] = '\0';
398 tmp = atoi (aux);
399 if (tmp == 0)
400 return FALSE;
401 *out_mcc = tmp;
402
403 if (len == 5) {
404 memcpy (&aux[0], &str[3], 2);
405 aux[2] = '\0';
406 } else
407 memcpy (&aux[0], &str[3], 3);
408 *out_mnc = atoi (aux);
409 if (out_pcs_digit)
410 *out_pcs_digit = len == 6;
411
412 return TRUE;
413 }
414
415 gboolean
416 qmicli_read_ssp_net_options_from_string (const gchar *str,
417 QmiNasNetworkSelectionPreference *out,
418 guint16 *out_mcc,
419 guint16 *out_mnc)
420 {
421 GType type;
422 GEnumClass *enum_class;
423 GEnumValue *enum_value;
424 gchar *copy, *equals;
425 guint16 mcc = 0, mnc = 0;
426
427 copy = g_strdup (str);
428 equals = strchr (copy, '=');
429 if (equals) {
430 /* Parse MCC/MNC */
431 *equals++ = '\0';
432 if (!parse_3gpp_mcc_mnc (equals, &mcc, &mnc, NULL)) {
433 g_free (copy);
434 g_printerr ("error: invalid net selection MCC/MNC: '%s'\n", equals);
435 return FALSE;
436 }
437 }
438
439 type = qmi_nas_network_selection_preference_get_type ();
440 enum_class = G_ENUM_CLASS (g_type_class_ref (type));
441 enum_value = g_enum_get_value_by_nick (enum_class, copy);
442 if (enum_value) {
443 *out = (QmiNasNetworkSelectionPreference)enum_value->value;
444 *out_mcc = mcc;
445 *out_mnc = mnc;
446 } else
447 g_printerr ("error: invalid net selection preference value given: '%s'\n", copy);
448
449 g_free (copy);
450 g_type_class_unref (enum_class);
451 return !!enum_value;
452 }
453
454 gboolean
455 qmicli_read_parse_3gpp_mcc_mnc (const gchar *str,
456 guint16 *out_mcc,
457 guint16 *out_mnc,
458 gboolean *out_pcs_digit)
459 {
460 g_autofree gchar *copy = NULL;
461 guint16 mcc = 0, mnc = 0;
462 gboolean pcs_digit = FALSE;
463
464 copy = g_strdup (str);
465 if (!parse_3gpp_mcc_mnc (copy, &mcc, &mnc, &pcs_digit)) {
466 g_printerr ("error: invalid net selection MCC/MNC: '%s'\n", str);
467 return FALSE;
468 }
469
470 *out_mcc = mcc;
471 *out_mnc = mnc;
472 if (out_pcs_digit)
473 *out_pcs_digit = pcs_digit;
474
475 return TRUE;
476 }
477
478 gboolean
479 qmicli_read_enable_disable_from_string (const gchar *str,
480 gboolean *out)
481 {
482 if (!str || str[0] == '\0') {
483 g_printerr ("error: expected 'disable' or 'enable', got: none\n");
484 return FALSE;
485 }
486
487 if (g_str_equal (str, "disable")) {
488 *out = FALSE;
489 return TRUE;
490 }
491
492 if (g_str_equal (str, "enable")) {
493 *out = TRUE;
494 return TRUE;
495 }
496
497 g_printerr ("error: expected 'disable' or 'enable', got: '%s'\n", str);
498 return FALSE;
499 }
500
501 gboolean
502 qmicli_read_yes_no_from_string (const gchar *str,
503 gboolean *out)
504 {
505 if (!str || str[0] == '\0') {
506 g_printerr ("error: expected 'true', 'false', 'yes' or 'no', got: none\n");
507 return FALSE;
508 }
509
510 if ((g_strcasecmp (str, "yes") == 0) || (g_strcasecmp (str, "true") == 0)) {
511 *out = TRUE;
512 return TRUE;
513 }
514
515 if ((g_strcasecmp (str, "no") == 0) || (g_strcasecmp (str, "false") == 0)) {
516 *out = FALSE;
517 return TRUE;
518 }
519
520 g_printerr ("error: expected 'true', 'false', 'yes' or 'no', got: %s\n", str);
521 return FALSE;
522 }
523
524 gboolean
525 qmicli_read_non_empty_string (const gchar *str,
526 const gchar *description,
527 gchar **out)
528 {
529 if (!str || str[0] == '\0') {
530 g_printerr ("error: empty %s given\n", description);
531 return FALSE;
532 }
533
534 *out = (gchar *)str;
535 return TRUE;
536 }
537
538 gboolean
539 qmicli_read_raw_data_from_string (const gchar *str,
540 GArray **out)
541 {
542 GArray *array;
543 gsize i;
544 gsize str_len;
545
546 array = g_array_new (FALSE, FALSE, sizeof (guint8));
547
548 str_len = str ? strlen (str) : 0;
549
550 for (i = 0; i < str_len; i += 2) {
551 gint high, low;
552 guint8 value;
553
554 /* For easy processing, we just ignore the ':' chars, if any available */
555 if (str[i] == ':')
556 i++;
557
558 high = g_ascii_xdigit_value (str[i]);
559 if (high < 0 || high > 0xF) {
560 g_printerr ("error: invalid hex char found: '%c'\n", str[i]);
561 g_clear_pointer (&array, g_array_unref);
562 return FALSE;
563 }
564
565 if (!((i + 1) < str_len)) {
566 g_printerr ("unterminated byte found: '%c?'\n", str[i]);
567 g_clear_pointer (&array, g_array_unref);
568 return FALSE;
569 }
570
571 low = g_ascii_xdigit_value (str[i + 1]);
572 if (low < 0 || low > 0xF) {
573 g_printerr ("invalid hex char found: '%c'\n", str[i + 1]);
574 g_clear_pointer (&array, g_array_unref);
575 return FALSE;
576 }
577
578 value = (high << 4) | low;
579 g_array_append_val (array, value);
580 }
581
582 *out = array;
583 return TRUE;
584 }
585
586 gboolean
587 qmicli_read_firmware_id_from_string (const gchar *str,
588 QmiDmsFirmwareImageType *out_type,
589 guint *out_index)
590 {
591 const gchar *index_str;
592
593 if (g_str_has_prefix (str, "modem")) {
594 *out_type = QMI_DMS_FIRMWARE_IMAGE_TYPE_MODEM;
595 index_str = &str[5];
596 } else if (g_str_has_prefix (str, "pri")) {
597 *out_type = QMI_DMS_FIRMWARE_IMAGE_TYPE_PRI;
598 index_str = &str[3];
599 } else {
600 g_printerr ("error: invalid firmware image type value given: '%s'\n", str);
601 return FALSE;
602 }
603
604 return qmicli_read_uint_from_string (index_str, out_index);
605 }
606
607 gboolean
608 qmicli_read_binary_array_from_string (const gchar *str,
609 GArray **out)
610 {
611 gsize i, j, len;
612
613 g_return_val_if_fail (out != NULL, FALSE);
614 g_return_val_if_fail (str, FALSE);
615
616 /* Ignore ':' digits in the binary string input */
617 for (len = 0, i = 0; str[i]; i++) {
618 if (str[i] == ':')
619 continue;
620 len++;
621 }
622
623 /* Length must be a multiple of 2 */
624 if (len & 1)
625 return FALSE;
626
627 *out = g_array_sized_new (FALSE, TRUE, sizeof (guint8), len >> 1);
628 g_array_set_size (*out, len >> 1);
629
630 i = 0;
631 j = 0;
632 while (str[i]) {
633 gint a, b;
634
635 while (str[i] == ':')
636 i++;
637 a = g_ascii_xdigit_value (str[i++]);
638 while (str[i] == ':')
639 i++;
640 b = g_ascii_xdigit_value (str[i++]);
641 if (a < 0 || b < 0) {
642 g_array_unref (*out);
643 return FALSE;
644 }
645
646 g_array_index (*out, guint8, j++) = (a << 4) | b;
647 }
648
649 return TRUE;
650 }
651
652 #if defined HAVE_QMI_MESSAGE_WMS_GET_BROADCAST_CONFIG
653
654 gboolean
655 qmicli_read_cbs_channels_from_string (const gchar *str,
656 GArray **out_channels)
657 {
658 g_autoptr(GArray) array = NULL;
659 g_auto(GStrv) channel_strings = NULL;
660
661 array = g_array_new (FALSE, FALSE, sizeof (QmiMessageWmsSetBroadcastConfigInputChannelsElement));
662 channel_strings = g_strsplit (str, ",", -1);
663 if (channel_strings) {
664 guint i;
665
666 for (i = 0; channel_strings[i]; i++) {
667 char *startptr, *endptr;
668 gint64 start;
669
670 startptr = channel_strings[i];
671 start = g_ascii_strtoll (startptr, &endptr, 10);
672 if (startptr == endptr || start > G_MAXUINT16 || start < 0) {
673 g_printerr ("error: Couldn't parse '%s' as CBS channel start value",
674 channel_strings[i]);
675 return FALSE;
676 }
677 if (*endptr == '\0') {
678 QmiMessageWmsSetBroadcastConfigInputChannelsElement ch;
679
680 ch.start = start;
681 ch.end = start;
682 ch.selected = TRUE;
683 g_array_append_val (array, ch);
684 } else if (*endptr == '-') {
685 gint64 end;
686
687 startptr = endptr + 1;
688 end = g_ascii_strtoll (startptr, &endptr, 10);
689 if (startptr == endptr || end > G_MAXUINT16 || end < 0) {
690 g_printerr ("error: Couldn't parse '%s' as CBS channel end value",
691 channel_strings[i]);
692 return FALSE;
693 }
694 if (*endptr == '\0') {
695 QmiMessageWmsSetBroadcastConfigInputChannelsElement ch;
696
697 ch.start = start;
698 ch.end = end;
699 ch.selected = TRUE;
700 g_array_append_val (array, ch);
701 } else {
702 g_printerr ("error: Couldn't parse '%s' as CBS channel end value",
703 channel_strings[i]);
704 return FALSE;
705 }
706 } else {
707 g_printerr ("error: Couldn't parse '%s' as CBS channel value",
708 channel_strings[i]);
709 return FALSE;
710 }
711 }
712 }
713
714 *out_channels = g_steal_pointer (&array);
715 return TRUE;
716 }
717
718 #endif
719
720 gboolean
721 qmicli_validate_device_open_flags (QmiDeviceOpenFlags mask)
722 {
723 if (!mask) {
724 g_printerr ("error: invalid device open flags given\n");
725 return FALSE;
726 }
727
728 if ((mask & QMI_DEVICE_OPEN_FLAGS_NET_802_3) &&
729 (mask & QMI_DEVICE_OPEN_FLAGS_NET_RAW_IP)) {
730 g_printerr ("error: cannot give both 802.3 and raw-IP options\n");
731 return FALSE;
732 }
733
734 if ((mask & QMI_DEVICE_OPEN_FLAGS_NET_QOS_HEADER) &&
735 (mask & QMI_DEVICE_OPEN_FLAGS_NET_NO_QOS_HEADER)) {
736 g_printerr ("error: cannot request both QoS and no-QoS headers\n");
737 return FALSE;
738 }
739
740 if ((mask & (QMI_DEVICE_OPEN_FLAGS_NET_802_3 | QMI_DEVICE_OPEN_FLAGS_NET_RAW_IP)) &&
741 !(mask & (QMI_DEVICE_OPEN_FLAGS_NET_QOS_HEADER | QMI_DEVICE_OPEN_FLAGS_NET_NO_QOS_HEADER))) {
742 g_printerr ("error: missing QoS or no-QoS header request\n");
743 return FALSE;
744 }
745
746 if ((mask & (QMI_DEVICE_OPEN_FLAGS_NET_QOS_HEADER | QMI_DEVICE_OPEN_FLAGS_NET_NO_QOS_HEADER)) &&
747 !(mask & (QMI_DEVICE_OPEN_FLAGS_NET_802_3 | QMI_DEVICE_OPEN_FLAGS_NET_RAW_IP))) {
748 g_printerr ("error: missing link protocol (802.3 or raw IP)\n");
749 return FALSE;
750 }
751
752 return TRUE;
753 }
754
755 gboolean
756 qmicli_read_authentication_from_string (const gchar *str,
757 QmiWdsAuthentication *out)
758 {
759 if (g_ascii_strcasecmp (str, "PAP") == 0)
760 *out = QMI_WDS_AUTHENTICATION_PAP;
761 else if (g_ascii_strcasecmp (str, "CHAP") == 0)
762 *out = QMI_WDS_AUTHENTICATION_CHAP;
763 else if (g_ascii_strcasecmp (str, "BOTH") == 0)
764 *out = (QMI_WDS_AUTHENTICATION_PAP | QMI_WDS_AUTHENTICATION_CHAP);
765 else if (!str[0] || g_ascii_strcasecmp (str, "NONE") == 0)
766 *out = QMI_WDS_AUTHENTICATION_NONE;
767 else
768 return FALSE;
769
770 return TRUE;
771 }
772
773 gboolean
774 qmicli_read_pdp_type_from_string (const gchar *str,
775 QmiWdsPdpType *out)
776 {
777 if (g_ascii_strcasecmp (str, "IP") == 0 || g_ascii_strcasecmp (str, "IPV4") == 0)
778 *out = QMI_WDS_PDP_TYPE_IPV4;
779 else if (g_ascii_strcasecmp (str, "PPP") == 0)
780 *out = QMI_WDS_PDP_TYPE_PPP;
781 else if (g_ascii_strcasecmp (str, "IPV6") == 0)
782 *out = (QMI_WDS_PDP_TYPE_IPV6);
783 else if (g_ascii_strcasecmp (str, "IPV4V6") == 0)
784 *out = QMI_WDS_PDP_TYPE_IPV4_OR_IPV6;
785 else
786 return FALSE;
787
788 return TRUE;
789 }
790
791 gboolean
792 qmicli_read_uint_from_string (const gchar *str,
793 guint *out)
794 {
795 gulong num;
796
797 if (!str || !str[0])
798 return FALSE;
799
800 for (num = 0; str[num]; num++) {
801 if (!g_ascii_isdigit (str[num]))
802 return FALSE;
803 }
804
805 errno = 0;
806 num = strtoul (str, NULL, 10);
807 if (!errno && num <= G_MAXUINT) {
808 *out = (guint)num;
809 return TRUE;
810 }
811 return FALSE;
812 }
813
814 gchar *
815 qmicli_get_supported_messages_list (const guint8 *data,
816 gsize len)
817 {
818 GString *str = NULL;
819
820 if (len > 0 && data) {
821 guint bytearray_i;
822
823 for (bytearray_i = 0; bytearray_i < len; bytearray_i++) {
824 guint bit_i;
825
826 for (bit_i = 0; bit_i < 8; bit_i++) {
827 if (data[bytearray_i] & (1 << bit_i)) {
828 if (!str)
829 str = g_string_new ("");
830 g_string_append_printf (str, "\t0x%04X\n", (guint16) (bit_i + (8 * bytearray_i)));
831 }
832 }
833 }
834 }
835
836 return (str ? g_string_free (str, FALSE) : g_strdup ("\tnone\n"));
837 }
838
839 /******************************************************************************/
840
841 typedef struct {
842 guint16 min;
843 guint16 max;
844 const gchar *name;
845 } EarfcnRange;
846
847 /* http://niviuk.free.fr/lte_band.php */
848 static const EarfcnRange earfcn_ranges[] = {
849 { 0, 599, "E-UTRA band 1: 2100" },
850 { 600, 1199, "E-UTRA band 2: 1900 PCS" },
851 { 1200, 1949, "E-UTRA band 3: 1800+" },
852 { 1950, 2399, "E-UTRA band 4: AWS-1" },
853 { 2400, 2649, "E-UTRA band 5: 850" },
854 { 2650, 2749, "E-UTRA band 6: UMTS only" },
855 { 2750, 3449, "E-UTRA band 7: 2600" },
856 { 3450, 3799, "E-UTRA band 8: 900" },
857 { 3800, 4149, "E-UTRA band 9: 1800" },
858 { 4150, 4749, "E-UTRA band 10: AWS-1+" },
859 { 4750, 4999, "E-UTRA band 11: 1500 Lower" },
860 { 5000, 5179, "E-UTRA band 12: 700 a" },
861 { 5180, 5279, "E-UTRA band 13: 700 c" },
862 { 5280, 5379, "E-UTRA band 14: 700 PS" },
863 { 5730, 5849, "E-UTRA band 17: 700 b" },
864 { 5850, 5999, "E-UTRA band 18: 800 Lower" },
865 { 6000, 6149, "E-UTRA band 19: 800 Upper" },
866 { 6150, 6449, "E-UTRA band 20: 800 DD" },
867 { 6450, 6599, "E-UTRA band 21: 1500 Upper" },
868 { 6600, 7399, "E-UTRA band 22: 3500" },
869 { 7500, 7699, "E-UTRA band 23: 2000 S-band" },
870 { 7700, 8039, "E-UTRA band 24: 1600 L-band" },
871 { 8040, 8689, "E-UTRA band 25: 1900+" },
872 { 8690, 9039, "E-UTRA band 26: 850+" },
873 { 9040, 9209, "E-UTRA band 27: 800 SMR" },
874 { 9210, 9659, "E-UTRA band 28: 700 APT" },
875 { 9660, 9769, "E-UTRA band 29: 700 d" },
876 { 9770, 9869, "E-UTRA band 30: 2300 WCS" },
877 { 9870, 9919, "E-UTRA band 31: 450" },
878 { 9920, 10359, "E-UTRA band 32: 1500 L-band" },
879 { 36000, 36199, "E-UTRA band 33: TD 1900" },
880 { 36200, 36349, "E-UTRA band 34: TD 2000" },
881 { 36350, 36949, "E-UTRA band 35: TD PCS Lower" },
882 { 36950, 37549, "E-UTRA band 36: TD PCS Upper" },
883 { 37550, 37749, "E-UTRA band 37: TD PCS Center" },
884 { 37750, 38249, "E-UTRA band 38: TD 2600" },
885 { 38250, 38649, "E-UTRA band 39: TD 1900+" },
886 { 38650, 39649, "E-UTRA band 40: TD 2300" },
887 { 39650, 41589, "E-UTRA band 41: TD 2500" },
888 { 41590, 43589, "E-UTRA band 42: TD 3500" },
889 { 43590, 45589, "E-UTRA band 43: TD 3700" },
890 { 45590, 46589, "E-UTRA band 44: TD 700" },
891 };
892
893 const char *
894 qmicli_earfcn_to_eutra_band_string (guint16 earfcn)
895 {
896 guint i;
897
898 for (i = 0; i < G_N_ELEMENTS (earfcn_ranges); i++) {
899 if (earfcn <= earfcn_ranges[i].max && earfcn >= earfcn_ranges[i].min)
900 return earfcn_ranges[i].name;
901 }
902 return "unknown";
903 }
904
905 /* Expecting input as:
906 * key1=string,key2=true,key3=false...
907 * Strings may also be passed enclosed between double or single quotes, like:
908 * key1="this is a string", key2='and so is this'
909 *
910 * Based on libmbim's mbimcli_parse_key_value_string().
911 */
912 gboolean
913 qmicli_parse_key_value_string (const gchar *str,
914 GError **error,
915 QmiParseKeyValueForeachFn callback,
916 gpointer user_data)
917 {
918 GError *inner_error = NULL;
919 gchar *dupstr, *p, *key, *key_end, *value, *value_end, quote;
920
921 g_return_val_if_fail (callback != NULL, FALSE);
922 g_return_val_if_fail (str != NULL, FALSE);
923
924 /* Allow empty strings, we'll just return with success */
925 while (g_ascii_isspace (*str))
926 str++;
927 if (!str[0])
928 return TRUE;
929
930 dupstr = g_strdup (str);
931 p = dupstr;
932
933 while (TRUE) {
934 gboolean keep_iteration = FALSE;
935
936 /* Skip leading spaces */
937 while (g_ascii_isspace (*p))
938 p++;
939
940 /* Key start */
941 key = p;
942 if (!g_ascii_isalnum (*key)) {
943 inner_error = g_error_new (QMI_CORE_ERROR,
944 QMI_CORE_ERROR_FAILED,
945 "Key must start with alpha/num, starts with '%c'",
946 *key);
947 break;
948 }
949
950 /* Key end */
951 while (g_ascii_isalnum (*p) || (*p == '-') || (*p == '_'))
952 p++;
953 key_end = p;
954 if (key_end == key) {
955 inner_error = g_error_new (QMI_CORE_ERROR,
956 QMI_CORE_ERROR_FAILED,
957 "Couldn't find a proper key");
958 break;
959 }
960
961 /* Skip whitespaces, if any */
962 while (g_ascii_isspace (*p))
963 p++;
964
965 /* Equal sign must be here */
966 if (*p != '=') {
967 inner_error = g_error_new (QMI_CORE_ERROR,
968 QMI_CORE_ERROR_FAILED,
969 "Couldn't find equal sign separator");
970 break;
971 }
972 /* Skip the equal */
973 p++;
974
975 /* Skip whitespaces, if any */
976 while (g_ascii_isspace (*p))
977 p++;
978
979 /* Do we have a quote-enclosed string? */
980 if (*p == '\"' || *p == '\'') {
981 quote = *p;
982 /* Skip the quote */
983 p++;
984 /* Value start */
985 value = p;
986 /* Find the closing quote */
987 p = strchr (p, quote);
988 if (!p) {
989 inner_error = g_error_new (QMI_CORE_ERROR,
990 QMI_CORE_ERROR_FAILED,
991 "Unmatched quotes in string value");
992 break;
993 }
994
995 /* Value end */
996 value_end = p;
997 /* Skip the quote */
998 p++;
999 } else {
1000 /* Value start */
1001 value = p;
1002
1003 /* Value end */
1004 while ((*p != ',') && (*p != '\0') && !g_ascii_isspace (*p))
1005 p++;
1006 value_end = p;
1007 }
1008
1009 /* Note that we allow value == value_end here */
1010
1011 /* Skip whitespaces, if any */
1012 while (g_ascii_isspace (*p))
1013 p++;
1014
1015 /* If a comma is found, we should keep the iteration */
1016 if (*p == ',') {
1017 /* skip the comma */
1018 p++;
1019 keep_iteration = TRUE;
1020 }
1021
1022 /* Got key and value, prepare them and run the callback */
1023 *value_end = '\0';
1024 *key_end = '\0';
1025 if (!callback (key, value, &inner_error, user_data)) {
1026 /* We were told to abort */
1027 break;
1028 }
1029 g_assert (!inner_error);
1030
1031 if (keep_iteration)
1032 continue;
1033
1034 /* Check if no more key/value pairs expected */
1035 if (*p == '\0')
1036 break;
1037
1038 inner_error = g_error_new (QMI_CORE_ERROR,
1039 QMI_CORE_ERROR_FAILED,
1040 "Unexpected content (%s) after value",
1041 p);
1042 break;
1043 }
1044
1045 g_free (dupstr);
1046
1047 if (inner_error) {
1048 g_propagate_error (error, inner_error);
1049 return FALSE;
1050 }
1051
1052 return TRUE;
1053 }