Debian Codegraph @

src/qmicli/qmicli.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) 2012-2023 Aleksander Morgado <[email protected]>
19 * Copyright (c) 2022 Qualcomm Innovation Center, Inc.
20 */
21
22 #include "config.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <locale.h>
27 #include <string.h>
28
29 #include <glib.h>
30 #include <glib/gprintf.h>
31 #include <gio/gio.h>
32 #include <glib-unix.h>
33
34 #include <libqmi-glib.h>
35
36 #if QMI_MBIM_QMUX_SUPPORTED
37 #include <libmbim-glib.h>
38 #endif
39
40 #include "qmicli.h"
41 #include "qmicli-helpers.h"
42
43 #define PROGRAM_NAME "qmicli"
44 #define PROGRAM_VERSION PACKAGE_VERSION
45
46 /* Globals */
47 static GMainLoop *loop;
48 static GCancellable *cancellable;
49 static QmiDevice *device;
50 static QmiClient *client;
51 static QmiService service;
52 static gboolean operation_status;
53 static gboolean expect_indications;
54 #if QMI_QRTR_SUPPORTED
55 static QrtrBus *qrtr_bus;
56 #endif
57
58 /* Main options */
59 static gchar *device_str;
60 static gboolean get_service_version_info_flag;
61 static gchar *device_set_instance_id_str;
62 static gboolean device_open_version_info_flag;
63 static gboolean device_open_sync_flag;
64 static gchar *device_open_net_str;
65 static gboolean device_open_proxy_flag;
66 #if QMI_MBIM_QMUX_SUPPORTED
67 static gboolean device_open_qmi_flag;
68 static gboolean device_open_mbim_flag;
69 static gboolean device_open_auto_flag;
70 #endif
71 static gchar *client_cid_str;
72 static gboolean client_no_release_cid_flag;
73 static gboolean verbose_flag;
74 static gboolean verbose_full_flag;
75 static gboolean silent_flag;
76 static gboolean version_flag;
77
78 static GOptionEntry main_entries[] = {
79 { "device", 'd', 0, G_OPTION_ARG_STRING, &device_str,
80 #if QMI_QRTR_SUPPORTED
81 "Specify device path or QRTR URI (e.g. qrtr://0)",
82 "[PATH|URI]"
83 #else
84 "Specify device path",
85 "[PATH]"
86 #endif
87 },
88 { "get-service-version-info", 0, 0, G_OPTION_ARG_NONE, &get_service_version_info_flag,
89 "Get service version info",
90 NULL
91 },
92 { "device-set-instance-id", 0, 0, G_OPTION_ARG_STRING, &device_set_instance_id_str,
93 "Set instance ID",
94 "[Instance ID]"
95 },
96 { "device-open-version-info", 0, 0, G_OPTION_ARG_NONE, &device_open_version_info_flag,
97 "Run version info check when opening device",
98 NULL
99 },
100 { "device-open-sync", 0, 0, G_OPTION_ARG_NONE, &device_open_sync_flag,
101 "Run sync operation when opening device",
102 NULL
103 },
104 { "device-open-proxy", 'p', 0, G_OPTION_ARG_NONE, &device_open_proxy_flag,
105 "Request to use the 'qmi-proxy' proxy",
106 NULL
107 },
108 #if QMI_MBIM_QMUX_SUPPORTED
109 { "device-open-qmi", 0, 0, G_OPTION_ARG_NONE, &device_open_qmi_flag,
110 "Open a cdc-wdm device explicitly in QMI mode",
111 NULL
112 },
113 { "device-open-mbim", 0, 0, G_OPTION_ARG_NONE, &device_open_mbim_flag,
114 "Open a cdc-wdm device explicitly in MBIM mode",
115 NULL
116 },
117 { "device-open-auto", 0, 0, G_OPTION_ARG_NONE, &device_open_auto_flag,
118 "Open a cdc-wdm device in either QMI or MBIM mode (default)",
119 NULL
120 },
121 #endif
122 { "device-open-net", 0, 0, G_OPTION_ARG_STRING, &device_open_net_str,
123 "Open device with specific link protocol and QoS flags",
124 "[net-802-3|net-raw-ip|net-qos-header|net-no-qos-header]"
125 },
126 { "client-cid", 0, 0, G_OPTION_ARG_STRING, &client_cid_str,
127 "Use the given CID, don't allocate a new one",
128 "[CID]"
129 },
130 { "client-no-release-cid", 0, 0, G_OPTION_ARG_NONE, &client_no_release_cid_flag,
131 "Do not release the CID when exiting",
132 NULL
133 },
134 { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose_flag,
135 "Run action with verbose logs, including the debug ones",
136 NULL
137 },
138 { "verbose-full", 0, 0, G_OPTION_ARG_NONE, &verbose_full_flag,
139 "Run action with verbose logs, including the debug ones and personal info",
140 NULL
141 },
142 { "silent", 0, 0, G_OPTION_ARG_NONE, &silent_flag,
143 "Run action with no logs; not even the error/warning ones",
144 NULL
145 },
146 { "version", 'V', 0, G_OPTION_ARG_NONE, &version_flag,
147 "Print version",
148 NULL
149 },
150 { NULL, 0, 0, 0, NULL, NULL, NULL }
151 };
152
153 static gboolean
154 signals_handler (void)
155 {
156 if (cancellable) {
157 /* Ignore consecutive requests of cancellation */
158 if (!g_cancellable_is_cancelled (cancellable)) {
159 g_printerr ("cancelling the operation...\n");
160 g_cancellable_cancel (cancellable);
161 /* Re-set the signal handler to allow main loop cancellation on
162 * second signal */
163 return G_SOURCE_CONTINUE;
164 }
165 }
166
167 if (loop && g_main_loop_is_running (loop)) {
168 g_printerr ("cancelling the main loop...\n");
169 g_idle_add ((GSourceFunc) g_main_loop_quit, loop);
170 }
171 return G_SOURCE_REMOVE;
172 }
173
174 static void
175 log_handler (const gchar *log_domain,
176 GLogLevelFlags log_level,
177 const gchar *message,
178 gpointer user_data)
179 {
180 const gchar *log_level_str;
181 time_t now;
182 gchar time_str[64];
183 struct tm *local_time;
184 gboolean err;
185
186 /* Nothing to do if we're silent */
187 if (silent_flag)
188 return;
189
190 now = time ((time_t *) NULL);
191 local_time = localtime (&now);
192 strftime (time_str, 64, "%d %b %Y, %H:%M:%S", local_time);
193 err = FALSE;
194
195 switch (log_level) {
196 case G_LOG_LEVEL_WARNING:
197 log_level_str = "-Warning **";
198 err = TRUE;
199 break;
200
201 case G_LOG_LEVEL_CRITICAL:
202 case G_LOG_LEVEL_ERROR:
203 log_level_str = "-Error **";
204 err = TRUE;
205 break;
206
207 case G_LOG_LEVEL_DEBUG:
208 log_level_str = "[Debug]";
209 break;
210
211 case G_LOG_LEVEL_MESSAGE:
212 case G_LOG_LEVEL_INFO:
213 log_level_str = "";
214 break;
215
216 case G_LOG_FLAG_FATAL:
217 case G_LOG_LEVEL_MASK:
218 case G_LOG_FLAG_RECURSION:
219 default:
220 g_assert_not_reached ();
221 }
222
223 if (!verbose_flag && !verbose_full_flag && !err)
224 return;
225
226 g_fprintf (err ? stderr : stdout,
227 "[%s] %s %s\n",
228 time_str,
229 log_level_str,
230 message);
231 }
232
233 G_GNUC_NORETURN
234 static void
235 print_version_and_exit (void)
236 {
237 g_print (PROGRAM_NAME " " PROGRAM_VERSION "\n"
238 "Copyright (C) 2012-2023 Aleksander Morgado\n"
239 "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl-2.0.html>\n"
240 "This is free software: you are free to change and redistribute it.\n"
241 "There is NO WARRANTY, to the extent permitted by law.\n"
242 "\n");
243 exit (EXIT_SUCCESS);
244 }
245
246 static gboolean
247 generic_options_enabled (void)
248 {
249 static guint n_actions = 0;
250 static gboolean checked = FALSE;
251
252 if (checked)
253 return !!n_actions;
254
255 n_actions = (!!device_set_instance_id_str +
256 get_service_version_info_flag);
257
258 if (n_actions > 1) {
259 g_printerr ("error: too many generic actions requested\n");
260 exit (EXIT_FAILURE);
261 }
262
263 checked = TRUE;
264 return !!n_actions;
265 }
266
267 /*****************************************************************************/
268 /* Report that indications are expected */
269
270 void
271 qmicli_expect_indications (void)
272 {
273 expect_indications = TRUE;
274 }
275
276 /*****************************************************************************/
277 /* Running asynchronously */
278
279 static void
280 close_ready (QmiDevice *dev,
281 GAsyncResult *res)
282 {
283 GError *error = NULL;
284
285 if (!qmi_device_close_finish (dev, res, &error)) {
286 g_printerr ("error: couldn't close: %s\n", error->message);
287 g_error_free (error);
288 } else
289 g_debug ("Closed");
290
291 g_main_loop_quit (loop);
292 }
293
294 static void
295 release_client_ready (QmiDevice *dev,
296 GAsyncResult *res)
297 {
298 GError *error = NULL;
299
300 if (!qmi_device_release_client_finish (dev, res, &error)) {
301 g_printerr ("error: couldn't release client: %s\n", error->message);
302 g_error_free (error);
303 } else
304 g_debug ("Client released");
305
306 qmi_device_close_async (dev, 10, NULL, (GAsyncReadyCallback) close_ready, NULL);
307 }
308
309 void
310 qmicli_async_operation_done (gboolean reported_operation_status,
311 gboolean skip_cid_release)
312 {
313 QmiDeviceReleaseClientFlags flags = QMI_DEVICE_RELEASE_CLIENT_FLAGS_NONE;
314
315 /* Keep the result of the operation */
316 operation_status = reported_operation_status;
317
318 /* Cleanup cancellation */
319 g_clear_object (&cancellable);
320
321 /* If no client was allocated (e.g. generic action), just quit */
322 if (!client) {
323 g_main_loop_quit (loop);
324 return;
325 }
326
327 if (skip_cid_release)
328 g_debug ("Skipped CID release");
329 else if (!client_no_release_cid_flag)
330 flags |= QMI_DEVICE_RELEASE_CLIENT_FLAGS_RELEASE_CID;
331 else
332 g_print ("[%s] Client ID not released:\n"
333 "\tService: '%s'\n"
334 "\t CID: '%u'\n",
335 qmi_device_get_path_display (device),
336 qmi_service_get_string (service),
337 qmi_client_get_cid (client));
338
339 qmi_device_release_client (device,
340 client,
341 flags,
342 10,
343 NULL,
344 (GAsyncReadyCallback)release_client_ready,
345 NULL);
346 }
347
348 static void
349 allocate_client_ready (QmiDevice *dev,
350 GAsyncResult *res)
351 {
352 GError *error = NULL;
353
354 client = qmi_device_allocate_client_finish (dev, res, &error);
355 if (!client) {
356 g_printerr ("error: couldn't create client for the '%s' service: %s\n",
357 qmi_service_get_string (service),
358 error->message);
359 exit (EXIT_FAILURE);
360 }
361
362 /* Run the service-specific action */
363 switch (service) {
364 case QMI_SERVICE_DMS:
365 #if defined HAVE_QMI_SERVICE_DMS
366 qmicli_dms_run (dev, QMI_CLIENT_DMS (client), cancellable);
367 return;
368 #else
369 break;
370 #endif
371 case QMI_SERVICE_NAS:
372 #if defined HAVE_QMI_SERVICE_NAS
373 qmicli_nas_run (dev, QMI_CLIENT_NAS (client), cancellable);
374 return;
375 #else
376 break;
377 #endif
378 case QMI_SERVICE_WDS:
379 #if defined HAVE_QMI_SERVICE_WDS
380 qmicli_wds_run (dev, QMI_CLIENT_WDS (client), cancellable);
381 return;
382 #else
383 break;
384 #endif
385 case QMI_SERVICE_PBM:
386 #if defined HAVE_QMI_SERVICE_PBM
387 qmicli_pbm_run (dev, QMI_CLIENT_PBM (client), cancellable);
388 return;
389 #else
390 break;
391 #endif
392 case QMI_SERVICE_PDC:
393 #if defined HAVE_QMI_SERVICE_PDC
394 qmicli_pdc_run (dev, QMI_CLIENT_PDC (client), cancellable);
395 return;
396 #else
397 break;
398 #endif
399 case QMI_SERVICE_UIM:
400 #if defined HAVE_QMI_SERVICE_UIM
401 qmicli_uim_run (dev, QMI_CLIENT_UIM (client), cancellable);
402 return;
403 #else
404 break;
405 #endif
406 case QMI_SERVICE_WMS:
407 #if defined HAVE_QMI_SERVICE_WMS
408 qmicli_wms_run (dev, QMI_CLIENT_WMS (client), cancellable);
409 return;
410 #else
411 break;
412 #endif
413 case QMI_SERVICE_WDA:
414 #if defined HAVE_QMI_SERVICE_WDA
415 qmicli_wda_run (dev, QMI_CLIENT_WDA (client), cancellable);
416 return;
417 #else
418 break;
419 #endif
420 case QMI_SERVICE_VOICE:
421 #if defined HAVE_QMI_SERVICE_VOICE
422 qmicli_voice_run (dev, QMI_CLIENT_VOICE (client), cancellable);
423 return;
424 #else
425 break;
426 #endif
427 case QMI_SERVICE_LOC:
428 #if defined HAVE_QMI_SERVICE_LOC
429 qmicli_loc_run (dev, QMI_CLIENT_LOC (client), cancellable);
430 return;
431 #else
432 break;
433 #endif
434 case QMI_SERVICE_QOS:
435 #if defined HAVE_QMI_SERVICE_QOS
436 qmicli_qos_run (dev, QMI_CLIENT_QOS (client), cancellable);
437 return;
438 #else
439 break;
440 #endif
441 case QMI_SERVICE_GAS:
442 #if defined HAVE_QMI_SERVICE_GAS
443 qmicli_gas_run (dev, QMI_CLIENT_GAS (client), cancellable);
444 return;
445 #else
446 break;
447 #endif
448 case QMI_SERVICE_GMS:
449 #if defined HAVE_QMI_SERVICE_GMS
450 qmicli_gms_run (dev, QMI_CLIENT_GMS (client), cancellable);
451 return;
452 #else
453 break;
454 #endif
455 case QMI_SERVICE_DSD:
456 #if defined HAVE_QMI_SERVICE_DSD
457 qmicli_dsd_run (dev, QMI_CLIENT_DSD (client), cancellable);
458 return;
459 #else
460 break;
461 #endif
462 case QMI_SERVICE_SAR:
463 #if defined HAVE_QMI_SERVICE_SAR
464 qmicli_sar_run (dev, QMI_CLIENT_SAR (client), cancellable);
465 return;
466 #else
467 break;
468 #endif
469 case QMI_SERVICE_DPM:
470 #if defined HAVE_QMI_SERVICE_DPM
471 qmicli_dpm_run (dev, QMI_CLIENT_DPM (client), cancellable);
472 return;
473 #else
474 break;
475 #endif
476 case QMI_SERVICE_FOX:
477 #if defined HAVE_QMI_SERVICE_FOX
478 qmicli_fox_run (dev, QMI_CLIENT_FOX (client), cancellable);
479 return;
480 #else
481 break;
482 #endif
483 case QMI_SERVICE_ATR:
484 #if defined HAVE_QMI_SERVICE_ATR
485 qmicli_atr_run (dev, QMI_CLIENT_ATR (client), cancellable);
486 return;
487 #else
488 break;
489 #endif
490 case QMI_SERVICE_IMSP:
491 #if defined HAVE_QMI_SERVICE_IMSP
492 qmicli_imsp_run (dev, QMI_CLIENT_IMSP (client), cancellable);
493 return;
494 #else
495 break;
496 #endif
497 case QMI_SERVICE_IMSA:
498 #if defined HAVE_QMI_SERVICE_IMSA
499 qmicli_imsa_run (dev, QMI_CLIENT_IMSA (client), cancellable);
500 return;
501 #else
502 break;
503 #endif
504 case QMI_SERVICE_IMS:
505 #if defined HAVE_QMI_SERVICE_IMS
506 qmicli_ims_run (dev, QMI_CLIENT_IMS (client), cancellable);
507 return;
508 #else
509 break;
510 #endif
511 case QMI_SERVICE_UNKNOWN:
512 case QMI_SERVICE_CTL:
513 case QMI_SERVICE_AUTH:
514 case QMI_SERVICE_AT:
515 case QMI_SERVICE_CAT2:
516 case QMI_SERVICE_QCHAT:
517 case QMI_SERVICE_RMTFS:
518 case QMI_SERVICE_TEST:
519 case QMI_SERVICE_ADC:
520 case QMI_SERVICE_CSD:
521 case QMI_SERVICE_MFS:
522 case QMI_SERVICE_TIME:
523 case QMI_SERVICE_TS:
524 case QMI_SERVICE_TMD:
525 case QMI_SERVICE_SAP:
526 case QMI_SERVICE_TSYNC:
527 case QMI_SERVICE_RFSA:
528 case QMI_SERVICE_CSVT:
529 case QMI_SERVICE_QCMAP:
530 case QMI_SERVICE_IMSVT:
531 case QMI_SERVICE_COEX:
532 case QMI_SERVICE_STX:
533 case QMI_SERVICE_BIT:
534 case QMI_SERVICE_IMSRTP:
535 case QMI_SERVICE_RFRPE:
536 case QMI_SERVICE_SSCTL:
537 case QMI_SERVICE_CAT:
538 case QMI_SERVICE_RMS:
539 case QMI_SERVICE_FOTA:
540 case QMI_SERVICE_PDS:
541 case QMI_SERVICE_OMA:
542 case QMI_SERVICE_SSC:
543 case QMI_SERVICE_IMSDCM:
544 default:
545 break;
546 }
547 g_assert_not_reached ();
548 }
549
550 static void
551 device_allocate_client (QmiDevice *dev)
552 {
553 guint8 cid = QMI_CID_NONE;
554
555 if (client_cid_str) {
556 guint32 cid32;
557
558 cid32 = atoi (client_cid_str);
559 if (!cid32 || cid32 > G_MAXUINT8) {
560 g_printerr ("error: invalid CID given '%s'\n",
561 client_cid_str);
562 exit (EXIT_FAILURE);
563 }
564
565 cid = (guint8)cid32;
566 g_debug ("Reusing CID '%u'", cid);
567 }
568
569 /* As soon as we get the QmiDevice, create a client for the requested
570 * service */
571 qmi_device_allocate_client (dev,
572 service,
573 cid,
574 10,
575 cancellable,
576 (GAsyncReadyCallback)allocate_client_ready,
577 NULL);
578 }
579
580 static void
581 set_instance_id_ready (QmiDevice *dev,
582 GAsyncResult *res)
583 {
584 GError *error = NULL;
585 guint16 link_id;
586
587 if (!qmi_device_set_instance_id_finish (dev, res, &link_id, &error)) {
588 g_printerr ("error: couldn't set instance ID: %s\n",
589 error->message);
590 exit (EXIT_FAILURE);
591 }
592
593 g_print ("[%s] Instance ID set:\n"
594 "\tLink ID: '%" G_GUINT16_FORMAT "'\n",
595 qmi_device_get_path_display (dev),
596 link_id);
597
598 /* We're done now */
599 qmicli_async_operation_done (TRUE, FALSE);
600 }
601
602 static void
603 device_set_instance_id (QmiDevice *dev)
604 {
605 gint instance_id;
606
607 if (g_str_equal (device_set_instance_id_str, "0"))
608 instance_id = 0;
609 else {
610 instance_id = atoi (device_set_instance_id_str);
611 if (instance_id == 0) {
612 g_printerr ("error: invalid instance ID given: '%s'\n", device_set_instance_id_str);
613 exit (EXIT_FAILURE);
614 } else if (instance_id < 0 || instance_id > G_MAXUINT8) {
615 g_printerr ("error: given instance ID is out of range [0,%u]: '%s'\n",
616 G_MAXUINT8,
617 device_set_instance_id_str);
618 exit (EXIT_FAILURE);
619 }
620 }
621
622 g_debug ("Setting instance ID '%d'...", instance_id);
623 qmi_device_set_instance_id (dev,
624 (guint8)instance_id,
625 10,
626 cancellable,
627 (GAsyncReadyCallback)set_instance_id_ready,
628 NULL);
629 }
630
631 static void
632 get_service_version_info_ready (QmiDevice *dev,
633 GAsyncResult *res)
634 {
635 GError *error = NULL;
636 GArray *services;
637 guint i;
638
639 services = qmi_device_get_service_version_info_finish (dev, res, &error);
640 if (!services) {
641 g_printerr ("error: couldn't get service version info: %s\n",
642 error->message);
643 exit (EXIT_FAILURE);
644 }
645
646 g_print ("[%s] Supported versions:\n",
647 qmi_device_get_path_display (dev));
648 for (i = 0; i < services->len; i++) {
649 QmiDeviceServiceVersionInfo *info;
650 const gchar *service_str;
651
652 info = &g_array_index (services, QmiDeviceServiceVersionInfo, i);
653 service_str = qmi_service_get_string (info->service);
654 if (service_str)
655 g_print ("\t%s (%u.%u)\n",
656 service_str,
657 info->major_version,
658 info->minor_version);
659 else
660 g_print ("\tunknown [0x%02x] (%u.%u)\n",
661 info->service,
662 info->major_version,
663 info->minor_version);
664 }
665 g_array_unref (services);
666
667 /* We're done now */
668 qmicli_async_operation_done (TRUE, FALSE);
669 }
670
671 static void
672 device_get_service_version_info (QmiDevice *dev)
673 {
674 g_debug ("Getting service version info...");
675 qmi_device_get_service_version_info (dev,
676 10,
677 cancellable,
678 (GAsyncReadyCallback)get_service_version_info_ready,
679 NULL);
680 }
681
682 static void
683 device_open_ready (QmiDevice *dev,
684 GAsyncResult *res)
685 {
686 GError *error = NULL;
687
688 if (!qmi_device_open_finish (dev, res, &error)) {
689 g_printerr ("error: couldn't open the QmiDevice: %s\n",
690 error->message);
691 exit (EXIT_FAILURE);
692 }
693
694 g_debug ("QMI Device at '%s' ready",
695 qmi_device_get_path_display (dev));
696
697 if (device_set_instance_id_str)
698 device_set_instance_id (dev);
699 else if (get_service_version_info_flag)
700 device_get_service_version_info (dev);
701 else if (qmicli_link_management_options_enabled ())
702 qmicli_link_management_run (dev, cancellable);
703 else if (qmicli_qmiwwan_options_enabled ())
704 qmicli_qmiwwan_run (dev, cancellable);
705 else
706 device_allocate_client (dev);
707 }
708
709 static void
710 device_new_ready (GObject *unused,
711 GAsyncResult *res)
712 {
713 QmiDeviceOpenFlags open_flags = QMI_DEVICE_OPEN_FLAGS_NONE;
714 GError *error = NULL;
715
716 device = qmi_device_new_finish (res, &error);
717 if (!device) {
718 g_printerr ("error: couldn't create QmiDevice: %s\n",
719 error->message);
720 exit (EXIT_FAILURE);
721 }
722
723 #if QMI_MBIM_QMUX_SUPPORTED
724 if (device_open_mbim_flag + device_open_qmi_flag + device_open_auto_flag > 1) {
725 g_printerr ("error: cannot specify multiple mode flags to open device\n");
726 exit (EXIT_FAILURE);
727 }
728 #endif
729
730 /* Setup device open flags */
731 if (device_open_version_info_flag)
732 open_flags |= QMI_DEVICE_OPEN_FLAGS_VERSION_INFO;
733 if (device_open_sync_flag)
734 open_flags |= QMI_DEVICE_OPEN_FLAGS_SYNC;
735 if (device_open_proxy_flag)
736 open_flags |= QMI_DEVICE_OPEN_FLAGS_PROXY;
737 #if QMI_MBIM_QMUX_SUPPORTED
738 if (device_open_mbim_flag)
739 open_flags |= QMI_DEVICE_OPEN_FLAGS_MBIM;
740 if (device_open_auto_flag || (!device_open_qmi_flag && !device_open_mbim_flag))
741 open_flags |= QMI_DEVICE_OPEN_FLAGS_AUTO;
742 #endif
743 if (expect_indications)
744 open_flags |= QMI_DEVICE_OPEN_FLAGS_EXPECT_INDICATIONS;
745 if (device_open_net_str) {
746 if (!qmicli_read_device_open_flags_from_string (device_open_net_str, &open_flags) ||
747 !qmicli_validate_device_open_flags (open_flags))
748 exit (EXIT_FAILURE);
749 }
750
751 /* Open the device */
752 qmi_device_open (device,
753 open_flags,
754 15,
755 cancellable,
756 (GAsyncReadyCallback)device_open_ready,
757 NULL);
758 }
759
760 #if QMI_QRTR_SUPPORTED
761
762 static void
763 bus_new_ready (GObject *source,
764 GAsyncResult *res,
765 gpointer user_data)
766 {
767 g_autoptr(GError) error = NULL;
768 guint node_id;
769 QrtrNode *node;
770
771 node_id = GPOINTER_TO_UINT (user_data);
772
773 qrtr_bus = qrtr_bus_new_finish (res, &error);
774 if (!qrtr_bus) {
775 g_printerr ("error: couldn't access QRTR bus: %s\n", error->message);
776 exit (EXIT_FAILURE);
777 }
778
779 node = qrtr_bus_peek_node (qrtr_bus, node_id);
780 if (!node) {
781 g_printerr ("error: node with id %u not found in QRTR bus\n", node_id);
782 exit (EXIT_FAILURE);
783 }
784
785 qmi_device_new_from_node (node,
786 cancellable,
787 (GAsyncReadyCallback)device_new_ready,
788 NULL);
789 }
790
791 #endif
792
793 static gboolean
794 make_device (GFile *file)
795 {
796 g_autofree gchar *id = NULL;
797
798 id = g_file_get_path (file);
799 if (id) {
800 /* Describes a local device file. */
801 qmi_device_new (file,
802 cancellable,
803 (GAsyncReadyCallback)device_new_ready,
804 NULL);
805 return TRUE;
806 }
807
808 #if QMI_QRTR_SUPPORTED
809 {
810 guint32 node_id;
811
812 id = g_file_get_uri (file);
813 if (qrtr_get_node_for_uri (id, &node_id)) {
814 qrtr_bus_new (1000, /* ms */
815 cancellable,
816 (GAsyncReadyCallback)bus_new_ready,
817 GUINT_TO_POINTER (node_id));
818 return TRUE;
819 }
820
821 g_printerr ("error: URI is neither a local file path nor a QRTR node: %s\n", id);
822 return FALSE;
823 }
824 #else
825 g_printerr ("error: URI is not a local file path: %s\n", id);
826 return FALSE;
827 #endif
828 }
829
830 /*****************************************************************************/
831
832 static void
833 parse_actions (void)
834 {
835 guint actions_enabled = 0;
836
837 if (generic_options_enabled ()) {
838 service = QMI_SERVICE_CTL;
839 actions_enabled++;
840 }
841
842 if (qmicli_link_management_options_enabled ()) {
843 service = QMI_SERVICE_UNKNOWN;
844 actions_enabled++;
845 }
846
847 if (qmicli_qmiwwan_options_enabled ()) {
848 service = QMI_SERVICE_UNKNOWN;
849 actions_enabled++;
850 }
851
852 #if defined HAVE_QMI_SERVICE_DMS
853 if (qmicli_dms_options_enabled ()) {
854 service = QMI_SERVICE_DMS;
855 actions_enabled++;
856 }
857 #endif
858
859 #if defined HAVE_QMI_SERVICE_NAS
860 if (qmicli_nas_options_enabled ()) {
861 service = QMI_SERVICE_NAS;
862 actions_enabled++;
863 }
864 #endif
865
866 #if defined HAVE_QMI_SERVICE_WDS
867 if (qmicli_wds_options_enabled ()) {
868 service = QMI_SERVICE_WDS;
869 actions_enabled++;
870 }
871 #endif
872
873 #if defined HAVE_QMI_SERVICE_PBM
874 if (qmicli_pbm_options_enabled ()) {
875 service = QMI_SERVICE_PBM;
876 actions_enabled++;
877 }
878 #endif
879
880 #if defined HAVE_QMI_SERVICE_PDC
881 if (qmicli_pdc_options_enabled ()) {
882 service = QMI_SERVICE_PDC;
883 actions_enabled++;
884 }
885 #endif
886
887 #if defined HAVE_QMI_SERVICE_UIM
888 if (qmicli_uim_options_enabled ()) {
889 service = QMI_SERVICE_UIM;
890 actions_enabled++;
891 }
892 #endif
893
894 #if defined HAVE_QMI_SERVICE_SAR
895 if (qmicli_sar_options_enabled ()) {
896 service = QMI_SERVICE_SAR;
897 actions_enabled++;
898 }
899 #endif
900
901 #if defined HAVE_QMI_SERVICE_WMS
902 if (qmicli_wms_options_enabled ()) {
903 service = QMI_SERVICE_WMS;
904 actions_enabled++;
905 }
906 #endif
907
908 #if defined HAVE_QMI_SERVICE_WDA
909 if (qmicli_wda_options_enabled ()) {
910 service = QMI_SERVICE_WDA;
911 actions_enabled++;
912 }
913 #endif
914
915 #if defined HAVE_QMI_SERVICE_VOICE
916 if (qmicli_voice_options_enabled ()) {
917 service = QMI_SERVICE_VOICE;
918 actions_enabled++;
919 }
920 #endif
921
922 #if defined HAVE_QMI_SERVICE_LOC
923 if (qmicli_loc_options_enabled ()) {
924 service = QMI_SERVICE_LOC;
925 actions_enabled++;
926 }
927 #endif
928
929 #if defined HAVE_QMI_SERVICE_QOS
930 if (qmicli_qos_options_enabled ()) {
931 service = QMI_SERVICE_QOS;
932 actions_enabled++;
933 }
934 #endif
935
936 #if defined HAVE_QMI_SERVICE_GAS
937 if (qmicli_gas_options_enabled ()) {
938 service = QMI_SERVICE_GAS;
939 actions_enabled++;
940 }
941 #endif
942
943 #if defined HAVE_QMI_SERVICE_GMS
944 if (qmicli_gms_options_enabled ()) {
945 service = QMI_SERVICE_GMS;
946 actions_enabled++;
947 }
948 #endif
949
950 #if defined HAVE_QMI_SERVICE_DSD
951 if (qmicli_dsd_options_enabled ()) {
952 service = QMI_SERVICE_DSD;
953 actions_enabled++;
954 }
955 #endif
956
957 #if defined HAVE_QMI_SERVICE_DPM
958 if (qmicli_dpm_options_enabled ()) {
959 service = QMI_SERVICE_DPM;
960 actions_enabled++;
961 }
962 #endif
963
964 #if defined HAVE_QMI_SERVICE_FOX
965 if (qmicli_fox_options_enabled ()) {
966 service = QMI_SERVICE_FOX;
967 actions_enabled++;
968 }
969 #endif
970
971 #if defined HAVE_QMI_SERVICE_ATR
972 if (qmicli_atr_options_enabled ()) {
973 service = QMI_SERVICE_ATR;
974 actions_enabled++;
975 }
976 #endif
977
978 #if defined HAVE_QMI_SERVICE_IMSP
979 if (qmicli_imsp_options_enabled ()) {
980 service = QMI_SERVICE_IMSP;
981 actions_enabled++;
982 }
983 #endif
984
985 #if defined HAVE_QMI_SERVICE_IMSA
986 if (qmicli_imsa_options_enabled ()) {
987 service = QMI_SERVICE_IMSA;
988 actions_enabled++;
989 }
990 #endif
991
992 #if defined HAVE_QMI_SERVICE_IMS
993 if (qmicli_ims_options_enabled ()) {
994 service = QMI_SERVICE_IMS;
995 actions_enabled++;
996 }
997 #endif
998
999 /* Cannot mix actions from different services */
1000 if (actions_enabled > 1) {
1001 g_printerr ("error: cannot execute multiple actions of different services\n");
1002 exit (EXIT_FAILURE);
1003 }
1004
1005 /* No options? */
1006 if (actions_enabled == 0) {
1007 g_printerr ("error: no actions specified\n");
1008 exit (EXIT_FAILURE);
1009 }
1010
1011 /* Go on! */
1012 }
1013
1014 int main (int argc, char **argv)
1015 {
1016 GError *error = NULL;
1017 GFile *file;
1018 GOptionContext *context;
1019
1020 setlocale (LC_ALL, "");
1021
1022 /* Setup option context, process it and destroy it */
1023 context = g_option_context_new ("- Control QMI devices");
1024 #if defined HAVE_QMI_SERVICE_DMS
1025 g_option_context_add_group (context, qmicli_dms_get_option_group ());
1026 #endif
1027 #if defined HAVE_QMI_SERVICE_NAS
1028 g_option_context_add_group (context, qmicli_nas_get_option_group ());
1029 #endif
1030 #if defined HAVE_QMI_SERVICE_WDS
1031 g_option_context_add_group (context, qmicli_wds_get_option_group ());
1032 #endif
1033 #if defined HAVE_QMI_SERVICE_PBM
1034 g_option_context_add_group (context, qmicli_pbm_get_option_group ());
1035 #endif
1036 #if defined HAVE_QMI_SERVICE_PDC
1037 g_option_context_add_group (context, qmicli_pdc_get_option_group ());
1038 #endif
1039 #if defined HAVE_QMI_SERVICE_UIM
1040 g_option_context_add_group (context, qmicli_uim_get_option_group ());
1041 #endif
1042 #if defined HAVE_QMI_SERVICE_SAR
1043 g_option_context_add_group (context, qmicli_sar_get_option_group ());
1044 #endif
1045 #if defined HAVE_QMI_SERVICE_WMS
1046 g_option_context_add_group (context, qmicli_wms_get_option_group ());
1047 #endif
1048 #if defined HAVE_QMI_SERVICE_WDA
1049 g_option_context_add_group (context, qmicli_wda_get_option_group ());
1050 #endif
1051 #if defined HAVE_QMI_SERVICE_VOICE
1052 g_option_context_add_group (context, qmicli_voice_get_option_group ());
1053 #endif
1054 #if defined HAVE_QMI_SERVICE_LOC
1055 g_option_context_add_group (context, qmicli_loc_get_option_group ());
1056 #endif
1057 #if defined HAVE_QMI_SERVICE_QOS
1058 g_option_context_add_group (context, qmicli_qos_get_option_group ());
1059 #endif
1060 #if defined HAVE_QMI_SERVICE_GAS
1061 g_option_context_add_group (context, qmicli_gas_get_option_group ());
1062 #endif
1063 #if defined HAVE_QMI_SERVICE_GMS
1064 g_option_context_add_group (context, qmicli_gms_get_option_group ());
1065 #endif
1066 #if defined HAVE_QMI_SERVICE_DSD
1067 g_option_context_add_group (context, qmicli_dsd_get_option_group ());
1068 #endif
1069 #if defined HAVE_QMI_SERVICE_DPM
1070 g_option_context_add_group (context, qmicli_dpm_get_option_group ());
1071 #endif
1072 #if defined HAVE_QMI_SERVICE_FOX
1073 g_option_context_add_group (context, qmicli_fox_get_option_group ());
1074 #endif
1075 #if defined HAVE_QMI_SERVICE_ATR
1076 g_option_context_add_group (context, qmicli_atr_get_option_group ());
1077 #endif
1078 #if defined HAVE_QMI_SERVICE_IMSP
1079 g_option_context_add_group (context, qmicli_imsp_get_option_group ());
1080 #endif
1081 #if defined HAVE_QMI_SERVICE_IMSA
1082 g_option_context_add_group (context, qmicli_imsa_get_option_group ());
1083 #endif
1084 #if defined HAVE_QMI_SERVICE_IMS
1085 g_option_context_add_group (context, qmicli_ims_get_option_group ());
1086 #endif
1087 g_option_context_add_group (context, qmicli_link_management_get_option_group ());
1088 g_option_context_add_group (context, qmicli_qmiwwan_get_option_group ());
1089 g_option_context_add_main_entries (context, main_entries, NULL);
1090 if (!g_option_context_parse (context, &argc, &argv, &error)) {
1091 g_printerr ("error: %s\n",
1092 error->message);
1093 exit (EXIT_FAILURE);
1094 }
1095 g_option_context_free (context);
1096
1097 if (version_flag)
1098 print_version_and_exit ();
1099
1100 g_log_set_handler (NULL, G_LOG_LEVEL_MASK, log_handler, NULL);
1101 g_log_set_handler ("Qmi", G_LOG_LEVEL_MASK, log_handler, NULL);
1102
1103 if (verbose_flag && verbose_full_flag) {
1104 g_printerr ("error: cannot specify --verbose and --verbose-full at the same time\n");
1105 exit (EXIT_FAILURE);
1106 } else if (verbose_flag) {
1107 qmi_utils_set_traces_enabled (TRUE);
1108 qmi_utils_set_show_personal_info (FALSE);
1109 } else if (verbose_full_flag) {
1110 qmi_utils_set_traces_enabled (TRUE);
1111 qmi_utils_set_show_personal_info (TRUE);
1112 }
1113
1114 #if QMI_MBIM_QMUX_SUPPORTED
1115 /* libmbim logging */
1116 g_log_set_handler ("Mbim", G_LOG_LEVEL_MASK, log_handler, NULL);
1117 if (verbose_flag) {
1118 mbim_utils_set_traces_enabled (TRUE);
1119 #if MBIM_CHECK_VERSION(1,27,6)
1120 mbim_utils_set_show_personal_info (FALSE);
1121 #endif
1122 } else if (verbose_full_flag) {
1123 mbim_utils_set_traces_enabled (TRUE);
1124 #if MBIM_CHECK_VERSION(1,27,6)
1125 mbim_utils_set_show_personal_info (TRUE);
1126 #endif
1127 }
1128 #endif
1129
1130 #if QMI_QRTR_SUPPORTED
1131 /* libqrtr-glib logging */
1132 g_log_set_handler ("Qrtr", G_LOG_LEVEL_MASK, log_handler, NULL);
1133 #endif
1134
1135 /* No device path given? */
1136 if (!device_str) {
1137 g_printerr ("error: no device path specified\n");
1138 exit (EXIT_FAILURE);
1139 }
1140
1141 /* Build new GFile from the commandline arg */
1142 file = g_file_new_for_commandline_arg (device_str);
1143
1144 parse_actions ();
1145
1146 /* Create requirements for async options */
1147 cancellable = g_cancellable_new ();
1148 loop = g_main_loop_new (NULL, FALSE);
1149
1150 /* Setup signals */
1151 g_unix_signal_add (SIGINT, (GSourceFunc) signals_handler, NULL);
1152 g_unix_signal_add (SIGHUP, (GSourceFunc) signals_handler, NULL);
1153 g_unix_signal_add (SIGTERM, (GSourceFunc) signals_handler, NULL);
1154
1155 /* Launch QmiDevice creation */
1156 if (!make_device (file))
1157 return EXIT_FAILURE;
1158
1159 g_main_loop_run (loop);
1160
1161 if (cancellable)
1162 g_object_unref (cancellable);
1163 if (client)
1164 g_object_unref (client);
1165 if (device)
1166 g_object_unref (device);
1167 g_main_loop_unref (loop);
1168 g_object_unref (file);
1169
1170 return (operation_status ? EXIT_SUCCESS : EXIT_FAILURE);
1171 }