Debian Codegraph @

src/mbimcli/mbimcli-atds.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) 2017 Red Hat, Inc.
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
23 /* Context */
24 typedef struct {
25 MbimDevice *device;
26 GCancellable *cancellable;
27 } Context;
28 static Context *ctx;
29
30 /* Options */
31 static gboolean query_signal_flag;
32 static gboolean query_location_flag;
33 static gboolean query_operators_flag;
34 static gboolean query_rat_flag;
35
36 static GOptionEntry entries[] = {
37 { "atds-query-signal", 0, 0, G_OPTION_ARG_NONE, &query_signal_flag,
38 "Query signal info",
39 NULL
40 },
41 { "atds-query-location", 0, 0, G_OPTION_ARG_NONE, &query_location_flag,
42 "Query cell location",
43 NULL
44 },
45 { "atds-query-operators", 0, 0, G_OPTION_ARG_NONE, &query_operators_flag,
46 "Query operators",
47 NULL
48 },
49 { "atds-query-rat", 0, 0, G_OPTION_ARG_NONE, &query_rat_flag,
50 "Query Radio Access Technology",
51 NULL
52 },
53 { NULL, 0, 0, 0, NULL, NULL, NULL }
54 };
55
56 GOptionGroup *
57 mbimcli_atds_get_option_group (void)
58 {
59 GOptionGroup *group;
60
61 group = g_option_group_new ("atds",
62 "AT&T Device Service options:",
63 "Show AT&T Device Service options",
64 NULL,
65 NULL);
66 g_option_group_add_entries (group, entries);
67
68 return group;
69 }
70
71 gboolean
72 mbimcli_atds_options_enabled (void)
73 {
74 static guint n_actions = 0;
75 static gboolean checked = FALSE;
76
77 if (checked)
78 return !!n_actions;
79
80 n_actions = (query_signal_flag +
81 query_location_flag +
82 query_operators_flag +
83 query_rat_flag);
84
85 if (n_actions > 1) {
86 g_printerr ("error: too many AT&T Device Service actions requested\n");
87 exit (EXIT_FAILURE);
88 }
89
90 checked = TRUE;
91 return !!n_actions;
92 }
93
94 static void
95 context_free (Context *context)
96 {
97 if (!context)
98 return;
99
100 if (context->cancellable)
101 g_object_unref (context->cancellable);
102 if (context->device)
103 g_object_unref (context->device);
104 g_slice_free (Context, context);
105 }
106
107 static void
108 shutdown (gboolean operation_status)
109 {
110 /* Cleanup context and finish async operation */
111 context_free (ctx);
112 mbimcli_async_operation_done (operation_status);
113 }
114
115 static void
116 query_signal_ready (MbimDevice *device,
117 GAsyncResult *res)
118 {
119 g_autoptr(MbimMessage) response = NULL;
120 g_autoptr(GError) error = NULL;
121 g_autofree gchar *rssi_str = NULL;
122 g_autofree gchar *error_rate_str = NULL;
123 g_autofree gchar *rscp_str = NULL;
124 g_autofree gchar *ecno_str = NULL;
125 g_autofree gchar *rsrq_str = NULL;
126 g_autofree gchar *rsrp_str = NULL;
127 g_autofree gchar *rssnr_str = NULL;
128 guint32 rssi = 0, error_rate = 0, rscp = 0, ecno = 0, rsrq = 0, rsrp = 0, rssnr = 0;
129
130 response = mbim_device_command_finish (device, res, &error);
131 if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
132 g_printerr ("error: operation failed: %s\n", error->message);
133 shutdown (FALSE);
134 return;
135 }
136
137 if (!mbim_message_atds_signal_response_parse (
138 response,
139 &rssi,
140 &error_rate,
141 &rscp,
142 &ecno,
143 &rsrq,
144 &rsrp,
145 &rssnr,
146 &error)) {
147 g_printerr ("error: couldn't parse response message: %s\n", error->message);
148 shutdown (FALSE);
149 return;
150 }
151
152 if (rssi <= 31)
153 rssi_str = g_strdup_printf ("%d dBm", -113 + (2 * rssi));
154
155 switch (error_rate) {
156 case 0:
157 error_rate_str = g_strdup_printf ("< 0.2%%");
158 break;
159 case 1:
160 error_rate_str = g_strdup_printf ("0.2%% - 0.39%%");
161 break;
162 case 2:
163 error_rate_str = g_strdup_printf ("0.4%% - 0.79%%");
164 break;
165 case 3:
166 error_rate_str = g_strdup_printf ("0.8%% - 1.59%%");
167 break;
168 case 4:
169 error_rate_str = g_strdup_printf ("1.6%% - 3.19%%");
170 break;
171 case 5:
172 error_rate_str = g_strdup_printf ("3.2%% - 6.39%%");
173 break;
174 case 6:
175 error_rate_str = g_strdup_printf ("6.4%% - 12.79%%");
176 break;
177 case 7:
178 error_rate_str = g_strdup_printf ("> 12.8%%");
179 break;
180 default:
181 error_rate_str = g_strdup_printf ("unknown (%u)", error_rate);
182 break;
183 }
184
185 if (rscp == 0)
186 rscp_str = g_strdup_printf ("< -120 dBm");
187 else if (rscp < 96)
188 rscp_str = g_strdup_printf ("%d dBm", -120 + rscp);
189 else if (rscp == 96)
190 rscp_str = g_strdup_printf (">= -24 dBm");
191
192 if (ecno == 0)
193 ecno_str = g_strdup_printf ("< -24 dBm");
194 else if (ecno < 49)
195 ecno_str = g_strdup_printf ("%.2lf dBm", -24.0 + ((gdouble)ecno / 2));
196 else if (ecno == 49)
197 ecno_str = g_strdup_printf (">= 0.5 dBm");
198
199 if (rsrq == 0)
200 rsrq_str = g_strdup_printf ("< -19.5 dBm");
201 else if (rsrq < 34)
202 rsrq_str = g_strdup_printf ("%.2lf dBm", -19.5 + ((gdouble)rsrq / 2));
203 else if (rsrq == 34)
204 rsrq_str = g_strdup_printf (">= -2.5 dBm");
205
206 if (rsrp == 0)
207 rsrp_str = g_strdup_printf ("< -140 dBm");
208 else if (rsrp < 97)
209 rsrp_str = g_strdup_printf ("%d dBm", -140 + rsrp);
210 else if (rsrp == 97)
211 rsrp_str = g_strdup_printf (">= -43 dBm");
212
213 if (rssnr == 0)
214 rssnr_str = g_strdup_printf ("< -5 dB");
215 else if (rssnr < 35)
216 rssnr_str = g_strdup_printf ("%d dB", -5 + rssnr);
217 else if (rssnr == 35)
218 rssnr_str = g_strdup_printf (">= 30 dB");
219
220 g_print ("[%s] Signal info retrieved:\n"
221 "\t RSSI: %s\n"
222 "\t BER: %s\n"
223 "\t RSCP: %s\n"
224 "\t Ec/No: %s\n"
225 "\t RSRQ: %s\n"
226 "\t RSRP: %s\n"
227 "\t RSSNR: %s\n",
228 mbim_device_get_path_display (device),
229 VALIDATE_UNKNOWN (rssi_str),
230 VALIDATE_UNKNOWN (error_rate_str),
231 VALIDATE_UNKNOWN (rscp_str),
232 VALIDATE_UNKNOWN (ecno_str),
233 VALIDATE_UNKNOWN (rsrq_str),
234 VALIDATE_UNKNOWN (rsrp_str),
235 VALIDATE_UNKNOWN (rssnr_str));
236
237 shutdown (TRUE);
238 }
239
240 static void
241 query_location_ready (MbimDevice *device,
242 GAsyncResult *res)
243 {
244 g_autoptr(MbimMessage) response = NULL;
245 g_autoptr(GError) error = NULL;
246 guint32 lac = 0, tac = 0, cellid = 0;
247
248 response = mbim_device_command_finish (device, res, &error);
249 if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
250 g_printerr ("error: operation failed: %s\n", error->message);
251 shutdown (FALSE);
252 return;
253 }
254
255 if (!mbim_message_atds_location_response_parse (
256 response,
257 &lac,
258 &tac,
259 &cellid,
260 &error)) {
261 g_printerr ("error: couldn't parse response message: %s\n", error->message);
262 shutdown (FALSE);
263 return;
264 }
265
266 g_print ("[%s] Cell location retrieved:\n"
267 "\t LAC: %04x\n"
268 "\t TAC: %04x\n"
269 "\t Cell ID: %04x\n",
270 mbim_device_get_path_display (device),
271 lac,
272 tac,
273 cellid);
274
275 shutdown (TRUE);
276 }
277
278 static void
279 query_operators_ready (MbimDevice *device,
280 GAsyncResult *res)
281 {
282 g_autoptr(MbimMessage) response = NULL;
283 g_autoptr(GError) error = NULL;
284 guint32 n_operators = 0;
285 g_autoptr(MbimAtdsProviderArray) operators = NULL;
286 guint i;
287
288 response = mbim_device_command_finish (device, res, &error);
289 if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
290 g_printerr ("error: operation failed: %s\n", error->message);
291 shutdown (FALSE);
292 return;
293 }
294
295 if (!mbim_message_atds_operators_response_parse (
296 response,
297 &n_operators,
298 &operators,
299 &error)) {
300 g_printerr ("error: couldn't parse response message: %s\n", error->message);
301 shutdown (FALSE);
302 return;
303 }
304
305 if (!n_operators)
306 g_print ("[%s] No operators given\n", mbim_device_get_path_display (device));
307 else
308 g_print ("[%s] Operators (%u):\n", mbim_device_get_path_display (device), n_operators);
309
310 for (i = 0; i < n_operators; i++) {
311 g_autofree gchar *provider_state_str = NULL;
312 const gchar *plmn_mode_str = NULL;
313
314 provider_state_str = mbim_provider_state_build_string_from_mask (operators[i]->provider_state);
315 plmn_mode_str = mbim_atds_provider_plmn_mode_get_string (operators[i]->plmn_mode);
316
317 g_print ("\tOperator [%u]:\n"
318 "\t\t Provider ID: '%s'\n"
319 "\t\t Provider name: '%s'\n"
320 "\t\t State: '%s'\n"
321 "\t\t Mode: '%s'\n"
322 "\t\t RSSI: '%u'\n"
323 "\t\t Error rate: '%u'\n",
324 i,
325 VALIDATE_UNKNOWN (operators[i]->provider_id),
326 VALIDATE_UNKNOWN (operators[i]->provider_name),
327 VALIDATE_UNKNOWN (provider_state_str),
328 VALIDATE_UNKNOWN (plmn_mode_str),
329 operators[i]->rssi,
330 operators[i]->error_rate);
331 }
332
333 shutdown (TRUE);
334 }
335
336 static void
337 query_rat_ready (MbimDevice *device,
338 GAsyncResult *res)
339 {
340 g_autoptr(MbimMessage) response = NULL;
341 g_autoptr(GError) error = NULL;
342 MbimAtdsRatMode rat = MBIM_ATDS_RAT_MODE_AUTOMATIC;
343
344 response = mbim_device_command_finish (device, res, &error);
345 if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
346 g_printerr ("error: operation failed: %s\n", error->message);
347 shutdown (FALSE);
348 return;
349 }
350
351 if (!mbim_message_atds_rat_response_parse (
352 response,
353 &rat,
354 &error)) {
355 g_printerr ("error: couldn't parse response message: %s\n", error->message);
356 shutdown (FALSE);
357 return;
358 }
359
360 g_print ("[%s] RAT mode retrieved:\n"
361 "\t Mode: '%s'\n",
362 mbim_device_get_path_display (device),
363 mbim_atds_rat_mode_get_string (rat));
364
365 shutdown (TRUE);
366 }
367
368 void
369 mbimcli_atds_run (MbimDevice *device,
370 GCancellable *cancellable)
371 {
372 g_autoptr(MbimMessage) request = NULL;
373
374 /* Initialize context */
375 ctx = g_slice_new (Context);
376 ctx->device = g_object_ref (device);
377 ctx->cancellable = cancellable ? g_object_ref (cancellable) : NULL;
378
379 /* Request to get signal info? */
380 if (query_signal_flag) {
381 g_debug ("Asynchronously querying signal info...");
382 request = (mbim_message_atds_signal_query_new (NULL));
383 mbim_device_command (ctx->device,
384 request,
385 10,
386 ctx->cancellable,
387 (GAsyncReadyCallback)query_signal_ready,
388 NULL);
389 return;
390 }
391
392 /* Request to get cell location? */
393 if (query_location_flag) {
394 g_debug ("Asynchronously querying cell location...");
395 request = (mbim_message_atds_location_query_new (NULL));
396 mbim_device_command (ctx->device,
397 request,
398 10,
399 ctx->cancellable,
400 (GAsyncReadyCallback)query_location_ready,
401 NULL);
402 return;
403 }
404
405 /* Request to get operators? */
406 if (query_operators_flag) {
407 g_debug ("Asynchronously querying operators...");
408 request = (mbim_message_atds_operators_query_new (NULL));
409 mbim_device_command (ctx->device,
410 request,
411 240, /* longer timeout, needs to scan */
412 ctx->cancellable,
413 (GAsyncReadyCallback)query_operators_ready,
414 NULL);
415 return;
416 }
417
418 /* Request to get RAT? */
419 if (query_rat_flag) {
420 g_debug ("Asynchronously querying RAT...");
421 request = (mbim_message_atds_rat_query_new (NULL));
422 mbim_device_command (ctx->device,
423 request,
424 10,
425 ctx->cancellable,
426 (GAsyncReadyCallback)query_rat_ready,
427 NULL);
428 return;
429 }
430
431 g_warn_if_reached ();
432 }