Debian Codegraph @

src/libqmi-glib/qmi-endpoint-mbim.c

raw
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * libqmi-glib -- GLib/GIO based library to control QMI devices
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library 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 GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA.
19 *
20 * Copyright (C) 2012 Lanedo GmbH
21 * Copyright (C) 2012-2017 Aleksander Morgado <[email protected]>
22 * Copyright (C) 2019 Eric Caruso <[email protected]>
23 */
24
25 #include <string.h>
26
27 #include <libmbim-glib.h>
28
29 #include "qmi-endpoint-mbim.h"
30 #include "qmi-errors.h"
31 #include "qmi-error-types.h"
32 #include "qmi-file.h"
33
34 G_DEFINE_TYPE (QmiEndpointMbim, qmi_endpoint_mbim, QMI_TYPE_ENDPOINT)
35
36 struct _QmiEndpointMbimPrivate {
37 MbimDevice *mbimdev;
38 guint mbim_notification_id;
39 guint mbim_removed_id;
40 };
41
42 /*
43 * Number of extra seconds to give the MBIM timeout delay.
44 * Needed so the QMI timeout triggers first and we can be sure
45 * that timeouts on the QMI side are not because of libmbim timeouts.
46 */
47 #define MBIM_TIMEOUT_DELAY_SECS 1
48
49 /*****************************************************************************/
50
51 static void
52 mbim_device_removed_cb (MbimDevice *device,
53 QmiEndpointMbim *self)
54 {
55 g_signal_emit_by_name (QMI_ENDPOINT (self), QMI_ENDPOINT_SIGNAL_HANGUP);
56 }
57
58 /*****************************************************************************/
59
60 static void
61 mbim_device_command_ready (MbimDevice *dev,
62 GAsyncResult *res,
63 QmiEndpointMbim *self)
64 {
65 MbimMessage *response;
66 const guint8 *buf;
67 guint32 len;
68 GError *error = NULL;
69
70 response = mbim_device_command_finish (dev, res, &error);
71 if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
72 g_warning ("[%s] MBIM error: %s", qmi_endpoint_get_name (QMI_ENDPOINT (self)), error->message);
73
74 if (response)
75 mbim_message_unref (response);
76 g_object_unref (self);
77 g_error_free (error);
78 return;
79 }
80
81 /* Store the raw information buffer in the internal reception buffer,
82 * as if we had read from a iochannel. */
83 buf = mbim_message_command_done_get_raw_information_buffer (response, &len);
84 qmi_endpoint_add_message (QMI_ENDPOINT (self), buf, len);
85 mbim_message_unref (response);
86 g_object_unref (self);
87 }
88
89 static void
90 mbim_qmi_notification_cb (MbimDevice *device,
91 MbimMessage *notification,
92 QmiEndpointMbim *self)
93 {
94 MbimService service;
95 const guint8 *buf;
96 guint32 len;
97
98 service = mbim_message_indicate_status_get_service (notification);
99 if (service != MBIM_SERVICE_QMI)
100 return;
101
102 buf = mbim_message_indicate_status_get_raw_information_buffer (notification, &len);
103 qmi_endpoint_add_message (QMI_ENDPOINT (self), buf, len);
104 }
105
106 static void
107 mbim_subscribe_list_set_ready_cb (MbimDevice *device,
108 GAsyncResult *res,
109 GTask *task)
110 {
111 QmiEndpointMbim *self;
112 MbimMessage *response;
113 GError *error = NULL;
114
115 self = g_task_get_source_object (task);
116
117 response = mbim_device_command_finish (device, res, &error);
118 if (response) {
119 mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error);
120 mbim_message_unref (response);
121 }
122
123 if (error) {
124 g_warning ("[%s] couldn't enable QMI indications via MBIM: %s",
125 qmi_endpoint_get_name (QMI_ENDPOINT (self)), error->message);
126 g_task_return_error (task, error);
127 g_object_unref (task);
128 return;
129 }
130
131 g_debug ("[%s] enabled QMI indications via MBIM", qmi_endpoint_get_name (QMI_ENDPOINT (self)));
132 self->priv->mbim_notification_id = g_signal_connect (device,
133 MBIM_DEVICE_SIGNAL_INDICATE_STATUS,
134 G_CALLBACK (mbim_qmi_notification_cb),
135 self);
136 g_task_return_boolean (task, TRUE);
137 g_object_unref (task);
138 }
139
140 /*****************************************************************************/
141
142 typedef struct {
143 gboolean use_proxy;
144 guint timeout;
145 } MbimDeviceOpenContext;
146
147 static void
148 mbim_device_open_context_free (MbimDeviceOpenContext *ctx)
149 {
150 g_slice_free (MbimDeviceOpenContext, ctx);
151 }
152
153 static gboolean
154 endpoint_open_finish (QmiEndpoint *self,
155 GAsyncResult *res,
156 GError **error)
157 {
158 return g_task_propagate_boolean (G_TASK (res), error);
159 }
160
161 static void
162 mbim_device_open_ready (MbimDevice *dev,
163 GAsyncResult *res,
164 GTask *task)
165 {
166 QmiEndpointMbim *self;
167 GError *error = NULL;
168
169 if (!mbim_device_open_finish (dev, res, &error)) {
170 g_task_return_error (task, error);
171 g_object_unref (task);
172 return;
173 }
174
175 self = g_task_get_source_object (task);
176 g_debug ("[%s] MBIM device open", qmi_endpoint_get_name (QMI_ENDPOINT (self)));
177 g_task_return_boolean (task, TRUE);
178 g_object_unref (task);
179 }
180
181 static void
182 mbim_device_new_ready (GObject *source,
183 GAsyncResult *res,
184 GTask *task)
185 {
186 QmiEndpointMbim *self;
187 MbimDeviceOpenContext *ctx;
188 MbimDeviceOpenFlags open_flags = MBIM_DEVICE_OPEN_FLAGS_NONE;
189 GError *error = NULL;
190
191 self = g_task_get_source_object (task);
192 ctx = g_task_get_task_data (task);
193
194 self->priv->mbimdev = mbim_device_new_finish (res, &error);
195 if (!self->priv->mbimdev) {
196 g_task_return_error (task, error);
197 g_object_unref (task);
198 return;
199 }
200
201 g_debug ("[%s] MBIM device created", qmi_endpoint_get_name (QMI_ENDPOINT (self)));
202
203 self->priv->mbim_removed_id =
204 g_signal_connect (self->priv->mbimdev,
205 MBIM_DEVICE_SIGNAL_REMOVED,
206 G_CALLBACK (mbim_device_removed_cb),
207 self);
208
209 /* If QMI proxy was requested, use MBIM proxy instead */
210 if (ctx->use_proxy)
211 open_flags |= MBIM_DEVICE_OPEN_FLAGS_PROXY;
212
213 /* We pass the original timeout of the request to the open operation */
214 g_debug ("[%s] opening MBIM device...", qmi_endpoint_get_name (QMI_ENDPOINT (self)));
215 mbim_device_open_full (self->priv->mbimdev,
216 open_flags,
217 ctx->timeout,
218 g_task_get_cancellable (task),
219 (GAsyncReadyCallback) mbim_device_open_ready,
220 task);
221 }
222
223 static void
224 endpoint_open (QmiEndpoint *endpoint,
225 gboolean use_proxy,
226 guint timeout,
227 GCancellable *cancellable,
228 GAsyncReadyCallback callback,
229 gpointer user_data)
230 {
231 QmiEndpointMbim *self;
232 MbimDeviceOpenContext *ctx;
233 QmiFile *qfile;
234 GFile *file;
235 GTask *task;
236
237 self = QMI_ENDPOINT_MBIM (endpoint);
238 task = g_task_new (self, cancellable, callback, user_data);
239
240 ctx = g_slice_new0 (MbimDeviceOpenContext);
241 ctx->use_proxy = use_proxy;
242 ctx->timeout = timeout;
243 g_task_set_task_data (task, ctx, (GDestroyNotify)mbim_device_open_context_free);
244
245 if (self->priv->mbimdev) {
246 g_task_return_new_error (task,
247 QMI_CORE_ERROR,
248 QMI_CORE_ERROR_WRONG_STATE,
249 "Already open");
250 g_object_unref (task);
251 return;
252 }
253
254 g_object_get (self, QMI_ENDPOINT_FILE, &qfile, NULL);
255 g_debug ("[%s] creating MBIM device...", qmi_endpoint_get_name (QMI_ENDPOINT (self)));
256 file = g_file_new_for_path (qmi_file_get_path (qfile));
257 g_object_unref (qfile);
258
259 mbim_device_new (file,
260 g_task_get_cancellable (task),
261 (GAsyncReadyCallback) mbim_device_new_ready,
262 task);
263 g_object_unref (file);
264 }
265
266 static gboolean
267 endpoint_is_open (QmiEndpoint *self)
268 {
269 return !!(QMI_ENDPOINT_MBIM (self)->priv->mbimdev);
270 }
271
272 static gboolean
273 endpoint_setup_indications_finish (QmiEndpoint *self,
274 GAsyncResult *res,
275 GError **error)
276 {
277 return g_task_propagate_boolean (G_TASK (res), error);
278 }
279
280 static void
281 endpoint_setup_indications (QmiEndpoint *self,
282 guint timeout,
283 GCancellable *cancellable,
284 GAsyncReadyCallback callback,
285 gpointer user_data)
286 {
287 GTask *task;
288 MbimEventEntry **entries;
289 guint n_entries = 0;
290 MbimMessage *request;
291
292 task = g_task_new (self, cancellable, callback, user_data);
293
294 g_debug ("[%s] Enabling QMI indications via MBIM...", qmi_endpoint_get_name (QMI_ENDPOINT (self)));
295
296 entries = g_new0 (MbimEventEntry *, 2);
297 entries[n_entries] = g_new (MbimEventEntry, 1);
298 memcpy (&(entries[n_entries]->device_service_id), MBIM_UUID_QMI, sizeof (MbimUuid));
299 entries[n_entries]->cids_count = 1;
300 entries[n_entries]->cids = g_new0 (guint32, 1);
301 entries[n_entries]->cids[0] = MBIM_CID_QMI_MSG;
302 n_entries++;
303
304 request = mbim_message_device_service_subscribe_list_set_new (
305 n_entries,
306 (const MbimEventEntry *const *)entries,
307 NULL);
308 mbim_device_command (QMI_ENDPOINT_MBIM (self)->priv->mbimdev,
309 request,
310 10,
311 NULL,
312 (GAsyncReadyCallback)mbim_subscribe_list_set_ready_cb,
313 task);
314 mbim_message_unref (request);
315 mbim_event_entry_array_free (entries);
316 }
317
318 static gboolean
319 endpoint_send (QmiEndpoint *self,
320 QmiMessage *message,
321 guint timeout,
322 GCancellable *cancellable,
323 GError **error)
324 {
325 g_autoptr(MbimMessage) mbim_message = NULL;
326 gconstpointer raw_message;
327 gsize raw_message_len;
328 GError *inner_error = NULL;
329
330 /* MBIM endpoint allows only QMUX messages */
331 if (qmi_message_get_marker (message) != QMI_MESSAGE_QMUX_MARKER) {
332 g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED,
333 "MBIM endpoint expects only QMUX messages");
334 return FALSE;
335 }
336
337 /* Disallow private CTL operations */
338 if ((qmi_message_get_service (message) == QMI_SERVICE_CTL) &&
339 (qmi_message_get_message_id (message) == QMI_MESSAGE_CTL_INTERNAL_ALLOCATE_CID_QRTR ||
340 qmi_message_get_message_id (message) == QMI_MESSAGE_CTL_INTERNAL_RELEASE_CID_QRTR)) {
341 g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED,
342 "MBIM endpoint expects only 8bit QMI services");
343 return FALSE;
344 }
345
346 /* Get raw message */
347 raw_message = qmi_message_get_raw (message, &raw_message_len, &inner_error);
348 if (!raw_message) {
349 g_propagate_prefixed_error (error, inner_error, "Cannot get raw message: ");
350 return FALSE;
351 }
352
353 mbim_message = mbim_message_qmi_msg_set_new (raw_message_len, raw_message, &inner_error);
354 if (!mbim_message) {
355 g_propagate_error (error, inner_error);
356 return FALSE;
357 }
358
359 /* Note:
360 *
361 * Pass a full reference to the QMI endpoint to the MBIM command
362 * operation, so that we make sure the parent object is valid regardless
363 * of when the underlying device is fully disposed. This is required
364 * because device close is async().
365 */
366 mbim_device_command (QMI_ENDPOINT_MBIM (self)->priv->mbimdev,
367 mbim_message,
368 timeout + MBIM_TIMEOUT_DELAY_SECS,
369 cancellable,
370 (GAsyncReadyCallback) mbim_device_command_ready,
371 g_object_ref (self));
372 return TRUE;
373 }
374
375 /*****************************************************************************/
376
377 static gboolean
378 endpoint_close_finish (QmiEndpoint *self,
379 GAsyncResult *res,
380 GError **error)
381 {
382 return g_task_propagate_boolean (G_TASK (res), error);
383 }
384
385 static void
386 mbim_device_close_ready (MbimDevice *dev,
387 GAsyncResult *res,
388 GTask *task)
389 {
390 GError *error = NULL;
391
392 if (!mbim_device_close_finish (dev, res, &error))
393 g_task_return_error (task, error);
394 else
395 g_task_return_boolean (task, TRUE);
396 g_object_unref (task);
397 }
398
399 static void
400 endpoint_close (QmiEndpoint *endpoint,
401 guint timeout,
402 GCancellable *cancellable,
403 GAsyncReadyCallback callback,
404 gpointer user_data)
405 {
406 GTask *task;
407 QmiEndpointMbim *self;
408
409 self = QMI_ENDPOINT_MBIM (endpoint);
410 task = g_task_new (self, cancellable, callback, user_data);
411
412 if (!self->priv->mbimdev) {
413 g_task_return_boolean (task, TRUE);
414 g_object_unref (task);
415 }
416
417 /* Schedule in new main context */
418 mbim_device_close (self->priv->mbimdev,
419 timeout,
420 NULL,
421 (GAsyncReadyCallback) mbim_device_close_ready,
422 task);
423 /* Cleanup right away, we don't want multiple close attempts on the
424 * device */
425 if (self->priv->mbim_notification_id) {
426 g_signal_handler_disconnect (self->priv->mbimdev, self->priv->mbim_notification_id);
427 self->priv->mbim_notification_id = 0;
428 }
429 if (self->priv->mbim_removed_id) {
430 g_signal_handler_disconnect (self->priv->mbimdev, self->priv->mbim_removed_id);
431 self->priv->mbim_removed_id = 0;
432 }
433 g_clear_object (&self->priv->mbimdev);
434 }
435
436 /*****************************************************************************/
437
438 QmiEndpointMbim *
439 qmi_endpoint_mbim_new (QmiFile *file)
440 {
441 if (!file)
442 return NULL;
443
444 return g_object_new (QMI_TYPE_ENDPOINT_MBIM,
445 QMI_ENDPOINT_FILE, file,
446 NULL);
447 }
448
449 /*****************************************************************************/
450
451 static void
452 qmi_endpoint_mbim_init (QmiEndpointMbim *self)
453 {
454 self->priv = G_TYPE_INSTANCE_GET_PRIVATE ((self),
455 QMI_TYPE_ENDPOINT_MBIM,
456 QmiEndpointMbimPrivate);
457 }
458
459 static void
460 dispose (GObject *object)
461 {
462 QmiEndpointMbim *self = QMI_ENDPOINT_MBIM (object);
463
464 if (self->priv->mbimdev) {
465 g_warning ("[%s] MBIM device wasn't explicitly closed",
466 qmi_endpoint_get_name (QMI_ENDPOINT (self)));
467
468 if (self->priv->mbim_notification_id) {
469 g_signal_handler_disconnect (self->priv->mbimdev, self->priv->mbim_notification_id);
470 self->priv->mbim_notification_id = 0;
471 }
472 if (self->priv->mbim_removed_id) {
473 g_signal_handler_disconnect (self->priv->mbimdev, self->priv->mbim_removed_id);
474 self->priv->mbim_removed_id = 0;
475 }
476 g_clear_object (&self->priv->mbimdev);
477 }
478
479 G_OBJECT_CLASS (qmi_endpoint_mbim_parent_class)->dispose (object);
480 }
481
482 static void
483 qmi_endpoint_mbim_class_init (QmiEndpointMbimClass *klass)
484 {
485 GObjectClass *object_class = G_OBJECT_CLASS (klass);
486 QmiEndpointClass *endpoint_class = QMI_ENDPOINT_CLASS (klass);
487
488 g_type_class_add_private (object_class, sizeof (QmiEndpointMbimPrivate));
489
490 object_class->dispose = dispose;
491
492 endpoint_class->open = endpoint_open;
493 endpoint_class->open_finish = endpoint_open_finish;
494 endpoint_class->is_open = endpoint_is_open;
495 endpoint_class->setup_indications = endpoint_setup_indications;
496 endpoint_class->setup_indications_finish = endpoint_setup_indications_finish;
497 endpoint_class->send = endpoint_send;
498 endpoint_class->close = endpoint_close;
499 endpoint_class->close_finish = endpoint_close_finish;
500 }