Debian Codegraph @

src/mbimcli/mbimcli-helpers.c

raw
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 /*
4 * mbimcli -- Command line interface to control MBIM devices
5 *
6 * Copyright (C) 2014 Aleksander Morgado <[email protected]>
7 */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <errno.h>
13
14 #include "mbimcli-helpers.h"
15
16 gboolean
17 mbimcli_read_uint_from_string (const gchar *str,
18 guint *out)
19 {
20 gulong num;
21
22 if (!str || !str[0])
23 return FALSE;
24
25 for (num = 0; str[num]; num++) {
26 if (!g_ascii_isdigit (str[num]))
27 return FALSE;
28 }
29
30 errno = 0;
31 num = strtoul (str, NULL, 10);
32 if (!errno && num <= G_MAXUINT) {
33 *out = (guint)num;
34 return TRUE;
35 }
36 return FALSE;
37 }
38
39 gboolean
40 mbimcli_read_uint_from_bcd_string (const gchar *str,
41 guint *out)
42 {
43 gulong num;
44
45 if (!str || !str[0])
46 return FALSE;
47
48 /* in bcd, only numeric values (0-9) */
49 for (num = 0; str[num]; num++) {
50 if (!g_ascii_isdigit (str[num]))
51 return FALSE;
52 }
53
54 /* for the numeric values of str, we can just read the string as hex
55 * (base 16) and it will be valid bcd */
56 errno = 0;
57 num = strtoul (str, NULL, 16);
58 if (!errno && num <= G_MAXUINT) {
59 *out = (guint)num;
60 return TRUE;
61 }
62 return FALSE;
63 }
64
65 gboolean
66 mbimcli_read_uint8_from_bcd_string (const gchar *str,
67 guint8 *out)
68 {
69 guint num;
70
71 if (!mbimcli_read_uint_from_bcd_string (str, &num) || (num > G_MAXUINT8))
72 return FALSE;
73
74 *out = (guint8)num;
75 return TRUE;
76 }
77
78 gboolean
79 mbimcli_read_boolean_from_string (const gchar *value,
80 gboolean *out)
81 {
82 if (!g_ascii_strcasecmp (value, "true") || g_str_equal (value, "1") || !g_ascii_strcasecmp (value, "yes")) {
83 *out = TRUE;
84 return TRUE;
85 }
86
87 if (!g_ascii_strcasecmp (value, "false") || g_str_equal (value, "0") || !g_ascii_strcasecmp (value, "no")) {
88 *out = FALSE;
89 return TRUE;
90 }
91
92 return FALSE;
93 }
94
95 /* Based on ModemManager's mm_utils_hexstr2bin() */
96
97 static gint
98 hex2num (gchar c)
99 {
100 if (c >= '0' && c <= '9')
101 return c - '0';
102 if (c >= 'a' && c <= 'f')
103 return c - 'a' + 10;
104 if (c >= 'A' && c <= 'F')
105 return c - 'A' + 10;
106 return -1;
107 }
108
109 static gint
110 hex2byte (const gchar *hex)
111 {
112 gint a, b;
113
114 a = hex2num (*hex++);
115 if (a < 0)
116 return -1;
117 b = hex2num (*hex++);
118 if (b < 0)
119 return -1;
120 return (a << 4) | b;
121 }
122
123 guint8 *
124 mbimcli_read_buffer_from_string (const gchar *hex,
125 gssize len,
126 gsize *out_len,
127 GError **error)
128 {
129 const gchar *ipos = hex;
130 g_autofree guint8 *buf = NULL;
131 gssize i;
132 gint a;
133 guint8 *opos;
134
135 if (len < 0)
136 len = strlen (hex);
137
138 if (len == 0) {
139 g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_FAILED,
140 "Hex conversion failed: empty string");
141 return NULL;
142 }
143
144 /* Length must be a multiple of 2 */
145 if ((len % 2) != 0) {
146 g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_FAILED,
147 "Hex conversion failed: invalid input length");
148 return NULL;
149 }
150
151 opos = buf = g_malloc0 (len / 2);
152 for (i = 0; i < len; i += 2) {
153 a = hex2byte (ipos);
154 if (a < 0) {
155 g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_FAILED,
156 "Hex byte conversion from '%c%c' failed",
157 ipos[0], ipos[1]);
158 return NULL;
159 }
160 *opos++ = (guint8)a;
161 ipos += 2;
162 }
163 *out_len = len / 2;
164 return g_steal_pointer (&buf);
165 }
166
167 gboolean
168 mbimcli_print_ip_config (MbimDevice *device,
169 MbimMessage *response,
170 GError **error)
171 {
172 MbimIPConfigurationAvailableFlag ipv4configurationavailable;
173 g_autofree gchar *ipv4configurationavailable_str = NULL;
174 MbimIPConfigurationAvailableFlag ipv6configurationavailable;
175 g_autofree gchar *ipv6configurationavailable_str = NULL;
176 guint32 ipv4addresscount;
177 g_autoptr(MbimIPv4ElementArray) ipv4address = NULL;
178 guint32 ipv6addresscount;
179 g_autoptr(MbimIPv6ElementArray) ipv6address = NULL;
180 const MbimIPv4 *ipv4gateway;
181 const MbimIPv6 *ipv6gateway;
182 guint32 ipv4dnsservercount;
183 g_autofree MbimIPv4 *ipv4dnsserver = NULL;
184 guint32 ipv6dnsservercount;
185 g_autofree MbimIPv6 *ipv6dnsserver = NULL;
186 guint32 ipv4mtu;
187 guint32 ipv6mtu;
188
189 if (!mbim_message_ip_configuration_response_parse (
190 response,
191 NULL, /* sessionid */
192 &ipv4configurationavailable,
193 &ipv6configurationavailable,
194 &ipv4addresscount,
195 &ipv4address,
196 &ipv6addresscount,
197 &ipv6address,
198 &ipv4gateway,
199 &ipv6gateway,
200 &ipv4dnsservercount,
201 &ipv4dnsserver,
202 &ipv6dnsservercount,
203 &ipv6dnsserver,
204 &ipv4mtu,
205 &ipv6mtu,
206 error))
207 return FALSE;
208
209 /* IPv4 info */
210
211 ipv4configurationavailable_str = mbim_ip_configuration_available_flag_build_string_from_mask (ipv4configurationavailable);
212 g_print ("\n[%s] IPv4 configuration available: '%s'\n", mbim_device_get_path_display (device), ipv4configurationavailable_str);
213
214 if (ipv4configurationavailable & MBIM_IP_CONFIGURATION_AVAILABLE_FLAG_ADDRESS) {
215 guint i;
216
217 for (i = 0; i < ipv4addresscount; i++) {
218 g_autoptr(GInetAddress) addr = NULL;
219 g_autofree gchar *addr_str = NULL;
220
221 addr = g_inet_address_new_from_bytes ((guint8 *)&ipv4address[i]->ipv4_address, G_SOCKET_FAMILY_IPV4);
222 addr_str = g_inet_address_to_string (addr);
223 g_print (" IP [%u]: '%s/%u'\n", i, addr_str, ipv4address[i]->on_link_prefix_length);
224 }
225 }
226
227 if (ipv4configurationavailable & MBIM_IP_CONFIGURATION_AVAILABLE_FLAG_GATEWAY) {
228 g_autoptr(GInetAddress) addr = NULL;
229 g_autofree gchar *addr_str = NULL;
230
231 addr = g_inet_address_new_from_bytes ((guint8 *)ipv4gateway, G_SOCKET_FAMILY_IPV4);
232 addr_str = g_inet_address_to_string (addr);
233 g_print (" Gateway: '%s'\n", addr_str);
234 }
235
236 if (ipv4configurationavailable & MBIM_IP_CONFIGURATION_AVAILABLE_FLAG_DNS) {
237 guint i;
238
239 for (i = 0; i < ipv4dnsservercount; i++) {
240 g_autoptr(GInetAddress) addr = NULL;
241
242 addr = g_inet_address_new_from_bytes ((guint8 *)&ipv4dnsserver[i], G_SOCKET_FAMILY_IPV4);
243 if (!g_inet_address_get_is_any (addr)) {
244 g_autofree gchar *addr_str = NULL;
245
246 addr_str = g_inet_address_to_string (addr);
247 g_print (" DNS [%u]: '%s'\n", i, addr_str);
248 }
249 }
250 }
251
252 if (ipv4configurationavailable & MBIM_IP_CONFIGURATION_AVAILABLE_FLAG_MTU)
253 g_print (" MTU: '%u'\n", ipv4mtu);
254
255 /* IPv6 info */
256 ipv6configurationavailable_str = mbim_ip_configuration_available_flag_build_string_from_mask (ipv6configurationavailable);
257 g_print ("\n[%s] IPv6 configuration available: '%s'\n", mbim_device_get_path_display (device), ipv6configurationavailable_str);
258
259 if (ipv6configurationavailable & MBIM_IP_CONFIGURATION_AVAILABLE_FLAG_ADDRESS) {
260 guint i;
261
262 for (i = 0; i < ipv6addresscount; i++) {
263 g_autoptr(GInetAddress) addr = NULL;
264 g_autofree gchar *addr_str = NULL;
265
266 addr = g_inet_address_new_from_bytes ((guint8 *)&ipv6address[i]->ipv6_address, G_SOCKET_FAMILY_IPV6);
267 addr_str = g_inet_address_to_string (addr);
268 g_print (" IP [%u]: '%s/%u'\n", i, addr_str, ipv6address[i]->on_link_prefix_length);
269 }
270 }
271
272 if (ipv6configurationavailable & MBIM_IP_CONFIGURATION_AVAILABLE_FLAG_GATEWAY) {
273 g_autoptr(GInetAddress) addr = NULL;
274 g_autofree gchar *addr_str = NULL;
275
276 addr = g_inet_address_new_from_bytes ((guint8 *)ipv6gateway, G_SOCKET_FAMILY_IPV6);
277 addr_str = g_inet_address_to_string (addr);
278 g_print (" Gateway: '%s'\n", addr_str);
279 }
280
281 if (ipv6configurationavailable & MBIM_IP_CONFIGURATION_AVAILABLE_FLAG_DNS) {
282 guint i;
283
284 for (i = 0; i < ipv6dnsservercount; i++) {
285 g_autoptr(GInetAddress) addr = NULL;
286
287 addr = g_inet_address_new_from_bytes ((guint8 *)&ipv6dnsserver[i], G_SOCKET_FAMILY_IPV6);
288 if (!g_inet_address_get_is_any (addr)) {
289 g_autofree gchar *addr_str = NULL;
290
291 addr_str = g_inet_address_to_string (addr);
292 g_print (" DNS [%u]: '%s'\n", i, addr_str);
293 }
294 }
295 }
296
297 if (ipv6configurationavailable & MBIM_IP_CONFIGURATION_AVAILABLE_FLAG_MTU)
298 g_print (" MTU: '%u'\n", ipv6mtu);
299
300 return TRUE;
301 }
302
303 /* Expecting input as:
304 * key1=string,key2=true,key3=false...
305 * Strings may also be passed enclosed between double or single quotes, like:
306 * key1="this is a string", key2='and so is this'
307 */
308 gboolean
309 mbimcli_parse_key_value_string (const gchar *str,
310 GError **error,
311 MbimParseKeyValueForeachFn callback,
312 gpointer user_data)
313 {
314 GError *inner_error = NULL;
315 g_autofree gchar *dupstr = NULL;
316 gchar *p, *key, *key_end, *value, *value_end, quote;
317
318 g_return_val_if_fail (callback != NULL, FALSE);
319 g_return_val_if_fail (str != NULL, FALSE);
320
321 /* Allow empty strings, we'll just return with success */
322 while (g_ascii_isspace (*str))
323 str++;
324 if (!str[0])
325 return TRUE;
326
327 dupstr = g_strdup (str);
328 p = dupstr;
329
330 while (TRUE) {
331 gboolean keep_iteration = FALSE;
332
333 /* Skip leading spaces */
334 while (g_ascii_isspace (*p))
335 p++;
336
337 /* Key start */
338 key = p;
339 if (!g_ascii_isalnum (*key)) {
340 inner_error = g_error_new (MBIM_CORE_ERROR,
341 MBIM_CORE_ERROR_FAILED,
342 "Key must start with alpha/num, starts with '%c'",
343 *key);
344 break;
345 }
346
347 /* Key end */
348 while (g_ascii_isalnum (*p) || (*p == '-') || (*p == '_'))
349 p++;
350 key_end = p;
351 if (key_end == key) {
352 inner_error = g_error_new (MBIM_CORE_ERROR,
353 MBIM_CORE_ERROR_FAILED,
354 "Couldn't find a proper key");
355 break;
356 }
357
358 /* Skip whitespaces, if any */
359 while (g_ascii_isspace (*p))
360 p++;
361
362 /* Equal sign must be here */
363 if (*p != '=') {
364 inner_error = g_error_new (MBIM_CORE_ERROR,
365 MBIM_CORE_ERROR_FAILED,
366 "Couldn't find equal sign separator");
367 break;
368 }
369 /* Skip the equal */
370 p++;
371
372 /* Skip whitespaces, if any */
373 while (g_ascii_isspace (*p))
374 p++;
375
376 /* Do we have a quote-enclosed string? */
377 if (*p == '\"' || *p == '\'') {
378 quote = *p;
379 /* Skip the quote */
380 p++;
381 /* Value start */
382 value = p;
383 /* Find the closing quote */
384 p = strchr (p, quote);
385 if (!p) {
386 inner_error = g_error_new (MBIM_CORE_ERROR,
387 MBIM_CORE_ERROR_FAILED,
388 "Unmatched quotes in string value");
389 break;
390 }
391
392 /* Value end */
393 value_end = p;
394 /* Skip the quote */
395 p++;
396 } else {
397 /* Value start */
398 value = p;
399
400 /* Value end */
401 while ((*p != ',') && (*p != '\0') && !g_ascii_isspace (*p))
402 p++;
403 value_end = p;
404 }
405
406 /* Note that we allow value == value_end here */
407
408 /* Skip whitespaces, if any */
409 while (g_ascii_isspace (*p))
410 p++;
411
412 /* If a comma is found, we should keep the iteration */
413 if (*p == ',') {
414 /* skip the comma */
415 p++;
416 keep_iteration = TRUE;
417 }
418
419 /* Got key and value, prepare them and run the callback */
420 *value_end = '\0';
421 *key_end = '\0';
422 if (!callback (key, value, &inner_error, user_data)) {
423 /* We were told to abort */
424 break;
425 }
426 g_assert (!inner_error);
427
428 if (keep_iteration)
429 continue;
430
431 /* Check if no more key/value pairs expected */
432 if (*p == '\0')
433 break;
434
435 inner_error = g_error_new (MBIM_CORE_ERROR,
436 MBIM_CORE_ERROR_FAILED,
437 "Unexpected content (%s) after value",
438 p);
439 break;
440 }
441
442 if (inner_error) {
443 g_propagate_error (error, inner_error);
444 return FALSE;
445 }
446
447 return TRUE;
448 }
449
450 gboolean
451 mbimcli_parse_sar_config_state_array (const gchar *str,
452 GPtrArray **out,
453 GError **error)
454 {
455 g_autoptr(GPtrArray) config_state_array = NULL;
456 g_autoptr(GRegex) regex = NULL;
457 g_autoptr(GMatchInfo) match_info = NULL;
458 g_autoptr(GError) inner_error = NULL;
459
460 config_state_array = g_ptr_array_new_with_free_func (g_free);
461
462 if (!str || !str[0]) {
463 *out = NULL;
464 return TRUE;
465 }
466
467 regex = g_regex_new ("\\s*{\\s*(\\d+|all)\\s*,\\s*(\\d+)\\s*}(?:\\s*,)?", G_REGEX_RAW, 0, NULL);
468 g_assert (regex);
469
470 g_regex_match_full (regex, str, strlen (str), 0, 0, &match_info, &inner_error);
471 while (!inner_error && g_match_info_matches (match_info)) {
472 g_autofree MbimSarConfigState *config_state = NULL;
473 g_autofree gchar *antenna_index_str = NULL;
474 g_autofree gchar *backoff_index_str = NULL;
475
476 config_state = g_new (MbimSarConfigState, 1);
477
478 antenna_index_str = g_match_info_fetch (match_info, 1);
479 backoff_index_str = g_match_info_fetch (match_info, 2);
480
481 if (g_ascii_strcasecmp (antenna_index_str, "all") == 0)
482 config_state->antenna_index = 0xFFFFFFFF;
483 else if (!mbimcli_read_uint_from_string (antenna_index_str, &config_state->antenna_index)) {
484 g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_FAILED,
485 "invalid antenna index: '%s'",
486 antenna_index_str);
487 return FALSE;
488 }
489 if (!mbimcli_read_uint_from_string (backoff_index_str, &config_state->backoff_index)) {
490 g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_FAILED,
491 "invalid backoff index: '%s'",
492 backoff_index_str);
493 return FALSE;
494 }
495
496 g_ptr_array_add (config_state_array, g_steal_pointer (&config_state));
497 g_match_info_next (match_info, &inner_error);
498 }
499
500 if (inner_error) {
501 g_propagate_prefixed_error (error, inner_error,
502 "couldn't match config state array: ");
503 return FALSE;
504 }
505
506 if (config_state_array->len == 0) {
507 g_set_error_literal (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_FAILED,
508 "no elements found in the array");
509 return FALSE;
510 }
511
512 *out = (config_state_array->len > 0) ? g_steal_pointer (&config_state_array) : NULL;
513 return TRUE;
514 }
515
516 #define MBIMCLI_ENUM_LIST_ITEM(TYPE,TYPE_UNDERSCORE,DESCR) \
517 gboolean \
518 mbimcli_read_## TYPE_UNDERSCORE ##_from_string (const gchar *str, \
519 TYPE *out, \
520 GError **error) \
521 { \
522 GType type; \
523 GEnumClass *enum_class; \
524 GEnumValue *enum_value; \
525 \
526 type = mbim_## TYPE_UNDERSCORE ##_get_type (); \
527 enum_class = G_ENUM_CLASS (g_type_class_ref (type)); \
528 enum_value = g_enum_get_value_by_nick (enum_class, str); \
529 \
530 if (enum_value) \
531 *out = (TYPE)enum_value->value; \
532 else { \
533 g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_ARGS,\
534 "error: invalid " DESCR " value given: '%s'", \
535 str); \
536 } \
537 \
538 g_type_class_unref (enum_class); \
539 return !!enum_value; \
540 }
541 MBIMCLI_ENUM_LIST
542 #undef MBIMCLI_ENUM_LIST_ITEM
543
544 #define MBIMCLI_FLAGS_LIST_ITEM(TYPE,TYPE_UNDERSCORE,DESCR) \
545 gboolean \
546 mbimcli_read_## TYPE_UNDERSCORE ##_mask_from_string (const gchar *str, \
547 TYPE *out, \
548 GError **error) \
549 { \
550 GType type; \
551 GFlagsClass *flags_class; \
552 g_auto(GStrv) split = NULL; \
553 guint i; \
554 GError *inner_error = NULL; \
555 \
556 *out = 0; \
557 \
558 if (!str) \
559 return TRUE; \
560 \
561 type = mbim_## TYPE_UNDERSCORE ##_get_type (); \
562 flags_class = G_FLAGS_CLASS (g_type_class_ref (type)); \
563 \
564 split = g_strsplit (str, "|", -1); \
565 for (i = 0; split && split[i] && !inner_error; i++) { \
566 GFlagsValue *flags_value; \
567 \
568 flags_value = g_flags_get_value_by_nick (flags_class, split[i]); \
569 if (flags_value) \
570 *out |= (TYPE)flags_value->value; \
571 else { \
572 inner_error = g_error_new (MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_ARGS, \
573 "error: invalid " DESCR " value given: '%s'", \
574 str); \
575 } \
576 } \
577 \
578 g_type_class_unref (flags_class); \
579 \
580 if (inner_error) { \
581 g_propagate_error (error, inner_error); \
582 return FALSE; \
583 } \
584 return TRUE; \
585 }
586 MBIMCLI_FLAGS_LIST
587 #undef MBIMCLI_FLAGS_LIST_ITEM