Debian Codegraph @

src/qmicli/qmicli-gms.c

raw
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * qmicli -- Command line interface to control QMI devices
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * Copyright (C) 2020 Vladimir Podshivalov <[email protected]>
19 */
20
21 #include "config.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <locale.h>
26 #include <string.h>
27 #include <assert.h>
28
29 #include <glib.h>
30 #include <gio/gio.h>
31
32 #include <libqmi-glib.h>
33
34 #include "qmicli.h"
35 #include "qmicli-helpers.h"
36
37 #if defined HAVE_QMI_SERVICE_GMS
38
39 /* Context */
40 typedef struct {
41 QmiDevice *device;
42 QmiClientGms *client;
43 GCancellable *cancellable;
44 } Context;
45 static Context *ctx;
46
47 /* Options */
48 static gboolean get_value_flag;
49 static gchar *set_value_str;
50 static gboolean noop_flag;
51
52 static GOptionEntry entries[] = {
53 #if defined HAVE_QMI_MESSAGE_GMS_TEST_GET_VALUE
54 { "gms-test-get-value", 0, 0, G_OPTION_ARG_NONE, &get_value_flag,
55 "Gets test value",
56 NULL
57 },
58 #endif
59 #if defined HAVE_QMI_MESSAGE_GMS_TEST_SET_VALUE
60 { "gms-test-set-value", 0, 0, G_OPTION_ARG_STRING, &set_value_str,
61 "Sets test value",
62 "[mandatory-value][,[optional-value]]"
63 },
64 #endif
65 { "gms-noop", 0, 0, G_OPTION_ARG_NONE, &noop_flag,
66 "Just allocate or release a GMS client. Use with `--client-no-release-cid' and/or `--client-cid'",
67 NULL
68 },
69 { NULL, 0, 0, 0, NULL, NULL, NULL }
70 };
71
72 GOptionGroup *
73 qmicli_gms_get_option_group (void)
74 {
75 GOptionGroup *group;
76
77 group = g_option_group_new ("gms",
78 "GMS options:",
79 "Show General Modem Service options",
80 NULL,
81 NULL);
82 g_option_group_add_entries (group, entries);
83
84 return group;
85 }
86
87 gboolean
88 qmicli_gms_options_enabled (void)
89 {
90 static guint n_actions = 0;
91 static gboolean checked = FALSE;
92
93 if (checked)
94 return !!n_actions;
95
96 n_actions = (get_value_flag +
97 !!set_value_str +
98 noop_flag);
99
100 if (n_actions > 1) {
101 g_printerr ("error: too many GMS actions requested\n");
102 exit (EXIT_FAILURE);
103 }
104
105 checked = TRUE;
106 return !!n_actions;
107 }
108
109 static void
110 context_free (Context *context)
111 {
112 if (!context)
113 return;
114
115 if (context->cancellable)
116 g_object_unref (context->cancellable);
117 if (context->device)
118 g_object_unref (context->device);
119 if (context->client)
120 g_object_unref (context->client);
121 g_slice_free (Context, context);
122 }
123
124 static void
125 operation_shutdown (gboolean operation_status)
126 {
127 /* Cleanup context and finish async operation */
128 context_free (ctx);
129 qmicli_async_operation_done (operation_status, FALSE);
130 }
131
132 #if defined HAVE_QMI_MESSAGE_GMS_TEST_GET_VALUE
133
134 static void
135 get_value_ready (QmiClientGms *client,
136 GAsyncResult *res)
137 {
138 QmiMessageGmsTestGetValueOutput *output;
139 GError *error = NULL;
140 guint8 test_mandatory_value;
141 guint8 test_optional_value;
142
143 output = qmi_client_gms_test_get_value_finish (client, res, &error);
144 if (!output) {
145 g_printerr ("error: operation failed: %s\n", error->message);
146 g_error_free (error);
147 operation_shutdown (FALSE);
148 return;
149 }
150
151 if (!qmi_message_gms_test_get_value_output_get_result (output, &error)) {
152 g_printerr ("error: couldn't get stored test value: %s\n", error->message);
153 g_error_free (error);
154 qmi_message_gms_test_get_value_output_unref (output);
155 operation_shutdown (FALSE);
156 return;
157 }
158
159 if (qmi_message_gms_test_get_value_output_get_test_mandatory_value (output, &test_mandatory_value, NULL)) {
160 g_print ("Test mandatory value: %u\n", test_mandatory_value);
161 }
162
163 if (qmi_message_gms_test_get_value_output_get_test_optional_value (output, &test_optional_value, NULL)) {
164 g_print ("Test optional value: %u\n", test_optional_value);
165 }
166
167 qmi_message_gms_test_get_value_output_unref (output);
168 operation_shutdown (TRUE);
169 }
170
171 #endif /* HAVE_QMI_MESSAGE_GMS_TEST_GET_VALUE */
172
173 #if defined HAVE_QMI_MESSAGE_GMS_TEST_SET_VALUE
174
175 static QmiMessageGmsTestSetValueInput *
176 set_value_input_create (const gchar *str)
177 {
178 QmiMessageGmsTestSetValueInput *input = NULL;
179 const gchar *mand_value_str = NULL;
180 const gchar *opt_value_str = NULL;
181 guint mand_value_int = 0;
182 guint opt_value_int = 0;
183 gchar **parts = NULL;
184
185 if (strchr (str, ',')) {
186 /* Both Mandatory Test Value and Optional Test Value were given */
187 parts = g_strsplit_set (str, ",", -1);
188 if (g_strv_length (parts) != 2) {
189 g_printerr ("error: failed to parse test value: '%s'\n", str);
190 goto out;
191 }
192 mand_value_str = parts[0];
193 opt_value_str = parts[1];
194 } else {
195 /* Only Mandatory Test Value was given */
196 mand_value_str = str;
197 }
198
199 g_assert (mand_value_str);
200 if (!qmicli_read_uint_from_string (mand_value_str, &mand_value_int) || (mand_value_int > G_MAXUINT8)) {
201 g_printerr ("error: failed to parse test mandatory value as 8bit value: '%s'\n", mand_value_str);
202 goto out;
203 }
204
205 if (opt_value_str && (!qmicli_read_uint_from_string (opt_value_str, &opt_value_int) || (opt_value_int > G_MAXUINT8))) {
206 g_printerr ("error: failed to parse test optional value as 8bit value: '%s'\n", opt_value_str);
207 goto out;
208 }
209
210 input = qmi_message_gms_test_set_value_input_new ();
211
212 qmi_message_gms_test_set_value_input_set_test_mandatory_value (input, (guint8) mand_value_int, NULL);
213 if (opt_value_str) {
214 qmi_message_gms_test_set_value_input_set_test_optional_value (input, (guint8) opt_value_int, NULL);
215 }
216
217 out:
218 g_strfreev (parts);
219 return input;
220 }
221
222 static void
223 set_value_ready (QmiClientGms *client,
224 GAsyncResult *res)
225 {
226 QmiMessageGmsTestSetValueOutput *output;
227 GError *error = NULL;
228
229 output = qmi_client_gms_test_set_value_finish (client, res, &error);
230 if (!output) {
231 g_printerr ("error: operation failed: %s\n", error->message);
232 g_error_free (error);
233 operation_shutdown (FALSE);
234 return;
235 }
236
237 if (!qmi_message_gms_test_set_value_output_get_result (output, &error)) {
238 g_printerr ("error: couldn't set test value: %s\n", error->message);
239 g_error_free (error);
240 qmi_message_gms_test_set_value_output_unref (output);
241 operation_shutdown (FALSE);
242 return;
243 }
244
245 g_print ("Successfully set test value.\n");
246
247 qmi_message_gms_test_set_value_output_unref (output);
248 operation_shutdown (TRUE);
249 }
250
251 #endif /* HAVE_QMI_MESSAGE_GMS_TEST_SET_VALUE */
252
253 static gboolean
254 noop_cb (gpointer unused)
255 {
256 operation_shutdown (TRUE);
257 return FALSE;
258 }
259
260 void
261 qmicli_gms_run (QmiDevice *device,
262 QmiClientGms *client,
263 GCancellable *cancellable)
264 {
265 /* Initialize context */
266 ctx = g_slice_new (Context);
267 ctx->device = g_object_ref (device);
268 ctx->client = g_object_ref (client);
269 ctx->cancellable = g_object_ref (cancellable);
270
271 #if defined HAVE_QMI_MESSAGE_GMS_TEST_GET_VALUE
272 if (get_value_flag) {
273 g_debug ("Asynchronously getting test value...");
274 qmi_client_gms_test_get_value (ctx->client,
275 NULL,
276 10,
277 ctx->cancellable,
278 (GAsyncReadyCallback)get_value_ready,
279 NULL);
280 return;
281 }
282 #endif
283
284 #if defined HAVE_QMI_MESSAGE_GMS_TEST_SET_VALUE
285 if (set_value_str) {
286 QmiMessageGmsTestSetValueInput *input;
287 g_debug ("Asynchronously setting test value...");
288
289 input = set_value_input_create (set_value_str);
290 if (!input) {
291 operation_shutdown (FALSE);
292 return;
293 }
294
295 qmi_client_gms_test_set_value (ctx->client,
296 input,
297 10,
298 ctx->cancellable,
299 (GAsyncReadyCallback)set_value_ready,
300 NULL);
301
302 qmi_message_gms_test_set_value_input_unref (input);
303 return;
304 }
305 #endif
306
307 /* Just client allocate/release? */
308 if (noop_flag) {
309 g_idle_add (noop_cb, NULL);
310 return;
311 }
312
313 g_warn_if_reached ();
314 }
315
316 #endif /* HAVE_QMI_SERVICE_GMS */