Debian Codegraph @

src/qmicli/qmicli-dpm.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) 2021 Aleksander Morgado <[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
28 #include <glib.h>
29 #include <gio/gio.h>
30
31 #include <libqmi-glib.h>
32
33 #include "qmicli.h"
34 #include "qmicli-helpers.h"
35
36 #if defined HAVE_QMI_SERVICE_DPM
37
38 #define QMI_ENDPOINT_INTERFACE_NUMBER_UNDEFINED -1
39
40 /* Context */
41 typedef struct {
42 QmiDevice *device;
43 QmiClientDpm *client;
44 GCancellable *cancellable;
45 } Context;
46 static Context *ctx;
47
48 /* Options */
49 static gchar *open_port_str;
50 static gboolean close_port_flag;
51 static gboolean noop_flag;
52
53 static GOptionEntry entries[] = {
54 #if defined HAVE_QMI_MESSAGE_DPM_OPEN_PORT
55 { "dpm-open-port", 0, 0, G_OPTION_ARG_STRING, &open_port_str,
56 "Open port (allowed-keys: ctrl-ep-type, ctrl-ep-iface-number, ctrl-port-name, hw-data-ep-type, hw-data-ep-iface-number, hw-data-rx-id, hw-data-tx-id, sw-data-ep-type, sw-data-ep-iface-number, sw-data-port-name)",
57 "[\"key=value,...\"]"
58 },
59 #endif
60 #if defined HAVE_QMI_MESSAGE_DPM_CLOSE_PORT
61 { "dpm-close-port", 0, 0, G_OPTION_ARG_NONE, &close_port_flag,
62 "Close port",
63 NULL
64 },
65 #endif
66 { "dpm-noop", 0, 0, G_OPTION_ARG_NONE, &noop_flag,
67 "Just allocate or release a DPM client. Use with `--client-no-release-cid' and/or `--client-cid'",
68 NULL
69 },
70 { NULL, 0, 0, 0, NULL, NULL, NULL }
71 };
72
73 GOptionGroup *
74 qmicli_dpm_get_option_group (void)
75 {
76 GOptionGroup *group;
77
78 group = g_option_group_new ("dpm",
79 "DPM options:",
80 "Show Data Port Mapper Service options",
81 NULL,
82 NULL);
83 g_option_group_add_entries (group, entries);
84
85 return group;
86 }
87
88 gboolean
89 qmicli_dpm_options_enabled (void)
90 {
91 static guint n_actions = 0;
92 static gboolean checked = FALSE;
93
94 if (checked)
95 return !!n_actions;
96
97 n_actions = (!!open_port_str +
98 close_port_flag +
99 noop_flag);
100
101 if (n_actions > 1) {
102 g_printerr ("error: too many DPM actions requested\n");
103 exit (EXIT_FAILURE);
104 }
105
106 checked = TRUE;
107 return !!n_actions;
108 }
109
110 static void
111 context_free (Context *context)
112 {
113 if (!context)
114 return;
115
116 if (context->cancellable)
117 g_object_unref (context->cancellable);
118 if (context->device)
119 g_object_unref (context->device);
120 if (context->client)
121 g_object_unref (context->client);
122 g_slice_free (Context, context);
123 }
124
125 static void
126 operation_shutdown (gboolean operation_status)
127 {
128 /* Cleanup context and finish async operation */
129 context_free (ctx);
130 qmicli_async_operation_done (operation_status, FALSE);
131 }
132
133 #if defined HAVE_QMI_MESSAGE_DPM_OPEN_PORT
134
135 static void
136 open_port_ready (QmiClientDpm *client,
137 GAsyncResult *res)
138 {
139 g_autoptr(QmiMessageDpmOpenPortOutput) output = NULL;
140 g_autoptr(GError) error = NULL;
141
142 output = qmi_client_dpm_open_port_finish (client, res, &error);
143 if (!output) {
144 g_printerr ("error: operation failed: %s\n", error->message);
145 operation_shutdown (FALSE);
146 return;
147 }
148
149 if (!qmi_message_dpm_open_port_output_get_result (output, &error)) {
150 g_printerr ("error: couldn't open port: %s\n", error->message);
151 operation_shutdown (FALSE);
152 return;
153 }
154
155 g_print ("Successfully opened the port\n");
156 operation_shutdown (TRUE);
157 }
158
159 typedef struct {
160 /* control port item building */
161 GArray *ctrl_ports;
162 QmiDataEndpointType ctrl_ep_type;
163 gint ctrl_ep_iface_number;
164 gchar *ctrl_port_name;
165
166 /* hw data port item building */
167 GArray *hw_data_ports;
168 QmiDataEndpointType hw_data_ep_type;
169 gint hw_data_ep_iface_number;
170 guint hw_data_rx_id;
171 guint hw_data_tx_id;
172
173 /* sw data port item building */
174 GArray *sw_data_ports;
175 QmiDataEndpointType sw_data_ep_type;
176 gint sw_data_ep_iface_number;
177 gchar *sw_data_port_name;
178 } OpenPortProperties;
179
180 static void
181 reset_ctrl_port_item (OpenPortProperties *props)
182 {
183 g_free (props->ctrl_port_name);
184 props->ctrl_port_name = NULL;
185 props->ctrl_ep_type = QMI_DATA_ENDPOINT_TYPE_UNKNOWN;
186 props->ctrl_ep_iface_number = QMI_ENDPOINT_INTERFACE_NUMBER_UNDEFINED;
187 }
188
189 static void
190 ctrl_port_details_clear (QmiMessageDpmOpenPortInputControlPortsElement *details)
191 {
192 g_free (details->port_name);
193 }
194
195 static void
196 build_ctrl_port_item (OpenPortProperties *props)
197 {
198 if ((props->ctrl_ep_type != QMI_DATA_ENDPOINT_TYPE_UNKNOWN) &&
199 (props->ctrl_ep_iface_number != QMI_ENDPOINT_INTERFACE_NUMBER_UNDEFINED) &&
200 (props->ctrl_port_name != NULL)) {
201 QmiMessageDpmOpenPortInputControlPortsElement details;
202
203 details.port_name = g_strdup (props->ctrl_port_name);
204 details.endpoint_type = props->ctrl_ep_type;
205 details.interface_number = props->ctrl_ep_iface_number;
206
207 if (!props->ctrl_ports) {
208 props->ctrl_ports = g_array_new (FALSE, FALSE, sizeof (QmiMessageDpmOpenPortInputControlPortsElement));
209 g_array_set_clear_func (props->ctrl_ports, (GDestroyNotify)ctrl_port_details_clear);
210 }
211
212 g_array_append_val (props->ctrl_ports, details);
213 reset_ctrl_port_item (props);
214 }
215 }
216
217 static gboolean
218 check_unfinished_ctrl_port_item (OpenPortProperties *props,
219 GError **error)
220 {
221 if ((props->ctrl_ep_type != QMI_DATA_ENDPOINT_TYPE_UNKNOWN) ||
222 (props->ctrl_ep_iface_number != QMI_ENDPOINT_INTERFACE_NUMBER_UNDEFINED) ||
223 (props->ctrl_port_name != NULL)) {
224 g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED,
225 "error: unfinished ctrl port item");
226 return FALSE;
227 }
228 return TRUE;
229 }
230
231 static void
232 reset_hw_data_port_item (OpenPortProperties *props)
233 {
234 props->hw_data_rx_id = 0;
235 props->hw_data_tx_id = 0;
236 props->hw_data_ep_type = QMI_DATA_ENDPOINT_TYPE_UNKNOWN;
237 props->hw_data_ep_iface_number = QMI_ENDPOINT_INTERFACE_NUMBER_UNDEFINED;
238 }
239
240 static void
241 build_hw_data_port_item (OpenPortProperties *props)
242 {
243 if ((props->hw_data_ep_type != QMI_DATA_ENDPOINT_TYPE_UNKNOWN) &&
244 (props->hw_data_ep_iface_number != QMI_ENDPOINT_INTERFACE_NUMBER_UNDEFINED) &&
245 (props->hw_data_rx_id != 0) &&
246 (props->hw_data_tx_id != 0)) {
247 QmiMessageDpmOpenPortInputHardwareDataPortsElement details;
248
249 details.rx_endpoint_number = props->hw_data_rx_id;
250 details.tx_endpoint_number = props->hw_data_tx_id;
251 details.endpoint_type = props->hw_data_ep_type;
252 details.interface_number = props->hw_data_ep_iface_number;
253
254 if (!props->hw_data_ports)
255 props->hw_data_ports = g_array_new (FALSE, FALSE, sizeof (QmiMessageDpmOpenPortInputHardwareDataPortsElement));
256
257 g_array_append_val (props->hw_data_ports, details);
258 reset_hw_data_port_item (props);
259 }
260 }
261
262 static gboolean
263 check_unfinished_hw_data_port_item (OpenPortProperties *props,
264 GError **error)
265 {
266 if ((props->hw_data_ep_type != QMI_DATA_ENDPOINT_TYPE_UNKNOWN) ||
267 (props->hw_data_ep_iface_number != QMI_ENDPOINT_INTERFACE_NUMBER_UNDEFINED) ||
268 (props->hw_data_rx_id != 0) ||
269 (props->hw_data_tx_id != 0)) {
270 g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED,
271 "error: unfinished hw data port item");
272 return FALSE;
273 }
274 return TRUE;
275 }
276
277 static void
278 reset_sw_data_port_item (OpenPortProperties *props)
279 {
280 g_free (props->sw_data_port_name);
281 props->sw_data_port_name = NULL;
282 props->sw_data_ep_type = QMI_DATA_ENDPOINT_TYPE_UNKNOWN;
283 props->sw_data_ep_iface_number = QMI_ENDPOINT_INTERFACE_NUMBER_UNDEFINED;
284 }
285
286 static void
287 sw_data_port_details_clear (QmiMessageDpmOpenPortInputSoftwareDataPortsElement *details)
288 {
289 g_free (details->port_name);
290 }
291
292 static void
293 build_sw_data_port_item (OpenPortProperties *props)
294 {
295 if ((props->sw_data_ep_type != QMI_DATA_ENDPOINT_TYPE_UNKNOWN) &&
296 (props->sw_data_ep_iface_number != QMI_ENDPOINT_INTERFACE_NUMBER_UNDEFINED) &&
297 (props->sw_data_port_name != NULL)) {
298 QmiMessageDpmOpenPortInputSoftwareDataPortsElement details;
299
300 details.port_name = g_strdup (props->sw_data_port_name);
301 details.endpoint_type = props->sw_data_ep_type;
302 details.interface_number = props->sw_data_ep_iface_number;
303
304 if (!props->sw_data_ports) {
305 props->sw_data_ports = g_array_new (FALSE, FALSE, sizeof (QmiMessageDpmOpenPortInputSoftwareDataPortsElement));
306 g_array_set_clear_func (props->sw_data_ports, (GDestroyNotify)sw_data_port_details_clear);
307 }
308
309 g_array_append_val (props->sw_data_ports, details);
310 reset_sw_data_port_item (props);
311 }
312 }
313
314 static gboolean
315 check_unfinished_sw_data_port_item (OpenPortProperties *props,
316 GError **error)
317 {
318 if ((props->sw_data_ep_type != QMI_DATA_ENDPOINT_TYPE_UNKNOWN) ||
319 (props->sw_data_ep_iface_number != QMI_ENDPOINT_INTERFACE_NUMBER_UNDEFINED) ||
320 (props->sw_data_port_name != NULL)) {
321 g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED,
322 "error: unfinished sw data port item");
323 return FALSE;
324 }
325 return TRUE;
326 }
327
328 static gboolean
329 open_port_properties_handle (const gchar *key,
330 const gchar *value,
331 GError **error,
332 gpointer user_data)
333 {
334 OpenPortProperties *props = (OpenPortProperties *)user_data;
335
336 if (!value || !value[0]) {
337 g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED,
338 "key '%s' requires a value", key);
339 return FALSE;
340 }
341
342 /* control port item */
343 if (g_ascii_strcasecmp (key, "ctrl-ep-type") == 0) {
344 if (!qmicli_read_data_endpoint_type_from_string (value, &(props->ctrl_ep_type))) {
345 g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED,
346 "Unrecognized Endpoint Type '%s'", value);
347 return FALSE;
348 }
349 build_ctrl_port_item (props);
350 return TRUE;
351 }
352 if (g_ascii_strcasecmp (key, "ctrl-ep-iface-number") == 0) {
353 props->ctrl_ep_iface_number = atoi (value);
354 build_ctrl_port_item (props);
355 return TRUE;
356 }
357 if (g_ascii_strcasecmp (key, "ctrl-port-name") == 0) {
358 g_clear_pointer (&props->ctrl_port_name, g_free);
359 props->ctrl_port_name = g_strdup (value);
360 build_ctrl_port_item (props);
361 return TRUE;
362 }
363
364 /* hw data port item */
365 if (g_ascii_strcasecmp (key, "hw-data-ep-type") == 0) {
366 if (!qmicli_read_data_endpoint_type_from_string (value, &(props->hw_data_ep_type))) {
367 g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED,
368 "Unrecognized Endpoint Type '%s'", value);
369 return FALSE;
370 }
371 build_hw_data_port_item (props);
372 return TRUE;
373 }
374 if (g_ascii_strcasecmp (key, "hw-data-ep-iface-number") == 0) {
375 props->hw_data_ep_iface_number = atoi (value);
376 build_hw_data_port_item (props);
377 return TRUE;
378 }
379 if (g_ascii_strcasecmp (key, "hw-data-rx-id") == 0) {
380 props->hw_data_rx_id = atoi (value);
381 build_hw_data_port_item (props);
382 return TRUE;
383 }
384 if (g_ascii_strcasecmp (key, "hw-data-tx-id") == 0) {
385 props->hw_data_tx_id = atoi (value);
386 build_hw_data_port_item (props);
387 return TRUE;
388 }
389
390 /* sw data port item */
391 if (g_ascii_strcasecmp (key, "sw-data-ep-type") == 0) {
392 if (!qmicli_read_data_endpoint_type_from_string (value, &(props->sw_data_ep_type))) {
393 g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED,
394 "Unrecognized Endpoint Type '%s'", value);
395 return FALSE;
396 }
397 build_sw_data_port_item (props);
398 return TRUE;
399 }
400 if (g_ascii_strcasecmp (key, "sw-data-ep-iface-number") == 0) {
401 props->sw_data_ep_iface_number = atoi (value);
402 build_sw_data_port_item (props);
403 return TRUE;
404 }
405 if (g_ascii_strcasecmp (key, "sw-data-port-name") == 0) {
406 g_clear_pointer (&props->sw_data_port_name, g_free);
407 props->sw_data_port_name = g_strdup (value);
408 build_sw_data_port_item (props);
409 return TRUE;
410 }
411
412 g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED,
413 "Unrecognized option '%s'", key);
414 return FALSE;
415 }
416
417 static gboolean
418 open_port_input_create (const gchar *str,
419 QmiMessageDpmOpenPortInput **out_input,
420 GError **error)
421 {
422 QmiMessageDpmOpenPortInput *input;
423 OpenPortProperties props = { 0 };
424
425 reset_ctrl_port_item (&props);
426 reset_hw_data_port_item (&props);
427 reset_sw_data_port_item (&props);
428
429 if (!qmicli_parse_key_value_string (str, error, open_port_properties_handle, &props))
430 return FALSE;
431
432 if (!check_unfinished_ctrl_port_item (&props, error) ||
433 !check_unfinished_hw_data_port_item (&props, error) ||
434 !check_unfinished_sw_data_port_item (&props, error))
435 return FALSE;
436
437 input = qmi_message_dpm_open_port_input_new ();
438 if (props.ctrl_ports) {
439 qmi_message_dpm_open_port_input_set_control_ports (input, props.ctrl_ports, NULL);
440 g_clear_pointer (&props.ctrl_ports, g_array_unref);
441 }
442 if (props.hw_data_ports) {
443 qmi_message_dpm_open_port_input_set_hardware_data_ports (input, props.hw_data_ports, NULL);
444 g_clear_pointer (&props.hw_data_ports, g_array_unref);
445 }
446 if (props.sw_data_ports) {
447 qmi_message_dpm_open_port_input_set_software_data_ports (input, props.sw_data_ports, NULL);
448 g_clear_pointer (&props.sw_data_ports, g_array_unref);
449 }
450
451 *out_input = input;
452 return TRUE;
453 }
454
455 #endif /* HAVE_QMI_MESSAGE_DPM_OPEN_PORT */
456
457 #if defined HAVE_QMI_MESSAGE_DPM_CLOSE_PORT
458
459 static void
460 close_port_ready (QmiClientDpm *client,
461 GAsyncResult *res)
462 {
463 g_autoptr(QmiMessageDpmClosePortOutput) output = NULL;
464 g_autoptr(GError) error = NULL;
465
466 output = qmi_client_dpm_close_port_finish (client, res, &error);
467 if (!output) {
468 g_printerr ("error: operation failed: %s\n", error->message);
469 operation_shutdown (FALSE);
470 return;
471 }
472
473 if (!qmi_message_dpm_close_port_output_get_result (output, &error)) {
474 g_printerr ("error: couldn't close port: %s\n", error->message);
475 operation_shutdown (FALSE);
476 return;
477 }
478
479 g_print ("Successfully closeed the port\n");
480 operation_shutdown (TRUE);
481 }
482
483 #endif /* HAVE_QMI_MESSAGE_DPM_CLOSE_PORT */
484
485 static gboolean
486 noop_cb (gpointer unused)
487 {
488 operation_shutdown (TRUE);
489 return FALSE;
490 }
491
492 void
493 qmicli_dpm_run (QmiDevice *device,
494 QmiClientDpm *client,
495 GCancellable *cancellable)
496 {
497 /* Initialize context */
498 ctx = g_slice_new (Context);
499 ctx->device = g_object_ref (device);
500 ctx->client = g_object_ref (client);
501 ctx->cancellable = g_object_ref (cancellable);
502
503 #if defined HAVE_QMI_MESSAGE_DPM_OPEN_PORT
504 if (open_port_str) {
505 g_autoptr(QmiMessageDpmOpenPortInput) input = NULL;
506 g_autoptr(GError) error = NULL;
507
508 if (!open_port_input_create (open_port_str, &input, &error)) {
509 g_printerr ("error: couldn't process input arguments: %s\n", error->message);
510 operation_shutdown (FALSE);
511 return;
512 }
513 qmi_client_dpm_open_port (ctx->client,
514 input,
515 10,
516 ctx->cancellable,
517 (GAsyncReadyCallback)open_port_ready,
518 NULL);
519 return;
520 }
521 #endif
522
523 #if defined HAVE_QMI_MESSAGE_DPM_CLOSE_PORT
524 if (close_port_flag) {
525 qmi_client_dpm_close_port (ctx->client,
526 NULL,
527 10,
528 ctx->cancellable,
529 (GAsyncReadyCallback)close_port_ready,
530 NULL);
531 return;
532 }
533 #endif
534
535 /* Just client allocate/release? */
536 if (noop_flag) {
537 g_idle_add (noop_cb, NULL);
538 return;
539 }
540
541 g_warn_if_reached ();
542 }
543
544 #endif /* HAVE_QMI_SERVICE_DPM */