Debian Codegraph @

examples/simple-tester-c/simple-tester-c.c

raw
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * Simple tester
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) 2024 Aleksander Morgado <[email protected]>
19 */
20
21 #include <glib.h>
22 #include <glib-unix.h>
23 #include <libqmi-glib.h>
24
25 static GMainLoop *loop;
26 static QmiDevice *device;
27
28 static gboolean
29 signals_handler (void)
30 {
31 g_main_loop_quit (loop);
32 return G_SOURCE_REMOVE;
33 }
34
35 static void
36 close_ready (QmiDevice *dev,
37 GAsyncResult *res)
38 {
39 g_autoptr(GError) error = NULL;
40
41 if (!qmi_device_close_finish (dev, res, &error))
42 g_printerr ("error: couldn't close: %s\n", error->message);
43
44 g_main_loop_quit (loop);
45 }
46
47
48 static void
49 device_close (void)
50 {
51 qmi_device_close_async (device,
52 10,
53 NULL,
54 (GAsyncReadyCallback) close_ready,
55 NULL);
56 }
57
58 static void
59 release_client_ready (QmiDevice *dev,
60 GAsyncResult *res)
61 {
62 g_autoptr(GError) error = NULL;
63
64 if (!qmi_device_release_client_finish (dev, res, &error))
65 g_printerr ("error: couldn't release client: %s\n", error->message);
66
67 device_close ();
68 }
69
70 static void
71 release_client (QmiClientDms *client)
72 {
73 qmi_device_release_client (device,
74 QMI_CLIENT (client),
75 QMI_DEVICE_RELEASE_CLIENT_FLAGS_RELEASE_CID,
76 10,
77 NULL,
78 (GAsyncReadyCallback)release_client_ready,
79 NULL);
80 }
81
82 static void
83 get_ids_ready (QmiClientDms *client,
84 GAsyncResult *res)
85 {
86 g_autoptr(QmiMessageDmsGetIdsOutput) output = NULL;
87 g_autoptr(GError) error = NULL;
88 const gchar *esn = NULL;
89 const gchar *imei = NULL;
90 const gchar *meid = NULL;
91 const gchar *imei_software_version = NULL;
92
93 output = qmi_client_dms_get_ids_finish (client, res, &error);
94 if (!output || !qmi_message_dms_get_ids_output_get_result (output, &error)) {
95 g_printerr ("error: couldn't query device ids: %s\n", error->message);
96 release_client (client);
97 return;
98 }
99
100 if (qmi_message_dms_get_ids_output_get_imei (output, &imei, NULL))
101 g_print("imei: %s\n", imei);
102
103 if (qmi_message_dms_get_ids_output_get_imei_software_version (output,
104 &imei_software_version,
105 NULL))
106 g_print("imei software version: %s\n", imei_software_version);
107
108 if (qmi_message_dms_get_ids_output_get_meid (output, &meid, NULL))
109 g_print("meid: %s\n", meid);
110
111 if (qmi_message_dms_get_ids_output_get_esn (output, &esn, NULL))
112 g_print("esn: %s\n", esn);
113
114 release_client (client);
115 }
116
117 static void
118 get_capabilities_ready (QmiClientDms *client,
119 GAsyncResult *res)
120 {
121 g_autoptr(QmiMessageDmsGetCapabilitiesOutput) output = NULL;
122 g_autoptr(GError) error = NULL;
123 guint32 max_tx_channel_rate;
124 guint32 max_rx_channel_rate;
125 QmiDmsDataServiceCapability data_service_capability;
126 QmiDmsSimCapability sim_capability;
127 GArray *radio_interface_list;
128 g_autoptr(GString) networks = NULL;
129 guint i;
130
131 output = qmi_client_dms_get_capabilities_finish (client, res, &error);
132 if (!output || !qmi_message_dms_get_capabilities_output_get_result (output, &error)) {
133 g_printerr ("error: couldn't query device capabilities: %s\n", error->message);
134 release_client (client);
135 return;
136 }
137
138 qmi_message_dms_get_capabilities_output_get_info (output,
139 &max_tx_channel_rate,
140 &max_rx_channel_rate,
141 &data_service_capability,
142 &sim_capability,
143 &radio_interface_list,
144 NULL);
145
146 g_print ("max tx channel rate: %u\n", max_tx_channel_rate);
147 g_print ("max rx channel rate: %u\n", max_rx_channel_rate);
148 g_print ("data service: %s\n", qmi_dms_data_service_capability_get_string (data_service_capability));
149 g_print ("sim: %s\n", qmi_dms_sim_capability_get_string (sim_capability));
150
151 networks = g_string_new ("");
152 for (i = 0; i < radio_interface_list->len; i++) {
153 if (i != 0)
154 g_string_append (networks, ", ");
155 g_string_append (networks,
156 qmi_dms_radio_interface_get_string (g_array_index (radio_interface_list,
157 QmiDmsRadioInterface,
158 i)));
159 }
160 g_print ("networks: %s\n", networks->str);
161
162 qmi_client_dms_get_ids (client,
163 NULL,
164 10,
165 NULL,
166 (GAsyncReadyCallback)get_ids_ready,
167 NULL);
168 }
169
170 static void
171 allocate_client_ready (QmiDevice *dev,
172 GAsyncResult *res)
173 {
174 g_autoptr(GError) error = NULL;
175 g_autoptr(QmiClient) client = NULL;
176
177 client = qmi_device_allocate_client_finish (dev, res, &error);
178 if (!client) {
179 g_printerr ("error: couldn't allocate QMI client: %s\n",
180 error->message);
181 device_close ();
182 return;
183 }
184
185 qmi_client_dms_get_capabilities (QMI_CLIENT_DMS (client),
186 NULL,
187 10,
188 NULL,
189 (GAsyncReadyCallback)get_capabilities_ready,
190 NULL);
191 }
192
193 static void
194 open_ready (QmiDevice *dev,
195 GAsyncResult *res)
196 {
197 g_autoptr(GError) error = NULL;
198
199 if (!qmi_device_open_finish (dev, res, &error)) {
200 g_printerr ("error: couldn't open the QmiDevice: %s\n",
201 error->message);
202 g_main_loop_quit (loop);
203 return;
204 }
205
206 qmi_device_allocate_client (device,
207 QMI_SERVICE_DMS,
208 QMI_CID_NONE,
209 10,
210 NULL,
211 (GAsyncReadyCallback)allocate_client_ready,
212 NULL);
213 }
214
215 static void
216 new_ready (GObject *unused,
217 GAsyncResult *res)
218 {
219 g_autoptr(GError) error = NULL;
220
221 device = qmi_device_new_finish (res, &error);
222 if (!device) {
223 g_printerr ("error: couldn't create QmiDevice: %s\n", error->message);
224 g_main_loop_quit (loop);
225 return;
226 }
227
228 /* Open the device */
229 qmi_device_open (device,
230 QMI_DEVICE_OPEN_FLAGS_PROXY | QMI_DEVICE_OPEN_FLAGS_AUTO,
231 10,
232 NULL,
233 (GAsyncReadyCallback)open_ready,
234 NULL);
235 }
236
237 int main (int argc, char **argv)
238 {
239 g_autoptr(GFile) file = NULL;
240
241 if (argc != 2) {
242 g_printerr ("error: wrong number of arguments\n");
243 g_printerr ("usage: simple-tester-c <DEVICE>\n");
244 exit (1);
245 }
246
247 file = g_file_new_for_path (argv[1]);
248 qmi_device_new (file,
249 NULL,
250 (GAsyncReadyCallback)new_ready,
251 NULL);
252
253 loop = g_main_loop_new (NULL, FALSE);
254
255 g_unix_signal_add (SIGHUP, (GSourceFunc) signals_handler, NULL);
256 g_unix_signal_add (SIGTERM, (GSourceFunc) signals_handler, NULL);
257
258 g_main_loop_run (loop);
259
260 g_clear_object (&device);
261 g_main_loop_unref (loop);
262 return 0;
263 }