Debian Codegraph @

src/mbimcli/mbimcli-quectel.c

raw
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) 2021 Aleksander Morgado <[email protected]>
7 */
8
9 #include "config.h"
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <locale.h>
14 #include <string.h>
15
16 #include <glib.h>
17 #include <gio/gio.h>
18
19 #include <libmbim-glib.h>
20
21 #include "mbimcli.h"
22 #include "mbimcli-helpers.h"
23 #include "mbimcli-helpers-quectel.h"
24
25 /* Context */
26 typedef struct {
27 MbimDevice *device;
28 GCancellable *cancellable;
29 } Context;
30 static Context *ctx;
31
32 /* Options */
33 static gboolean query_radio_state_flag;
34 static gchar *set_radio_state_str;
35 static gchar *set_command_str;
36
37 static GOptionEntry entries[] = {
38 { "quectel-query-radio-state", 0, 0, G_OPTION_ARG_NONE, &query_radio_state_flag,
39 "Query radio state",
40 NULL
41 },
42 { "quectel-set-radio-state", 0, 0, G_OPTION_ARG_STRING, &set_radio_state_str,
43 "Set radio state",
44 "[(on)]"
45 },
46 { "quectel-set-command", 0, 0, G_OPTION_ARG_STRING, &set_command_str,
47 "Send command to module (Command type is optional, defaults to AT, allowed options: "
48 "(at, system)",
49 "[(Command type),(\"Command\")]"
50 },
51 { NULL, 0, 0, 0, NULL, NULL, NULL }
52 };
53
54 GOptionGroup *
55 mbimcli_quectel_get_option_group (void)
56 {
57 GOptionGroup *group;
58
59 group = g_option_group_new ("quectel",
60 "Quectel options:",
61 "Show Quectel Service options",
62 NULL,
63 NULL);
64 g_option_group_add_entries (group, entries);
65
66 return group;
67 }
68
69 gboolean
70 mbimcli_quectel_options_enabled (void)
71 {
72 static guint n_actions = 0;
73 static gboolean checked = FALSE;
74
75 if (checked)
76 return !!n_actions;
77
78 n_actions = (query_radio_state_flag +
79 !!set_radio_state_str +
80 !!set_command_str);
81
82 if (n_actions > 1) {
83 g_printerr ("error: too many Quectel actions requested\n");
84 exit (EXIT_FAILURE);
85 }
86
87 checked = TRUE;
88 return !!n_actions;
89 }
90
91 static void
92 context_free (Context *context)
93 {
94 if (!context)
95 return;
96
97 if (context->cancellable)
98 g_object_unref (context->cancellable);
99 if (context->device)
100 g_object_unref (context->device);
101 g_slice_free (Context, context);
102 }
103
104 static void
105 shutdown (gboolean operation_status)
106 {
107 /* Cleanup context and finish async operation */
108 context_free (ctx);
109 mbimcli_async_operation_done (operation_status);
110 }
111
112 static void
113 radio_state_ready (MbimDevice *device,
114 GAsyncResult *res,
115 gpointer user_data)
116 {
117 g_autoptr(MbimMessage) response = NULL;
118 g_autoptr(GError) error = NULL;
119 MbimQuectelRadioSwitchState radio_state;
120 const gchar *radio_state_str;
121 gboolean action_flag;
122
123 action_flag = GPOINTER_TO_UINT (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 (action_flag) {
133 g_print ("[%s] Successfully requested to enable radio\n",
134 mbim_device_get_path_display (device));
135 shutdown (TRUE);
136 return;
137 }
138
139 /* The body is only included in the query response, not in the set response */
140 if (!mbim_message_quectel_radio_state_response_parse (response, &radio_state, &error)) {
141 g_printerr ("error: couldn't parse response message: %s\n", error->message);
142 shutdown (FALSE);
143 return;
144 }
145
146 radio_state_str = mbim_quectel_radio_switch_state_get_string (radio_state);
147
148 g_print ("[%s] Radio state retrieved: '%s'\n",
149 mbim_device_get_path_display (device),
150 VALIDATE_UNKNOWN (radio_state_str));
151
152 shutdown (TRUE);
153 }
154
155 static void
156 qdu_command_ready (MbimDevice *device,
157 GAsyncResult *res)
158 {
159 g_autoptr(GError) error = NULL;
160 guint32 ret_status = 0;
161 guint32 ret_size = 0;
162 const guint8 *ret_data = NULL;
163 g_autoptr(MbimMessage) response = NULL;
164
165 response = mbim_device_command_finish (device, res, &error);
166
167 if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
168 g_printerr ("error: operation failed: %s\n", error->message);
169 shutdown (FALSE);
170 return;
171 }
172
173 if (!mbim_message_qdu_command_response_parse (
174 response,
175 &ret_status,
176 &ret_size,
177 &ret_data,
178 &error)) {
179 g_printerr ("error: couldn't parse response message: %s\n", error->message);
180 shutdown (FALSE);
181 return;
182 }
183
184 if (ret_status == MBIM_QUECTEL_COMMAND_RESPONSE_STATUS_OK)
185 g_print ("%.*s\n", ret_size , ret_data);
186 else
187 g_print ("error: Command returns error!!\n");
188
189 shutdown (TRUE);
190 }
191
192 void
193 mbimcli_quectel_run (MbimDevice *device,
194 GCancellable *cancellable)
195 {
196 g_autoptr(MbimMessage) request = NULL;
197
198 /* Initialize context */
199 ctx = g_slice_new (Context);
200 ctx->device = g_object_ref (device);
201 ctx->cancellable = cancellable ? g_object_ref (cancellable) : NULL;
202
203 /* Request to get radio state? */
204 if (query_radio_state_flag) {
205 g_debug ("Asynchronously querying radio state...");
206 request = (mbim_message_quectel_radio_state_query_new (NULL));
207 mbim_device_command (ctx->device,
208 request,
209 10,
210 ctx->cancellable,
211 (GAsyncReadyCallback)radio_state_ready,
212 GUINT_TO_POINTER (FALSE));
213 return;
214 }
215
216 /* Request to set radio state? */
217 if (set_radio_state_str) {
218 MbimQuectelRadioSwitchState radio_state;
219
220 if (g_ascii_strcasecmp (set_radio_state_str, "on") == 0)
221 radio_state = MBIM_QUECTEL_RADIO_SWITCH_STATE_ON;
222 else {
223 g_printerr ("error: invalid radio state (only 'on' allowed): '%s'\n", set_radio_state_str);
224 shutdown (FALSE);
225 return;
226 }
227
228 g_debug ("Asynchronously setting radio state to on...");
229 request = mbim_message_quectel_radio_state_set_new (radio_state, NULL);
230 mbim_device_command (ctx->device,
231 request,
232 10,
233 ctx->cancellable,
234 (GAsyncReadyCallback)radio_state_ready,
235 GUINT_TO_POINTER (TRUE));
236 return;
237 }
238
239 /* Request to send AT command */
240 if (set_command_str) {
241 g_autofree gchar *req_str = NULL;
242 guint32 req_size = 0;
243 MbimQuectelCommandType command_type = MBIM_QUECTEL_COMMAND_TYPE_AT;
244
245 if (!mbimcli_helpers_quectel_set_command_input_parse (set_command_str, &req_str, &command_type)) {
246 g_printerr ("error: parse input string failed!\n");
247 shutdown (FALSE);
248 return;
249 }
250
251 req_size = strlen (req_str);
252
253 request = mbim_message_qdu_command_set_new (command_type,
254 req_size,
255 (const guint8 *) req_str,
256 NULL);
257 mbim_device_command (ctx->device,
258 request,
259 10,
260 ctx->cancellable,
261 (GAsyncReadyCallback)qdu_command_ready,
262 NULL);
263 return;
264 }
265
266 g_warn_if_reached ();
267 }