Debian Codegraph @

src/qmicli/qmicli-pbm.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-2017 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 <gio/gio.h>
30
31 #include <libqmi-glib.h>
32
33 #include "qmicli.h"
34
35 #if defined HAVE_QMI_SERVICE_PBM
36
37 #undef VALIDATE_MASK_NONE
38 #define VALIDATE_MASK_NONE(str) (str ? str : "none")
39
40 /* Context */
41 typedef struct {
42 QmiDevice *device;
43 QmiClientPbm *client;
44 GCancellable *cancellable;
45 } Context;
46 static Context *ctx;
47
48 /* Options */
49 static gboolean get_all_capabilities_flag;
50 static gboolean get_emergency_list_flag;
51 static gboolean noop_flag;
52
53 static GOptionEntry entries[] = {
54 #if defined HAVE_QMI_MESSAGE_PBM_GET_ALL_CAPABILITIES
55 { "pbm-get-all-capabilities", 0, 0, G_OPTION_ARG_NONE, &get_all_capabilities_flag,
56 "Get all phonebook capabilities",
57 NULL
58 },
59 #endif
60 #if defined HAVE_QMI_MESSAGE_PBM_GET_EMERGENCY_LIST
61 { "pbm-get-emergency-list", 0, 0, G_OPTION_ARG_NONE, &get_emergency_list_flag,
62 "Get list of emergency numbers",
63 NULL
64 },
65 #endif
66 { "pbm-noop", 0, 0, G_OPTION_ARG_NONE, &noop_flag,
67 "Just allocate or release a PBM client. Use with `--client-no-release-cid' and/or `--client-cid'",
68 NULL
69 },
70 { NULL, 0, 0, 0, NULL, NULL, NULL }
71 };
72
73 GOptionGroup *
74 qmicli_pbm_get_option_group (void)
75 {
76 GOptionGroup *group;
77
78 group = g_option_group_new ("pbm",
79 "PBM options:",
80 "Show Phonebook Management options",
81 NULL,
82 NULL);
83 g_option_group_add_entries (group, entries);
84
85 return group;
86 }
87
88 gboolean
89 qmicli_pbm_options_enabled (void)
90 {
91 static guint n_actions = 0;
92 static gboolean checked = FALSE;
93
94 if (checked)
95 return !!n_actions;
96
97 n_actions = (get_all_capabilities_flag +
98 get_emergency_list_flag +
99 noop_flag);
100
101 if (n_actions > 1) {
102 g_printerr ("error: too many PBM actions requested\n");
103 exit (EXIT_FAILURE);
104 }
105
106 checked = TRUE;
107 return !!n_actions;
108 }
109
110 static void
111 context_free (Context *context)
112 {
113 if (!context)
114 return;
115
116 if (context->client)
117 g_object_unref (context->client);
118 g_object_unref (context->cancellable);
119 g_object_unref (context->device);
120 g_slice_free (Context, context);
121 }
122
123 static void
124 operation_shutdown (gboolean operation_status)
125 {
126 /* Cleanup context and finish async operation */
127 context_free (ctx);
128 qmicli_async_operation_done (operation_status, FALSE);
129 }
130
131 #if defined HAVE_QMI_MESSAGE_PBM_GET_ALL_CAPABILITIES
132
133 static void
134 get_all_capabilities_ready (QmiClientPbm *client,
135 GAsyncResult *res)
136 {
137 GError *error = NULL;
138 QmiMessagePbmGetAllCapabilitiesOutput *output;
139 GArray *capability_basic_information = NULL;
140 GArray *group_capability = NULL;
141 GArray *additional_number_capability = NULL;
142 GArray *email_capability = NULL;
143 GArray *second_name_capability = NULL;
144 GArray *hidden_records_capability = NULL;
145 GArray *grouping_information_alpha_string_capability = NULL;
146 GArray *additional_number_alpha_string_capability = NULL;
147 guint i, j;
148
149 output = qmi_client_pbm_get_all_capabilities_finish (client, res, &error);
150 if (!output) {
151 g_printerr ("error: operation failed: %s\n",
152 error->message);
153 g_error_free (error);
154 operation_shutdown (FALSE);
155 return;
156 }
157
158 if (!qmi_message_pbm_get_all_capabilities_output_get_result (output, &error)) {
159 g_printerr ("error: couldn't get capabilities: %s\n", error->message);
160 g_error_free (error);
161 qmi_message_pbm_get_all_capabilities_output_unref (output);
162 operation_shutdown (FALSE);
163 return;
164 }
165
166 qmi_message_pbm_get_all_capabilities_output_get_capability_basic_information (output, &capability_basic_information, NULL);
167 qmi_message_pbm_get_all_capabilities_output_get_group_capability (output, &group_capability, NULL);
168 qmi_message_pbm_get_all_capabilities_output_get_additional_number_capability (output, &additional_number_capability, NULL);
169 qmi_message_pbm_get_all_capabilities_output_get_email_capability (output, &email_capability, NULL);
170 qmi_message_pbm_get_all_capabilities_output_get_second_name_capability (output, &second_name_capability, NULL);
171 qmi_message_pbm_get_all_capabilities_output_get_hidden_records_capability (output, &hidden_records_capability, NULL);
172 qmi_message_pbm_get_all_capabilities_output_get_grouping_information_alpha_string_capability (output, &grouping_information_alpha_string_capability, NULL);
173 qmi_message_pbm_get_all_capabilities_output_get_additional_number_alpha_string_capability (output, &additional_number_alpha_string_capability, NULL);
174
175 g_print ("[%s] Phonebook capabilities:%s\n",
176 qmi_device_get_path_display (ctx->device),
177 (capability_basic_information ||
178 group_capability ||
179 additional_number_capability ||
180 email_capability ||
181 second_name_capability ||
182 hidden_records_capability ||
183 grouping_information_alpha_string_capability ||
184 additional_number_alpha_string_capability) ? "" : " none");
185
186 if (capability_basic_information) {
187 g_print ("Capability basic information:\n");
188 for (i = 0; i < capability_basic_information->len; i++) {
189 QmiMessagePbmGetAllCapabilitiesOutputCapabilityBasicInformationElement *session;
190
191 session = &g_array_index (capability_basic_information,
192 QmiMessagePbmGetAllCapabilitiesOutputCapabilityBasicInformationElement,
193 i);
194 g_print ("\t[%s]:\n", qmi_pbm_session_type_get_string (session->session_type));
195 for (j = 0; j < session->phonebooks->len; j++) {
196 QmiMessagePbmGetAllCapabilitiesOutputCapabilityBasicInformationElementPhonebooksElement *phonebook;
197 g_autofree gchar *phonebook_type_str = NULL;
198
199 phonebook = &g_array_index (session->phonebooks,
200 QmiMessagePbmGetAllCapabilitiesOutputCapabilityBasicInformationElementPhonebooksElement,
201 j);
202 phonebook_type_str = qmi_pbm_phonebook_type_build_string_from_mask (phonebook->phonebook_type);
203 g_print ("\t\t[%s]:\n", VALIDATE_MASK_NONE (phonebook_type_str));
204 g_print ("\t\t\tUsed records: %" G_GUINT16_FORMAT "\n", phonebook->used_records);
205 g_print ("\t\t\tMaximum records: %" G_GUINT16_FORMAT "\n", phonebook->maximum_records);
206 g_print ("\t\t\tMaximum number length: %u\n", phonebook->maximum_number_length);
207 g_print ("\t\t\tMaximum name length: %u\n", phonebook->maximum_name_length);
208 }
209 }
210 }
211
212 if (group_capability) {
213 g_print ("Group capability:\n");
214 for (i = 0; i < group_capability->len; i++) {
215 QmiMessagePbmGetAllCapabilitiesOutputGroupCapabilityElement *session;
216
217 session = &g_array_index (group_capability,
218 QmiMessagePbmGetAllCapabilitiesOutputGroupCapabilityElement,
219 i);
220 g_print ("\t[%s]:\n", qmi_pbm_session_type_get_string (session->session_type));
221 g_print ("\t\tMaximum groups: %u\n", session->maximum_groups);
222 g_print ("\t\tMaximum group tag length: %u\n", session->maximum_group_tag_length);
223 }
224 }
225
226 if (additional_number_capability) {
227 g_print ("Additional number capability:\n");
228 for (i = 0; i < additional_number_capability->len; i++) {
229 QmiMessagePbmGetAllCapabilitiesOutputAdditionalNumberCapabilityElement *session;
230
231 session = &g_array_index (additional_number_capability,
232 QmiMessagePbmGetAllCapabilitiesOutputAdditionalNumberCapabilityElement,
233 i);
234 g_print ("\t[%s]:\n", qmi_pbm_session_type_get_string (session->session_type));
235 g_print ("\t\tMaximum additional numbers: %u\n", session->maximum_additional_numbers);
236 g_print ("\t\tMaximum additional number length: %u\n", session->maximum_additional_number_length);
237 g_print ("\t\tMaximum additional number tag length: %u\n", session->maximum_additional_number_tag_length);
238 }
239 }
240
241 if (email_capability) {
242 g_print ("Email capability:\n");
243 for (i = 0; i < email_capability->len; i++) {
244 QmiMessagePbmGetAllCapabilitiesOutputEmailCapabilityElement *session;
245
246 session = &g_array_index (email_capability,
247 QmiMessagePbmGetAllCapabilitiesOutputEmailCapabilityElement,
248 i);
249 g_print ("\t[%s]:\n", qmi_pbm_session_type_get_string (session->session_type));
250 g_print ("\t\tMaximum emails: %u\n", session->maximum_emails);
251 g_print ("\t\tMaximum email address length: %u\n", session->maximum_email_address_length);
252 }
253 }
254
255 if (second_name_capability) {
256 g_print ("Second name capability:\n");
257 for (i = 0; i < second_name_capability->len; i++) {
258 QmiMessagePbmGetAllCapabilitiesOutputSecondNameCapabilityElement *session;
259
260 session = &g_array_index (second_name_capability,
261 QmiMessagePbmGetAllCapabilitiesOutputSecondNameCapabilityElement,
262 i);
263 g_print ("\t[%s]:\n", qmi_pbm_session_type_get_string (session->session_type));
264 g_print ("\t\tMaximum second name length: %u\n", session->maximum_second_name_length);
265 }
266 }
267
268 if (hidden_records_capability) {
269 g_print ("Hidden records capability:\n");
270 for (i = 0; i < hidden_records_capability->len; i++) {
271 QmiMessagePbmGetAllCapabilitiesOutputHiddenRecordsCapabilityElement *session;
272
273 session = &g_array_index (hidden_records_capability,
274 QmiMessagePbmGetAllCapabilitiesOutputHiddenRecordsCapabilityElement,
275 i);
276 g_print ("\t[%s]:\n", qmi_pbm_session_type_get_string (session->session_type));
277 g_print ("\t\tSupported: %s\n", session->supported ? "yes" : "no");
278 }
279 }
280
281 if (grouping_information_alpha_string_capability) {
282 g_print ("Alpha string capability:\n");
283 for (i = 0; i < grouping_information_alpha_string_capability->len; i++) {
284 QmiMessagePbmGetAllCapabilitiesOutputGroupingInformationAlphaStringCapabilityElement *session;
285
286 session = &g_array_index (grouping_information_alpha_string_capability,
287 QmiMessagePbmGetAllCapabilitiesOutputGroupingInformationAlphaStringCapabilityElement,
288 i);
289 g_print ("\t[%s]:\n", qmi_pbm_session_type_get_string (session->session_type));
290 g_print ("\t\tMaximum records: %u\n", session->maximum_records);
291 g_print ("\t\tUsed records: %u\n", session->used_records);
292 g_print ("\t\tMaximum string length: %u\n", session->maximum_string_length);
293 }
294 }
295
296 if (additional_number_alpha_string_capability) {
297 g_print ("Additional number alpha string capability:\n");
298 for (i = 0; i < additional_number_alpha_string_capability->len; i++) {
299 QmiMessagePbmGetAllCapabilitiesOutputAdditionalNumberAlphaStringCapabilityElement *session;
300
301 session = &g_array_index (additional_number_alpha_string_capability,
302 QmiMessagePbmGetAllCapabilitiesOutputAdditionalNumberAlphaStringCapabilityElement,
303 i);
304 g_print ("\t[%s]:\n", qmi_pbm_session_type_get_string (session->session_type));
305 g_print ("\t\tMaximum records: %u\n", session->maximum_records);
306 g_print ("\t\tUsed records: %u\n", session->used_records);
307 g_print ("\t\tMaximum string length: %u\n", session->maximum_string_length);
308 }
309 }
310
311 qmi_message_pbm_get_all_capabilities_output_unref (output);
312 operation_shutdown (TRUE);
313 }
314
315 #endif /* HAVE_QMI_MESSAGE_PBM_GET_ALL_CAPABILITIES */
316
317 #if defined HAVE_QMI_MESSAGE_PBM_GET_EMERGENCY_LIST
318
319 static void
320 get_emergency_list_ready (QmiClientPbm *client,
321 GAsyncResult *res)
322 {
323 GError *error = NULL;
324 QmiMessagePbmGetEmergencyListOutput *output;
325 GArray *hardcoded_numbers = NULL;
326 GArray *nv_numbers = NULL;
327 GArray *card_numbers = NULL;
328 GArray *network_numbers = NULL;
329 guint i, j;
330
331 output = qmi_client_pbm_get_emergency_list_finish (client, res, &error);
332 if (!output) {
333 g_printerr ("error: operation failed: %s\n",
334 error->message);
335 g_error_free (error);
336 operation_shutdown (FALSE);
337 return;
338 }
339
340 if (!qmi_message_pbm_get_emergency_list_output_get_result (output, &error)) {
341 g_printerr ("error: couldn't get emergency list: %s\n", error->message);
342 g_error_free (error);
343 qmi_message_pbm_get_emergency_list_output_unref (output);
344 operation_shutdown (FALSE);
345 return;
346 }
347
348 qmi_message_pbm_get_emergency_list_output_get_hardcoded_numbers (output, &hardcoded_numbers, NULL);
349 qmi_message_pbm_get_emergency_list_output_get_nv_numbers (output, &nv_numbers, NULL);
350 qmi_message_pbm_get_emergency_list_output_get_card_numbers (output, &card_numbers, NULL);
351 qmi_message_pbm_get_emergency_list_output_get_network_numbers (output, &network_numbers, NULL);
352
353 g_print ("[%s] Emergency List:%s\n",
354 qmi_device_get_path_display (ctx->device),
355 (hardcoded_numbers ||
356 nv_numbers ||
357 card_numbers ||
358 network_numbers) ? "" : " none");
359
360 if (hardcoded_numbers) {
361 g_print ("Hardcoded Numbers:\n");
362 for (i = 0; i < hardcoded_numbers->len; i++) {
363 g_print ("\t%s\n", g_array_index (hardcoded_numbers, const gchar *, i));
364 }
365 }
366
367 if (nv_numbers) {
368 g_print ("NV Numbers:\n");
369 for (i = 0; i < nv_numbers->len; i++) {
370 g_print ("\t%s\n", g_array_index (nv_numbers, const gchar *, i));
371 }
372 }
373
374 if (card_numbers) {
375 g_print ("Card Numbers:\n");
376 for (i = 0; i < card_numbers->len; i++) {
377 QmiMessagePbmGetEmergencyListOutputCardNumbersElement *session;
378
379 session = &g_array_index (card_numbers,
380 QmiMessagePbmGetEmergencyListOutputCardNumbersElement,
381 i);
382 g_print ("\t[%s]:\n", qmi_pbm_session_type_get_string (session->session_type));
383 for (j = 0; j < session->emergency_numbers->len; j++) {
384 QmiMessagePbmGetEmergencyListOutputCardNumbersElementEmergencyNumbersElement *numbers;
385 g_autofree gchar *flags_str = NULL;
386
387 numbers = &g_array_index (session->emergency_numbers,
388 QmiMessagePbmGetEmergencyListOutputCardNumbersElementEmergencyNumbersElement,
389 j);
390 flags_str = qmi_pbm_emergency_number_flags_build_string_from_mask (numbers->flags);
391 g_print ("\t\t%s%s%s%s\n",
392 numbers->emergency_number,
393 numbers->flags ? " [" : "",
394 numbers->flags ? VALIDATE_MASK_NONE (flags_str) : "",
395 numbers->flags ? "]" : "");
396 }
397 }
398 }
399
400 if (network_numbers) {
401 g_print ("Network Numbers:\n");
402 for (i = 0; i < network_numbers->len; i++) {
403 QmiMessagePbmGetEmergencyListOutputNetworkNumbersElement *session;
404
405 session = &g_array_index (network_numbers,
406 QmiMessagePbmGetEmergencyListOutputNetworkNumbersElement,
407 i);
408 g_print ("\t[%s]:\n", qmi_pbm_session_type_get_string (session->session_type));
409 for (j = 0; j < session->emergency_numbers->len; j++) {
410 QmiMessagePbmGetEmergencyListOutputNetworkNumbersElementEmergencyNumbersElement *numbers;
411 g_autofree gchar *flags_str = NULL;
412
413 numbers = &g_array_index (session->emergency_numbers,
414 QmiMessagePbmGetEmergencyListOutputNetworkNumbersElementEmergencyNumbersElement,
415 j);
416 flags_str = qmi_pbm_emergency_number_flags_build_string_from_mask (numbers->flags);
417 g_print ("\t\t%s%s%s%s\n",
418 numbers->emergency_number,
419 numbers->flags ? " [" : "",
420 numbers->flags ? VALIDATE_MASK_NONE (flags_str) : "",
421 numbers->flags ? "]" : "");
422 }
423 }
424 }
425
426 qmi_message_pbm_get_emergency_list_output_unref (output);
427 operation_shutdown (TRUE);
428 }
429
430 #endif /* HAVE_QMI_MESSAGE_PBM_GET_EMERGENCY_LIST */
431
432 static gboolean
433 noop_cb (gpointer unused)
434 {
435 operation_shutdown (TRUE);
436 return FALSE;
437 }
438
439 void
440 qmicli_pbm_run (QmiDevice *device,
441 QmiClientPbm *client,
442 GCancellable *cancellable)
443 {
444 /* Initialize context */
445 ctx = g_slice_new (Context);
446 ctx->device = g_object_ref (device);
447 ctx->client = g_object_ref (client);
448 ctx->cancellable = g_object_ref (cancellable);
449
450 #if defined HAVE_QMI_MESSAGE_PBM_GET_ALL_CAPABILITIES
451 if (get_all_capabilities_flag) {
452 g_debug ("Asynchronously getting phonebook capabilities...");
453 qmi_client_pbm_get_all_capabilities (ctx->client,
454 NULL,
455 10,
456 ctx->cancellable,
457 (GAsyncReadyCallback)get_all_capabilities_ready,
458 NULL);
459 return;
460 }
461 #endif
462
463 #if defined HAVE_QMI_MESSAGE_PBM_GET_EMERGENCY_LIST
464 if (get_emergency_list_flag) {
465 g_debug ("Asynchronously getting phonebook emergency list...");
466 qmi_client_pbm_get_emergency_list (ctx->client,
467 NULL,
468 10,
469 ctx->cancellable,
470 (GAsyncReadyCallback)get_emergency_list_ready,
471 NULL);
472 return;
473 }
474 #endif
475
476 /* Just client allocate/release? */
477 if (noop_flag) {
478 g_idle_add (noop_cb, NULL);
479 return;
480 }
481
482 g_warn_if_reached ();
483 }
484
485 #endif /* HAVE_QMI_SERVICE_PBM */