Debian Codegraph @

src/qmicli/qmicli-qos.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) 2018 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_QOS
37
38 #undef VALIDATE_UNKNOWN
39 #define VALIDATE_UNKNOWN(str) (str ? str : "unknown")
40
41 /* Context */
42 typedef struct {
43 QmiDevice *device;
44 QmiClientQos *client;
45 GCancellable *cancellable;
46 } Context;
47 static Context *ctx;
48
49 /* Options */
50 static gint get_flow_status_int = -1;
51 static gboolean get_network_status_flag;
52 static gint swi_read_data_stats_int = -1;
53 static gboolean reset_flag;
54 static gboolean noop_flag;
55
56 static GOptionEntry entries[] = {
57 #if defined HAVE_QMI_MESSAGE_QOS_GET_FLOW_STATUS
58 { "qos-get-flow-status", 0, 0, G_OPTION_ARG_INT, &get_flow_status_int,
59 "Get QoS flow status",
60 "[QoS ID]"
61 },
62 #endif
63 #if defined HAVE_QMI_MESSAGE_QOS_GET_NETWORK_STATUS
64 { "qos-get-network-status", 0, 0, G_OPTION_ARG_NONE, &get_network_status_flag,
65 "Gets the network status",
66 NULL
67 },
68 #endif
69 #if defined HAVE_QMI_MESSAGE_QOS_SWI_READ_DATA_STATS
70 { "qos-swi-read-data-stats", 0, 0, G_OPTION_ARG_INT, &swi_read_data_stats_int,
71 "Read data stats (Sierra Wireless specific)",
72 "[APN ID]"
73 },
74 #endif
75 #if defined HAVE_QMI_MESSAGE_QOS_RESET
76 { "qos-reset", 0, 0, G_OPTION_ARG_NONE, &reset_flag,
77 "Reset the service state",
78 NULL
79 },
80 #endif
81 { "qos-noop", 0, 0, G_OPTION_ARG_NONE, &noop_flag,
82 "Just allocate or release a QOS client. Use with `--client-no-release-cid' and/or `--client-cid'",
83 NULL
84 },
85 { NULL, 0, 0, 0, NULL, NULL, NULL }
86 };
87
88 GOptionGroup *
89 qmicli_qos_get_option_group (void)
90 {
91 GOptionGroup *group;
92
93 group = g_option_group_new ("qos",
94 "QoS options:",
95 "Show Quality of Service options",
96 NULL,
97 NULL);
98 g_option_group_add_entries (group, entries);
99
100 return group;
101 }
102
103 gboolean
104 qmicli_qos_options_enabled (void)
105 {
106 static guint n_actions = 0;
107 static gboolean checked = FALSE;
108
109 if (checked)
110 return !!n_actions;
111
112 n_actions = ((get_flow_status_int >= 0) +
113 get_network_status_flag +
114 (swi_read_data_stats_int >= 0) +
115 reset_flag +
116 noop_flag);
117
118 if (n_actions > 1) {
119 g_printerr ("error: too many QoS actions requested\n");
120 exit (EXIT_FAILURE);
121 }
122
123 checked = TRUE;
124 return !!n_actions;
125 }
126
127 static void
128 context_free (Context *context)
129 {
130 if (!context)
131 return;
132
133 if (context->cancellable)
134 g_object_unref (context->cancellable);
135 if (context->device)
136 g_object_unref (context->device);
137 if (context->client)
138 g_object_unref (context->client);
139 g_slice_free (Context, context);
140 }
141
142 static void
143 operation_shutdown (gboolean operation_status)
144 {
145 /* Cleanup context and finish async operation */
146 context_free (ctx);
147 qmicli_async_operation_done (operation_status, FALSE);
148 }
149
150 #if defined HAVE_QMI_MESSAGE_QOS_GET_FLOW_STATUS
151
152 static void
153 get_flow_status_ready (QmiClientQos *client,
154 GAsyncResult *res)
155 {
156 QmiMessageQosGetFlowStatusOutput *output;
157 GError *error = NULL;
158 QmiQosStatus flow_status;
159
160 output = qmi_client_qos_get_flow_status_finish (client, res, &error);
161 if (!output) {
162 g_printerr ("error: operation failed: %s\n", error->message);
163 g_error_free (error);
164 operation_shutdown (FALSE);
165 return;
166 }
167
168 if (!qmi_message_qos_get_flow_status_output_get_result (output, &error)) {
169 g_printerr ("error: couldn't get QoS flow status: %s\n", error->message);
170 g_error_free (error);
171 qmi_message_qos_get_flow_status_output_unref (output);
172 operation_shutdown (FALSE);
173 return;
174 }
175
176 qmi_message_qos_get_flow_status_output_get_value (output, &flow_status, NULL);
177
178 g_print ("[%s] QoS flow status: %s\n",
179 qmi_device_get_path_display (ctx->device),
180 qmi_qos_status_get_string (flow_status));
181
182 qmi_message_qos_get_flow_status_output_unref (output);
183 operation_shutdown (TRUE);
184 }
185
186 #endif /* HAVE_QMI_MESSAGE_QOS_GET_FLOW_STATUS */
187
188 #if defined HAVE_QMI_MESSAGE_QOS_GET_NETWORK_STATUS
189
190 static void
191 get_network_status_ready (QmiClientQos *client,
192 GAsyncResult *res)
193 {
194 QmiMessageQosGetNetworkStatusOutput *output;
195 GError *error = NULL;
196 gboolean qos_supported;
197
198 output = qmi_client_qos_get_network_status_finish (client, res, &error);
199 if (!output) {
200 g_printerr ("error: operation failed: %s\n", error->message);
201 g_error_free (error);
202 operation_shutdown (FALSE);
203 return;
204 }
205
206 if (!qmi_message_qos_get_network_status_output_get_result (output, &error)) {
207 g_printerr ("error: couldn't get network status: %s\n", error->message);
208 g_error_free (error);
209 qmi_message_qos_get_network_status_output_unref (output);
210 operation_shutdown (FALSE);
211 return;
212 }
213
214 qmi_message_qos_get_network_status_output_get_qos_supported (output, &qos_supported, NULL);
215
216 g_print ("[%s] QoS %ssupported in network\n",
217 qmi_device_get_path_display (ctx->device),
218 qos_supported ? "" : "not ");
219
220 qmi_message_qos_get_network_status_output_unref (output);
221 operation_shutdown (TRUE);
222 }
223
224 #endif /* HAVE_QMI_MESSAGE_QOS_GET_NETWORK_STATUS */
225
226 #if defined HAVE_QMI_MESSAGE_QOS_SWI_READ_DATA_STATS
227
228 static void
229 swi_read_data_stats_ready (QmiClientQos *client,
230 GAsyncResult *res)
231 {
232 QmiMessageQosSwiReadDataStatsOutput *output;
233 GError *error = NULL;
234 guint32 apn_id = 0;
235 guint32 apn_tx_packets = 0;
236 guint32 apn_tx_packets_dropped = 0;
237 guint32 apn_rx_packets = 0;
238 guint64 apn_tx_bytes = 0;
239 guint64 apn_tx_bytes_dropped = 0;
240 guint64 apn_rx_bytes = 0;
241 GArray *flow = NULL;
242
243 output = qmi_client_qos_swi_read_data_stats_finish (client, res, &error);
244 if (!output) {
245 g_printerr ("error: operation failed: %s\n", error->message);
246 g_error_free (error);
247 operation_shutdown (FALSE);
248 return;
249 }
250
251 if (!qmi_message_qos_swi_read_data_stats_output_get_result (output, &error)) {
252 g_printerr ("error: couldn't read data stats: %s\n", error->message);
253 g_error_free (error);
254 qmi_message_qos_swi_read_data_stats_output_unref (output);
255 operation_shutdown (FALSE);
256 return;
257 }
258
259 g_print ("[%s] QoS data stats read\n",
260 qmi_device_get_path_display (ctx->device));
261
262 if (qmi_message_qos_swi_read_data_stats_output_get_apn (
263 output,
264 &apn_id,
265 &apn_tx_packets,
266 &apn_tx_packets_dropped,
267 &apn_rx_packets,
268 &apn_tx_bytes,
269 &apn_tx_bytes_dropped,
270 &apn_rx_bytes,
271 NULL)) {
272 g_print (" APN ID: %u\n", apn_id);
273 g_print (" TX packets: %u\n", apn_tx_packets);
274 g_print (" TX packets dropped: %u\n", apn_tx_packets_dropped);
275 g_print (" RX packets: %u\n", apn_rx_packets);
276 g_print (" TX bytes: %" G_GUINT64_FORMAT "\n", apn_tx_bytes);
277 g_print (" TX bytes dropped: %" G_GUINT64_FORMAT "\n", apn_tx_bytes_dropped);
278 g_print (" RX bytes: %" G_GUINT64_FORMAT "\n", apn_rx_bytes);
279 }
280
281 if (qmi_message_qos_swi_read_data_stats_output_get_flow (
282 output,
283 &flow,
284 NULL)) {
285 guint i;
286
287 for (i = 0; i < flow->len; i++) {
288 QmiMessageQosSwiReadDataStatsOutputFlowElement *element;
289
290 element = &g_array_index(flow, QmiMessageQosSwiReadDataStatsOutputFlowElement, i);
291
292 g_print (" Flow %u\n", i);
293 g_print (" Bearer ID: %u\n", element->bearer_id);
294 g_print (" TX packets: %u\n", element->tx_packets);
295 g_print (" TX packets dropped: %u\n", element->tx_packets_dropped);
296 g_print (" TX bytes: %" G_GUINT64_FORMAT "\n", element->tx_bytes);
297 g_print (" TX bytes dropped: %" G_GUINT64_FORMAT "\n", element->tx_bytes_dropped);
298 }
299 }
300
301 qmi_message_qos_swi_read_data_stats_output_unref (output);
302 operation_shutdown (TRUE);
303 }
304
305 #endif /* HAVE_QMI_MESSAGE_QOS_SWI_READ_DATA_STATS */
306
307 #if defined HAVE_QMI_MESSAGE_QOS_RESET
308
309 static void
310 reset_ready (QmiClientQos *client,
311 GAsyncResult *res)
312 {
313 QmiMessageQosResetOutput *output;
314 GError *error = NULL;
315
316 output = qmi_client_qos_reset_finish (client, res, &error);
317 if (!output) {
318 g_printerr ("error: operation failed: %s\n", error->message);
319 g_error_free (error);
320 operation_shutdown (FALSE);
321 return;
322 }
323
324 if (!qmi_message_qos_reset_output_get_result (output, &error)) {
325 g_printerr ("error: couldn't reset the QoS service: %s\n", error->message);
326 g_error_free (error);
327 qmi_message_qos_reset_output_unref (output);
328 operation_shutdown (FALSE);
329 return;
330 }
331
332 g_print ("[%s] Successfully performed QoS service reset\n",
333 qmi_device_get_path_display (ctx->device));
334
335 qmi_message_qos_reset_output_unref (output);
336 operation_shutdown (TRUE);
337 }
338
339 #endif /* HAVE_QMI_MESSAGE_QOS_RESET */
340
341 static gboolean
342 noop_cb (gpointer unused)
343 {
344 operation_shutdown (TRUE);
345 return FALSE;
346 }
347
348 void
349 qmicli_qos_run (QmiDevice *device,
350 QmiClientQos *client,
351 GCancellable *cancellable)
352 {
353 /* Initialize context */
354 ctx = g_slice_new (Context);
355 ctx->device = g_object_ref (device);
356 ctx->client = g_object_ref (client);
357 ctx->cancellable = g_object_ref (cancellable);
358
359 #if defined HAVE_QMI_MESSAGE_QOS_GET_FLOW_STATUS
360 if (get_flow_status_int >= 0) {
361 QmiMessageQosGetFlowStatusInput *input;
362
363 input = qmi_message_qos_get_flow_status_input_new ();
364 qmi_message_qos_get_flow_status_input_set_qos_id (input, get_flow_status_int, NULL);
365 g_debug ("Asynchronously getting QoS flow status...");
366 qmi_client_qos_get_flow_status (ctx->client,
367 input,
368 10,
369 ctx->cancellable,
370 (GAsyncReadyCallback)get_flow_status_ready,
371 NULL);
372 qmi_message_qos_get_flow_status_input_unref (input);
373 return;
374 }
375 #endif
376
377 #if defined HAVE_QMI_MESSAGE_QOS_GET_NETWORK_STATUS
378 if (get_network_status_flag) {
379 g_debug ("Asynchronously getting network status...");
380 qmi_client_qos_get_network_status (ctx->client,
381 NULL,
382 10,
383 ctx->cancellable,
384 (GAsyncReadyCallback)get_network_status_ready,
385 NULL);
386 return;
387 }
388 #endif
389
390 #if defined HAVE_QMI_MESSAGE_QOS_SWI_READ_DATA_STATS
391 if (swi_read_data_stats_int >= 0) {
392 QmiMessageQosSwiReadDataStatsInput *input;
393
394 input = qmi_message_qos_swi_read_data_stats_input_new ();
395 qmi_message_qos_swi_read_data_stats_input_set_apn_id (input, swi_read_data_stats_int, NULL);
396 g_debug ("Asynchronously reading data stats...");
397 qmi_client_qos_swi_read_data_stats (ctx->client,
398 input,
399 10,
400 ctx->cancellable,
401 (GAsyncReadyCallback)swi_read_data_stats_ready,
402 NULL);
403 qmi_message_qos_swi_read_data_stats_input_unref (input);
404 return;
405 }
406 #endif
407
408 #if defined HAVE_QMI_MESSAGE_QOS_RESET
409 if (reset_flag) {
410 g_debug ("Asynchronously resetting QoS service...");
411 qmi_client_qos_reset (ctx->client,
412 NULL,
413 10,
414 ctx->cancellable,
415 (GAsyncReadyCallback)reset_ready,
416 NULL);
417 return;
418 }
419 #endif
420
421 /* Just client allocate/release? */
422 if (noop_flag) {
423 g_idle_add (noop_cb, NULL);
424 return;
425 }
426
427 g_warn_if_reached ();
428 }
429
430 #endif /* HAVE_QMI_SERVICE_QOS */