summaryrefslogtreecommitdiff
path: root/src/mbimcli/mbimcli-basic-connect-extensions.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/mbimcli/mbimcli-basic-connect-extensions.c')
-rw-r--r--src/mbimcli/mbimcli-basic-connect-extensions.c246
1 files changed, 246 insertions, 0 deletions
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 ();
+}