Debian Codegraph @

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

raw
1
2 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /*
4 * Simple tester
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Copyright (c) 2024 Aleksander Morgado <[email protected]>
20 */
21
22 #include <glib.h>
23 #include <glib-unix.h>
24 #include <libmbim-glib.h>
25
26 static GMainLoop *loop;
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 (MbimDevice *device,
37 GAsyncResult *res)
38 {
39 g_autoptr(GError) error = NULL;
40
41 if (!mbim_device_close_finish (device, res, &error))
42 g_printerr ("error: couldn't close: %s\n", error->message);
43
44 g_main_loop_quit (loop);
45 }
46
47 static void
48 device_close (MbimDevice *device)
49 {
50 mbim_device_close (device,
51 10,
52 NULL,
53 (GAsyncReadyCallback) close_ready,
54 NULL);
55 }
56
57 static void
58 query_device_caps_ready (MbimDevice *device,
59 GAsyncResult *res)
60 {
61 g_autoptr(GError) error = NULL;
62 g_autoptr(MbimMessage) response = NULL;
63 MbimDeviceType device_type;
64 const gchar *device_type_str;
65 MbimVoiceClass voice_class;
66 const gchar *voice_class_str;
67 MbimCellularClass cellular_class;
68 g_autofree gchar *cellular_class_str = NULL;
69 MbimSimClass sim_class;
70 g_autofree gchar *sim_class_str = NULL;
71 MbimDataClass data_class;
72 g_autofree gchar *data_class_str = NULL;
73 MbimSmsCaps sms_caps;
74 g_autofree gchar *sms_caps_str = NULL;
75 MbimCtrlCaps ctrl_caps;
76 g_autofree gchar *ctrl_caps_str = NULL;
77 guint32 max_sessions;
78 g_autofree gchar *custom_data_class = NULL;
79 g_autofree gchar *device_id = NULL;
80 g_autofree gchar *firmware_info = NULL;
81 g_autofree gchar *hardware_info = NULL;
82
83 response = mbim_device_command_finish (device, res, &error);
84 if (!response) {
85 g_printerr ("error: couldn't open the MbimDevice: %s\n",
86 error->message);
87 device_close (device);
88 return;
89 }
90
91 if (!mbim_message_device_caps_response_parse (
92 response,
93 &device_type,
94 &cellular_class,
95 &voice_class,
96 &sim_class,
97 &data_class,
98 &sms_caps,
99 &ctrl_caps,
100 &max_sessions,
101 &custom_data_class,
102 &device_id,
103 &firmware_info,
104 &hardware_info,
105 &error)) {
106 g_printerr ("error: couldn't parse response message: %s\n",
107 error->message);
108 device_close (device);
109 return;
110 }
111
112 device_type_str = mbim_device_type_get_string (device_type);
113 cellular_class_str = mbim_cellular_class_build_string_from_mask (cellular_class);
114 voice_class_str = mbim_voice_class_get_string (voice_class);
115 sim_class_str = mbim_sim_class_build_string_from_mask (sim_class);
116 data_class_str = mbim_data_class_build_string_from_mask (data_class);
117 sms_caps_str = mbim_sms_caps_build_string_from_mask (sms_caps);
118 ctrl_caps_str = mbim_ctrl_caps_build_string_from_mask (ctrl_caps);
119
120 g_print ("device type: %s\n", device_type_str);
121 g_print ("cellular class: %s\n", cellular_class_str);
122 g_print ("voice class: %s\n", voice_class_str);
123 g_print ("sim class: %s\n", sim_class_str);
124 g_print ("data class: %s\n", data_class_str);
125 g_print ("sms capabilities: %s\n", sms_caps_str);
126 g_print ("control capabilities: %s\n", ctrl_caps_str);
127 g_print ("max sessions: %u\n", max_sessions);
128 g_print ("custom data class: %s\n", custom_data_class);
129 g_print ("device id: %s\n", device_id);
130 g_print ("firmware info: %s\n", firmware_info);
131 g_print ("hardware info: %s\n", hardware_info);
132
133 device_close (device);
134 }
135
136 static void
137 open_full_ready (MbimDevice *device,
138 GAsyncResult *res)
139 {
140 g_autoptr(GError) error = NULL;
141 g_autoptr(MbimMessage) request = NULL;
142
143 if (!mbim_device_open_full_finish (device, res, &error)) {
144 g_printerr ("error: couldn't open the MbimDevice: %s\n",
145 error->message);
146 g_main_loop_quit (loop);
147 return;
148 }
149
150 request = mbim_message_device_caps_query_new (NULL);
151 mbim_device_command (device,
152 request,
153 10,
154 NULL,
155 (GAsyncReadyCallback)query_device_caps_ready,
156 NULL);
157 }
158
159 static void
160 new_ready (GObject *unused,
161 GAsyncResult *res)
162 {
163 g_autoptr(MbimDevice) device = NULL;
164 g_autoptr(GError) error = NULL;
165
166 device = mbim_device_new_finish (res, &error);
167 if (!device) {
168 g_printerr ("error: couldn't create MbimDevice: %s\n", error->message);
169 g_main_loop_quit (loop);
170 return;
171 }
172
173 /* Open the device */
174 mbim_device_open_full (device,
175 MBIM_DEVICE_OPEN_FLAGS_PROXY,
176 10,
177 NULL,
178 (GAsyncReadyCallback)open_full_ready,
179 NULL);
180 }
181
182 int main (int argc, char **argv)
183 {
184 g_autoptr(GFile) file = NULL;
185
186 if (argc != 2) {
187 g_printerr ("error: wrong number of arguments\n");
188 g_printerr ("usage: simple-tester-c <DEVICE>\n");
189 exit (1);
190 }
191
192 file = g_file_new_for_path (argv[1]);
193 mbim_device_new (file,
194 NULL,
195 (GAsyncReadyCallback)new_ready,
196 NULL);
197
198 loop = g_main_loop_new (NULL, FALSE);
199
200 g_unix_signal_add (SIGHUP, (GSourceFunc) signals_handler, NULL);
201 g_unix_signal_add (SIGTERM, (GSourceFunc) signals_handler, NULL);
202
203 g_main_loop_run (loop);
204 g_main_loop_unref (loop);
205 return 0;
206 }