[PATCH 2/2] mbimcli: new '--query-pco' action

Ben Chan benchan at chromium.org
Fri Jul 27 06:38:07 UTC 2018


Change-Id: I7f24d481cb92b2e817eba487d38739b8fed58b28
---
 docs/man/Makefile.am                          |   1 +
 src/mbimcli/Makefile.am                       |   1 +
 .../mbimcli-basic-connect-extensions.c        | 246 ++++++++++++++++++
 src/mbimcli/mbimcli.c                         |   8 +
 src/mbimcli/mbimcli.h                         |   4 +
 5 files changed, 260 insertions(+)
 create mode 100644 src/mbimcli/mbimcli-basic-connect-extensions.c

diff --git a/docs/man/Makefile.am b/docs/man/Makefile.am
index bb32c48..53648f5 100644
--- a/docs/man/Makefile.am
+++ b/docs/man/Makefile.am
@@ -7,6 +7,7 @@ dist_man_MANS = \
 MBIMCLI_SOURCES_WITH_HELP = \
 	$(top_srcdir)/src/mbimcli/mbimcli-atds.c \
 	$(top_srcdir)/src/mbimcli/mbimcli-basic-connect.c \
+	$(top_srcdir)/src/mbimcli/mbimcli-basic-connect-extensions.c \
 	$(top_srcdir)/src/mbimcli/mbimcli-dss.c \
 	$(top_srcdir)/src/mbimcli/mbimcli-ms-firmware-id.c \
 	$(top_srcdir)/src/mbimcli/mbimcli-ms-host-shutdown.c \
diff --git a/src/mbimcli/Makefile.am b/src/mbimcli/Makefile.am
index 48f272f..ee213cc 100644
--- a/src/mbimcli/Makefile.am
+++ b/src/mbimcli/Makefile.am
@@ -14,6 +14,7 @@ mbimcli_SOURCES = \
 	mbimcli.h mbimcli.c \
 	mbimcli-helpers.h mbimcli-helpers.c \
 	mbimcli-basic-connect.c \
+	mbimcli-basic-connect-extensions.c \
 	mbimcli-phonebook.c \
 	mbimcli-dss.c \
 	mbimcli-ms-firmware-id.c \
diff --git a/src/mbimcli/mbimcli-basic-connect-extensions.c b/src/mbimcli/mbimcli-basic-connect-extensions.c
new file mode 100644
index 0000000..112905c
--- /dev/null
+++ b/src/mbimcli/mbimcli-basic-connect-extensions.c
@@ -0,0 +1,246 @@
+/* -*- 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 <http://www.gnu.org/licenses/>.
+ *
+ * Copyright 2018 Google LLC
+ */
+
+#include "config.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <locale.h>
+#include <string.h>
+#include <errno.h>
+
+#include <glib.h>
+#include <gio/gio.h>
+
+#include <libmbim-glib.h>
+
+#include "mbim-common.h"
+#include "mbimcli.h"
+
+/* Context */
+typedef struct {
+    MbimDevice *device;
+    GCancellable *cancellable;
+} Context;
+static Context *ctx;
+
+/* Options */
+static gchar *query_pco_str;
+
+static gboolean query_pco_arg_parse (const char *option_name,
+                                     const char *value,
+                                     gpointer user_data,
+                                     GError **error);
+
+static GOptionEntry entries[] = {
+    { "query-pco", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, G_CALLBACK (query_pco_arg_parse),
+      "Query PCO value (SessionID is optional, defaults to 0)",
+      "[SessionID]"
+    },
+    { NULL }
+};
+
+static gboolean
+query_pco_arg_parse (const char *option_name,
+                     const char *value,
+                     gpointer user_data,
+                     GError **error)
+{
+    query_pco_str = g_strdup (value ? value : "0");
+    return TRUE;
+}
+
+GOptionGroup *
+mbimcli_basic_connect_extensions_get_option_group (void)
+{
+   GOptionGroup *group;
+
+   group = g_option_group_new ("basic-connect-extensions",
+                               "Basic Connect Extensions options",
+                               "Show Basic Connect Extensions Service options",
+                               NULL,
+                               NULL);
+   g_option_group_add_entries (group, entries);
+
+   return group;
+}
+
+static gboolean
+session_id_parse (const gchar  *str,
+                  guint32      *session_id,
+                  GError      **error)
+{
+    gchar *endptr = NULL;
+    gint64 n;
+
+    g_assert (str != NULL);
+    g_assert (session_id != NULL);
+
+    if (!str[0]) {
+        *session_id = 0;
+        return TRUE;
+    }
+
+    errno = 0;
+    n = g_ascii_strtoll (str, &endptr, 10);
+    if (errno || n < 0 || n > 255 || ((endptr - str) < strlen (str))) {
+        g_set_error (error,
+                     MBIM_CORE_ERROR,
+                     MBIM_CORE_ERROR_FAILED,
+                     "couldn't parse session ID '%s' (must be 0 - 255)",
+                     str);
+        return FALSE;
+    }
+    *session_id = (guint32) n;
+
+    return TRUE;
+}
+
+gboolean
+mbimcli_basic_connect_extensions_options_enabled (void)
+{
+    static guint n_actions = 0;
+    static gboolean checked = FALSE;
+
+    if (checked)
+        return !!n_actions;
+
+    n_actions = !!query_pco_str;
+
+    if (n_actions > 1) {
+        g_printerr ("error: too many Basic Connect Extensions Service 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_pco_ready (MbimDevice   *device,
+                 GAsyncResult *res)
+{
+    MbimMessage *response;
+    GError *error = NULL;
+    MbimPcoValue *pco_value;
+    gchar *pco_data;
+
+    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;
+    }
+
+    g_print ("[%s] Successfully queried PCO\n\n",
+             mbim_device_get_path_display (device));
+    if (!mbim_message_basic_connect_extensions_pco_response_parse (
+            response,
+            &pco_value,
+            &error)) {
+        g_printerr ("error: couldn't parse response message: %s\n", error->message);
+        g_error_free (error);
+        mbim_message_unref (response);
+        shutdown (FALSE);
+        return;
+    }
+
+    pco_data = mbim_common_str_hex (pco_value->pco_data_buffer, pco_value->pco_data_size, ' ');
+    g_print ("[%s] PCO:\n"
+             "\t   Session ID: '%u'\n"
+             "\tPCO data type: '%s'\n"
+             "\tPCO data size: '%u'\n"
+             "\t     PCO data: '%s'\n",
+             mbim_device_get_path_display (device),
+             pco_value->session_id,
+             VALIDATE_UNKNOWN (mbim_pco_type_get_string (pco_value->pco_data_type)),
+             pco_value->pco_data_size,
+             pco_data);
+    g_free (pco_data);
+    mbim_pco_value_free (pco_value);
+
+    mbim_message_unref (response);
+    shutdown (TRUE);
+}
+
+void
+mbimcli_basic_connect_extensions_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;
+
+    /* Request to get PCO? */
+    if (query_pco_str) {
+        MbimMessage *request;
+        MbimPcoValue pco_value;
+        GError *error = NULL;
+
+        if (!session_id_parse (query_pco_str, &pco_value.session_id, &error)) {
+            g_printerr ("error: couldn't parse session ID: %s\n", error->message);
+            g_error_free (error);
+            shutdown (FALSE);
+            return;
+        }
+
+        pco_value.pco_data_size = 0;
+        pco_value.pco_data_type = MBIM_PCO_TYPE_COMPLETE;
+        pco_value.pco_data_buffer = NULL;
+
+        g_debug ("Asynchronously querying PCO...");
+        request = mbim_message_basic_connect_extensions_pco_query_new (&pco_value, NULL);
+        mbim_device_command (ctx->device,
+                             request,
+                             10,
+                             ctx->cancellable,
+                             (GAsyncReadyCallback)query_pco_ready,
+                             NULL);
+        mbim_message_unref (request);
+        return;
+    }
+
+    g_warn_if_reached ();
+}
diff --git a/src/mbimcli/mbimcli.c b/src/mbimcli/mbimcli.c
index 7ee1716..abf5757 100644
--- a/src/mbimcli/mbimcli.c
+++ b/src/mbimcli/mbimcli.c
@@ -278,6 +278,9 @@ device_open_ready (MbimDevice   *dev,
     case MBIM_SERVICE_INTEL_FIRMWARE_UPDATE:
         mbimcli_intel_firmware_update_run (dev, cancellable);
         return;
+    case MBIM_SERVICE_BASIC_CONNECT_EXTENSIONS:
+        mbimcli_basic_connect_extensions_run (dev, cancellable);
+        return;
     default:
         g_assert_not_reached ();
     }
