Debian Codegraph @

src/qmicli/qmicli-ims.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) 2023 Dylan Van Assche <[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_IMS
38
39 /* Context */
40 typedef struct {
41 QmiDevice *device;
42 QmiClientIms *client;
43 GCancellable *cancellable;
44 } Context;
45 static Context *ctx;
46
47 /* Options */
48 static gint bind_flag = -1;
49 static gboolean get_services_enabled_flag;
50 static gboolean noop_flag;
51
52 static GOptionEntry entries[] = {
53 #if defined HAVE_QMI_MESSAGE_IMS_BIND
54 { "ims-bind", 0, 0, G_OPTION_ARG_INT, &bind_flag,
55 "Bind to IMS Settings (use with --client-no-release-cid)",
56 "[binding]"
57 },
58 #endif
59 #if defined HAVE_QMI_MESSAGE_IMS_GET_IMS_SERVICES_ENABLED_SETTING
60 { "ims-get-ims-services-enabled-setting", 0, 0, G_OPTION_ARG_NONE, &get_services_enabled_flag,
61 "Get IMS Services Enabled Setting",
62 NULL
63 },
64 #endif
65 { "ims-noop", 0, 0, G_OPTION_ARG_NONE, &noop_flag,
66 "Just allocate or release a IMS client. Use with `--client-no-release-cid' and/or `--client-cid'",
67 NULL
68 },
69 { NULL }
70 };
71
72 GOptionGroup *
73 qmicli_ims_get_option_group (void)
74 {
75 GOptionGroup *group;
76
77 group = g_option_group_new ("ims",
78 "IMS options:",
79 "Show IP Multimedia Subsystem Settings Service options",
80 NULL,
81 NULL);
82 g_option_group_add_entries (group, entries);
83
84 return group;
85 }
86
87 gboolean
88 qmicli_ims_options_enabled (void)
89 {
90 static guint n_actions = 0;
91 static gboolean checked = FALSE;
92
93 if (checked)
94 return !!n_actions;
95
96 n_actions = ((bind_flag >= 0) +
97 get_services_enabled_flag +
98 noop_flag);
99
100 if (n_actions > 1) {
101 g_printerr ("error: too many IMS actions requested\n");
102 exit (EXIT_FAILURE);
103 }
104
105 checked = TRUE;
106 return !!n_actions;
107 }
108
109 static void
110 context_free (Context *context)
111 {
112 if (!context)
113 return;
114
115 if (context->cancellable)
116 g_object_unref (context->cancellable);
117 if (context->device)
118 g_object_unref (context->device);
119 if (context->client)
120 g_object_unref (context->client);
121 g_slice_free (Context, context);
122 }
123
124 static void
125 operation_shutdown (gboolean operation_status)
126 {
127 /* Cleanup context and finish async operation */
128 context_free (ctx);
129 qmicli_async_operation_done (operation_status, FALSE);
130 }
131
132 #if defined HAVE_QMI_MESSAGE_IMS_BIND
133
134 static void
135 bind_ready (QmiClientIms *client,
136 GAsyncResult *res)
137 {
138 QmiMessageImsBindOutput *output;
139 g_autoptr(GError) error = NULL;
140
141 output = qmi_client_ims_bind_finish (client, res, &error);
142 if (!output) {
143 g_printerr ("error: operation failed: %s\n", error->message);
144 operation_shutdown (FALSE);
145 return;
146 }
147
148 if (!qmi_message_ims_bind_output_get_result (output, &error)) {
149 g_printerr ("error: couldn't bind to IMS Settings: %s\n", error->message);
150 qmi_message_ims_bind_output_unref (output);
151 operation_shutdown (FALSE);
152 return;
153 }
154
155 g_print ("[%s] IMS Settings bind successful\n", qmi_device_get_path_display (ctx->device));
156
157 qmi_message_ims_bind_output_unref (output);
158 operation_shutdown (TRUE);
159 }
160
161 #endif /* HAVE_QMI_MESSAGE_IMS_BIND */
162
163 #if defined HAVE_QMI_MESSAGE_IMS_GET_IMS_SERVICES_ENABLED_SETTING
164
165 static void
166 get_services_enabled_ready (QmiClientIms *client,
167 GAsyncResult *res)
168 {
169 QmiMessageImsGetImsServicesEnabledSettingOutput *output;
170 gboolean service_voice_enabled;
171 gboolean service_vt_enabled;
172 gboolean service_voice_wifi_enabled;
173 gboolean service_ims_registration_enabled;
174 gboolean service_ut_enabled;
175 gboolean service_sms_enabled;
176 gboolean service_ussd_enabled;
177 GError *error = NULL;
178
179 output = qmi_client_ims_get_ims_services_enabled_setting_finish (client, res, &error);
180 if (!output) {
181 g_printerr ("error: operation failed: %s\n", error->message);
182 g_error_free (error);
183 operation_shutdown (FALSE);
184 return;
185 }
186
187 if (!qmi_message_ims_get_ims_services_enabled_setting_output_get_result (output, &error)) {
188 g_printerr ("error: couldn't get IMS services enabled setting: %s\n", error->message);
189 g_error_free (error);
190 qmi_message_ims_get_ims_services_enabled_setting_output_unref (output);
191 operation_shutdown (FALSE);
192 return;
193 }
194
195 g_print ("[%s] IMS services:\n", qmi_device_get_path_display (ctx->device));
196
197 if (qmi_message_ims_get_ims_services_enabled_setting_output_get_ims_voice_service_enabled (output, &service_voice_enabled, NULL))
198 g_print ("\t Voice service enabled: %s\n", service_voice_enabled? "yes" : "no");
199
200 if (qmi_message_ims_get_ims_services_enabled_setting_output_get_ims_video_telephony_service_enabled (output, &service_vt_enabled, NULL))
201 g_print ("\tVideo Telephony service enabled: %s\n", service_vt_enabled? "yes" : "no");
202
203 if (qmi_message_ims_get_ims_services_enabled_setting_output_get_ims_voice_wifi_service_enabled (output, &service_voice_wifi_enabled, NULL))
204 g_print ("\t Voice WiFi service enabled: %s\n", service_voice_wifi_enabled? "yes" : "no");
205
206 if (qmi_message_ims_get_ims_services_enabled_setting_output_get_ims_registration_service_enabled (output, &service_ims_registration_enabled, NULL))
207 g_print ("\t IMS registration enabled: %s\n", service_ims_registration_enabled? "yes" : "no");
208
209 if (qmi_message_ims_get_ims_services_enabled_setting_output_get_ims_ut_service_enabled (output, &service_ut_enabled, NULL))
210 g_print ("\t UE to TAS service enabled: %s\n", service_ut_enabled? "yes" : "no");
211
212 if (qmi_message_ims_get_ims_services_enabled_setting_output_get_ims_sms_service_enabled (output, &service_sms_enabled, NULL))
213 g_print ("\t SMS service enabled: %s\n", service_sms_enabled? "yes" : "no");
214
215 if (qmi_message_ims_get_ims_services_enabled_setting_output_get_ims_ussd_service_enabled (output, &service_ussd_enabled, NULL))
216 g_print ("\t USSD service enabled: %s\n", service_ut_enabled? "yes" : "no");
217
218 qmi_message_ims_get_ims_services_enabled_setting_output_unref (output);
219 operation_shutdown (TRUE);
220 }
221
222 #endif /* HAVE_QMI_MESSAGE_IMS_GET_IMS_SERVICES_ENABLED_SETTING */
223
224 static gboolean
225 noop_cb (gpointer unused)
226 {
227 operation_shutdown (TRUE);
228 return FALSE;
229 }
230
231 void
232 qmicli_ims_run (QmiDevice *device,
233 QmiClientIms *client,
234 GCancellable *cancellable)
235 {
236 /* Initialize context */
237 ctx = g_slice_new (Context);
238 ctx->device = g_object_ref (device);
239 ctx->client = g_object_ref (client);
240 ctx->cancellable = g_object_ref (cancellable);
241
242 #if defined HAVE_QMI_MESSAGE_IMS_BIND
243 if (bind_flag >= 0) {
244 QmiMessageImsBindInput *input;
245
246 input = qmi_message_ims_bind_input_new ();
247 qmi_message_ims_bind_input_set_binding (input, bind_flag, NULL);
248 g_debug ("Asynchronously binding to IMS settings service...");
249 qmi_client_ims_bind (ctx->client,
250 input,
251 10,
252 ctx->cancellable,
253 (GAsyncReadyCallback)bind_ready,
254 NULL);
255 qmi_message_ims_bind_input_unref (input);
256 return;
257 }
258 #endif /* HAVE_QMI_MESSAGE_IMS_BIND */
259 #if defined HAVE_QMI_MESSAGE_IMS_GET_IMS_SERVICES_ENABLED_SETTING
260 if (get_services_enabled_flag) {
261 g_debug ("Asynchronously getting services enabled setting...");
262
263 qmi_client_ims_get_ims_services_enabled_setting (ctx->client,
264 NULL,
265 10,
266 ctx->cancellable,
267 (GAsyncReadyCallback)get_services_enabled_ready,
268 NULL);
269 return;
270 }
271 #endif /* HAVE_QMI_MESSAGE_IMS_GET_IMS_SERVICES_ENABLED_SETTING */
272
273 /* Just client allocate/release? */
274 if (noop_flag) {
275 g_idle_add (noop_cb, NULL);
276 return;
277 }
278
279 g_warn_if_reached ();
280 }
281
282 #endif /* HAVE_QMI_SERVICE_IMS */
283