Debian Codegraph @

src/qmicli/qmicli-imsp.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_IMSP
38
39 /* Context */
40 typedef struct {
41 QmiDevice *device;
42 QmiClientImsp *client;
43 GCancellable *cancellable;
44 } Context;
45 static Context *ctx;
46
47 /* Options */
48 static gboolean get_enabler_state_flag;
49 static gboolean noop_flag;
50
51 static GOptionEntry entries[] = {
52 #if defined HAVE_QMI_MESSAGE_IMSP_GET_ENABLER_STATE
53 { "imsp-get-enabler-state", 0, 0, G_OPTION_ARG_NONE, &get_enabler_state_flag,
54 "Get IMSP enabler state",
55 NULL
56 },
57 #endif
58 { "imsp-noop", 0, 0, G_OPTION_ARG_NONE, &noop_flag,
59 "Just allocate or release a IMSP client. Use with `--client-no-release-cid' and/or `--client-cid'",
60 NULL
61 },
62 { NULL }
63 };
64
65 GOptionGroup *
66 qmicli_imsp_get_option_group (void)
67 {
68 GOptionGroup *group;
69
70 group = g_option_group_new ("imsp",
71 "IMSP options:",
72 "Show IP Multimedia Subsystem Presence Service options",
73 NULL,
74 NULL);
75 g_option_group_add_entries (group, entries);
76
77 return group;
78 }
79
80 gboolean
81 qmicli_imsp_options_enabled (void)
82 {
83 static guint n_actions = 0;
84 static gboolean checked = FALSE;
85
86 if (checked)
87 return !!n_actions;
88
89 n_actions = (get_enabler_state_flag +
90 noop_flag);
91
92 if (n_actions > 1) {
93 g_printerr ("error: too many IMSP actions requested\n");
94 exit (EXIT_FAILURE);
95 }
96
97 checked = TRUE;
98 return !!n_actions;
99 }
100
101 static void
102 context_free (Context *context)
103 {
104 if (!context)
105 return;
106
107 if (context->cancellable)
108 g_object_unref (context->cancellable);
109 if (context->device)
110 g_object_unref (context->device);
111 if (context->client)
112 g_object_unref (context->client);
113 g_slice_free (Context, context);
114 }
115
116 static void
117 operation_shutdown (gboolean operation_status)
118 {
119 /* Cleanup context and finish async operation */
120 context_free (ctx);
121 qmicli_async_operation_done (operation_status, FALSE);
122 }
123
124 #if defined HAVE_QMI_MESSAGE_IMSP_GET_ENABLER_STATE
125
126 static void
127 get_enabler_state_ready (QmiClientImsp *client,
128 GAsyncResult *res)
129 {
130 QmiMessageImspGetEnablerStateOutput *output;
131 QmiImspEnablerState enabler_state;
132 GError *error = NULL;
133
134 output = qmi_client_imsp_get_enabler_state_finish (client, res, &error);
135 if (!output) {
136 g_printerr ("error: operation failed: %s\n", error->message);
137 g_error_free (error);
138 operation_shutdown (FALSE);
139 return;
140 }
141
142 if (!qmi_message_imsp_get_enabler_state_output_get_result (output, &error)) {
143 g_printerr ("error: couldn't get enabler state: %s\n", error->message);
144 g_error_free (error);
145 qmi_message_imsp_get_enabler_state_output_unref (output);
146 operation_shutdown (FALSE);
147 return;
148 }
149
150 g_print ("[%s] IMSP enabler state retrieved:\n", qmi_device_get_path_display (ctx->device));
151
152 if (qmi_message_imsp_get_enabler_state_output_get_enabler_state (output, &enabler_state, NULL)) {
153 g_print ("\tRegistration status: '%s'\n",
154 qmi_imsp_enabler_state_get_string (enabler_state));
155 }
156
157 qmi_message_imsp_get_enabler_state_output_unref (output);
158 operation_shutdown (TRUE);
159 }
160
161 #endif /* HAVE_QMI_MESSAGE_IMSP_GET_ENABLER_STATE */
162
163 static gboolean
164 noop_cb (gpointer unused)
165 {
166 operation_shutdown (TRUE);
167 return FALSE;
168 }
169
170 void
171 qmicli_imsp_run (QmiDevice *device,
172 QmiClientImsp *client,
173 GCancellable *cancellable)
174 {
175 /* Initialize context */
176 ctx = g_slice_new (Context);
177 ctx->device = g_object_ref (device);
178 ctx->client = g_object_ref (client);
179 ctx->cancellable = g_object_ref (cancellable);
180
181 #if defined HAVE_QMI_MESSAGE_IMSP_GET_ENABLER_STATE
182 if (get_enabler_state_flag) {
183 g_debug ("Asynchronously getting enabler state...");
184
185 qmi_client_imsp_get_enabler_state (ctx->client,
186 NULL,
187 10,
188 ctx->cancellable,
189 (GAsyncReadyCallback)get_enabler_state_ready,
190 NULL);
191 return;
192 }
193 #endif /* HAVE_QMI_MESSAGE_IMSP_GET_ENABLER_STATE */
194
195 /* Just client allocate/release? */
196 if (noop_flag) {
197 g_idle_add (noop_cb, NULL);
198 return;
199 }
200
201 g_warn_if_reached ();
202 }
203
204 #endif /* HAVE_QMI_SERVICE_IMSP */
205