Debian Codegraph @

src/qmicli/qmicli-fox.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) 2022 Freedom Liu <[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_FOX
38
39 #undef VALIDATE_UNKNOWN
40 #define VALIDATE_UNKNOWN(str) (str ? str : "unknown")
41
42 /* Context */
43 typedef struct {
44 QmiDevice *device;
45 QmiClientFox *client;
46 GCancellable *cancellable;
47 } Context;
48 static Context *ctx;
49
50 /* Options */
51 static gchar *get_firmware_version_str;
52 static gchar *set_fcc_authentication_str;
53 static gboolean noop_flag;
54
55 static GOptionEntry entries[] = {
56 #if defined HAVE_QMI_MESSAGE_FOX_GET_FIRMWARE_VERSION
57 { "fox-get-firmware-version", 0, 0, G_OPTION_ARG_STRING, &get_firmware_version_str,
58 "Get firmware version",
59 "[firmware-mcfg-apps|firmware-mcfg|apps]"
60 },
61 #endif
62 #if defined HAVE_QMI_MESSAGE_FOX_SET_FCC_AUTHENTICATION
63 { "fox-set-fcc-authentication", 0, 0, G_OPTION_ARG_STRING, &set_fcc_authentication_str,
64 "Set FCC authentication",
65 "[magic-string,magic-number]"
66 },
67 #endif
68 { "fox-noop", 0, 0, G_OPTION_ARG_NONE, &noop_flag,
69 "Just allocate or release a FOX client. Use with `--client-no-release-cid' and/or `--client-cid'",
70 NULL
71 },
72 { NULL, 0, 0, 0, NULL, NULL, NULL }
73 };
74
75 GOptionGroup *
76 qmicli_fox_get_option_group (void)
77 {
78 GOptionGroup *group;
79
80 group = g_option_group_new ("fox",
81 "FOX options:",
82 "Show Foxconn Modem Service options",
83 NULL,
84 NULL);
85 g_option_group_add_entries (group, entries);
86
87 return group;
88 }
89
90 gboolean
91 qmicli_fox_options_enabled (void)
92 {
93 static guint n_actions = 0;
94 static gboolean checked = FALSE;
95
96 if (checked)
97 return !!n_actions;
98
99 n_actions = (!!get_firmware_version_str +
100 !!set_fcc_authentication_str +
101 noop_flag);
102
103 if (n_actions > 1) {
104 g_printerr ("error: too many FOX actions requested\n");
105 exit (EXIT_FAILURE);
106 }
107
108 checked = TRUE;
109 return !!n_actions;
110 }
111
112 static void
113 context_free (Context *context)
114 {
115 if (!context)
116 return;
117
118 if (context->cancellable)
119 g_object_unref (context->cancellable);
120 if (context->device)
121 g_object_unref (context->device);
122 if (context->client)
123 g_object_unref (context->client);
124 g_slice_free (Context, context);
125 }
126
127 static void
128 operation_shutdown (gboolean operation_status)
129 {
130 /* Cleanup context and finish async operation */
131 context_free (ctx);
132 qmicli_async_operation_done (operation_status, FALSE);
133 }
134
135 #if defined HAVE_QMI_MESSAGE_FOX_GET_FIRMWARE_VERSION
136
137 static QmiMessageFoxGetFirmwareVersionInput *
138 get_firmware_version_input_create (const gchar *str)
139 {
140 QmiMessageFoxGetFirmwareVersionInput *input = NULL;
141 QmiFoxFirmwareVersionType type;
142 GError *error = NULL;
143
144 if (!qmicli_read_fox_firmware_version_type_from_string (str, &type)) {
145 g_printerr ("error: couldn't parse input firmware version type : '%s'\n", str);
146 return NULL;
147 }
148
149 input = qmi_message_fox_get_firmware_version_input_new ();
150 if (!qmi_message_fox_get_firmware_version_input_set_version_type (input, type, &error)) {
151 g_printerr ("error: couldn't create input data bundle: '%s'\n",
152 error->message);
153 g_error_free (error);
154 qmi_message_fox_get_firmware_version_input_unref (input);
155 return NULL;
156 }
157
158 return input;
159 }
160
161 static void
162 get_firmware_version_ready (QmiClientFox *client,
163 GAsyncResult *res)
164 {
165 const gchar *str = NULL;
166 QmiMessageFoxGetFirmwareVersionOutput *output;
167 GError *error = NULL;
168
169 output = qmi_client_fox_get_firmware_version_finish (client, res, &error);
170 if (!output) {
171 g_printerr ("error: operation failed: %s\n", error->message);
172 g_error_free (error);
173 operation_shutdown (FALSE);
174 return;
175 }
176
177 if (!qmi_message_fox_get_firmware_version_output_get_result (output, &error)) {
178 g_printerr ("error: couldn't get firmware version: %s\n", error->message);
179 g_error_free (error);
180 qmi_message_fox_get_firmware_version_output_unref (output);
181 operation_shutdown (FALSE);
182 return;
183 }
184
185 qmi_message_fox_get_firmware_version_output_get_version (output, &str, NULL);
186
187 g_print ("[%s] Firmware version retrieved:\n"
188 "\tVersion: '%s'\n",
189 qmi_device_get_path_display (ctx->device),
190 VALIDATE_UNKNOWN (str));
191
192 qmi_message_fox_get_firmware_version_output_unref (output);
193 operation_shutdown (TRUE);
194 }
195
196 #endif /* HAVE_QMI_MESSAGE_FOX_GET_FIRMWARE_VERSION */
197
198 #if defined HAVE_QMI_MESSAGE_FOX_SET_FCC_AUTHENTICATION
199
200 static void
201 set_fcc_authentication_ready (QmiClientFox *client,
202 GAsyncResult *res)
203 {
204 g_autoptr(QmiMessageFoxSetFccAuthenticationOutput) output = NULL;
205 g_autoptr(GError) error = NULL;
206
207 output = qmi_client_fox_set_fcc_authentication_finish (client, res, &error);
208 if (!output) {
209 g_printerr ("error: operation failed: %s\n", error->message);
210 operation_shutdown (FALSE);
211 return;
212 }
213
214 if (!qmi_message_fox_set_fcc_authentication_output_get_result (output, &error)) {
215 g_printerr ("error: couldn't run Fox FCC authentication: %s\n", error->message);
216 operation_shutdown (FALSE);
217 return;
218 }
219
220 g_print ("[%s] Successfully run Fox FCC authentication\n",
221 qmi_device_get_path_display (ctx->device));
222 operation_shutdown (TRUE);
223 }
224
225 #endif /* HAVE_QMI_MESSAGE_FOX_SET_FCC_AUTHENTICATION */
226
227 static gboolean
228 noop_cb (gpointer unused)
229 {
230 operation_shutdown (TRUE);
231 return FALSE;
232 }
233
234 void
235 qmicli_fox_run (QmiDevice *device,
236 QmiClientFox *client,
237 GCancellable *cancellable)
238 {
239 /* Initialize context */
240 ctx = g_slice_new (Context);
241 ctx->device = g_object_ref (device);
242 ctx->client = g_object_ref (client);
243 ctx->cancellable = g_object_ref (cancellable);
244
245 #if defined HAVE_QMI_MESSAGE_FOX_GET_FIRMWARE_VERSION
246 if (get_firmware_version_str) {
247 QmiMessageFoxGetFirmwareVersionInput *input;
248
249 g_debug ("Asynchronously getting firmware version...");
250
251 input = get_firmware_version_input_create (get_firmware_version_str);
252 if (!input) {
253 operation_shutdown (FALSE);
254 return;
255 }
256
257 qmi_client_fox_get_firmware_version (ctx->client,
258 input,
259 10,
260 ctx->cancellable,
261 (GAsyncReadyCallback)get_firmware_version_ready,
262 NULL);
263 qmi_message_fox_get_firmware_version_input_unref (input);
264 return;
265 }
266 #endif
267
268 #if defined HAVE_QMI_MESSAGE_FOX_SET_FCC_AUTHENTICATION
269 if (set_fcc_authentication_str) {
270 g_autoptr(QmiMessageFoxSetFccAuthenticationInput) input = NULL;
271 g_auto(GStrv) split = NULL;
272 gulong magic_number;
273
274 split = g_strsplit (set_fcc_authentication_str, ",", -1);
275 if (g_strv_length (split) < 2) {
276 g_printerr ("error: missing fields\n");
277 operation_shutdown (FALSE);
278 return;
279 }
280 if (g_strv_length (split) > 2) {
281 g_printerr ("error: too many fields given\n");
282 operation_shutdown (FALSE);
283 return;
284 }
285
286 magic_number = strtoul (split[1], NULL, 10);
287 if (magic_number > 0xFF) {
288 g_printerr ("error: magic number value out of [0,255] range\n");
289 operation_shutdown (FALSE);
290 return;
291 }
292
293 g_debug ("Asynchronously running Fox FCC authentication...");
294
295 input = qmi_message_fox_set_fcc_authentication_input_new ();
296 qmi_message_fox_set_fcc_authentication_input_set_magic_string (input, split[0], NULL);
297 qmi_message_fox_set_fcc_authentication_input_set_magic_number (input, magic_number, NULL);
298 qmi_client_fox_set_fcc_authentication (ctx->client,
299 input,
300 10,
301 ctx->cancellable,
302 (GAsyncReadyCallback)set_fcc_authentication_ready,
303 NULL);
304 return;
305 }
306 #endif
307
308 /* Just client allocate/release? */
309 if (noop_flag) {
310 g_idle_add (noop_cb, NULL);
311 return;
312 }
313
314 g_warn_if_reached ();
315 }
316
317 #endif /* HAVE_QMI_SERVICE_FOX */