@@ -355,6 +358,9 @@ parse_actions (void)
     } else if (mbimcli_intel_firmware_update_options_enabled ()) {
         service = MBIM_SERVICE_INTEL_FIRMWARE_UPDATE;
         actions_enabled++;
+    } else if (mbimcli_basic_connect_extensions_options_enabled ()) {
+        service = MBIM_SERVICE_BASIC_CONNECT_EXTENSIONS;
+        actions_enabled++;
     }
 
     /* Noop */
@@ -400,6 +406,8 @@ int main (int argc, char **argv)
                                 mbimcli_atds_get_option_group ());
     g_option_context_add_group (context,
                                 mbimcli_intel_firmware_update_get_option_group ());
+    g_option_context_add_group (context,
+                                mbimcli_basic_connect_extensions_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 a920872..5033b4e 100644
--- a/src/mbimcli/mbimcli.h
+++ b/src/mbimcli/mbimcli.h
@@ -36,6 +36,7 @@ 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_intel_firmware_update_get_option_group (void);
+GOptionGroup *mbimcli_basic_connect_extensions_get_option_group (void);
 
 gboolean      mbimcli_basic_connect_options_enabled     (void);
 gboolean      mbimcli_phonebook_options_enabled         (void);
@@ -44,6 +45,7 @@ gboolean      mbimcli_ms_firmware_id_options_enabled    (void);
 gboolean      mbimcli_ms_host_shutdown_options_enabled  (void);
 gboolean      mbimcli_atds_options_enabled              (void);
 gboolean      mbimcli_intel_firmware_update_options_enabled (void);
+gboolean      mbimcli_basic_connect_extensions_options_enabled (void);
 
 void          mbimcli_basic_connect_run                 (MbimDevice *device,
                                                          GCancellable *cancellable);
@@ -59,5 +61,7 @@ void          mbimcli_atds_run                          (MbimDevice *device,
                                                          GCancellable *cancellable);
 void          mbimcli_intel_firmware_update_run         (MbimDevice *device,
                                                          GCancellable *cancellable);
+void          mbimcli_basic_connect_extensions_run      (MbimDevice *device,
+                                                         GCancellable *cancellable);
 
 #endif /* __MBIMCLI_H__ */
-- 
2.18.0.345.g5c9ce644c3-goog



More information about the libmbim-devel mailing list