From ca8fafef3afbee92bc469ef90a10ed64f8c1cbe9 Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Tue, 28 Nov 2017 14:51:40 +0100 Subject: [PATCH] mbimcli: expose cdma sms reading --- src/mbimcli/Makefile.am | 3 +- src/mbimcli/mbimcli-sms.c | 212 ++++++++++++++++++++++++++++++++++++++++++++++ src/mbimcli/mbimcli.c | 8 ++ src/mbimcli/mbimcli.h | 4 + 4 files changed, 226 insertions(+), 1 deletion(-) create mode 100644 src/mbimcli/mbimcli-sms.c diff --git a/src/mbimcli/Makefile.am b/src/mbimcli/Makefile.am index 0c38ad3..8c99ff6 100644 --- a/src/mbimcli/Makefile.am +++ b/src/mbimcli/Makefile.am @@ -18,7 +18,8 @@ mbimcli_SOURCES = \ mbimcli-dss.c \ mbimcli-ms-firmware-id.c \ mbimcli-ms-host-shutdown.c \ - mbimcli-atds.c + mbimcli-atds.c \ + mbimcli-sms.c mbimcli_LDADD = \ $(MBIMCLI_LIBS) \ diff --git a/src/mbimcli/mbimcli-sms.c b/src/mbimcli/mbimcli-sms.c new file mode 100644 index 0000000..ec7f2ea --- /dev/null +++ b/src/mbimcli/mbimcli-sms.c @@ -0,0 +1,212 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * mbimcli -- Command line interface to control MBIM devices + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * Copyright (C) 2013 - 2014 Nagaraju Kadiri + */ + +#include "config.h" + +#include +#include +#include +#include +#include + +#include +#include + +#include + +#include "mbimcli.h" +#include "mbimcli-helpers.h" + +/* Context */ +typedef struct { + MbimDevice *device; + GCancellable *cancellable; +} Context; +static Context *ctx; + +/* Options */ +static gint sms_read_index; +static gboolean sms_read_all_flag; + +static GOptionEntry entries[] = { + { "sms-read", 0, 0, G_OPTION_ARG_INT, &sms_read_index, + "Read sms entry with given index", + "[(SMS index)]" + }, + { "sms-read-all", 0, 0, G_OPTION_ARG_NONE, &sms_read_all_flag, + "Read all sms entries", + NULL + }, + { NULL } +}; + +GOptionGroup * +mbimcli_sms_get_option_group (void) +{ + GOptionGroup *group; + + group = g_option_group_new ("sms", + "SMS options", + "Show SMS Service options", + NULL, + NULL); + g_option_group_add_entries (group, entries); + + return group; +} + +gboolean +mbimcli_sms_options_enabled (void) +{ + static guint n_actions = 0; + static gboolean checked = FALSE; + + if (checked) + return !!n_actions; + + n_actions = (sms_read_index + + !!sms_read_all_flag); + + if (n_actions > 1) { + g_printerr ("error: too many sms actions requested\n"); + exit (EXIT_FAILURE); + } + + checked = TRUE; + return !!n_actions; +} + +static void +context_free (Context *context) +{ + if (!context) + return; + + if (context->cancellable) + g_object_unref (context->cancellable); + if (context->device) + g_object_unref (context->device); + g_slice_free (Context, context); +} + +static void +shutdown (gboolean operation_status) +{ + /* Cleanup context and finish async operation */ + context_free (ctx); + mbimcli_async_operation_done (operation_status); +} + +static void +query_sms_read_ready (MbimDevice *device, + GAsyncResult *res) +{ + MbimMessage *response; + GError *error = NULL; + guint32 entry_count; + MbimSmsFormat format = MBIM_SMS_FORMAT_CDMA; + MbimSmsCdmaReadRecord **cdma_messages; + gint i = 0; + + response = mbim_device_command_finish (device, res, &error); + if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) { + g_printerr ("error: operation failed: %s\n", error->message); + g_error_free (error); + if (response) + mbim_message_unref (response); + shutdown (FALSE); + return; + } + + if (!mbim_message_sms_read_response_parse (response, + &format, + &entry_count, + NULL, + &cdma_messages, + &error)) { + g_printerr ("error: couldn't parse response message: %s\n", error->message); + g_error_free (error); + mbim_message_unref (response); + shutdown (FALSE); + return; + } + + g_print ("Successfully read sms entries (%d)\n", entry_count); + for (i = 0; i < entry_count; i++) { + g_print ("\tMessage index : %d \n" + "\t Address: %s \n" + "\t Message: %s \n", + cdma_messages[i]->message_index, + cdma_messages[i]->address, + cdma_messages[i]->encoded_message); + } + mbim_sms_cdma_read_record_array_free (cdma_messages); + + mbim_message_unref (response); + shutdown (TRUE); +} + +void +mbimcli_sms_run (MbimDevice *device, + GCancellable *cancellable) +{ + /* Initialize context */ + ctx = g_slice_new (Context); + ctx->device = g_object_ref (device); + ctx->cancellable = cancellable ? g_object_ref (cancellable) : NULL; + + /* SMS read */ + if (sms_read_index) { + MbimMessage *request; + + g_debug ("Asynchronously querying sms read..."); + request = mbim_message_sms_read_query_new (MBIM_SMS_CAPS_TEXT_RECEIVE, + MBIM_SMS_FLAG_INDEX, + sms_read_index, + NULL); + mbim_device_command (ctx->device, + request, + 10, + ctx->cancellable, + (GAsyncReadyCallback)query_sms_read_ready, + NULL); + mbim_message_unref (request); + return; + } + + /* SMS read all */ + if (sms_read_all_flag) { + MbimMessage *request; + + g_debug ("Asynchronously querying sms read all..."); + request = mbim_message_sms_read_query_new (MBIM_SMS_CAPS_TEXT_RECEIVE, + MBIM_SMS_FLAG_ALL, 0, NULL); + mbim_device_command (ctx->device, + request, + 10, + ctx->cancellable, + (GAsyncReadyCallback)query_sms_read_ready, + NULL); + mbim_message_unref (request); + return; + } + + g_warn_if_reached (); +} diff --git a/src/mbimcli/mbimcli.c b/src/mbimcli/mbimcli.c index 27bb634..b566365 100644 --- a/src/mbimcli/mbimcli.c +++ b/src/mbimcli/mbimcli.c @@ -275,6 +275,9 @@ device_open_ready (MbimDevice *dev, case MBIM_SERVICE_ATDS: mbimcli_atds_run (dev, cancellable); return; + case MBIM_SERVICE_SMS: + mbimcli_sms_run (dev, cancellable); + return; default: g_assert_not_reached (); } @@ -349,6 +352,9 @@ parse_actions (void) } else if (mbimcli_atds_options_enabled ()) { service = MBIM_SERVICE_ATDS; actions_enabled++; + } else if (mbimcli_sms_options_enabled ()) { + service = MBIM_SERVICE_SMS; + actions_enabled++; } /* Noop */ @@ -392,6 +398,8 @@ int main (int argc, char **argv) mbimcli_ms_host_shutdown_get_option_group ()); g_option_context_add_group (context, mbimcli_atds_get_option_group ()); + g_option_context_add_group (context, + mbimcli_sms_get_option_group ()); g_option_context_add_main_entries (context, main_entries, NULL); if (!g_option_context_parse (context, &argc, &argv, &error)) { g_printerr ("error: %s\n", diff --git a/src/mbimcli/mbimcli.h b/src/mbimcli/mbimcli.h index 02a1b78..378d2f2 100644 --- a/src/mbimcli/mbimcli.h +++ b/src/mbimcli/mbimcli.h @@ -35,6 +35,7 @@ GOptionGroup *mbimcli_dss_get_option_group (void); GOptionGroup *mbimcli_ms_firmware_id_get_option_group (void); GOptionGroup *mbimcli_ms_host_shutdown_get_option_group (void); GOptionGroup *mbimcli_atds_get_option_group (void); +GOptionGroup *mbimcli_sms_get_option_group (void); gboolean mbimcli_basic_connect_options_enabled (void); gboolean mbimcli_phonebook_options_enabled (void); @@ -42,6 +43,7 @@ gboolean mbimcli_dss_options_enabled (void); gboolean mbimcli_ms_firmware_id_options_enabled (void); gboolean mbimcli_ms_host_shutdown_options_enabled (void); gboolean mbimcli_atds_options_enabled (void); +gboolean mbimcli_sms_options_enabled (void); void mbimcli_basic_connect_run (MbimDevice *device, GCancellable *cancellable); @@ -55,5 +57,7 @@ void mbimcli_ms_host_shutdown_run (MbimDevice *device, GCancellable *cancellable); void mbimcli_atds_run (MbimDevice *device, GCancellable *cancellable); +void mbimcli_sms_run (MbimDevice *device, + GCancellable *cancellable); #endif /* __MBIMCLI_H__ */ -- 2.15.0