Debian Codegraph @

src/mbimcli/mbimcli-basic-connect.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 - 2014 Aleksander Morgado <[email protected]>
7 */
8
9 #include "config.h"
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <locale.h>
14 #include <string.h>
15 #include <errno.h>
16
17 #include <glib.h>
18 #include <gio/gio.h>
19
20 #include <libmbim-glib.h>
21
22 #include "mbim-common.h"
23 #include "mbimcli.h"
24 #include "mbimcli-helpers.h"
25
26 /* Context */
27 typedef struct {
28 MbimDevice *device;
29 GCancellable *cancellable;
30 } Context;
31 static Context *ctx;
32
33 /* Options */
34 static gboolean query_device_caps_flag;
35 static gboolean query_subscriber_ready_status_flag;
36 static gboolean query_radio_state_flag;
37 static gchar *set_radio_state_str;
38 static gboolean query_device_services_flag;
39 static gboolean query_pin_flag;
40 static gchar *set_pin_enter_str;
41 static gchar *set_pin_change_str;
42 static gchar *set_pin_enable_str;
43 static gchar *set_pin_disable_str;
44 static gchar *set_pin_enter_puk_str;
45 static gboolean query_pin_list_flag;
46 static gboolean query_home_provider_flag;
47 static gboolean query_preferred_providers_flag;
48 static gboolean query_visible_providers_flag;
49 static gboolean query_register_state_flag;
50 static gchar *set_register_state_str;
51 static gboolean query_signal_state_flag;
52 static gboolean query_packet_service_flag;
53 static gboolean set_packet_service_attach_flag;
54 static gboolean set_packet_service_detach_flag;
55 static gchar *query_connect_str;
56 static gchar *set_connect_activate_str;
57 static gchar *query_ip_configuration_str;
58 static gchar *set_connect_deactivate_str;
59 static gboolean query_packet_statistics_flag;
60 static gchar *query_ip_packet_filters_str;
61 static gchar *set_ip_packet_filters_str;
62 static gboolean query_provisioned_contexts_flag;
63 static gchar *set_provisioned_contexts_str;
64 static gchar *set_signal_state_str;
65 static gchar *set_network_idle_hint_str;
66 static gboolean query_network_idle_hint_flag;
67 static gchar *set_emergency_mode_str;
68 static gboolean query_emergency_mode_flag;
69 static gchar *set_service_activation_str;
70
71 /* deprecated */
72 static gboolean register_automatic_flag;
73 static gboolean query_registration_state_flag;
74
75 static gboolean query_connection_state_arg_parse (const char *option_name,
76 const char *value,
77 gpointer user_data,
78 GError **error);
79
80 static gboolean query_ip_configuration_arg_parse (const char *option_name,
81 const char *value,
82 gpointer user_data,
83 GError **error);
84
85 static gboolean disconnect_arg_parse (const char *option_name,
86 const char *value,
87 gpointer user_data,
88 GError **error);
89
90 static gboolean query_ip_packet_filters_arg_parse (const char *option_name,
91 const char *value,
92 gpointer user_data,
93 GError **error);
94
95 static GOptionEntry entries[] = {
96 { "query-device-caps", 0, 0, G_OPTION_ARG_NONE, &query_device_caps_flag,
97 "Query device capabilities",
98 NULL
99 },
100 { "query-subscriber-ready-status", 0, 0, G_OPTION_ARG_NONE, &query_subscriber_ready_status_flag,
101 "Query subscriber ready status",
102 NULL
103 },
104 { "query-radio-state", 0, 0, G_OPTION_ARG_NONE, &query_radio_state_flag,
105 "Query radio state",
106 NULL
107 },
108 { "set-radio-state", 0, 0, G_OPTION_ARG_STRING, &set_radio_state_str,
109 "Set radio state",
110 "[(on|off)]"
111 },
112 { "query-device-services", 0, 0, G_OPTION_ARG_NONE, &query_device_services_flag,
113 "Query device services",
114 NULL
115 },
116 { "query-pin-state", 0, 0, G_OPTION_ARG_NONE, &query_pin_flag,
117 "Query PIN state",
118 NULL
119 },
120 { "enter-pin", 0, 0, G_OPTION_ARG_STRING, &set_pin_enter_str,
121 "Enter PIN (PIN type is optional, defaults to PIN1, allowed options: "
122 "(pin1,network-pin,network-subset-pin,service-provider-pin,corporate-pin)",
123 "[(PIN type),(current PIN)]"
124 },
125 { "change-pin", 0, 0, G_OPTION_ARG_STRING, &set_pin_change_str,
126 "Change PIN",
127 "[(current PIN),(new PIN)]"
128 },
129 { "enable-pin", 0, 0, G_OPTION_ARG_STRING, &set_pin_enable_str,
130 "Enable PIN",
131 "[(current PIN)]"
132 },
133 { "disable-pin", 0, 0, G_OPTION_ARG_STRING, &set_pin_disable_str,
134 "Disable PIN (PIN type is optional, see enter-pin for details)",
135 "[(PIN type),(current PIN)]"
136 },
137 { "enter-puk", 0, 0, G_OPTION_ARG_STRING, &set_pin_enter_puk_str,
138 "Enter PUK (PUK type is optional, defaults to PUK1, allowed options: "
139 "(puk1,network-puk,network-subset-puk,service-provider-puk,corporate-puk)",
140 "[(PUK type),(PUK),(new PIN)]"
141 },
142 { "query-pin-list", 0, 0, G_OPTION_ARG_NONE, &query_pin_list_flag,
143 "Query PIN list",
144 NULL
145 },
146 { "query-home-provider", 0, 0, G_OPTION_ARG_NONE, &query_home_provider_flag,
147 "Query home provider",
148 NULL
149 },
150 { "query-preferred-providers", 0, 0, G_OPTION_ARG_NONE, &query_preferred_providers_flag,
151 "Query preferred providers",
152 NULL
153 },
154 { "query-visible-providers", 0, 0, G_OPTION_ARG_NONE, &query_visible_providers_flag,
155 "Query visible providers",
156 NULL
157 },
158 { "query-register-state", 0, 0, G_OPTION_ARG_NONE, &query_register_state_flag,
159 "Query register state",
160 NULL
161 },
162 { "set-register-state", 0, 0, G_OPTION_ARG_STRING, &set_register_state_str,
163 "Set basic register settings (allowed keys: action, provider-id, data-class)",
164 "[\"key=value,...\"]"
165 },
166 { "query-signal-state", 0, 0, G_OPTION_ARG_NONE, &query_signal_state_flag,
167 "Query signal state",
168 NULL
169 },
170 { "query-packet-service-state", 0, 0, G_OPTION_ARG_NONE, &query_packet_service_flag,
171 "Query packet service state",
172 NULL
173 },
174 { "attach-packet-service", 0, 0, G_OPTION_ARG_NONE, &set_packet_service_attach_flag,
175 "Attach to the packet service",
176 NULL
177 },
178 { "detach-packet-service", 0, 0, G_OPTION_ARG_NONE, &set_packet_service_detach_flag,
179 "Detach from the packet service",
180 NULL
181 },
182 { "query-connection-state", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, G_CALLBACK (query_connection_state_arg_parse),
183 "Query connection state (SessionID is optional, defaults to 0)",
184 "[SessionID]"
185 },
186 { "connect", 0, 0, G_OPTION_ARG_STRING, &set_connect_activate_str,
187 "Connect (allowed keys: session-id, access-string, ip-type, auth, username, password, compression, context-type)",
188 "[\"key=value,...\"]"
189 },
190 { "query-ip-configuration", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, G_CALLBACK (query_ip_configuration_arg_parse),
191 "Query IP configuration (SessionID is optional, defaults to 0)",
192 "[SessionID]"
193 },
194 { "disconnect", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, G_CALLBACK (disconnect_arg_parse),
195 "Disconnect (SessionID is optional, defaults to 0)",
196 "[SessionID]"
197 },
198 { "query-packet-statistics", 0, 0, G_OPTION_ARG_NONE, &query_packet_statistics_flag,
199 "Query packet statistics",
200 NULL
201 },
202 { "query-ip-packet-filters", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, G_CALLBACK (query_ip_packet_filters_arg_parse),
203 "Query IP packet filters (SessionID is optional, defaults to 0)",
204 "[SessionID]"
205 },
206 { "set-ip-packet-filters", 0, 0, G_OPTION_ARG_STRING, &set_ip_packet_filters_str,
207 "Set IP packet filters (allowed keys: session-id, packet-filter, packet-mask, filter-id)",
208 "[\"key=value,...\"]"
209 },
210 { "query-provisioned-contexts", 0, 0, G_OPTION_ARG_NONE, &query_provisioned_contexts_flag,
211 "Query provisioned contexts",
212 NULL
213 },
214 { "set-provisioned-contexts", 0, 0, G_OPTION_ARG_STRING, &set_provisioned_contexts_str,
215 "Set provisioned contexts (allowed keys: context-id, context-type, auth, compression, username, password, access-string, provider-id)",
216 "[\"key=value,...\"]"
217 },
218 { "set-signal-state", 0, 0, G_OPTION_ARG_STRING, &set_signal_state_str,
219 "Set signal state (allowed keys: signal-strength-interval, rssi-threshold, error-rate-threshold)",
220 "[\"key=value,...\"]"
221 },
222 { "set-network-idle-hint", 0, 0, G_OPTION_ARG_STRING, &set_network_idle_hint_str,
223 "Set network idle hint",
224 "[(enabled|disabled)]"
225 },
226 { "query-network-idle-hint", 0, 0, G_OPTION_ARG_NONE, &query_network_idle_hint_flag,
227 "Query network idle hint",
228 NULL
229 },
230 { "set-emergency-mode", 0, 0, G_OPTION_ARG_STRING, &set_emergency_mode_str,
231 "Set emergency mode",
232 "[(on|off)]"
233 },
234 { "query-emergency-mode", 0, 0, G_OPTION_ARG_NONE, &query_emergency_mode_flag,
235 "Query emergency mode",
236 NULL
237 },
238 { "set-service-activation", 0, 0, G_OPTION_ARG_STRING, &set_service_activation_str,
239 "Set service activation",
240 "[Data]"
241 },
242 /* Deprecated actions. They are kept for compatibility purposes, but should
243 * otherwise not be used in newly written code. */
244 { "query-registration-state", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &query_registration_state_flag,
245 NULL,
246 NULL
247 },
248 { "register-automatic", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &register_automatic_flag,
249 NULL,
250 NULL
251 },
252 { NULL, 0, 0, 0, NULL, NULL, NULL }
253 };
254
255 GOptionGroup *
256 mbimcli_basic_connect_get_option_group (void)
257 {
258 GOptionGroup *group;
259
260 group = g_option_group_new ("basic-connect",
261 "Basic Connect options:",
262 "Show Basic Connect Service options",
263 NULL,
264 NULL);
265 g_option_group_add_entries (group, entries);
266
267 return group;
268 }
269
270 static gboolean
271 query_connection_state_arg_parse (const char *option_name,
272 const char *value,
273 gpointer user_data,
274 GError **error)
275 {
276 query_connect_str = g_strdup (value ? value : "0");
277 return TRUE;
278 }
279
280 static gboolean
281 query_ip_configuration_arg_parse (const char *option_name,
282 const char *value,
283 gpointer user_data,
284 GError **error)
285 {
286 query_ip_configuration_str = g_strdup (value ? value : "0");
287 return TRUE;
288 }
289
290 static gboolean
291 disconnect_arg_parse (const char *option_name,
292 const char *value,
293 gpointer user_data,
294 GError **error)
295 {
296 set_connect_deactivate_str = g_strdup (value ? value : "0");
297 return TRUE;
298 }
299
300 static gboolean
301 query_ip_packet_filters_arg_parse (const char *option_name,
302 const char *value,
303 gpointer user_data,
304 GError **error)
305 {
306 query_ip_packet_filters_str = g_strdup (value ? value : "0");
307 return TRUE;
308 }
309
310 gboolean
311 mbimcli_basic_connect_options_enabled (void)
312 {
313 static guint n_actions = 0;
314 static gboolean checked = FALSE;
315
316 if (checked)
317 return !!n_actions;
318
319 n_actions = (query_device_caps_flag +
320 query_subscriber_ready_status_flag +
321 query_radio_state_flag +
322 !!set_radio_state_str +
323 query_device_services_flag +
324 query_pin_flag +
325 !!set_pin_enter_str +
326 !!set_pin_change_str +
327 !!set_pin_enable_str +
328 !!set_pin_disable_str +
329 !!set_pin_enter_puk_str +
330 query_pin_list_flag +
331 query_register_state_flag +
332 query_home_provider_flag +
333 query_preferred_providers_flag +
334 query_visible_providers_flag +
335 !!set_register_state_str +
336 query_signal_state_flag +
337 query_packet_service_flag +
338 set_packet_service_attach_flag +
339 set_packet_service_detach_flag +
340 !!query_connect_str +
341 !!set_connect_activate_str +
342 !!query_ip_configuration_str +
343 !!set_connect_deactivate_str +
344 query_packet_statistics_flag +
345 !!query_ip_packet_filters_str +
346 !!set_ip_packet_filters_str +
347 query_provisioned_contexts_flag +
348 !!set_provisioned_contexts_str +
349 !!set_signal_state_str +
350 !!set_network_idle_hint_str +
351 query_network_idle_hint_flag +
352 !!set_emergency_mode_str +
353 query_emergency_mode_flag +
354 !!set_service_activation_str +
355 query_registration_state_flag +
356 register_automatic_flag);
357
358 if (n_actions > 1) {
359 g_printerr ("error: too many Basic Connect actions requested\n");
360 exit (EXIT_FAILURE);
361 }
362
363 checked = TRUE;
364 return !!n_actions;
365 }
366
367 static void
368 context_free (Context *context)
369 {
370 if (!context)
371 return;
372
373 if (context->cancellable)
374 g_object_unref (context->cancellable);
375 if (context->device)
376 g_object_unref (context->device);
377 g_slice_free (Context, context);
378 }
379
380 static void
381 shutdown (gboolean operation_status)
382 {
383 /* Cleanup context and finish async operation */
384 context_free (ctx);
385 mbimcli_async_operation_done (operation_status);
386 }
387
388 static void
389 query_device_caps_ready (MbimDevice *device,
390 GAsyncResult *res)
391 {
392 g_autoptr(MbimMessage) response = NULL;
393 g_autoptr(GError) error = NULL;
394 MbimDeviceType device_type;
395 const gchar *device_type_str;
396 MbimVoiceClass voice_class;
397 const gchar *voice_class_str;
398 MbimCellularClass cellular_class;
399 g_autofree gchar *cellular_class_str = NULL;
400 MbimSimClass sim_class;
401 g_autofree gchar *sim_class_str = NULL;
402 MbimDataClass data_class;
403 g_autofree gchar *data_class_str = NULL;
404 MbimSmsCaps sms_caps;
405 g_autofree gchar *sms_caps_str = NULL;
406 MbimCtrlCaps ctrl_caps;
407 g_autofree gchar *ctrl_caps_str = NULL;
408 guint32 max_sessions;
409 g_autofree gchar *custom_data_class = NULL;
410 g_autofree gchar *device_id = NULL;
411 g_autofree gchar *firmware_info = NULL;
412 g_autofree gchar *hardware_info = NULL;
413
414 response = mbim_device_command_finish (device, res, &error);
415 if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
416 g_printerr ("error: operation failed: %s\n", error->message);
417 shutdown (FALSE);
418 return;
419 }
420
421 if (!mbim_message_device_caps_response_parse (
422 response,
423 &device_type,
424 &cellular_class,
425 &voice_class,
426 &sim_class,
427 &data_class,
428 &sms_caps,
429 &ctrl_caps,
430 &max_sessions,
431 &custom_data_class,
432 &device_id,
433 &firmware_info,
434 &hardware_info,
435 &error)) {
436 g_printerr ("error: couldn't parse response message: %s\n", error->message);
437 shutdown (FALSE);
438 return;
439 }
440
441 device_type_str = mbim_device_type_get_string (device_type);
442 cellular_class_str = mbim_cellular_class_build_string_from_mask (cellular_class);
443 voice_class_str = mbim_voice_class_get_string (voice_class);
444 sim_class_str = mbim_sim_class_build_string_from_mask (sim_class);
445 data_class_str = mbim_data_class_build_string_from_mask (data_class);
446 sms_caps_str = mbim_sms_caps_build_string_from_mask (sms_caps);
447 ctrl_caps_str = mbim_ctrl_caps_build_string_from_mask (ctrl_caps);
448
449 g_print ("[%s] Device capabilities retrieved:\n"
450 "\t Device type: '%s'\n"
451 "\t Cellular class: '%s'\n"
452 "\t Voice class: '%s'\n"
453 "\t SIM class: '%s'\n"
454 "\t Data class: '%s'\n"
455 "\t SMS caps: '%s'\n"
456 "\t Ctrl caps: '%s'\n"
457 "\t Max sessions: '%u'\n"
458 "\tCustom data class: '%s'\n"
459 "\t Device ID: '%s'\n"
460 "\t Firmware info: '%s'\n"
461 "\t Hardware info: '%s'\n",
462 mbim_device_get_path_display (device),
463 VALIDATE_UNKNOWN (device_type_str),
464 VALIDATE_UNKNOWN (cellular_class_str),
465 VALIDATE_UNKNOWN (voice_class_str),
466 VALIDATE_UNKNOWN (sim_class_str),
467 VALIDATE_UNKNOWN (data_class_str),
468 VALIDATE_UNKNOWN (sms_caps_str),
469 VALIDATE_UNKNOWN (ctrl_caps_str),
470 max_sessions,
471 VALIDATE_UNKNOWN (custom_data_class),
472 VALIDATE_UNKNOWN (device_id),
473 VALIDATE_UNKNOWN (firmware_info),
474 VALIDATE_UNKNOWN (hardware_info));
475
476 shutdown (TRUE);
477 }
478
479 static void
480 query_subscriber_ready_status_ready (MbimDevice *device,
481 GAsyncResult *res)
482 {
483 g_autoptr (MbimMessage) response = NULL;
484 g_autoptr (GError) error = NULL;
485 MbimSubscriberReadyState ready_state;
486 const gchar *ready_state_str;
487 g_autofree gchar *subscriber_id = NULL;
488 g_autofree gchar *sim_iccid = NULL;
489 MbimReadyInfoFlag ready_info;
490 g_autofree gchar *ready_info_str = NULL;
491 guint32 telephone_numbers_count;
492 g_auto(GStrv) telephone_numbers = NULL;
493 g_autofree gchar *telephone_numbers_str = NULL;
494 MbimSubscriberReadyStatusFlag flags;
495
496 response = mbim_device_command_finish (device, res, &error);
497 if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
498 g_printerr ("error: operation failed: %s\n", error->message);
499 shutdown (FALSE);
500 return;
501 }
502
503 /* MBIMEx 3.0 support */
504 if (mbim_device_check_ms_mbimex_version (device, 3, 0)) {
505 if (!mbim_message_ms_basic_connect_v3_subscriber_ready_status_response_parse (response,
506 &ready_state,
507 &flags,
508 &subscriber_id,
509 &sim_iccid,
510 &ready_info,
511 &telephone_numbers_count,
512 &telephone_numbers,
513 &error)) {
514
515 g_printerr ("error: couldn't parse response message: %s\n", error->message);
516 shutdown (FALSE);
517 return;
518 }
519 g_debug ("Successfully parsed response as MBIMEx 3.0 Subscriber State");
520 }
521 /* MBIM 1.0 support */
522 else {
523 if (!mbim_message_subscriber_ready_status_response_parse (response,
524 &ready_state,
525 &subscriber_id,
526 &sim_iccid,
527 &ready_info,
528 &telephone_numbers_count,
529 &telephone_numbers,
530 &error)) {
531
532 g_printerr ("error: couldn't parse response message: %s\n", error->message);
533 shutdown (FALSE);
534 return;
535 }
536 g_debug ("Successfully parsed response as MBIM 1.0 Subscriber State");
537 }
538
539 telephone_numbers_str = (telephone_numbers ? g_strjoinv (", ", telephone_numbers) : NULL);
540 ready_state_str = mbim_subscriber_ready_state_get_string (ready_state);
541 ready_info_str = mbim_ready_info_flag_build_string_from_mask (ready_info);
542
543 g_print ("[%s] Subscriber ready status retrieved:\n"
544 "\t Ready state: '%s'\n"
545 "\t Subscriber ID: '%s'\n"
546 "\t SIM ICCID: '%s'\n"
547 "\t Ready info: '%s'\n"
548 "\tTelephone numbers: (%u) '%s'\n",
549 mbim_device_get_path_display (device),
550 VALIDATE_UNKNOWN (ready_state_str),
551 VALIDATE_UNKNOWN (subscriber_id),
552 VALIDATE_UNKNOWN (sim_iccid),
553 VALIDATE_UNKNOWN (ready_info_str),
554 telephone_numbers_count, VALIDATE_UNKNOWN (telephone_numbers_str));
555
556 if (mbim_device_check_ms_mbimex_version (device, 3, 0)) {
557 g_autofree gchar *flags_str = NULL;
558
559 flags_str = mbim_subscriber_ready_status_flag_build_string_from_mask (flags);
560 g_print ("\t Flags: '%s'\n",
561 VALIDATE_UNKNOWN (flags_str));
562 }
563
564 shutdown (TRUE);
565 }
566
567 static void
568 query_radio_state_ready (MbimDevice *device,
569 GAsyncResult *res)
570 {
571 g_autoptr(MbimMessage) response = NULL;
572 g_autoptr(GError) error = NULL;
573 MbimRadioSwitchState hardware_radio_state;
574 const gchar *hardware_radio_state_str;
575 MbimRadioSwitchState software_radio_state;
576 const gchar *software_radio_state_str;
577
578 response = mbim_device_command_finish (device, res, &error);
579 if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
580 g_printerr ("error: operation failed: %s\n", error->message);
581 shutdown (FALSE);
582 return;
583 }
584
585 if (!mbim_message_radio_state_response_parse (
586 response,
587 &hardware_radio_state,
588 &software_radio_state,
589 &error)) {
590 g_printerr ("error: couldn't parse response message: %s\n", error->message);
591 shutdown (FALSE);
592 return;
593 }
594
595 hardware_radio_state_str = mbim_radio_switch_state_get_string (hardware_radio_state);
596 software_radio_state_str = mbim_radio_switch_state_get_string (software_radio_state);
597
598 g_print ("[%s] Radio state retrieved:\n"
599 "\t Hardware radio state: '%s'\n"
600 "\t Software radio state: '%s'\n",
601 mbim_device_get_path_display (device),
602 VALIDATE_UNKNOWN (hardware_radio_state_str),
603 VALIDATE_UNKNOWN (software_radio_state_str));
604
605 shutdown (TRUE);
606 }
607
608 static void
609 query_device_services_ready (MbimDevice *device,
610 GAsyncResult *res)
611 {
612 g_autoptr(MbimMessage) response = NULL;
613 g_autoptr(GError) error = NULL;
614 g_autoptr(MbimDeviceServiceElementArray) device_services = NULL;
615 guint32 device_services_count;
616 guint32 max_dss_sessions;
617
618 response = mbim_device_command_finish (device, res, &error);
619 if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
620 g_printerr ("error: operation failed: %s\n", error->message);
621 shutdown (FALSE);
622 return;
623 }
624
625 if (!mbim_message_device_services_response_parse (
626 response,
627 &device_services_count,
628 &max_dss_sessions,
629 &device_services,
630 &error)) {
631 g_printerr ("error: couldn't parse response message: %s\n", error->message);
632 return;
633 }
634
635 g_print ("[%s] Device services retrieved:\n"
636 "\tMax DSS sessions: '%u'\n",
637 mbim_device_get_path_display (device),
638 max_dss_sessions);
639 if (device_services_count == 0)
640 g_print ("\t Services: None\n");
641 else {
642 guint32 i;
643
644 g_print ("\t Services: (%u)\n", device_services_count);
645 for (i = 0; i < device_services_count; i++) {
646 MbimService service;
647 g_autofree gchar *uuid_str = NULL;
648 g_autoptr(GString) cids = NULL;
649 guint32 j;
650
651 service = mbim_uuid_to_service (&device_services[i]->device_service_id);
652 uuid_str = mbim_uuid_get_printable (&device_services[i]->device_service_id);
653
654 cids = g_string_new ("");
655 for (j = 0; j < device_services[i]->cids_count; j++) {
656 if (service == MBIM_SERVICE_INVALID) {
657 g_string_append_printf (cids, "%u", device_services[i]->cids[j]);
658 if (j < device_services[i]->cids_count - 1)
659 g_string_append (cids, ", ");
660 } else {
661 g_string_append_printf (cids, "%s%s (%u)",
662 j == 0 ? "" : "\t\t ",
663 VALIDATE_UNKNOWN (mbim_cid_get_printable (service, device_services[i]->cids[j])),
664 device_services[i]->cids[j]);
665 if (j < device_services[i]->cids_count - 1)
666 g_string_append (cids, ",\n");
667 }
668 }
669
670 g_print ("\n"
671 "\t\t Service: '%s'\n"
672 "\t\t UUID: [%s]:\n"
673 "\t\t DSS payload: %u\n"
674 "\t\tMax DSS instances: %u\n"
675 "\t\t CIDs: %s\n",
676 service == MBIM_SERVICE_INVALID ? "unknown" : mbim_service_get_string (service),
677 uuid_str,
678 device_services[i]->dss_payload,
679 device_services[i]->max_dss_instances,
680 cids->str);
681 }
682 }
683
684 shutdown (TRUE);
685 }
686
687 static void
688 pin_ready (MbimDevice *device,
689 GAsyncResult *res,
690 gpointer user_data)
691 {
692 g_autoptr(MbimMessage) response = NULL;
693 g_autoptr(GError) error = NULL;
694 MbimPinType pin_type;
695 MbimPinState pin_state;
696 const gchar *pin_state_str;
697 guint32 remaining_attempts;
698
699 response = mbim_device_command_finish (device, res, &error);
700 if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
701 g_printerr ("error: operation failed: %s\n", error->message);
702 shutdown (FALSE);
703 return;
704 }
705
706 if (!mbim_message_pin_response_parse (
707 response,
708 &pin_type,
709 &pin_state,
710 &remaining_attempts,
711 &error)) {
712 g_printerr ("error: couldn't parse response message: %s\n", error->message);
713 shutdown (FALSE);
714 return;
715 }
716
717 if (GPOINTER_TO_UINT (user_data))
718 g_print ("[%s] PIN operation successful\n\n",
719 mbim_device_get_path_display (device));
720
721 pin_state_str = mbim_pin_state_get_string (pin_state);
722
723 g_print ("[%s] PIN info:\n"
724 "\t PIN state: '%s'\n",
725 mbim_device_get_path_display (device),
726 VALIDATE_UNKNOWN (pin_state_str));
727 if (pin_type != MBIM_PIN_TYPE_UNKNOWN) {
728 const gchar *pin_type_str;
729
730 pin_type_str = mbim_pin_type_get_string (pin_type);
731 g_print ("\t PIN type: '%s'\n"
732 "\tRemaining attempts: '%u'\n",
733 VALIDATE_UNKNOWN (pin_type_str),
734 remaining_attempts);
735 }
736
737 shutdown (TRUE);
738 }
739
740 static gboolean
741 set_pin_input_parse (const gchar *str,
742 gchar **pin,
743 gchar **new_pin,
744 MbimPinType *pin_type)
745 {
746 g_auto(GStrv) split = NULL;
747 guint n_min, n_max, n = 0;
748 MbimPinType new_pin_type;
749 g_autoptr(GError) error = NULL;
750
751 g_assert (pin != NULL);
752 n_min = (new_pin ? 2 : 1);
753 n_max = n_min + (pin_type ? 1 : 0);
754
755 /* Format of the string is:
756 * "[(current PIN)]"
757 * or:
758 * "[(PIN name),(current PIN)]"
759 * or:
760 * "[(current PIN),(new PIN)]"
761 * or:
762 * "[(PUK name),(PUK),(new PIN)]"
763 */
764 split = g_strsplit (str, ",", -1);
765
766 if (g_strv_length (split) > n_max) {
767 g_printerr ("error: couldn't parse input string, too many arguments\n");
768 return FALSE;
769 }
770
771 if (g_strv_length (split) < n_min) {
772 g_printerr ("error: couldn't parse input string, missing arguments\n");
773 return FALSE;
774 }
775
776 if (pin_type && (g_strv_length (split) == n_max)) {
777 const gchar *pin_type_str;
778
779 pin_type_str = split[n++];
780 if (!mbimcli_read_pin_type_from_string (pin_type_str, &new_pin_type, &error)) {
781 g_printerr ("error: couldn't parse input pin-type: %s\n", error->message);
782 return FALSE;
783 }
784 if (new_pin_type == MBIM_PIN_TYPE_UNKNOWN ||
785 (*pin_type == MBIM_PIN_TYPE_PIN1 && new_pin_type >= MBIM_PIN_TYPE_PUK1) ||
786 (*pin_type == MBIM_PIN_TYPE_PUK1 && new_pin_type < MBIM_PIN_TYPE_PUK1)) {
787 g_printerr ("error: couldn't parse input string, invalid PIN type\n");
788 return FALSE;
789 }
790 *pin_type = new_pin_type;
791 }
792
793 *pin = g_strdup (split[n++]);
794 if (new_pin)
795 *new_pin = g_strdup (split[n]);
796
797 return TRUE;
798 }
799
800 enum {
801 CONNECTION_STATUS,
802 CONNECT,
803 DISCONNECT
804 };
805
806 static void
807 print_pin_desc (const gchar *pin_name,
808 const MbimPinDesc *pin_desc)
809 {
810 g_print ("\t%s:\n"
811 "\t\t Mode: '%s'\n"
812 "\t\t Format: '%s'\n"
813 "\t\tMin length: '%d'\n"
814 "\t\tMax length: '%d'\n"
815 "\n",
816 pin_name,
817 VALIDATE_UNKNOWN (mbim_pin_mode_get_string (pin_desc->pin_mode)),
818 VALIDATE_UNKNOWN (mbim_pin_format_get_string (pin_desc->pin_format)),
819 pin_desc->pin_length_min,
820 pin_desc->pin_length_max);
821 }
822
823 static void
824 pin_list_ready (MbimDevice *device,
825 GAsyncResult *res,
826 gpointer user_data)
827 {
828 g_autoptr(MbimMessage) response = NULL;
829 g_autoptr(GError) error = NULL;
830 g_autoptr(MbimPinDesc) pin_desc_pin1 = NULL;
831 g_autoptr(MbimPinDesc) pin_desc_pin2 = NULL;
832 g_autoptr(MbimPinDesc) pin_desc_device_sim_pin = NULL;
833 g_autoptr(MbimPinDesc) pin_desc_device_first_sim_pin = NULL;
834 g_autoptr(MbimPinDesc) pin_desc_network_pin = NULL;
835 g_autoptr(MbimPinDesc) pin_desc_network_subset_pin = NULL;
836 g_autoptr(MbimPinDesc) pin_desc_service_provider_pin = NULL;
837 g_autoptr(MbimPinDesc) pin_desc_corporate_pin = NULL;
838 g_autoptr(MbimPinDesc) pin_desc_subsidy_lock = NULL;
839 g_autoptr(MbimPinDesc) pin_desc_custom = NULL;
840
841 response = mbim_device_command_finish (device, res, &error);
842 if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
843 g_printerr ("error: operation failed: %s\n", error->message);
844 shutdown (FALSE);
845 return;
846 }
847
848 if (!mbim_message_pin_list_response_parse (
849 response,
850 &pin_desc_pin1,
851 &pin_desc_pin2,
852 &pin_desc_device_sim_pin,
853 &pin_desc_device_first_sim_pin,
854 &pin_desc_network_pin,
855 &pin_desc_network_subset_pin,
856 &pin_desc_service_provider_pin,
857 &pin_desc_corporate_pin,
858 &pin_desc_subsidy_lock,
859 &pin_desc_custom,
860 &error)) {
861 g_printerr ("error: couldn't parse response message: %s\n", error->message);
862 shutdown (FALSE);
863 return;
864 }
865
866 g_print ("[%s] PIN list:\n\n",
867 mbim_device_get_path_display (device));
868
869 print_pin_desc ("PIN1", pin_desc_pin1);
870 print_pin_desc ("PIN2", pin_desc_pin2);
871 print_pin_desc ("Device SIM PIN", pin_desc_device_sim_pin);
872 print_pin_desc ("Device first SIM PIN", pin_desc_device_first_sim_pin);
873 print_pin_desc ("Network PIN", pin_desc_network_pin);
874 print_pin_desc ("Network subset PIN", pin_desc_network_subset_pin);
875 print_pin_desc ("Service provider PIN", pin_desc_service_provider_pin);
876 print_pin_desc ("Corporate PIN", pin_desc_corporate_pin);
877 print_pin_desc ("Subsidy lock", pin_desc_subsidy_lock);
878 print_pin_desc ("Custom", pin_desc_custom);
879
880 shutdown (TRUE);
881 }
882
883 static void
884 ip_configuration_query_ready (MbimDevice *device,
885 GAsyncResult *res)
886 {
887 g_autoptr(GError) error = NULL;
888 g_autoptr(MbimMessage) response;
889 gboolean success = FALSE;
890
891 response = mbim_device_command_finish (device, res, &error);
892 if (!response ||
893 !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
894 g_printerr ("error: couldn't get IP configuration response message: %s\n", error->message);
895 } else {
896 success = mbimcli_print_ip_config (device, response, &error);
897 if (!success)
898 g_printerr ("error: couldn't parse IP configuration response message: %s\n", error->message);
899 }
900
901 shutdown (success);
902 }
903
904 static void
905 ip_configuration_query (MbimDevice *device,
906 GCancellable *cancellable,
907 guint32 session_id)
908 {
909 g_autoptr(MbimMessage) message = NULL;
910 g_autoptr(GError) error = NULL;
911
912 message = (mbim_message_ip_configuration_query_new (
913 session_id,
914 MBIM_IP_CONFIGURATION_AVAILABLE_FLAG_NONE, /* ipv4configurationavailable */
915 MBIM_IP_CONFIGURATION_AVAILABLE_FLAG_NONE, /* ipv6configurationavailable */
916 0, /* ipv4addresscount */
917 NULL, /* ipv4address */
918 0, /* ipv6addresscount */
919 NULL, /* ipv6address */
920 NULL, /* ipv4gateway */
921 NULL, /* ipv6gateway */
922 0, /* ipv4dnsservercount */
923 NULL, /* ipv4dnsserver */
924 0, /* ipv6dnsservercount */
925 NULL, /* ipv6dnsserver */
926 0, /* ipv4mtu */
927 0, /* ipv6mtu */
928 &error));
929 if (!message) {
930 g_printerr ("error: couldn't create IP config request: %s\n", error->message);
931 shutdown (FALSE);
932 return;
933 }
934
935 mbim_device_command (device,
936 message,
937 60,
938 cancellable,
939 (GAsyncReadyCallback)ip_configuration_query_ready,
940 NULL);
941 }
942
943 static void
944 connect_ready (MbimDevice *device,
945 GAsyncResult *res,
946 gpointer user_data)
947 {
948 g_autoptr(MbimMessage) response = NULL;
949 g_autoptr(GError) error = NULL;
950 guint32 session_id;
951 MbimActivationState activation_state;
952 MbimVoiceCallState voice_call_state;
953 MbimContextIpType ip_type;
954 MbimAccessMediaType media_type = MBIM_ACCESS_MEDIA_TYPE_UNKNOWN;
955 g_autofree gchar *access_string = NULL;
956 const MbimUuid *context_type;
957 guint32 nw_error;
958
959 response = mbim_device_command_finish (device, res, &error);
960 if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
961 g_printerr ("error: operation failed: %s\n", error->message);
962 shutdown (FALSE);
963 return;
964 }
965
966 if (mbim_device_check_ms_mbimex_version (device, 3, 0)) {
967 if (!mbim_message_ms_basic_connect_v3_connect_response_parse (
968 response,
969 &session_id,
970 &activation_state,
971 &voice_call_state,
972 &ip_type,
973 &context_type,
974 &nw_error,
975 &media_type,
976 &access_string,
977 NULL, /* unnamed IEs ignored */
978 &error)) {
979 g_printerr ("error: couldn't parse response message: %s\n", error->message);
980 shutdown (FALSE);
981 return;
982 }
983 } else {
984 if (!mbim_message_connect_response_parse (
985 response,
986 &session_id,
987 &activation_state,
988 &voice_call_state,
989 &ip_type,
990 &context_type,
991 &nw_error,
992 &error)) {
993 g_printerr ("error: couldn't parse response message: %s\n", error->message);
994 shutdown (FALSE);
995 return;
996 }
997 }
998
999 switch (GPOINTER_TO_UINT (user_data)) {
1000 case CONNECT:
1001 g_print ("[%s] Successfully connected\n\n",
1002 mbim_device_get_path_display (device));
1003 break;
1004 case DISCONNECT:
1005 g_print ("[%s] Successfully disconnected\n\n",
1006 mbim_device_get_path_display (device));
1007 break;
1008 default:
1009 break;
1010 }
1011
1012 g_print ("[%s] Connection status:\n"
1013 "\t Session ID: '%u'\n"
1014 "\tActivation state: '%s'\n"
1015 "\tVoice call state: '%s'\n"
1016 "\t IP type: '%s'\n"
1017 "\t Context type: '%s'\n"
1018 "\t Network error: '%s'\n",
1019 mbim_device_get_path_display (device),
1020 session_id,
1021 VALIDATE_UNKNOWN (mbim_activation_state_get_string (activation_state)),
1022 VALIDATE_UNKNOWN (mbim_voice_call_state_get_string (voice_call_state)),
1023 VALIDATE_UNKNOWN (mbim_context_ip_type_get_string (ip_type)),
1024 VALIDATE_UNKNOWN (mbim_context_type_get_string (mbim_uuid_to_context_type (context_type))),
1025 VALIDATE_UNKNOWN (mbim_nw_error_get_string (nw_error)));
1026
1027 if (mbim_device_check_ms_mbimex_version (device, 3, 0)) {
1028 g_print ("\tAccess media type: '%s'\n"
1029 "\t Access string: '%s'\n",
1030 VALIDATE_UNKNOWN (mbim_access_media_type_get_string (media_type)),
1031 access_string);
1032 }
1033
1034 if (GPOINTER_TO_UINT (user_data) == CONNECT) {
1035 ip_configuration_query (device, NULL, session_id);
1036 return;
1037 }
1038
1039 shutdown (TRUE);
1040 }
1041
1042 static void
1043 ip_packet_filters_ready (MbimDevice *device,
1044 GAsyncResult *res)
1045 {
1046 g_autoptr(MbimMessage) response = NULL;
1047 g_autoptr(GError) error = NULL;
1048 g_autoptr(MbimPacketFilterArray) filters = NULL;
1049 g_autoptr(MbimPacketFilterV3Array) filters_v3 = NULL;
1050 guint32 filters_count;
1051 guint32 i;
1052
1053 response = mbim_device_command_finish (device, res, &error);
1054 if (!response ||
1055 !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
1056 g_printerr ("error: operation failed: %s\n", error->message);
1057 shutdown (FALSE);
1058 return;
1059 }
1060
1061 /* MBIMEx 3.0 support */
1062 if (mbim_device_check_ms_mbimex_version (device, 3, 0)) {
1063 if (!mbim_message_ms_basic_connect_v3_ip_packet_filters_response_parse (
1064 response,
1065 NULL, /* sessionid */
1066 &filters_count,
1067 &filters_v3,
1068 &error)) {
1069 g_printerr ("error: couldn't parse response message: %s\n", error->message);
1070 shutdown (FALSE);
1071 return;
1072 }
1073 g_debug ("Successfully parsed response as MBIMEx 3.0 IP Packet Filters");
1074 }
1075 /* MBIM 1.0 support */
1076 else {
1077 if (!mbim_message_ip_packet_filters_response_parse (
1078 response,
1079 NULL, /* sessionid */
1080 &filters_count,
1081 &filters,
1082 &error)) {
1083 g_printerr ("error: couldn't parse response message: %s\n", error->message);
1084 shutdown (FALSE);
1085 return;
1086 }
1087 g_debug ("Successfully parsed response as MBIM 1.0 IP Packet Filters");
1088 }
1089
1090 g_print ("[%s] IP packet filters: (%u)\n", mbim_device_get_path_display (device), filters_count);
1091
1092 for (i = 0; i < filters_count; i++) {
1093 g_autofree gchar *packet_filter = NULL;
1094 g_autofree gchar *packet_mask = NULL;
1095 guint32 filter_size = 0;
1096
1097 if (filters_v3) {
1098 filter_size = filters_v3[i]->filter_size;
1099 packet_filter = mbim_common_str_hex (filters_v3[i]->packet_filter, filter_size, ' ');
1100 packet_mask = mbim_common_str_hex (filters_v3[i]->packet_mask, filter_size, ' ');
1101 } else if (filters) {
1102 filter_size = filters[i]->filter_size;
1103 packet_filter = mbim_common_str_hex (filters[i]->packet_filter, filter_size, ' ');
1104 packet_mask = mbim_common_str_hex (filters[i]->packet_mask, filter_size, ' ');
1105 } else
1106 g_assert_not_reached ();
1107
1108 g_print ("Filter %u:\n", i);
1109 g_print ("\tFilter size : %u\n", filter_size);
1110 g_print ("\tPacket filter : %s\n", VALIDATE_UNKNOWN (packet_filter));
1111 g_print ("\tPacket mask : %s\n", VALIDATE_UNKNOWN (packet_mask));
1112 if (filters_v3)
1113 g_print ("\tFilter ID : %u\n", filters_v3[i]->filter_id);
1114 }
1115
1116 shutdown (TRUE);
1117 }
1118
1119 static gboolean
1120 connect_session_id_parse (const gchar *str,
1121 gboolean allow_empty,
1122 guint32 *session_id,
1123 GError **error)
1124 {
1125 gchar *endptr = NULL;
1126 gint64 n;
1127
1128 g_assert (str != NULL);
1129 g_assert (session_id != NULL);
1130
1131 if (!str[0]) {
1132 if (allow_empty) {
1133 *session_id = 0;
1134 return TRUE;
1135 }
1136 g_set_error_literal (error,
1137 MBIM_CORE_ERROR,
1138 MBIM_CORE_ERROR_FAILED,
1139 "missing session ID (must be 0 - 255)");
1140 return FALSE;
1141 }
1142
1143 errno = 0;
1144 n = g_ascii_strtoll (str, &endptr, 10);
1145 if (errno || n < 0 || n > 255 || ((size_t)(endptr - str) < strlen (str))) {
1146 g_set_error (error,
1147 MBIM_CORE_ERROR,
1148 MBIM_CORE_ERROR_FAILED,
1149 "couldn't parse session ID '%s' (must be 0 - 255)",
1150 str);
1151 return FALSE;
1152 }
1153 *session_id = (guint32) n;
1154
1155 return TRUE;
1156 }
1157
1158 typedef struct {
1159 gboolean v3;
1160 guint32 session_id;
1161 GPtrArray *array;
1162 gchar *tmp_packet_filter;
1163 gchar *tmp_packet_mask;
1164 gchar *tmp_filter_id;
1165 } SetIpPacketFiltersProperties;
1166
1167 static void
1168 mbim_packet_filter_v3_free (MbimPacketFilterV3 *var)
1169 {
1170 if (!var)
1171 return;
1172
1173 g_free (var->packet_filter);
1174 g_free (var->packet_mask);
1175 g_free (var);
1176 }
1177
1178 static void
1179 mbim_packet_filter_free (MbimPacketFilter *var)
1180 {
1181 if (!var)
1182 return;
1183
1184 g_free (var->packet_filter);
1185 g_free (var->packet_mask);
1186 g_free (var);
1187 }
1188
1189 static void
1190 set_ip_packet_filters_properties_clear (SetIpPacketFiltersProperties *props)
1191 {
1192 g_free (props->tmp_packet_filter);
1193 g_free (props->tmp_packet_mask);
1194 g_free (props->tmp_filter_id);
1195 g_ptr_array_unref (props->array);
1196 }
1197
1198 G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(SetIpPacketFiltersProperties, set_ip_packet_filters_properties_clear);
1199
1200 static gboolean
1201 check_filter_add (SetIpPacketFiltersProperties *props,
1202 GError **error)
1203 {
1204 g_autofree guint8 *packet_filter = NULL;
1205 gsize packet_filter_size = 0;
1206 g_autofree guint8 *packet_mask = NULL;
1207 gsize packet_mask_size = 0;
1208
1209 if (!props->tmp_packet_filter) {
1210 g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_FAILED,
1211 "Option 'packet-filter' is missing");
1212 return FALSE;
1213 }
1214
1215 if (!props->tmp_packet_mask) {
1216 g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_FAILED,
1217 "Option 'packet-mask' is missing");
1218 return FALSE;
1219 }
1220
1221 if (!props->v3 && props->tmp_filter_id) {
1222 g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_FAILED,
1223 "Option 'filter-id' is specific to MBIMEx v3.0");
1224 return FALSE;
1225 }
1226
1227 if (props->v3 && !props->tmp_filter_id) {
1228 g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_FAILED,
1229 "Option 'filter-id' is missing");
1230 return FALSE;
1231 }
1232
1233 packet_filter = mbimcli_read_buffer_from_string (props->tmp_packet_filter, -1, &packet_filter_size, error);
1234 if (!packet_filter)
1235 return FALSE;
1236
1237 packet_mask = mbimcli_read_buffer_from_string (props->tmp_packet_mask, -1, &packet_mask_size, error);
1238 if (!packet_mask)
1239 return FALSE;
1240
1241 if (packet_filter_size != packet_mask_size) {
1242 g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_FAILED,
1243 "Option 'packet-filter' and 'packet-mask' must have same size");
1244 return FALSE;
1245 }
1246
1247 if (props->v3) {
1248 MbimPacketFilterV3 *filter;
1249 guint filter_id;
1250
1251 if (!mbimcli_read_uint_from_string (props->tmp_filter_id, &filter_id)) {
1252 g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_FAILED,
1253 "Failed to parse 'filter-id' field as an integer");
1254 return FALSE;
1255 }
1256
1257 filter = g_new0 (MbimPacketFilterV3, 1);
1258 filter->filter_size = packet_filter_size;
1259 filter->packet_filter = g_steal_pointer (&packet_filter);
1260 filter->packet_mask = g_steal_pointer (&packet_mask);
1261 filter->filter_id = filter_id;
1262 g_ptr_array_add (props->array, filter);
1263 } else {
1264 MbimPacketFilter *filter;
1265
1266 filter = g_new0 (MbimPacketFilter, 1);
1267 filter->filter_size = packet_filter_size;
1268 filter->packet_filter = g_steal_pointer (&packet_filter);
1269 filter->packet_mask = g_steal_pointer (&packet_mask);
1270 g_ptr_array_add (props->array, filter);
1271 }
1272
1273 g_clear_pointer (&props->tmp_filter_id, g_free);
1274 g_clear_pointer (&props->tmp_packet_filter, g_free);
1275 g_clear_pointer (&props->tmp_packet_mask, g_free);
1276
1277 return TRUE;
1278 }
1279
1280 static gboolean
1281 set_ip_packet_filters_properties_handle (const gchar *key,
1282 const gchar *value,
1283 GError **error,
1284 gpointer user_data)
1285 {
1286 SetIpPacketFiltersProperties *props = user_data;
1287
1288 if (g_ascii_strcasecmp (key, "session-id") == 0) {
1289 if (!connect_session_id_parse (value, FALSE, &props->session_id, error))
1290 return FALSE;
1291 } else if (g_ascii_strcasecmp (key, "packet-filter") == 0) {
1292 if (props->tmp_packet_filter) {
1293 if (!check_filter_add (props, error))
1294 return FALSE;
1295 g_clear_pointer (&props->tmp_packet_filter, g_free);
1296 }
1297 props->tmp_packet_filter = g_strdup (value);
1298 } else if (g_ascii_strcasecmp (key, "packet-mask") == 0) {
1299 if (props->tmp_packet_mask) {
1300 if (!check_filter_add (props, error))
1301 return FALSE;
1302 g_clear_pointer (&props->tmp_packet_mask, g_free);
1303 }
1304 props->tmp_packet_mask = g_strdup (value);
1305 } else if (g_ascii_strcasecmp (key, "filter-id") == 0) {
1306 if (props->tmp_filter_id) {
1307 if (!check_filter_add (props, error))
1308 return FALSE;
1309 g_clear_pointer (&props->tmp_filter_id, g_free);
1310 }
1311 props->tmp_filter_id = g_strdup (value);
1312 } else {
1313 g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_FAILED,
1314 "unrecognized option '%s'", key);
1315 return FALSE;
1316 }
1317
1318 return TRUE;
1319 }
1320
1321 static gboolean
1322 set_ip_packet_filters_parse (const gchar *str,
1323 SetIpPacketFiltersProperties *props,
1324 MbimDevice *device)
1325 {
1326 g_autoptr(GError) error = NULL;
1327
1328 if (!mbimcli_parse_key_value_string (str,
1329 &error,
1330 set_ip_packet_filters_properties_handle,
1331 props)) {
1332 g_printerr ("error: couldn't parse input string: %s\n", error->message);
1333 return FALSE;
1334 }
1335
1336 if ((props->tmp_packet_filter || props->tmp_packet_mask) &&
1337 !check_filter_add (props, &error)) {
1338 g_printerr ("error: failed to add last packet filter item: %s\n", error->message);
1339 return FALSE;
1340 }
1341
1342 return TRUE;
1343 }
1344
1345 typedef struct {
1346 guint32 session_id;
1347 gchar *access_string;
1348 MbimAuthProtocol auth_protocol;
1349 gchar *username;
1350 gchar *password;
1351 MbimContextIpType ip_type;
1352 MbimCompression compression;
1353 MbimContextType context_type;
1354 MbimAccessMediaType media_type;
1355 } ConnectActivateProperties;
1356
1357 static void
1358 connect_activate_properties_clear (ConnectActivateProperties *props)
1359 {
1360 g_free (props->access_string);
1361 g_free (props->username);
1362 g_free (props->password);
1363 }
1364
1365 G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ConnectActivateProperties, connect_activate_properties_clear);
1366
1367 static gboolean
1368 connect_activate_properties_handle (const gchar *key,
1369 const gchar *value,
1370 GError **error,
1371 gpointer user_data)
1372 {
1373 ConnectActivateProperties *props = user_data;
1374
1375 /* access-string/apn may be empty */
1376 if ((g_ascii_strcasecmp (key, "access-string") != 0) &&
1377 (g_ascii_strcasecmp (key, "apn") != 0) &&
1378 (!value || !value[0])) {
1379 g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_FAILED,
1380 "key '%s' required a value", key);
1381 return FALSE;
1382 }
1383
1384 if (g_ascii_strcasecmp (key, "session-id") == 0) {
1385 if (!connect_session_id_parse (value, FALSE, &props->session_id, error))
1386 return FALSE;
1387 } else if (g_ascii_strcasecmp (key, "apn") == 0) {
1388 g_printerr ("warning: key 'apn' is deprecated, use 'access-string' instead\n");
1389 g_free (props->access_string);
1390 props->access_string = g_strdup (value);
1391 } else if (g_ascii_strcasecmp (key, "access-string") == 0) {
1392 g_free (props->access_string);
1393 props->access_string = g_strdup (value);
1394 } else if (g_ascii_strcasecmp (key, "auth") == 0) {
1395 if (!mbimcli_read_auth_protocol_from_string (value, &props->auth_protocol, error)) {
1396 return FALSE;
1397 }
1398 } else if (g_ascii_strcasecmp (key, "username") == 0) {
1399 g_free (props->username);
1400 props->username = g_strdup (value);
1401 } else if (g_ascii_strcasecmp (key, "password") == 0) {
1402 g_free (props->password);
1403 props->password = g_strdup (value);
1404 } else if (g_ascii_strcasecmp (key, "ip-type") == 0) {
1405 if (!mbimcli_read_context_ip_type_from_string (value, &props->ip_type, error)) {
1406 return FALSE;
1407 }
1408 } else if (g_ascii_strcasecmp (key, "compression") == 0) {
1409 if (!mbimcli_read_compression_from_string (value, &props->compression, error)) {
1410 return FALSE;
1411 }
1412 } else if (g_ascii_strcasecmp (key, "context-type") == 0) {
1413 if (!mbimcli_read_context_type_from_string (value, &props->context_type, error)) {
1414 return FALSE;
1415 }
1416 } else if (g_ascii_strcasecmp (key, "media-type") == 0) {
1417 if (!mbimcli_read_access_media_type_from_string (value, &props->media_type, error)) {
1418 return FALSE;
1419 }
1420 } else {
1421 g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_FAILED,
1422 "unrecognized option '%s'", key);
1423 return FALSE;
1424 }
1425
1426 return TRUE;
1427 }
1428
1429 static gboolean
1430 set_connect_activate_parse (const gchar *str,
1431 ConnectActivateProperties *props,
1432 MbimDevice *device)
1433 {
1434 g_auto(GStrv) split = NULL;
1435 g_autoptr(GError) error = NULL;
1436
1437 if (strchr (str, '=')) {
1438 /* New key=value format */
1439 if (!mbimcli_parse_key_value_string (str,
1440 &error,
1441 connect_activate_properties_handle,
1442 props)) {
1443 g_printerr ("error: couldn't parse input string: %s\n", error->message);
1444 return FALSE;
1445 }
1446 } else {
1447 /* Old non key=value format, like this:
1448 * "[(APN),(PAP|CHAP|MSCHAPV2),(Username),(Password)]"
1449 */
1450 g_printerr ("warning: positional input arguments format is deprecated, use key-value format instead\n");
1451 split = g_strsplit (str, ",", -1);
1452
1453 if (g_strv_length (split) > 4) {
1454 g_printerr ("error: couldn't parse input string, too many arguments\n");
1455 return FALSE;
1456 }
1457
1458 if (g_strv_length (split) > 0) {
1459 /* APN */
1460 props->access_string = g_strdup (split[0]);
1461
1462 /* Use authentication method */
1463 if (split[1]) {
1464 if (!mbimcli_read_auth_protocol_from_string (split[1], &props->auth_protocol, &error)) {
1465 g_printerr ("error: couldn't parse auth protocol: %s\n", error->message);
1466 return FALSE;
1467 }
1468 /* Username */
1469 if (split[2]) {
1470 props->username = g_strdup (split[2]);
1471 /* Password */
1472 props->password = g_strdup (split[3]);
1473 }
1474 }
1475 }
1476
1477 if (props->auth_protocol == MBIM_AUTH_PROTOCOL_NONE) {
1478 if (props->username || props->password) {
1479 g_printerr ("error: username or password requires an auth protocol\n");
1480 return FALSE;
1481 }
1482 } else {
1483 if (!props->username) {
1484 g_printerr ("error: auth protocol requires a username\n");
1485 return FALSE;
1486 }
1487 }
1488 }
1489
1490 return TRUE;
1491 }
1492
1493 static void
1494 home_provider_ready (MbimDevice *device,
1495 GAsyncResult *res)
1496 {
1497 g_autoptr(MbimMessage) response = NULL;
1498 g_autoptr(GError) error = NULL;
1499 g_autoptr(MbimProvider) provider = NULL;
1500 g_autofree gchar *provider_state_str = NULL;
1501 g_autofree gchar *cellular_class_str = NULL;
1502
1503 response = mbim_device_command_finish (device, res, &error);
1504 if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
1505 g_printerr ("error: operation failed: %s\n", error->message);
1506 shutdown (FALSE);
1507 return;
1508 }
1509
1510 if (!mbim_message_home_provider_response_parse (response, &provider, &error)) {
1511 g_printerr ("error: couldn't parse response message: %s\n", error->message);
1512 shutdown (FALSE);
1513 return;
1514 }
1515
1516 provider_state_str = mbim_provider_state_build_string_from_mask (provider->provider_state);
1517 cellular_class_str = mbim_cellular_class_build_string_from_mask (provider->cellular_class);
1518
1519 g_print ("[%s] Home provider:\n"
1520 "\t Provider ID: '%s'\n"
1521 "\t Provider name: '%s'\n"
1522 "\t State: '%s'\n"
1523 "\tCellular class: '%s'\n"
1524 "\t RSSI: '%u'\n"
1525 "\t Error rate: '%u'\n",
1526 mbim_device_get_path_display (device),
1527 VALIDATE_UNKNOWN (provider->provider_id),
1528 VALIDATE_UNKNOWN (provider->provider_name),
1529 VALIDATE_UNKNOWN (provider_state_str),
1530 VALIDATE_UNKNOWN (cellular_class_str),
1531 provider->rssi,
1532 provider->error_rate);
1533
1534 shutdown (TRUE);
1535 }
1536
1537 static void
1538 preferred_providers_ready (MbimDevice *device,
1539 GAsyncResult *res)
1540 {
1541 g_autoptr(MbimMessage) response = NULL;
1542 g_autoptr(GError) error = NULL;
1543 g_autoptr(MbimProviderArray) providers = NULL;
1544 guint n_providers;
1545 guint i;
1546
1547 response = mbim_device_command_finish (device, res, &error);
1548 if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
1549 g_printerr ("error: operation failed: %s\n", error->message);
1550 shutdown (FALSE);
1551 return;
1552 }
1553
1554 if (!mbim_message_preferred_providers_response_parse (response, &n_providers, &providers, &error)) {
1555 g_printerr ("error: couldn't parse response message: %s\n", error->message);
1556 shutdown (FALSE);
1557 return;
1558 }
1559
1560 if (!n_providers)
1561 g_print ("[%s] No preferred providers given\n", mbim_device_get_path_display (device));
1562 else
1563 g_print ("[%s] Preferred providers (%u):\n", mbim_device_get_path_display (device), n_providers);
1564
1565 for (i = 0; i < n_providers; i++) {
1566 g_autofree gchar *provider_state_str = NULL;
1567 g_autofree gchar *cellular_class_str = NULL;
1568
1569 provider_state_str = mbim_provider_state_build_string_from_mask (providers[i]->provider_state);
1570 cellular_class_str = mbim_cellular_class_build_string_from_mask (providers[i]->cellular_class);
1571
1572 g_print ("\tProvider [%u]:\n"
1573 "\t\t Provider ID: '%s'\n"
1574 "\t\t Provider name: '%s'\n"
1575 "\t\t State: '%s'\n"
1576 "\t\t Cellular class: '%s'\n"
1577 "\t\t RSSI: '%u'\n"
1578 "\t\t Error rate: '%u'\n",
1579 i,
1580 VALIDATE_UNKNOWN (providers[i]->provider_id),
1581 VALIDATE_UNKNOWN (providers[i]->provider_name),
1582 VALIDATE_UNKNOWN (provider_state_str),
1583 VALIDATE_UNKNOWN (cellular_class_str),
1584 providers[i]->rssi,
1585 providers[i]->error_rate);
1586 }
1587
1588 shutdown (TRUE);
1589 }
1590
1591 static void
1592 visible_providers_ready (MbimDevice *device,
1593 GAsyncResult *res)
1594 {
1595 g_autoptr(MbimMessage) response = NULL;
1596 g_autoptr(GError) error = NULL;
1597 g_autoptr(MbimProviderArray) providers = NULL;
1598 guint n_providers;
1599 guint i;
1600
1601 response = mbim_device_command_finish (device, res, &error);
1602 if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
1603 g_printerr ("error: operation failed: %s\n", error->message);
1604 shutdown (FALSE);
1605 return;
1606 }
1607
1608 if (!mbim_message_visible_providers_response_parse (response, &n_providers, &providers, &error)) {
1609 g_printerr ("error: couldn't parse response message: %s\n", error->message);
1610 shutdown (FALSE);
1611 return;
1612 }
1613
1614 if (!n_providers)
1615 g_print ("[%s] No visible providers given\n", mbim_device_get_path_display (device));
1616 else
1617 g_print ("[%s] Visible providers (%u):\n", mbim_device_get_path_display (device), n_providers);
1618
1619 for (i = 0; i < n_providers; i++) {
1620 g_autofree gchar *provider_state_str = NULL;
1621 g_autofree gchar *cellular_class_str = NULL;
1622
1623 provider_state_str = mbim_provider_state_build_string_from_mask (providers[i]->provider_state);
1624 cellular_class_str = mbim_cellular_class_build_string_from_mask (providers[i]->cellular_class);
1625
1626 g_print ("\tProvider [%u]:\n"
1627 "\t\t Provider ID: '%s'\n"
1628 "\t\t Provider name: '%s'\n"
1629 "\t\t State: '%s'\n"
1630 "\t\t Cellular class: '%s'\n"
1631 "\t\t RSSI: '%u'\n"
1632 "\t\t Error rate: '%u'\n",
1633 i,
1634 VALIDATE_UNKNOWN (providers[i]->provider_id),
1635 VALIDATE_UNKNOWN (providers[i]->provider_name),
1636 VALIDATE_UNKNOWN (provider_state_str),
1637 VALIDATE_UNKNOWN (cellular_class_str),
1638 providers[i]->rssi,
1639 providers[i]->error_rate);
1640 }
1641
1642 shutdown (TRUE);
1643 }
1644
1645 static void
1646 register_state_ready (MbimDevice *device,
1647 GAsyncResult *res,
1648 gpointer user_data)
1649 {
1650 g_autoptr(MbimMessage) response = NULL;
1651 g_autoptr(GError) error = NULL;
1652 MbimNwError nw_error;
1653 MbimRegisterState register_state;
1654 MbimRegisterMode register_mode;
1655 MbimDataClass available_data_classes;
1656 g_autofree gchar *available_data_classes_str = NULL;
1657 MbimCellularClass cellular_class;
1658 g_autofree gchar *cellular_class_str = NULL;
1659 g_autofree gchar *provider_id = NULL;
1660 g_autofree gchar *provider_name = NULL;
1661 g_autofree gchar *roaming_text = NULL;
1662 MbimRegistrationFlag registration_flag;
1663 g_autofree gchar *registration_flag_str = NULL;
1664 MbimDataClass preferred_data_classes;
1665
1666 response = mbim_device_command_finish (device, res, &error);
1667 if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
1668 g_printerr ("error: operation failed: %s\n", error->message);
1669 shutdown (FALSE);
1670 return;
1671 }
1672
1673 /* MBIMEx 2.0 support */
1674 if (mbim_device_check_ms_mbimex_version (device, 2, 0)) {
1675 if (!mbim_message_ms_basic_connect_v2_register_state_response_parse (response,
1676 &nw_error,
1677 &register_state,
1678 &register_mode,
1679 &available_data_classes,
1680 &cellular_class,
1681 &provider_id,
1682 &provider_name,
1683 &roaming_text,
1684 &registration_flag,
1685 &preferred_data_classes,
1686 &error)) {
1687 g_printerr ("error: couldn't parse response message: %s\n", error->message);
1688 shutdown (FALSE);
1689 return;
1690 }
1691 g_debug ("Successfully parsed response as MBIMEx 2.0 Register State");
1692 }
1693 /* MBIM 1.0 support */
1694 else {
1695 if (!mbim_message_register_state_response_parse (response,
1696 &nw_error,
1697 &register_state,
1698 &register_mode,
1699 &available_data_classes,
1700 &cellular_class,
1701 &provider_id,
1702 &provider_name,
1703 &roaming_text,
1704 &registration_flag,
1705 &error)) {
1706 g_printerr ("error: couldn't parse response message: %s\n", error->message);
1707 shutdown (FALSE);
1708 return;
1709 }
1710 g_debug ("Successfully parsed response as MBIM 1.0 Register State");
1711 }
1712
1713 if (GPOINTER_TO_UINT (user_data))
1714 g_print ("[%s] Successfully launched automatic registration\n\n",
1715 mbim_device_get_path_display (device));
1716
1717 available_data_classes_str = mbim_data_class_build_string_from_mask (available_data_classes);
1718 cellular_class_str = mbim_cellular_class_build_string_from_mask (cellular_class);
1719 registration_flag_str = mbim_registration_flag_build_string_from_mask (registration_flag);
1720
1721 g_print ("[%s] Registration status:\n"
1722 "\t Network error: '%s'\n"
1723 "\t Register state: '%s'\n"
1724 "\t Register mode: '%s'\n"
1725 "\tAvailable data classes: '%s'\n"
1726 "\tCurrent cellular class: '%s'\n"
1727 "\t Provider ID: '%s'\n"
1728 "\t Provider name: '%s'\n"
1729 "\t Roaming text: '%s'\n"
1730 "\t Registration flags: '%s'\n",
1731 mbim_device_get_path_display (device),
1732 VALIDATE_UNKNOWN (mbim_nw_error_get_string (nw_error)),
1733 VALIDATE_UNKNOWN (mbim_register_state_get_string (register_state)),
1734 VALIDATE_UNKNOWN (mbim_register_mode_get_string (register_mode)),
1735 VALIDATE_UNKNOWN (available_data_classes_str),
1736 VALIDATE_UNKNOWN (cellular_class_str),
1737 VALIDATE_UNKNOWN (provider_id),
1738 VALIDATE_UNKNOWN (provider_name),
1739 VALIDATE_UNKNOWN (roaming_text),
1740 VALIDATE_UNKNOWN (registration_flag_str));
1741
1742 if (mbim_device_check_ms_mbimex_version (device, 2, 0)) {
1743 g_autofree gchar *preferred_data_classes_str = NULL;
1744
1745 preferred_data_classes_str = mbim_data_class_build_string_from_mask (preferred_data_classes);
1746 g_print ("\tPreferred data classes: '%s'\n",
1747 VALIDATE_UNKNOWN (preferred_data_classes_str));
1748 }
1749
1750 shutdown (TRUE);
1751 }
1752
1753 typedef struct {
1754 gchar *provider_id;
1755 MbimRegisterAction action;
1756 MbimDataClass data_class;
1757 } RegisterStateProperties;
1758
1759 static void
1760 register_state_properties_clear (RegisterStateProperties *props)
1761 {
1762 g_free (props->provider_id);
1763 }
1764
1765 G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(RegisterStateProperties, register_state_properties_clear);
1766
1767 static gboolean
1768 set_register_state_foreach_cb (const gchar *key,
1769 const gchar *value,
1770 GError **error,
1771 RegisterStateProperties *props)
1772 {
1773 if (g_ascii_strcasecmp (key, "action") == 0) {
1774 if (!mbimcli_read_register_action_from_string (value, &props->action, error)) {
1775 return FALSE;
1776 }
1777 } else if (g_ascii_strcasecmp (key, "provider-id") == 0) {
1778 g_free (props->provider_id);
1779 props->provider_id = g_strdup (value);
1780 } else if (g_ascii_strcasecmp (key, "data-class") == 0) {
1781 if (!mbimcli_read_data_class_mask_from_string (value, &props->data_class, error)) {
1782 return FALSE;
1783 }
1784 } else {
1785 g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_FAILED,
1786 "unrecognized option '%s'", key);
1787 return FALSE;
1788 }
1789
1790 return TRUE;
1791 }
1792
1793 static void
1794 signal_state_ready (MbimDevice *device,
1795 GAsyncResult *res)
1796 {
1797 g_autoptr(MbimMessage) response = NULL;
1798 g_autoptr(GError) error = NULL;
1799 guint32 rssi;
1800 guint32 error_rate;
1801 guint32 signal_strength_interval;
1802 guint32 rssi_threshold;
1803 guint32 error_rate_threshold;
1804 guint32 rsrp_snr_count;
1805 g_autoptr(MbimRsrpSnrInfoArray) rsrp_snr = NULL;
1806
1807 response = mbim_device_command_finish (device, res, &error);
1808 if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
1809 g_printerr ("error: operation failed: %s\n", error->message);
1810 shutdown (FALSE);
1811 return;
1812 }
1813
1814 /* MBIMEx 2.0 support */
1815 if (mbim_device_check_ms_mbimex_version (device, 2, 0)) {
1816 if (!mbim_message_ms_basic_connect_v2_signal_state_response_parse (response,
1817 &rssi,
1818 &error_rate,
1819 &signal_strength_interval,
1820 &rssi_threshold,
1821 &error_rate_threshold,
1822 &rsrp_snr_count,
1823 &rsrp_snr,
1824 &error)) {
1825 g_printerr ("error: couldn't parse response message: %s\n", error->message);
1826 shutdown (FALSE);
1827 return;
1828 }
1829 }
1830 /* MBIM 1.0 support */
1831 else {
1832 if (!mbim_message_signal_state_response_parse (response,
1833 &rssi,
1834 &error_rate,
1835 &signal_strength_interval,
1836 &rssi_threshold,
1837 &error_rate_threshold,
1838 &error)) {
1839 g_printerr ("error: couldn't parse response message: %s\n", error->message);
1840 shutdown (FALSE);
1841 return;
1842 }
1843 }
1844
1845 g_print ("[%s] Signal state:\n"
1846 "\t RSSI [0-31,99]: '%u'\n"
1847 "\t Error rate [0-7,99]: '%u'\n"
1848 "\tSignal strength interval: '%u'\n"
1849 "\t RSSI threshold: '%u'\n",
1850 mbim_device_get_path_display (device),
1851 rssi,
1852 error_rate,
1853 signal_strength_interval,
1854 rssi_threshold);
1855
1856 if (error_rate_threshold == 0xFFFFFFFF)
1857 g_print ("\t Error rate threshold: 'unspecified'\n");
1858 else
1859 g_print ("\t Error rate threshold: '%u'\n", error_rate_threshold);
1860
1861 if (mbim_device_check_ms_mbimex_version (device, 2, 0)) {
1862 g_print ("\n");
1863 if (rsrp_snr_count == 0) {
1864 g_print ("[%s] RSRP/SNR info: 'n/a'\n", mbim_device_get_path_display (device));
1865 } else {
1866 guint i;
1867
1868 for (i = 0; i < rsrp_snr_count; i++) {
1869 g_autofree gchar *system_type_str = NULL;
1870 MbimRsrpSnrInfo *info;
1871
1872 info = rsrp_snr[i];
1873
1874 system_type_str = mbim_data_class_build_string_from_mask (info->system_type);
1875 g_print ("[%s] RSRP/SNR info: '%s'\n",
1876 mbim_device_get_path_display (device),
1877 system_type_str);
1878
1879 if (info->rsrp >= 127)
1880 g_print ("\t RSRP: 'unknown'\n");
1881 else
1882 g_print ("\t RSRP: '%d dBm'\n", -157 + info->rsrp);
1883
1884 if (info->snr >= 128)
1885 g_print ("\t SNR: 'unknown'\n");
1886 else
1887 g_print ("\t SNR: '%.1lf dB'\n", -23.5 + (info->snr * 0.5));
1888
1889 if (info->rsrp_threshold == 0)
1890 g_print ("\t RSRP threshold: 'default'\n");
1891 else if (info->rsrp_threshold == 0xFFFFFFFF)
1892 g_print ("\t RSRP threshold: 'unspecified'\n");
1893 else
1894 g_print ("\t RSRP threshold: '%u'\n", info->rsrp_threshold);
1895
1896 if (info->snr_threshold == 0)
1897 g_print ("\t SNR threshold: 'default'\n");
1898 else if (info->snr_threshold == 0xFFFFFFFF)
1899 g_print ("\t SNR threshold: 'unspecified'\n");
1900 else
1901 g_print ("\t SNR threshold: '%u'\n", info->snr_threshold);
1902 g_print ("\n");
1903 }
1904 }
1905 }
1906
1907 shutdown (TRUE);
1908 }
1909
1910 enum {
1911 PACKET_SERVICE_STATUS,
1912 PACKET_SERVICE_ATTACH,
1913 PACKET_SERVICE_DETACH
1914 };
1915
1916 static void
1917 packet_service_ready (MbimDevice *device,
1918 GAsyncResult *res,
1919 gpointer user_data)
1920 {
1921 g_autoptr(MbimMessage) response = NULL;
1922 g_autoptr(GError) error = NULL;
1923 guint32 nw_error;
1924 MbimPacketServiceState packet_service_state;
1925 MbimDataClass highest_available_data_class;
1926 MbimDataClassV3 highest_available_data_class_v3;
1927 g_autofree gchar *highest_available_data_class_str = NULL;
1928 guint64 uplink_speed;
1929 guint64 downlink_speed;
1930 MbimFrequencyRange frequency_range;
1931 MbimDataSubclass data_subclass;
1932 g_autoptr(MbimTai) tai = NULL;
1933
1934 response = mbim_device_command_finish (device, res, &error);
1935 if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
1936 g_printerr ("error: operation failed: %s\n", error->message);
1937 shutdown (FALSE);
1938 return;
1939 }
1940
1941 /* MBIMEx 3.0 support */
1942 if (mbim_device_check_ms_mbimex_version (device, 3, 0)) {
1943 if (!mbim_message_ms_basic_connect_v3_packet_service_response_parse (response,
1944 &nw_error,
1945 &packet_service_state,
1946 &highest_available_data_class_v3,
1947 &uplink_speed,
1948 &downlink_speed,
1949 &frequency_range,
1950 &data_subclass,
1951 &tai,
1952 &error)) {
1953 g_printerr ("error: couldn't parse response message: %s\n", error->message);
1954 shutdown (FALSE);
1955 return;
1956 }
1957 g_debug ("Successfully parsed response as MBIM 3.0 Packet Service");
1958 }
1959 /* MBIMEx 2.0 support */
1960 else if (mbim_device_check_ms_mbimex_version (device, 2, 0)) {
1961 if (!mbim_message_ms_basic_connect_v2_packet_service_response_parse (response,
1962 &nw_error,
1963 &packet_service_state,
1964 &highest_available_data_class,
1965 &uplink_speed,
1966 &downlink_speed,
1967 &frequency_range,
1968 &error)) {
1969 g_printerr ("error: couldn't parse response message: %s\n", error->message);
1970 shutdown (FALSE);
1971 return;
1972 }
1973 g_debug ("Successfully parsed response as MBIM 2.0 Packet Service");
1974 }
1975 /* MBIM 1.0 support */
1976 else {
1977 if (!mbim_message_packet_service_response_parse (response,
1978 &nw_error,
1979 &packet_service_state,
1980 &highest_available_data_class,
1981 &uplink_speed,
1982 &downlink_speed,
1983 &error)) {
1984 g_printerr ("error: couldn't parse response message: %s\n", error->message);
1985 shutdown (FALSE);
1986 return;
1987 }
1988 g_debug ("Successfully parsed response as MBIM 1.0 Packet Service");
1989 }
1990
1991 switch (GPOINTER_TO_UINT (user_data)) {
1992 case PACKET_SERVICE_ATTACH:
1993 g_print ("[%s] Successfully attached to packet service\n\n",
1994 mbim_device_get_path_display (device));
1995 break;
1996 case PACKET_SERVICE_DETACH:
1997 g_print ("[%s] Successfully detached from packet service\n\n",
1998 mbim_device_get_path_display (device));
1999 break;
2000 default:
2001 break;
2002 }
2003
2004 if (mbim_device_check_ms_mbimex_version (device, 3, 0))
2005 highest_available_data_class_str = mbim_data_class_v3_build_string_from_mask (highest_available_data_class_v3);
2006 else
2007 highest_available_data_class_str = mbim_data_class_build_string_from_mask (highest_available_data_class);
2008
2009 g_print ("[%s] Packet service status:\n"
2010 "\t Network error: '%s'\n"
2011 "\t Packet service state: '%s'\n"
2012 "\tAvailable data classes: '%s'\n"
2013 "\t Uplink speed: '%" G_GUINT64_FORMAT " bps'\n"
2014 "\t Downlink speed: '%" G_GUINT64_FORMAT " bps'\n",
2015 mbim_device_get_path_display (device),
2016 VALIDATE_UNKNOWN (mbim_nw_error_get_string (nw_error)),
2017 VALIDATE_UNKNOWN (mbim_packet_service_state_get_string (packet_service_state)),
2018 VALIDATE_UNKNOWN (highest_available_data_class_str),
2019 uplink_speed,
2020 downlink_speed);
2021
2022 if (mbim_device_check_ms_mbimex_version (device, 2, 0)) {
2023 g_autofree gchar *frequency_range_str = NULL;
2024
2025 frequency_range_str = mbim_frequency_range_build_string_from_mask (frequency_range);
2026 g_print ("\t Frequency range: '%s'\n",
2027 VALIDATE_UNKNOWN (frequency_range_str));
2028 }
2029
2030 if (mbim_device_check_ms_mbimex_version (device, 3, 0)) {
2031 g_autofree gchar *data_subclass_str = NULL;
2032 g_autofree gchar *tai_plmn_mcc_str = NULL;
2033 g_autofree gchar *tai_plmn_mnc_str = NULL;
2034
2035 /* Mobile Country Code of 3 decimal digits; The
2036 * least significant 12 bits contains BCD-encoded
2037 * 3 decimal digits sequentially for the MCC, with
2038 * the last digit of the MCC in the least significant
2039 * 4 bits. The unused bits in the UINT16 integer
2040 * must be zeros. */
2041 tai_plmn_mcc_str = g_strdup_printf ("%03x", tai->plmn_mcc & 0x0FFF);
2042
2043 /* Mobile Network Code of either 3 or 2 decimal
2044 * digits; The most significant bit indicates
2045 * whether the MNC has 2 decimal digits or 3
2046 * decimal digits. If this bit has 1, the MNC has 2
2047 * decimal digits and the least significant 8 bits
2048 * contains them in BCD-encoded form
2049 * sequentially, with the last digit of the MNC in
2050 * the least significant 4 bits. If the most
2051 * significant bit has 0, the MNC has 3 decimal
2052 * digits and the least significant 12 bits contains
2053 * them in BCD-encoded form sequentially, with
2054 * the last digit of the MNC in the least
2055 * esignificant 4 bits. The unused bits in the
2056 * UINT16 integer must be zeros. */
2057 if (tai->plmn_mnc & 0x8000)
2058 tai_plmn_mnc_str = g_strdup_printf ("%02x", tai->plmn_mnc & 0x00FF);
2059 else
2060 tai_plmn_mnc_str = g_strdup_printf ("%03x", tai->plmn_mnc & 0x0FFF);
2061
2062 data_subclass_str = mbim_data_subclass_build_string_from_mask (data_subclass);
2063 g_print ("\t Data sub class: '%s'\n"
2064 "\t TAI PLMN MCC: '%s'\n"
2065 "\t TAI PLMN MNC: '%s'\n"
2066 "\t TAI TAC: '%u'\n",
2067 VALIDATE_UNKNOWN (data_subclass_str),
2068 tai_plmn_mcc_str,
2069 tai_plmn_mnc_str,
2070 tai->tac);
2071 }
2072
2073 shutdown (TRUE);
2074 }
2075
2076 static void
2077 packet_statistics_ready (MbimDevice *device,
2078 GAsyncResult *res)
2079 {
2080 g_autoptr(MbimMessage) response = NULL;
2081 g_autoptr(GError) error = NULL;
2082 guint32 in_discards;
2083 guint32 in_errors;
2084 guint64 in_octets;
2085 guint64 in_packets;
2086 guint64 out_octets;
2087 guint64 out_packets;
2088 guint32 out_errors;
2089 guint32 out_discards;
2090
2091 response = mbim_device_command_finish (device, res, &error);
2092 if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
2093 g_printerr ("error: operation failed: %s\n", error->message);
2094 shutdown (FALSE);
2095 return;
2096 }
2097
2098 if (!mbim_message_packet_statistics_response_parse (response,
2099 &in_discards,
2100 &in_errors,
2101 &in_octets,
2102 &in_packets,
2103 &out_octets,
2104 &out_packets,
2105 &out_errors,
2106 &out_discards,
2107 &error)) {
2108 g_printerr ("error: couldn't parse response message: %s\n", error->message);
2109 shutdown (FALSE);
2110 return;
2111 }
2112
2113 g_print ("[%s] Packet statistics:\n"
2114 "\t Octets (in): '%" G_GUINT64_FORMAT "'\n"
2115 "\t Packets (in): '%" G_GUINT64_FORMAT "'\n"
2116 "\t Discards (in): '%u'\n"
2117 "\t Errors (in): '%u'\n"
2118 "\t Octets (out): '%" G_GUINT64_FORMAT "'\n"
2119 "\t Packets (out): '%" G_GUINT64_FORMAT "'\n"
2120 "\tDiscards (out): '%u'\n"
2121 "\t Errors (out): '%u'\n",
2122 mbim_device_get_path_display (device),
2123 in_octets,
2124 in_packets,
2125 in_discards,
2126 in_errors,
2127 out_octets,
2128 out_packets,
2129 out_discards,
2130 out_errors);
2131
2132 shutdown (TRUE);
2133 }
2134
2135 static void
2136 provisioned_contexts_ready (MbimDevice *device,
2137 GAsyncResult *res)
2138 {
2139 g_autoptr(MbimMessage) response = NULL;
2140 g_autoptr(MbimProvisionedContextElementArray) provisioned_contexts = NULL;
2141 g_autoptr(GError) error = NULL;
2142 guint32 provisioned_contexts_count;
2143 guint i;
2144
2145 response = mbim_device_command_finish (device, res, &error);
2146 if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
2147 g_printerr ("error: operation failed: %s\n", error->message);
2148 shutdown (FALSE);
2149 return;
2150 }
2151
2152 if (!mbim_message_provisioned_contexts_response_parse (
2153 response,
2154 &provisioned_contexts_count,
2155 &provisioned_contexts,
2156 &error)) {
2157 g_printerr ("error: couldn't parse response message: %s\n", error->message);
2158 shutdown (FALSE);
2159 return;
2160 }
2161
2162 g_print ("[%s] Provisioned contexts (%u):\n",
2163 mbim_device_get_path_display (device),
2164 provisioned_contexts_count);
2165
2166 for (i = 0; i < provisioned_contexts_count; i++) {
2167 g_print ("\tContext ID %u:\n"
2168 "\t Context type: '%s'\n"
2169 "\t Access string: '%s'\n"
2170 "\t Username: '%s'\n"
2171 "\t Password: '%s'\n"
2172 "\t Compression: '%s'\n"
2173 "\t Auth protocol: '%s'\n",
2174 provisioned_contexts[i]->context_id,
2175 VALIDATE_UNKNOWN (mbim_context_type_get_string (
2176 mbim_uuid_to_context_type (&provisioned_contexts[i]->context_type))),
2177 VALIDATE_EMPTY (provisioned_contexts[i]->access_string),
2178 VALIDATE_EMPTY (provisioned_contexts[i]->user_name),
2179 VALIDATE_EMPTY (provisioned_contexts[i]->password),
2180 VALIDATE_UNKNOWN (mbim_compression_get_string (
2181 provisioned_contexts[i]->compression)),
2182 VALIDATE_UNKNOWN (mbim_auth_protocol_get_string (
2183 provisioned_contexts[i]->auth_protocol)));
2184 }
2185
2186 shutdown (TRUE);
2187 }
2188
2189 typedef struct {
2190 guint context_id;
2191 MbimCompression compression;
2192 MbimAuthProtocol auth_protocol;
2193 MbimContextType context_type;
2194 gchar *access_string;
2195 gchar *username;
2196 gchar *password;
2197 gchar *provider_id;
2198 } ProvisionedContextProperties;
2199
2200 static void
2201 provisioned_context_properties_clear (ProvisionedContextProperties *props)
2202 {
2203 g_free (props->access_string);
2204 g_free (props->username);
2205 g_free (props->password);
2206 g_free (props->provider_id);
2207 }
2208
2209 G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ProvisionedContextProperties, provisioned_context_properties_clear)
2210
2211 static gboolean
2212 set_provisioned_contexts_foreach_cb (const gchar *key,
2213 const gchar *value,
2214 GError **error,
2215 ProvisionedContextProperties *props)
2216 {
2217 if (g_ascii_strcasecmp (key, "context-id") == 0) {
2218 if (!mbimcli_read_uint_from_string (value, &props->context_id)) {
2219 g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_ARGS,
2220 "Couldn't parse context-id as integer : '%s'", value);
2221 return FALSE;
2222 }
2223 } else if (g_ascii_strcasecmp (key, "context-type") == 0) {
2224 if (!mbimcli_read_context_type_from_string (value, &props->context_type, error)) {
2225 return FALSE;
2226 }
2227 } else if (g_ascii_strcasecmp (key, "auth") == 0) {
2228 if (!mbimcli_read_auth_protocol_from_string (value, &props->auth_protocol, error)) {
2229 return FALSE;
2230 }
2231 } else if (g_ascii_strcasecmp (key, "compression") == 0) {
2232 if (!mbimcli_read_compression_from_string (value, &props->compression, error)) {
2233 return FALSE;
2234 }
2235 } else if (g_ascii_strcasecmp (key, "username") == 0) {
2236 g_free (props->username);
2237 props->username = g_strdup (value);
2238 } else if (g_ascii_strcasecmp (key, "password") == 0) {
2239 g_free (props->password);
2240 props->password = g_strdup (value);
2241 } else if (g_ascii_strcasecmp (key, "access-string") == 0) {
2242 g_free (props->access_string);
2243 props->access_string = g_strdup (value);
2244 } else if (g_ascii_strcasecmp (key, "provider-id") == 0) {
2245 g_free (props->provider_id);
2246 props->provider_id = g_strdup (value);
2247 } else {
2248 g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_FAILED,
2249 "unrecognized option '%s'", key);
2250 return FALSE;
2251 }
2252
2253 return TRUE;
2254 }
2255
2256 typedef struct {
2257 guint32 signal_strength;
2258 guint32 rssi;
2259 guint32 error_rate;
2260 } SignalStateProperties;
2261
2262 static gboolean
2263 set_signal_state_foreach_cb (const gchar *key,
2264 const gchar *value,
2265 GError **error,
2266 SignalStateProperties *props)
2267 {
2268 if (g_ascii_strcasecmp (key, "signal-strength-interval") == 0) {
2269 if (!mbimcli_read_uint_from_string (value, &props->signal_strength)) {
2270 g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_ARGS,
2271 "Couldn't parse signal-strength as integer : '%s'", value);
2272 return FALSE;
2273 }
2274 } else if (g_ascii_strcasecmp (key, "rssi-threshold") == 0) {
2275 if (!mbimcli_read_uint_from_string (value, &props->rssi)) {
2276 g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_ARGS,
2277 "Couldn't parse rssi as integer : '%s'", value);
2278 return FALSE;
2279 }
2280 } else if (g_ascii_strcasecmp (key, "error-rate-threshold") == 0) {
2281 if (!mbimcli_read_uint_from_string (value, &props->error_rate)) {
2282 g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_ARGS,
2283 "Couldn't parse error-rate as integer : '%s'", value);
2284 return FALSE;
2285 }
2286 } else {
2287 g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_FAILED,
2288 "unrecognized option '%s'", key);
2289 return FALSE;
2290 }
2291
2292 return TRUE;
2293 }
2294
2295 static void
2296 network_idle_hint_ready (MbimDevice *device,
2297 GAsyncResult *res)
2298 {
2299 g_autoptr(MbimMessage) response = NULL;
2300 g_autoptr(GError) error = NULL;
2301 MbimNetworkIdleHintState network_state;
2302 const gchar *network_state_str = NULL;
2303
2304 response = mbim_device_command_finish (device, res, &error);
2305 if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
2306 g_printerr ("error: operation failed: %s\n", error->message);
2307 shutdown (FALSE);
2308 return;
2309 }
2310
2311 if (!mbim_message_network_idle_hint_response_parse (
2312 response,
2313 &network_state,
2314 &error)) {
2315 g_printerr ("error: couldn't parse response message: %s\n", error->message);
2316 shutdown (FALSE);
2317 return;
2318 }
2319
2320 network_state_str = mbim_network_idle_hint_state_get_string (network_state);
2321 g_print ("[%s] Network idle hint state: '%s'\n",
2322 mbim_device_get_path_display (device),
2323 VALIDATE_UNKNOWN (network_state_str));
2324
2325 shutdown (TRUE);
2326 }
2327
2328 static void
2329 emergency_mode_ready (MbimDevice *device,
2330 GAsyncResult *res)
2331 {
2332 g_autoptr(MbimMessage) response = NULL;
2333 g_autoptr(GError) error = NULL;
2334 MbimEmergencyModeState emergency_state;
2335 const gchar *emergency_state_str = NULL;
2336
2337 response = mbim_device_command_finish (device, res, &error);
2338 if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
2339 g_printerr ("error: operation failed: %s\n", error->message);
2340 shutdown (FALSE);
2341 return;
2342 }
2343
2344 if (!mbim_message_emergency_mode_response_parse (
2345 response,
2346 &emergency_state,
2347 &error)) {
2348 g_printerr ("error: couldn't parse response message: %s\n", error->message);
2349 shutdown (FALSE);
2350 return;
2351 }
2352
2353 emergency_state_str = mbim_emergency_mode_state_get_string (emergency_state);
2354 g_print ("[%s] Emergency mode: '%s'\n",
2355 mbim_device_get_path_display (device),
2356 VALIDATE_UNKNOWN (emergency_state_str));
2357
2358 shutdown (TRUE);
2359 }
2360
2361 static void
2362 set_service_activation_ready (MbimDevice *device,
2363 GAsyncResult *res)
2364 {
2365 g_autoptr(MbimMessage) response = NULL;
2366 g_autoptr(GError) error = NULL;
2367 MbimNwError nw_error;
2368 guint32 result_data_size;
2369 const guint8 *result_data = NULL;
2370 g_autofree gchar *result_data_str = NULL;
2371
2372 response = mbim_device_command_finish (device, res, &error);
2373 if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
2374 g_printerr ("error: operation failed: %s\n", error->message);
2375 shutdown (FALSE);
2376 return;
2377 }
2378
2379 if (!mbim_message_service_activation_response_parse (response,
2380 &nw_error,
2381 &result_data_size,
2382 &result_data,
2383 &error)) {
2384 g_printerr ("error: couldn't parse response message: %s\n", error->message);
2385 shutdown (FALSE);
2386 return;
2387 }
2388
2389 result_data_str = mbim_common_str_hex (result_data, result_data_size, ':');
2390 g_print ("[%s] Service activation response received successfully:\n"
2391 "\t Network error: '%s'\n"
2392 "\t Data: '%s'\n",
2393 mbim_device_get_path_display (device),
2394 VALIDATE_UNKNOWN (mbim_nw_error_get_string (nw_error)),
2395 result_data_str);
2396
2397 shutdown (TRUE);
2398 }
2399
2400 void
2401 mbimcli_basic_connect_run (MbimDevice *device,
2402 GCancellable *cancellable)
2403 {
2404 g_autoptr(MbimMessage) request = NULL;
2405 g_autoptr(GError) error = NULL;
2406
2407 /* Initialize context */
2408 ctx = g_slice_new (Context);
2409 ctx->device = g_object_ref (device);
2410 ctx->cancellable = cancellable ? g_object_ref (cancellable) : NULL;
2411
2412 /* Request to get capabilities? */
2413 if (query_device_caps_flag) {
2414 g_debug ("Asynchronously querying device capabilities...");
2415 request = (mbim_message_device_caps_query_new (NULL));
2416 mbim_device_command (ctx->device,
2417 request,
2418 10,
2419 ctx->cancellable,
2420 (GAsyncReadyCallback)query_device_caps_ready,
2421 NULL);
2422 return;
2423 }
2424
2425 /* Request to get subscriber ready status? */
2426 if (query_subscriber_ready_status_flag) {
2427 g_debug ("Asynchronously querying subscriber ready status...");
2428 request = (mbim_message_subscriber_ready_status_query_new (NULL));
2429 mbim_device_command (ctx->device,
2430 request,
2431 10,
2432 ctx->cancellable,
2433 (GAsyncReadyCallback)query_subscriber_ready_status_ready,
2434 NULL);
2435 return;
2436 }
2437
2438 /* Request to get radio state? */
2439 if (query_radio_state_flag) {
2440 g_debug ("Asynchronously querying radio state...");
2441 request = (mbim_message_radio_state_query_new (NULL));
2442 mbim_device_command (ctx->device,
2443 request,
2444 10,
2445 ctx->cancellable,
2446 (GAsyncReadyCallback)query_radio_state_ready,
2447 NULL);
2448 return;
2449 }
2450
2451 /* Request to set radio state? */
2452 if (set_radio_state_str) {
2453 MbimRadioSwitchState radio_state;
2454
2455 if (g_ascii_strcasecmp (set_radio_state_str, "on") == 0) {
2456 radio_state = MBIM_RADIO_SWITCH_STATE_ON;
2457 } else if (g_ascii_strcasecmp (set_radio_state_str, "off") == 0) {
2458 radio_state = MBIM_RADIO_SWITCH_STATE_OFF;
2459 } else {
2460 g_printerr ("error: invalid radio state: '%s'\n", set_radio_state_str);
2461 shutdown (FALSE);
2462 return;
2463 }
2464
2465 g_debug ("Asynchronously setting radio state to %s...",
2466 radio_state == MBIM_RADIO_SWITCH_STATE_ON ? "on" : "off");
2467 request = mbim_message_radio_state_set_new (radio_state, NULL);
2468 mbim_device_command (ctx->device,
2469 request,
2470 10,
2471 ctx->cancellable,
2472 (GAsyncReadyCallback)query_radio_state_ready,
2473 NULL);
2474 return;
2475 }
2476
2477 /* Request to query device services? */
2478 if (query_device_services_flag) {
2479 g_debug ("Asynchronously querying device services...");
2480 request = (mbim_message_device_services_query_new (NULL));
2481 mbim_device_command (ctx->device,
2482 request,
2483 10,
2484 ctx->cancellable,
2485 (GAsyncReadyCallback)query_device_services_ready,
2486 NULL);
2487 return;
2488 }
2489
2490 /* Query PIN state? */
2491 if (query_pin_flag) {
2492 g_debug ("Asynchronously querying PIN state...");
2493 request = (mbim_message_pin_query_new (NULL));
2494 mbim_device_command (ctx->device,
2495 request,
2496 10,
2497 ctx->cancellable,
2498 (GAsyncReadyCallback)pin_ready,
2499 GUINT_TO_POINTER (FALSE));
2500 return;
2501 }
2502
2503 /* Set PIN? */
2504 if (set_pin_enter_str ||
2505 set_pin_change_str ||
2506 set_pin_enable_str ||
2507 set_pin_disable_str ||
2508 set_pin_enter_puk_str) {
2509 MbimPinType pin_type;
2510 MbimPinOperation pin_operation;
2511 g_autofree gchar *pin = NULL;
2512 g_autofree gchar *new_pin = NULL;
2513
2514 if (set_pin_enter_puk_str) {
2515 g_debug ("Asynchronously entering PUK...");
2516 pin_type = MBIM_PIN_TYPE_PUK1;
2517 pin_operation = MBIM_PIN_OPERATION_ENTER;
2518 set_pin_input_parse (set_pin_enter_puk_str, &pin, &new_pin, &pin_type);
2519 } else {
2520 pin_type = MBIM_PIN_TYPE_PIN1;
2521 if (set_pin_change_str) {
2522 g_debug ("Asynchronously changing PIN...");
2523 pin_operation = MBIM_PIN_OPERATION_CHANGE;
2524 set_pin_input_parse (set_pin_change_str, &pin, &new_pin, NULL);
2525 } else if (set_pin_enable_str) {
2526 g_debug ("Asynchronously enabling PIN...");
2527 pin_operation = MBIM_PIN_OPERATION_ENABLE;
2528 set_pin_input_parse (set_pin_enable_str, &pin, NULL, NULL);
2529 } else if (set_pin_disable_str) {
2530 g_debug ("Asynchronously disabling PIN...");
2531 pin_operation = MBIM_PIN_OPERATION_DISABLE;
2532 set_pin_input_parse (set_pin_disable_str, &pin, NULL, &pin_type);
2533 } else if (set_pin_enter_str) {
2534 g_debug ("Asynchronously entering PIN...");
2535 pin_operation = MBIM_PIN_OPERATION_ENTER;
2536 set_pin_input_parse (set_pin_enter_str, &pin, NULL, &pin_type);
2537 } else
2538 g_assert_not_reached ();
2539 }
2540
2541 if (!pin || pin_type == MBIM_PIN_TYPE_UNKNOWN) {
2542 shutdown (FALSE);
2543 return;
2544 }
2545
2546 request = (mbim_message_pin_set_new (pin_type,
2547 pin_operation,
2548 pin,
2549 new_pin,
2550 &error));
2551 if (!request) {
2552 g_printerr ("error: couldn't create request: %s\n", error->message);
2553 shutdown (FALSE);
2554 return;
2555 }
2556
2557 mbim_device_command (ctx->device,
2558 request,
2559 10,
2560 ctx->cancellable,
2561 (GAsyncReadyCallback)pin_ready,
2562 GUINT_TO_POINTER (TRUE));
2563 return;
2564 }
2565
2566 /* Query PIN list? */
2567 if (query_pin_list_flag) {
2568 g_debug ("Asynchronously querying PIN list...");
2569 request = (mbim_message_pin_list_query_new (NULL));
2570 mbim_device_command (ctx->device,
2571 request,
2572 10,
2573 ctx->cancellable,
2574 (GAsyncReadyCallback)pin_list_ready,
2575 GUINT_TO_POINTER (FALSE));
2576 return;
2577 }
2578
2579 /* Query home provider? */
2580 if (query_home_provider_flag) {
2581 request = mbim_message_home_provider_query_new (NULL);
2582 mbim_device_command (ctx->device,
2583 request,
2584 10,
2585 ctx->cancellable,
2586 (GAsyncReadyCallback)home_provider_ready,
2587 NULL);
2588 return;
2589 }
2590
2591 /* Query preferred providers? */
2592 if (query_preferred_providers_flag) {
2593 request = mbim_message_preferred_providers_query_new (NULL);
2594 mbim_device_command (ctx->device,
2595 request,
2596 10,
2597 ctx->cancellable,
2598 (GAsyncReadyCallback)preferred_providers_ready,
2599 NULL);
2600 return;
2601 }
2602
2603 /* Query visible providers? */
2604 if (query_visible_providers_flag) {
2605 request = mbim_message_visible_providers_query_new (MBIM_VISIBLE_PROVIDERS_ACTION_FULL_SCAN, NULL);
2606 mbim_device_command (ctx->device,
2607 request,
2608 240, /* longer timeout, needs to scan */
2609 ctx->cancellable,
2610 (GAsyncReadyCallback)visible_providers_ready,
2611 NULL);
2612 return;
2613 }
2614
2615 /* Query register state? */
2616 if (query_register_state_flag || query_registration_state_flag) {
2617 request = mbim_message_register_state_query_new (NULL);
2618 mbim_device_command (ctx->device,
2619 request,
2620 10,
2621 ctx->cancellable,
2622 (GAsyncReadyCallback)register_state_ready,
2623 GUINT_TO_POINTER (FALSE));
2624 return;
2625 }
2626
2627 /* Update register state? */
2628 if (set_register_state_str || register_automatic_flag) {
2629 /* defaults for --register-automatic */
2630 g_auto(RegisterStateProperties) props = {
2631 .provider_id = NULL,
2632 .action = MBIM_REGISTER_ACTION_AUTOMATIC,
2633 .data_class = 0,
2634 };
2635
2636 if (set_register_state_str) {
2637 if (!mbimcli_parse_key_value_string (set_register_state_str,
2638 &error,
2639 (MbimParseKeyValueForeachFn)set_register_state_foreach_cb,
2640 &props)) {
2641 g_printerr ("error: couldn't parse input string: %s\n", error->message);
2642 shutdown (FALSE);
2643 return;
2644 }
2645 }
2646
2647 request = mbim_message_register_state_set_new (props.provider_id, props.action, props.data_class, &error);
2648 if (!request) {
2649 g_printerr ("error: couldn't create request: %s\n", error->message);
2650 shutdown (FALSE);
2651 return;
2652 }
2653
2654 mbim_device_command (ctx->device,
2655 request,
2656 120, /* longer timeout, needs to look for the home network */
2657 ctx->cancellable,
2658 (GAsyncReadyCallback)register_state_ready,
2659 GUINT_TO_POINTER (TRUE));
2660 return;
2661 }
2662
2663 /* Query signal status? */
2664 if (query_signal_state_flag) {
2665 request = mbim_message_signal_state_query_new (NULL);
2666 mbim_device_command (ctx->device,
2667 request,
2668 10,
2669 ctx->cancellable,
2670 (GAsyncReadyCallback)signal_state_ready,
2671 NULL);
2672 return;
2673 }
2674
2675 /* Query packet service status? */
2676 if (query_packet_service_flag) {
2677 request = mbim_message_packet_service_query_new (NULL);
2678 mbim_device_command (ctx->device,
2679 request,
2680 10,
2681 ctx->cancellable,
2682 (GAsyncReadyCallback)packet_service_ready,
2683 GUINT_TO_POINTER (PACKET_SERVICE_STATUS));
2684 return;
2685 }
2686
2687 /* Launch packet attach or detach? */
2688 if (set_packet_service_attach_flag ||
2689 set_packet_service_detach_flag) {
2690 MbimPacketServiceAction action;
2691
2692 if (set_packet_service_attach_flag)
2693 action = MBIM_PACKET_SERVICE_ACTION_ATTACH;
2694 else if (set_packet_service_detach_flag)
2695 action = MBIM_PACKET_SERVICE_ACTION_DETACH;
2696 else
2697 g_assert_not_reached ();
2698
2699 request = mbim_message_packet_service_set_new (action, &error);
2700 if (!request) {
2701 g_printerr ("error: couldn't create request: %s\n", error->message);
2702 shutdown (FALSE);
2703 return;
2704 }
2705
2706 mbim_device_command (ctx->device,
2707 request,
2708 120,
2709 ctx->cancellable,
2710 (GAsyncReadyCallback)packet_service_ready,
2711 GUINT_TO_POINTER (set_packet_service_attach_flag ?
2712 PACKET_SERVICE_ATTACH :
2713 PACKET_SERVICE_DETACH));
2714 return;
2715 }
2716
2717 /* Query connection status? */
2718 if (query_connect_str) {
2719 guint32 session_id = 0;
2720
2721 if (!connect_session_id_parse (query_connect_str, TRUE, &session_id, &error)) {
2722 g_printerr ("error: couldn't parse session ID: %s\n", error->message);
2723 shutdown (FALSE);
2724 return;
2725 }
2726
2727 if (mbim_device_check_ms_mbimex_version (device, 3, 0)) {
2728 request = mbim_message_ms_basic_connect_v3_connect_query_new (session_id,
2729 &error);
2730 } else {
2731 request = mbim_message_connect_query_new (session_id,
2732 MBIM_ACTIVATION_STATE_UNKNOWN,
2733 MBIM_VOICE_CALL_STATE_NONE,
2734 MBIM_CONTEXT_IP_TYPE_DEFAULT,
2735 mbim_uuid_from_context_type (MBIM_CONTEXT_TYPE_INTERNET),
2736 0,
2737 &error);
2738 }
2739 if (!request) {
2740 g_printerr ("error: couldn't create request: %s\n", error->message);
2741 shutdown (FALSE);
2742 return;
2743 }
2744
2745 mbim_device_command (ctx->device,
2746 request,
2747 10,
2748 ctx->cancellable,
2749 (GAsyncReadyCallback)connect_ready,
2750 GUINT_TO_POINTER (CONNECTION_STATUS));
2751 return;
2752 }
2753
2754 /* Connect? */
2755 if (set_connect_activate_str) {
2756 g_auto(ConnectActivateProperties) props = {
2757 .session_id = 0,
2758 .access_string = NULL,
2759 .auth_protocol = MBIM_AUTH_PROTOCOL_NONE,
2760 .username = NULL,
2761 .password = NULL,
2762 .ip_type = MBIM_CONTEXT_IP_TYPE_DEFAULT,
2763 .compression = MBIM_COMPRESSION_NONE,
2764 .context_type = MBIM_CONTEXT_TYPE_INTERNET,
2765 .media_type = MBIM_ACCESS_MEDIA_TYPE_UNKNOWN,
2766 };
2767
2768 if (!set_connect_activate_parse (set_connect_activate_str, &props, device)) {
2769 shutdown (FALSE);
2770 return;
2771 }
2772
2773 if (mbim_device_check_ms_mbimex_version (device, 3, 0)) {
2774 request = mbim_message_ms_basic_connect_v3_connect_set_new (props.session_id,
2775 MBIM_ACTIVATION_COMMAND_ACTIVATE,
2776 props.compression,
2777 props.auth_protocol,
2778 props.ip_type,
2779 mbim_uuid_from_context_type (props.context_type),
2780 props.media_type,
2781 props.access_string,
2782 props.username,
2783 props.password,
2784 NULL, /* unnamed IEs */
2785 &error);
2786 } else {
2787 request = mbim_message_connect_set_new (props.session_id,
2788 MBIM_ACTIVATION_COMMAND_ACTIVATE,
2789 props.access_string,
2790 props.username,
2791 props.password,
2792 props.compression,
2793 props.auth_protocol,
2794 props.ip_type,
2795 mbim_uuid_from_context_type (props.context_type),
2796 &error);
2797 }
2798
2799 if (!request) {
2800 g_printerr ("error: couldn't create request: %s\n", error->message);
2801 shutdown (FALSE);
2802 return;
2803 }
2804
2805 mbim_device_command (ctx->device,
2806 request,
2807 120,
2808 ctx->cancellable,
2809 (GAsyncReadyCallback)connect_ready,
2810 GUINT_TO_POINTER (CONNECT));
2811 return;
2812 }
2813
2814 /* Query IP configuration? */
2815 if (query_ip_configuration_str) {
2816 guint32 session_id = 0;
2817
2818 if (!connect_session_id_parse (query_ip_configuration_str, TRUE, &session_id, &error)) {
2819 g_printerr ("error: couldn't parse session ID: %s\n", error->message);
2820 shutdown (FALSE);
2821 return;
2822 }
2823
2824 ip_configuration_query (ctx->device, ctx->cancellable, session_id);
2825 return;
2826 }
2827
2828 /* Disconnect? */
2829 if (set_connect_deactivate_str) {
2830 guint32 session_id = 0;
2831
2832 if (!connect_session_id_parse (set_connect_deactivate_str, TRUE, &session_id, &error)) {
2833 g_printerr ("error: couldn't parse session ID: %s\n", error->message);
2834 shutdown (FALSE);
2835 return;
2836 }
2837
2838 if (mbim_device_check_ms_mbimex_version (device, 3, 0)) {
2839 request = mbim_message_ms_basic_connect_v3_connect_set_new (session_id,
2840 MBIM_ACTIVATION_COMMAND_DEACTIVATE,
2841 MBIM_COMPRESSION_NONE,
2842 MBIM_AUTH_PROTOCOL_NONE,
2843 MBIM_CONTEXT_IP_TYPE_DEFAULT,
2844 mbim_uuid_from_context_type (MBIM_CONTEXT_TYPE_INTERNET),
2845 MBIM_ACCESS_MEDIA_TYPE_UNKNOWN,
2846 NULL,
2847 NULL,
2848 NULL,
2849 NULL,
2850 &error);
2851 } else {
2852 request = mbim_message_connect_set_new (session_id,
2853 MBIM_ACTIVATION_COMMAND_DEACTIVATE,
2854 NULL,
2855 NULL,
2856 NULL,
2857 MBIM_COMPRESSION_NONE,
2858 MBIM_AUTH_PROTOCOL_NONE,
2859 MBIM_CONTEXT_IP_TYPE_DEFAULT,
2860 mbim_uuid_from_context_type (MBIM_CONTEXT_TYPE_INTERNET),
2861 &error);
2862 }
2863
2864 if (!request) {
2865 g_printerr ("error: couldn't create request: %s\n", error->message);
2866 shutdown (FALSE);
2867 return;
2868 }
2869
2870 mbim_device_command (ctx->device,
2871 request,
2872 60,
2873 ctx->cancellable,
2874 (GAsyncReadyCallback)connect_ready,
2875 GUINT_TO_POINTER (DISCONNECT));
2876 return;
2877 }
2878
2879 /* Packet statistics? */
2880 if (query_packet_statistics_flag) {
2881 request = mbim_message_packet_statistics_query_new (NULL);
2882 mbim_device_command (ctx->device,
2883 request,
2884 10,
2885 ctx->cancellable,
2886 (GAsyncReadyCallback)packet_statistics_ready,
2887 NULL);
2888 return;
2889 }
2890
2891 /* Query IP packet filters? */
2892 if (query_ip_packet_filters_str) {
2893 guint32 session_id = 0;
2894
2895 if (!connect_session_id_parse (query_ip_packet_filters_str, TRUE, &session_id, &error)) {
2896 g_printerr ("error: couldn't parse session ID: %s\n", error->message);
2897 shutdown (FALSE);
2898 return;
2899 }
2900
2901 if (mbim_device_check_ms_mbimex_version (device, 3, 0)) {
2902 g_debug ("Asychronously querying v3.0 IP packet filter......");
2903 request = mbim_message_ms_basic_connect_v3_ip_packet_filters_query_new (session_id, 0, NULL, &error);
2904 } else {
2905 g_debug ("Asychronously querying v1.0 IP packet filter......");
2906 request = mbim_message_ip_packet_filters_query_new (session_id, 0, NULL, &error);
2907 }
2908
2909 if (!request) {
2910 g_printerr ("error: couldn't create IP packet filters request: %s\n", error->message);
2911 shutdown (FALSE);
2912 return;
2913 }
2914
2915 mbim_device_command (ctx->device,
2916 request,
2917 10,
2918 ctx->cancellable,
2919 (GAsyncReadyCallback)ip_packet_filters_ready,
2920 NULL);
2921 return;
2922 }
2923
2924 /* Set IP packet filters? */
2925 if (set_ip_packet_filters_str) {
2926 g_auto(SetIpPacketFiltersProperties) props = {
2927 .v3 = FALSE,
2928 .session_id = 0,
2929 .array = NULL,
2930 };
2931
2932 props.v3 = mbim_device_check_ms_mbimex_version (device, 3, 0);
2933 if (props.v3)
2934 props.array = g_ptr_array_new_with_free_func ((GDestroyNotify)mbim_packet_filter_v3_free);
2935 else
2936 props.array = g_ptr_array_new_with_free_func ((GDestroyNotify)mbim_packet_filter_free);
2937
2938 if (!set_ip_packet_filters_parse (set_ip_packet_filters_str, &props, device)) {
2939 shutdown (FALSE);
2940 return;
2941 }
2942
2943 if (mbim_device_check_ms_mbimex_version (device, 3, 0)) {
2944 g_debug ("Asychronously set v3.0 IP packet filter......");
2945 request = mbim_message_ms_basic_connect_v3_ip_packet_filters_set_new (props.session_id,
2946 props.array->len,
2947 (const MbimPacketFilterV3 *const *)props.array->pdata,
2948 &error);
2949 } else {
2950 g_debug ("Asychronously set v1.0 IP packet filter......");
2951 request = mbim_message_ip_packet_filters_set_new (props.session_id,
2952 props.array->len,
2953 (const MbimPacketFilter *const *)props.array->pdata,
2954 &error);
2955 }
2956
2957 if (!request) {
2958 g_printerr ("error: couldn't create IP packet filters request: %s\n", error->message);
2959 shutdown (FALSE);
2960 return;
2961 }
2962
2963 mbim_device_command (ctx->device,
2964 request,
2965 10,
2966 ctx->cancellable,
2967 (GAsyncReadyCallback)ip_packet_filters_ready,
2968 NULL);
2969 return;
2970 }
2971
2972 /* Provisioned contexts? */
2973 if (query_provisioned_contexts_flag) {
2974 request = mbim_message_provisioned_contexts_query_new (NULL);
2975 mbim_device_command (ctx->device,
2976 request,
2977 10,
2978 ctx->cancellable,
2979 (GAsyncReadyCallback)provisioned_contexts_ready,
2980 NULL);
2981 return;
2982 }
2983
2984 /* Set provisioned context */
2985 if (set_provisioned_contexts_str) {
2986 g_auto(ProvisionedContextProperties) props = {
2987 .access_string = NULL,
2988 .context_id = 0,
2989 .auth_protocol = MBIM_AUTH_PROTOCOL_NONE,
2990 .username = NULL,
2991 .password = NULL,
2992 .compression = MBIM_COMPRESSION_NONE,
2993 .context_type = MBIM_CONTEXT_TYPE_INVALID,
2994 .provider_id = NULL
2995 };
2996
2997 if (!mbimcli_parse_key_value_string (set_provisioned_contexts_str,
2998 &error,
2999 (MbimParseKeyValueForeachFn)set_provisioned_contexts_foreach_cb,
3000 &props)) {
3001 g_printerr ("error: couldn't parse input string: %s\n", error->message);
3002 shutdown (FALSE);
3003 return;
3004 }
3005
3006 request = mbim_message_provisioned_contexts_set_new ((guint32)props.context_id,
3007 mbim_uuid_from_context_type (props.context_type),
3008 props.access_string,
3009 props.username,
3010 props.password,
3011 props.compression,
3012 props.auth_protocol,
3013 props.provider_id,
3014 &error);
3015 if (!request) {
3016 g_printerr ("error: couldn't create request: %s\n", error->message);
3017 shutdown (FALSE);
3018 return;
3019 }
3020
3021 mbim_device_command (ctx->device,
3022 request,
3023 60,
3024 ctx->cancellable,
3025 (GAsyncReadyCallback)provisioned_contexts_ready,
3026 NULL);
3027 return;
3028 }
3029
3030 /* Set signal state */
3031 if (set_signal_state_str) {
3032 SignalStateProperties props = {
3033 .signal_strength = 0,
3034 .rssi = 0,
3035 .error_rate = 0
3036 };
3037
3038 if (!mbimcli_parse_key_value_string (set_signal_state_str,
3039 &error,
3040 (MbimParseKeyValueForeachFn)set_signal_state_foreach_cb,
3041 &props)) {
3042 g_printerr ("error: couldn't parse input string: %s\n", error->message);
3043 shutdown (FALSE);
3044 return;
3045 }
3046
3047 request = mbim_message_signal_state_set_new (props.signal_strength,
3048 props.rssi,
3049 props.error_rate,
3050 &error);
3051 if (!request) {
3052 g_printerr ("error: couldn't create request: %s\n", error->message);
3053 shutdown (FALSE);
3054 return;
3055 }
3056
3057 mbim_device_command (ctx->device,
3058 request,
3059 60,
3060 ctx->cancellable,
3061 (GAsyncReadyCallback)signal_state_ready,
3062 NULL);
3063 return;
3064 }
3065
3066 /* Set network idle hint state */
3067 if (set_network_idle_hint_str) {
3068 MbimNetworkIdleHintState network_state;
3069
3070 if (!mbimcli_read_network_idle_hint_state_from_string (set_network_idle_hint_str, &network_state, &error)) {
3071 g_printerr ("error: couldn't read idle hint state: %s\n", error->message);
3072 shutdown (FALSE);
3073 return;
3074 }
3075
3076 request = mbim_message_network_idle_hint_set_new (network_state, &error);
3077 if (!request) {
3078 g_printerr ("error: couldn't create request: %s\n", error->message);
3079 shutdown (FALSE);
3080 return;
3081 }
3082
3083 mbim_device_command (ctx->device,
3084 request,
3085 10,
3086 ctx->cancellable,
3087 (GAsyncReadyCallback)network_idle_hint_ready,
3088 NULL);
3089 return;
3090 }
3091
3092 /* Get network idle hint state */
3093 if (query_network_idle_hint_flag) {
3094 request = mbim_message_network_idle_hint_query_new (NULL);
3095 mbim_device_command (ctx->device,
3096 request,
3097 10,
3098 ctx->cancellable,
3099 (GAsyncReadyCallback)network_idle_hint_ready,
3100 NULL);
3101 return;
3102 }
3103
3104 /* Set emergency mode state */
3105 if (set_emergency_mode_str) {
3106 MbimEmergencyModeState emergency_state;
3107
3108 if (!mbimcli_read_emergency_mode_state_from_string (set_emergency_mode_str, &emergency_state, &error)) {
3109 g_printerr ("error: couldn't read emergency mode state: %s\n", error->message);
3110 shutdown (FALSE);
3111 return;
3112 }
3113
3114 request = mbim_message_emergency_mode_set_new (emergency_state, &error);
3115 if (!request) {
3116 g_printerr ("error: couldn't create request: %s\n", error->message);
3117 shutdown (FALSE);
3118 return;
3119 }
3120
3121 mbim_device_command (ctx->device,
3122 request,
3123 10,
3124 ctx->cancellable,
3125 (GAsyncReadyCallback)emergency_mode_ready,
3126 NULL);
3127 return;
3128 }
3129
3130 /* Query emergency mode state */
3131 if (query_emergency_mode_flag) {
3132 request = mbim_message_emergency_mode_query_new (NULL);
3133 mbim_device_command (ctx->device,
3134 request,
3135 10,
3136 ctx->cancellable,
3137 (GAsyncReadyCallback)emergency_mode_ready,
3138 NULL);
3139 return;
3140 }
3141
3142 /* Request to set service activation */
3143 if (set_service_activation_str) {
3144 guint8 *data = NULL;
3145 gsize data_size = 0;
3146
3147 data = mbimcli_read_buffer_from_string (set_service_activation_str, -1, &data_size, &error);
3148 if (!data) {
3149 g_printerr ("error: couldn't parse the input: %s\n", error->message);
3150 shutdown (FALSE);
3151 return;
3152 }
3153
3154 request = mbim_message_service_activation_set_new (data_size,
3155 data,
3156 &error);
3157 if (!request) {
3158 g_printerr ("error: couldn't create request: %s\n", error->message);
3159 shutdown (FALSE);
3160 return;
3161 }
3162
3163 mbim_device_command (ctx->device,
3164 request,
3165 10,
3166 ctx->cancellable,
3167 (GAsyncReadyCallback)set_service_activation_ready,
3168 NULL);
3169 return;
3170 }
3171
3172 g_warn_if_reached ();
3173 }