Debian Codegraph @

src/mbimcli/mbimcli.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) 2013 - 2023 Aleksander Morgado <[email protected]>
7 * Copyright (C) 2022 Intel Corporation
8 */
9
10 #include "config.h"
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <locale.h>
15 #include <string.h>
16 #include <errno.h>
17
18 #include <glib.h>
19 #include <glib/gprintf.h>
20 #include <gio/gio.h>
21 #include <glib-unix.h>
22
23 #include <libmbim-glib.h>
24
25 #include "mbimcli.h"
26 #include "mbimcli-helpers.h"
27
28 #define PROGRAM_NAME "mbimcli"
29 #define PROGRAM_VERSION PACKAGE_VERSION
30
31 /* Globals */
32 static GMainLoop *loop;
33 static GCancellable *cancellable;
34 static MbimDevice *device;
35 static MbimService service;
36 static gboolean operation_status;
37
38 /* Main options */
39 static gchar *device_str;
40 static gboolean device_open_proxy_flag;
41 static gboolean device_open_ms_mbimex_v2_flag;
42 static gboolean device_open_ms_mbimex_v3_flag;
43 static gchar *no_open_str;
44 static gboolean no_close_flag;
45 static gboolean noop_flag;
46 static gboolean verbose_flag;
47 static gboolean verbose_full_flag;
48 static gboolean silent_flag;
49 static gchar *printable_str;
50 static gboolean version_flag;
51
52 static GOptionEntry main_entries[] = {
53 { "device", 'd', 0, G_OPTION_ARG_STRING, &device_str,
54 "Specify device path",
55 "[PATH]"
56 },
57 { "device-open-proxy", 'p', 0, G_OPTION_ARG_NONE, &device_open_proxy_flag,
58 "Request to use the 'mbim-proxy' proxy",
59 NULL
60 },
61 { "device-open-ms-mbimex-v2", 0, 0, G_OPTION_ARG_NONE, &device_open_ms_mbimex_v2_flag,
62 "Request to enable Microsoft MBIMEx v2.0 support",
63 NULL
64 },
65 { "device-open-ms-mbimex-v3", 0, 0, G_OPTION_ARG_NONE, &device_open_ms_mbimex_v3_flag,
66 "Request to enable Microsoft MBIMEx v3.0 support",
67 NULL
68 },
69 { "no-open", 0, 0, G_OPTION_ARG_STRING, &no_open_str,
70 "Do not explicitly open the MBIM device before running the command",
71 "[Transaction ID]"
72 },
73 { "no-close", 0, 0, G_OPTION_ARG_NONE, &no_close_flag,
74 "Do not close the MBIM device after running the command",
75 NULL
76 },
77 { "noop", 0, 0, G_OPTION_ARG_NONE, &noop_flag,
78 "Don't run any command",
79 NULL
80 },
81 { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose_flag,
82 "Run action with verbose logs, including the debug ones",
83 NULL
84 },
85 { "verbose-full", 0, 0, G_OPTION_ARG_NONE, &verbose_full_flag,
86 "Run action with verbose logs, including the debug ones and personal info",
87 NULL
88 },
89 { "silent", 0, 0, G_OPTION_ARG_NONE, &silent_flag,
90 "Run action with no logs; not even the error/warning ones",
91 NULL
92 },
93 { "printable", 0, 0, G_OPTION_ARG_STRING, &printable_str,
94 "Get the printable info of the given hex encoded MBIM message",
95 "[(Data)]"
96 },
97 { "version", 'V', 0, G_OPTION_ARG_NONE, &version_flag,
98 "Print version",
99 NULL
100 },
101 { NULL, 0, 0, 0, NULL, NULL, NULL }
102 };
103
104 static gboolean
105 signals_handler (gpointer psignum)
106 {
107 if (cancellable) {
108 /* Ignore consecutive requests of cancellation */
109 if (!g_cancellable_is_cancelled (cancellable)) {
110 g_printerr ("cancelling the operation...\n");
111 g_cancellable_cancel (cancellable);
112 /* Re-set the signal handler to allow main loop cancellation on
113 * second signal */
114 g_unix_signal_add (GPOINTER_TO_INT (psignum), (GSourceFunc) signals_handler, psignum);
115 return FALSE;
116 }
117 }
118
119 if (loop && g_main_loop_is_running (loop)) {
120 g_printerr ("cancelling the main loop...\n");
121 g_idle_add ((GSourceFunc) g_main_loop_quit, loop);
122 }
123 return FALSE;
124 }
125
126 static void
127 log_handler (const gchar *log_domain,
128 GLogLevelFlags log_level,
129 const gchar *message,
130 gpointer user_data)
131 {
132 const gchar *log_level_str;
133 time_t now;
134 gchar time_str[64];
135 struct tm *local_time;
136 gboolean err;
137
138 /* Nothing to do if we're silent */
139 if (silent_flag)
140 return;
141
142 now = time ((time_t *) NULL);
143 local_time = localtime (&now);
144 strftime (time_str, 64, "%d %b %Y, %H:%M:%S", local_time);
145 err = FALSE;
146
147 switch (log_level) {
148 case G_LOG_LEVEL_WARNING:
149 log_level_str = "-Warning **";
150 err = TRUE;
151 break;
152
153 case G_LOG_LEVEL_CRITICAL:
154 case G_LOG_LEVEL_ERROR:
155 log_level_str = "-Error **";
156 err = TRUE;
157 break;
158
159 case G_LOG_LEVEL_DEBUG:
160 log_level_str = "[Debug]";
161 break;
162
163 case G_LOG_LEVEL_MESSAGE:
164 case G_LOG_LEVEL_INFO:
165 log_level_str = "";
166 break;
167
168 case G_LOG_FLAG_FATAL:
169 case G_LOG_LEVEL_MASK:
170 case G_LOG_FLAG_RECURSION:
171 default:
172 g_assert_not_reached ();
173 }
174
175 if (!verbose_flag && !verbose_full_flag && !err)
176 return;
177
178 g_fprintf (err ? stderr : stdout,
179 "[%s] %s %s\n",
180 time_str,
181 log_level_str,
182 message);
183 }
184
185 G_GNUC_NORETURN
186 static void
187 print_version_and_exit (void)
188 {
189 g_print (PROGRAM_NAME " " PROGRAM_VERSION "\n"
190 "Copyright (C) 2013-2023 Aleksander Morgado\n"
191 "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl-2.0.html>\n"
192 "This is free software: you are free to change and redistribute it.\n"
193 "There is NO WARRANTY, to the extent permitted by law.\n"
194 "\n");
195 exit (EXIT_SUCCESS);
196 }
197
198 /*****************************************************************************/
199
200 G_GNUC_NORETURN
201 static void
202 print_printable_str_and_exit (const gchar *hex)
203 {
204 g_autofree gchar *printable = NULL;
205 g_autoptr(MbimMessage) message = NULL;
206 g_autoptr(GError) error = NULL;
207 gsize data_size = 0;
208 g_autofree guint8 *data = NULL;
209
210 data = mbimcli_read_buffer_from_string (hex, -1, &data_size, &error);
211 if (!data) {
212 g_printerr ("Failed to read data: %s\n", error->message);
213 exit (EXIT_FAILURE);
214 }
215
216 message = mbim_message_new (data, data_size);
217 if (!mbim_message_validate (message, &error)) {
218 g_printerr ("error: message validation failed: %s\n", error->message);
219 exit (EXIT_FAILURE);
220 }
221
222 printable = mbim_message_get_printable_full (message, 1, 0, "---- ", FALSE, &error);
223 if (!printable) {
224 g_printerr ("error: printable info retrieval failed: %s\n", error->message);
225 exit (EXIT_FAILURE);
226 }
227
228 g_print ("%s\n", printable);
229 exit (EXIT_SUCCESS);
230 }
231
232 /*****************************************************************************/
233 /* Running asynchronously */
234
235 static void
236 device_close_ready (MbimDevice *dev,
237 GAsyncResult *res)
238 {
239 GError *error = NULL;
240
241 if (!mbim_device_close_finish (dev, res, &error)) {
242 g_printerr ("error: couldn't close device: %s\n", error->message);
243 g_error_free (error);
244 } else
245 g_debug ("Device closed");
246
247 /* If we left the device open, dump next transaction id */
248 if (no_close_flag) {
249 guint transaction_id;
250
251 g_object_get (dev,
252 MBIM_DEVICE_TRANSACTION_ID, &transaction_id,
253 NULL);
254
255 g_print ("[%s] Session not closed:\n"
256 "\t TRID: '%u'\n",
257 mbim_device_get_path_display (dev),
258 transaction_id);
259 }
260
261 g_main_loop_quit (loop);
262 }
263
264 void
265 mbimcli_async_operation_done (gboolean reported_operation_status)
266 {
267 /* Keep the result of the operation */
268 operation_status = reported_operation_status;
269
270 /* Cleanup cancellation */
271 g_clear_object (&cancellable);
272
273 /* Set the in-session setup */
274 g_object_set (device,
275 MBIM_DEVICE_IN_SESSION, no_close_flag,
276 NULL);
277
278 /* Close the device */
279 mbim_device_close (device,
280 15,
281 cancellable,
282 (GAsyncReadyCallback) device_close_ready,
283 NULL);
284 }
285
286 static void
287 device_open_ready (MbimDevice *dev,
288 GAsyncResult *res)
289 {
290 GError *error = NULL;
291
292 if (!mbim_device_open_finish (dev, res, &error)) {
293 g_printerr ("error: couldn't open the MbimDevice: %s\n",
294 error->message);
295 exit (EXIT_FAILURE);
296 }
297
298 g_debug ("MBIM Device at '%s' ready",
299 mbim_device_get_path_display (dev));
300
301 /* If no operation requested, finish */
302 if (noop_flag) {
303 mbimcli_async_operation_done (TRUE);
304 return;
305 }
306
307 /* Link management action? */
308 if (mbimcli_link_management_options_enabled ()) {
309 mbimcli_link_management_run (dev, cancellable);
310 return;
311 }
312
313 /* Run the service-specific action */
314 switch (service) {
315 case MBIM_SERVICE_BASIC_CONNECT:
316 mbimcli_basic_connect_run (dev, cancellable);
317 return;
318 case MBIM_SERVICE_PHONEBOOK:
319 mbimcli_phonebook_run (dev, cancellable);
320 return;
321 case MBIM_SERVICE_DSS:
322 mbimcli_dss_run (dev, cancellable);
323 return;
324 case MBIM_SERVICE_MS_FIRMWARE_ID:
325 mbimcli_ms_firmware_id_run (dev, cancellable);
326 return;
327 case MBIM_SERVICE_MS_HOST_SHUTDOWN:
328 mbimcli_ms_host_shutdown_run (dev, cancellable);
329 return;
330 case MBIM_SERVICE_MS_SAR:
331 mbimcli_ms_sar_run (dev, cancellable);
332 return;
333 case MBIM_SERVICE_ATDS:
334 mbimcli_atds_run (dev, cancellable);
335 return;
336 case MBIM_SERVICE_INTEL_FIRMWARE_UPDATE:
337 mbimcli_intel_firmware_update_run (dev, cancellable);
338 return;
339 case MBIM_SERVICE_MS_BASIC_CONNECT_EXTENSIONS:
340 mbimcli_ms_basic_connect_extensions_run (dev, cancellable);
341 return;
342 case MBIM_SERVICE_QUECTEL:
343 mbimcli_quectel_run (dev, cancellable);
344 return;
345 case MBIM_SERVICE_INTEL_THERMAL_RF:
346 mbimcli_intel_thermal_rf_run (dev, cancellable);
347 return;
348 case MBIM_SERVICE_MS_VOICE_EXTENSIONS:
349 mbimcli_ms_voice_extensions_run (dev, cancellable);
350 return;
351 case MBIM_SERVICE_MS_UICC_LOW_LEVEL_ACCESS:
352 mbimcli_ms_uicc_low_level_access_run (dev, cancellable);
353 return;
354 case MBIM_SERVICE_INTEL_MUTUAL_AUTHENTICATION:
355 mbimcli_intel_mutual_authentication_run (dev, cancellable);
356 return;
357 case MBIM_SERVICE_GOOGLE:
358 mbimcli_google_run (dev, cancellable);
359 return;
360 case MBIM_SERVICE_SMS:
361 mbimcli_sms_run (dev, cancellable);
362 return;
363 case MBIM_SERVICE_USSD:
364 case MBIM_SERVICE_STK:
365 case MBIM_SERVICE_AUTH:
366 case MBIM_SERVICE_PROXY_CONTROL:
367 case MBIM_SERVICE_QMI:
368 case MBIM_SERVICE_QDU:
369 case MBIM_SERVICE_INTEL_TOOLS:
370 mbimcli_intel_tools_run (dev, cancellable);
371 return;
372 case MBIM_SERVICE_FIBOCOM:
373 mbimcli_fibocom_run (dev, cancellable);
374 return;
375 case MBIM_SERVICE_COMPAL:
376 mbimcli_compal_run (dev, cancellable);
377 return;
378 case MBIM_SERVICE_INTEL_AT_TUNNEL:
379 mbimcli_intel_at_tunnel_run (dev, cancellable);
380 return;
381
382 /* unsupported actions in the CLI */
383 case MBIM_SERVICE_INVALID:
384 default:
385 g_assert_not_reached ();
386 }
387 }
388
389 static void
390 device_new_ready (GObject *unused,
391 GAsyncResult *res)
392 {
393 GError *error = NULL;
394 MbimDeviceOpenFlags open_flags = MBIM_DEVICE_OPEN_FLAGS_NONE;
395
396 device = mbim_device_new_finish (res, &error);
397 if (!device) {
398 g_printerr ("error: couldn't create MbimDevice: %s\n",
399 error->message);
400 exit (EXIT_FAILURE);
401 }
402
403 /* Set the in-session setup */
404 if (no_open_str) {
405 guint transaction_id;
406
407 if (!mbimcli_read_uint_from_string (no_open_str, &transaction_id)) {
408 g_printerr ("error: invalid transaction ID specified: %s\n",
409 no_open_str);
410 exit (EXIT_FAILURE);
411 }
412
413 g_object_set (device,
414 MBIM_DEVICE_IN_SESSION, TRUE,
415 MBIM_DEVICE_TRANSACTION_ID, transaction_id,
416 NULL);
417 }
418
419 /* Setup device open flags */
420 if (device_open_proxy_flag)
421 open_flags |= MBIM_DEVICE_OPEN_FLAGS_PROXY;
422 if (device_open_ms_mbimex_v2_flag)
423 open_flags |= MBIM_DEVICE_OPEN_FLAGS_MS_MBIMEX_V2;
424 if (device_open_ms_mbimex_v3_flag)
425 open_flags |= MBIM_DEVICE_OPEN_FLAGS_MS_MBIMEX_V3;
426
427 /* Open the device */
428 mbim_device_open_full (device,
429 open_flags,
430 30,
431 cancellable,
432 (GAsyncReadyCallback) device_open_ready,
433 NULL);
434 }
435
436 /*****************************************************************************/
437
438 static void
439 parse_actions (void)
440 {
441 guint actions_enabled = 0;
442
443 if (mbimcli_link_management_options_enabled ())
444 actions_enabled++;
445
446 if (mbimcli_basic_connect_options_enabled ()) {
447 service = MBIM_SERVICE_BASIC_CONNECT;
448 actions_enabled++;
449 }
450
451 if (mbimcli_phonebook_options_enabled ()) {
452 service = MBIM_SERVICE_PHONEBOOK;
453 actions_enabled++;
454 }
455
456 if (mbimcli_dss_options_enabled ()) {
457 service = MBIM_SERVICE_DSS;
458 actions_enabled++;
459 }
460
461 if (mbimcli_ms_firmware_id_options_enabled ()) {
462 service = MBIM_SERVICE_MS_FIRMWARE_ID;
463 actions_enabled++;
464 }
465
466 if (mbimcli_ms_host_shutdown_options_enabled ()) {
467 service = MBIM_SERVICE_MS_HOST_SHUTDOWN;
468 actions_enabled++;
469 }
470
471 if (mbimcli_ms_sar_options_enabled ()) {
472 service = MBIM_SERVICE_MS_SAR;
473 actions_enabled++;
474 }
475
476 if (mbimcli_atds_options_enabled ()) {
477 service = MBIM_SERVICE_ATDS;
478 actions_enabled++;
479 }
480
481 if (mbimcli_intel_firmware_update_options_enabled ()) {
482 service = MBIM_SERVICE_INTEL_FIRMWARE_UPDATE;
483 actions_enabled++;
484 }
485
486 if (mbimcli_ms_basic_connect_extensions_options_enabled ()) {
487 service = MBIM_SERVICE_MS_BASIC_CONNECT_EXTENSIONS;
488 actions_enabled++;
489 }
490
491 if (mbimcli_quectel_options_enabled ()) {
492 service = MBIM_SERVICE_QUECTEL;
493 actions_enabled++;
494 }
495
496 if (mbimcli_intel_thermal_rf_options_enabled ()) {
497 service = MBIM_SERVICE_INTEL_THERMAL_RF;
498 actions_enabled++;
499 }
500
501 if (mbimcli_ms_voice_extensions_options_enabled ()) {
502 service = MBIM_SERVICE_MS_VOICE_EXTENSIONS;
503 actions_enabled++;
504 }
505
506 if (mbimcli_ms_uicc_low_level_access_options_enabled ()) {
507 service = MBIM_SERVICE_MS_UICC_LOW_LEVEL_ACCESS;
508 actions_enabled++;
509 }
510
511 if (mbimcli_intel_mutual_authentication_options_enabled ()) {
512 service = MBIM_SERVICE_INTEL_MUTUAL_AUTHENTICATION;
513 actions_enabled++;
514 }
515
516 if (mbimcli_intel_tools_options_enabled ()) {
517 service = MBIM_SERVICE_INTEL_TOOLS;
518 actions_enabled++;
519 }
520
521 if (mbimcli_google_options_enabled ()) {
522 service = MBIM_SERVICE_GOOGLE;
523 actions_enabled++;
524 }
525
526 if (mbimcli_fibocom_options_enabled ()) {
527 service = MBIM_SERVICE_FIBOCOM;
528 actions_enabled++;
529 }
530
531 if (mbimcli_sms_options_enabled()) {
532 service = MBIM_SERVICE_SMS;
533 actions_enabled++;
534 }
535
536 if (mbimcli_compal_options_enabled ()) {
537 service = MBIM_SERVICE_COMPAL;
538 actions_enabled++;
539 }
540
541 if (mbimcli_intel_at_tunnel_options_enabled ()) {
542 service = MBIM_SERVICE_INTEL_AT_TUNNEL;
543 actions_enabled++;
544 }
545
546 /* Noop */
547 if (noop_flag)
548 actions_enabled++;
549
550 /* Cannot mix actions from different services */
551 if (actions_enabled > 1) {
552 g_printerr ("error: cannot execute multiple actions of different services\n");
553 exit (EXIT_FAILURE);
554 }
555
556 /* No options? */
557 if (actions_enabled == 0) {
558 g_printerr ("error: no actions specified\n");
559 exit (EXIT_FAILURE);
560 }
561
562 if (device_open_ms_mbimex_v2_flag && device_open_ms_mbimex_v3_flag) {
563 g_printerr ("error: cannot request both MBIMEx v2.0 and 3.0 at the same time\n");
564 exit (EXIT_FAILURE);
565 }
566
567 /* Go on! */
568 }
569
570 int main (int argc, char **argv)
571 {
572 g_autoptr(GError) error = NULL;
573 g_autoptr(GFile) file = NULL;
574 g_autoptr(GOptionContext) context = NULL;
575
576 setlocale (LC_ALL, "");
577
578 /* Setup option context, process it and destroy it */
579 context = g_option_context_new ("- Control MBIM devices");
580 g_option_context_add_group (context, mbimcli_basic_connect_get_option_group ());
581 g_option_context_add_group (context, mbimcli_phonebook_get_option_group ());
582 g_option_context_add_group (context, mbimcli_dss_get_option_group ());
583 g_option_context_add_group (context, mbimcli_ms_firmware_id_get_option_group ());
584 g_option_context_add_group (context, mbimcli_ms_host_shutdown_get_option_group ());
585 g_option_context_add_group (context, mbimcli_ms_sar_get_option_group ());
586 g_option_context_add_group (context, mbimcli_atds_get_option_group ());
587 g_option_context_add_group (context, mbimcli_intel_firmware_update_get_option_group ());
588 g_option_context_add_group (context, mbimcli_ms_basic_connect_extensions_get_option_group ());
589 g_option_context_add_group (context, mbimcli_quectel_get_option_group ());
590 g_option_context_add_group (context, mbimcli_link_management_get_option_group ());
591 g_option_context_add_group (context, mbimcli_intel_thermal_rf_get_option_group ());
592 g_option_context_add_group (context, mbimcli_ms_voice_extensions_get_option_group ());
593 g_option_context_add_group (context, mbimcli_ms_uicc_low_level_access_get_option_group ());
594 g_option_context_add_group (context, mbimcli_intel_mutual_authentication_get_option_group ());
595 g_option_context_add_group (context, mbimcli_intel_tools_get_option_group ());
596 g_option_context_add_group (context, mbimcli_google_get_option_group());
597 g_option_context_add_group (context, mbimcli_fibocom_get_option_group());
598 g_option_context_add_group (context, mbimcli_sms_get_option_group ());
599 g_option_context_add_group (context, mbimcli_compal_get_option_group ());
600 g_option_context_add_group (context, mbimcli_intel_at_tunnel_get_option_group ());
601
602 g_option_context_add_main_entries (context, main_entries, NULL);
603 if (!g_option_context_parse (context, &argc, &argv, &error)) {
604 g_printerr ("error: %s\n", error->message);
605 exit (EXIT_FAILURE);
606 }
607
608 if (version_flag)
609 print_version_and_exit ();
610
611 g_log_set_handler (NULL, G_LOG_LEVEL_MASK, log_handler, NULL);
612 g_log_set_handler ("Mbim", G_LOG_LEVEL_MASK, log_handler, NULL);
613
614 if (verbose_flag && verbose_full_flag) {
615 g_printerr ("error: cannot specify --verbose and --verbose-full at the same time\n");
616 exit (EXIT_FAILURE);
617 } else if (verbose_flag) {
618 mbim_utils_set_traces_enabled (TRUE);
619 mbim_utils_set_show_personal_info (FALSE);
620 } else if (verbose_full_flag) {
621 mbim_utils_set_traces_enabled (TRUE);
622 mbim_utils_set_show_personal_info (TRUE);
623 }
624
625 if (printable_str)
626 print_printable_str_and_exit (printable_str);
627
628 /* No device path given? */
629 if (!device_str) {
630 g_printerr ("error: no device path specified\n");
631 exit (EXIT_FAILURE);
632 }
633
634 /* Build new GFile from the commandline arg */
635 file = g_file_new_for_commandline_arg (device_str);
636
637 parse_actions ();
638
639 /* Create requirements for async options */
640 cancellable = g_cancellable_new ();
641 loop = g_main_loop_new (NULL, FALSE);
642
643 /* Setup signals */
644 g_unix_signal_add (SIGINT, (GSourceFunc)signals_handler, GUINT_TO_POINTER (SIGINT));
645 g_unix_signal_add (SIGHUP, (GSourceFunc)signals_handler, GUINT_TO_POINTER (SIGHUP));
646 g_unix_signal_add (SIGTERM, (GSourceFunc)signals_handler, GUINT_TO_POINTER (SIGTERM));
647
648 /* Launch MbimDevice creation */
649 mbim_device_new (file, cancellable, (GAsyncReadyCallback)device_new_ready, NULL);
650 g_main_loop_run (loop);
651
652 if (cancellable)
653 g_object_unref (cancellable);
654 if (device)
655 g_object_unref (device);
656 g_main_loop_unref (loop);
657
658 return (operation_status ? EXIT_SUCCESS : EXIT_FAILURE);
659 }