Debian Codegraph @

src/qmicli/qmicli-uim.c

raw
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * qmicli -- Command line interface to control QMI devices
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * Copyright (C) 2012-2017 Aleksander Morgado <[email protected]>
19 * Copyright (c) 2022 Qualcomm Innovation Center, Inc.
20 */
21
22 #include "config.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <locale.h>
27 #include <string.h>
28
29 #include <glib.h>
30 #include <gio/gio.h>
31
32 #include <libqmi-glib.h>
33 #include <qmi-common.h>
34
35 #include "qmicli.h"
36 #include "qmicli-helpers.h"
37
38 #if defined HAVE_QMI_SERVICE_UIM
39
40 #undef VALIDATE_MASK_NONE
41 #define VALIDATE_MASK_NONE(str) (str ? str : "none")
42
43 /* Context */
44 typedef struct {
45 QmiDevice *device;
46 QmiClientUim *client;
47 GCancellable *cancellable;
48
49 /* For Slot Status indication */
50 guint slot_status_indication_id;
51 guint refresh_indication_id;
52 } Context;
53 static Context *ctx;
54
55 /* Options */
56 static gchar *read_transparent_str;
57 static gchar *read_record_str;
58 static gchar *set_pin_protection_str;
59 static gchar *verify_pin_str;
60 static gchar *unblock_pin_str;
61 static gchar *change_pin_str;
62 static gchar *get_file_attributes_str;
63 static gchar *sim_power_on_str;
64 static gchar *sim_power_off_str;
65 static gchar *change_provisioning_session_str;
66 static gchar *switch_slot_str;
67 static gchar *depersonalization_str;
68 static gchar *remote_unlock_str;
69 static gchar *open_logical_channel_str;
70 static gchar *close_logical_channel_str;
71 static gchar *send_apdu_str;
72 static gchar **monitor_refresh_file_array;
73 static gboolean get_card_status_flag;
74 static gboolean get_supported_messages_flag;
75 static gboolean get_slot_status_flag;
76 static gboolean monitor_slot_status_flag;
77 static gboolean reset_flag;
78 static gboolean monitor_refresh_all_flag;
79 static gboolean noop_flag;
80 static gboolean get_configuration_flag;
81
82 #undef VALIDATE_UNKNOWN
83 #define VALIDATE_UNKNOWN(str) (str ? str : "unknown")
84
85 static GOptionEntry entries[] = {
86 #if defined HAVE_QMI_MESSAGE_UIM_SET_PIN_PROTECTION
87 { "uim-set-pin-protection", 0, 0, G_OPTION_ARG_STRING, &set_pin_protection_str,
88 "Set PIN protection (allowed keys: session-type ((primary|secondary|tertiary|quarternary|quinary)-gw-provisioning|card-slot-[1-5]))",
89 "[(PIN1|PIN2|UPIN),(disable|enable),(current PIN)[,\"key=value,...\"]]"
90 },
91 #endif
92 #if defined HAVE_QMI_MESSAGE_UIM_VERIFY_PIN
93 { "uim-verify-pin", 0, 0, G_OPTION_ARG_STRING, &verify_pin_str,
94 "Verify PIN (allowed keys: session-type ((primary|secondary|tertiary|quarternary|quinary)-gw-provisioning|card-slot-[1-5]))",
95 "[(PIN1|PIN2|UPIN),(current PIN)[,\"key=value,...\"]]",
96 },
97 #endif
98 #if defined HAVE_QMI_MESSAGE_UIM_UNBLOCK_PIN
99 { "uim-unblock-pin", 0, 0, G_OPTION_ARG_STRING, &unblock_pin_str,
100 "Unblock PIN (allowed keys: session-type ((primary|secondary|tertiary|quarternary|quinary)-gw-provisioning|card-slot-[1-5]))",
101 "[(PIN1|PIN2|UPIN),(PUK),(new PIN)[,\"key=value,...\"]]",
102 },
103 #endif
104 #if defined HAVE_QMI_MESSAGE_UIM_CHANGE_PIN
105 { "uim-change-pin", 0, 0, G_OPTION_ARG_STRING, &change_pin_str,
106 "Change PIN (allowed keys: session-type ((primary|secondary|tertiary|quarternary|quinary)-gw-provisioning|card-slot-[1-5]))",
107 "[(PIN1|PIN2|UPIN),(old PIN),(new PIN)[,\"key=value,...\"]]",
108 },
109 #endif
110 #if defined HAVE_QMI_MESSAGE_UIM_READ_TRANSPARENT
111 { "uim-read-transparent", 0, 0, G_OPTION_ARG_STRING, &read_transparent_str,
112 "Read a transparent file given the file path",
113 "[0xNNNN,0xNNNN,...]"
114 },
115 #endif
116 #if defined HAVE_QMI_MESSAGE_UIM_GET_FILE_ATTRIBUTES
117 { "uim-get-file-attributes", 0, 0, G_OPTION_ARG_STRING, &get_file_attributes_str,
118 "Get the attributes of a given file",
119 "[0xNNNN,0xNNNN,...]"
120 },
121 #endif
122 #if defined HAVE_QMI_MESSAGE_UIM_READ_RECORD
123 { "uim-read-record", 0, 0, G_OPTION_ARG_STRING, &read_record_str,
124 "Read a record from given file (allowed keys: record-number, record-length, file ([0xNNNN-0xNNNN,...])",
125 "[\"key=value,...\"]"
126 },
127 #endif
128 #if defined HAVE_QMI_MESSAGE_UIM_GET_CARD_STATUS
129 { "uim-get-card-status", 0, 0, G_OPTION_ARG_NONE, &get_card_status_flag,
130 "Get card status",
131 NULL
132 },
133 #endif
134 #if defined HAVE_QMI_MESSAGE_UIM_GET_SUPPORTED_MESSAGES
135 { "uim-get-supported-messages", 0, 0, G_OPTION_ARG_NONE, &get_supported_messages_flag,
136 "Get supported messages",
137 NULL
138 },
139 #endif
140 #if defined HAVE_QMI_MESSAGE_UIM_POWER_ON_SIM
141 { "uim-sim-power-on", 0, 0, G_OPTION_ARG_STRING, &sim_power_on_str,
142 "Power on SIM card",
143 "[(slot number)]"
144 },
145 #endif
146 #if defined HAVE_QMI_MESSAGE_UIM_POWER_OFF_SIM
147 { "uim-sim-power-off", 0, 0, G_OPTION_ARG_STRING, &sim_power_off_str,
148 "Power off SIM card",
149 "[(slot number)]"
150 },
151 #endif
152 #if defined HAVE_QMI_MESSAGE_UIM_CHANGE_PROVISIONING_SESSION
153 { "uim-change-provisioning-session", 0, 0, G_OPTION_ARG_STRING, &change_provisioning_session_str,
154 "Change provisioning session (allowed keys: session-type ((primary|secondary|tertiary|quarternary|quinary)-gw-provisioning), activate (yes|no), slot, aid)",
155 "[\"key=value,...\"]"
156 },
157 #endif
158 #if defined HAVE_QMI_MESSAGE_UIM_GET_SLOT_STATUS
159 { "uim-get-slot-status", 0, 0, G_OPTION_ARG_NONE, &get_slot_status_flag,
160 "Get slot status",
161 NULL
162 },
163 #endif
164 #if defined HAVE_QMI_MESSAGE_UIM_SWITCH_SLOT && defined HAVE_QMI_MESSAGE_UIM_GET_SLOT_STATUS
165 { "uim-switch-slot", 0, 0, G_OPTION_ARG_STRING, &switch_slot_str,
166 "Switch active physical slot",
167 "[(slot number)]"
168 },
169 #endif
170 #if defined HAVE_QMI_INDICATION_UIM_SLOT_STATUS
171 { "uim-monitor-slot-status", 0, 0, G_OPTION_ARG_NONE, &monitor_slot_status_flag,
172 "Watch for slot status indications",
173 NULL
174 },
175 #endif
176 #if defined HAVE_QMI_MESSAGE_UIM_RESET
177 { "uim-reset", 0, 0, G_OPTION_ARG_NONE, &reset_flag,
178 "Reset the service state",
179 NULL
180 },
181 #endif
182 #if defined HAVE_QMI_MESSAGE_UIM_REFRESH_REGISTER
183 { "uim-monitor-refresh-file", 0, 0, G_OPTION_ARG_STRING_ARRAY, &monitor_refresh_file_array,
184 "Watch for REFRESH events for given file paths",
185 "[0xNNNN,0xNNNN,...]"
186 },
187 #endif
188 #if defined HAVE_QMI_MESSAGE_UIM_REFRESH_REGISTER_ALL
189 { "uim-monitor-refresh-all", 0, 0, G_OPTION_ARG_NONE, &monitor_refresh_all_flag,
190 "Watch for REFRESH events for any file",
191 NULL
192 },
193 #endif
194 #if defined HAVE_QMI_MESSAGE_UIM_GET_CONFIGURATION
195 { "uim-get-configuration", 0, 0, G_OPTION_ARG_NONE, &get_configuration_flag,
196 "Get personalization status of the modem",
197 NULL
198 },
199 #endif
200 #if defined HAVE_QMI_MESSAGE_UIM_DEPERSONALIZATION
201 { "uim-depersonalization", 0, 0, G_OPTION_ARG_STRING, &depersonalization_str,
202 "Deactivates or unblocks personalization feature",
203 "[(feature),(operation),(control key)[,(slot number)]]"
204 },
205 #endif
206 #if defined HAVE_QMI_MESSAGE_UIM_REMOTE_UNLOCK
207 { "uim-remote-unlock", 0, 0, G_OPTION_ARG_STRING, &remote_unlock_str,
208 "Updates the SimLock configuration data",
209 "[XX:XX:...]"
210 },
211 #endif
212 #if defined HAVE_QMI_MESSAGE_UIM_OPEN_LOGICAL_CHANNEL
213 { "uim-open-logical-channel", 0, 0, G_OPTION_ARG_STRING, &open_logical_channel_str,
214 "Open logical channel",
215 "[(slot number),(aid)]"
216 },
217 #endif
218 #if defined HAVE_QMI_MESSAGE_UIM_LOGICAL_CHANNEL
219 { "uim-close-logical-channel", 0, 0, G_OPTION_ARG_STRING, &close_logical_channel_str,
220 "Close logical channel",
221 "[(slot number),(channel ID)]"
222 },
223 #endif
224 #if defined HAVE_QMI_MESSAGE_UIM_SEND_APDU
225 { "uim-send-apdu", 0, 0, G_OPTION_ARG_STRING, &send_apdu_str,
226 "Send APDU",
227 "[(slot number),(channel ID),(apdu)]"
228 },
229 #endif
230 { "uim-noop", 0, 0, G_OPTION_ARG_NONE, &noop_flag,
231 "Just allocate or release a UIM client. Use with `--client-no-release-cid' and/or `--client-cid'",
232 NULL
233 },
234 { NULL, 0, 0, 0, NULL, NULL, NULL }
235 };
236
237 GOptionGroup *
238 qmicli_uim_get_option_group (void)
239 {
240 GOptionGroup *group;
241
242 group = g_option_group_new ("uim",
243 "UIM options:",
244 "Show User Identity Module options",
245 NULL,
246 NULL);
247 g_option_group_add_entries (group, entries);
248
249 return group;
250 }
251
252 gboolean
253 qmicli_uim_options_enabled (void)
254 {
255 static guint n_actions = 0;
256 static gboolean checked = FALSE;
257
258 if (checked)
259 return !!n_actions;
260
261 n_actions = (!!set_pin_protection_str +
262 !!verify_pin_str +
263 !!unblock_pin_str +
264 !!change_pin_str +
265 !!read_transparent_str +
266 !!read_record_str +
267 !!get_file_attributes_str +
268 !!sim_power_on_str +
269 !!sim_power_off_str +
270 !!change_provisioning_session_str +
271 !!switch_slot_str +
272 !!monitor_refresh_file_array +
273 !!depersonalization_str +
274 !!remote_unlock_str +
275 !!open_logical_channel_str +
276 !!close_logical_channel_str +
277 !!send_apdu_str +
278 get_card_status_flag +
279 get_supported_messages_flag +
280 get_slot_status_flag +
281 monitor_slot_status_flag +
282 reset_flag +
283 monitor_refresh_all_flag +
284 get_configuration_flag +
285 noop_flag);
286
287 if (n_actions > 1) {
288 g_printerr ("error: too many UIM actions requested\n");
289 exit (EXIT_FAILURE);
290 }
291
292 if (monitor_slot_status_flag || monitor_refresh_file_array || monitor_refresh_all_flag)
293 qmicli_expect_indications ();
294
295 checked = TRUE;
296 return !!n_actions;
297 }
298
299 static void
300 context_free (Context *context)
301 {
302 if (!context)
303 return;
304
305 if (context->slot_status_indication_id)
306 g_signal_handler_disconnect (context->client,
307 context->slot_status_indication_id);
308 if (context->refresh_indication_id)
309 g_signal_handler_disconnect (context->client,
310 context->refresh_indication_id);
311
312 if (context->client)
313 g_object_unref (context->client);
314 g_object_unref (context->cancellable);
315 g_object_unref (context->device);
316 g_slice_free (Context, context);
317 }
318
319 static void
320 operation_shutdown (gboolean operation_status)
321 {
322 /* Cleanup context and finish async operation */
323 context_free (ctx);
324 qmicli_async_operation_done (operation_status, FALSE);
325 }
326
327 #if defined HAVE_QMI_MESSAGE_UIM_SET_PIN_PROTECTION || \
328 defined HAVE_QMI_MESSAGE_UIM_VERIFY_PIN || \
329 defined HAVE_QMI_MESSAGE_UIM_UNBLOCK_PIN ||\
330 defined HAVE_QMI_MESSAGE_UIM_CHANGE_PIN
331
332 static gboolean
333 provisioning_session_type_handle (const gchar *key,
334 const gchar *value,
335 GError **error,
336 gpointer user_data)
337 {
338 QmiUimSessionType *session_type = (QmiUimSessionType *) user_data;
339
340 if (!value || !value[0]) {
341 g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED,
342 "key '%s' requires a value", key);
343 return FALSE;
344 }
345
346 if (g_ascii_strcasecmp (key, "session-type") == 0) {
347 if (!qmicli_read_uim_session_type_from_string (value, session_type)) {
348 g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED,
349 "invalid session type value: %s (not a valid enum)", value);
350 return FALSE;
351 }
352 return TRUE;
353 }
354
355 g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED,
356 "Unrecognized option '%s'", key);
357 return FALSE;
358 }
359
360 #endif
361
362 #if defined HAVE_QMI_MESSAGE_UIM_SET_PIN_PROTECTION
363
364 static QmiMessageUimSetPinProtectionInput *
365 set_pin_protection_input_create (const gchar *str)
366 {
367 QmiMessageUimSetPinProtectionInput *input = NULL;
368 QmiUimSessionType session_type = QMI_UIM_SESSION_TYPE_CARD_SLOT_1;
369 gchar **split;
370 guint len_split;
371 GError *error = NULL;
372 QmiUimPinId pin_id;
373 gboolean enable_disable;
374 gchar *current_pin;
375
376 /* Prepare inputs.
377 * Format of the string is:
378 * "[(PIN1|PIN2|UPIN),(disable|enable),(current PIN)[,'key=value,...']]" with valid key = (session-type)
379 */
380 split = g_strsplit (str, ",", 4);
381 len_split = g_strv_length (split);
382
383 /* Parse optional kv-pairs */
384 if (len_split >= 4) {
385 if (!qmicli_parse_key_value_string (split[3], &error, provisioning_session_type_handle, &session_type)) {
386 g_printerr ("error: could not parse input string '%s': %s\n", str, error->message);
387 }
388 }
389
390 if (error == NULL &&
391 qmicli_read_uim_pin_id_from_string (split[0], &pin_id) &&
392 qmicli_read_enable_disable_from_string (split[1], &enable_disable) &&
393 qmicli_read_non_empty_string (split[2], "current PIN", &current_pin)) {
394 GArray *placeholder_aid;
395
396 placeholder_aid = g_array_new (FALSE, FALSE, sizeof (guint8));
397
398 input = qmi_message_uim_set_pin_protection_input_new ();
399 if (!qmi_message_uim_set_pin_protection_input_set_info (
400 input,
401 pin_id,
402 enable_disable,
403 current_pin,
404 &error) ||
405 !qmi_message_uim_set_pin_protection_input_set_session (
406 input,
407 session_type,
408 placeholder_aid, /* ignored */
409 &error)) {
410 g_printerr ("error: couldn't create input data bundle: '%s'\n",
411 error->message);
412 qmi_message_uim_set_pin_protection_input_unref (input);
413 input = NULL;
414 }
415 g_array_unref (placeholder_aid);
416 }
417 g_strfreev (split);
418 g_clear_error (&error);
419
420 return input;
421 }
422
423 static void
424 set_pin_protection_ready (QmiClientUim *client,
425 GAsyncResult *res)
426 {
427 QmiMessageUimSetPinProtectionOutput *output;
428 GError *error = NULL;
429
430 output = qmi_client_uim_set_pin_protection_finish (client, res, &error);
431 if (!output) {
432 g_printerr ("error: operation failed: %s\n", error->message);
433 g_error_free (error);
434 operation_shutdown (FALSE);
435 return;
436 }
437
438 if (!qmi_message_uim_set_pin_protection_output_get_result (output, &error)) {
439 guint8 verify_retries_left;
440 guint8 unblock_retries_left;
441
442 g_printerr ("error: couldn't set PIN protection: %s\n", error->message);
443 g_error_free (error);
444
445 if (qmi_message_uim_set_pin_protection_output_get_retries_remaining (
446 output,
447 &verify_retries_left,
448 &unblock_retries_left,
449 NULL)) {
450 g_printerr ("[%s] Retries left:\n"
451 "\tVerify: %u\n"
452 "\tUnblock: %u\n",
453 qmi_device_get_path_display (ctx->device),
454 verify_retries_left,
455 unblock_retries_left);
456 }
457
458 qmi_message_uim_set_pin_protection_output_unref (output);
459 operation_shutdown (FALSE);
460 return;
461 }
462
463 g_print ("[%s] PIN protection updated\n",
464 qmi_device_get_path_display (ctx->device));
465
466 qmi_message_uim_set_pin_protection_output_unref (output);
467 operation_shutdown (TRUE);
468 }
469
470 #endif /* HAVE_QMI_MESSAGE_UIM_SET_PIN_PROTECTION */
471
472 #if defined HAVE_QMI_MESSAGE_UIM_VERIFY_PIN
473
474 static QmiMessageUimVerifyPinInput *
475 verify_pin_input_create (const gchar *str)
476 {
477 QmiMessageUimVerifyPinInput *input = NULL;
478 QmiUimSessionType session_type = QMI_UIM_SESSION_TYPE_CARD_SLOT_1;
479 gchar **split;
480 guint len_split;
481 GError *error = NULL;
482 QmiUimPinId pin_id;
483 gchar *current_pin;
484
485 /* Prepare inputs.
486 * Format of the string is:
487 * "[(PIN1|PIN2),(current PIN)[,'key=value,...']]" with valid key = (session-type)
488 */
489 split = g_strsplit (str, ",", 3);
490 len_split = g_strv_length (split);
491
492 /* Parse optional kv-pairs */
493 if (len_split >= 3) {
494 if (!qmicli_parse_key_value_string (split[2], &error, provisioning_session_type_handle, &session_type)) {
495 g_printerr ("error: could not parse input string '%s': %s\n", str, error->message);
496 }
497 }
498
499 if (error == NULL &&
500 qmicli_read_uim_pin_id_from_string (split[0], &pin_id) &&
501 qmicli_read_non_empty_string (split[1], "current PIN", &current_pin)) {
502 GArray *placeholder_aid;
503
504 placeholder_aid = g_array_new (FALSE, FALSE, sizeof (guint8));
505
506 input = qmi_message_uim_verify_pin_input_new ();
507 if (!qmi_message_uim_verify_pin_input_set_info (
508 input,
509 pin_id,
510 current_pin,
511 &error) ||
512 !qmi_message_uim_verify_pin_input_set_session (
513 input,
514 session_type,
515 placeholder_aid, /* ignored */
516 &error)) {
517 g_printerr ("error: couldn't create input data bundle: '%s'\n",
518 error->message);
519 qmi_message_uim_verify_pin_input_unref (input);
520 input = NULL;
521 }
522 g_array_unref (placeholder_aid);
523 }
524 g_strfreev (split);
525 g_clear_error (&error);
526
527 return input;
528 }
529
530 static void
531 verify_pin_ready (QmiClientUim *client,
532 GAsyncResult *res)
533 {
534 QmiMessageUimVerifyPinOutput *output;
535 GError *error = NULL;
536
537 output = qmi_client_uim_verify_pin_finish (client, res, &error);
538 if (!output) {
539 g_printerr ("error: operation failed: %s\n", error->message);
540 g_error_free (error);
541 operation_shutdown (FALSE);
542 return;
543 }
544
545 if (!qmi_message_uim_verify_pin_output_get_result (output, &error)) {
546 guint8 verify_retries_left;
547 guint8 unblock_retries_left;
548
549 g_printerr ("error: couldn't verify PIN: %s\n", error->message);
550 g_error_free (error);
551
552 if (qmi_message_uim_verify_pin_output_get_retries_remaining (
553 output,
554 &verify_retries_left,
555 &unblock_retries_left,
556 NULL)) {
557 g_printerr ("[%s] Retries left:\n"
558 "\tVerify: %u\n"
559 "\tUnblock: %u\n",
560 qmi_device_get_path_display (ctx->device),
561 verify_retries_left,
562 unblock_retries_left);
563 }
564
565 qmi_message_uim_verify_pin_output_unref (output);
566 operation_shutdown (FALSE);
567 return;
568 }
569
570 g_print ("[%s] PIN verified successfully\n",
571 qmi_device_get_path_display (ctx->device));
572
573 qmi_message_uim_verify_pin_output_unref (output);
574 operation_shutdown (TRUE);
575 }
576
577 #endif /* HAVE_QMI_MESSAGE_UIM_VERIFY_PIN */
578
579 #if defined HAVE_QMI_MESSAGE_UIM_UNBLOCK_PIN
580
581 static QmiMessageUimUnblockPinInput *
582 unblock_pin_input_create (const gchar *str)
583 {
584 QmiMessageUimUnblockPinInput *input = NULL;
585 QmiUimSessionType session_type = QMI_UIM_SESSION_TYPE_CARD_SLOT_1;
586 gchar **split;
587 guint len_split;
588 GError *error = NULL;
589 QmiUimPinId pin_id;
590 gchar *puk;
591 gchar *new_pin;
592
593 /* Prepare inputs.
594 * Format of the string is:
595 * "[(PIN|PIN2),(PUK),(new PIN)[,'key=value,...']]" with valid key = (session-type)
596 */
597 split = g_strsplit (str, ",", 4);
598 len_split = g_strv_length (split);
599
600 /* Parse optional kv-pairs */
601 if (len_split >= 4) {
602 if (!qmicli_parse_key_value_string (split[3], &error, provisioning_session_type_handle, &session_type)) {
603 g_printerr ("error: could not parse input string '%s': %s\n", str, error->message);
604 }
605 }
606
607 if (error == NULL &&
608 qmicli_read_uim_pin_id_from_string (split[0], &pin_id) &&
609 qmicli_read_non_empty_string (split[1], "PUK", &puk) &&
610 qmicli_read_non_empty_string (split[2], "new PIN", &new_pin)) {
611 GArray *placeholder_aid;
612
613 placeholder_aid = g_array_new (FALSE, FALSE, sizeof (guint8));
614
615 input = qmi_message_uim_unblock_pin_input_new ();
616 if (!qmi_message_uim_unblock_pin_input_set_info (
617 input,
618 pin_id,
619 puk,
620 new_pin,
621 &error) ||
622 !qmi_message_uim_unblock_pin_input_set_session (
623 input,
624 session_type,
625 placeholder_aid, /* ignored */
626 &error)) {
627 g_printerr ("error: couldn't create input data bundle: '%s'\n",
628 error->message);
629 g_error_free (error);
630 qmi_message_uim_unblock_pin_input_unref (input);
631 input = NULL;
632 }
633 g_array_unref (placeholder_aid);
634 }
635 g_strfreev (split);
636 g_clear_error (&error);
637
638 return input;
639 }
640
641 static void
642 unblock_pin_ready (QmiClientUim *client,
643 GAsyncResult *res)
644 {
645 QmiMessageUimUnblockPinOutput *output;
646 GError *error = NULL;
647
648 output = qmi_client_uim_unblock_pin_finish (client, res, &error);
649 if (!output) {
650 g_printerr ("error: operation failed: %s\n", error->message);
651 g_error_free (error);
652 operation_shutdown (FALSE);
653 return;
654 }
655
656 if (!qmi_message_uim_unblock_pin_output_get_result (output, &error)) {
657 guint8 verify_retries_left;
658 guint8 unblock_retries_left;
659
660 g_printerr ("error: couldn't unblock PIN: %s\n", error->message);
661 g_error_free (error);
662
663 if (qmi_message_uim_unblock_pin_output_get_retries_remaining (
664 output,
665 &verify_retries_left,
666 &unblock_retries_left,
667 NULL)) {
668 g_printerr ("[%s] Retries left:\n"
669 "\tVerify: %u\n"
670 "\tUnblock: %u\n",
671 qmi_device_get_path_display (ctx->device),
672 verify_retries_left,
673 unblock_retries_left);
674 }
675
676 qmi_message_uim_unblock_pin_output_unref (output);
677 operation_shutdown (FALSE);
678 return;
679 }
680
681 g_print ("[%s] PIN unblocked successfully\n",
682 qmi_device_get_path_display (ctx->device));
683
684 qmi_message_uim_unblock_pin_output_unref (output);
685 operation_shutdown (TRUE);
686 }
687
688 #endif /* HAVE_QMI_MESSAGE_UIM_UNBLOCK_PIN */
689
690 #if defined HAVE_QMI_MESSAGE_UIM_CHANGE_PIN
691
692 static QmiMessageUimChangePinInput *
693 change_pin_input_create (const gchar *str)
694 {
695 QmiMessageUimChangePinInput *input = NULL;
696 QmiUimSessionType session_type = QMI_UIM_SESSION_TYPE_CARD_SLOT_1;
697 gchar **split;
698 guint len_split;
699 GError *error = NULL;
700 QmiUimPinId pin_id;
701 gchar *old_pin;
702 gchar *new_pin;
703
704 /* Prepare inputs.
705 * Format of the string is:
706 * "[(PIN1|PIN2),(old PIN),(new PIN)[,'key=value,...']]" with valid key = (session-type)
707 */
708 split = g_strsplit (str, ",", 4);
709 len_split = g_strv_length (split);
710
711 /* Parse optional kv-pairs */
712 if (len_split >= 4) {
713 if (!qmicli_parse_key_value_string (split[3], &error, provisioning_session_type_handle, &session_type)) {
714 g_printerr ("error: could not parse input string '%s': %s\n", str, error->message);
715 }
716 }
717
718 if (error == NULL &&
719 qmicli_read_uim_pin_id_from_string (split[0], &pin_id) &&
720 qmicli_read_non_empty_string (split[1], "old PIN", &old_pin) &&
721 qmicli_read_non_empty_string (split[2], "new PIN", &new_pin)) {
722 GArray *placeholder_aid;
723
724 placeholder_aid = g_array_new (FALSE, FALSE, sizeof (guint8));
725
726 input = qmi_message_uim_change_pin_input_new ();
727 if (!qmi_message_uim_change_pin_input_set_info (
728 input,
729 pin_id,
730 old_pin,
731 new_pin,
732 &error) ||
733 !qmi_message_uim_change_pin_input_set_session (
734 input,
735 session_type,
736 placeholder_aid, /* ignored */
737 &error)) {
738 g_printerr ("error: couldn't create input data bundle: '%s'\n",
739 error->message);
740 g_error_free (error);
741 qmi_message_uim_change_pin_input_unref (input);
742 input = NULL;
743 }
744 g_array_unref (placeholder_aid);
745 }
746 g_strfreev (split);
747 g_clear_error (&error);
748
749 return input;
750 }
751
752 static void
753 change_pin_ready (QmiClientUim *client,
754 GAsyncResult *res)
755 {
756 QmiMessageUimChangePinOutput *output;
757 GError *error = NULL;
758
759 output = qmi_client_uim_change_pin_finish (client, res, &error);
760 if (!output) {
761 g_printerr ("error: operation failed: %s\n", error->message);
762 g_error_free (error);
763 operation_shutdown (FALSE);
764 return;
765 }
766
767 if (!qmi_message_uim_change_pin_output_get_result (output, &error)) {
768 guint8 verify_retries_left;
769 guint8 unblock_retries_left;
770
771 g_printerr ("error: couldn't change PIN: %s\n", error->message);
772 g_error_free (error);
773
774 if (qmi_message_uim_change_pin_output_get_retries_remaining (
775 output,
776 &verify_retries_left,
777 &unblock_retries_left,
778 NULL)) {
779 g_printerr ("[%s] Retries left:\n"
780 "\tVerify: %u\n"
781 "\tUnblock: %u\n",
782 qmi_device_get_path_display (ctx->device),
783 verify_retries_left,
784 unblock_retries_left);
785 }
786
787 qmi_message_uim_change_pin_output_unref (output);
788 operation_shutdown (FALSE);
789 return;
790 }
791
792 g_print ("[%s] PIN changed successfully\n",
793 qmi_device_get_path_display (ctx->device));
794
795 qmi_message_uim_change_pin_output_unref (output);
796 operation_shutdown (TRUE);
797 }
798
799 #endif /* HAVE_QMI_MESSAGE_UIM_CHANGE_PIN */
800
801 #if defined HAVE_QMI_MESSAGE_UIM_GET_SUPPORTED_MESSAGES
802
803 static void
804 get_supported_messages_ready (QmiClientUim *client,
805 GAsyncResult *res)
806 {
807 QmiMessageUimGetSupportedMessagesOutput *output;
808 GError *error = NULL;
809 GArray *bytearray = NULL;
810 gchar *str;
811
812 output = qmi_client_uim_get_supported_messages_finish (client, res, &error);
813 if (!output) {
814 g_printerr ("error: operation failed: %s\n", error->message);
815 g_error_free (error);
816 operation_shutdown (FALSE);
817 return;
818 }
819
820 if (!qmi_message_uim_get_supported_messages_output_get_result (output, &error)) {
821 g_printerr ("error: couldn't get supported UIM messages: %s\n", error->message);
822 g_error_free (error);
823 qmi_message_uim_get_supported_messages_output_unref (output);
824 operation_shutdown (FALSE);
825 return;
826 }
827
828 g_print ("[%s] Successfully got supported UIM messages:\n",
829 qmi_device_get_path_display (ctx->device));
830
831 qmi_message_uim_get_supported_messages_output_get_list (output, &bytearray, NULL);
832 str = qmicli_get_supported_messages_list (bytearray ? (const guint8 *)bytearray->data : NULL,
833 bytearray ? bytearray->len : 0);
834 g_print ("%s", str);
835 g_free (str);
836
837 qmi_message_uim_get_supported_messages_output_unref (output);
838 operation_shutdown (TRUE);
839 }
840
841 #endif /* HAVE_QMI_MESSAGE_UIM_GET_SUPPORTED_MESSAGES */
842
843 #if defined HAVE_QMI_MESSAGE_UIM_POWER_ON_SIM
844
845 static QmiMessageUimPowerOnSimInput *
846 power_on_sim_input_create (const gchar *slot_str)
847 {
848 QmiMessageUimPowerOnSimInput *input;
849 guint slot;
850 GError *error = NULL;
851
852 if (!qmicli_read_uint_from_string (slot_str, &slot) || (slot > G_MAXUINT8)) {
853 g_printerr ("error: invalid slot number\n");
854 return NULL;
855 }
856
857 input = qmi_message_uim_power_on_sim_input_new ();
858
859 if (!qmi_message_uim_power_on_sim_input_set_slot (input, slot, &error)) {
860 g_printerr ("error: could not create SIM power on input: %s\n", error->message);
861 g_error_free (error);
862 qmi_message_uim_power_on_sim_input_unref (input);
863 input = NULL;
864 }
865
866 return input;
867 }
868
869 static void
870 power_on_sim_ready (QmiClientUim *client,
871 GAsyncResult *res)
872 {
873 QmiMessageUimPowerOnSimOutput *output;
874 GError *error = NULL;
875
876 output = qmi_client_uim_power_on_sim_finish (client, res, &error);
877 if (!output) {
878 g_printerr ("error: operation failed: %s\n", error->message);
879 g_error_free (error);
880 operation_shutdown (FALSE);
881 return;
882 }
883
884 if (!qmi_message_uim_power_on_sim_output_get_result (output, &error)) {
885 g_printerr ("error: could not power on SIM: %s\n", error->message);
886 g_error_free (error);
887 qmi_message_uim_power_on_sim_output_unref (output);
888 operation_shutdown (FALSE);
889 return;
890 }
891
892 g_print ("[%s] Successfully performed SIM power on\n",
893 qmi_device_get_path_display (ctx->device));
894
895 qmi_message_uim_power_on_sim_output_unref (output);
896 operation_shutdown (TRUE);
897 }
898
899 #endif /* HAVE_QMI_MESSAGE_UIM_POWER_ON_SIM */
900
901 #if defined HAVE_QMI_MESSAGE_UIM_POWER_OFF_SIM
902
903 static QmiMessageUimPowerOffSimInput *
904 power_off_sim_input_create (const gchar *slot_str)
905 {
906 QmiMessageUimPowerOffSimInput *input;
907 guint slot;
908 GError *error = NULL;
909
910 if (!qmicli_read_uint_from_string (slot_str, &slot) || (slot > G_MAXUINT8)) {
911 g_printerr ("error: invalid slot number\n");
912 return NULL;
913 }
914
915 input = qmi_message_uim_power_off_sim_input_new ();
916
917 if (!qmi_message_uim_power_off_sim_input_set_slot (input, slot, &error)) {
918 g_printerr ("error: could not create SIM power off input: %s\n", error->message);
919 g_error_free (error);
920 qmi_message_uim_power_off_sim_input_unref (input);
921 input = NULL;
922 }
923
924 return input;
925 }
926
927 static void
928 power_off_sim_ready (QmiClientUim *client,
929 GAsyncResult *res)
930 {
931 QmiMessageUimPowerOffSimOutput *output;
932 GError *error = NULL;
933
934 output = qmi_client_uim_power_off_sim_finish (client, res, &error);
935 if (!output) {
936 g_printerr ("error: operation failed: %s\n", error->message);
937 g_error_free (error);
938 operation_shutdown (FALSE);
939 return;
940 }
941
942 if (!qmi_message_uim_power_off_sim_output_get_result (output, &error)) {
943 g_printerr ("error: could not power off SIM: %s\n", error->message);
944 g_error_free (error);
945 qmi_message_uim_power_off_sim_output_unref (output);
946 operation_shutdown (FALSE);
947 return;
948 }
949
950 g_print ("[%s] Successfully performed SIM power off\n",
951 qmi_device_get_path_display (ctx->device));
952
953 qmi_message_uim_power_off_sim_output_unref (output);
954 operation_shutdown (TRUE);
955 }
956
957 #endif /* HAVE_QMI_MESSAGE_UIM_POWER_OFF_SIM */
958
959 #if defined HAVE_QMI_MESSAGE_UIM_CHANGE_PROVISIONING_SESSION
960
961 typedef struct {
962 QmiUimSessionType session_type;
963 gboolean session_type_set;
964 gboolean activate;
965 gboolean activate_set;
966 guint slot;
967 GArray *aid;
968 } SetChangeProvisioningSessionProperties;
969
970 static gboolean
971 set_change_provisioning_session_properties_handle (const gchar *key,
972 const gchar *value,
973 GError **error,
974 gpointer user_data)
975 {
976 SetChangeProvisioningSessionProperties *props = (SetChangeProvisioningSessionProperties *) user_data;
977
978 if (!value || !value[0]) {
979 g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED,
980 "key '%s' requires a value", key);
981 return FALSE;
982 }
983
984 if (g_ascii_strcasecmp (key, "session-type") == 0) {
985 if (!qmicli_read_uim_session_type_from_string (value, &props->session_type)) {
986 g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED,
987 "invalid session type value: %s (not a valid enum)", value);
988 return FALSE;
989 }
990 props->session_type_set = TRUE;
991 return TRUE;
992 }
993
994 if (g_ascii_strcasecmp (key, "activate") == 0) {
995 if (!qmicli_read_yes_no_from_string (value, &props->activate)) {
996 g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED,
997 "invalid activate value: %s (not a boolean)", value);
998 return FALSE;
999 }
1000 props->activate_set = TRUE;
1001 return TRUE;
1002 }
1003
1004 if (g_ascii_strcasecmp (key, "slot") == 0) {
1005 if (!qmicli_read_uint_from_string (value, &props->slot)) {
1006 g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED,
1007 "invalid slot value: %s (not a number)", value);
1008 return FALSE;
1009 }
1010 if (props->slot > G_MAXUINT8) {
1011 g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED,
1012 "invalid slot value: %s (out of range)", value);
1013 return FALSE;
1014 }
1015 return TRUE;
1016 }
1017
1018 if (g_ascii_strcasecmp (key, "aid") == 0) {
1019 if (!qmicli_read_raw_data_from_string (value, &props->aid)) {
1020 g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED,
1021 "invalid aid value: %s (not an hex string)", value);
1022 return FALSE;
1023 }
1024 return TRUE;
1025 }
1026
1027 g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED,
1028 "Unrecognized option '%s'", key);
1029 return FALSE;
1030 }
1031
1032 static QmiMessageUimChangeProvisioningSessionInput *
1033 change_provisioning_session_input_create (const gchar *str)
1034 {
1035 QmiMessageUimChangeProvisioningSessionInput *input;
1036 GError *error = NULL;
1037 SetChangeProvisioningSessionProperties props = { 0 };
1038
1039 input = qmi_message_uim_change_provisioning_session_input_new ();
1040
1041 if (!qmicli_parse_key_value_string (str,
1042 &error,
1043 set_change_provisioning_session_properties_handle,
1044 &props)) {
1045 g_printerr ("error: could not parse input string '%s': %s\n", str, error->message);
1046 g_error_free (error);
1047 g_clear_pointer (&input, qmi_message_uim_change_provisioning_session_input_unref);
1048 goto out;
1049 }
1050
1051 if (!props.session_type_set || !props.activate_set) {
1052 g_printerr ("error: mandatory fields 'session-type' and 'activate' not given\n");
1053 g_clear_pointer (&input, qmi_message_uim_change_provisioning_session_input_unref);
1054 goto out;
1055 }
1056
1057 qmi_message_uim_change_provisioning_session_input_set_session_change (
1058 input,
1059 props.session_type,
1060 props.activate,
1061 NULL);
1062
1063 if (props.slot || props.aid) {
1064 GArray *aid = NULL;
1065
1066 aid = props.aid ? g_array_ref (props.aid) : g_array_new (FALSE, FALSE, sizeof (guint8));
1067 qmi_message_uim_change_provisioning_session_input_set_application_information (
1068 input,
1069 props.slot,
1070 aid,
1071 NULL);
1072 g_array_unref (aid);
1073 }
1074
1075 out:
1076 return input;
1077 }
1078
1079 static void
1080 change_provisioning_session_ready (QmiClientUim *client,
1081 GAsyncResult *res)
1082 {
1083 QmiMessageUimChangeProvisioningSessionOutput *output;
1084 GError *error = NULL;
1085
1086 output = qmi_client_uim_change_provisioning_session_finish (client, res, &error);
1087 if (!output) {
1088 g_printerr ("error: operation failed: %s\n", error->message);
1089 g_error_free (error);
1090 operation_shutdown (FALSE);
1091 return;
1092 }
1093
1094 if (!qmi_message_uim_change_provisioning_session_output_get_result (output, &error)) {
1095 g_printerr ("error: could not power off SIM: %s\n", error->message);
1096 g_error_free (error);
1097 qmi_message_uim_change_provisioning_session_output_unref (output);
1098 operation_shutdown (FALSE);
1099 return;
1100 }
1101
1102 g_print ("[%s] Successfully changed provisioning session\n",
1103 qmi_device_get_path_display (ctx->device));
1104
1105 qmi_message_uim_change_provisioning_session_output_unref (output);
1106 operation_shutdown (TRUE);
1107 }
1108
1109 #endif /* HAVE_QMI_MESSAGE_UIM_CHANGE_PROVISIONING_SESSION */
1110
1111 #if (defined HAVE_QMI_MESSAGE_UIM_GET_SLOT_STATUS || \
1112 defined HAVE_QMI_INDICATION_UIM_SLOT_STATUS)
1113
1114 static const gchar bcd_chars[] = "0123456789\0\0\0\0\0\0";
1115
1116 static gchar *
1117 decode_iccid (const gchar *bcd, gsize bcd_len)
1118 {
1119 GString *str;
1120 gsize i;
1121
1122 if (!bcd)
1123 return NULL;
1124
1125 str = g_string_sized_new (bcd_len * 2 + 1);
1126 for (i = 0; i < bcd_len; i++) {
1127 str = g_string_append_c (str, bcd_chars[bcd[i] & 0xF]);
1128 str = g_string_append_c (str, bcd_chars[(bcd[i] >> 4) & 0xF]);
1129 }
1130 return g_string_free (str, FALSE);
1131 }
1132
1133 #define EID_LENGTH 16
1134
1135 static gchar *
1136 decode_eid (const gchar *eid, gsize eid_len)
1137 {
1138 GString *str;
1139 gsize i;
1140
1141 if (!eid)
1142 return NULL;
1143 if (eid_len != EID_LENGTH)
1144 return NULL;
1145
1146 str = g_string_sized_new (eid_len * 2 + 1);
1147 for (i = 0; i < eid_len; i++) {
1148 str = g_string_append_c (str, bcd_chars[(eid[i] >> 4) & 0xF]);
1149 str = g_string_append_c (str, bcd_chars[eid[i] & 0xF]);
1150 }
1151 return g_string_free (str, FALSE);
1152 }
1153
1154 static void
1155 print_slot_status (GArray *physical_slots,
1156 GArray *ext_information,
1157 GArray *slot_eids)
1158 {
1159 guint i;
1160
1161 if (ext_information && physical_slots->len != ext_information->len) {
1162 g_print ("Malformed extended information data");
1163 ext_information = NULL;
1164 }
1165
1166 if (slot_eids && physical_slots->len != slot_eids->len) {
1167 g_print ("Malformed slot EID data");
1168 slot_eids = NULL;
1169 }
1170
1171 for (i = 0; i < physical_slots->len; i++) {
1172 QmiPhysicalSlotStatusSlot *slot_status;
1173 QmiPhysicalSlotInformationSlot *slot_info = NULL;
1174 QmiSlotEidElement *slot_eid = NULL;
1175 g_autofree gchar *iccid = NULL;
1176 g_autofree gchar *eid = NULL;
1177
1178 slot_status = &g_array_index (physical_slots, QmiPhysicalSlotStatusSlot, i);
1179
1180 g_print (" Physical slot %u:\n", i + 1);
1181 g_print (" Card status: %s\n",
1182 qmi_uim_physical_card_state_get_string (slot_status->physical_card_status));
1183 g_print (" Slot status: %s\n",
1184 qmi_uim_slot_state_get_string (slot_status->physical_slot_status));
1185
1186 if (slot_status->physical_slot_status == QMI_UIM_SLOT_STATE_ACTIVE)
1187 g_print (" Logical slot: %u\n", slot_status->logical_slot);
1188
1189 if (slot_status->physical_card_status != QMI_UIM_PHYSICAL_CARD_STATE_PRESENT)
1190 continue;
1191
1192 if (slot_status->iccid->len)
1193 iccid = decode_iccid (slot_status->iccid->data, slot_status->iccid->len);
1194 g_print (" ICCID: %s\n", VALIDATE_UNKNOWN (iccid));
1195
1196 /* Extended information, if available */
1197 if (!ext_information)
1198 continue;
1199
1200 slot_info = &g_array_index (ext_information, QmiPhysicalSlotInformationSlot, i);
1201 g_print (" Protocol: %s\n",
1202 qmi_uim_card_protocol_get_string (slot_info->card_protocol));
1203 g_print (" Num apps: %u\n", slot_info->valid_applications);
1204 g_print (" Is eUICC: %s\n", slot_info->is_euicc ? "yes" : "no");
1205
1206 /* EID info, if available and this is an eUICC */
1207 if (!slot_info->is_euicc || !slot_eids)
1208 continue;
1209
1210 slot_eid = &g_array_index (slot_eids, QmiSlotEidElement, i);
1211 if (slot_eid->eid)
1212 eid = decode_eid (slot_eid->eid->data, slot_eid->eid->len);
1213 g_print (" EID: %s\n", VALIDATE_UNKNOWN (eid));
1214 }
1215 }
1216
1217 #endif
1218
1219 #if defined HAVE_QMI_MESSAGE_UIM_GET_SLOT_STATUS
1220
1221 static void
1222 get_slot_status_ready (QmiClientUim *client,
1223 GAsyncResult *res)
1224 {
1225 QmiMessageUimGetSlotStatusOutput *output;
1226 GArray *physical_slots;
1227 GArray *ext_information = NULL;
1228 GArray *slot_eids = NULL;
1229 GError *error = NULL;
1230
1231 output = qmi_client_uim_get_slot_status_finish (client, res, &error);
1232 if (!output) {
1233 g_printerr ("error: operation failed: %s\n", error->message);
1234 g_error_free (error);
1235 operation_shutdown (FALSE);
1236 return;
1237 }
1238
1239 if (!qmi_message_uim_get_slot_status_output_get_result (output, &error)) {
1240 g_printerr ("error: could not get slots status: %s\n", error->message);
1241 g_error_free (error);
1242 qmi_message_uim_get_slot_status_output_unref (output);
1243 operation_shutdown (FALSE);
1244 return;
1245 }
1246
1247 g_print ("[%s] Successfully got slots status\n",
1248 qmi_device_get_path_display (ctx->device));
1249
1250 if (!qmi_message_uim_get_slot_status_output_get_physical_slot_status (
1251 output, &physical_slots, &error)) {
1252 g_printerr ("error: could not parse slots status response: %s\n", error->message);
1253 g_error_free (error);
1254 qmi_message_uim_get_slot_status_output_unref (output);
1255 operation_shutdown (FALSE);
1256 return;
1257 }
1258
1259 /* Both of these are recoverable, just print less information per slot */
1260 qmi_message_uim_get_slot_status_output_get_physical_slot_information (output, &ext_information, NULL);
1261 qmi_message_uim_get_slot_status_output_get_slot_eid (output, &slot_eids, NULL);
1262
1263 g_print ("[%s] %u physical slots found:\n",
1264 qmi_device_get_path_display (ctx->device), physical_slots->len);
1265
1266 print_slot_status (physical_slots, ext_information, slot_eids);
1267
1268 qmi_message_uim_get_slot_status_output_unref (output);
1269 operation_shutdown (TRUE);
1270 }
1271
1272 #endif /* HAVE_QMI_MESSAGE_UIM_GET_SLOT_STATUS */
1273
1274 #if defined HAVE_QMI_MESSAGE_UIM_SWITCH_SLOT && defined HAVE_QMI_MESSAGE_UIM_GET_SLOT_STATUS
1275
1276 static void
1277 switch_slot_ready (QmiClientUim *client,
1278 GAsyncResult *res)
1279 {
1280 QmiMessageUimSwitchSlotOutput *output;
1281 GError *error = NULL;
1282
1283 output = qmi_client_uim_switch_slot_finish (client, res, &error);
1284 if (!output) {
1285 g_printerr ("error: operation failed: %s\n", error->message);
1286 g_error_free (error);
1287 operation_shutdown (FALSE);
1288 return;
1289 }
1290
1291 if (!qmi_message_uim_switch_slot_output_get_result (output, &error)) {
1292 g_printerr ("error: couldn't switch slots: %s\n", error->message);
1293 g_error_free (error);
1294 qmi_message_uim_switch_slot_output_unref (output);
1295 operation_shutdown (FALSE);
1296 return;
1297 }
1298
1299 g_print ("[%s] Successfully switched slots\n",
1300 qmi_device_get_path_display (ctx->device));
1301
1302 qmi_message_uim_switch_slot_output_unref (output);
1303 operation_shutdown (TRUE);
1304 }
1305
1306 static QmiMessageUimSwitchSlotInput *
1307 switch_slot_input_create (guint logical_slot,
1308 guint physical_slot)
1309 {
1310 QmiMessageUimSwitchSlotInput *input;
1311 GError *error = NULL;
1312
1313 input = qmi_message_uim_switch_slot_input_new ();
1314
1315 if (!qmi_message_uim_switch_slot_input_set_logical_slot (input, logical_slot, &error) ||
1316 !qmi_message_uim_switch_slot_input_set_physical_slot (input, physical_slot, &error)) {
1317 g_printerr ("error: could not create switch slot input: %s\n", error->message);
1318 g_error_free (error);
1319 g_clear_pointer (&input, qmi_message_uim_switch_slot_input_unref);
1320 }
1321
1322 return input;
1323 }
1324
1325 static void
1326 get_active_logical_slot_ready (QmiClientUim *client,
1327 GAsyncResult *res,
1328 gpointer user_data)
1329 {
1330 QmiMessageUimGetSlotStatusOutput *output;
1331 QmiMessageUimSwitchSlotInput *input;
1332 GArray *physical_slots;
1333 guint physical_slot_id;
1334 guint active_logical_slot_id = 0;
1335 guint i;
1336 GError *error = NULL;
1337
1338 physical_slot_id = GPOINTER_TO_UINT (user_data);
1339
1340 output = qmi_client_uim_get_slot_status_finish (client, res, &error);
1341 if (!output) {
1342 g_printerr ("error: operation failed: %s\n", error->message);
1343 g_error_free (error);
1344 operation_shutdown (FALSE);
1345 return;
1346 }
1347
1348 if (!qmi_message_uim_get_slot_status_output_get_result (output, &error)) {
1349 g_printerr ("error: could not get slots status: %s\n", error->message);
1350 g_error_free (error);
1351 qmi_message_uim_get_slot_status_output_unref (output);
1352 operation_shutdown (FALSE);
1353 return;
1354 }
1355
1356 if (!qmi_message_uim_get_slot_status_output_get_physical_slot_status (
1357 output, &physical_slots, &error)) {
1358 g_printerr ("error: could not parse slots status response: %s\n", error->message);
1359 g_error_free (error);
1360 qmi_message_uim_get_slot_status_output_unref (output);
1361 operation_shutdown (FALSE);
1362 return;
1363 }
1364
1365 /* Ensure the physical slot is available. */
1366 if (physical_slot_id > physical_slots->len) {
1367 g_printerr ("error: physical slot %u is unavailable\n", physical_slot_id);
1368 qmi_message_uim_get_slot_status_output_unref (output);
1369 operation_shutdown (FALSE);
1370 return;
1371 }
1372
1373 /* Find active logical slot */
1374 for (i = 0; i < physical_slots->len; i++) {
1375 QmiPhysicalSlotStatusSlot *slot_status;
1376
1377 slot_status = &g_array_index (physical_slots, QmiPhysicalSlotStatusSlot, i);
1378 if (slot_status->physical_slot_status == QMI_UIM_SLOT_STATE_ACTIVE) {
1379 active_logical_slot_id = slot_status->logical_slot;
1380 break;
1381 }
1382 }
1383 qmi_message_uim_get_slot_status_output_unref (output);
1384
1385 if (active_logical_slot_id == 0) {
1386 g_printerr ("error: no active logical slot\n");
1387 operation_shutdown (FALSE);
1388 return;
1389 }
1390
1391 input = switch_slot_input_create (active_logical_slot_id, physical_slot_id);
1392 qmi_client_uim_switch_slot (ctx->client,
1393 input,
1394 10,
1395 ctx->cancellable,
1396 (GAsyncReadyCallback)switch_slot_ready,
1397 ctx);
1398 qmi_message_uim_switch_slot_input_unref (input);
1399 }
1400
1401 #endif /* HAVE_QMI_MESSAGE_UIM_SWITCH_SLOT && HAVE_QMI_MESSAGE_UIM_GET_SLOT_STATUS */
1402
1403 #if defined HAVE_QMI_INDICATION_UIM_SLOT_STATUS
1404
1405 static void
1406 monitoring_cancelled (GCancellable *cancellable)
1407 {
1408 operation_shutdown (TRUE);
1409 }
1410
1411 static void
1412 slot_status_received (QmiClientUim *client,
1413 QmiIndicationUimSlotStatusOutput *output)
1414 {
1415 GArray *physical_slots;
1416 GArray *ext_information = NULL;
1417 GArray *slot_eids = NULL;
1418 GError *error = NULL;
1419
1420 g_print ("[%s] Received slot status indication:\n",
1421 qmi_device_get_path_display (ctx->device));
1422
1423 if (!qmi_indication_uim_slot_status_output_get_physical_slot_status (
1424 output, &physical_slots, &error)) {
1425 g_printerr ("error: could not parse slots status: %s\n", error->message);
1426 g_error_free (error);
1427 operation_shutdown (FALSE);
1428 return;
1429 }
1430
1431 /* Both of these are recoverable, just print less information per slot */
1432 qmi_indication_uim_slot_status_output_get_physical_slot_information (output, &ext_information, NULL);
1433 qmi_indication_uim_slot_status_output_get_slot_eid (output, &slot_eids, NULL);
1434
1435 print_slot_status (physical_slots, ext_information, slot_eids);
1436 }
1437
1438 static void
1439 register_physical_slot_status_events_ready (QmiClientUim *client,
1440 GAsyncResult *res)
1441 {
1442 QmiMessageUimRegisterEventsOutput *output;
1443 GError *error = NULL;
1444
1445 output = qmi_client_uim_register_events_finish (client, res, &error);
1446 if (!output) {
1447 g_printerr ("error: operation failed: %s\n", error->message);
1448 g_error_free (error);
1449 operation_shutdown (FALSE);
1450 return;
1451 }
1452
1453 if (!qmi_message_uim_register_events_output_get_result (output, &error)) {
1454 g_printerr ("error: could not register slot status change events: %s\n", error->message);
1455 qmi_message_uim_register_events_output_unref (output);
1456 g_error_free (error);
1457 operation_shutdown (FALSE);
1458 return;
1459 }
1460
1461 g_debug ("Registered physical slot status change events...");
1462 ctx->slot_status_indication_id =
1463 g_signal_connect (ctx->client,
1464 "slot-status",
1465 G_CALLBACK (slot_status_received),
1466 NULL);
1467
1468 /* User can use Ctrl+C to cancel the monitoring at any time */
1469 g_cancellable_connect (ctx->cancellable,
1470 G_CALLBACK (monitoring_cancelled),
1471 NULL,
1472 NULL);
1473 }
1474
1475 static void
1476 register_physical_slot_status_events (void)
1477 {
1478 QmiMessageUimRegisterEventsInput *re_input;
1479
1480 re_input = qmi_message_uim_register_events_input_new ();
1481 qmi_message_uim_register_events_input_set_event_registration_mask (
1482 re_input, QMI_UIM_EVENT_REGISTRATION_FLAG_PHYSICAL_SLOT_STATUS, NULL);
1483 qmi_client_uim_register_events (
1484 ctx->client,
1485 re_input,
1486 10,
1487 ctx->cancellable,
1488 (GAsyncReadyCallback) register_physical_slot_status_events_ready,
1489 NULL);
1490 qmi_message_uim_register_events_input_unref (re_input);
1491 }
1492
1493 #endif /* HAVE_QMI_INDICATION_UIM_SLOT_STATUS */
1494
1495 #if defined HAVE_QMI_MESSAGE_UIM_RESET
1496
1497 static void
1498 reset_ready (QmiClientUim *client,
1499 GAsyncResult *res)
1500 {
1501 QmiMessageUimResetOutput *output;
1502 GError *error = NULL;
1503
1504 output = qmi_client_uim_reset_finish (client, res, &error);
1505 if (!output) {
1506 g_printerr ("error: operation failed: %s\n", error->message);
1507 g_error_free (error);
1508 operation_shutdown (FALSE);
1509 return;
1510 }
1511
1512 if (!qmi_message_uim_reset_output_get_result (output, &error)) {
1513 g_printerr ("error: couldn't reset the UIM service: %s\n", error->message);
1514 g_error_free (error);
1515 qmi_message_uim_reset_output_unref (output);
1516 operation_shutdown (FALSE);
1517 return;
1518 }
1519
1520 g_print ("[%s] Successfully performed UIM service reset\n",
1521 qmi_device_get_path_display (ctx->device));
1522
1523 qmi_message_uim_reset_output_unref (output);
1524 operation_shutdown (TRUE);
1525 }
1526
1527 #endif
1528
1529 static gboolean
1530 noop_cb (gpointer unused)
1531 {
1532 operation_shutdown (TRUE);
1533 return FALSE;
1534 }
1535
1536 #if defined HAVE_QMI_MESSAGE_UIM_GET_CARD_STATUS
1537
1538 static void
1539 get_card_status_ready (QmiClientUim *client,
1540 GAsyncResult *res)
1541 {
1542 QmiMessageUimGetCardStatusOutput *output;
1543 GError *error = NULL;
1544 guint16 index_gw_primary;
1545 guint16 index_1x_primary;
1546 guint16 index_gw_secondary;
1547 guint16 index_1x_secondary;
1548 GArray *cards;
1549 guint i;
1550
1551 output = qmi_client_uim_get_card_status_finish (client, res, &error);
1552 if (!output) {
1553 g_printerr ("error: operation failed: %s\n", error->message);
1554 g_error_free (error);
1555 operation_shutdown (FALSE);
1556 return;
1557 }
1558
1559 if (!qmi_message_uim_get_card_status_output_get_result (output, &error)) {
1560 g_printerr ("error: couldn't get card status: %s\n", error->message);
1561 g_error_free (error);
1562 qmi_message_uim_get_card_status_output_unref (output);
1563 operation_shutdown (FALSE);
1564 return;
1565 }
1566
1567 g_print ("[%s] Successfully got card status\n",
1568 qmi_device_get_path_display (ctx->device));
1569
1570 qmi_message_uim_get_card_status_output_get_card_status (
1571 output,
1572 &index_gw_primary,
1573 &index_1x_primary,
1574 &index_gw_secondary,
1575 &index_1x_secondary,
1576 &cards,
1577 NULL);
1578
1579 g_print ("Provisioning applications:\n");
1580 if (index_gw_primary == 0xFFFF)
1581 g_print ("\tPrimary GW: session doesn't exist\n");
1582 else
1583 g_print ("\tPrimary GW: slot '%u', application '%u'\n",
1584 ((index_gw_primary & 0xFF00) >> 8) + 1,
1585 ((index_gw_primary & 0x00FF)) + 1);
1586
1587 if (index_1x_primary == 0xFFFF)
1588 g_print ("\tPrimary 1X: session doesn't exist\n");
1589 else
1590 g_print ("\tPrimary 1X: slot '%u', application '%u'\n",
1591 ((index_1x_primary & 0xFF00) >> 8) + 1,
1592 ((index_1x_primary & 0x00FF)) + 1);
1593
1594 if (index_gw_secondary == 0xFFFF)
1595 g_print ("\tSecondary GW: session doesn't exist\n");
1596 else
1597 g_print ("\tSecondary GW: slot '%u', application '%u'\n",
1598 ((index_gw_secondary & 0xFF00) >> 8) + 1,
1599 ((index_gw_secondary & 0x00FF)) + 1);
1600
1601 if (index_1x_secondary == 0xFFFF)
1602 g_print ("\tSecondary 1X: session doesn't exist\n");
1603 else
1604 g_print ("\tSecondary 1X: slot '%u', application '%u'\n",
1605 ((index_1x_secondary & 0xFF00) >> 8) + 1,
1606 ((index_1x_secondary & 0x00FF)) + 1);
1607
1608 for (i = 0; i < cards->len; i++) {
1609 QmiMessageUimGetCardStatusOutputCardStatusCardsElement *card;
1610 guint j;
1611
1612 card = &g_array_index (cards, QmiMessageUimGetCardStatusOutputCardStatusCardsElement, i);
1613
1614 g_print ("Slot [%u]:\n", i + 1);
1615
1616 if (card->card_state != QMI_UIM_CARD_STATE_ERROR)
1617 g_print ("\tCard state: '%s'\n",
1618 qmi_uim_card_state_get_string (card->card_state));
1619 else
1620 g_print ("\tCard state: '%s: %s (%u)'\n",
1621 qmi_uim_card_state_get_string (card->card_state),
1622 VALIDATE_UNKNOWN (qmi_uim_card_error_get_string (card->error_code)),
1623 card->error_code);
1624 g_print ("\tUPIN state: '%s'\n"
1625 "\t\tUPIN retries: '%u'\n"
1626 "\t\tUPUK retries: '%u'\n",
1627 qmi_uim_pin_state_get_string (card->upin_state),
1628 card->upin_retries,
1629 card->upuk_retries);
1630
1631 for (j = 0; j < card->applications->len; j++) {
1632 QmiMessageUimGetCardStatusOutputCardStatusCardsElementApplicationsElementV2 *app;
1633 gchar *str;
1634
1635 app = &g_array_index (card->applications, QmiMessageUimGetCardStatusOutputCardStatusCardsElementApplicationsElementV2, j);
1636
1637 str = qmicli_get_raw_data_printable (app->application_identifier_value, 80, "");
1638
1639 g_print ("\tApplication [%u]:\n"
1640 "\t\tApplication type: '%s (%u)'\n"
1641 "\t\tApplication state: '%s'\n"
1642 "\t\tApplication ID:\n"
1643 "\t\t\t%s",
1644 j + 1,
1645 VALIDATE_UNKNOWN (qmi_uim_card_application_type_get_string (app->type)), app->type,
1646 qmi_uim_card_application_state_get_string (app->state),
1647 str);
1648
1649 if (app->personalization_state == QMI_UIM_CARD_APPLICATION_PERSONALIZATION_STATE_CODE_REQUIRED ||
1650 app->personalization_state == QMI_UIM_CARD_APPLICATION_PERSONALIZATION_STATE_PUK_CODE_REQUIRED)
1651 g_print ("\t\tPersonalization state: '%s' (feature: %s)\n"
1652 "\t\t\tDisable retries: '%u'\n"
1653 "\t\t\tUnblock retries: '%u'\n",
1654 qmi_uim_card_application_personalization_state_get_string (app->personalization_state),
1655 qmi_uim_card_application_personalization_feature_status_get_string (app->personalization_feature),
1656 app->personalization_retries,
1657 app->personalization_unblock_retries);
1658 else
1659 g_print ("\t\tPersonalization state: '%s'\n",
1660 qmi_uim_card_application_personalization_state_get_string (app->personalization_state));
1661
1662 g_print ("\t\tUPIN replaces PIN1: '%s'\n",
1663 app->upin_replaces_pin1 ? "yes" : "no");
1664
1665 g_print ("\t\tPIN1 state: '%s'\n"
1666 "\t\t\tPIN1 retries: '%u'\n"
1667 "\t\t\tPUK1 retries: '%u'\n"
1668 "\t\tPIN2 state: '%s'\n"
1669 "\t\t\tPIN2 retries: '%u'\n"
1670 "\t\t\tPUK2 retries: '%u'\n",
1671 qmi_uim_pin_state_get_string (app->pin1_state),
1672 app->pin1_retries,
1673 app->puk1_retries,
1674 qmi_uim_pin_state_get_string (app->pin2_state),
1675 app->pin2_retries,
1676 app->puk2_retries);
1677 g_free (str);
1678 }
1679 }
1680
1681 qmi_message_uim_get_card_status_output_unref (output);
1682 operation_shutdown (TRUE);
1683 }
1684
1685 #endif /* defined HAVE_QMI_MESSAGE_UIM_GET_CARD_STATUS */
1686
1687 #if defined HAVE_QMI_MESSAGE_UIM_READ_TRANSPARENT || \
1688 defined HAVE_QMI_MESSAGE_UIM_READ_RECORD || \
1689 defined HAVE_QMI_MESSAGE_UIM_GET_FILE_ATTRIBUTES
1690
1691 static gboolean
1692 get_sim_file_id_and_path_with_separator (const gchar *file_path_str,
1693 guint16 *file_id,
1694 GArray **file_path,
1695 const gchar *separator)
1696 {
1697 guint i;
1698 gchar **split;
1699
1700 split = g_strsplit (file_path_str, separator, -1);
1701 if (!split) {
1702 g_printerr ("error: invalid file path given: '%s'\n", file_path_str);
1703 return FALSE;
1704 }
1705
1706 *file_path = g_array_sized_new (FALSE,
1707 FALSE,
1708 sizeof (guint8),
1709 g_strv_length (split) - 1);
1710
1711 *file_id = 0;
1712 for (i = 0; split[i]; i++) {
1713 gulong path_item;
1714
1715 path_item = (strtoul (split[i], NULL, 16)) & 0xFFFF;
1716
1717 /* If there are more fields, this is part of the path; otherwise it's
1718 * the file id */
1719 if (split[i + 1]) {
1720 guint8 val;
1721
1722 val = path_item & 0xFF;
1723 g_array_append_val (*file_path, val);
1724 val = (path_item >> 8) & 0xFF;
1725 g_array_append_val (*file_path, val);
1726 } else {
1727 *file_id = path_item;
1728 }
1729 }
1730
1731 g_strfreev (split);
1732
1733 if (*file_id == 0) {
1734 g_array_unref (*file_path);
1735 g_printerr ("error: invalid file path given: '%s'\n", file_path_str);
1736 return FALSE;
1737 }
1738
1739 return TRUE;
1740 }
1741
1742 #endif /* HAVE_QMI_MESSAGE_UIM_READ_TRANSPARENT
1743 * HAVE_QMI_MESSAGE_UIM_READ_RECORD
1744 * HAVE_QMI_MESSAGE_UIM_GET_FILE_ATTRIBUTES */
1745
1746 #if defined HAVE_QMI_MESSAGE_UIM_READ_TRANSPARENT || \
1747 defined HAVE_QMI_MESSAGE_UIM_GET_FILE_ATTRIBUTES || \
1748 defined HAVE_QMI_MESSAGE_UIM_REFRESH_REGISTER
1749
1750 static gboolean
1751 get_sim_file_id_and_path (const gchar *file_path_str,
1752 guint16 *file_id,
1753 GArray **file_path)
1754 {
1755 return get_sim_file_id_and_path_with_separator (file_path_str, file_id, file_path, ",");
1756 }
1757
1758 #endif /* HAVE_QMI_MESSAGE_UIM_READ_TRANSPARENT
1759 * HAVE_QMI_MESSAGE_UIM_GET_FILE_ATTRIBUTES
1760 * HAVE_QMI_MESSAGE_UIM_REFRESH_REGISTER */
1761
1762 #if defined HAVE_QMI_MESSAGE_UIM_READ_TRANSPARENT
1763
1764 static void
1765 read_transparent_ready (QmiClientUim *client,
1766 GAsyncResult *res)
1767 {
1768 QmiMessageUimReadTransparentOutput *output;
1769 GError *error = NULL;
1770 guint8 sw1 = 0;
1771 guint8 sw2 = 0;
1772 GArray *read_result = NULL;
1773
1774 output = qmi_client_uim_read_transparent_finish (client, res, &error);
1775 if (!output) {
1776 g_printerr ("error: operation failed: %s\n", error->message);
1777 g_error_free (error);
1778 operation_shutdown (FALSE);
1779 return;
1780 }
1781
1782 if (!qmi_message_uim_read_transparent_output_get_result (output, &error)) {
1783 g_printerr ("error: couldn't read transparent file from the UIM: %s\n", error->message);
1784 g_error_free (error);
1785
1786 /* Card result */
1787 if (qmi_message_uim_read_transparent_output_get_card_result (
1788 output,
1789 &sw1,
1790 &sw2,
1791 NULL)) {
1792 g_print ("Card result:\n"
1793 "\tSW1: '0x%02x'\n"
1794 "\tSW2: '0x%02x'\n",
1795 sw1, sw2);
1796 }
1797
1798 qmi_message_uim_read_transparent_output_unref (output);
1799 operation_shutdown (FALSE);
1800 return;
1801 }
1802
1803 g_print ("[%s] Successfully read information from the UIM:\n",
1804 qmi_device_get_path_display (ctx->device));
1805
1806 /* Card result */
1807 if (qmi_message_uim_read_transparent_output_get_card_result (
1808 output,
1809 &sw1,
1810 &sw2,
1811 NULL)) {
1812 g_print ("Card result:\n"
1813 "\tSW1: '0x%02x'\n"
1814 "\tSW2: '0x%02x'\n",
1815 sw1, sw2);
1816 }
1817
1818 /* Read result */
1819 if (qmi_message_uim_read_transparent_output_get_read_result (
1820 output,
1821 &read_result,
1822 NULL)) {
1823 gchar *str;
1824
1825 str = qmicli_get_raw_data_printable (read_result, 80, "\t");
1826 g_print ("Read result:\n"
1827 "%s\n",
1828 str);
1829 g_free (str);
1830 }
1831
1832 qmi_message_uim_read_transparent_output_unref (output);
1833 operation_shutdown (TRUE);
1834 }
1835
1836 static QmiMessageUimReadTransparentInput *
1837 read_transparent_build_input (const gchar *file_path_str)
1838 {
1839 QmiMessageUimReadTransparentInput *input;
1840 guint16 file_id = 0;
1841 GArray *file_path = NULL;
1842 GArray *placeholder_aid;
1843
1844 if (!get_sim_file_id_and_path (file_path_str, &file_id, &file_path))
1845 return NULL;
1846
1847 placeholder_aid = g_array_new (FALSE, FALSE, sizeof (guint8));
1848
1849 input = qmi_message_uim_read_transparent_input_new ();
1850 qmi_message_uim_read_transparent_input_set_session (
1851 input,
1852 QMI_UIM_SESSION_TYPE_PRIMARY_GW_PROVISIONING,
1853 placeholder_aid, /* ignored */
1854 NULL);
1855 qmi_message_uim_read_transparent_input_set_file (
1856 input,
1857 file_id,
1858 file_path,
1859 NULL);
1860 qmi_message_uim_read_transparent_input_set_read_information (input, 0, 0, NULL);
1861 g_array_unref (file_path);
1862 g_array_unref (placeholder_aid);
1863 return input;
1864 }
1865
1866 #endif /* HAVE_QMI_MESSAGE_UIM_READ_TRANSPARENT */
1867
1868 #if defined HAVE_QMI_MESSAGE_UIM_READ_RECORD
1869
1870 static void
1871 read_record_ready (QmiClientUim *client,
1872 GAsyncResult *res)
1873 {
1874 QmiMessageUimReadRecordOutput *output;
1875 GError *error = NULL;
1876 guint8 sw1 = 0;
1877 guint8 sw2 = 0;
1878 GArray *read_result = NULL;
1879
1880 output = qmi_client_uim_read_record_finish (client, res, &error);
1881 if (!output) {
1882 g_printerr ("error: operation failed: %s\n", error->message);
1883 g_error_free (error);
1884 operation_shutdown (FALSE);
1885 return;
1886 }
1887
1888 if (!qmi_message_uim_read_record_output_get_result (output, &error)) {
1889 g_printerr ("error: couldn't read record file from the UIM: %s\n", error->message);
1890 g_error_free (error);
1891
1892 /* Card result */
1893 if (qmi_message_uim_read_record_output_get_card_result (
1894 output,
1895 &sw1,
1896 &sw2,
1897 NULL)) {
1898 g_print ("Card result:\n"
1899 "\tSW1: '0x%02x'\n"
1900 "\tSW2: '0x%02x'\n",
1901 sw1, sw2);
1902 }
1903
1904 qmi_message_uim_read_record_output_unref (output);
1905 operation_shutdown (FALSE);
1906 return;
1907 }
1908
1909 g_print ("[%s] Successfully read information from the UIM:\n",
1910 qmi_device_get_path_display (ctx->device));
1911
1912 /* Card result */
1913 if (qmi_message_uim_read_record_output_get_card_result (
1914 output,
1915 &sw1,
1916 &sw2,
1917 NULL)) {
1918 g_print ("Card result:\n"
1919 "\tSW1: '0x%02x'\n"
1920 "\tSW2: '0x%02x'\n",
1921 sw1, sw2);
1922 }
1923
1924 /* Read result */
1925 if (qmi_message_uim_read_record_output_get_read_result (
1926 output,
1927 &read_result,
1928 NULL)) {
1929 gchar *str;
1930
1931 str = qmicli_get_raw_data_printable (read_result, 80, "\t");
1932 g_print ("Read result:\n"
1933 "%s\n",
1934 str);
1935 g_free (str);
1936 }
1937
1938 qmi_message_uim_read_record_output_unref (output);
1939 operation_shutdown (TRUE);
1940 }
1941
1942 typedef struct {
1943 char *file;
1944 guint16 record_number;
1945 guint16 record_length;
1946 } SetReadRecordProperties;
1947
1948 static gboolean
1949 set_read_record_properties_handle (const gchar *key,
1950 const gchar *value,
1951 GError **error,
1952 gpointer user_data)
1953 {
1954 SetReadRecordProperties *props = (SetReadRecordProperties *) user_data;
1955
1956 if (!value || !value[0]) {
1957 g_set_error (error,
1958 QMI_CORE_ERROR,
1959 QMI_CORE_ERROR_FAILED,
1960 "key '%s' requires a value",
1961 key);
1962 return FALSE;
1963 }
1964
1965 if (g_ascii_strcasecmp (key, "file") == 0) {
1966 props->file = g_strdup (value);
1967 return TRUE;
1968 }
1969
1970 if (g_ascii_strcasecmp (key, "record-number") == 0) {
1971 guint aux;
1972
1973 if (!qmicli_read_uint_from_string (value, &aux) || (aux > G_MAXUINT16)) {
1974 g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED,
1975 "failed reading key 'record-number' as 16bit value");
1976 return FALSE;
1977 }
1978 props->record_number = (guint16) aux;
1979 return TRUE;
1980 }
1981
1982 if (g_ascii_strcasecmp (key, "record-length") == 0) {
1983 guint aux;
1984
1985 if (!qmicli_read_uint_from_string (value, &aux) || (aux > G_MAXUINT16)) {
1986 g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED,
1987 "failed reading key 'record-length' as 16bit value");
1988 return FALSE;
1989 }
1990 props->record_length = (guint16) aux;
1991 return TRUE;
1992 }
1993
1994 g_set_error (error,
1995 QMI_CORE_ERROR,
1996 QMI_CORE_ERROR_FAILED,
1997 "Unrecognized option '%s'",
1998 key);
1999 return FALSE;
2000 }
2001
2002 static QmiMessageUimReadRecordInput *
2003 read_record_input_create (const gchar *str)
2004 {
2005 GError *error = NULL;
2006 QmiMessageUimReadRecordInput *input = NULL;
2007 SetReadRecordProperties props = {
2008 .file = NULL,
2009 .record_number = 0,
2010 .record_length = 0,
2011 };
2012 guint16 file_id = 0;
2013 GArray *file_path = NULL;
2014 GArray *placeholder_aid;
2015
2016 if (!qmicli_parse_key_value_string (str,
2017 &error,
2018 set_read_record_properties_handle,
2019 &props)) {
2020 g_printerr ("error: could not parse input string '%s': %s\n",
2021 str,
2022 error->message);
2023 g_error_free (error);
2024 goto out;
2025 }
2026
2027 if (!get_sim_file_id_and_path_with_separator (props.file, &file_id, &file_path, "-"))
2028 goto out;
2029
2030 placeholder_aid = g_array_new (FALSE, FALSE, sizeof (guint8));
2031
2032 input = qmi_message_uim_read_record_input_new ();
2033
2034 qmi_message_uim_read_record_input_set_session (
2035 input,
2036 QMI_UIM_SESSION_TYPE_PRIMARY_GW_PROVISIONING,
2037 placeholder_aid, /* ignored */
2038 NULL);
2039 qmi_message_uim_read_record_input_set_file (
2040 input,
2041 file_id,
2042 file_path,
2043 NULL);
2044 qmi_message_uim_read_record_input_set_record (
2045 input,
2046 props.record_number,
2047 props.record_length,
2048 NULL);
2049
2050 g_array_unref (placeholder_aid);
2051
2052 out:
2053 free (props.file);
2054 g_array_unref (file_path);
2055 return input;
2056 }
2057
2058 #endif /* HAVE_QMI_MESSAGE_UIM_READ_RECORD */
2059
2060 #if defined HAVE_QMI_MESSAGE_UIM_GET_FILE_ATTRIBUTES
2061
2062 static void
2063 get_file_attributes_ready (QmiClientUim *client,
2064 GAsyncResult *res,
2065 gchar *file_name)
2066 {
2067 QmiMessageUimGetFileAttributesOutput *output;
2068 GError *error = NULL;
2069 guint8 sw1 = 0;
2070 guint8 sw2 = 0;
2071 guint16 file_size;
2072 guint16 file_id;
2073 QmiUimFileType file_type;
2074 guint16 record_size;
2075 guint16 record_count;
2076 QmiUimSecurityAttributeLogic read_security_attributes_logic;
2077 QmiUimSecurityAttribute read_security_attributes;
2078 QmiUimSecurityAttributeLogic write_security_attributes_logic;
2079 QmiUimSecurityAttribute write_security_attributes;
2080 QmiUimSecurityAttributeLogic increase_security_attributes_logic;
2081 QmiUimSecurityAttribute increase_security_attributes;
2082 QmiUimSecurityAttributeLogic deactivate_security_attributes_logic;
2083 QmiUimSecurityAttribute deactivate_security_attributes;
2084 QmiUimSecurityAttributeLogic activate_security_attributes_logic;
2085 QmiUimSecurityAttribute activate_security_attributes;
2086 GArray *raw = NULL;
2087
2088 output = qmi_client_uim_get_file_attributes_finish (client, res, &error);
2089 if (!output) {
2090 g_printerr ("error: operation failed: %s\n", error->message);
2091 g_error_free (error);
2092 operation_shutdown (FALSE);
2093 g_free (file_name);
2094 return;
2095 }
2096
2097 if (!qmi_message_uim_get_file_attributes_output_get_result (output, &error)) {
2098 g_printerr ("error: couldn't get '%s' file attributes from the UIM: %s\n",
2099 file_name,
2100 error->message);
2101 g_error_free (error);
2102
2103 /* Card result */
2104 if (qmi_message_uim_get_file_attributes_output_get_card_result (
2105 output,
2106 &sw1,
2107 &sw2,
2108 NULL)) {
2109 g_print ("Card result:\n"
2110 "\tSW1: '0x%02x'\n"
2111 "\tSW2: '0x%02x'\n",
2112 sw1, sw2);
2113 }
2114
2115 qmi_message_uim_get_file_attributes_output_unref (output);
2116 operation_shutdown (FALSE);
2117 g_free (file_name);
2118 return;
2119 }
2120
2121 g_print ("[%s] Successfully got file '%s' attributes from the UIM:\n",
2122 file_name,
2123 qmi_device_get_path_display (ctx->device));
2124
2125 /* Card result */
2126 if (qmi_message_uim_get_file_attributes_output_get_card_result (
2127 output,
2128 &sw1,
2129 &sw2,
2130 NULL)) {
2131 g_print ("Card result:\n"
2132 "\tSW1: '0x%02x'\n"
2133 "\tSW2: '0x%02x'\n",
2134 sw1, sw2);
2135 }
2136
2137 /* File attributes */
2138 if (qmi_message_uim_get_file_attributes_output_get_file_attributes (
2139 output,
2140 &file_size,
2141 &file_id,
2142 &file_type,
2143 &record_size,
2144 &record_count,
2145 &read_security_attributes_logic,
2146 &read_security_attributes,
2147 &write_security_attributes_logic,
2148 &write_security_attributes,
2149 &increase_security_attributes_logic,
2150 &increase_security_attributes,
2151 &deactivate_security_attributes_logic,
2152 &deactivate_security_attributes,
2153 &activate_security_attributes_logic,
2154 &activate_security_attributes,
2155 &raw,
2156 NULL)) {
2157 g_autofree gchar *read_security_attributes_str = NULL;
2158 g_autofree gchar *write_security_attributes_str = NULL;
2159 g_autofree gchar *increase_security_attributes_str = NULL;
2160 g_autofree gchar *deactivate_security_attributes_str = NULL;
2161 g_autofree gchar *activate_security_attributes_str = NULL;
2162 g_autofree gchar *raw_str = NULL;
2163
2164 g_print ("File attributes:\n");
2165 g_print ("\tFile size: %u\n", (guint)file_size);
2166 g_print ("\tFile ID: %u\n", (guint)file_id);
2167 g_print ("\tFile type: %s\n", qmi_uim_file_type_get_string (file_type));
2168 g_print ("\tRecord size: %u\n", (guint)record_size);
2169 g_print ("\tRecord count: %u\n", (guint)record_count);
2170
2171 read_security_attributes_str = qmi_uim_security_attribute_build_string_from_mask (read_security_attributes);
2172 g_print ("\tRead security attributes: (%s) %s\n",
2173 qmi_uim_security_attribute_logic_get_string (read_security_attributes_logic),
2174 VALIDATE_MASK_NONE (read_security_attributes_str));
2175
2176 write_security_attributes_str = qmi_uim_security_attribute_build_string_from_mask (write_security_attributes);
2177 g_print ("\tWrite security attributes: (%s) %s\n",
2178 qmi_uim_security_attribute_logic_get_string (write_security_attributes_logic),
2179 VALIDATE_MASK_NONE (write_security_attributes_str));
2180
2181 increase_security_attributes_str = qmi_uim_security_attribute_build_string_from_mask (increase_security_attributes);
2182 g_print ("\tIncrease security attributes: (%s) %s\n",
2183 qmi_uim_security_attribute_logic_get_string (increase_security_attributes_logic),
2184 VALIDATE_MASK_NONE (increase_security_attributes_str));
2185
2186 deactivate_security_attributes_str = qmi_uim_security_attribute_build_string_from_mask (deactivate_security_attributes);
2187 g_print ("\tDeactivate security attributes: (%s) %s\n",
2188 qmi_uim_security_attribute_logic_get_string (deactivate_security_attributes_logic),
2189 VALIDATE_MASK_NONE (deactivate_security_attributes_str));
2190
2191 activate_security_attributes_str = qmi_uim_security_attribute_build_string_from_mask (activate_security_attributes);
2192 g_print ("\tActivate security attributes: (%s) %s\n",
2193 qmi_uim_security_attribute_logic_get_string (activate_security_attributes_logic),
2194 VALIDATE_MASK_NONE (activate_security_attributes_str));
2195
2196 raw_str = qmicli_get_raw_data_printable (raw, 80, "\t");
2197 g_print ("\tRaw: %s\n", raw_str);
2198 }
2199
2200 qmi_message_uim_get_file_attributes_output_unref (output);
2201 operation_shutdown (TRUE);
2202 }
2203
2204 static QmiMessageUimGetFileAttributesInput *
2205 get_file_attributes_build_input (const gchar *file_path_str)
2206 {
2207 QmiMessageUimGetFileAttributesInput *input;
2208 guint16 file_id = 0;
2209 GArray *file_path = NULL;
2210 GArray *placeholder_aid;
2211
2212 if (!get_sim_file_id_and_path (file_path_str, &file_id, &file_path))
2213 return NULL;
2214
2215 placeholder_aid = g_array_new (FALSE, FALSE, sizeof (guint8));
2216
2217 input = qmi_message_uim_get_file_attributes_input_new ();
2218 qmi_message_uim_get_file_attributes_input_set_session (
2219 input,
2220 QMI_UIM_SESSION_TYPE_PRIMARY_GW_PROVISIONING,
2221 placeholder_aid, /* ignored */
2222 NULL);
2223 qmi_message_uim_get_file_attributes_input_set_file (
2224 input,
2225 file_id,
2226 file_path,
2227 NULL);
2228 g_array_unref (placeholder_aid);
2229 g_array_unref (file_path);
2230 return input;
2231 }
2232
2233 #endif /* HAVE_QMI_MESSAGE_UIM_GET_FILE_ATTRIBUTES */
2234
2235 #if defined HAVE_QMI_MESSAGE_UIM_REFRESH_REGISTER || \
2236 defined HAVE_QMI_MESSAGE_UIM_REFRESH_REGISTER_ALL
2237
2238 static void
2239 refresh_complete_ready (QmiClientUim *client,
2240 GAsyncResult *res)
2241 {
2242 QmiMessageUimRefreshCompleteOutput *output;
2243 GError *error = NULL;
2244
2245 output = qmi_client_uim_refresh_complete_finish (client, res, &error);
2246 if (!output) {
2247 g_printerr ("error: refresh complete failed: %s\n", error->message);
2248 g_error_free (error);
2249 return;
2250 }
2251
2252 /* Ignore error, just log it as warning. In case we send complete message when
2253 * the modem does not expect it, we could get an error that is harmless.
2254 */
2255 if (!qmi_message_uim_refresh_complete_output_get_result (output, &error)) {
2256 g_warning ("refresh complete failed: %s\n", error->message);
2257 g_error_free (error);
2258 } else
2259 g_debug ("Refresh complete OK.");
2260
2261 qmi_message_uim_refresh_complete_output_unref (output);
2262 }
2263
2264 static void
2265 refresh_complete (QmiClientUim *client,
2266 gboolean success)
2267 {
2268 QmiMessageUimRefreshCompleteInput *refresh_complete_input;
2269 GArray *placeholder_aid;
2270
2271 placeholder_aid = g_array_new (FALSE, FALSE, sizeof (guint8));
2272
2273 refresh_complete_input = qmi_message_uim_refresh_complete_input_new ();
2274 qmi_message_uim_refresh_complete_input_set_session (
2275 refresh_complete_input,
2276 QMI_UIM_SESSION_TYPE_CARD_SLOT_1,
2277 placeholder_aid, /* ignored */
2278 NULL);
2279 qmi_message_uim_refresh_complete_input_set_info (
2280 refresh_complete_input,
2281 success,
2282 NULL);
2283
2284 qmi_client_uim_refresh_complete (
2285 ctx->client,
2286 refresh_complete_input,
2287 10,
2288 ctx->cancellable,
2289 (GAsyncReadyCallback) refresh_complete_ready,
2290 NULL);
2291 qmi_message_uim_refresh_complete_input_unref (refresh_complete_input);
2292 g_array_unref (placeholder_aid);
2293 }
2294
2295 static void
2296 refresh_received (QmiClientUim *client,
2297 QmiIndicationUimRefreshOutput *output)
2298 {
2299 QmiUimRefreshStage stage;
2300 QmiUimRefreshMode mode;
2301 GArray *files = NULL;
2302 GError *error = NULL;
2303 guint i, j;
2304
2305 g_print ("[%s] Received refresh indication:\n",
2306 qmi_device_get_path_display (ctx->device));
2307 if (!qmi_indication_uim_refresh_output_get_event (
2308 output, &stage, &mode, NULL, NULL, &files, &error)) {
2309 g_printerr ("error: could not parse refresh ind: %s\n", error->message);
2310 g_error_free (error);
2311 operation_shutdown (FALSE);
2312 return;
2313 }
2314 g_print (" Refresh stage: %s\n",
2315 qmi_uim_refresh_stage_get_string (stage));
2316 g_print (" Refresh mode: %s\n",
2317 qmi_uim_refresh_mode_get_string (mode));
2318 g_print (" Files:\n");
2319 if (files && files->len > 0)
2320 for (i = 0; i < files->len; i++) {
2321 QmiIndicationUimRefreshOutputEventFilesElement *file;
2322 GArray *path;
2323
2324 file = &g_array_index (files, QmiIndicationUimRefreshOutputEventFilesElement, i);
2325 g_print (" 0x%x; path:",
2326 file->file_id);
2327 path = file->path;
2328 if (path && path->len >= 2)
2329 for (j = 0; j < path->len / 2; j++) {
2330 guint16 path_component;
2331 path_component = g_array_index (path, guint8, j * 2) |
2332 ((guint16)g_array_index (path, guint8, j * 2 + 1) << 8);
2333 g_print (" 0x%x", path_component);
2334 }
2335 else
2336 g_print (" <none>");
2337 g_print ("\n");
2338 }
2339 else
2340 g_print (" <none>\n");
2341 /* Send refresh complete message only in start stage and only if the
2342 * mode is something other than reset.
2343 */
2344 if (stage == QMI_UIM_REFRESH_STAGE_START && mode != QMI_UIM_REFRESH_MODE_RESET)
2345 refresh_complete (client, TRUE);
2346 }
2347
2348 static void
2349 refresh_cancelled (GCancellable *cancellable)
2350 {
2351 operation_shutdown (TRUE);
2352 }
2353
2354 #endif /* HAVE_QMI_MESSAGE_UIM_REFRESH_REGISTER
2355 * HAVE_QMI_MESSAGE_UIM_REFRESH_REGISTER_ALL */
2356
2357 #if defined HAVE_QMI_MESSAGE_UIM_REFRESH_REGISTER
2358
2359 static void
2360 register_refresh_events_ready (QmiClientUim *client,
2361 GAsyncResult *res)
2362 {
2363 QmiMessageUimRefreshRegisterOutput *output;
2364 GError *error = NULL;
2365
2366 output = qmi_client_uim_refresh_register_finish (client, res, &error);
2367 if (!output) {
2368 g_printerr ("error: operation failed: %s\n", error->message);
2369 g_error_free (error);
2370 operation_shutdown (FALSE);
2371 return;
2372 }
2373
2374 if (!qmi_message_uim_refresh_register_output_get_result (output, &error)) {
2375 g_printerr ("error: could not register refresh file events: %s\n", error->message);
2376 qmi_message_uim_refresh_register_output_unref (output);
2377 g_error_free (error);
2378 operation_shutdown (FALSE);
2379 return;
2380 }
2381
2382 g_debug ("Registered refresh file events...");
2383 ctx->refresh_indication_id =
2384 g_signal_connect (ctx->client,
2385 "refresh",
2386 G_CALLBACK (refresh_received),
2387 NULL);
2388
2389 /* User can use Ctrl+C to cancel the monitoring at any time */
2390 g_cancellable_connect (ctx->cancellable,
2391 G_CALLBACK (refresh_cancelled),
2392 NULL,
2393 NULL);
2394 qmi_message_uim_refresh_register_output_unref (output);
2395 }
2396
2397 static void
2398 register_refresh_events (gchar **file_path_array)
2399 {
2400 QmiMessageUimRefreshRegisterInput *refresh_input;
2401 GArray *placeholder_aid;
2402 GArray *file_list;
2403 QmiMessageUimRefreshRegisterInputInfoFilesElement file_element;
2404 guint i;
2405
2406 file_list = g_array_new (FALSE, FALSE, sizeof (QmiMessageUimRefreshRegisterInputInfoFilesElement));
2407 while (*file_path_array) {
2408 memset (&file_element, 0, sizeof (file_element));
2409 if (!get_sim_file_id_and_path (*file_path_array, &file_element.file_id, &file_element.path))
2410 goto out;
2411
2412 g_array_append_val (file_list, file_element);
2413 file_path_array++;
2414 }
2415
2416 placeholder_aid = g_array_new (FALSE, FALSE, sizeof (guint8));
2417 refresh_input = qmi_message_uim_refresh_register_input_new ();
2418 qmi_message_uim_refresh_register_input_set_session (
2419 refresh_input,
2420 QMI_UIM_SESSION_TYPE_CARD_SLOT_1,
2421 placeholder_aid, /* ignored */
2422 NULL);
2423 qmi_message_uim_refresh_register_input_set_info (
2424 refresh_input,
2425 TRUE,
2426 FALSE,
2427 file_list,
2428 NULL);
2429
2430 qmi_client_uim_refresh_register (
2431 ctx->client,
2432 refresh_input,
2433 10,
2434 ctx->cancellable,
2435 (GAsyncReadyCallback) register_refresh_events_ready,
2436 NULL);
2437 qmi_message_uim_refresh_register_input_unref (refresh_input);
2438 g_array_unref (placeholder_aid);
2439
2440 out:
2441 for (i = 0; i < file_list->len; i++) {
2442 QmiMessageUimRefreshRegisterInputInfoFilesElement *file;
2443 file = &g_array_index (file_list, QmiMessageUimRefreshRegisterInputInfoFilesElement, i);
2444 g_array_unref (file->path);
2445 }
2446 g_array_unref (file_list);
2447 }
2448
2449 #endif /* HAVE_QMI_MESSAGE_UIM_REFRESH_REGISTER */
2450
2451 #if defined HAVE_QMI_MESSAGE_UIM_REFRESH_REGISTER_ALL
2452
2453 static void
2454 register_refresh_all_events_ready (QmiClientUim *client,
2455 GAsyncResult *res)
2456 {
2457 QmiMessageUimRefreshRegisterAllOutput *output;
2458 GError *error = NULL;
2459
2460 output = qmi_client_uim_refresh_register_all_finish (client, res, &error);
2461 if (!output) {
2462 g_printerr ("error: operation failed: %s\n", error->message);
2463 g_error_free (error);
2464 operation_shutdown (FALSE);
2465 return;
2466 }
2467
2468 if (!qmi_message_uim_refresh_register_all_output_get_result (output, &error)) {
2469 g_printerr ("error: could not register refresh file events: %s\n", error->message);
2470 qmi_message_uim_refresh_register_all_output_unref (output);
2471 g_error_free (error);
2472 operation_shutdown (FALSE);
2473 return;
2474 }
2475
2476 g_debug ("Registered refresh all file events...");
2477 ctx->refresh_indication_id =
2478 g_signal_connect (ctx->client,
2479 "refresh",
2480 G_CALLBACK (refresh_received),
2481 NULL);
2482
2483 /* User can use Ctrl+C to cancel the monitoring at any time */
2484 g_cancellable_connect (ctx->cancellable,
2485 G_CALLBACK (refresh_cancelled),
2486 NULL,
2487 NULL);
2488 qmi_message_uim_refresh_register_all_output_unref (output);
2489 }
2490
2491 static void
2492 register_refresh_all_events (void)
2493 {
2494 QmiMessageUimRefreshRegisterAllInput *refresh_all_input;
2495 GArray *placeholder_aid;
2496
2497 refresh_all_input = qmi_message_uim_refresh_register_all_input_new ();
2498 placeholder_aid = g_array_new (FALSE, FALSE, sizeof (guint8));
2499 qmi_message_uim_refresh_register_all_input_set_session (
2500 refresh_all_input,
2501 QMI_UIM_SESSION_TYPE_CARD_SLOT_1,
2502 placeholder_aid, /* ignored */
2503 NULL);
2504
2505 qmi_message_uim_refresh_register_all_input_set_info (
2506 refresh_all_input,
2507 TRUE,
2508 NULL);
2509 qmi_client_uim_refresh_register_all (
2510 ctx->client,
2511 refresh_all_input,
2512 10,
2513 ctx->cancellable,
2514 (GAsyncReadyCallback) register_refresh_all_events_ready,
2515 NULL);
2516 qmi_message_uim_refresh_register_all_input_unref (refresh_all_input);
2517 g_array_unref (placeholder_aid);
2518 }
2519
2520 #endif /* HAVE_QMI_MESSAGE_UIM_REFRESH_REGISTER_ALL */
2521
2522 #if defined HAVE_QMI_MESSAGE_UIM_GET_CONFIGURATION
2523
2524 static QmiMessageUimGetConfigurationInput *
2525 get_configuration_input_create (void)
2526 {
2527 QmiMessageUimGetConfigurationInput *input;
2528
2529 input = qmi_message_uim_get_configuration_input_new ();
2530
2531 qmi_message_uim_get_configuration_input_set_configuration_mask (
2532 input,
2533 QMI_UIM_CONFIGURATION_PERSONALIZATION_STATUS,
2534 NULL);
2535
2536 return input;
2537 }
2538
2539 static void
2540 get_configuration_ready (QmiClientUim *client,
2541 GAsyncResult *res)
2542 {
2543 g_autoptr(QmiMessageUimGetConfigurationOutput) output = NULL;
2544 g_autoptr(GError) error = NULL;
2545 GArray *elements = NULL;
2546 GArray *other_slots = NULL;
2547
2548 output = qmi_client_uim_get_configuration_finish (client, res, &error);
2549 if (!output) {
2550 g_printerr ("error: operation failed: %s\n", error->message);
2551 operation_shutdown (FALSE);
2552 return;
2553 }
2554
2555 if (!qmi_message_uim_get_configuration_output_get_result (output, &error)) {
2556 g_printerr ("error: get configuration failed: %s\n", error->message);
2557 operation_shutdown (FALSE);
2558 return;
2559 }
2560
2561 g_print ("Configuration successfully retrieved\n");
2562
2563 /* Other slots TLV contains info for slots > 1 */
2564 qmi_message_uim_get_configuration_output_get_personalization_status_other (output, &other_slots, NULL);
2565
2566 if (qmi_message_uim_get_configuration_output_get_personalization_status (output, &elements, NULL)) {
2567 if (elements->len == 0)
2568 g_print ("Personalization features%s: all disabled\n",
2569 other_slots ? " in slot 1" : "");
2570 else {
2571 QmiMessageUimGetConfigurationOutputPersonalizationStatusElement *element;
2572 guint i;
2573
2574 g_print ("Personalization features%s:\n",
2575 other_slots ? " in slot 1" : "");
2576 for (i = 0; i < elements->len; i++) {
2577 element = &g_array_index (elements,
2578 QmiMessageUimGetConfigurationOutputPersonalizationStatusElement,
2579 i);
2580 g_print ("\tPersonalization: %s\n"
2581 "\t\tVerify left: %u\n"
2582 "\t\tUnblock left: %u\n",
2583 qmi_uim_card_application_personalization_feature_get_string (element->feature),
2584 element->verify_left,
2585 element->unblock_left);
2586 }
2587 }
2588 }
2589
2590 if (other_slots) {
2591 if (other_slots->len == 0)
2592 g_print ("Personalization features in other slots: all disabled\n");
2593 else {
2594 guint i_slot;
2595
2596 for (i_slot = 0; i_slot < other_slots->len; i_slot++) {
2597 QmiMessageUimGetConfigurationOutputPersonalizationStatusOtherElement *slot_element;
2598 guint i;
2599
2600 slot_element = &g_array_index (other_slots, QmiMessageUimGetConfigurationOutputPersonalizationStatusOtherElement, i_slot);
2601 if (!slot_element->slot)
2602 continue;
2603
2604 g_print ("Personalization features in slot %u:\n", i_slot + 2);
2605 for (i = 0; i < slot_element->slot->len; i++) {
2606 QmiMessageUimGetConfigurationOutputPersonalizationStatusOtherElementSlotElement *element;
2607
2608 element = &g_array_index (slot_element->slot,
2609 QmiMessageUimGetConfigurationOutputPersonalizationStatusOtherElementSlotElement,
2610 i);
2611 g_print ("\tPersonalization: %s\n"
2612 "\t\tVerify left: %u\n"
2613 "\t\tUnblock left: %u\n",
2614 qmi_uim_card_application_personalization_feature_get_string (element->feature),
2615 element->verify_left,
2616 element->unblock_left);
2617 }
2618 }
2619 }
2620 }
2621
2622 operation_shutdown (TRUE);
2623 }
2624
2625 #endif /* HAVE_QMI_MESSAGE_UIM_GET_CONFIGURATION */
2626
2627 #if defined HAVE_QMI_MESSAGE_UIM_DEPERSONALIZATION
2628
2629 static QmiMessageUimDepersonalizationInput *
2630 depersonalization_input_create (const gchar *str)
2631 {
2632 g_auto(GStrv) split = NULL;
2633 QmiMessageUimDepersonalizationInput *input = NULL;
2634 QmiUimCardApplicationPersonalizationFeature feature;
2635 QmiUimDepersonalizationOperation operation;
2636 const gchar *control_key;
2637 guint slot = 0;
2638
2639 /* Prepare inputs.
2640 * Format of the string is:
2641 * "[(feature),(operation),(control key)[,(slot number)]]"
2642 */
2643 split = g_strsplit (str, ",", -1);
2644
2645 if (!split[0] || !qmicli_read_uim_card_application_personalization_feature_from_string (split[0], &feature)) {
2646 g_printerr ("error: invalid personalization feature\n");
2647 return NULL;
2648 }
2649
2650 if (!split[1] || !qmicli_read_uim_depersonalization_operation_from_string (split[1], &operation)) {
2651 g_printerr ("error: invalid depersonalization operation\n");
2652 return NULL;
2653 }
2654
2655 if (!split[2]) {
2656 g_printerr ("error: missing control key\n");
2657 return NULL;
2658 }
2659 control_key = split[2];
2660
2661 if (g_strv_length (split) > 3) {
2662 if (!qmicli_read_uint_from_string (split[3], &slot) || (slot < 1) || (slot > 5)) {
2663 g_printerr ("error: invalid slot number\n");
2664 return NULL;
2665 }
2666 }
2667
2668 input = qmi_message_uim_depersonalization_input_new ();
2669 qmi_message_uim_depersonalization_input_set_info (input, feature, operation, control_key, NULL);
2670
2671 /* skip setting slot if not given by the user */
2672 if (slot > 0)
2673 qmi_message_uim_depersonalization_input_set_slot (input, slot, NULL);
2674
2675 return input;
2676 }
2677
2678 static void
2679 depersonalization_ready (QmiClientUim *client,
2680 GAsyncResult *res)
2681 {
2682 g_autoptr(QmiMessageUimDepersonalizationOutput) output = NULL;
2683 g_autoptr(GError) error = NULL;
2684 guint8 unblock_left;
2685 guint8 verify_left;
2686
2687 output = qmi_client_uim_depersonalization_finish (client, res, &error);
2688 if (!output) {
2689 g_printerr ("error: operation failed: %s\n", error->message);
2690 operation_shutdown (FALSE);
2691 return;
2692 }
2693
2694 if (qmi_message_uim_depersonalization_output_get_result (output, &error)) {
2695 g_print ("Modem was unlocked successfully\n");
2696 operation_shutdown (TRUE);
2697 return;
2698 }
2699
2700 g_printerr ("error: depersonalization failed: %s\n", error->message);
2701 if (qmi_message_uim_depersonalization_output_get_retries_remaining (
2702 output,
2703 &verify_left,
2704 &unblock_left,
2705 NULL)) {
2706 g_printerr ("Retries left:\n"
2707 "\tVerify: %u\n"
2708 "\tUnblock: %u\n",
2709 verify_left,
2710 unblock_left);
2711 }
2712
2713 operation_shutdown (FALSE);
2714 }
2715
2716 #endif /* HAVE_QMI_MESSAGE_UIM_DEPERSONALIZATION */
2717
2718 #if defined HAVE_QMI_MESSAGE_UIM_REMOTE_UNLOCK
2719
2720 static QmiMessageUimRemoteUnlockInput *
2721 remote_unlock_input_create (const gchar *simlock_data_str)
2722 {
2723 QmiMessageUimRemoteUnlockInput *input;
2724 GArray *simlock_data = NULL;
2725
2726 if (!qmicli_read_raw_data_from_string (simlock_data_str, &simlock_data))
2727 return NULL;
2728
2729 input = qmi_message_uim_remote_unlock_input_new ();
2730
2731 if (simlock_data->len <= 1024) {
2732 qmi_message_uim_remote_unlock_input_set_simlock_data (input,
2733 simlock_data,
2734 NULL);
2735 } else {
2736 qmi_message_uim_remote_unlock_input_set_simlock_extended_data (input,
2737 simlock_data,
2738 NULL);
2739 }
2740
2741 g_array_unref (simlock_data);
2742 return input;
2743 }
2744
2745 static void
2746 remote_unlock_ready (QmiClientUim *client,
2747 GAsyncResult *res)
2748 {
2749 g_autoptr(QmiMessageUimRemoteUnlockOutput) output = NULL;
2750 g_autoptr(GError) error = NULL;
2751
2752 output = qmi_client_uim_remote_unlock_finish (client, res, &error);
2753 if (!output) {
2754 g_printerr ("error: operation failed: %s\n", error->message);
2755 operation_shutdown (FALSE);
2756 return;
2757 }
2758
2759 if (!qmi_message_uim_remote_unlock_output_get_result (output, &error)) {
2760 g_printerr ("error: remote unlock operation failed: %s\n", error->message);
2761 operation_shutdown (FALSE);
2762 return;
2763 }
2764
2765 g_print ("Remote unlock operation successfully completed\n");
2766 operation_shutdown (TRUE);
2767 }
2768
2769 #endif /* HAVE_QMI_MESSAGE_UIM_REMOTE_UNLOCK */
2770
2771 #if defined HAVE_QMI_MESSAGE_UIM_OPEN_LOGICAL_CHANNEL
2772
2773 static QmiMessageUimOpenLogicalChannelInput *
2774 open_logical_channel_input_create (const gchar *str)
2775 {
2776 QmiMessageUimOpenLogicalChannelInput *input;
2777 g_auto(GStrv) split = NULL;
2778 guint slot;
2779 g_autoptr(GArray) aid_data = NULL;
2780
2781 /* Prepare inputs.
2782 * Format of the string is:
2783 * "[(slot number),(aid)]"
2784 */
2785 split = g_strsplit (str, ",", -1);
2786
2787 if (!split[0] || !qmicli_read_uint_from_string (split[0], &slot) || (slot > G_MAXUINT8)) {
2788 g_printerr ("error: invalid slot number\n");
2789 return NULL;
2790 }
2791
2792 /* AID is optional */
2793 if (split[1]) {
2794 if (!qmicli_read_raw_data_from_string (split[1], &aid_data)) {
2795 g_printerr ("error: invalid AID data\n");
2796 return NULL;
2797 }
2798 }
2799
2800 input = qmi_message_uim_open_logical_channel_input_new ();
2801 qmi_message_uim_open_logical_channel_input_set_slot (input, slot, NULL);
2802 if (aid_data)
2803 qmi_message_uim_open_logical_channel_input_set_aid (input, aid_data, NULL);
2804
2805 return input;
2806 }
2807
2808 static void
2809 open_logical_channel_ready (QmiClientUim *client,
2810 GAsyncResult *res)
2811 {
2812 g_autoptr(QmiMessageUimOpenLogicalChannelOutput) output = NULL;
2813 g_autoptr(GError) error = NULL;
2814 guint8 channel_id;
2815
2816 output = qmi_client_uim_open_logical_channel_finish (client, res, &error);
2817 if (!output) {
2818 g_printerr ("error: operation failed: %s\n", error->message);
2819 operation_shutdown (FALSE);
2820 return;
2821 }
2822
2823 if (!qmi_message_uim_open_logical_channel_output_get_result (output, &error)) {
2824 g_printerr ("error: open logical channel operation failed: %s\n", error->message);
2825 operation_shutdown (FALSE);
2826 return;
2827 }
2828
2829 if (!qmi_message_uim_open_logical_channel_output_get_channel_id (output, &channel_id, &error)) {
2830 g_printerr ("error: get channel id operation failed: %s\n", error->message);
2831 operation_shutdown (FALSE);
2832 return;
2833 }
2834
2835 g_print ("Open Logical Channel operation successfully completed: %d\n", channel_id);
2836 operation_shutdown (TRUE);
2837 }
2838
2839 #endif /* HAVE_QMI_MESSAGE_UIM_OPEN_LOGICAL_CHANNEL */
2840
2841 #if defined HAVE_QMI_MESSAGE_UIM_LOGICAL_CHANNEL
2842
2843 static QmiMessageUimLogicalChannelInput *
2844 close_logical_channel_input_create (const gchar *str)
2845 {
2846 QmiMessageUimLogicalChannelInput *input;
2847 g_auto(GStrv) split = NULL;
2848 guint slot;
2849 guint channel_id;
2850
2851 /* Prepare inputs.
2852 * Format of the string is:
2853 * "[(slot number),(channel ID)]"
2854 */
2855 split = g_strsplit (str, ",", -1);
2856
2857 if (!split[0] || !qmicli_read_uint_from_string (split[0], &slot) || (slot > G_MAXUINT8)) {
2858 g_printerr ("error: invalid slot number\n");
2859 return NULL;
2860 }
2861
2862 if (!split[1] || !qmicli_read_uint_from_string (split[1], &channel_id) || (channel_id > G_MAXUINT8)) {
2863 g_printerr ("error: invalid channel ID\n");
2864 return NULL;
2865 }
2866
2867 input = qmi_message_uim_logical_channel_input_new ();
2868 qmi_message_uim_logical_channel_input_set_slot (input, slot, NULL);
2869 qmi_message_uim_logical_channel_input_set_channel_id (input, channel_id, NULL);
2870
2871 return input;
2872 }
2873
2874 static void
2875 close_logical_channel_ready (QmiClientUim *client,
2876 GAsyncResult *res)
2877 {
2878 g_autoptr(QmiMessageUimLogicalChannelOutput) output = NULL;
2879 g_autoptr(GError) error = NULL;
2880
2881 output = qmi_client_uim_logical_channel_finish (client, res, &error);
2882 if (!output) {
2883 g_printerr ("error: operation failed: %s\n", error->message);
2884 operation_shutdown (FALSE);
2885 return;
2886 }
2887
2888 if (!qmi_message_uim_logical_channel_output_get_result (output, &error)) {
2889 g_printerr ("error: close logical channel operation failed: %s\n", error->message);
2890 operation_shutdown (FALSE);
2891 return;
2892 }
2893
2894 g_print ("Close Logical Channel operation successfully completed\n");
2895 operation_shutdown (TRUE);
2896 }
2897
2898 #endif /* HAVE_QMI_MESSAGE_UIM_LOGICAL_CHANNEL */
2899
2900 #if defined HAVE_QMI_MESSAGE_UIM_SEND_APDU
2901
2902 static QmiMessageUimSendApduInput *
2903 send_apdu_input_create (const gchar *str)
2904 {
2905 QmiMessageUimSendApduInput *input;
2906 g_auto(GStrv) split = NULL;
2907 guint slot;
2908 guint channel_id;
2909 g_autoptr(GArray) apdu_data = NULL;
2910
2911 /* Prepare inputs.
2912 * Format of the string is:
2913 * "[(slot number),(channel ID),(apdu)]"
2914 */
2915 split = g_strsplit (str, ",", -1);
2916
2917 if (!split[0] || !qmicli_read_uint_from_string (split[0], &slot) || (slot > G_MAXUINT8)) {
2918 g_printerr ("error: invalid slot number\n");
2919 return NULL;
2920 }
2921
2922 if (!split[1] || !qmicli_read_uint_from_string (split[1], &channel_id) || (channel_id > G_MAXUINT8)) {
2923 g_printerr ("error: invalid channel ID\n");
2924 return NULL;
2925 }
2926
2927 if (!split[2] || !qmicli_read_raw_data_from_string (split[2], &apdu_data)) {
2928 g_printerr ("error: invalid APDU data\n");
2929 return NULL;
2930 }
2931
2932 input = qmi_message_uim_send_apdu_input_new ();
2933 qmi_message_uim_send_apdu_input_set_slot (input, slot, NULL);
2934 qmi_message_uim_send_apdu_input_set_channel_id (input, channel_id, NULL);
2935 qmi_message_uim_send_apdu_input_set_apdu (input, apdu_data, NULL);
2936
2937 return input;
2938 }
2939
2940 static void
2941 send_apdu_ready (QmiClientUim *client,
2942 GAsyncResult *res)
2943 {
2944 g_autoptr(QmiMessageUimSendApduOutput) output = NULL;
2945 g_autoptr(GError) error = NULL;
2946 GArray *apdu_res = NULL;
2947 gchar *apdu_res_hex;
2948
2949 output = qmi_client_uim_send_apdu_finish (client, res, &error);
2950 if (!output) {
2951 g_printerr ("error: operation failed: %s\n", error->message);
2952 operation_shutdown (FALSE);
2953 return;
2954 }
2955
2956 if (!qmi_message_uim_send_apdu_output_get_result (output, &error)) {
2957 g_printerr ("error: send apdu operation failed: %s\n", error->message);
2958 operation_shutdown (FALSE);
2959 return;
2960 }
2961
2962 if (!qmi_message_uim_send_apdu_output_get_apdu_response (output, &apdu_res, &error)) {
2963 g_printerr ("error: get apdu response operation failed: %s\n", error->message);
2964 operation_shutdown (FALSE);
2965 return;
2966 }
2967
2968 g_print ("Send APDU operation successfully completed:");
2969 apdu_res_hex = qmi_common_str_hex (apdu_res->data, apdu_res->len, ':');
2970 g_print (" %s\n", apdu_res_hex);
2971 g_free (apdu_res_hex);
2972
2973 operation_shutdown (TRUE);
2974 }
2975
2976 #endif /* HAVE_QMI_MESSAGE_UIM_SEND_APDU */
2977
2978 void
2979 qmicli_uim_run (QmiDevice *device,
2980 QmiClientUim *client,
2981 GCancellable *cancellable)
2982 {
2983 /* Initialize context */
2984 ctx = g_slice_new0 (Context);
2985 ctx->device = g_object_ref (device);
2986 ctx->client = g_object_ref (client);
2987 ctx->cancellable = g_object_ref (cancellable);
2988
2989 #if defined HAVE_QMI_MESSAGE_UIM_SET_PIN_PROTECTION
2990 if (set_pin_protection_str) {
2991 QmiMessageUimSetPinProtectionInput *input;
2992
2993 g_debug ("Asynchronously setting PIN protection...");
2994 input = set_pin_protection_input_create (set_pin_protection_str);
2995 if (!input) {
2996 operation_shutdown (FALSE);
2997 return;
2998 }
2999 qmi_client_uim_set_pin_protection (ctx->client,
3000 input,
3001 10,
3002 ctx->cancellable,
3003 (GAsyncReadyCallback)set_pin_protection_ready,
3004 NULL);
3005 qmi_message_uim_set_pin_protection_input_unref (input);
3006 return;
3007 }
3008 #endif
3009
3010 #if defined HAVE_QMI_MESSAGE_UIM_VERIFY_PIN
3011 if (verify_pin_str) {
3012 QmiMessageUimVerifyPinInput *input;
3013
3014 g_debug ("Asynchronously verifying PIN...");
3015 input = verify_pin_input_create (verify_pin_str);
3016 if (!input) {
3017 operation_shutdown (FALSE);
3018 return;
3019 }
3020 qmi_client_uim_verify_pin (ctx->client,
3021 input,
3022 10,
3023 ctx->cancellable,
3024 (GAsyncReadyCallback)verify_pin_ready,
3025 NULL);
3026 qmi_message_uim_verify_pin_input_unref (input);
3027 return;
3028 }
3029 #endif
3030
3031 #if defined HAVE_QMI_MESSAGE_UIM_UNBLOCK_PIN
3032 if (unblock_pin_str) {
3033 QmiMessageUimUnblockPinInput *input;
3034
3035 g_debug ("Asynchronously unblocking PIN...");
3036 input = unblock_pin_input_create (unblock_pin_str);
3037 if (!input) {
3038 operation_shutdown (FALSE);
3039 return;
3040 }
3041 qmi_client_uim_unblock_pin (ctx->client,
3042 input,
3043 10,
3044 ctx->cancellable,
3045 (GAsyncReadyCallback)unblock_pin_ready,
3046 NULL);
3047 qmi_message_uim_unblock_pin_input_unref (input);
3048 return;
3049 }
3050 #endif
3051
3052 #if defined HAVE_QMI_MESSAGE_UIM_CHANGE_PIN
3053 if (change_pin_str) {
3054 QmiMessageUimChangePinInput *input;
3055
3056 g_debug ("Asynchronously changing PIN...");
3057 input = change_pin_input_create (change_pin_str);
3058 if (!input) {
3059 operation_shutdown (FALSE);
3060 return;
3061 }
3062 qmi_client_uim_change_pin (ctx->client,
3063 input,
3064 10,
3065 ctx->cancellable,
3066 (GAsyncReadyCallback)change_pin_ready,
3067 NULL);
3068 qmi_message_uim_change_pin_input_unref (input);
3069 return;
3070 }
3071 #endif
3072
3073 #if defined HAVE_QMI_MESSAGE_UIM_READ_TRANSPARENT
3074 if (read_transparent_str) {
3075 QmiMessageUimReadTransparentInput *input;
3076
3077 input = read_transparent_build_input (read_transparent_str);
3078 if (!input) {
3079 operation_shutdown (FALSE);
3080 return;
3081 }
3082
3083 g_debug ("Asynchronously reading transparent file at '%s'...",
3084 read_transparent_str);
3085 qmi_client_uim_read_transparent (ctx->client,
3086 input,
3087 10,
3088 ctx->cancellable,
3089 (GAsyncReadyCallback)read_transparent_ready,
3090 NULL);
3091 qmi_message_uim_read_transparent_input_unref (input);
3092 return;
3093 }
3094 #endif
3095
3096 #if defined HAVE_QMI_MESSAGE_UIM_READ_RECORD
3097 if (read_record_str) {
3098 QmiMessageUimReadRecordInput *input;
3099
3100 input = read_record_input_create (read_record_str);
3101 if (!input) {
3102 operation_shutdown (FALSE);
3103 return;
3104 }
3105
3106 g_debug ("Asynchronously reading record file at '%s'...",
3107 read_record_str);
3108 qmi_client_uim_read_record (ctx->client,
3109 input,
3110 10,
3111 ctx->cancellable,
3112 (GAsyncReadyCallback)read_record_ready,
3113 NULL);
3114 qmi_message_uim_read_record_input_unref (input);
3115 return;
3116 }
3117 #endif
3118
3119 #if defined HAVE_QMI_MESSAGE_UIM_GET_FILE_ATTRIBUTES
3120 if (get_file_attributes_str) {
3121 QmiMessageUimGetFileAttributesInput *input;
3122
3123 input = get_file_attributes_build_input (get_file_attributes_str);
3124 if (!input) {
3125 operation_shutdown (FALSE);
3126 return;
3127 }
3128
3129 g_debug ("Asynchronously reading attributes of file '%s'...",
3130 get_file_attributes_str);
3131 qmi_client_uim_get_file_attributes (ctx->client,
3132 input,
3133 10,
3134 ctx->cancellable,
3135 (GAsyncReadyCallback)get_file_attributes_ready,
3136 NULL);
3137 qmi_message_uim_get_file_attributes_input_unref (input);
3138 return;
3139 }
3140 #endif
3141
3142 #if defined HAVE_QMI_MESSAGE_UIM_GET_CARD_STATUS
3143 if (get_card_status_flag) {
3144 g_debug ("Asynchronously getting card status...");
3145 qmi_client_uim_get_card_status (ctx->client,
3146 NULL,
3147 10,
3148 ctx->cancellable,
3149 (GAsyncReadyCallback)get_card_status_ready,
3150 NULL);
3151 return;
3152 }
3153 #endif
3154
3155 #if defined HAVE_QMI_MESSAGE_UIM_GET_SUPPORTED_MESSAGES
3156 if (get_supported_messages_flag) {
3157 g_debug ("Asynchronously getting supported UIM messages...");
3158 qmi_client_uim_get_supported_messages (ctx->client,
3159 NULL,
3160 10,
3161 ctx->cancellable,
3162 (GAsyncReadyCallback)get_supported_messages_ready,
3163 NULL);
3164 return;
3165 }
3166 #endif
3167
3168 #if defined HAVE_QMI_MESSAGE_UIM_POWER_ON_SIM
3169 if (sim_power_on_str) {
3170 QmiMessageUimPowerOnSimInput *input;
3171
3172 g_debug ("Asynchronously power on SIM card");
3173 input = power_on_sim_input_create (sim_power_on_str);
3174 if (!input) {
3175 operation_shutdown (FALSE);
3176 return;
3177 }
3178
3179 qmi_client_uim_power_on_sim (ctx->client,
3180 input,
3181 10,
3182 ctx->cancellable,
3183 (GAsyncReadyCallback)power_on_sim_ready,
3184 NULL);
3185 qmi_message_uim_power_on_sim_input_unref (input);
3186 return;
3187 }
3188 #endif
3189
3190 #if defined HAVE_QMI_MESSAGE_UIM_POWER_OFF_SIM
3191 if (sim_power_off_str) {
3192 QmiMessageUimPowerOffSimInput *input;
3193
3194 g_debug ("Asynchronously power off SIM card");
3195 input = power_off_sim_input_create (sim_power_off_str);
3196 if (!input) {
3197 operation_shutdown (FALSE);
3198 return;
3199 }
3200
3201 qmi_client_uim_power_off_sim (ctx->client,
3202 input,
3203 10,
3204 ctx->cancellable,
3205 (GAsyncReadyCallback)power_off_sim_ready,
3206 NULL);
3207 qmi_message_uim_power_off_sim_input_unref (input);
3208 return;
3209 }
3210 #endif
3211
3212 #if defined HAVE_QMI_MESSAGE_UIM_CHANGE_PROVISIONING_SESSION
3213 if (change_provisioning_session_str) {
3214 QmiMessageUimChangeProvisioningSessionInput *input;
3215
3216 g_debug ("Asynchronously changing provisioning session");
3217 input = change_provisioning_session_input_create (change_provisioning_session_str);
3218 if (!input) {
3219 operation_shutdown (FALSE);
3220 return;
3221 }
3222
3223 qmi_client_uim_change_provisioning_session (ctx->client,
3224 input,
3225 10,
3226 ctx->cancellable,
3227 (GAsyncReadyCallback)change_provisioning_session_ready,
3228 NULL);
3229 qmi_message_uim_change_provisioning_session_input_unref (input);
3230 return;
3231 }
3232 #endif
3233
3234 #if defined HAVE_QMI_MESSAGE_UIM_GET_SLOT_STATUS
3235 /* Request to get slot status? */
3236 if (get_slot_status_flag) {
3237 g_debug ("Asynchronously getting slot status...");
3238 qmi_client_uim_get_slot_status (ctx->client,
3239 NULL,
3240 10,
3241 ctx->cancellable,
3242 (GAsyncReadyCallback)get_slot_status_ready,
3243 NULL);
3244 return;
3245 }
3246 #endif
3247
3248 #if defined HAVE_QMI_MESSAGE_UIM_SWITCH_SLOT && defined HAVE_QMI_MESSAGE_UIM_GET_SLOT_STATUS
3249 /* Request to change active slot? */
3250 if (switch_slot_str) {
3251 guint physical_slot;
3252
3253 if (!qmicli_read_uint_from_string (switch_slot_str, &physical_slot) ||
3254 (physical_slot < 1) || (physical_slot > G_MAXUINT8)) {
3255 g_printerr ("error: invalid slot number\n");
3256 return;
3257 }
3258
3259 g_debug ("Asynchronously switching active slot");
3260
3261 qmi_client_uim_get_slot_status (ctx->client,
3262 NULL,
3263 10,
3264 ctx->cancellable,
3265 (GAsyncReadyCallback)get_active_logical_slot_ready,
3266 GUINT_TO_POINTER (physical_slot));
3267 return;
3268 }
3269 #endif
3270
3271 #if defined HAVE_QMI_INDICATION_UIM_SLOT_STATUS
3272 /* Watch for slot status changes? */
3273 if (monitor_slot_status_flag) {
3274 g_debug ("Listening for slot status changes...");
3275 register_physical_slot_status_events ();
3276 return;
3277 }
3278 #endif
3279
3280 #if defined HAVE_QMI_MESSAGE_UIM_REFRESH_REGISTER
3281 if (monitor_refresh_file_array) {
3282 g_debug ("Listening for refresh events...");
3283 register_refresh_events (monitor_refresh_file_array);
3284 return;
3285 }
3286 #endif
3287
3288 #if defined HAVE_QMI_MESSAGE_UIM_REFRESH_REGISTER_ALL
3289 if (monitor_refresh_all_flag) {
3290 g_debug ("Listening for all refresh events...");
3291 register_refresh_all_events ();
3292 return;
3293 }
3294 #endif
3295
3296 #if defined HAVE_QMI_MESSAGE_UIM_RESET
3297 /* Request to reset UIM service? */
3298 if (reset_flag) {
3299 g_debug ("Asynchronously resetting UIM service...");
3300 qmi_client_uim_reset (ctx->client,
3301 NULL,
3302 10,
3303 ctx->cancellable,
3304 (GAsyncReadyCallback)reset_ready,
3305 NULL);
3306 return;
3307 }
3308 #endif
3309
3310 #if defined HAVE_QMI_MESSAGE_UIM_GET_CONFIGURATION
3311 /* Request to get personalization status? */
3312 if (get_configuration_flag) {
3313 g_autoptr(QmiMessageUimGetConfigurationInput) input = NULL;
3314
3315 g_debug ("Asynchronously getting UIM configuration...");
3316 input = get_configuration_input_create ();
3317 if (!input) {
3318 operation_shutdown (FALSE);
3319 return;
3320 }
3321
3322 qmi_client_uim_get_configuration (ctx->client,
3323 input,
3324 10,
3325 ctx->cancellable,
3326 (GAsyncReadyCallback)get_configuration_ready,
3327 NULL);
3328 return;
3329 }
3330 #endif
3331
3332 #if defined HAVE_QMI_MESSAGE_UIM_DEPERSONALIZATION
3333 /* Request to depersonalize the modem? */
3334 if (depersonalization_str) {
3335 g_autoptr(QmiMessageUimDepersonalizationInput) input = NULL;
3336
3337 g_debug ("Asynchronously removing personalization...");
3338 input = depersonalization_input_create (depersonalization_str);
3339 if (!input) {
3340 operation_shutdown (FALSE);
3341 return;
3342 }
3343
3344 qmi_client_uim_depersonalization (ctx->client,
3345 input,
3346 10,
3347 ctx->cancellable,
3348 (GAsyncReadyCallback)depersonalization_ready,
3349 NULL);
3350 return;
3351 }
3352 #endif
3353
3354 #if defined HAVE_QMI_MESSAGE_UIM_REMOTE_UNLOCK
3355 /* Request to perform remote unlock operations? */
3356 if (remote_unlock_str) {
3357 g_autoptr(QmiMessageUimRemoteUnlockInput) input = NULL;
3358
3359 g_debug ("Asynchronously updating SimLock data...");
3360 input = remote_unlock_input_create (remote_unlock_str);
3361 if (!input) {
3362 g_printerr ("error: couldn't parse the input string as a bytearray\n");
3363 operation_shutdown (FALSE);
3364 return;
3365 }
3366
3367 qmi_client_uim_remote_unlock (ctx->client,
3368 input,
3369 10,
3370 ctx->cancellable,
3371 (GAsyncReadyCallback)remote_unlock_ready,
3372 NULL);
3373 return;
3374 }
3375 #endif
3376
3377 #if defined HAVE_QMI_MESSAGE_UIM_OPEN_LOGICAL_CHANNEL
3378 /* Request to open logical channel? */
3379 if (open_logical_channel_str) {
3380 g_autoptr(QmiMessageUimOpenLogicalChannelInput) input = NULL;
3381
3382 g_debug ("Asynchronously opening logical channel...");
3383 input = open_logical_channel_input_create (open_logical_channel_str);
3384 if (!input) {
3385 operation_shutdown (FALSE);
3386 return;
3387 }
3388
3389 qmi_client_uim_open_logical_channel (ctx->client,
3390 input,
3391 10,
3392 ctx->cancellable,
3393 (GAsyncReadyCallback)open_logical_channel_ready,
3394 NULL);
3395 return;
3396 }
3397 #endif
3398
3399 #if defined HAVE_QMI_MESSAGE_UIM_LOGICAL_CHANNEL
3400 /* Request to close logical channel? */
3401 if (close_logical_channel_str) {
3402 g_autoptr(QmiMessageUimLogicalChannelInput) input = NULL;
3403
3404 g_debug ("Asynchronously closing logical channel...");
3405 input = close_logical_channel_input_create (close_logical_channel_str);
3406 if (!input) {
3407 operation_shutdown (FALSE);
3408 return;
3409 }
3410
3411 qmi_client_uim_logical_channel (ctx->client,
3412 input,
3413 10,
3414 ctx->cancellable,
3415 (GAsyncReadyCallback)close_logical_channel_ready,
3416 NULL);
3417 return;
3418 }
3419 #endif
3420
3421 #if defined HAVE_QMI_MESSAGE_UIM_SEND_APDU
3422 /* Request to send APDU? */
3423 if (send_apdu_str) {
3424 g_autoptr(QmiMessageUimSendApduInput) input = NULL;
3425
3426 g_debug ("Asynchronously sending APDU...");
3427 input = send_apdu_input_create (send_apdu_str);
3428 if (!input) {
3429 operation_shutdown (FALSE);
3430 return;
3431 }
3432
3433 qmi_client_uim_send_apdu (ctx->client,
3434 input,
3435 10,
3436 ctx->cancellable,
3437 (GAsyncReadyCallback)send_apdu_ready,
3438 NULL);
3439 return;
3440 }
3441 #endif
3442
3443 /* Just client allocate/release? */
3444 if (noop_flag) {
3445 g_idle_add (noop_cb, NULL);
3446 return;
3447 }
3448
3449 g_warn_if_reached ();
3450 }
3451
3452 #endif /* HAVE_QMI_SERVICE_UIM */