Debian Codegraph @

src/qmicli/qmicli-pdc.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) 2013-2022 Aleksander Morgado <[email protected]>
19 */
20
21 #include "config.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <locale.h>
26 #include <string.h>
27
28 #include <glib.h>
29 #include <glib/gprintf.h>
30 #include <gio/gio.h>
31
32 #include <libqmi-glib.h>
33
34 #include "qmicli.h"
35 #include "qmicli-helpers.h"
36
37 #if defined HAVE_QMI_SERVICE_PDC
38
39 #define LIST_CONFIGS_TIMEOUT_SECS 2
40 #define LOAD_CONFIG_CHUNK_SIZE 0x400
41
42 /* Info about config */
43 typedef struct {
44 GArray *id;
45 QmiPdcConfigurationType config_type;
46 guint32 token;
47 guint32 version;
48 gchar *description;
49 guint32 total_size;
50 } ConfigInfo;
51
52 /* Info about loading config */
53 typedef struct {
54 GMappedFile *mapped_file;
55 GArray *checksum;
56 gsize offset;
57 } LoadConfigFileData;
58
59 /* Context */
60 typedef struct {
61 QmiDevice *device;
62 QmiClientPdc *client;
63 GCancellable *cancellable;
64 gboolean skip_cid_release;
65
66 /* local data */
67 guint timeout_id;
68 GArray *config_list;
69 guint configs_loaded;
70 GArray *active_config_id;
71 GArray *pending_config_id;
72 gboolean ids_loaded;
73 guint list_configs_indication_id;
74 guint get_selected_config_indication_id;
75
76 LoadConfigFileData *load_config_file_data;
77 guint load_config_indication_id;
78 guint get_config_info_indication_id;
79
80 guint set_selected_config_indication_id;
81 guint activate_config_indication_id;
82 guint device_removed_indication_id;
83
84 guint deactivate_config_indication_id;
85
86 guint refresh_indication_id;
87
88 guint token;
89 } Context;
90 static Context *ctx;
91
92 /* Options */
93 static gchar *list_configs_str;
94 static gchar *delete_config_str;
95 static gchar *activate_config_str;
96 static gchar *deactivate_config_str;
97 static gchar *load_config_str;
98 static gboolean monitor_refresh_flag;
99 static gboolean noop_flag;
100
101 #if defined HAVE_QMI_MESSAGE_PDC_LIST_CONFIGS && \
102 defined HAVE_QMI_INDICATION_PDC_LIST_CONFIGS && \
103 defined HAVE_QMI_MESSAGE_PDC_GET_SELECTED_CONFIG && \
104 defined HAVE_QMI_INDICATION_PDC_GET_SELECTED_CONFIG && \
105 defined HAVE_QMI_MESSAGE_PDC_GET_CONFIG_INFO && \
106 defined HAVE_QMI_INDICATION_PDC_GET_CONFIG_INFO
107 # define HAVE_QMI_ACTION_PDC_LIST_CONFIGS
108 #endif
109
110 #if defined HAVE_QMI_MESSAGE_PDC_ACTIVATE_CONFIG && \
111 defined HAVE_QMI_INDICATION_PDC_ACTIVATE_CONFIG && \
112 defined HAVE_QMI_MESSAGE_PDC_SET_SELECTED_CONFIG && \
113 defined HAVE_QMI_INDICATION_PDC_SET_SELECTED_CONFIG
114 # define HAVE_QMI_ACTION_PDC_ACTIVATE_CONFIG
115 #endif
116
117 #if defined HAVE_QMI_MESSAGE_PDC_DEACTIVATE_CONFIG && \
118 defined HAVE_QMI_INDICATION_PDC_DEACTIVATE_CONFIG
119 # define HAVE_QMI_ACTION_PDC_DEACTIVATE_CONFIG
120 #endif
121
122 #if defined HAVE_QMI_MESSAGE_PDC_LOAD_CONFIG && \
123 defined HAVE_QMI_INDICATION_PDC_LOAD_CONFIG
124 # define HAVE_QMI_ACTION_PDC_LOAD_CONFIG
125 #endif
126
127 #if defined HAVE_QMI_INDICATION_PDC_REFRESH && \
128 defined HAVE_QMI_MESSAGE_PDC_REGISTER
129 # define HAVE_QMI_ACTION_PDC_REFRESH
130 #endif
131
132 static GOptionEntry entries[] = {
133 #if defined HAVE_QMI_ACTION_PDC_LIST_CONFIGS
134 {
135 "pdc-list-configs", 0, 0, G_OPTION_ARG_STRING, &list_configs_str,
136 "List all configs",
137 "[(platform|software)]"
138 },
139 #endif
140 #if defined HAVE_QMI_MESSAGE_PDC_DELETE_CONFIG
141 {
142 "pdc-delete-config", 0, 0, G_OPTION_ARG_STRING, &delete_config_str,
143 "Delete config",
144 "[(platform|software),ConfigId]"
145 },
146 #endif
147 #if defined HAVE_QMI_ACTION_PDC_ACTIVATE_CONFIG
148 {
149 "pdc-activate-config", 0, 0, G_OPTION_ARG_STRING, &activate_config_str,
150 "Activate config",
151 "[(platform|software),ConfigId]"
152 },
153 #endif
154 #if defined HAVE_QMI_ACTION_PDC_DEACTIVATE_CONFIG
155 {
156 "pdc-deactivate-config", 0, 0, G_OPTION_ARG_STRING, &deactivate_config_str,
157 "Deactivate config",
158 "[(platform|software),ConfigId]"
159 },
160 #endif
161 #if defined HAVE_QMI_ACTION_PDC_LOAD_CONFIG
162 {
163 "pdc-load-config", 0, 0, G_OPTION_ARG_STRING, &load_config_str,
164 "Load config to device",
165 "[Path to config]"
166 },
167 #endif
168 #if defined HAVE_QMI_ACTION_PDC_REFRESH
169 {
170 "pdc-monitor-refresh", 0, 0, G_OPTION_ARG_NONE, &monitor_refresh_flag,
171 "Watch for refresh indications",
172 NULL
173 },
174 #endif
175 {
176 "pdc-noop", 0, 0, G_OPTION_ARG_NONE, &noop_flag,
177 "Just allocate or release a PDC client. Use with `--client-no-release-cid' and/or `--client-cid'",
178 NULL
179 },
180 { NULL, 0, 0, 0, NULL, NULL, NULL }
181 };
182
183 GOptionGroup *
184 qmicli_pdc_get_option_group (void)
185 {
186 GOptionGroup *group;
187
188 group = g_option_group_new ("pdc",
189 "PDC options:",
190 "Show platform device configurations options", NULL, NULL);
191 g_option_group_add_entries (group, entries);
192
193 return group;
194 }
195
196 gboolean
197 qmicli_pdc_options_enabled (void)
198 {
199 static guint n_actions = 0;
200 static gboolean checked = FALSE;
201
202 if (checked)
203 return !!n_actions;
204
205 n_actions = (!!list_configs_str +
206 !!delete_config_str +
207 !!activate_config_str +
208 !!deactivate_config_str +
209 !!load_config_str +
210 monitor_refresh_flag +
211 noop_flag);
212
213 if (n_actions > 1) {
214 g_printerr ("error: too many PDC actions requested\n");
215 exit (EXIT_FAILURE);
216 }
217
218 /* Actions that require receiving QMI indication messages must specify that
219 * indications are expected. */
220 if (list_configs_str || activate_config_str || deactivate_config_str || load_config_str || monitor_refresh_flag)
221 qmicli_expect_indications ();
222
223 checked = TRUE;
224 return !!n_actions;
225 }
226
227 static Context *
228 context_new (QmiDevice *device,
229 QmiClientPdc *client,
230 GCancellable *cancellable)
231 {
232 Context *context;
233
234 context = g_slice_new0 (Context);
235 context->device = g_object_ref (device);
236 context->client = g_object_ref (client);
237 context->cancellable = g_object_ref (cancellable);
238 return context;
239 }
240
241 static void
242 context_free (Context *context)
243 {
244 guint i;
245
246 if (!context)
247 return;
248
249 if (context->config_list) {
250 for (i = 0; i < context->config_list->len; i++) {
251 ConfigInfo *current_config;
252
253 current_config = &g_array_index (context->config_list, ConfigInfo, i);
254 g_free (current_config->description);
255 if (current_config->id)
256 g_array_unref (current_config->id);
257 }
258 g_array_unref (context->config_list);
259 g_signal_handler_disconnect (context->client, context->list_configs_indication_id);
260 g_signal_handler_disconnect (context->client, context->get_config_info_indication_id);
261 g_signal_handler_disconnect (context->client, context->get_selected_config_indication_id);
262 }
263
264 if (context->load_config_file_data) {
265 g_array_unref (context->load_config_file_data->checksum);
266 g_mapped_file_unref (context->load_config_file_data->mapped_file);
267 g_slice_free (LoadConfigFileData, context->load_config_file_data);
268 g_signal_handler_disconnect (context->client, context->load_config_indication_id);
269 }
270
271 if (context->set_selected_config_indication_id)
272 g_signal_handler_disconnect (context->client, context->set_selected_config_indication_id);
273
274 if (context->activate_config_indication_id)
275 g_signal_handler_disconnect (context->client, context->activate_config_indication_id);
276
277 if (context->device_removed_indication_id)
278 g_signal_handler_disconnect (context->device, context->device_removed_indication_id);
279
280 if (context->deactivate_config_indication_id)
281 g_signal_handler_disconnect (context->client, context->deactivate_config_indication_id);
282
283 g_object_unref (context->cancellable);
284 g_object_unref (context->client);
285 g_object_unref (context->device);
286
287 g_slice_free (Context, context);
288 }
289
290 static void
291 operation_shutdown (gboolean operation_status)
292 {
293 /* Cleanup context and finish async operation */
294 qmicli_async_operation_done (operation_status, ctx->skip_cid_release);
295 context_free (ctx);
296 ctx = NULL;
297 }
298
299 /******************************************************************************/
300 /* Common */
301
302 #if defined HAVE_QMI_ACTION_PDC_ACTIVATE_CONFIG || \
303 defined HAVE_QMI_ACTION_PDC_DEACTIVATE_CONFIG || \
304 defined HAVE_QMI_MESSAGE_PDC_DELETE_CONFIG
305
306 static gboolean
307 parse_type_and_id (const gchar *str,
308 QmiPdcConfigurationType *config_type,
309 GArray **id)
310 {
311 guint num_parts;
312 g_auto(GStrv) substrings = NULL;
313
314 substrings = g_strsplit (str, ",", -1);
315 num_parts = g_strv_length (substrings);
316
317 if (num_parts != 2) {
318 g_printerr ("Expected 2 parameters, but found %u\n", num_parts);
319 return FALSE;
320 }
321
322 if (!qmicli_read_pdc_configuration_type_from_string (substrings[0], config_type)) {
323 g_printerr ("Incorrect id specified: %s\n", substrings[0]);
324 return FALSE;
325 }
326
327 if (!qmicli_read_binary_array_from_string (substrings[1], id)) {
328 g_printerr ("Incorrect config type specified: %s\n", substrings[1]);
329 return FALSE;
330 }
331
332 return TRUE;
333 }
334
335 #endif /* HAVE_QMI_ACTION_PDC_ACTIVATE_CONFIG
336 * HAVE_QMI_ACTION_PDC_DEACTIVATE_CONFIG
337 * HAVE_QMI_MESSAGE_PDC_DELETE_CONFIG */
338
339 /******************************************************************************/
340 /* List configs and get selected config */
341
342 #if defined HAVE_QMI_ACTION_PDC_LIST_CONFIGS
343
344 static const char *
345 status_string (GArray *id)
346 {
347 if (!id)
348 return "Unknown";
349 if (ctx->active_config_id &&
350 id->len == ctx->active_config_id->len &&
351 memcmp (id->data, ctx->active_config_id->data, id->len) == 0)
352 return "Active";
353 if (ctx->pending_config_id &&
354 id->len == ctx->pending_config_id->len &&
355 memcmp (id->data, ctx->pending_config_id->data, id->len) == 0)
356 return "Pending";
357 return "Inactive";
358 }
359
360 static void
361 print_configs (GArray *configs)
362 {
363 guint i;
364
365 g_printf ("Total configurations: %u\n", ctx->config_list->len);
366 for (i = 0; i < ctx->config_list->len; i++) {
367 ConfigInfo *current_config;
368 char *id_str;
369
370 current_config = &g_array_index (ctx->config_list, ConfigInfo, i);
371
372 g_printf ("Configuration %u:\n", i + 1);
373 g_printf ("\tDescription: %s\n", current_config->description);
374 g_printf ("\tType: %s\n", qmi_pdc_configuration_type_get_string (current_config->config_type));
375 g_printf ("\tSize: %u\n", current_config->total_size);
376 g_printf ("\tStatus: %s\n", status_string (current_config->id));
377 g_printf ("\tVersion: 0x%X\n", current_config->version);
378 id_str = qmicli_get_raw_data_printable (current_config->id, 80, "");
379 g_printf ("\tID: %s\n", id_str ? id_str : "none");
380 g_free (id_str);
381 }
382 }
383
384 static void
385 check_list_config_completed (void)
386 {
387 if (ctx->configs_loaded == ctx->config_list->len &&
388 ctx->ids_loaded) {
389 print_configs (ctx->config_list);
390 operation_shutdown (TRUE);
391 }
392 }
393
394 static void
395 get_config_info_ready (QmiClientPdc *client,
396 GAsyncResult *res)
397 {
398 g_autoptr(GError) error = NULL;
399 g_autoptr(QmiMessagePdcGetConfigInfoOutput) output = NULL;
400
401 output = qmi_client_pdc_get_config_info_finish (client, res, &error);
402 if (!output) {
403 g_printerr ("error: operation failed: %s\n", error->message);
404 operation_shutdown (FALSE);
405 return;
406 }
407
408 if (!qmi_message_pdc_get_config_info_output_get_result (output, &error)) {
409 g_printerr ("error: couldn't get config info: %s\n", error->message);
410 operation_shutdown (FALSE);
411 return;
412 }
413 }
414
415 static void
416 get_config_info_ready_indication (QmiClientPdc *client,
417 QmiIndicationPdcGetConfigInfoOutput *output)
418 {
419 g_autoptr(GError) error = NULL;
420 ConfigInfo *current_config = NULL;
421 guint32 token;
422 const gchar *description;
423 guint i;
424 guint16 error_code = 0;
425
426 if (!qmi_indication_pdc_get_config_info_output_get_indication_result (output, &error_code, &error)) {
427 g_printerr ("error: couldn't get config info: %s\n", error->message);
428 operation_shutdown (FALSE);
429 return;
430 }
431
432 if (error_code != 0) {
433 g_printerr ("error: couldn't get config info: %s\n", qmi_protocol_error_get_string ((QmiProtocolError) error_code));
434 operation_shutdown (FALSE);
435 return;
436 }
437
438 if (!qmi_indication_pdc_get_config_info_output_get_token (output, &token, &error)) {
439 g_printerr ("error: couldn't get config info token: %s\n", error->message);
440 operation_shutdown (FALSE);
441 return;
442 }
443
444 /* Look for the current config in the list */
445 for (i = 0; i < ctx->config_list->len; i++) {
446 current_config = &g_array_index (ctx->config_list, ConfigInfo, i);
447 if (current_config->token == token)
448 break;
449 }
450
451 /* Store total size, version and description of the current config */
452 if (!qmi_indication_pdc_get_config_info_output_get_total_size (output,
453 &current_config->total_size,
454 &error) ||
455 !qmi_indication_pdc_get_config_info_output_get_version (output,
456 &current_config->version,
457 &error) ||
458 !qmi_indication_pdc_get_config_info_output_get_description (output, &description, &error)) {
459 g_printerr ("error: couldn't get config info details: %s\n", error->message);
460 operation_shutdown (FALSE);
461 return;
462 }
463 current_config->description = g_strdup (description);
464
465 ctx->configs_loaded++;
466
467 check_list_config_completed ();
468 }
469
470 static gboolean
471 list_configs_timeout (void)
472 {
473 /* No indication yet, cancelling */
474 if (!ctx->config_list) {
475 g_printf ("Total configurations: 0\n");
476 operation_shutdown (TRUE);
477 }
478
479 return FALSE;
480 }
481
482 static void
483 list_configs_ready_indication (QmiClientPdc *client,
484 QmiIndicationPdcListConfigsOutput *output)
485 {
486 g_autoptr(GError) error = NULL;
487 GArray *configs = NULL;
488 guint i;
489 guint16 error_code = 0;
490
491 /* Remove timeout as soon as we get the indication */
492 if (ctx->timeout_id) {
493 g_source_remove (ctx->timeout_id);
494 ctx->timeout_id = 0;
495 }
496
497 if (!qmi_indication_pdc_list_configs_output_get_indication_result (output, &error_code, &error)) {
498 g_printerr ("error: couldn't list configs: %s\n", error->message);
499 operation_shutdown (FALSE);
500 return;
501 }
502
503 if (error_code != 0) {
504 g_printerr ("error: couldn't list config: %s\n", qmi_protocol_error_get_string ((QmiProtocolError) error_code));
505 operation_shutdown (FALSE);
506 return;
507 }
508
509 if (!qmi_indication_pdc_list_configs_output_get_configs (output, &configs, &error)) {
510 g_printerr ("error: couldn't list configs: %s\n", error->message);
511 operation_shutdown (FALSE);
512 return;
513 }
514
515 /* Preallocate config list and request details for each */
516 ctx->config_list = g_array_sized_new (FALSE, TRUE, sizeof (ConfigInfo), configs->len);
517 g_array_set_size (ctx->config_list, configs->len);
518
519 for (i = 0; i < configs->len; i++) {
520 ConfigInfo *current_info;
521 QmiIndicationPdcListConfigsOutputConfigsElement *element;
522 guint32 token;
523 g_autoptr(QmiMessagePdcGetConfigInfoInput) input = NULL;
524
525 token = ctx->token++;
526
527 element = &g_array_index (configs, QmiIndicationPdcListConfigsOutputConfigsElement, i);
528
529 current_info = &g_array_index (ctx->config_list, ConfigInfo, i);
530 current_info->token = token;
531 current_info->id = g_array_ref (element->id);
532 current_info->config_type = element->config_type;
533
534 input = qmi_message_pdc_get_config_info_input_new ();
535
536 /* Add type with id */
537 if (!qmi_message_pdc_get_config_info_input_set_type_with_id_v2 (input, element->config_type, current_info->id, &error)) {
538 g_printerr ("error: couldn't set type with id: %s\n", error->message);
539 operation_shutdown (FALSE);
540 return;
541 }
542
543 /* Add token */
544 if (!qmi_message_pdc_get_config_info_input_set_token (input, token, &error)) {
545 g_printerr ("error: couldn't set token: %s\n", error->message);
546 operation_shutdown (FALSE);
547 return;
548 }
549
550 qmi_client_pdc_get_config_info (ctx->client,
551 input,
552 10,
553 ctx->cancellable,
554 (GAsyncReadyCallback) get_config_info_ready, NULL);
555 }
556
557 check_list_config_completed ();
558 }
559
560 static void
561 list_configs_ready (QmiClientPdc *client,
562 GAsyncResult *res)
563 {
564 g_autoptr(GError) error = NULL;
565 g_autoptr(QmiMessagePdcListConfigsOutput) output = NULL;
566
567 output = qmi_client_pdc_list_configs_finish (client, res, &error);
568 if (!output) {
569 g_printerr ("error: operation failed: %s\n", error->message);
570 operation_shutdown (FALSE);
571 return;
572 }
573
574 if (!qmi_message_pdc_list_configs_output_get_result (output, &error)) {
575 g_printerr ("error: couldn't list configs: %s\n", error->message);
576 operation_shutdown (FALSE);
577 return;
578 }
579 }
580
581 static QmiMessagePdcListConfigsInput *
582 list_configs_input_create (const gchar *str)
583 {
584 g_autoptr(GError) error = NULL;
585 g_autoptr(QmiMessagePdcListConfigsInput) input = NULL;
586 QmiPdcConfigurationType config_type;
587
588 if (!qmicli_read_pdc_configuration_type_from_string (str, &config_type))
589 return NULL;
590
591 input = qmi_message_pdc_list_configs_input_new ();
592 if (!qmi_message_pdc_list_configs_input_set_config_type (input, config_type, &error) ||
593 !qmi_message_pdc_list_configs_input_set_token (input, ctx->token++, &error)) {
594 g_printerr ("error: couldn't create input data bundle: '%s'\n", error->message);
595 return NULL;
596 }
597
598 return g_steal_pointer (&input);
599 }
600
601 static void
602 get_selected_config_ready_indication (QmiClientPdc *client,
603 QmiIndicationPdcGetSelectedConfigOutput *output)
604 {
605 g_autoptr(GError) error = NULL;
606 GArray *pending_id = NULL;
607 GArray *active_id = NULL;
608 guint16 error_code = 0;
609
610 if (!qmi_indication_pdc_get_selected_config_output_get_indication_result (output, &error_code, &error)) {
611 g_printerr ("error: couldn't get selected config: %s\n", error->message);
612 operation_shutdown (FALSE);
613 return;
614 }
615
616 if (error_code != 0 && error_code != QMI_PROTOCOL_ERROR_NOT_PROVISIONED) { /* No configs active */
617 g_printerr ("error: couldn't get selected config: %s\n",
618 qmi_protocol_error_get_string ((QmiProtocolError) error_code));
619 operation_shutdown (FALSE);
620 return;
621 }
622
623 qmi_indication_pdc_get_selected_config_output_get_pending_id (output, &pending_id, NULL);
624 qmi_indication_pdc_get_selected_config_output_get_active_id (output, &active_id, NULL);
625 if (active_id)
626 ctx->active_config_id = g_array_ref (active_id);
627 if (pending_id)
628 ctx->pending_config_id = g_array_ref (pending_id);
629
630 ctx->ids_loaded = TRUE;
631
632 check_list_config_completed ();
633 }
634
635 static void
636 get_selected_config_ready (QmiClientPdc *client,
637 GAsyncResult *res)
638 {
639 g_autoptr(GError) error = NULL;
640 g_autoptr(QmiMessagePdcGetSelectedConfigOutput) output = NULL;
641
642 output = qmi_client_pdc_get_selected_config_finish (client, res, &error);
643 if (!qmi_message_pdc_get_selected_config_output_get_result (output, &error)) {
644 g_printerr ("error: couldn't get selected config: %s\n", error->message);
645 operation_shutdown (FALSE);
646 return;
647 }
648 }
649
650 static QmiMessagePdcGetSelectedConfigInput *
651 get_selected_config_input_create (const gchar *str)
652 {
653 g_autoptr(GError) error = NULL;
654 g_autoptr(QmiMessagePdcGetSelectedConfigInput) input = NULL;
655 QmiPdcConfigurationType config_type;
656
657 if (!qmicli_read_pdc_configuration_type_from_string (str, &config_type))
658 return NULL;
659
660 input = qmi_message_pdc_get_selected_config_input_new ();
661 if (!qmi_message_pdc_get_selected_config_input_set_config_type (input, config_type, &error) ||
662 !qmi_message_pdc_get_selected_config_input_set_token (input, ctx->token++, &error)) {
663 g_printerr ("error: couldn't create input data bundle: '%s'\n", error->message);
664 return NULL;
665 }
666
667 return g_steal_pointer (&input);
668 }
669
670 static void
671 run_list_configs (void)
672 {
673 g_autoptr(QmiMessagePdcListConfigsInput) input = NULL;
674 g_autoptr(QmiMessagePdcGetSelectedConfigInput) get_selected_config_input = NULL;
675
676 g_debug ("Listing configs asynchronously...");
677
678 /* Results are reported via indications */
679 ctx->list_configs_indication_id =
680 g_signal_connect (ctx->client,
681 "list-configs",
682 G_CALLBACK
683 (list_configs_ready_indication), NULL);
684 ctx->get_selected_config_indication_id =
685 g_signal_connect (ctx->client, "get-selected-config",
686 G_CALLBACK (get_selected_config_ready_indication), NULL);
687 ctx->get_config_info_indication_id =
688 g_signal_connect (ctx->client, "get-config-info",
689 G_CALLBACK (get_config_info_ready_indication), NULL);
690
691 input = list_configs_input_create (list_configs_str);
692 if (!input) {
693 operation_shutdown (FALSE);
694 return;
695 }
696
697 get_selected_config_input = get_selected_config_input_create (list_configs_str);
698 if (!get_selected_config_input) {
699 operation_shutdown (FALSE);
700 return;
701 }
702
703 /* We need a timeout, because there will be no indications if no configs
704 * are loaded */
705 ctx->timeout_id = g_timeout_add_seconds (LIST_CONFIGS_TIMEOUT_SECS,
706 (GSourceFunc) list_configs_timeout,
707 NULL);
708
709 qmi_client_pdc_list_configs (ctx->client,
710 input,
711 10,
712 ctx->cancellable,
713 (GAsyncReadyCallback) list_configs_ready,
714 NULL);
715
716 qmi_client_pdc_get_selected_config (ctx->client,
717 get_selected_config_input,
718 10,
719 ctx->cancellable,
720 (GAsyncReadyCallback) get_selected_config_ready,
721 NULL);
722 }
723
724 #endif /* HAVE_QMI_ACTION_PDC_LIST_CONFIGS */
725
726 /******************************************************************************/
727 /* Activate config */
728
729 #if defined HAVE_QMI_ACTION_PDC_ACTIVATE_CONFIG
730
731 static void
732 device_removed_indication (QmiDevice *device)
733 {
734 g_print ("[%s] Successfully requested config activation\n",
735 qmi_device_get_path_display (ctx->device));
736
737 /* Device gone, don't attempt to release CIDs */
738 ctx->skip_cid_release = TRUE;
739
740 /* If device gets removed during an activate config operation,
741 * it means the operation is successful */
742 operation_shutdown (TRUE);
743 }
744
745 static void
746 activate_config_ready_indication (QmiClientPdc *client,
747 QmiIndicationPdcActivateConfigOutput *output)
748 {
749 g_autoptr(GError) error = NULL;
750 guint16 error_code = 0;
751
752 if (!qmi_indication_pdc_activate_config_output_get_indication_result (output, &error_code, &error)) {
753 g_printerr ("error: couldn't activate config: %s\n", error->message);
754 operation_shutdown (FALSE);
755 return;
756 }
757
758 if (error_code != 0) {
759 g_printerr ("error: couldn't activate config: %s\n", qmi_protocol_error_get_string ((QmiProtocolError) error_code));
760 operation_shutdown (FALSE);
761 return;
762 }
763
764 /* NOTE: config activation is expected to reboot the device, so we may detect the
765 * actual reboot before receiving this indication */
766
767 g_print ("[%s] Successfully requested config activation\n",
768 qmi_device_get_path_display (ctx->device));
769
770 operation_shutdown (TRUE);
771 }
772
773 static void
774 activate_config_ready (QmiClientPdc *client,
775 GAsyncResult *res)
776 {
777 g_autoptr(GError) error = NULL;
778 g_autoptr(QmiMessagePdcActivateConfigOutput) output = NULL;
779
780 output = qmi_client_pdc_activate_config_finish (client, res, &error);
781 if (!output) {
782 g_printerr ("error: operation failed: %s\n", error->message);
783 operation_shutdown (FALSE);
784 return;
785 }
786
787 if (!qmi_message_pdc_activate_config_output_get_result (output, &error)) {
788 g_printerr ("error: couldn't activate config: %s\n", error->message);
789 operation_shutdown (FALSE);
790 return;
791 }
792 }
793
794 static QmiMessagePdcActivateConfigInput *
795 activate_config_input_create (const gchar *str)
796 {
797 g_autoptr(QmiMessagePdcActivateConfigInput) input = NULL;
798 g_autoptr(GError) error = NULL;
799 QmiPdcConfigurationType config_type;
800 g_autoptr(GArray) id = NULL;
801
802 /* Note: id not needed here really */
803 if (!parse_type_and_id (str, &config_type, &id))
804 return NULL;
805
806 input = qmi_message_pdc_activate_config_input_new ();
807 if (!qmi_message_pdc_activate_config_input_set_config_type (input, config_type, &error) ||
808 !qmi_message_pdc_activate_config_input_set_token (input, ctx->token++, &error)) {
809 g_printerr ("error: couldn't create input data bundle: '%s'\n", error->message);
810 return NULL;
811 }
812
813 return g_steal_pointer (&input);
814 }
815
816 static void
817 set_selected_config_ready_indication (QmiClientPdc *client,
818 QmiIndicationPdcSetSelectedConfigOutput *output)
819 {
820 g_autoptr(GError) error = NULL;
821 g_autoptr(QmiMessagePdcActivateConfigInput) input = NULL;
822 guint16 error_code = 0;
823
824 if (!qmi_indication_pdc_set_selected_config_output_get_indication_result (output, &error_code, &error)) {
825 g_printerr ("error: couldn't set selected config: %s\n", error->message);
826 operation_shutdown (FALSE);
827 return;
828 }
829
830 if (error_code != 0) {
831 g_printerr ("error: couldn't set selected config: %s\n",
832 qmi_protocol_error_get_string ((QmiProtocolError) error_code));
833 operation_shutdown (FALSE);
834 return;
835 }
836
837 input = activate_config_input_create (activate_config_str);
838 if (!input) {
839 operation_shutdown (FALSE);
840 return;
841 }
842
843 ctx->activate_config_indication_id =
844 g_signal_connect (ctx->client,
845 "activate-config",
846 G_CALLBACK (activate_config_ready_indication),
847 NULL);
848 ctx->device_removed_indication_id =
849 g_signal_connect (ctx->device,
850 QMI_DEVICE_SIGNAL_REMOVED,
851 G_CALLBACK (device_removed_indication),
852 NULL);
853 qmi_client_pdc_activate_config (ctx->client,
854 input,
855 10,
856 ctx->cancellable,
857 (GAsyncReadyCallback) activate_config_ready, NULL);
858 }
859
860 static void
861 set_selected_config_ready (QmiClientPdc *client,
862 GAsyncResult *res)
863 {
864 g_autoptr(GError) error = NULL;
865 g_autoptr(QmiMessagePdcSetSelectedConfigOutput) output = NULL;
866
867 output = qmi_client_pdc_set_selected_config_finish (client, res, &error);
868 if (!output) {
869 g_printerr ("error: operation failed: %s\n", error->message);
870 operation_shutdown (FALSE);
871 return;
872 }
873 }
874
875 static QmiMessagePdcSetSelectedConfigInput *
876 set_selected_config_input_create (const gchar *str)
877 {
878 g_autoptr(QmiMessagePdcSetSelectedConfigInput) input = NULL;
879 g_autoptr(GError) error = NULL;
880 QmiPdcConfigurationType config_type;
881 g_autoptr(GArray) id = NULL;
882
883 if (!parse_type_and_id (str, &config_type, &id))
884 return NULL;
885
886 input = qmi_message_pdc_set_selected_config_input_new ();
887 if (!qmi_message_pdc_set_selected_config_input_set_type_with_id_v2 (input, config_type, id, &error) ||
888 !qmi_message_pdc_set_selected_config_input_set_token (input, ctx->token++, &error)) {
889 g_printerr ("error: couldn't create input data bundle: '%s'\n", error->message);
890 return NULL;
891 }
892
893 return g_steal_pointer (&input);
894 }
895
896 static void
897 run_activate_config (void)
898 {
899 g_autoptr(QmiMessagePdcSetSelectedConfigInput) input = NULL;
900
901 g_debug ("Activating config asynchronously...");
902 input = set_selected_config_input_create (activate_config_str);
903 if (!input) {
904 operation_shutdown (FALSE);
905 return;
906 }
907
908 /* Results are reported via indications */
909 ctx->set_selected_config_indication_id =
910 g_signal_connect (ctx->client,
911 "set-selected-config",
912 G_CALLBACK (set_selected_config_ready_indication), NULL);
913 qmi_client_pdc_set_selected_config (ctx->client,
914 input,
915 10,
916 ctx->cancellable,
917 (GAsyncReadyCallback) set_selected_config_ready,
918 NULL);
919 }
920
921 #endif /* HAVE_QMI_ACTION_PDC_ACTIVATE_CONFIG */
922
923 /******************************************************************************/
924 /* Deactivate config */
925
926 #if defined HAVE_QMI_ACTION_PDC_DEACTIVATE_CONFIG
927
928 static void
929 deactivate_config_ready_indication (QmiClientPdc *client,
930 QmiIndicationPdcDeactivateConfigOutput *output)
931 {
932 g_autoptr(GError) error = NULL;
933 guint16 error_code = 0;
934
935 if (!qmi_indication_pdc_deactivate_config_output_get_indication_result (output, &error_code, &error)) {
936 g_printerr ("error: couldn't deactivate config: %s\n", error->message);
937 operation_shutdown (FALSE);
938 return;
939 }
940
941 if (error_code != 0) {
942 g_printerr ("error: couldn't deactivate config: %s\n", qmi_protocol_error_get_string ((QmiProtocolError) error_code));
943 operation_shutdown (FALSE);
944 return;
945 }
946
947 g_print ("[%s] Successfully requested config deactivation\n",
948 qmi_device_get_path_display (ctx->device));
949
950 operation_shutdown (TRUE);
951 }
952
953 static void
954 deactivate_config_ready (QmiClientPdc *client,
955 GAsyncResult *res)
956 {
957 g_autoptr(GError) error = NULL;
958 g_autoptr(QmiMessagePdcDeactivateConfigOutput) output = NULL;
959
960 output = qmi_client_pdc_deactivate_config_finish (client, res, &error);
961 if (!output) {
962 g_printerr ("error: operation failed: %s\n", error->message);
963 operation_shutdown (FALSE);
964 return;
965 }
966
967 if (!qmi_message_pdc_deactivate_config_output_get_result (output, &error)) {
968 g_printerr ("error: couldn't deactivate config: %s\n", error->message);
969 operation_shutdown (FALSE);
970 return;
971 }
972 }
973
974 static QmiMessagePdcDeactivateConfigInput *
975 deactivate_config_input_create (const gchar *str)
976 {
977 g_autoptr(QmiMessagePdcDeactivateConfigInput) input = NULL;
978 g_autoptr(GError) error = NULL;
979 QmiPdcConfigurationType config_type;
980 g_autoptr(GArray) id = NULL;
981
982 /* Note: id not needed here really */
983 if (!parse_type_and_id (str, &config_type, &id))
984 return NULL;
985
986 input = qmi_message_pdc_deactivate_config_input_new ();
987 if (!qmi_message_pdc_deactivate_config_input_set_config_type (input, config_type, &error) ||
988 !qmi_message_pdc_deactivate_config_input_set_token (input, ctx->token++, &error)) {
989 g_printerr ("error: couldn't create input data bundle: '%s'\n", error->message);
990 return NULL;
991 }
992
993 return g_steal_pointer (&input);
994 }
995
996 static void
997 run_deactivate_config (void)
998 {
999 g_autoptr(QmiMessagePdcDeactivateConfigInput) input = NULL;
1000
1001 g_debug ("Deactivating config asynchronously...");
1002 input = deactivate_config_input_create (deactivate_config_str);
1003 if (!input) {
1004 operation_shutdown (FALSE);
1005 return;
1006 }
1007
1008 /* Results are reported via indications */
1009 ctx->deactivate_config_indication_id =
1010 g_signal_connect (ctx->client,
1011 "deactivate-config",
1012 G_CALLBACK
1013 (deactivate_config_ready_indication),
1014 NULL);
1015
1016 qmi_client_pdc_deactivate_config (ctx->client,
1017 input,
1018 10,
1019 ctx->cancellable,
1020 (GAsyncReadyCallback) deactivate_config_ready, NULL);
1021 }
1022
1023 #endif /* HAVE_QMI_ACTION_PDC_DEACTIVATE_CONFIG */
1024
1025 /******************************************************************************/
1026 /* Delete config */
1027
1028 #if defined HAVE_QMI_MESSAGE_PDC_DELETE_CONFIG
1029
1030 static void
1031 delete_config_ready (QmiClientPdc *client,
1032 GAsyncResult *res)
1033 {
1034 g_autoptr(GError) error = NULL;
1035 g_autoptr(QmiMessagePdcDeleteConfigOutput) output = NULL;
1036
1037 output = qmi_client_pdc_delete_config_finish (client, res, &error);
1038 if (!output) {
1039 g_printerr ("error: operation failed: %s\n", error->message);
1040 operation_shutdown (FALSE);
1041 return;
1042 }
1043
1044 if (!qmi_message_pdc_delete_config_output_get_result (output, &error)) {
1045 g_printerr ("error: couldn't delete config: %s\n", error->message);
1046 operation_shutdown (FALSE);
1047 return;
1048 }
1049
1050 g_print ("[%s] Successfully deleted config\n",
1051 qmi_device_get_path_display (ctx->device));
1052
1053 operation_shutdown (TRUE);
1054 }
1055
1056 static QmiMessagePdcDeleteConfigInput *
1057 delete_config_input_create (const gchar *str)
1058 {
1059 g_autoptr(QmiMessagePdcDeleteConfigInput) input = NULL;
1060 g_autoptr(GError) error = NULL;
1061 QmiPdcConfigurationType config_type;
1062 g_autoptr(GArray) id = NULL;
1063
1064 if (!parse_type_and_id (str, &config_type, &id))
1065 return NULL;
1066
1067 input = qmi_message_pdc_delete_config_input_new ();
1068 if (!qmi_message_pdc_delete_config_input_set_config_type (input, config_type, &error) ||
1069 !qmi_message_pdc_delete_config_input_set_token (input, ctx->token++, &error) ||
1070 !qmi_message_pdc_delete_config_input_set_id (input, id, &error)) {
1071 g_printerr ("error: couldn't create input data bundle: '%s'\n", error->message);
1072 return NULL;
1073 }
1074
1075 return g_steal_pointer (&input);
1076 }
1077
1078 static void
1079 run_delete_config (void)
1080 {
1081 g_autoptr(QmiMessagePdcDeleteConfigInput) input = NULL;
1082
1083 g_debug ("Deleting config asynchronously...");
1084 input = delete_config_input_create (delete_config_str);
1085 if (!input) {
1086 operation_shutdown (FALSE);
1087 return;
1088 }
1089
1090 qmi_client_pdc_delete_config (ctx->client,
1091 input,
1092 10,
1093 ctx->cancellable,
1094 (GAsyncReadyCallback) delete_config_ready, NULL);
1095 }
1096
1097 #endif /* HAVE_QMI_MESSAGE_PDC_DELETE_CONFIG */
1098
1099 /******************************************************************************/
1100 /* Load config */
1101
1102 #if defined HAVE_QMI_ACTION_PDC_LOAD_CONFIG
1103
1104 static LoadConfigFileData *
1105 load_config_file_from_string (const gchar *str)
1106 {
1107 g_autoptr(GError) error = NULL;
1108 g_autoptr(GMappedFile) mapped_file = NULL;
1109 LoadConfigFileData *data;
1110 guchar *file_contents;
1111 GChecksum *checksum;
1112 gsize file_size;
1113 gsize hash_size;
1114
1115 if (!(mapped_file = g_mapped_file_new (str, FALSE, &error))) {
1116 g_printerr ("error: couldn't map config file: '%s'\n", error->message);
1117 return NULL;
1118 }
1119
1120 if (!(file_contents = (guchar *) g_mapped_file_get_contents (mapped_file))) {
1121 g_printerr ("error: couldn't get file content\n");
1122 return NULL;
1123 }
1124
1125 /* Get checksum */
1126 file_size = g_mapped_file_get_length (mapped_file);
1127 hash_size = g_checksum_type_get_length (G_CHECKSUM_SHA1);
1128 checksum = g_checksum_new (G_CHECKSUM_SHA1);
1129 g_checksum_update (checksum, file_contents, file_size);
1130 g_free (file_contents);
1131
1132 data = g_slice_new (LoadConfigFileData);
1133 data->mapped_file = g_mapped_file_ref (mapped_file);
1134 data->checksum = g_array_sized_new (FALSE, FALSE, sizeof (guint8), hash_size);
1135 g_array_set_size (data->checksum, hash_size);
1136 data->offset = 0;
1137 g_checksum_get_digest (checksum, &g_array_index (data->checksum, guint8, 0), &hash_size);
1138 g_checksum_free (checksum);
1139
1140 return data;
1141 }
1142
1143 static QmiMessagePdcLoadConfigInput *
1144 load_config_input_create_chunk (LoadConfigFileData *config_file)
1145 {
1146 g_autoptr(QmiMessagePdcLoadConfigInput) input = NULL;
1147 g_autoptr(GError) error = NULL;
1148 g_autoptr(GArray) chunk = NULL;
1149 gsize full_size;
1150 gsize chunk_size;
1151 guint8 *file_content;
1152
1153 if (!config_file)
1154 return NULL;
1155
1156 input = qmi_message_pdc_load_config_input_new ();
1157 if (!qmi_message_pdc_load_config_input_set_token (input, ctx->token++, &error)) {
1158 g_printerr ("error: couldn't set token: '%s'\n", error->message);
1159 return NULL;
1160 }
1161
1162 chunk = g_array_new (FALSE, FALSE, sizeof (guint8));
1163 full_size = g_mapped_file_get_length (config_file->mapped_file);
1164 chunk_size = (((config_file->offset + LOAD_CONFIG_CHUNK_SIZE) > full_size) ?
1165 (full_size - config_file->offset) :
1166 LOAD_CONFIG_CHUNK_SIZE);
1167
1168 file_content = (guint8 *) g_mapped_file_get_contents (config_file->mapped_file);
1169 g_array_append_vals (chunk, &file_content[config_file->offset], chunk_size);
1170 g_print ("Uploaded %" G_GSIZE_FORMAT " of %" G_GSIZE_FORMAT "\n", config_file->offset, full_size);
1171
1172 if (!qmi_message_pdc_load_config_input_set_config_chunk (input,
1173 QMI_PDC_CONFIGURATION_TYPE_SOFTWARE,
1174 config_file->checksum,
1175 full_size,
1176 chunk,
1177 &error)) {
1178 g_printerr ("error: couldn't set chunk: '%s'\n", error->message);
1179 return NULL;
1180 }
1181
1182 config_file->offset += chunk_size;
1183
1184 return g_steal_pointer (&input);
1185 }
1186
1187 static void load_config_ready (QmiClientPdc *client,
1188 GAsyncResult *res);
1189
1190 static void
1191 load_config_ready_indication (QmiClientPdc *client,
1192 QmiIndicationPdcLoadConfigOutput *output)
1193 {
1194 g_autoptr(GError) error = NULL;
1195 g_autoptr(QmiMessagePdcLoadConfigInput) input = NULL;
1196 gboolean frame_reset;
1197 guint32 remaining_size;
1198 guint16 error_code = 0;
1199
1200 if (!qmi_indication_pdc_load_config_output_get_indication_result (output, &error_code, &error)) {
1201 g_printerr ("error: couldn't load config: %s\n", error->message);
1202 operation_shutdown (FALSE);
1203 return;
1204 }
1205
1206 if (error_code != 0) {
1207 g_printerr ("error: couldn't load config: %s\n", qmi_protocol_error_get_string ((QmiProtocolError) error_code));
1208 operation_shutdown (FALSE);
1209 return;
1210 }
1211
1212 if (qmi_indication_pdc_load_config_output_get_frame_reset (output, &frame_reset, NULL) && frame_reset) {
1213 g_printerr ("error: frame reset requested\n");
1214 operation_shutdown (FALSE);
1215 return;
1216 }
1217
1218 if (!qmi_indication_pdc_load_config_output_get_remaining_size (output, &remaining_size, &error)) {
1219 g_printerr ("error: couldn't load config: %s\n", error->message);
1220 operation_shutdown (FALSE);
1221 return;
1222 }
1223
1224 if (remaining_size == 0) {
1225 g_print ("Finished loading\n");
1226 operation_shutdown (TRUE);
1227 return;
1228 }
1229
1230 g_print ("Loading next chunk (%u bytes remaining)\n", remaining_size);
1231 input = load_config_input_create_chunk (ctx->load_config_file_data);
1232 if (!input) {
1233 g_printerr ("error: couldn't create next chunk\n");
1234 operation_shutdown (FALSE);
1235 return;
1236 }
1237
1238 qmi_client_pdc_load_config (ctx->client,
1239 input,
1240 10,
1241 ctx->cancellable,
1242 (GAsyncReadyCallback) load_config_ready, NULL);
1243 }
1244
1245 static void
1246 load_config_ready (QmiClientPdc *client,
1247 GAsyncResult *res)
1248 {
1249 g_autoptr(GError) error = NULL;
1250 g_autoptr(QmiMessagePdcLoadConfigOutput) output = NULL;
1251
1252 output = qmi_client_pdc_load_config_finish (client, res, &error);
1253 if (!output) {
1254 g_printerr ("error: operation failed: %s\n", error->message);
1255 operation_shutdown (FALSE);
1256 return;
1257 }
1258
1259 if (!qmi_message_pdc_load_config_output_get_result (output, &error)) {
1260 g_printerr ("error: couldn't load config: %s\n", error->message);
1261 operation_shutdown (FALSE);
1262 return;
1263 }
1264 }
1265
1266 static QmiMessagePdcLoadConfigInput *
1267 load_config_input_create (const gchar *str)
1268 {
1269 LoadConfigFileData *config_file;
1270 QmiMessagePdcLoadConfigInput *input;
1271
1272 config_file = load_config_file_from_string (str);
1273 if (!config_file)
1274 return NULL;
1275
1276 input = load_config_input_create_chunk (config_file);
1277 if (!input)
1278 return NULL;
1279
1280 ctx->load_config_file_data = config_file;
1281 return input;
1282 }
1283
1284 static void
1285 run_load_config (void)
1286 {
1287 g_autoptr(QmiMessagePdcLoadConfigInput) input = NULL;
1288
1289 g_debug ("Loading config asynchronously...");
1290 input = load_config_input_create (load_config_str);
1291 if (!input) {
1292 operation_shutdown (FALSE);
1293 return;
1294 }
1295
1296 /* Results are reported via indications */
1297 ctx->load_config_indication_id =
1298 g_signal_connect (ctx->client,
1299 "load-config",
1300 G_CALLBACK (load_config_ready_indication),
1301 NULL);
1302
1303 qmi_client_pdc_load_config (ctx->client,
1304 input,
1305 10,
1306 ctx->cancellable,
1307 (GAsyncReadyCallback) load_config_ready,
1308 NULL);
1309 }
1310
1311 #endif /* HAVE_QMI_ACTION_PDC_LOAD_CONFIG */
1312
1313 /******************************************************************************/
1314 /* Refresh */
1315
1316 #if defined HAVE_QMI_ACTION_PDC_REFRESH
1317
1318 #define VALIDATE_UNKNOWN(str) (str ? str : "unknown")
1319
1320 static void
1321 monitoring_cancelled (GCancellable *cancellable)
1322 {
1323 operation_shutdown (TRUE);
1324 }
1325
1326 static void
1327 refresh_received (QmiClientPdc *client,
1328 QmiIndicationPdcRefreshOutput *output)
1329 {
1330 g_autoptr(GError) error = NULL;
1331 QmiPdcRefreshEventType type;
1332 guint subscription_id;
1333 guint slot_id;
1334
1335 g_print ("[%s] Received refresh indication:\n",
1336 qmi_device_get_path_display (ctx->device));
1337
1338 if (!qmi_indication_pdc_refresh_output_get_refresh_event (output, &type, &error)) {
1339 g_printerr ("error: refresh event has no type: %s\n", error->message);
1340 operation_shutdown (FALSE);
1341 return;
1342 }
1343
1344 g_print (" Received event type %s",
1345 VALIDATE_UNKNOWN (qmi_pdc_refresh_event_type_get_string (type)));
1346 if (qmi_indication_pdc_refresh_output_get_subscription_id (output, &subscription_id, NULL))
1347 g_print (", subscription ID: %u", subscription_id);
1348 if (qmi_indication_pdc_refresh_output_get_slot_id (output, &slot_id, NULL))
1349 g_print (", slot ID: %u", slot_id);
1350 g_print ("\n");
1351 }
1352
1353 static void
1354 register_refresh_ready (QmiClientPdc *client,
1355 GAsyncResult *res)
1356 {
1357 g_autoptr(QmiMessagePdcRegisterOutput) output = NULL;
1358 g_autoptr(GError) error = NULL;
1359
1360 output = qmi_client_pdc_register_finish (client, res, &error);
1361 if (!output) {
1362 g_printerr ("error: operation failed: %s\n", error->message);
1363 operation_shutdown (FALSE);
1364 return;
1365 }
1366
1367 if (!qmi_message_pdc_register_output_get_result (output, &error)) {
1368 g_printerr ("error: could not register for refresh events: %s\n", error->message);
1369 operation_shutdown (FALSE);
1370 return;
1371 }
1372
1373 g_debug ("Registered for refresh events...");
1374 ctx->refresh_indication_id =
1375 g_signal_connect (ctx->client,
1376 "refresh",
1377 G_CALLBACK (refresh_received),
1378 NULL);
1379
1380 /* User can use Ctrl+C to cancel the monitoring at any time */
1381 g_cancellable_connect (ctx->cancellable,
1382 G_CALLBACK (monitoring_cancelled),
1383 NULL,
1384 NULL);
1385 }
1386
1387 static void
1388 register_refresh_events (void)
1389 {
1390 g_autoptr(QmiMessagePdcRegisterInput) re_input = NULL;
1391
1392 re_input = qmi_message_pdc_register_input_new ();
1393 qmi_message_pdc_register_input_set_enable_reporting (re_input, TRUE, NULL);
1394 qmi_message_pdc_register_input_set_enable_refresh (re_input, TRUE, NULL);
1395 qmi_client_pdc_register (
1396 ctx->client,
1397 re_input,
1398 10,
1399 ctx->cancellable,
1400 (GAsyncReadyCallback) register_refresh_ready,
1401 NULL);
1402 }
1403
1404 #endif /* HAVE_QMI_ACTION_PDC_REFRESH */
1405
1406 /******************************************************************************/
1407 /* Common */
1408
1409 static gboolean
1410 noop_cb (gpointer unused)
1411 {
1412 operation_shutdown (TRUE);
1413 return FALSE;
1414 }
1415
1416 void
1417 qmicli_pdc_run (QmiDevice *device,
1418 QmiClientPdc *client,
1419 GCancellable *cancellable)
1420 {
1421 /* Initialize context */
1422 ctx = context_new (device, client, cancellable);
1423
1424 #if defined HAVE_QMI_ACTION_PDC_LIST_CONFIGS
1425 if (list_configs_str) {
1426 run_list_configs ();
1427 return;
1428 }
1429 #endif
1430
1431 #if defined HAVE_QMI_ACTION_PDC_ACTIVATE_CONFIG
1432 if (activate_config_str) {
1433 run_activate_config ();
1434 return;
1435 }
1436 #endif
1437
1438 #if defined HAVE_QMI_ACTION_PDC_DEACTIVATE_CONFIG
1439 if (deactivate_config_str) {
1440 run_deactivate_config ();
1441 return;
1442 }
1443 #endif
1444
1445 #if defined HAVE_QMI_MESSAGE_PDC_DELETE_CONFIG
1446 if (delete_config_str) {
1447 run_delete_config ();
1448 return;
1449 }
1450 #endif
1451
1452 #if defined HAVE_QMI_ACTION_PDC_LOAD_CONFIG
1453 if (load_config_str) {
1454 run_load_config ();
1455 return;
1456 }
1457 #endif
1458
1459 #if defined HAVE_QMI_ACTION_PDC_REFRESH
1460 if (monitor_refresh_flag) {
1461 register_refresh_events ();
1462 return;
1463 }
1464 #endif
1465
1466 /* Just client allocate/release? */
1467 if (noop_flag) {
1468 g_idle_add (noop_cb, NULL);
1469 return;
1470 }
1471
1472 g_warn_if_reached ();
1473 }
1474
1475 #endif /* HAVE_QMI_SERVICE_PDC */