/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* SPDX-License-Identifier: GPL-2.0-or-later */ /* * mbimcli -- Command line interface to control MBIM devices * * Copyright (C) 2021 Aleksander Morgado */ #include "config.h" #include #include #include #include #include #include #include #include "mbimcli.h" #include "mbimcli-helpers.h" #include "mbimcli-helpers-quectel.h" /* Context */ typedef struct { MbimDevice *device; GCancellable *cancellable; } Context; static Context *ctx; /* Options */ static gboolean query_radio_state_flag; static gchar *set_radio_state_str; static gchar *set_command_str; static GOptionEntry entries[] = { { "quectel-query-radio-state", 0, 0, G_OPTION_ARG_NONE, &query_radio_state_flag, "Query radio state", NULL }, { "quectel-set-radio-state", 0, 0, G_OPTION_ARG_STRING, &set_radio_state_str, "Set radio state", "[(on)]" }, { "quectel-set-command", 0, 0, G_OPTION_ARG_STRING, &set_command_str, "Send command to module (Command type is optional, defaults to AT, allowed options: " "(at, system)", "[(Command type),(\"Command\")]" }, { NULL, 0, 0, 0, NULL, NULL, NULL } }; GOptionGroup * mbimcli_quectel_get_option_group (void) { GOptionGroup *group; group = g_option_group_new ("quectel", "Quectel options:", "Show Quectel Service options", NULL, NULL); g_option_group_add_entries (group, entries); return group; } gboolean mbimcli_quectel_options_enabled (void) { static guint n_actions = 0; static gboolean checked = FALSE; if (checked) return !!n_actions; n_actions = (query_radio_state_flag + !!set_radio_state_str + !!set_command_str); if (n_actions > 1) { g_printerr ("error: too many Quectel 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 radio_state_ready (MbimDevice *device, GAsyncResult *res, gpointer user_data) { g_autoptr(MbimMessage) response = NULL; g_autoptr(GError) error = NULL; MbimQuectelRadioSwitchState radio_state; const gchar *radio_state_str; gboolean action_flag; action_flag = GPOINTER_TO_UINT (user_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); shutdown (FALSE); return; } if (action_flag) { g_print ("[%s] Successfully requested to enable radio\n", mbim_device_get_path_display (device)); shutdown (TRUE); return; } /* The body is only included in the query response, not in the set response */ if (!mbim_message_quectel_radio_state_response_parse (response, &radio_state, &error)) { g_printerr ("error: couldn't parse response message: %s\n", error->message); shutdown (FALSE); return; } radio_state_str = mbim_quectel_radio_switch_state_get_string (radio_state); g_print ("[%s] Radio state retrieved: '%s'\n", mbim_device_get_path_display (device), VALIDATE_UNKNOWN (radio_state_str)); shutdown (TRUE); } static void qdu_command_ready (MbimDevice *device, GAsyncResult *res) { g_autoptr(GError) error = NULL; guint32 ret_status = 0; guint32 ret_size = 0; const guint8 *ret_data = NULL; g_autoptr(MbimMessage) response = NULL; 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); shutdown (FALSE); return; } if (!mbim_message_qdu_command_response_parse ( response, &ret_status, &ret_size, &ret_data, &error)) { g_printerr ("error: couldn't parse response message: %s\n", error->message); shutdown (FALSE); return; } if (ret_status == MBIM_QUECTEL_COMMAND_RESPONSE_STATUS_OK) g_print ("%.*s\n", ret_size , ret_data); else g_print ("error: Command returns error!!\n"); shutdown (TRUE); } void mbimcli_quectel_run (MbimDevice *device, GCancellable *cancellable) { g_autoptr(MbimMessage) request = NULL; /* Initialize context */ ctx = g_slice_new (Context); ctx->device = g_object_ref (device); ctx->cancellable = cancellable ? g_object_ref (cancellable) : NULL; /* Request to get radio state? */ if (query_radio_state_flag) { g_debug ("Asynchronously querying radio state..."); request = (mbim_message_quectel_radio_state_query_new (NULL)); mbim_device_command (ctx->device, request, 10, ctx->cancellable, (GAsyncReadyCallback)radio_state_ready, GUINT_TO_POINTER (FALSE)); return; } /* Request to set radio state? */ if (set_radio_state_str) { MbimQuectelRadioSwitchState radio_state; if (g_ascii_strcasecmp (set_radio_state_str, "on") == 0) radio_state = MBIM_QUECTEL_RADIO_SWITCH_STATE_ON; else { g_printerr ("error: invalid radio state (only 'on' allowed): '%s'\n", set_radio_state_str); shutdown (FALSE); return; } g_debug ("Asynchronously setting radio state to on..."); request = mbim_message_quectel_radio_state_set_new (radio_state, NULL); mbim_device_command (ctx->device, request, 10, ctx->cancellable, (GAsyncReadyCallback)radio_state_ready, GUINT_TO_POINTER (TRUE)); return; } /* Request to send AT command */ if (set_command_str) { g_autofree gchar *req_str = NULL; guint32 req_size = 0; MbimQuectelCommandType command_type = MBIM_QUECTEL_COMMAND_TYPE_AT; if (!mbimcli_helpers_quectel_set_command_input_parse (set_command_str, &req_str, &command_type)) { g_printerr ("error: parse input string failed!\n"); shutdown (FALSE); return; } req_size = strlen (req_str); request = mbim_message_qdu_command_set_new (command_type, req_size, (const guint8 *) req_str, NULL); mbim_device_command (ctx->device, request, 10, ctx->cancellable, (GAsyncReadyCallback)qdu_command_ready, NULL); return; } g_warn_if_reached (); }