| 1 |
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 3 |
/* |
| 4 |
* mbimcli -- Command line interface to control MBIM devices |
| 5 |
* |
| 6 |
* Copyright (C) 2013 - 2014 Nagaraju Kadiri <[email protected]> |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "config.h" |
| 10 |
|
| 11 |
#include <stdio.h> |
| 12 |
#include <stdlib.h> |
| 13 |
#include <locale.h> |
| 14 |
#include <string.h> |
| 15 |
#include <errno.h> |
| 16 |
|
| 17 |
#include <glib.h> |
| 18 |
#include <gio/gio.h> |
| 19 |
|
| 20 |
#include <libmbim-glib.h> |
| 21 |
|
| 22 |
#include "mbimcli.h" |
| 23 |
#include "mbimcli-helpers.h" |
| 24 |
|
| 25 |
/* Context */ |
| 26 |
typedef struct { |
| 27 |
MbimDevice *device; |
| 28 |
GCancellable *cancellable; |
| 29 |
} Context; |
| 30 |
static Context *ctx; |
| 31 |
|
| 32 |
/* Options */ |
| 33 |
static gboolean phonebook_configuration_flag; |
| 34 |
static gint phonebook_read_index; |
| 35 |
static gboolean phonebook_read_all_flag; |
| 36 |
static gchar *phonebook_write_str; |
| 37 |
static gint phonebook_delete_index; |
| 38 |
static gboolean phonebook_delete_all_flag; |
| 39 |
|
| 40 |
static GOptionEntry entries[] = { |
| 41 |
{ "phonebook-query-configuration", 0, 0, G_OPTION_ARG_NONE, &phonebook_configuration_flag, |
| 42 |
"Query the phonebook configuration", |
| 43 |
NULL |
| 44 |
}, |
| 45 |
{ "phonebook-read", 0, 0, G_OPTION_ARG_INT, &phonebook_read_index, |
| 46 |
"Read phonebook entry with given index", |
| 47 |
"[(Phonebook index)]" |
| 48 |
}, |
| 49 |
{ "phonebook-read-all", 0, 0, G_OPTION_ARG_NONE, &phonebook_read_all_flag, |
| 50 |
"Read all phonebook entries", |
| 51 |
NULL |
| 52 |
}, |
| 53 |
{ "phonebook-write", 0, 0, G_OPTION_ARG_STRING, &phonebook_write_str, |
| 54 |
"Add new phonebook entry or update an existing one", |
| 55 |
"[(Name),(Number)[,(Index)]]" |
| 56 |
}, |
| 57 |
{ "phonebook-delete", 0, 0, G_OPTION_ARG_INT, &phonebook_delete_index, |
| 58 |
"Delete phonebook entry with given index", |
| 59 |
"[(Phonebook index)]" |
| 60 |
}, |
| 61 |
{ "phonebook-delete-all", 0, 0, G_OPTION_ARG_NONE, &phonebook_delete_all_flag, |
| 62 |
"Delete all phonebook entries", |
| 63 |
NULL |
| 64 |
}, |
| 65 |
{ NULL, 0, 0, 0, NULL, NULL, NULL } |
| 66 |
}; |
| 67 |
|
| 68 |
GOptionGroup * |
| 69 |
mbimcli_phonebook_get_option_group (void) |
| 70 |
{ |
| 71 |
GOptionGroup *group; |
| 72 |
|
| 73 |
group = g_option_group_new ("phonebook", |
| 74 |
"Phonebook options:", |
| 75 |
"Show Phonebook Service options", |
| 76 |
NULL, |
| 77 |
NULL); |
| 78 |
g_option_group_add_entries (group, entries); |
| 79 |
|
| 80 |
return group; |
| 81 |
} |
| 82 |
|
| 83 |
gboolean |
| 84 |
mbimcli_phonebook_options_enabled (void) |
| 85 |
{ |
| 86 |
static guint n_actions = 0; |
| 87 |
static gboolean checked = FALSE; |
| 88 |
|
| 89 |
if (checked) |
| 90 |
return !!n_actions; |
| 91 |
|
| 92 |
n_actions = (phonebook_configuration_flag + |
| 93 |
!!phonebook_read_index + |
| 94 |
phonebook_read_all_flag + |
| 95 |
!!phonebook_write_str + |
| 96 |
!!phonebook_delete_index + |
| 97 |
phonebook_delete_all_flag); |
| 98 |
|
| 99 |
if (n_actions > 1) { |
| 100 |
g_printerr ("error: too many phonebook actions requested\n"); |
| 101 |
exit (EXIT_FAILURE); |
| 102 |
} |
| 103 |
|
| 104 |
checked = TRUE; |
| 105 |
return !!n_actions; |
| 106 |
} |
| 107 |
|
| 108 |
static void |
| 109 |
context_free (Context *context) |
| 110 |
{ |
| 111 |
if (!context) |
| 112 |
return; |
| 113 |
|
| 114 |
if (context->cancellable) |
| 115 |
g_object_unref (context->cancellable); |
| 116 |
if (context->device) |
| 117 |
g_object_unref (context->device); |
| 118 |
g_slice_free (Context, context); |
| 119 |
} |
| 120 |
|
| 121 |
static void |
| 122 |
shutdown (gboolean operation_status) |
| 123 |
{ |
| 124 |
/* Cleanup context and finish async operation */ |
| 125 |
context_free (ctx); |
| 126 |
mbimcli_async_operation_done (operation_status); |
| 127 |
} |
| 128 |
|
| 129 |
static gboolean |
| 130 |
phonebook_write_input_parse (const gchar *str, |
| 131 |
gchar **name, |
| 132 |
gchar **number, |
| 133 |
guint *idx) |
| 134 |
{ |
| 135 |
g_auto(GStrv) split = NULL; |
| 136 |
|
| 137 |
g_assert (name != NULL); |
| 138 |
g_assert (number != NULL); |
| 139 |
g_assert (idx != NULL); |
| 140 |
|
| 141 |
/* Format of the string is: |
| 142 |
* "[(Name),(Number)(,[Index])]" |
| 143 |
* i.e. index is optional |
| 144 |
*/ |
| 145 |
split = g_strsplit (str, ",", -1); |
| 146 |
|
| 147 |
if (g_strv_length (split) > 3) { |
| 148 |
g_printerr ("error: couldn't parse input string, too many arguments\n"); |
| 149 |
return FALSE; |
| 150 |
} |
| 151 |
|
| 152 |
if (g_strv_length (split) < 2) { |
| 153 |
g_printerr ("error: couldn't parse input string, missing arguments\n"); |
| 154 |
return FALSE; |
| 155 |
} |
| 156 |
|
| 157 |
/* Check whether we have the optional Index item */ |
| 158 |
if (split[2]) { |
| 159 |
if (!mbimcli_read_uint_from_string (split[2], idx)) { |
| 160 |
g_printerr ("error: couldn't parse input string, invalid index '%s'\n", split[2]); |
| 161 |
return FALSE; |
| 162 |
} |
| 163 |
} else { |
| 164 |
/* Default to index 0, which is an invalid one */ |
| 165 |
*idx = 0; |
| 166 |
} |
| 167 |
|
| 168 |
/* First two items will always be available */ |
| 169 |
*name = g_strdup (split[0]); |
| 170 |
*number = g_strdup (split[1]); |
| 171 |
return TRUE; |
| 172 |
} |
| 173 |
|
| 174 |
static void |
| 175 |
set_phonebook_write_ready (MbimDevice *device, |
| 176 |
GAsyncResult *res) |
| 177 |
{ |
| 178 |
g_autoptr(MbimMessage) response = NULL; |
| 179 |
g_autoptr(GError) error = NULL; |
| 180 |
|
| 181 |
response = mbim_device_command_finish (device, res, &error); |
| 182 |
if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) { |
| 183 |
g_printerr ("error: operation failed: %s\n", error->message); |
| 184 |
shutdown (FALSE); |
| 185 |
return; |
| 186 |
} |
| 187 |
|
| 188 |
if (!mbim_message_phonebook_write_response_parse (response, &error)) { |
| 189 |
g_printerr ("error: couldn't parse response message: %s\n", error->message); |
| 190 |
shutdown (FALSE); |
| 191 |
return; |
| 192 |
} |
| 193 |
|
| 194 |
g_print ("Phonebook entry successfully written\n"); |
| 195 |
|
| 196 |
shutdown (TRUE); |
| 197 |
} |
| 198 |
|
| 199 |
static void |
| 200 |
set_phonebook_delete_ready (MbimDevice *device, |
| 201 |
GAsyncResult *res) |
| 202 |
{ |
| 203 |
g_autoptr(MbimMessage) response = NULL; |
| 204 |
g_autoptr(GError) error = NULL; |
| 205 |
|
| 206 |
response = mbim_device_command_finish (device, res, &error); |
| 207 |
if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) { |
| 208 |
g_printerr ("error: operation failed: %s\n", error->message); |
| 209 |
shutdown (FALSE); |
| 210 |
return; |
| 211 |
} |
| 212 |
|
| 213 |
if (!mbim_message_phonebook_delete_response_parse (response, &error)) { |
| 214 |
g_printerr ("error: couldn't parse response message: %s\n", error->message); |
| 215 |
shutdown (FALSE); |
| 216 |
return; |
| 217 |
} |
| 218 |
|
| 219 |
g_print ("Phonebook entry/entries successfully deleted"); |
| 220 |
|
| 221 |
shutdown (TRUE); |
| 222 |
} |
| 223 |
|
| 224 |
static void |
| 225 |
query_phonebook_read_ready (MbimDevice *device, |
| 226 |
GAsyncResult *res) |
| 227 |
{ |
| 228 |
g_autoptr(MbimMessage) response = NULL; |
| 229 |
g_autoptr(GError) error = NULL; |
| 230 |
g_autoptr(MbimPhonebookEntryArray) phonebook_entries = NULL; |
| 231 |
guint32 entry_count; |
| 232 |
guint i; |
| 233 |
|
| 234 |
response = mbim_device_command_finish (device, res, &error); |
| 235 |
if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) { |
| 236 |
g_printerr ("error: operation failed: %s\n", error->message); |
| 237 |
shutdown (FALSE); |
| 238 |
return; |
| 239 |
} |
| 240 |
|
| 241 |
if (!mbim_message_phonebook_read_response_parse (response, |
| 242 |
&entry_count, |
| 243 |
&phonebook_entries, |
| 244 |
&error)) { |
| 245 |
g_printerr ("error: couldn't parse response message: %s\n", error->message); |
| 246 |
shutdown (FALSE); |
| 247 |
return; |
| 248 |
} |
| 249 |
|
| 250 |
g_print ("Successfully read phonebook entries (%d)\n", entry_count); |
| 251 |
for (i = 0; i < entry_count; i++) { |
| 252 |
g_print ("\tEntry index : %d \n" |
| 253 |
"\t Number: %s \n" |
| 254 |
"\t Name: %s \n", |
| 255 |
phonebook_entries[i]->entry_index, |
| 256 |
phonebook_entries[i]->number, |
| 257 |
phonebook_entries[i]->name); |
| 258 |
} |
| 259 |
|
| 260 |
shutdown (TRUE); |
| 261 |
} |
| 262 |
|
| 263 |
static void |
| 264 |
query_phonebook_configuration_ready (MbimDevice *device, |
| 265 |
GAsyncResult *res) |
| 266 |
{ |
| 267 |
g_autoptr(MbimMessage) response = NULL; |
| 268 |
g_autoptr(GError) error = NULL; |
| 269 |
MbimPhonebookState state; |
| 270 |
const gchar *state_str; |
| 271 |
guint32 number_of_entries; |
| 272 |
guint32 used_entries; |
| 273 |
guint32 max_number_length; |
| 274 |
guint32 max_name; |
| 275 |
|
| 276 |
response = mbim_device_command_finish (device, res, &error); |
| 277 |
if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) { |
| 278 |
g_printerr ("error: operation failed: %s\n", error->message); |
| 279 |
shutdown (FALSE); |
| 280 |
return; |
| 281 |
} |
| 282 |
|
| 283 |
if (!mbim_message_phonebook_configuration_response_parse (response, |
| 284 |
&state, |
| 285 |
&number_of_entries, |
| 286 |
&used_entries, |
| 287 |
&max_number_length, |
| 288 |
&max_name, |
| 289 |
&error)) { |
| 290 |
g_printerr ("error: couldn't parse response message: %s\n", error->message); |
| 291 |
shutdown (FALSE); |
| 292 |
return; |
| 293 |
} |
| 294 |
|
| 295 |
state_str = mbim_phonebook_state_get_string (state); |
| 296 |
|
| 297 |
g_print ("\n Phonebook configuration retrieved... \n" |
| 298 |
"\t Phonebook state: %s \n" |
| 299 |
"\t Number of entries: %d \n" |
| 300 |
"\t used entries: %d \n" |
| 301 |
"\t max number length: %d \n" |
| 302 |
"\t max name : %d \n", |
| 303 |
VALIDATE_UNKNOWN (state_str), |
| 304 |
number_of_entries, |
| 305 |
used_entries, |
| 306 |
max_number_length, |
| 307 |
max_name); |
| 308 |
|
| 309 |
shutdown (TRUE); |
| 310 |
} |
| 311 |
|
| 312 |
void |
| 313 |
mbimcli_phonebook_run (MbimDevice *device, |
| 314 |
GCancellable *cancellable) |
| 315 |
{ |
| 316 |
g_autoptr(MbimMessage) request = NULL; |
| 317 |
|
| 318 |
/* Initialize context */ |
| 319 |
ctx = g_slice_new (Context); |
| 320 |
ctx->device = g_object_ref (device); |
| 321 |
ctx->cancellable = cancellable ? g_object_ref (cancellable) : NULL; |
| 322 |
|
| 323 |
/* Request to get configuration? */ |
| 324 |
if (phonebook_configuration_flag) { |
| 325 |
g_debug ("Asynchronously querying phonebook configurations..."); |
| 326 |
request = mbim_message_phonebook_configuration_query_new (NULL); |
| 327 |
mbim_device_command (ctx->device, |
| 328 |
request, |
| 329 |
10, |
| 330 |
ctx->cancellable, |
| 331 |
(GAsyncReadyCallback)query_phonebook_configuration_ready, |
| 332 |
NULL); |
| 333 |
return; |
| 334 |
} |
| 335 |
|
| 336 |
/* Phonebook read */ |
| 337 |
if (phonebook_read_index) { |
| 338 |
g_debug ("Asynchronously querying phonebook read..."); |
| 339 |
request = mbim_message_phonebook_read_query_new (MBIM_PHONEBOOK_FLAG_INDEX, |
| 340 |
phonebook_read_index, |
| 341 |
NULL); |
| 342 |
mbim_device_command (ctx->device, |
| 343 |
request, |
| 344 |
10, |
| 345 |
ctx->cancellable, |
| 346 |
(GAsyncReadyCallback)query_phonebook_read_ready, |
| 347 |
NULL); |
| 348 |
return; |
| 349 |
} |
| 350 |
|
| 351 |
/* Phonebook read all */ |
| 352 |
if (phonebook_read_all_flag) { |
| 353 |
g_debug ("Asynchronously querying phonebook read all..."); |
| 354 |
request = mbim_message_phonebook_read_query_new (MBIM_PHONEBOOK_FLAG_ALL, 0, NULL); |
| 355 |
mbim_device_command (ctx->device, |
| 356 |
request, |
| 357 |
10, |
| 358 |
ctx->cancellable, |
| 359 |
(GAsyncReadyCallback)query_phonebook_read_ready, |
| 360 |
NULL); |
| 361 |
return; |
| 362 |
} |
| 363 |
|
| 364 |
/* Phonebook delete */ |
| 365 |
if (phonebook_delete_index) { |
| 366 |
g_debug ("Asynchronously phonebook delete..."); |
| 367 |
request = mbim_message_phonebook_delete_set_new (MBIM_PHONEBOOK_FLAG_INDEX, |
| 368 |
phonebook_delete_index, |
| 369 |
NULL); |
| 370 |
mbim_device_command (ctx->device, |
| 371 |
request, |
| 372 |
10, |
| 373 |
ctx->cancellable, |
| 374 |
(GAsyncReadyCallback)set_phonebook_delete_ready, |
| 375 |
NULL); |
| 376 |
return; |
| 377 |
} |
| 378 |
|
| 379 |
/* Phonebook delete all */ |
| 380 |
if (phonebook_delete_all_flag) { |
| 381 |
g_debug ("Asynchronously phonebook delete all..."); |
| 382 |
request = mbim_message_phonebook_delete_set_new (MBIM_PHONEBOOK_FLAG_ALL, 0, NULL); |
| 383 |
mbim_device_command (ctx->device, |
| 384 |
request, |
| 385 |
10, |
| 386 |
ctx->cancellable, |
| 387 |
(GAsyncReadyCallback)set_phonebook_delete_ready, |
| 388 |
NULL); |
| 389 |
return; |
| 390 |
} |
| 391 |
|
| 392 |
/* Phonebook write */ |
| 393 |
if (phonebook_write_str) { |
| 394 |
g_autofree gchar *name = NULL; |
| 395 |
g_autofree gchar *number = NULL; |
| 396 |
guint idx; |
| 397 |
|
| 398 |
g_debug ("Asynchronously writing phonebook..."); |
| 399 |
if (!phonebook_write_input_parse (phonebook_write_str, &name, &number, &idx)) { |
| 400 |
shutdown (FALSE); |
| 401 |
return; |
| 402 |
} |
| 403 |
|
| 404 |
request = mbim_message_phonebook_write_set_new ((idx ? |
| 405 |
MBIM_PHONEBOOK_WRITE_FLAG_SAVE_INDEX : |
| 406 |
MBIM_PHONEBOOK_WRITE_FLAG_SAVE_UNUSED), |
| 407 |
idx, |
| 408 |
number, |
| 409 |
name, |
| 410 |
NULL); |
| 411 |
mbim_device_command (ctx->device, |
| 412 |
request, |
| 413 |
10, |
| 414 |
ctx->cancellable, |
| 415 |
(GAsyncReadyCallback)set_phonebook_write_ready, |
| 416 |
NULL); |
| 417 |
return; |
| 418 |
} |
| 419 |
|
| 420 |
g_warn_if_reached (); |
| 421 |
} |