| 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) 2022 Ulrich Mohr |
| 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 gchar *delete_str = NULL; |
| 34 |
static gchar *read_str = NULL; |
| 35 |
static gboolean query_config_flag; |
| 36 |
static gboolean query_message_store_status_flag; |
| 37 |
|
| 38 |
static GOptionEntry entries[] = { |
| 39 |
{ "sms-query-configuration", 0, 0, G_OPTION_ARG_NONE, &query_config_flag, |
| 40 |
"Query SMS configuration", |
| 41 |
NULL |
| 42 |
}, |
| 43 |
{ "sms-query-message-store-status", 0, 0, G_OPTION_ARG_NONE, &query_message_store_status_flag, |
| 44 |
"Query SMS message store status", |
| 45 |
NULL |
| 46 |
}, |
| 47 |
{ "sms-delete", 0, 0, G_OPTION_ARG_STRING, &delete_str, |
| 48 |
"Delete all SMS matching a given filter", |
| 49 |
"[(all|new|old|sent|draft|index=N)]" |
| 50 |
}, |
| 51 |
{ "sms-read", 0, 0, G_OPTION_ARG_STRING, &read_str, |
| 52 |
"Read all SMS matching a given filter", |
| 53 |
"[(all|new|old|sent|draft|index=N)]" |
| 54 |
}, |
| 55 |
{ NULL } |
| 56 |
}; |
| 57 |
|
| 58 |
GOptionGroup * |
| 59 |
mbimcli_sms_get_option_group (void) |
| 60 |
{ |
| 61 |
GOptionGroup *group; |
| 62 |
|
| 63 |
group = g_option_group_new ("sms", |
| 64 |
"Simple message service options:", |
| 65 |
"Show SMS service options", |
| 66 |
NULL, |
| 67 |
NULL); |
| 68 |
g_option_group_add_entries (group, entries); |
| 69 |
|
| 70 |
return group; |
| 71 |
} |
| 72 |
|
| 73 |
gboolean |
| 74 |
mbimcli_sms_options_enabled (void) |
| 75 |
{ |
| 76 |
static guint n_actions = 0; |
| 77 |
static gboolean checked = FALSE; |
| 78 |
|
| 79 |
if (checked) |
| 80 |
return !!n_actions; |
| 81 |
|
| 82 |
n_actions = (!!delete_str + |
| 83 |
!!read_str + |
| 84 |
query_config_flag + |
| 85 |
query_message_store_status_flag); |
| 86 |
if (n_actions > 1) { |
| 87 |
g_printerr ("error: too many SIM actions requested\n"); |
| 88 |
exit (EXIT_FAILURE); |
| 89 |
} |
| 90 |
checked = TRUE; |
| 91 |
|
| 92 |
return !!n_actions; |
| 93 |
} |
| 94 |
|
| 95 |
static void |
| 96 |
context_free (Context *context) |
| 97 |
{ |
| 98 |
if (!context) |
| 99 |
return; |
| 100 |
|
| 101 |
if (context->cancellable) |
| 102 |
g_object_unref (context->cancellable); |
| 103 |
if (context->device) |
| 104 |
g_object_unref (context->device); |
| 105 |
g_slice_free (Context, context); |
| 106 |
} |
| 107 |
|
| 108 |
static void |
| 109 |
shutdown (gboolean operation_status) |
| 110 |
{ |
| 111 |
/* Cleanup context and finish async operation */ |
| 112 |
context_free (ctx); |
| 113 |
mbimcli_async_operation_done (operation_status); |
| 114 |
} |
| 115 |
|
| 116 |
static void |
| 117 |
delete_sms_ready (MbimDevice *device, |
| 118 |
GAsyncResult *res, |
| 119 |
gpointer user_data) |
| 120 |
{ |
| 121 |
g_autoptr(MbimMessage) response = NULL; |
| 122 |
g_autoptr(GError) error = NULL; |
| 123 |
MbimSmsFlag filter = GPOINTER_TO_INT (user_data); |
| 124 |
|
| 125 |
response = mbim_device_command_finish (device, res, &error); |
| 126 |
if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) { |
| 127 |
g_printerr ("error: operation failed: %s\n", error->message); |
| 128 |
shutdown (FALSE); |
| 129 |
return; |
| 130 |
} |
| 131 |
|
| 132 |
if (!mbim_message_sms_delete_response_parse (response, &error)) { |
| 133 |
g_printerr ("error: couldn't parse response message: %s\n", error->message); |
| 134 |
shutdown (FALSE); |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
if (filter != MBIM_SMS_FLAG_INDEX) |
| 139 |
g_print ("Successfully deleted %s sms\n", mbim_sms_flag_get_string (filter)); |
| 140 |
else |
| 141 |
g_print ("Successfully deleted sms\n"); |
| 142 |
|
| 143 |
shutdown (TRUE); |
| 144 |
return; |
| 145 |
} |
| 146 |
|
| 147 |
static void |
| 148 |
read_sms_ready (MbimDevice *device, |
| 149 |
GAsyncResult *res, |
| 150 |
gpointer user_data) |
| 151 |
{ |
| 152 |
g_autoptr(MbimMessage) response = NULL; |
| 153 |
g_autoptr(GError) error = NULL; |
| 154 |
g_autoptr(MbimSmsPduReadRecordArray) pdu_messages = NULL; |
| 155 |
guint32 num_messages = 0; |
| 156 |
MbimSmsFlag filter = GPOINTER_TO_INT (user_data); |
| 157 |
|
| 158 |
response = mbim_device_command_finish (device, res, &error); |
| 159 |
if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) { |
| 160 |
g_printerr ("error: operation failed: %s\n", error->message); |
| 161 |
shutdown (FALSE); |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
if (!mbim_message_sms_read_response_parse (response, NULL, &num_messages, &pdu_messages, NULL, &error)) { |
| 166 |
g_printerr ("error: couldn't parse response message: %s\n", error->message); |
| 167 |
shutdown (FALSE); |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
if (num_messages && pdu_messages) { |
| 172 |
guint32 i = 0; |
| 173 |
|
| 174 |
g_print ("Successfully read sms\n"); |
| 175 |
if (filter != MBIM_SMS_FLAG_INDEX) |
| 176 |
g_print ("Got %d messages\n", num_messages); |
| 177 |
while (pdu_messages[i]) { |
| 178 |
g_print (" PDU on index %u, status %s\n", pdu_messages[i]->message_index, mbim_sms_status_get_string (pdu_messages[i]->message_status)); |
| 179 |
i++; |
| 180 |
} |
| 181 |
} else { |
| 182 |
if (filter == MBIM_SMS_FLAG_ALL) |
| 183 |
g_print ("No messages found\n"); |
| 184 |
else if (filter != MBIM_SMS_FLAG_INDEX) |
| 185 |
g_print ("No %s messages found\n", mbim_sms_flag_get_string (filter) ); |
| 186 |
else |
| 187 |
g_print ("Message not found\n"); |
| 188 |
} |
| 189 |
|
| 190 |
shutdown (TRUE); |
| 191 |
return; |
| 192 |
} |
| 193 |
|
| 194 |
static void |
| 195 |
query_sms_config_ready (MbimDevice *device, |
| 196 |
GAsyncResult *res, |
| 197 |
gpointer user_data) |
| 198 |
{ |
| 199 |
g_autoptr(MbimMessage) response = NULL; |
| 200 |
g_autoptr(GError) error = NULL; |
| 201 |
MbimSmsStorageState storage_state = MBIM_SMS_STORAGE_STATE_NOT_INITIALIZED; |
| 202 |
const gchar *storage_state_str; |
| 203 |
MbimSmsFormat format = MBIM_SMS_FORMAT_PDU; |
| 204 |
const gchar *format_str; |
| 205 |
guint32 max_messages = 0; |
| 206 |
guint32 cdma_short_message_size = 0; |
| 207 |
g_autofree gchar *sc_address = NULL; |
| 208 |
|
| 209 |
response = mbim_device_command_finish (device, res, &error); |
| 210 |
if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) { |
| 211 |
g_printerr ("error: operation failed: %s\n", error->message); |
| 212 |
shutdown (FALSE); |
| 213 |
return; |
| 214 |
} |
| 215 |
|
| 216 |
if (!mbim_message_sms_configuration_response_parse (response, |
| 217 |
&storage_state, |
| 218 |
&format, |
| 219 |
&max_messages, |
| 220 |
&cdma_short_message_size, |
| 221 |
&sc_address, |
| 222 |
&error)) { |
| 223 |
g_printerr ("error: couldn't parse response message: %s\n", error->message); |
| 224 |
shutdown (FALSE); |
| 225 |
return; |
| 226 |
} |
| 227 |
|
| 228 |
storage_state_str = mbim_sms_storage_state_get_string (storage_state); |
| 229 |
format_str = mbim_sms_format_get_string (format); |
| 230 |
|
| 231 |
g_print ("[%s] SMS capabilities retrieved:\n" |
| 232 |
"\t Storage state: '%s'\n" |
| 233 |
"\t Format: '%s'\n" |
| 234 |
"\t Max PDU messages: '%u'\n" |
| 235 |
"\tCDMA short message size: '%u'\n" |
| 236 |
"\t Service center address: '%s'\n", |
| 237 |
mbim_device_get_path_display (device), |
| 238 |
VALIDATE_UNKNOWN (storage_state_str), |
| 239 |
VALIDATE_UNKNOWN (format_str), |
| 240 |
max_messages, |
| 241 |
cdma_short_message_size, |
| 242 |
sc_address); |
| 243 |
|
| 244 |
shutdown (TRUE); |
| 245 |
return; |
| 246 |
} |
| 247 |
|
| 248 |
static void |
| 249 |
query_sms_message_store_status_ready (MbimDevice *device, |
| 250 |
GAsyncResult *res, |
| 251 |
gpointer user_data) |
| 252 |
{ |
| 253 |
g_autoptr(MbimMessage) response = NULL; |
| 254 |
g_autoptr(GError) error = NULL; |
| 255 |
MbimSmsStatusFlag status = MBIM_SMS_STATUS_FLAG_NONE; |
| 256 |
const gchar *status_str; |
| 257 |
guint message_index = 0; |
| 258 |
|
| 259 |
response = mbim_device_command_finish (device, res, &error); |
| 260 |
if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) { |
| 261 |
g_printerr ("error: operation failed: %s\n", error->message); |
| 262 |
shutdown (FALSE); |
| 263 |
return; |
| 264 |
} |
| 265 |
|
| 266 |
if (!mbim_message_sms_message_store_status_response_parse (response, |
| 267 |
&status, |
| 268 |
&message_index, |
| 269 |
&error)) { |
| 270 |
g_printerr ("error: couldn't parse response message: %s\n", error->message); |
| 271 |
shutdown (FALSE); |
| 272 |
return; |
| 273 |
} |
| 274 |
|
| 275 |
status_str = mbim_sms_status_flag_build_string_from_mask (status); |
| 276 |
|
| 277 |
g_print ("[%s] SMS message store status retrieved:\n" |
| 278 |
"\t Status: '%s'\n" |
| 279 |
"\tMessage index: '%u'\n", |
| 280 |
mbim_device_get_path_display (device), |
| 281 |
VALIDATE_UNKNOWN (status_str), |
| 282 |
message_index); |
| 283 |
|
| 284 |
shutdown (TRUE); |
| 285 |
return; |
| 286 |
} |
| 287 |
|
| 288 |
static gboolean |
| 289 |
op_parse (const gchar *str, |
| 290 |
MbimSmsFlag *filter, |
| 291 |
guint32 *index) |
| 292 |
{ |
| 293 |
gboolean status = FALSE; |
| 294 |
g_auto(GStrv) filter_parts = NULL; |
| 295 |
g_autoptr(GError) error = NULL; |
| 296 |
|
| 297 |
g_assert (filter != NULL); |
| 298 |
|
| 299 |
/* according to the mbim specification, index must be > 0 and 0 is used when not needed */ |
| 300 |
*index = 0; |
| 301 |
|
| 302 |
filter_parts = g_strsplit (str, "=", -1); |
| 303 |
if (!filter_parts[0]) { |
| 304 |
g_printerr ("error: invalid sms filter: %s\n", str); |
| 305 |
return FALSE; |
| 306 |
} |
| 307 |
|
| 308 |
status = mbimcli_read_sms_flag_from_string (filter_parts[0], filter, &error); |
| 309 |
if (!status) { |
| 310 |
g_printerr ("error: invalid sms flag: %s\n", error->message); |
| 311 |
return FALSE; |
| 312 |
} |
| 313 |
|
| 314 |
if (*filter == MBIM_SMS_FLAG_INDEX) { |
| 315 |
status = filter_parts[1] && mbimcli_read_uint_from_string (filter_parts[1], index); |
| 316 |
if (!status) { |
| 317 |
if (!filter_parts[1]) |
| 318 |
g_printerr ("error: required index not given\n"); |
| 319 |
else |
| 320 |
g_printerr ("error: couln't parse sms index, should be a number\n"); |
| 321 |
return FALSE; |
| 322 |
} |
| 323 |
/* status must be TRUE; validate index */ |
| 324 |
if (*index == 0) { |
| 325 |
g_printerr ("error: index must be > 0\n"); |
| 326 |
return FALSE; |
| 327 |
} |
| 328 |
} else if (filter_parts[1]) { |
| 329 |
g_printerr ("error: unexpected assignment for the given operation\n"); |
| 330 |
return FALSE; |
| 331 |
} |
| 332 |
|
| 333 |
return status; |
| 334 |
} |
| 335 |
|
| 336 |
void |
| 337 |
mbimcli_sms_run (MbimDevice *device, |
| 338 |
GCancellable *cancellable) |
| 339 |
{ |
| 340 |
g_autoptr(MbimMessage) request = NULL; |
| 341 |
g_autoptr(GError) error = NULL; |
| 342 |
|
| 343 |
/* Initialize context */ |
| 344 |
ctx = g_slice_new (Context); |
| 345 |
ctx->device = g_object_ref (device); |
| 346 |
ctx->cancellable = cancellable ? g_object_ref (cancellable) : NULL; |
| 347 |
|
| 348 |
if (delete_str) { |
| 349 |
MbimSmsFlag filter; |
| 350 |
guint32 index; |
| 351 |
|
| 352 |
if (!op_parse (delete_str, &filter, &index)) { |
| 353 |
shutdown (FALSE); |
| 354 |
return; |
| 355 |
} |
| 356 |
request = mbim_message_sms_delete_set_new (filter, index, &error); |
| 357 |
if (!request) { |
| 358 |
g_printerr ("error: couldn't create request: %s\n", error->message); |
| 359 |
shutdown (FALSE); |
| 360 |
return; |
| 361 |
} |
| 362 |
mbim_device_command (ctx->device, |
| 363 |
request, |
| 364 |
10, |
| 365 |
ctx->cancellable, |
| 366 |
(GAsyncReadyCallback)delete_sms_ready, |
| 367 |
GINT_TO_POINTER (filter)); |
| 368 |
return; |
| 369 |
} |
| 370 |
|
| 371 |
if (read_str) { |
| 372 |
MbimSmsFlag filter; |
| 373 |
guint32 index; |
| 374 |
|
| 375 |
if (!op_parse (read_str, &filter, &index)) { |
| 376 |
shutdown (FALSE); |
| 377 |
return; |
| 378 |
} |
| 379 |
request = mbim_message_sms_read_query_new (MBIM_SMS_FORMAT_PDU, filter, index, &error); |
| 380 |
if (!request) { |
| 381 |
g_printerr ("error: couldn't create request: %s\n", error->message); |
| 382 |
shutdown (FALSE); |
| 383 |
return; |
| 384 |
} |
| 385 |
mbim_device_command (ctx->device, |
| 386 |
request, |
| 387 |
10, |
| 388 |
ctx->cancellable, |
| 389 |
(GAsyncReadyCallback)read_sms_ready, |
| 390 |
GINT_TO_POINTER (filter)); |
| 391 |
return; |
| 392 |
} |
| 393 |
|
| 394 |
if (query_config_flag) { |
| 395 |
request = mbim_message_sms_configuration_query_new (&error); |
| 396 |
if (!request) { |
| 397 |
g_printerr ("error: couldn't create request: %s\n", error->message); |
| 398 |
shutdown (FALSE); |
| 399 |
return; |
| 400 |
} |
| 401 |
mbim_device_command (ctx->device, |
| 402 |
request, |
| 403 |
10, |
| 404 |
ctx->cancellable, |
| 405 |
(GAsyncReadyCallback)query_sms_config_ready, |
| 406 |
NULL); |
| 407 |
return; |
| 408 |
} |
| 409 |
|
| 410 |
if (query_message_store_status_flag) { |
| 411 |
request = mbim_message_sms_message_store_status_query_new (&error); |
| 412 |
if (!request) { |
| 413 |
g_printerr ("error: couldn't create request: %s\n", error->message); |
| 414 |
shutdown (FALSE); |
| 415 |
return; |
| 416 |
} |
| 417 |
mbim_device_command (ctx->device, |
| 418 |
request, |
| 419 |
10, |
| 420 |
ctx->cancellable, |
| 421 |
(GAsyncReadyCallback)query_sms_message_store_status_ready, |
| 422 |
NULL); |
| 423 |
return; |
| 424 |
} |
| 425 |
|
| 426 |
g_warn_if_reached (); |
| 427 |
} |