Debian Codegraph @

src/mbimcli/mbimcli-ms-voice-extensions.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) 2022 Intel Corporation
7 */
8
9 #include "config.h"
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <locale.h>
14 #include <string.h>
15 #include <errno.h>
16
17 #include <glib.h>
18 #include <gio/gio.h>
19
20 #include <libmbim-glib.h>
21
22 #include "mbim-common.h"
23 #include "mbimcli.h"
24 #include "mbimcli-helpers.h"
25
26 /* Context */
27 typedef struct {
28 MbimDevice *device;
29 GCancellable *cancellable;
30 } Context;
31 static Context *ctx;
32
33 /* Options */
34 static gboolean query_nitz_flag;
35
36 static GOptionEntry entries[] = {
37 { "ms-query-nitz", 0, 0, G_OPTION_ARG_NONE, &query_nitz_flag,
38 "Query network identity and time zone",
39 NULL
40 },
41 { NULL, 0, 0, 0, NULL, NULL, NULL }
42 };
43
44 GOptionGroup *
45 mbimcli_ms_voice_extensions_get_option_group (void)
46 {
47 GOptionGroup *group;
48
49 group = g_option_group_new ("ms-voice-extensions",
50 "Microsoft Voice Extensions Service options:",
51 "Show Microsoft Voice Extensions Service options",
52 NULL,
53 NULL);
54 g_option_group_add_entries (group, entries);
55
56 return group;
57 }
58
59 gboolean
60 mbimcli_ms_voice_extensions_options_enabled (void)
61 {
62 static guint n_actions = 0;
63 static gboolean checked = FALSE;
64
65 if (checked)
66 return !!n_actions;
67
68 n_actions = query_nitz_flag;
69
70 if (n_actions > 1) {
71 g_printerr ("error: too many Microsoft Voice Extensions Service actions requested\n");
72 exit (EXIT_FAILURE);
73 }
74
75 checked = TRUE;
76 return !!n_actions;
77 }
78
79 static void
80 context_free (Context *context)
81 {
82 if (!context)
83 return;
84
85 if (context->cancellable)
86 g_object_unref (context->cancellable);
87 if (context->device)
88 g_object_unref (context->device);
89 g_slice_free (Context, context);
90 }
91
92 static void
93 shutdown (gboolean operation_status)
94 {
95 /* Cleanup context and finish async operation */
96 context_free (ctx);
97 mbimcli_async_operation_done (operation_status);
98 }
99
100 static void
101 query_nitz_ready (MbimDevice *device,
102 GAsyncResult *res)
103 {
104 g_autoptr(MbimMessage) response = NULL;
105 g_autoptr(GError) error = NULL;
106 guint32 day = 0;
107 guint32 month = 0;
108 guint32 year = 0;
109 guint32 hour = 0;
110 guint32 minutes = 0;
111 guint32 second = 0;
112 guint32 time_zone_offset_minutes = 0;
113 guint32 daylight_saving_time_offset_minutes = 0;
114 MbimDataClass data_class;
115 g_autofree gchar *data_class_str = NULL;
116
117 response = mbim_device_command_finish (device, res, &error);
118 if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
119 g_printerr ("error: operation failed: %s\n", error->message);
120 shutdown (FALSE);
121 return;
122 }
123
124 if (!mbim_message_ms_voice_extensions_nitz_response_parse (
125 response,
126 &year,
127 &month,
128 &day,
129 &hour,
130 &minutes,
131 &second,
132 &time_zone_offset_minutes,
133 &daylight_saving_time_offset_minutes,
134 &data_class,
135 &error)) {
136 g_printerr ("error: couldn't parse response message: %s\n", error->message);
137 shutdown (FALSE);
138 return;
139 }
140
141 data_class_str = mbim_data_class_build_string_from_mask (data_class);
142
143 g_print ("Successfully queried NITZ info from modem:\n"
144 "\t Date: %02u/%02u/%u\n"
145 "\t Time: %02u:%02u:%02u\n"
146 "\t Time zone offset minutes: %u\n"
147 "\tDaylight saving time offset minutes: %u\n"
148 "\t Data class: %s\n",
149 day,
150 month,
151 year,
152 hour,
153 minutes,
154 second,
155 time_zone_offset_minutes,
156 daylight_saving_time_offset_minutes,
157 VALIDATE_UNKNOWN (data_class_str));
158
159 shutdown (TRUE);
160 }
161
162 void
163 mbimcli_ms_voice_extensions_run (MbimDevice *device,
164 GCancellable *cancellable)
165 {
166 g_autoptr(MbimMessage) request = NULL;
167
168 /* Initialize context */
169 ctx = g_slice_new (Context);
170 ctx->device = g_object_ref (device);
171 ctx->cancellable = cancellable ? g_object_ref (cancellable) : NULL;
172
173 /* Request to reboot modem? */
174 if (query_nitz_flag) {
175 g_debug ("Asynchronously querying nitz info...");
176 request = mbim_message_ms_voice_extensions_nitz_query_new (NULL);
177 mbim_device_command (ctx->device,
178 request,
179 10,
180 ctx->cancellable,
181 (GAsyncReadyCallback)query_nitz_ready,
182 NULL);
183 return;
184 }
185
186 g_warn_if_reached ();
187 }