Debian Codegraph @

src/libmbim-glib/mbim-device.c

raw
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 /*
4 * libmbim-glib -- GLib/GIO based library to control MBIM devices
5 *
6 * Copyright (C) 2013-2021 Aleksander Morgado <[email protected]>
7 * Copyright (C) 2014 Smith Micro Software, Inc.
8 * Copyright (C) 2021 Intel Corporation
9 *
10 * Implementation based on the 'QmiDevice' GObject from libqmi-glib.
11 */
12
13 #include <config.h>
14
15 #define _GNU_SOURCE
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <errno.h>
19 #include <string.h>
20 #include <fcntl.h>
21 #include <termios.h>
22 #include <unistd.h>
23 #include <gio/gio.h>
24 #include <gio/gunixsocketaddress.h>
25 #include <sys/ioctl.h>
26 #define IOCTL_WDM_MAX_COMMAND _IOR('H', 0xA0, guint16)
27
28 #define OPEN_RETRY_TIMEOUT_SECS 5
29 #define OPEN_CLOSE_TIMEOUT_SECS 2
30
31 #include "mbim-common.h"
32 #include "mbim-utils.h"
33 #include "mbim-device.h"
34 #include "mbim-message.h"
35 #include "mbim-message-private.h"
36 #include "mbim-error-types.h"
37 #include "mbim-enum-types.h"
38 #include "mbim-helpers.h"
39 #include "mbim-proxy.h"
40 #include "mbim-proxy-control.h"
41 #include "mbim-net-port-manager.h"
42 #include "mbim-net-port-manager-wdm.h"
43 #include "mbim-net-port-manager-wwan.h"
44 #include "mbim-basic-connect.h"
45 #include "mbim-ms-basic-connect-extensions.h"
46
47 /* maximum number of printed data bytes when personal info
48 * should be hidden */
49 #define MAX_PRINTED_BYTES 12
50
51 static void async_initable_iface_init (GAsyncInitableIface *iface);
52
53 G_DEFINE_TYPE_EXTENDED (MbimDevice, mbim_device, G_TYPE_OBJECT, 0,
54 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init))
55
56 enum {
57 PROP_0,
58 PROP_FILE,
59 PROP_TRANSACTION_ID,
60 PROP_IN_SESSION,
61 PROP_CONSECUTIVE_TIMEOUTS,
62 PROP_LAST
63 };
64
65 static GParamSpec *properties[PROP_LAST];
66
67 enum {
68 SIGNAL_INDICATE_STATUS,
69 SIGNAL_ERROR,
70 SIGNAL_REMOVED,
71 SIGNAL_LAST
72 };
73
74 static guint signals[SIGNAL_LAST] = { 0 };
75
76 typedef enum {
77 TRANSACTION_TYPE_UNKNOWN = -1,
78 TRANSACTION_TYPE_HOST = 0,
79 TRANSACTION_TYPE_MODEM = 1,
80 TRANSACTION_TYPE_LAST = 2
81 } TransactionType;
82
83 typedef enum {
84 OPEN_STATUS_CLOSED = 0,
85 OPEN_STATUS_OPENING = 1,
86 OPEN_STATUS_OPEN = 2
87 } OpenStatus;
88
89 struct _MbimDevicePrivate {
90 /* File */
91 GFile *file;
92 gchar *path;
93 gchar *path_display;
94
95 /* WWAN interface */
96 gchar *wwan_iface;
97
98 /* I/O channel, set when the file is open */
99 GIOChannel *iochannel;
100 GSource *iochannel_source;
101 GByteArray *response;
102 OpenStatus open_status;
103 guint32 open_transaction_id;
104 GError *pending_error_indication;
105
106 /* Support for mbim-proxy */
107 GSocketClient *socket_client;
108 GSocketConnection *socket_connection;
109
110 /* HT to keep track of ongoing host/function transactions
111 * Host transactions: created by us
112 * Modem transactions: modem-created indications with multiple fragments
113 */
114 GHashTable *transactions[TRANSACTION_TYPE_LAST];
115
116 /* Transaction ID in the device */
117 guint32 transaction_id;
118
119 /* Flag to specify whether we're in a session */
120 gboolean in_session;
121
122 /* message size */
123 guint16 max_control_transfer;
124
125 /* MBIM extensions major and minor versions agreed with the device */
126 guint8 ms_mbimex_version_major;
127 guint8 ms_mbimex_version_minor;
128
129 /* Link management */
130 MbimNetPortManager *net_port_manager;
131
132 /* Number of consecutive timeouts detected */
133 guint consecutive_timeouts;
134 };
135
136 #define MAX_SPAWN_RETRIES 10
137 #define MAX_CONTROL_TRANSFER 4096
138 #define MAX_TIME_BETWEEN_FRAGMENTS_MS 1250
139
140 static void device_report_error (MbimDevice *self,
141 guint32 transaction_id,
142 const GError *error);
143
144 /*****************************************************************************/
145 /* Message transactions (private) */
146
147 typedef struct {
148 MbimDevice *self;
149 guint32 transaction_id;
150 TransactionType type;
151 } TransactionWaitContext;
152
153 typedef struct {
154 MbimMessage *fragments;
155 MbimMessageType type;
156 guint32 transaction_id;
157 GSource *timeout_source;
158 GCancellable *cancellable;
159 gulong cancellable_id;
160 TransactionWaitContext *wait_ctx;
161 } TransactionContext;
162
163 static void
164 transaction_context_free (TransactionContext *ctx)
165 {
166 if (ctx->fragments)
167 mbim_message_unref (ctx->fragments);
168
169 if (ctx->timeout_source) {
170 if (!g_source_is_destroyed (ctx->timeout_source))
171 g_source_destroy (ctx->timeout_source);
172 g_source_unref (ctx->timeout_source);
173 }
174
175 if (ctx->cancellable) {
176 if (ctx->cancellable_id)
177 g_cancellable_disconnect (ctx->cancellable, ctx->cancellable_id);
178 g_object_unref (ctx->cancellable);
179 }
180
181 if (ctx->wait_ctx)
182 g_slice_free (TransactionWaitContext, ctx->wait_ctx);
183
184 g_slice_free (TransactionContext, ctx);
185 }
186
187 /* #define TRACE_TRANSACTION 1 */
188 #ifdef TRACE_TRANSACTION
189 static void
190 transaction_task_trace (GTask *task,
191 const gchar *state)
192 {
193 MbimDevice *self;
194 TransactionContext *ctx;
195
196 self = g_task_get_source_object (task);
197 ctx = g_task_get_task_data (task);
198
199 g_debug ("[%s,%u] transaction %s: %s",
200 self->priv->path_display,
201 ctx->transaction_id,
202 mbim_message_type_get_string (ctx->type),
203 state);
204 }
205 #else
206 # define transaction_task_trace(...)
207 #endif
208
209 static GTask *
210 transaction_task_new (MbimDevice *self,
211 MbimMessageType type,
212 guint32 transaction_id,
213 GCancellable *cancellable,
214 GAsyncReadyCallback callback,
215 gpointer user_data)
216 {
217 GTask *task;
218 TransactionContext *ctx;
219
220 task = g_task_new (self, cancellable, callback, user_data);
221
222 ctx = g_slice_new0 (TransactionContext);
223 ctx->type = type;
224 ctx->transaction_id = transaction_id;
225 ctx->cancellable = (cancellable ? g_object_ref (cancellable) : NULL);
226 g_task_set_task_data (task, ctx, (GDestroyNotify) transaction_context_free);
227
228 transaction_task_trace (task, "new");
229
230 return task;
231 }
232
233 static void
234 transaction_task_complete_and_free (GTask *task,
235 const GError *error)
236 {
237 TransactionContext *ctx;
238 MbimDevice *self;
239
240 self = g_task_get_source_object (task);
241 ctx = g_task_get_task_data (task);
242
243 if (error) {
244 /* Increase number of consecutive timeouts */
245 if (g_error_matches (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_TIMEOUT) ||
246 g_error_matches (error, MBIM_PROTOCOL_ERROR, MBIM_PROTOCOL_ERROR_TIMEOUT_FRAGMENT)) {
247 self->priv->consecutive_timeouts++;
248 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_CONSECUTIVE_TIMEOUTS]);
249 g_debug ("[%s] number of consecutive timeouts: %u",
250 self->priv->path_display,
251 self->priv->consecutive_timeouts);
252 }
253 transaction_task_trace (task, "complete: error");
254 g_task_return_error (task, g_error_copy (error));
255 } else {
256 /* Reset number of consecutive timeouts */
257 if (self->priv->consecutive_timeouts > 0) {
258 g_debug ("[%s] reseted number of consecutive timeouts",
259 self->priv->path_display);
260 self->priv->consecutive_timeouts = 0;
261 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_CONSECUTIVE_TIMEOUTS]);
262 }
263 transaction_task_trace (task, "complete: response");
264 g_assert (ctx->fragments != NULL);
265 g_task_return_pointer (task, mbim_message_ref (ctx->fragments), (GDestroyNotify) mbim_message_unref);
266 }
267
268 g_object_unref (task);
269 }
270
271 static GTask *
272 device_release_transaction (MbimDevice *self,
273 TransactionType type,
274 MbimMessageType expected_type,
275 guint32 transaction_id)
276 {
277 GTask *task;
278 TransactionContext *ctx;
279
280 g_assert ((type != TRANSACTION_TYPE_UNKNOWN) && (type < TRANSACTION_TYPE_LAST));
281
282 /* Only return transaction if it was released from the HT */
283 if (!self->priv->transactions[type])
284 return NULL;
285
286 task = g_hash_table_lookup (self->priv->transactions[type], GUINT_TO_POINTER (transaction_id));
287 if (!task)
288 return NULL;
289
290 ctx = g_task_get_task_data (task);
291 if ((ctx->type == expected_type) || (expected_type == MBIM_MESSAGE_TYPE_INVALID)) {
292 /* If found, remove it from the HT */
293 transaction_task_trace (task, "release");
294 g_hash_table_remove (self->priv->transactions[type], GUINT_TO_POINTER (transaction_id));
295 return task;
296 }
297
298 return NULL;
299 }
300
301 static gboolean
302 transaction_timed_out (TransactionWaitContext *wait_ctx)
303 {
304 GTask *task;
305 TransactionContext *ctx;
306 g_autoptr(GError) error = NULL;
307
308 task = device_release_transaction (wait_ctx->self,
309 wait_ctx->type,
310 MBIM_MESSAGE_TYPE_INVALID,
311 wait_ctx->transaction_id);
312 if (!task)
313 /* transaction already completed */
314 return FALSE;
315
316 ctx = g_task_get_task_data (task);
317 ctx->timeout_source = NULL;
318
319 /* If no fragment was received, complete transaction with a timeout error */
320 if (!ctx->fragments) {
321 error = g_error_new (MBIM_CORE_ERROR,
322 MBIM_CORE_ERROR_TIMEOUT,
323 "Transaction timed out");
324 } else {
325 /* Fragment timeout... */
326 error = g_error_new (MBIM_PROTOCOL_ERROR,
327 MBIM_PROTOCOL_ERROR_TIMEOUT_FRAGMENT,
328 "Fragment timed out");
329
330 /* Also notify to the modem */
331 device_report_error (wait_ctx->self,
332 wait_ctx->transaction_id,
333 error);
334 }
335
336 transaction_task_complete_and_free (task, error);
337 return G_SOURCE_REMOVE;
338 }
339
340 static void
341 transaction_cancelled (GCancellable *cancellable,
342 TransactionWaitContext *wait_ctx)
343 {
344 GTask *task;
345 TransactionContext *ctx;
346 g_autoptr(GError) error = NULL;
347
348 task = device_release_transaction (wait_ctx->self,
349 wait_ctx->type,
350 MBIM_MESSAGE_TYPE_INVALID,
351 wait_ctx->transaction_id);
352
353 /* The transaction may have already been cancelled before we stored it in
354 * the tracking table */
355 if (!task)
356 return;
357
358 ctx = g_task_get_task_data (task);
359 ctx->cancellable_id = 0;
360
361 /* Complete transaction with an abort error */
362 error = g_error_new (MBIM_CORE_ERROR,
363 MBIM_CORE_ERROR_ABORTED,
364 "Transaction aborted");
365 transaction_task_complete_and_free (task, error);
366 }
367
368 static gboolean
369 device_store_transaction (MbimDevice *self,
370 TransactionType type,
371 GTask *task,
372 guint timeout_ms,
373 GError **error)
374 {
375 TransactionContext *ctx;
376
377 g_assert ((type != TRANSACTION_TYPE_UNKNOWN) && (type < TRANSACTION_TYPE_LAST));
378
379 transaction_task_trace (task, "store");
380
381 if (G_UNLIKELY (!self->priv->transactions[type]))
382 self->priv->transactions[type] = g_hash_table_new (g_direct_hash, g_direct_equal);
383
384 ctx = g_task_get_task_data (task);
385
386 /* When storing the transaction in the device, we have two options: either this
387 * is a completely new transaction, or this is a transaction that had already been
388 * previously stored (e.g. when waiting for more fragments). In the latter case,
389 * make sure we don't reset the wait context or the timeout. */
390
391 /* don't add timeout and setup wait context if one already exists */
392 if (!ctx->timeout_source) {
393 g_assert (!ctx->wait_ctx);
394 ctx->wait_ctx = g_slice_new (TransactionWaitContext);
395 ctx->wait_ctx->self = self;
396 ctx->wait_ctx->transaction_id = ctx->transaction_id;
397 ctx->wait_ctx->type = type;
398 ctx->timeout_source = g_timeout_source_new (timeout_ms);
399 g_source_set_callback (ctx->timeout_source, (GSourceFunc)transaction_timed_out, ctx->wait_ctx, NULL);
400 g_source_attach (ctx->timeout_source, g_main_context_get_thread_default ());
401 }
402
403 /* Indication transactions don't have cancellable */
404 if (ctx->cancellable && !ctx->cancellable_id) {
405 /* Note: transaction_cancelled() will also be called directly if the
406 * cancellable is already cancelled */
407 ctx->cancellable_id = g_cancellable_connect (ctx->cancellable,
408 (GCallback)transaction_cancelled,
409 ctx->wait_ctx,
410 NULL);
411 if (!ctx->cancellable_id) {
412 g_set_error_literal (error,
413 MBIM_CORE_ERROR,
414 MBIM_CORE_ERROR_ABORTED,
415 "Request is already cancelled");
416 return FALSE;
417 }
418 }
419
420 /* Keep in the HT */
421 g_hash_table_insert (self->priv->transactions[type], GUINT_TO_POINTER (ctx->transaction_id), task);
422
423 return TRUE;
424 }
425
426 /*****************************************************************************/
427
428 GFile *
429 mbim_device_get_file (MbimDevice *self)
430 {
431 GFile *file = NULL;
432
433 g_return_val_if_fail (MBIM_IS_DEVICE (self), NULL);
434
435 g_object_get (G_OBJECT (self),
436 MBIM_DEVICE_FILE, &file,
437 NULL);
438 return file;
439 }
440
441 GFile *
442 mbim_device_peek_file (MbimDevice *self)
443 {
444 g_return_val_if_fail (MBIM_IS_DEVICE (self), NULL);
445
446 return self->priv->file;
447 }
448
449 const gchar *
450 mbim_device_get_path (MbimDevice *self)
451 {
452 g_return_val_if_fail (MBIM_IS_DEVICE (self), NULL);
453
454 return self->priv->path;
455 }
456
457 const gchar *
458 mbim_device_get_path_display (MbimDevice *self)
459 {
460 g_return_val_if_fail (MBIM_IS_DEVICE (self), NULL);
461
462 return self->priv->path_display;
463 }
464
465 gboolean
466 mbim_device_is_open (MbimDevice *self)
467 {
468 g_return_val_if_fail (MBIM_IS_DEVICE (self), FALSE);
469
470 return (self->priv->open_status == OPEN_STATUS_OPEN);
471 }
472
473 guint8
474 mbim_device_get_ms_mbimex_version (MbimDevice *self,
475 guint8 *out_ms_mbimex_version_minor)
476 {
477 g_return_val_if_fail (MBIM_IS_DEVICE (self), 0);
478
479 if (out_ms_mbimex_version_minor)
480 *out_ms_mbimex_version_minor = self->priv->ms_mbimex_version_minor;
481
482 return self->priv->ms_mbimex_version_major;
483 }
484
485 gboolean
486 mbim_device_set_ms_mbimex_version (MbimDevice *self,
487 guint8 ms_mbimex_version_major,
488 guint8 ms_mbimex_version_minor,
489 GError **error)
490 {
491 g_return_val_if_fail (MBIM_IS_DEVICE (self), FALSE);
492
493 /* no checks that may make this method fail for now */
494 self->priv->ms_mbimex_version_major = ms_mbimex_version_major;
495 self->priv->ms_mbimex_version_minor = ms_mbimex_version_minor;
496 return TRUE;
497 }
498
499 gboolean
500 mbim_device_check_ms_mbimex_version (MbimDevice *self,
501 guint8 ms_mbimex_version_major,
502 guint8 ms_mbimex_version_minor)
503 {
504 g_return_val_if_fail (MBIM_IS_DEVICE (self), FALSE);
505
506 return ((self->priv->ms_mbimex_version_major > ms_mbimex_version_major) ||
507 ((self->priv->ms_mbimex_version_major == ms_mbimex_version_major) &&
508 (self->priv->ms_mbimex_version_minor >= ms_mbimex_version_minor)));
509 }
510
511 guint
512 mbim_device_get_consecutive_timeouts (MbimDevice *self)
513 {
514 g_return_val_if_fail (MBIM_IS_DEVICE (self), 0);
515
516 return self->priv->consecutive_timeouts;
517 }
518
519 /*****************************************************************************/
520
521 static void
522 reload_wwan_iface_name (MbimDevice *self)
523 {
524 g_autofree gchar *cdc_wdm_device_name = NULL;
525 guint i;
526 g_autoptr(GError) error = NULL;
527 static const gchar *driver_names[] = { "usbmisc", /* kernel >= 3.6 */
528 "usb" }; /* kernel < 3.6 */
529
530 g_clear_pointer (&self->priv->wwan_iface, g_free);
531
532 cdc_wdm_device_name = mbim_helpers_get_devname (self->priv->path, &error);
533 if (!cdc_wdm_device_name) {
534 g_warning ("[%s] invalid path for cdc-wdm control port: %s",
535 self->priv->path_display,
536 error->message);
537 return;
538 }
539
540 for (i = 0; i < G_N_ELEMENTS (driver_names) && !self->priv->wwan_iface; i++) {
541 g_autofree gchar *sysfs_path = NULL;
542 g_autoptr(GFile) sysfs_file = NULL;
543 g_autoptr(GFileEnumerator) enumerator = NULL;
544 GFileInfo *file_info = NULL;
545
546 /* WWAN iface name loading only applicable for cdc_mbim driver right now
547 * (so MBIM port exposed by the cdc-wdm driver in the usbmisc subsystem),
548 * not for any other subsystem or driver */
549 sysfs_path = g_strdup_printf ("/sys/class/%s/%s/device/net/", driver_names[i], cdc_wdm_device_name);
550 sysfs_file = g_file_new_for_path (sysfs_path);
551 enumerator = g_file_enumerate_children (sysfs_file,
552 G_FILE_ATTRIBUTE_STANDARD_NAME,
553 G_FILE_QUERY_INFO_NONE,
554 NULL,
555 NULL);
556 if (!enumerator)
557 continue;
558
559 /* Ignore errors when enumerating */
560 while ((file_info = g_file_enumerator_next_file (enumerator, NULL, NULL)) != NULL) {
561 const gchar *name;
562
563 name = g_file_info_get_name (file_info);
564 if (name) {
565 /* We only expect ONE file in the sysfs directory corresponding
566 * to this control port, if more found for any reason, warn about it */
567 if (self->priv->wwan_iface)
568 g_warning ("[%s] invalid additional wwan iface found: %s",
569 self->priv->path_display, name);
570 else
571 self->priv->wwan_iface = g_strdup (name);
572 }
573 g_object_unref (file_info);
574 }
575 if (!self->priv->wwan_iface)
576 g_warning ("[%s] wwan iface not found", self->priv->path_display);
577 }
578
579 /* wwan_iface won't be set at this point if the kernel driver in use isn't in
580 * the usbmisc subsystem */
581 }
582
583 static gboolean
584 setup_net_port_manager (MbimDevice *self,
585 GError **error)
586 {
587 /* If we have a valid one already, use that one */
588 if (self->priv->net_port_manager)
589 return TRUE;
590
591 reload_wwan_iface_name (self);
592 if (!self->priv->wwan_iface)
593 /* If wwan_iface is not set wwan subsystem is probably in use */
594 self->priv->net_port_manager = MBIM_NET_PORT_MANAGER (mbim_net_port_manager_wwan_new (error));
595 else
596 self->priv->net_port_manager = MBIM_NET_PORT_MANAGER (mbim_net_port_manager_wdm_new (self->priv->wwan_iface, error));
597
598 return !!self->priv->net_port_manager;
599 }
600
601 /*****************************************************************************/
602 /* Link management */
603
604 typedef struct {
605 guint session_id;
606 gchar *ifname;
607 } AddLinkResult;
608
609 static void
610 add_link_result_free (AddLinkResult *ctx)
611 {
612 g_free (ctx->ifname);
613 g_free (ctx);
614 }
615
616 gchar *
617 mbim_device_add_link_finish (MbimDevice *self,
618 GAsyncResult *res,
619 guint *session_id,
620 GError **error)
621 {
622 AddLinkResult *ctx;
623 gchar *ifname;
624
625 ctx = g_task_propagate_pointer (G_TASK (res), error);
626 if (!ctx)
627 return NULL;
628
629 if (session_id)
630 *session_id = ctx->session_id;
631
632 ifname = g_steal_pointer (&ctx->ifname);
633 add_link_result_free (ctx);
634 return ifname;
635 }
636
637 static void
638 device_add_link_ready (MbimNetPortManager *net_port_manager,
639 GAsyncResult *res,
640 GTask *task)
641 {
642 GError *error = NULL;
643 AddLinkResult *ctx;
644
645 ctx = g_new0 (AddLinkResult, 1);
646 ctx->ifname = mbim_net_port_manager_add_link_finish (net_port_manager, &ctx->session_id, res, &error);
647
648 if (!ctx->ifname) {
649 g_prefix_error (&error, "Could not allocate link: ");
650 g_task_return_error (task, error);
651 add_link_result_free (ctx);
652 } else
653 g_task_return_pointer (task, ctx, (GDestroyNotify) add_link_result_free);
654
655 g_object_unref (task);
656 }
657
658 void
659 mbim_device_add_link (MbimDevice *self,
660 guint session_id,
661 const gchar *base_ifname,
662 const gchar *ifname_prefix,
663 GCancellable *cancellable,
664 GAsyncReadyCallback callback,
665 gpointer user_data)
666 {
667 GTask *task;
668 GError *error = NULL;
669
670 g_return_if_fail (MBIM_IS_DEVICE (self));
671 g_return_if_fail (base_ifname);
672 g_return_if_fail ((session_id <= MBIM_DEVICE_SESSION_ID_MAX) || (session_id == MBIM_DEVICE_SESSION_ID_AUTOMATIC));
673
674 task = g_task_new (self, cancellable, callback, user_data);
675
676 if (!setup_net_port_manager (self, &error)) {
677 g_task_return_error (task, error);
678 g_object_unref (task);
679 return;
680 }
681
682 g_assert (self->priv->net_port_manager);
683 mbim_net_port_manager_add_link (self->priv->net_port_manager,
684 session_id,
685 base_ifname,
686 ifname_prefix,
687 5,
688 cancellable,
689 (GAsyncReadyCallback) device_add_link_ready,
690 task);
691 }
692
693 gboolean
694 mbim_device_delete_link_finish (MbimDevice *self,
695 GAsyncResult *res,
696 GError **error)
697 {
698 return g_task_propagate_boolean (G_TASK (res), error);
699 }
700
701 static void
702 device_del_link_ready (MbimNetPortManager *net_port_manager,
703 GAsyncResult *res,
704 GTask *task)
705 {
706 GError *error = NULL;
707
708 if (!mbim_net_port_manager_del_link_finish (net_port_manager, res, &error))
709 g_task_return_error (task, error);
710 else
711 g_task_return_boolean (task, TRUE);
712 g_object_unref (task);
713 }
714
715 void
716 mbim_device_delete_link (MbimDevice *self,
717 const gchar *ifname,
718 GCancellable *cancellable,
719 GAsyncReadyCallback callback,
720 gpointer user_data)
721 {
722 GTask *task;
723 GError *error = NULL;
724
725 g_return_if_fail (MBIM_IS_DEVICE (self));
726 g_return_if_fail (ifname);
727
728 task = g_task_new (self, cancellable, callback, user_data);
729
730 if (!setup_net_port_manager (self, &error)) {
731 g_task_return_error (task, error);
732 g_object_unref (task);
733 return;
734 }
735
736 g_assert (self->priv->net_port_manager);
737 mbim_net_port_manager_del_link (self->priv->net_port_manager,
738 ifname,
739 5, /* timeout */
740 cancellable,
741 (GAsyncReadyCallback) device_del_link_ready,
742 task);
743 }
744
745 /*****************************************************************************/
746
747 gboolean
748 mbim_device_delete_all_links_finish (MbimDevice *self,
749 GAsyncResult *res,
750 GError **error)
751 {
752 return g_task_propagate_boolean (G_TASK (res), error);
753 }
754
755 static void
756 device_del_all_links_ready (MbimNetPortManager *net_port_manager,
757 GAsyncResult *res,
758 GTask *task)
759 {
760 GError *error = NULL;
761
762 if (!mbim_net_port_manager_del_all_links_finish (net_port_manager, res, &error))
763 g_task_return_error (task, error);
764 else
765 g_task_return_boolean (task, TRUE);
766 g_object_unref (task);
767 }
768
769 void
770 mbim_device_delete_all_links (MbimDevice *self,
771 const gchar *base_ifname,
772 GCancellable *cancellable,
773 GAsyncReadyCallback callback,
774 gpointer user_data)
775 {
776 GTask *task;
777 GError *error = NULL;
778
779 g_return_if_fail (MBIM_IS_DEVICE (self));
780 g_return_if_fail (base_ifname);
781
782 task = g_task_new (self, cancellable, callback, user_data);
783
784 if (!setup_net_port_manager (self, &error)) {
785 g_task_return_error (task, error);
786 g_object_unref (task);
787 return;
788 }
789
790 g_assert (self->priv->net_port_manager);
791 mbim_net_port_manager_del_all_links (self->priv->net_port_manager,
792 base_ifname,
793 cancellable,
794 (GAsyncReadyCallback) device_del_all_links_ready,
795 task);
796 }
797
798 /*****************************************************************************/
799
800 gboolean
801 mbim_device_list_links (MbimDevice *self,
802 const gchar *base_ifname,
803 GPtrArray **out_links,
804 GError **error)
805 {
806 g_return_val_if_fail (MBIM_IS_DEVICE (self), FALSE);
807 g_return_val_if_fail (base_ifname, FALSE);
808
809 if (!setup_net_port_manager (self, error))
810 return FALSE;
811
812 g_assert (self->priv->net_port_manager);
813 return mbim_net_port_manager_list_links (self->priv->net_port_manager,
814 base_ifname,
815 out_links,
816 error);
817 }
818
819 /*****************************************************************************/
820
821 gboolean
822 mbim_device_check_link_supported (MbimDevice *self,
823 GError **error)
824 {
825 g_return_val_if_fail (MBIM_IS_DEVICE (self), FALSE);
826
827 /* if we can setup a net port manager, link management is supported */
828 return setup_net_port_manager (self, error);
829 }
830
831 /*****************************************************************************/
832 /* Open device */
833
834 static void
835 indication_ready (MbimDevice *self,
836 GAsyncResult *res)
837 {
838 g_autoptr(GError) error = NULL;
839 g_autoptr(MbimMessage) indication = NULL;
840
841 if (!(indication = g_task_propagate_pointer (G_TASK (res), &error))) {
842 g_debug ("[%s] error processing indication message: %s",
843 self->priv->path_display,
844 error->message);
845 return;
846 }
847
848 /* Indications in the internal proxy control service are not emitted as
849 * signals, they're consumed internally */
850 {
851 guint16 mbim_version;
852 guint16 ms_mbimex_version;
853
854 if ((mbim_message_indicate_status_get_service (indication) == MBIM_SERVICE_PROXY_CONTROL) &&
855 (mbim_message_indicate_status_get_cid (indication) == MBIM_CID_PROXY_CONTROL_VERSION) &&
856 mbim_message_proxy_control_version_notification_parse (indication, &mbim_version, &ms_mbimex_version, NULL)) {
857
858 self->priv->ms_mbimex_version_major = (ms_mbimex_version >> 8) & 0xFF;
859 self->priv->ms_mbimex_version_minor = ms_mbimex_version & 0xFF;
860
861 g_debug ("[%s] version information update reported: version %x.%02x, extended version %x.%02x",
862 self->priv->path_display,
863 (mbim_version >> 8) & 0xFF,
864 mbim_version & 0xFF,
865 self->priv->ms_mbimex_version_major,
866 self->priv->ms_mbimex_version_minor);
867 return;
868 }
869 }
870
871 g_signal_emit (self, signals[SIGNAL_INDICATE_STATUS], 0, indication);
872 }
873
874 static void
875 finalize_pending_open_request (MbimDevice *self)
876 {
877 GTask *task;
878 g_autoptr(GError) error = NULL;
879
880 if (!self->priv->open_transaction_id)
881 return;
882
883 /* Grab transaction. This is a _DONE message, so look for the request
884 * that generated the _DONE */
885 task = device_release_transaction (self,
886 TRANSACTION_TYPE_HOST,
887 MBIM_MESSAGE_TYPE_OPEN,
888 self->priv->open_transaction_id);
889
890 /* If there is a valid open_transaction_id, there must be a valid transaction */
891 g_assert (task);
892
893 /* Clear right away before completing the transaction */
894 self->priv->open_transaction_id = 0;
895
896 error = g_error_new (MBIM_CORE_ERROR, MBIM_CORE_ERROR_UNKNOWN_STATE, "device state is unknown");
897 transaction_task_complete_and_free (task, error);
898 }
899
900 static void
901 process_message (MbimDevice *self,
902 const MbimMessage *message)
903 {
904 gboolean is_partial_fragment;
905
906 is_partial_fragment = (_mbim_message_is_fragment (message) &&
907 _mbim_message_fragment_get_total (message) > 1);
908
909 if (mbim_utils_get_traces_enabled ()) {
910 g_autofree gchar *printable = NULL;
911
912 if (mbim_utils_get_show_personal_info () || (((GByteArray *)message)->len < MAX_PRINTED_BYTES)) {
913 printable = mbim_common_str_hex (((GByteArray *)message)->data,
914 ((GByteArray *)message)->len,
915 ':');
916 } else {
917 g_autofree gchar *tmp = NULL;
918
919 tmp = mbim_common_str_hex (((GByteArray *)message)->data, MAX_PRINTED_BYTES, ':');
920 printable = g_strdup_printf ("%s...", tmp);
921 }
922
923 g_debug ("[%s] received message...%s\n"
924 ">>>>>> RAW:\n"
925 ">>>>>> length = %u\n"
926 ">>>>>> data = %s\n",
927 self->priv->path_display,
928 is_partial_fragment ? " (partial fragment)" : "",
929 ((GByteArray *)message)->len,
930 printable);
931
932 if (is_partial_fragment) {
933 g_autofree gchar *translated = NULL;
934
935 translated = mbim_message_get_printable_full (message,
936 self->priv->ms_mbimex_version_major,
937 self->priv->ms_mbimex_version_minor,
938 ">>>>>> ",
939 TRUE,
940 NULL);
941 g_debug ("[%s] received message fragment (translated)...\n%s",
942 self->priv->path_display,
943 translated);
944 }
945 }
946
947 switch (MBIM_MESSAGE_GET_MESSAGE_TYPE (message)) {
948 case MBIM_MESSAGE_TYPE_OPEN_DONE:
949 case MBIM_MESSAGE_TYPE_CLOSE_DONE:
950 case MBIM_MESSAGE_TYPE_COMMAND_DONE:
951 case MBIM_MESSAGE_TYPE_INDICATE_STATUS: {
952 g_autoptr(GError) error = NULL;
953 GTask *task;
954 TransactionContext *ctx;
955 TransactionType transaction_type = TRANSACTION_TYPE_UNKNOWN;
956
957 if (MBIM_MESSAGE_GET_MESSAGE_TYPE (message) == MBIM_MESSAGE_TYPE_INDICATE_STATUS) {
958 /* Grab transaction */
959 transaction_type = TRANSACTION_TYPE_MODEM;
960 task = device_release_transaction (self,
961 transaction_type,
962 MBIM_MESSAGE_TYPE_INDICATE_STATUS,
963 mbim_message_get_transaction_id (message));
964
965 if (!task)
966 /* Create new transaction for the indication */
967 task = transaction_task_new (self,
968 MBIM_MESSAGE_TYPE_INDICATE_STATUS,
969 mbim_message_get_transaction_id (message),
970 NULL, /* no cancellable */
971 (GAsyncReadyCallback) indication_ready,
972 NULL);
973 } else {
974 /* Grab transaction. This is a _DONE message, so look for the request
975 * that generated the _DONE */
976 transaction_type = TRANSACTION_TYPE_HOST;
977 task = device_release_transaction (self,
978 transaction_type,
979 (MBIM_MESSAGE_GET_MESSAGE_TYPE (message) - 0x80000000),
980 mbim_message_get_transaction_id (message));
981 if (!task) {
982 g_debug ("[%s] no transaction matched in received message",
983 self->priv->path_display);
984
985 /* Attempt to print a user friendly dump of the packet anyway */
986 if (mbim_utils_get_traces_enabled ()) {
987 g_autofree gchar *printable = NULL;
988
989 printable = mbim_message_get_printable_full (message,
990 self->priv->ms_mbimex_version_major,
991 self->priv->ms_mbimex_version_minor,
992 ">>>>>> ",
993 is_partial_fragment,
994 NULL);
995 if (printable)
996 g_debug ("[%s] received unexpected message (translated)...\n%s",
997 self->priv->path_display,
998 printable);
999 }
1000
1001 /* If we're opening and we get a CLOSE_DONE message without any
1002 * matched transaction, finalize the open request right away to
1003 * trigger a close before open */
1004 if (self->priv->open_status == OPEN_STATUS_OPENING &&
1005 MBIM_MESSAGE_GET_MESSAGE_TYPE (message) == MBIM_MESSAGE_TYPE_CLOSE_DONE)
1006 finalize_pending_open_request (self);
1007
1008 return;
1009 }
1010
1011 /* If the message doesn't have fragments, we're done */
1012 if (!_mbim_message_is_fragment (message)) {
1013 ctx = g_task_get_task_data (task);
1014 g_assert (ctx->fragments == NULL);
1015 ctx->fragments = mbim_message_dup (message);
1016 if (mbim_utils_get_traces_enabled ()) {
1017 g_autofree gchar *printable = NULL;
1018
1019 printable = mbim_message_get_printable_full (ctx->fragments,
1020 self->priv->ms_mbimex_version_major,
1021 self->priv->ms_mbimex_version_minor,
1022 ">>>>>> ",
1023 FALSE,
1024 NULL);
1025 g_debug ("[%s] received message (translated)...\n%s",
1026 self->priv->path_display,
1027 printable);
1028 }
1029 transaction_task_complete_and_free (task, NULL);
1030 return;
1031 }
1032 }
1033
1034 /* More than one fragment expected; is this the first one? */
1035 ctx = g_task_get_task_data (task);
1036 if (!ctx->fragments)
1037 ctx->fragments = _mbim_message_fragment_collector_init (message, &error);
1038 else
1039 _mbim_message_fragment_collector_add (ctx->fragments, message, &error);
1040
1041 if (error) {
1042 device_report_error (self, ctx->transaction_id, error);
1043 transaction_task_complete_and_free (task, error);
1044 return;
1045 }
1046
1047 /* Did we get all needed fragments? */
1048 if (_mbim_message_fragment_collector_complete (ctx->fragments)) {
1049 /* Now, translate the whole message */
1050 if (mbim_utils_get_traces_enabled ()) {
1051 g_autofree gchar *printable = NULL;
1052
1053 printable = mbim_message_get_printable_full (ctx->fragments,
1054 self->priv->ms_mbimex_version_major,
1055 self->priv->ms_mbimex_version_minor,
1056 ">>>>>> ",
1057 FALSE,
1058 NULL);
1059 g_debug ("[%s] received message (translated)...\n%s",
1060 self->priv->path_display,
1061 printable);
1062 }
1063
1064 transaction_task_complete_and_free (task, NULL);
1065 return;
1066 }
1067
1068 /* Need more fragments, store transaction */
1069 g_assert (device_store_transaction (self,
1070 transaction_type,
1071 task,
1072 MAX_TIME_BETWEEN_FRAGMENTS_MS,
1073 NULL));
1074 return;
1075 }
1076
1077 case MBIM_MESSAGE_TYPE_FUNCTION_ERROR: {
1078 g_autoptr(GError) error_indication = NULL;
1079 GTask *task;
1080
1081 if (mbim_utils_get_traces_enabled ()) {
1082 g_autofree gchar *printable = NULL;
1083
1084 printable = mbim_message_get_printable_full (message,
1085 self->priv->ms_mbimex_version_major,
1086 self->priv->ms_mbimex_version_minor,
1087 ">>>>>> ",
1088 FALSE,
1089 NULL);
1090 g_debug ("[%s] received message (translated)...\n%s",
1091 self->priv->path_display,
1092 printable);
1093 }
1094
1095 /* Build indication error before task completion, to ensure the message
1096 * is valid by the time it is sent. This error will be cleared if the device
1097 * gets closed while the transaction response is processed. */
1098 g_assert (!self->priv->pending_error_indication);
1099 self->priv->pending_error_indication = mbim_message_error_get_error (message);
1100
1101 /* Try to match this transaction just per transaction ID */
1102 task = device_release_transaction (self,
1103 TRANSACTION_TYPE_HOST,
1104 MBIM_MESSAGE_TYPE_INVALID,
1105 mbim_message_get_transaction_id (message));
1106
1107 if (!task) {
1108 g_debug ("[%s] No transaction matched in received function error message",
1109 self->priv->path_display);
1110
1111 } else {
1112 TransactionContext *ctx;
1113
1114 ctx = g_task_get_task_data (task);
1115
1116 if (ctx->fragments)
1117 mbim_message_unref (ctx->fragments);
1118 ctx->fragments = mbim_message_dup (message);
1119 transaction_task_complete_and_free (task, NULL);
1120 }
1121
1122 /* Signals are emitted regardless of whether the transaction matched or not,
1123 * and emitted after the task completion. If the device is forced closed during
1124 * the transaction response processing, no error signal will be emitted. */
1125 error_indication = g_steal_pointer (&self->priv->pending_error_indication);
1126 if (error_indication)
1127 g_signal_emit (self, signals[SIGNAL_ERROR], 0, error_indication);
1128 return;
1129 }
1130
1131 case MBIM_MESSAGE_TYPE_INVALID:
1132 case MBIM_MESSAGE_TYPE_OPEN:
1133 case MBIM_MESSAGE_TYPE_CLOSE:
1134 case MBIM_MESSAGE_TYPE_COMMAND:
1135 case MBIM_MESSAGE_TYPE_HOST_ERROR:
1136 default:
1137 /* Shouldn't expect host-generated messages as replies */
1138 g_message ("[%s] Host-generated message received: ignoring",
1139 self->priv->path_display);
1140 return;
1141 }
1142 }
1143
1144 static void
1145 parse_response (MbimDevice *self)
1146 {
1147 do {
1148 const MbimMessage *message;
1149 guint32 len;
1150 g_autoptr(GError) error = NULL;
1151
1152 message = (const MbimMessage *)self->priv->response;
1153
1154 /* Invalid message? */
1155 if (!_mbim_message_validate_internal (message, TRUE, &error)) {
1156 /* No full message yet */
1157 if (g_error_matches (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INCOMPLETE_MESSAGE))
1158 return;
1159
1160 /* Invalid MBIM message */
1161 g_warning ("[%s] discarding %u bytes in stream as message validation fails: %s",
1162 self->priv->path_display, self->priv->response->len,
1163 error->message);
1164 g_byte_array_remove_range (self->priv->response, 0, self->priv->response->len);
1165 return;
1166 }
1167
1168 /* Play with the received message */
1169 len = mbim_message_get_message_length (message);
1170 process_message (self, message);
1171
1172 /* If we were force-closed during the processing of a message, we'd be
1173 * losing the response array directly, so check just in case */
1174 if (!self->priv->response)
1175 break;
1176
1177 /* Remove message from buffer */
1178 g_byte_array_remove_range (self->priv->response, 0, len);
1179 } while (self->priv->response->len > 0);
1180 }
1181
1182 static gboolean
1183 data_available (GIOChannel *source,
1184 GIOCondition condition,
1185 MbimDevice *self)
1186 {
1187 gsize bytes_read;
1188 GIOStatus status;
1189 gchar buffer[MAX_CONTROL_TRANSFER + 1];
1190
1191 if (condition & G_IO_HUP) {
1192 g_debug ("[%s] unexpected port hangup!",
1193 self->priv->path_display);
1194
1195 if (self->priv->response &&
1196 self->priv->response->len)
1197 g_byte_array_remove_range (self->priv->response, 0, self->priv->response->len);
1198
1199 mbim_device_close_force (self, NULL);
1200 g_signal_emit (self, signals[SIGNAL_REMOVED], 0 );
1201 return FALSE;
1202 }
1203
1204 if (condition & G_IO_ERR) {
1205 if (self->priv->response &&
1206 self->priv->response->len)
1207 g_byte_array_remove_range (self->priv->response, 0, self->priv->response->len);
1208 return TRUE;
1209 }
1210
1211 /* If not ready yet, prepare the response with default initial size. */
1212 if (G_UNLIKELY (!self->priv->response))
1213 self->priv->response = g_byte_array_sized_new (500);
1214
1215 /* The parse_response() message may end up triggering a close of the
1216 * MbimDevice or even a full unref. We are going to make sure a valid
1217 * reference is available for as long as we need it in the while()
1218 * loop. */
1219 g_object_ref (self);
1220 {
1221 do {
1222 g_autoptr(GError) error = NULL;
1223
1224 /* Port is closed; we're done */
1225 if (!self->priv->iochannel_source)
1226 break;
1227
1228 status = g_io_channel_read_chars (source,
1229 buffer,
1230 self->priv->max_control_transfer,
1231 &bytes_read,
1232 &error);
1233 if (status == G_IO_STATUS_ERROR && error)
1234 g_warning ("[%s] error reading from the IOChannel: '%s'",
1235 self->priv->path_display,
1236 error->message);
1237
1238 /* If no bytes read, just let g_io_channel wait for more data */
1239 if (bytes_read == 0)
1240 break;
1241
1242 if (bytes_read > 0)
1243 g_byte_array_append (self->priv->response, (const guint8 *)buffer, bytes_read);
1244
1245 /* Try to parse what we already got */
1246 parse_response (self);
1247
1248 /* And keep on if we were told to keep on */
1249 } while (bytes_read == self->priv->max_control_transfer || status == G_IO_STATUS_AGAIN);
1250 }
1251 g_object_unref (self);
1252
1253 return TRUE;
1254 }
1255
1256 /* "MBIM Control Model Functional Descriptor" */
1257 struct usb_cdc_mbim_desc {
1258 guint8 bLength;
1259 guint8 bDescriptorType;
1260 guint8 bDescriptorSubType;
1261 guint16 bcdMBIMVersion;
1262 guint16 wMaxControlMessage;
1263 guint8 bNumberFilters;
1264 guint8 bMaxFilterSize;
1265 guint16 wMaxSegmentSize;
1266 guint8 bmNetworkCapabilities;
1267 } __attribute__ ((packed));
1268
1269 static gchar *
1270 get_descriptors_filepath (MbimDevice *self)
1271 {
1272 static const gchar *subsystems[] = { "usbmisc", "usb" };
1273 g_autofree gchar *device_basename = NULL;
1274 g_autofree gchar *descriptors_path = NULL;
1275 guint i;
1276
1277 device_basename = g_path_get_basename (self->priv->path);
1278
1279 for (i = 0; i < G_N_ELEMENTS (subsystems); i++) {
1280 g_autofree gchar *tmp = NULL;
1281 g_autofree gchar *path = NULL;
1282
1283 /* parent sysfs can be built directly using subsystem and name; e.g. for subsystem
1284 * usbmisc and name cdc-wdm0:
1285 * $ realpath /sys/class/usbmisc/cdc-wdm0/device
1286 * /sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.5/2-1.5:2.0
1287 */
1288 tmp = g_strdup_printf ("/sys/class/%s/%s/device", subsystems[i], device_basename);
1289 path = realpath (tmp, NULL);
1290
1291 if (path && g_file_test (path, G_FILE_TEST_EXISTS)) {
1292 /* Now look for the parent dir with descriptors file. */
1293 g_autofree gchar *dirname = NULL;
1294
1295 dirname = g_path_get_dirname (path);
1296 descriptors_path = g_build_path (G_DIR_SEPARATOR_S, dirname, "descriptors", NULL);
1297 break;
1298 }
1299 }
1300
1301 if (descriptors_path && !g_file_test (descriptors_path, G_FILE_TEST_EXISTS)) {
1302 g_warning ("[%s] descriptors file doesn't exist", self->priv->path_display);
1303 return NULL;
1304 }
1305
1306 return g_steal_pointer (&descriptors_path);
1307 }
1308
1309 static guint16
1310 read_max_control_transfer (MbimDevice *self)
1311 {
1312 static const guint8 mbim_signature[4] = { 0x0c, 0x24, 0x1b, 0x00 };
1313 g_autoptr(GError) error = NULL;
1314 g_autofree gchar *descriptors_path = NULL;
1315 g_autofree gchar *contents = NULL;
1316 gsize length = 0;
1317 guint i;
1318
1319 /* Build descriptors filepath */
1320 descriptors_path = get_descriptors_filepath (self);
1321 if (!descriptors_path) {
1322 /* If descriptors file doesn't exist, it's probably because we're using
1323 * some other kernel driver, not the cdc_wdm/cdc_mbim pair, so fallback to
1324 * the default and avoid warning about it. */
1325 g_debug ("[%s] couldn't find descriptors file, possibly not using cdc_mbim",
1326 self->priv->path_display);
1327 g_debug ("[%s] fallback to default max control message size: %u",
1328 self->priv->path_display, MAX_CONTROL_TRANSFER);
1329 return MAX_CONTROL_TRANSFER;
1330 }
1331
1332 if (!g_file_get_contents (descriptors_path,
1333 &contents,
1334 &length,
1335 &error)) {
1336 g_warning ("[%s] couldn't read descriptors file: %s",
1337 self->priv->path_display,
1338 error->message);
1339 return MAX_CONTROL_TRANSFER;
1340 }
1341
1342 i = 0;
1343 while (i <= (length - sizeof (struct usb_cdc_mbim_desc))) {
1344 /* Try to match the MBIM descriptor signature */
1345 if ((memcmp (&contents[i], mbim_signature, sizeof (mbim_signature)) == 0)) {
1346 guint16 max;
1347
1348 /* Found! */
1349 max = GUINT16_FROM_LE (((struct usb_cdc_mbim_desc *)&contents[i])->wMaxControlMessage);
1350 g_debug ("[%s] read max control message size from descriptors file: %" G_GUINT16_FORMAT,
1351 self->priv->path_display,
1352 max);
1353 return max;
1354 }
1355
1356 /* The first byte of the descriptor info is the length; so keep on
1357 * skipping descriptors until we match the MBIM one */
1358 i += contents[i];
1359 }
1360
1361 g_warning ("[%s] couldn't find MBIM signature in descriptors file",
1362 self->priv->path_display);
1363 return MAX_CONTROL_TRANSFER;
1364 }
1365
1366 typedef struct {
1367 guint spawn_retries;
1368 } CreateIoChannelContext;
1369
1370 static void
1371 create_iochannel_context_free (CreateIoChannelContext *ctx)
1372 {
1373 g_slice_free (CreateIoChannelContext, ctx);
1374 }
1375
1376 static gboolean
1377 create_iochannel_finish (MbimDevice *self,
1378 GAsyncResult *res,
1379 GError **error)
1380 {
1381 return g_task_propagate_boolean (G_TASK (res), error);
1382 }
1383
1384 static void
1385 setup_iochannel (GTask *task)
1386 {
1387 MbimDevice *self;
1388 GError *inner_error = NULL;
1389
1390 self = g_task_get_source_object (task);
1391
1392 /* We don't want UTF-8 encoding, we're playing with raw binary data */
1393 g_io_channel_set_encoding (self->priv->iochannel, NULL, NULL);
1394
1395 /* We don't want to get the channel buffered */
1396 g_io_channel_set_buffered (self->priv->iochannel, FALSE);
1397
1398 /* Let the GIOChannel own the FD */
1399 g_io_channel_set_close_on_unref (self->priv->iochannel, TRUE);
1400
1401 /* We don't want to get blocked while writing stuff */
1402 if (!g_io_channel_set_flags (self->priv->iochannel,
1403 G_IO_FLAG_NONBLOCK,
1404 &inner_error)) {
1405 g_io_channel_shutdown (self->priv->iochannel, FALSE, NULL);
1406 g_io_channel_unref (self->priv->iochannel);
1407 self->priv->iochannel = NULL;
1408 g_clear_object (&self->priv->socket_connection);
1409 g_clear_object (&self->priv->socket_client);
1410 g_task_return_error (task, inner_error);
1411 g_object_unref (task);
1412 return;
1413 }
1414
1415 self->priv->iochannel_source = g_io_create_watch (self->priv->iochannel,
1416 G_IO_IN | G_IO_ERR | G_IO_HUP);
1417 g_source_set_callback (self->priv->iochannel_source,
1418 (GSourceFunc)data_available,
1419 self,
1420 NULL);
1421 g_source_attach (self->priv->iochannel_source, g_main_context_get_thread_default ());
1422
1423 g_task_return_boolean (task, TRUE);
1424 g_object_unref (task);
1425 }
1426
1427 static void
1428 create_iochannel_with_fd (GTask *task)
1429 {
1430 MbimDevice *self;
1431 gint fd;
1432 guint16 max;
1433
1434 self = g_task_get_source_object (task);
1435 errno = 0;
1436 fd = open (self->priv->path, O_RDWR | O_EXCL | O_NONBLOCK | O_NOCTTY);
1437 if (fd < 0) {
1438 g_task_return_new_error (task,
1439 MBIM_CORE_ERROR,
1440 MBIM_CORE_ERROR_FAILED,
1441 "Cannot open device file '%s': %s",
1442 self->priv->path_display,
1443 strerror (errno));
1444 g_object_unref (task);
1445 return;
1446 }
1447
1448 /* Query message size */
1449 if (ioctl (fd, IOCTL_WDM_MAX_COMMAND, &max) < 0) {
1450 g_debug ("[%s] couldn't query maximum message size: "
1451 "IOCTL_WDM_MAX_COMMAND failed: %s",
1452 self->priv->path_display,
1453 strerror (errno));
1454 /* Fallback, try to read the descriptor file */
1455 max = read_max_control_transfer (self);
1456 } else {
1457 g_debug ("[%s] queried max control message size: %" G_GUINT16_FORMAT,
1458 self->priv->path_display,
1459 max);
1460 }
1461 self->priv->max_control_transfer = max;
1462
1463 /* Create new GIOChannel */
1464 self->priv->iochannel = g_io_channel_unix_new (fd);
1465
1466 setup_iochannel (task);
1467 }
1468
1469 static void create_iochannel_with_socket (GTask *task);
1470
1471 static gboolean
1472 wait_for_proxy_cb (GTask *task)
1473 {
1474 create_iochannel_with_socket (task);
1475 return FALSE;
1476 }
1477
1478 static void
1479 spawn_child_setup (void)
1480 {
1481 if (setpgid (0, 0) < 0)
1482 g_warning ("couldn't setup proxy specific process group");
1483 }
1484
1485 static void
1486 create_iochannel_with_socket (GTask *task)
1487 {
1488 MbimDevice *self;
1489 CreateIoChannelContext *ctx;
1490 g_autoptr(GSocketAddress) socket_address = NULL;
1491 GError *error = NULL;
1492
1493 self = g_task_get_source_object (task);
1494 ctx = g_task_get_task_data (task);
1495
1496 /* Create socket client */
1497 if (self->priv->socket_client)
1498 g_object_unref (self->priv->socket_client);
1499 self->priv->socket_client = g_socket_client_new ();
1500 g_socket_client_set_family (self->priv->socket_client, G_SOCKET_FAMILY_UNIX);
1501 g_socket_client_set_socket_type (self->priv->socket_client, G_SOCKET_TYPE_STREAM);
1502 g_socket_client_set_protocol (self->priv->socket_client, G_SOCKET_PROTOCOL_DEFAULT);
1503
1504 /* Setup socket address */
1505 socket_address = (g_unix_socket_address_new_with_type (
1506 MBIM_PROXY_SOCKET_PATH,
1507 -1,
1508 G_UNIX_SOCKET_ADDRESS_ABSTRACT));
1509
1510 /* Connect to address */
1511 if (self->priv->socket_connection)
1512 g_object_unref (self->priv->socket_connection);
1513 self->priv->socket_connection = (g_socket_client_connect (
1514 self->priv->socket_client,
1515 G_SOCKET_CONNECTABLE (socket_address),
1516 NULL,
1517 &error));
1518
1519 if (!self->priv->socket_connection) {
1520 g_auto(GStrv) argc = NULL;
1521 g_autoptr(GSource) source = NULL;
1522
1523 g_debug ("[%s] cannot connect to proxy: %s", self->priv->path_display, error->message);
1524 g_clear_error (&error);
1525 g_clear_object (&self->priv->socket_client);
1526
1527 /* Don't retry forever */
1528 ctx->spawn_retries++;
1529 if (ctx->spawn_retries > MAX_SPAWN_RETRIES) {
1530 g_task_return_new_error (task,
1531 MBIM_CORE_ERROR,
1532 MBIM_CORE_ERROR_FAILED,
1533 "Couldn't spawn the mbim-proxy");
1534 g_object_unref (task);
1535 return;
1536 }
1537
1538 g_debug ("[%s] spawning new mbim-proxy (try %u)...", self->priv->path_display, ctx->spawn_retries);
1539
1540 argc = g_new0 (gchar *, 2);
1541 argc[0] = g_strdup (LIBEXEC_PATH "/mbim-proxy");
1542 if (!g_spawn_async (NULL, /* working directory */
1543 argc,
1544 NULL, /* envp */
1545 G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
1546 (GSpawnChildSetupFunc) spawn_child_setup,
1547 NULL, /* child_setup_user_data */
1548 NULL,
1549 &error)) {
1550 g_debug ("[%s] error spawning mbim-proxy: %s", self->priv->path_display, error->message);
1551 g_clear_error (&error);
1552 }
1553
1554 /* Wait some ms and retry */
1555 source = g_timeout_source_new (100);
1556 g_source_set_callback (source, (GSourceFunc)wait_for_proxy_cb, task, NULL);
1557 g_source_attach (source, g_main_context_get_thread_default ());
1558 return;
1559 }
1560
1561 self->priv->iochannel = g_io_channel_unix_new (
1562 g_socket_get_fd (
1563 g_socket_connection_get_socket (self->priv->socket_connection)));
1564
1565 /* try to read the descriptor file */
1566 self->priv->max_control_transfer = read_max_control_transfer (self);
1567
1568 setup_iochannel (task);
1569 }
1570
1571 static void
1572 create_iochannel (MbimDevice *self,
1573 gboolean proxy,
1574 GAsyncReadyCallback callback,
1575 gpointer user_data)
1576 {
1577 CreateIoChannelContext *ctx;
1578 GTask *task;
1579
1580 ctx = g_slice_new (CreateIoChannelContext);
1581 ctx->spawn_retries = 0;
1582
1583 task = g_task_new (self, NULL, callback, user_data);
1584 g_task_set_task_data (task, ctx, (GDestroyNotify)create_iochannel_context_free);
1585
1586 if (self->priv->iochannel) {
1587 g_task_return_new_error (task,
1588 MBIM_CORE_ERROR,
1589 MBIM_CORE_ERROR_WRONG_STATE,
1590 "Already open");
1591 g_object_unref (task);
1592 return;
1593 }
1594
1595 g_assert (self->priv->file);
1596 g_assert (self->priv->path);
1597
1598 if (proxy)
1599 create_iochannel_with_socket (task);
1600 else
1601 create_iochannel_with_fd (task);
1602 }
1603
1604 typedef enum {
1605 DEVICE_OPEN_CONTEXT_STEP_FIRST = 0,
1606 DEVICE_OPEN_CONTEXT_STEP_CREATE_IOCHANNEL,
1607 DEVICE_OPEN_CONTEXT_STEP_FLAGS_PROXY,
1608 DEVICE_OPEN_CONTEXT_STEP_CLOSE_MESSAGE,
1609 DEVICE_OPEN_CONTEXT_STEP_OPEN_MESSAGE,
1610 DEVICE_OPEN_CONTEXT_STEP_DEVICE_SERVICES,
1611 DEVICE_OPEN_CONTEXT_STEP_MS_EXT_VERSION,
1612 DEVICE_OPEN_CONTEXT_STEP_LAST
1613 } DeviceOpenContextStep;
1614
1615 typedef struct {
1616 DeviceOpenContextStep step;
1617 MbimDeviceOpenFlags flags;
1618 guint timeout;
1619 GTimer *timer;
1620 gboolean close_before_open;
1621 } DeviceOpenContext;
1622
1623 static void
1624 device_open_context_free (DeviceOpenContext *ctx)
1625 {
1626 g_timer_destroy (ctx->timer);
1627 g_slice_free (DeviceOpenContext, ctx);
1628 }
1629
1630 gboolean
1631 mbim_device_open_full_finish (MbimDevice *self,
1632 GAsyncResult *res,
1633 GError **error)
1634 {
1635 return g_task_propagate_boolean (G_TASK (res), error);
1636 }
1637
1638 gboolean
1639 mbim_device_open_finish (MbimDevice *self,
1640 GAsyncResult *res,
1641 GError **error)
1642 {
1643 return mbim_device_open_full_finish (self, res, error);
1644 }
1645
1646 static void device_open_context_step (GTask *task);
1647
1648 static void
1649 ms_ext_version_message_ready (MbimDevice *self,
1650 GAsyncResult *res,
1651 GTask *task)
1652 {
1653 g_autoptr(MbimMessage) response = NULL;
1654 GError *error = NULL;
1655 guint16 mbim_version;
1656 guint16 ms_mbimex_version;
1657 DeviceOpenContext *ctx;
1658
1659 ctx = g_task_get_task_data (task);
1660
1661 response = mbim_device_command_finish (self, res, &error);
1662 if (!response ||
1663 !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error) ||
1664 !mbim_message_ms_basic_connect_extensions_v2_version_response_parse (
1665 response,
1666 &mbim_version,
1667 &ms_mbimex_version,
1668 &error)){
1669 g_task_return_error (task, error);
1670 g_object_unref (task);
1671 return;
1672 }
1673
1674 /* We fully ignore the MBIM version for now, we just assume it's 1.0, which
1675 * is the only known release from the USB-IF for now. */
1676 self->priv->ms_mbimex_version_major = (ms_mbimex_version >> 8) & 0xFF;
1677 self->priv->ms_mbimex_version_minor = ms_mbimex_version & 0xFF;
1678
1679 g_debug ("[%s] successfully exchanged version information: version %x.%02x, extended version %x.%02x",
1680 self->priv->path_display,
1681 (mbim_version >> 8) & 0xFF,
1682 mbim_version & 0xFF,
1683 self->priv->ms_mbimex_version_major,
1684 self->priv->ms_mbimex_version_minor);
1685
1686 ctx->step++;
1687 device_open_context_step (task);
1688 }
1689
1690 static void
1691 ms_ext_version_message (GTask *task)
1692 {
1693 MbimDevice *self;
1694 DeviceOpenContext *ctx;
1695 g_autoptr(MbimMessage) request = NULL;
1696 guint32 mbim_version = 0;
1697 guint32 ms_mbimex_version = 0;
1698
1699 self = g_task_get_source_object (task);
1700 ctx = g_task_get_task_data (task);
1701
1702 if ((ctx->flags & MBIM_DEVICE_OPEN_FLAGS_MS_MBIMEX_V2) && (ctx->flags & MBIM_DEVICE_OPEN_FLAGS_MS_MBIMEX_V3)) {
1703 g_task_return_new_error (task, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_ARGS,
1704 "Cannot request both MBIMEx v2.0 and v3.0 at the same time");
1705 g_object_unref (task);
1706 return;
1707 }
1708
1709 /* User requested MBIMEx 2.0 or 3.0, so we'll report it along with MBIM 1.0 */
1710 mbim_version = 0x01 << 8 | 0x00;
1711 if (ctx->flags & MBIM_DEVICE_OPEN_FLAGS_MS_MBIMEX_V2)
1712 ms_mbimex_version = 0x02 << 8 | 0x00;
1713 else if (ctx->flags & MBIM_DEVICE_OPEN_FLAGS_MS_MBIMEX_V3)
1714 ms_mbimex_version = 0x03 << 8 | 0x00;
1715 else
1716 g_assert_not_reached ();
1717
1718 request = mbim_message_ms_basic_connect_extensions_v2_version_query_new (mbim_version, ms_mbimex_version, NULL);
1719 g_assert (request);
1720
1721 mbim_device_command (self,
1722 request,
1723 ctx->timeout,
1724 g_task_get_cancellable (task),
1725 (GAsyncReadyCallback)ms_ext_version_message_ready,
1726 task);
1727 }
1728
1729 static void
1730 device_services_message_ready (MbimDevice *device,
1731 GAsyncResult *res,
1732 GTask *task)
1733 {
1734 g_autoptr(MbimMessage) response = NULL;
1735 g_autoptr(MbimDeviceServiceElementArray) device_services = NULL;
1736 GError *error = NULL;
1737 guint32 device_services_count;
1738 guint32 max_dss_sessions;
1739 DeviceOpenContext *ctx;
1740 guint i;
1741
1742 ctx = g_task_get_task_data (task);
1743
1744 response = mbim_device_command_finish (device, res, &error);
1745 if (!response ||
1746 !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error) ||
1747 !mbim_message_device_services_response_parse (
1748 response,
1749 &device_services_count,
1750 &max_dss_sessions,
1751 &device_services,
1752 &error)) {
1753 g_task_return_error (task, error);
1754 g_object_unref (task);
1755 return;
1756 }
1757
1758 if (device_services_count == 0) {
1759 g_task_return_new_error (task, MBIM_CORE_ERROR, MBIM_CORE_ERROR_FAILED,
1760 "No supported services reported by the modem");
1761 g_object_unref (task);
1762 return;
1763 }
1764
1765 for (i = 0; i < device_services_count; i++) {
1766 MbimService service;
1767 guint32 j;
1768
1769 service = mbim_uuid_to_service (&device_services[i]->device_service_id);
1770 for (j = 0; j < device_services[i]->cids_count; j++) {
1771
1772 if ((service == MBIM_SERVICE_MS_BASIC_CONNECT_EXTENSIONS) &&
1773 device_services[i]->cids[j] == MBIM_CID_MS_BASIC_CONNECT_EXTENSIONS_VERSION) {
1774 /* version command is supported, go on */
1775 ctx->step++;
1776 device_open_context_step (task);
1777 return;
1778 }
1779 }
1780 }
1781
1782 /* the version command isn't supported, so we can just jump to the end */
1783 ctx->step = DEVICE_OPEN_CONTEXT_STEP_LAST;;
1784 device_open_context_step (task);
1785 }
1786
1787 static void
1788 device_services_message (GTask *task)
1789 {
1790 MbimDevice *self;
1791 DeviceOpenContext *ctx;
1792 g_autoptr(MbimMessage) request = NULL;
1793
1794 self = g_task_get_source_object (task);
1795 ctx = g_task_get_task_data (task);
1796
1797 request = mbim_message_device_services_query_new (NULL);
1798 g_assert (request);
1799
1800 mbim_device_command (self,
1801 request,
1802 ctx->timeout,
1803 g_task_get_cancellable (task),
1804 (GAsyncReadyCallback)device_services_message_ready,
1805 task);
1806 }
1807
1808 static void
1809 open_message_ready (MbimDevice *self,
1810 GAsyncResult *res,
1811 GTask *task)
1812 {
1813 DeviceOpenContext *ctx;
1814 g_autoptr(MbimMessage) response = NULL;
1815 g_autoptr(GError) error = NULL;
1816
1817 ctx = g_task_get_task_data (task);
1818
1819 /* Cleanup, as no longer needed */
1820 self->priv->open_transaction_id = 0;
1821
1822 response = mbim_device_command_finish (self, res, &error);
1823 if (!response) {
1824 /* If we get reported that the state is unknown, try to close before open */
1825 if (g_error_matches (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_UNKNOWN_STATE)) {
1826 ctx->close_before_open = TRUE;
1827 ctx->step = DEVICE_OPEN_CONTEXT_STEP_CLOSE_MESSAGE;
1828 device_open_context_step (task);
1829 return;
1830 }
1831
1832 /* Check if we should be retrying after a timeout */
1833 if (g_error_matches (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_TIMEOUT)) {
1834 device_open_context_step (task);
1835 return;
1836 }
1837
1838 g_debug ("[%s] error reported in open operation: closed", self->priv->path_display);
1839 self->priv->open_status = OPEN_STATUS_CLOSED;
1840 g_task_return_error (task, g_steal_pointer (&error));
1841 g_object_unref (task);
1842 return;
1843 }
1844
1845 if (!mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_OPEN_DONE, &error)) {
1846 g_debug ("[%s] getting open done result failed: closed", self->priv->path_display);
1847 self->priv->open_status = OPEN_STATUS_CLOSED;
1848 g_task_return_error (task, g_steal_pointer (&error));
1849 g_object_unref (task);
1850 return;
1851 }
1852
1853 /* go on */
1854 ctx->step++;
1855 device_open_context_step (task);
1856 }
1857
1858 static void
1859 open_message (GTask *task)
1860 {
1861 MbimDevice *self;
1862 g_autoptr(MbimMessage) request = NULL;
1863
1864 self = g_task_get_source_object (task);
1865
1866 /* Launch 'Open' command */
1867 self->priv->open_transaction_id = mbim_device_get_next_transaction_id (self);
1868 request = mbim_message_open_new (self->priv->open_transaction_id,
1869 self->priv->max_control_transfer);
1870 mbim_device_command (self,
1871 request,
1872 OPEN_RETRY_TIMEOUT_SECS,
1873 g_task_get_cancellable (task),
1874 (GAsyncReadyCallback)open_message_ready,
1875 task);
1876 }
1877
1878 static void
1879 close_message_before_open_ready (MbimDevice *self,
1880 GAsyncResult *res,
1881 GTask *task)
1882 {
1883 DeviceOpenContext *ctx;
1884 g_autoptr(MbimMessage) response = NULL;
1885 g_autoptr(GError) error = NULL;
1886
1887 ctx = g_task_get_task_data (task);
1888
1889 response = mbim_device_command_finish (self, res, &error);
1890 if (!response)
1891 g_debug ("[%s] error reported in close before open: %s (ignored)", self->priv->path_display, error->message);
1892 else if (!mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_CLOSE_DONE, &error))
1893 g_debug ("[%s] getting close done result failed: %s (ignored)", self->priv->path_display, error->message);
1894
1895 /* go on */
1896 ctx->step++;
1897 device_open_context_step (task);
1898 }
1899
1900 static void
1901 close_message_before_open (GTask *task)
1902 {
1903 MbimDevice *self;
1904 g_autoptr(MbimMessage) request = NULL;
1905
1906 self = g_task_get_source_object (task);
1907
1908 /* Launch 'Close' command */
1909 request = mbim_message_close_new (mbim_device_get_next_transaction_id (self));
1910 mbim_device_command (self,
1911 request,
1912 OPEN_CLOSE_TIMEOUT_SECS,
1913 g_task_get_cancellable (task),
1914 (GAsyncReadyCallback)close_message_before_open_ready,
1915 task);
1916 }
1917
1918 static void
1919 proxy_cfg_message_ready (MbimDevice *self,
1920 GAsyncResult *res,
1921 GTask *task)
1922 {
1923 DeviceOpenContext *ctx;
1924 GError *error = NULL;
1925 g_autoptr(MbimMessage) response = NULL;
1926
1927 ctx = g_task_get_task_data (task);
1928
1929 response = mbim_device_command_finish (self, res, &error);
1930 if (!response) {
1931 /* Hard error if proxy cfg command fails */
1932 g_debug ("[%s] proxy configuration failed: closed", self->priv->path_display);
1933 self->priv->open_status = OPEN_STATUS_CLOSED;
1934 g_task_return_error (task, error);
1935 g_object_unref (task);
1936 return;
1937 }
1938
1939 ctx->step++;
1940 device_open_context_step (task);
1941 }
1942
1943 static void
1944 proxy_cfg_message (GTask *task)
1945 {
1946 MbimDevice *self;
1947 DeviceOpenContext *ctx;
1948 g_autoptr(MbimMessage) request = NULL;
1949
1950 self = g_task_get_source_object (task);
1951 ctx = g_task_get_task_data (task);
1952
1953 request = mbim_message_proxy_control_configuration_set_new (self->priv->path, ctx->timeout, NULL);
1954 g_assert (request);
1955
1956 /* This message is no longer a direct reply; as the proxy will also try to open the device
1957 * directly. If it cannot open the device, it will return an error. */
1958 mbim_device_command (self,
1959 request,
1960 ctx->timeout,
1961 g_task_get_cancellable (task),
1962 (GAsyncReadyCallback)proxy_cfg_message_ready,
1963 task);
1964 }
1965
1966 static void
1967 create_iochannel_ready (MbimDevice *self,
1968 GAsyncResult *res,
1969 GTask *task)
1970 {
1971 DeviceOpenContext *ctx;
1972 GError *error = NULL;
1973
1974 if (!create_iochannel_finish (self, res, &error)) {
1975 g_debug ("[%s] creating iochannel failed: closed", self->priv->path_display);
1976 self->priv->open_status = OPEN_STATUS_CLOSED;
1977 g_task_return_error (task, error);
1978 g_object_unref (task);
1979 return;
1980 }
1981
1982 /* Go on */
1983 ctx = g_task_get_task_data (task);
1984 ctx->step++;
1985 device_open_context_step (task);
1986 }
1987
1988 static void
1989 device_open_context_step (GTask *task)
1990 {
1991 MbimDevice *self;
1992 DeviceOpenContext *ctx;
1993
1994 self = g_task_get_source_object (task);
1995 ctx = g_task_get_task_data (task);
1996
1997 /* Timed out? */
1998 if (g_timer_elapsed (ctx->timer, NULL) > ctx->timeout) {
1999 g_debug ("[%s] open operation timed out: closed", self->priv->path_display);
2000 self->priv->open_status = OPEN_STATUS_CLOSED;
2001 g_task_return_new_error (task,
2002 MBIM_CORE_ERROR,
2003 MBIM_CORE_ERROR_TIMEOUT,
2004 "Operation timed out: device is closed");
2005 g_object_unref (task);
2006 return;
2007 }
2008
2009 switch (ctx->step) {
2010 case DEVICE_OPEN_CONTEXT_STEP_FIRST:
2011 if (self->priv->open_status == OPEN_STATUS_OPEN) {
2012 g_task_return_new_error (task,
2013 MBIM_CORE_ERROR,
2014 MBIM_CORE_ERROR_WRONG_STATE,
2015 "Already open");
2016 g_object_unref (task);
2017 return;
2018 }
2019
2020 if (self->priv->open_status == OPEN_STATUS_OPENING) {
2021 g_task_return_new_error (task,
2022 MBIM_CORE_ERROR,
2023 MBIM_CORE_ERROR_WRONG_STATE,
2024 "Already opening");
2025 g_object_unref (task);
2026 return;
2027 }
2028
2029 g_debug ("[%s] opening device...", self->priv->path_display);
2030 g_assert (self->priv->open_status == OPEN_STATUS_CLOSED);
2031 self->priv->open_status = OPEN_STATUS_OPENING;
2032
2033 ctx->step++;
2034 /* Fall through */
2035
2036 case DEVICE_OPEN_CONTEXT_STEP_CREATE_IOCHANNEL:
2037 create_iochannel (self,
2038 !!(ctx->flags & MBIM_DEVICE_OPEN_FLAGS_PROXY),
2039 (GAsyncReadyCallback)create_iochannel_ready,
2040 task);
2041 return;
2042
2043 case DEVICE_OPEN_CONTEXT_STEP_FLAGS_PROXY:
2044 if (ctx->flags & MBIM_DEVICE_OPEN_FLAGS_PROXY) {
2045 proxy_cfg_message (task);
2046 return;
2047 }
2048 ctx->step++;
2049 /* Fall through */
2050
2051 case DEVICE_OPEN_CONTEXT_STEP_CLOSE_MESSAGE:
2052 /* Only send an explicit close during open if needed */
2053 if (ctx->close_before_open) {
2054 ctx->close_before_open = FALSE;
2055 close_message_before_open (task);
2056 return;
2057 }
2058 ctx->step++;
2059 /* Fall through */
2060
2061 case DEVICE_OPEN_CONTEXT_STEP_OPEN_MESSAGE:
2062 /* If the device is already in-session, avoid the open message */
2063 if (!self->priv->in_session) {
2064 open_message (task);
2065 return;
2066 }
2067 ctx->step++;
2068 /* Fall through */
2069
2070 case DEVICE_OPEN_CONTEXT_STEP_DEVICE_SERVICES:
2071 if (ctx->flags & (MBIM_DEVICE_OPEN_FLAGS_MS_MBIMEX_V2 | MBIM_DEVICE_OPEN_FLAGS_MS_MBIMEX_V3)) {
2072 device_services_message (task);
2073 return;
2074 }
2075 ctx->step++;
2076 /* Fall through */
2077
2078 case DEVICE_OPEN_CONTEXT_STEP_MS_EXT_VERSION:
2079 if (ctx->flags & (MBIM_DEVICE_OPEN_FLAGS_MS_MBIMEX_V2 | MBIM_DEVICE_OPEN_FLAGS_MS_MBIMEX_V3)) {
2080 ms_ext_version_message (task);
2081 return;
2082 }
2083 ctx->step++;
2084 /* Fall through */
2085
2086 case DEVICE_OPEN_CONTEXT_STEP_LAST:
2087 /* Nothing else to process, complete without error */
2088 self->priv->open_status = OPEN_STATUS_OPEN;
2089 g_task_return_boolean (task, TRUE);
2090 g_object_unref (task);
2091 return;
2092
2093 default:
2094 break;
2095 }
2096
2097 g_assert_not_reached ();
2098 }
2099
2100 void
2101 mbim_device_open_full (MbimDevice *self,
2102 MbimDeviceOpenFlags flags,
2103 guint timeout,
2104 GCancellable *cancellable,
2105 GAsyncReadyCallback callback,
2106 gpointer user_data)
2107 {
2108 DeviceOpenContext *ctx;
2109 GTask *task;
2110
2111 g_return_if_fail (MBIM_IS_DEVICE (self));
2112 g_return_if_fail (timeout > 0);
2113
2114 ctx = g_slice_new0 (DeviceOpenContext);
2115 ctx->step = DEVICE_OPEN_CONTEXT_STEP_FIRST;
2116 ctx->flags = flags;
2117 ctx->timeout = timeout;
2118 ctx->timer = g_timer_new ();
2119 ctx->close_before_open = FALSE;
2120
2121 task = g_task_new (self, cancellable, callback, user_data);
2122 g_task_set_task_data (task, ctx, (GDestroyNotify)device_open_context_free);
2123
2124 /* Start processing */
2125 device_open_context_step (task);
2126 }
2127
2128 void
2129 mbim_device_open (MbimDevice *self,
2130 guint timeout,
2131 GCancellable *cancellable,
2132 GAsyncReadyCallback callback,
2133 gpointer user_data)
2134 {
2135 mbim_device_open_full (self,
2136 MBIM_DEVICE_OPEN_FLAGS_NONE,
2137 timeout,
2138 cancellable,
2139 callback,
2140 user_data);
2141 }
2142
2143 /*****************************************************************************/
2144 /* Close channel */
2145
2146 static gboolean
2147 destroy_iochannel (MbimDevice *self,
2148 GError **error)
2149 {
2150 GError *inner_error = NULL;
2151
2152 /* Already closed? */
2153 if (!self->priv->iochannel && !self->priv->socket_connection && !self->priv->socket_client)
2154 return TRUE;
2155
2156 g_debug ("[%s] channel destroyed", self->priv->path_display);
2157
2158 if (self->priv->iochannel) {
2159 g_io_channel_shutdown (self->priv->iochannel, TRUE, &inner_error);
2160 g_io_channel_unref (self->priv->iochannel);
2161 self->priv->iochannel = NULL;
2162 }
2163
2164 /* Failures when closing still make the device to get closed */
2165 g_clear_object (&self->priv->socket_connection);
2166 g_clear_object (&self->priv->socket_client);
2167
2168 /* Pending error indications are always cleared if the channel is
2169 * closed */
2170 g_clear_error (&self->priv->pending_error_indication);
2171
2172 if (self->priv->iochannel_source) {
2173 g_source_destroy (self->priv->iochannel_source);
2174 g_source_unref (self->priv->iochannel_source);
2175 self->priv->iochannel_source = NULL;
2176 }
2177
2178 if (self->priv->response) {
2179 g_byte_array_unref (self->priv->response);
2180 self->priv->response = NULL;
2181 }
2182
2183 if (inner_error) {
2184 g_propagate_error (error, inner_error);
2185 return FALSE;
2186 }
2187
2188 return TRUE;
2189 }
2190
2191 gboolean
2192 mbim_device_close_force (MbimDevice *self,
2193 GError **error)
2194 {
2195 g_return_val_if_fail (MBIM_IS_DEVICE (self), FALSE);
2196
2197 self->priv->open_status = OPEN_STATUS_CLOSED;
2198
2199 return destroy_iochannel (self, error);
2200 }
2201
2202 typedef struct {
2203 guint timeout;
2204 } DeviceCloseContext;
2205
2206 static void
2207 device_close_context_free (DeviceCloseContext *ctx)
2208 {
2209 g_slice_free (DeviceCloseContext, ctx);
2210 }
2211
2212 gboolean
2213 mbim_device_close_finish (MbimDevice *self,
2214 GAsyncResult *res,
2215 GError **error)
2216 {
2217 return g_task_propagate_boolean (G_TASK (res), error);
2218 }
2219
2220 static void
2221 close_message_ready (MbimDevice *self,
2222 GAsyncResult *res,
2223 GTask *task)
2224 {
2225 g_autoptr(MbimMessage) response = NULL;
2226 g_autoptr(GError) error = NULL;
2227 g_autoptr(GError) iochannel_error = NULL;
2228
2229 response = mbim_device_command_finish (self, res, &error);
2230 if (response)
2231 mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_CLOSE_DONE, &error);
2232
2233 self->priv->open_status = OPEN_STATUS_CLOSED;
2234 destroy_iochannel (self, &iochannel_error);
2235
2236 if (error)
2237 g_task_return_error (task, g_steal_pointer (&error));
2238 else if (iochannel_error)
2239 g_task_return_error (task, g_steal_pointer (&iochannel_error));
2240 else
2241 g_task_return_boolean (task, TRUE);
2242 g_object_unref (task);
2243 }
2244
2245 void
2246 mbim_device_close (MbimDevice *self,
2247 guint timeout,
2248 GCancellable *cancellable,
2249 GAsyncReadyCallback callback,
2250 gpointer user_data)
2251 {
2252 g_autoptr(MbimMessage) request = NULL;
2253 DeviceCloseContext *ctx;
2254 GTask *task;
2255
2256 g_return_if_fail (MBIM_IS_DEVICE (self));
2257
2258 ctx = g_slice_new (DeviceCloseContext);
2259 ctx->timeout = timeout;
2260
2261 task = g_task_new (self, cancellable, callback, user_data);
2262 g_task_set_task_data (task, ctx, (GDestroyNotify)device_close_context_free);
2263
2264 /* If already closed, we're done */
2265 if (self->priv->open_status == OPEN_STATUS_CLOSED) {
2266 g_task_return_boolean (task, TRUE);
2267 g_object_unref (task);
2268 return;
2269 }
2270
2271 /* If we're opening, fail with error. We could say we would abort the
2272 * ongoing open attempt, but the way to abort that attempt is with the
2273 * cancellable given in the open operation, not with an additional close.
2274 */
2275 if (self->priv->open_status == OPEN_STATUS_OPENING) {
2276 g_task_return_new_error (
2277 task, MBIM_CORE_ERROR, MBIM_CORE_ERROR_WRONG_STATE,
2278 "Cannot close device: not yet fully open");
2279 g_object_unref (task);
2280 return;
2281 }
2282
2283 g_debug ("[%s] closing device...", self->priv->path_display);
2284 g_assert (self->priv->open_status == OPEN_STATUS_OPEN);
2285
2286 /* If the device is in-session, avoid the close message */
2287 if (self->priv->in_session) {
2288 GError *error = NULL;
2289
2290 self->priv->open_status = OPEN_STATUS_CLOSED;
2291 if (!destroy_iochannel (self, &error))
2292 g_task_return_error (task, error);
2293 else
2294 g_task_return_boolean (task, TRUE);
2295 g_object_unref (task);
2296 return;
2297 }
2298
2299 /* Launch 'Close' command */
2300 request = mbim_message_close_new (mbim_device_get_next_transaction_id (self));
2301 mbim_device_command (self,
2302 request,
2303 10,
2304 cancellable,
2305 (GAsyncReadyCallback) close_message_ready,
2306 task);
2307 }
2308
2309 /*****************************************************************************/
2310
2311 guint32
2312 mbim_device_get_next_transaction_id (MbimDevice *self)
2313 {
2314 guint32 next;
2315
2316 g_return_val_if_fail (MBIM_IS_DEVICE (self), 0);
2317
2318 next = self->priv->transaction_id;
2319
2320 /* Don't go further than 8bits in the CTL service */
2321 if (self->priv->transaction_id == G_MAXUINT32)
2322 /* Reset! */
2323 self->priv->transaction_id = 0x01;
2324 else
2325 self->priv->transaction_id++;
2326
2327 return next;
2328 }
2329
2330 /*****************************************************************************/
2331
2332 guint32
2333 mbim_device_get_transaction_id (MbimDevice *self)
2334 {
2335 g_return_val_if_fail (MBIM_IS_DEVICE (self), 0);
2336
2337 return self->priv->transaction_id;
2338 }
2339
2340 /*****************************************************************************/
2341
2342 static gboolean
2343 device_write (MbimDevice *self,
2344 const guint8 *data,
2345 guint32 data_length,
2346 GError **error)
2347 {
2348 gsize written;
2349 GIOStatus write_status;
2350
2351 written = 0;
2352 write_status = G_IO_STATUS_AGAIN;
2353 while (write_status == G_IO_STATUS_AGAIN) {
2354 write_status = g_io_channel_write_chars (self->priv->iochannel,
2355 (gconstpointer)data,
2356 (gssize)data_length,
2357 &written,
2358 error);
2359 switch (write_status) {
2360 case G_IO_STATUS_ERROR:
2361 g_prefix_error (error, "Cannot write message: ");
2362 return FALSE;
2363
2364 case G_IO_STATUS_EOF:
2365 /* We shouldn't get EOF when writing */
2366 g_assert_not_reached ();
2367 break;
2368
2369 case G_IO_STATUS_NORMAL:
2370 /* All good, we'll exit the loop now */
2371 break;
2372
2373 case G_IO_STATUS_AGAIN:
2374 /* We're in a non-blocking channel and therefore we're up to receive
2375 * EAGAIN; just retry in this case. TODO: in an idle? */
2376 break;
2377
2378 default:
2379 g_assert_not_reached ();
2380 break;
2381 }
2382 }
2383
2384 return TRUE;
2385 }
2386
2387 static gboolean
2388 device_send (MbimDevice *self,
2389 MbimMessage *message,
2390 GError **error)
2391 {
2392 const guint8 *raw_message;
2393 guint32 raw_message_len;
2394 g_autofree struct fragment_info *fragments = NULL;
2395 guint n_fragments;
2396 guint i;
2397
2398 raw_message = mbim_message_get_raw (message, &raw_message_len, NULL);
2399 g_assert (raw_message);
2400
2401 if (mbim_utils_get_traces_enabled ()) {
2402 g_autofree gchar *hex = NULL;
2403 g_autofree gchar *printable = NULL;
2404
2405 if (mbim_utils_get_show_personal_info () || (raw_message_len < MAX_PRINTED_BYTES)) {
2406 hex = mbim_common_str_hex (raw_message, raw_message_len, ':');
2407 } else {
2408 g_autofree gchar *tmp = NULL;
2409
2410 tmp = mbim_common_str_hex (raw_message, MAX_PRINTED_BYTES, ':');
2411 hex = g_strdup_printf ("%s...", tmp);
2412 }
2413
2414 g_debug ("[%s] sent message...\n"
2415 "<<<<<< RAW:\n"
2416 "<<<<<< length = %u\n"
2417 "<<<<<< data = %s\n",
2418 self->priv->path_display,
2419 ((GByteArray *)message)->len,
2420 hex);
2421
2422 printable = mbim_message_get_printable_full (message,
2423 self->priv->ms_mbimex_version_major,
2424 self->priv->ms_mbimex_version_minor,
2425 "<<<<<< ",
2426 FALSE,
2427 NULL);
2428 g_debug ("[%s] sent message (translated)...\n%s",
2429 self->priv->path_display,
2430 printable);
2431 }
2432
2433 /* Single fragment? Send it! */
2434 if (raw_message_len <= MAX_CONTROL_TRANSFER)
2435 return device_write (self, raw_message, raw_message_len, error);
2436
2437 /* The message to send must be able to handle fragments */
2438 g_assert (_mbim_message_is_fragment (message));
2439
2440 fragments = _mbim_message_split_fragments (message, MAX_CONTROL_TRANSFER, &n_fragments);
2441 for (i = 0; i < n_fragments; i++) {
2442 g_autoptr(GByteArray) full_fragment = NULL;
2443 g_autofree gchar *printable_headers = NULL;
2444
2445 /* Build compiled fragment headers */
2446 full_fragment = g_byte_array_new ();
2447 g_byte_array_append (full_fragment, (guint8 *)&fragments[i].header, sizeof (fragments[i].header));
2448 g_byte_array_append (full_fragment, (guint8 *)&fragments[i].fragment_header, sizeof (fragments[i].fragment_header));
2449
2450 /* Build placeholder message with only headers for printable purposes only */
2451 if (mbim_utils_get_traces_enabled ())
2452 printable_headers = mbim_message_get_printable_full ((MbimMessage *)full_fragment,
2453 self->priv->ms_mbimex_version_major,
2454 self->priv->ms_mbimex_version_minor,
2455 "<<<<<< ",
2456 TRUE,
2457 NULL);
2458
2459 /* Append the actual fragment data */
2460 g_byte_array_append (full_fragment, (guint8 *)fragments[i].data, fragments[i].data_length);
2461
2462 if (mbim_utils_get_traces_enabled ()) {
2463 g_autofree gchar *printable_full = NULL;
2464
2465 printable_full = mbim_common_str_hex ((const guint8 *)full_fragment->data, full_fragment->len, ':');
2466 g_debug ("[%s] sent fragment (%u)...\n"
2467 "<<<<<< RAW:\n"
2468 "<<<<<< length = %u\n"
2469 "<<<<<< data = %s\n",
2470 self->priv->path_display, i,
2471 full_fragment->len,
2472 printable_full);
2473
2474 g_debug ("[%s] sent fragment (translated)...\n%s",
2475 self->priv->path_display,
2476 printable_headers);
2477 }
2478
2479 /* Write whole packet to MBIM device.
2480 * Here send whole packet rather than seperated elements, such as header,
2481 * fragment_header, data, because some MBIM devices may have errors on
2482 * seperated fragment case, such as "MBIM protocol error: LengthMismatch"
2483 */
2484 if (!device_write (self,
2485 (guint8 *)full_fragment->data,
2486 full_fragment->len,
2487 error))
2488 return FALSE;
2489 }
2490
2491 return TRUE;
2492 }
2493
2494 /*****************************************************************************/
2495 /* Report error */
2496
2497 typedef struct {
2498 MbimDevice *self;
2499 MbimMessage *message;
2500 } ReportErrorContext;
2501
2502 static void
2503 device_report_error_context_free (ReportErrorContext *ctx)
2504 {
2505 mbim_message_unref (ctx->message);
2506 g_object_unref (ctx->self);
2507 g_slice_free (ReportErrorContext, ctx);
2508 }
2509
2510 static gboolean
2511 device_report_error_in_idle (ReportErrorContext *ctx)
2512 {
2513 /* Device must be open */
2514 if (ctx->self->priv->iochannel) {
2515 g_autoptr(GError) error = NULL;
2516
2517 if (!device_send (ctx->self, ctx->message, &error))
2518 g_warning ("[%s] couldn't send host error message: %s",
2519 ctx->self->priv->path_display,
2520 error->message);
2521 }
2522
2523 device_report_error_context_free (ctx);
2524 return FALSE;
2525 }
2526
2527 static void
2528 device_report_error (MbimDevice *self,
2529 guint32 transaction_id,
2530 const GError *error)
2531 {
2532 ReportErrorContext *ctx;
2533 g_autoptr(GSource) source = NULL;
2534
2535 /* Only protocol errors to be reported to the modem */
2536 if (error->domain != MBIM_PROTOCOL_ERROR)
2537 return;
2538
2539 ctx = g_slice_new (ReportErrorContext);
2540 ctx->self = g_object_ref (self);
2541 ctx->message = mbim_message_error_new (transaction_id, error->code);
2542
2543 source = g_idle_source_new ();
2544 g_source_set_callback (source, (GSourceFunc)device_report_error_in_idle, ctx, NULL);
2545 g_source_attach (source, g_main_context_get_thread_default ());
2546 }
2547
2548 /*****************************************************************************/
2549 /* Command */
2550
2551 MbimMessage *
2552 mbim_device_command_finish (MbimDevice *self,
2553 GAsyncResult *res,
2554 GError **error)
2555 {
2556 return g_task_propagate_pointer (G_TASK (res), error);
2557 }
2558
2559 void
2560 mbim_device_command (MbimDevice *self,
2561 MbimMessage *message,
2562 guint timeout,
2563 GCancellable *cancellable,
2564 GAsyncReadyCallback callback,
2565 gpointer user_data)
2566 {
2567 g_autoptr(GError) error = NULL;
2568 GTask *task;
2569 guint32 transaction_id;
2570
2571 g_return_if_fail (MBIM_IS_DEVICE (self));
2572 g_return_if_fail (message != NULL);
2573
2574 /* If the message comes without a explicit transaction ID, add one
2575 * ourselves */
2576 transaction_id = mbim_message_get_transaction_id (message);
2577 if (!transaction_id) {
2578 transaction_id = mbim_device_get_next_transaction_id (self);
2579 mbim_message_set_transaction_id (message, transaction_id);
2580 }
2581
2582 task = transaction_task_new (self,
2583 MBIM_MESSAGE_GET_MESSAGE_TYPE (message),
2584 transaction_id,
2585 cancellable,
2586 callback,
2587 user_data);
2588
2589 /* Device must be open */
2590 if (!self->priv->iochannel) {
2591 error = g_error_new (MBIM_CORE_ERROR,
2592 MBIM_CORE_ERROR_WRONG_STATE,
2593 "Device must be open to send commands");
2594 transaction_task_complete_and_free (task, error);
2595 return;
2596 }
2597
2598 /* Setup context to match response */
2599 if (!device_store_transaction (self, TRANSACTION_TYPE_HOST, task, timeout * 1000, &error)) {
2600 g_prefix_error (&error, "Cannot store transaction: ");
2601 transaction_task_complete_and_free (task, error);
2602 return;
2603 }
2604
2605 if (!device_send (self, message, &error)) {
2606 /* Match transaction so that we remove it from our tracking table */
2607 task = device_release_transaction (self,
2608 TRANSACTION_TYPE_HOST,
2609 MBIM_MESSAGE_GET_MESSAGE_TYPE (message),
2610 mbim_message_get_transaction_id (message));
2611 transaction_task_complete_and_free (task, error);
2612 return;
2613 }
2614
2615 /* Just return, we'll get response asynchronously */
2616 }
2617
2618 /*****************************************************************************/
2619 /* New MBIM device */
2620
2621 MbimDevice *
2622 mbim_device_new_finish (GAsyncResult *res,
2623 GError **error)
2624 {
2625 GObject *ret;
2626 GObject *source_object;
2627
2628 source_object = g_async_result_get_source_object (res);
2629 ret = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object), res, error);
2630 g_object_unref (source_object);
2631
2632 return (ret ? MBIM_DEVICE (ret) : NULL);
2633 }
2634
2635 void
2636 mbim_device_new (GFile *file,
2637 GCancellable *cancellable,
2638 GAsyncReadyCallback callback,
2639 gpointer user_data)
2640 {
2641 g_async_initable_new_async (MBIM_TYPE_DEVICE,
2642 G_PRIORITY_DEFAULT,
2643 cancellable,
2644 callback,
2645 user_data,
2646 MBIM_DEVICE_FILE, file,
2647 NULL);
2648 }
2649
2650 /*****************************************************************************/
2651 /* Async init */
2652
2653 static gboolean
2654 initable_init_finish (GAsyncInitable *initable,
2655 GAsyncResult *result,
2656 GError **error)
2657 {
2658 return g_task_propagate_boolean (G_TASK (result), error);
2659 }
2660
2661 static void
2662 query_info_async_ready (GFile *file,
2663 GAsyncResult *res,
2664 GTask *task)
2665 {
2666 GError *error = NULL;
2667 GFileInfo *info;
2668
2669 info = g_file_query_info_finish (file, res, &error);
2670 if (!info) {
2671 g_prefix_error (&error,
2672 "Couldn't query file info: ");
2673 g_task_return_error (task, error);
2674 g_object_unref (task);
2675 return;
2676 }
2677
2678 /* Our MBIM device must be of SPECIAL type */
2679 if (g_file_info_get_file_type (info) != G_FILE_TYPE_SPECIAL) {
2680 g_task_return_new_error (task,
2681 MBIM_CORE_ERROR,
2682 MBIM_CORE_ERROR_FAILED,
2683 "Wrong file type");
2684 g_object_unref (task);
2685 return;
2686 }
2687 g_object_unref (info);
2688
2689 /* Done we are */
2690 g_task_return_boolean (task, TRUE);
2691 g_object_unref (task);
2692 }
2693
2694 static void
2695 initable_init_async (GAsyncInitable *initable,
2696 int io_priority,
2697 GCancellable *cancellable,
2698 GAsyncReadyCallback callback,
2699 gpointer user_data)
2700 {
2701 MbimDevice *self;
2702 GTask *task;
2703
2704 self = MBIM_DEVICE (initable);
2705 task = g_task_new (self, cancellable, callback, user_data);
2706
2707 /* We need a proper file to initialize */
2708 if (!self->priv->file) {
2709 g_task_return_new_error (task,
2710 MBIM_CORE_ERROR,
2711 MBIM_CORE_ERROR_INVALID_ARGS,
2712 "Cannot initialize MBIM device: No file given");
2713 g_object_unref (task);
2714 return;
2715 }
2716
2717 /* Check the file type. Note that this is just a quick check to avoid
2718 * creating MbimDevices pointing to a location already known not to be a MBIM
2719 * device. */
2720 g_file_query_info_async (self->priv->file,
2721 G_FILE_ATTRIBUTE_STANDARD_TYPE,
2722 G_FILE_QUERY_INFO_NONE,
2723 G_PRIORITY_DEFAULT,
2724 cancellable,
2725 (GAsyncReadyCallback)query_info_async_ready,
2726 task);
2727 }
2728
2729 /*****************************************************************************/
2730
2731 static void
2732 set_property (GObject *object,
2733 guint prop_id,
2734 const GValue *value,
2735 GParamSpec *pspec)
2736 {
2737 MbimDevice *self = MBIM_DEVICE (object);
2738
2739 switch (prop_id) {
2740 case PROP_FILE:
2741 g_assert (self->priv->file == NULL);
2742 self->priv->file = g_value_dup_object (value);
2743 self->priv->path = g_file_get_path (self->priv->file);
2744 self->priv->path_display = g_filename_display_name (self->priv->path);
2745 break;
2746 case PROP_TRANSACTION_ID:
2747 self->priv->transaction_id = g_value_get_uint (value);
2748 break;
2749 case PROP_IN_SESSION:
2750 self->priv->in_session = g_value_get_boolean (value);
2751 break;
2752 case PROP_CONSECUTIVE_TIMEOUTS:
2753 g_assert_not_reached ();
2754 break;
2755 default:
2756 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2757 break;
2758 }
2759 }
2760
2761 static void
2762 get_property (GObject *object,
2763 guint prop_id,
2764 GValue *value,
2765 GParamSpec *pspec)
2766 {
2767 MbimDevice *self = MBIM_DEVICE (object);
2768
2769 switch (prop_id) {
2770 case PROP_FILE:
2771 g_value_set_object (value, self->priv->file);
2772 break;
2773 case PROP_TRANSACTION_ID:
2774 g_value_set_uint (value, self->priv->transaction_id);
2775 break;
2776 case PROP_IN_SESSION:
2777 g_value_set_boolean (value, self->priv->in_session);
2778 break;
2779 case PROP_CONSECUTIVE_TIMEOUTS:
2780 g_value_set_uint (value, self->priv->consecutive_timeouts);
2781 break;
2782 default:
2783 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2784 break;
2785 }
2786 }
2787
2788 static void
2789 mbim_device_init (MbimDevice *self)
2790 {
2791 self->priv = G_TYPE_INSTANCE_GET_PRIVATE ((self),
2792 MBIM_TYPE_DEVICE,
2793 MbimDevicePrivate);
2794
2795 /* Initialize transaction ID */
2796 self->priv->transaction_id = 0x01;
2797 self->priv->open_status = OPEN_STATUS_CLOSED;
2798
2799 /* By default, assume v1.0 supported */
2800 self->priv->ms_mbimex_version_major = 0x01;
2801 }
2802
2803 static void
2804 dispose (GObject *object)
2805 {
2806 MbimDevice *self = MBIM_DEVICE (object);
2807
2808 g_clear_object (&self->priv->file);
2809
2810 self->priv->open_status = OPEN_STATUS_CLOSED;
2811 destroy_iochannel (self, NULL);
2812 g_clear_object (&self->priv->net_port_manager);
2813
2814 G_OBJECT_CLASS (mbim_device_parent_class)->dispose (object);
2815 }
2816
2817 static void
2818 finalize (GObject *object)
2819 {
2820 MbimDevice *self = MBIM_DEVICE (object);
2821 guint i;
2822
2823 /* Transactions keep refs to the device, so it's actually
2824 * impossible to have any content in the HT */
2825 for (i = 0; i < TRANSACTION_TYPE_LAST; i++) {
2826 if (self->priv->transactions[i]) {
2827 g_assert (g_hash_table_size (self->priv->transactions[i]) == 0);
2828 g_hash_table_unref (self->priv->transactions[i]);
2829 self->priv->transactions[i] = NULL;
2830 }
2831 }
2832
2833 g_free (self->priv->path);
2834 g_free (self->priv->path_display);
2835 g_free (self->priv->wwan_iface);
2836
2837 G_OBJECT_CLASS (mbim_device_parent_class)->finalize (object);
2838 }
2839
2840 static void
2841 async_initable_iface_init (GAsyncInitableIface *iface)
2842 {
2843 iface->init_async = initable_init_async;
2844 iface->init_finish = initable_init_finish;
2845 }
2846
2847 static void
2848 mbim_device_class_init (MbimDeviceClass *klass)
2849 {
2850 GObjectClass *object_class = G_OBJECT_CLASS (klass);
2851
2852 g_type_class_add_private (object_class, sizeof (MbimDevicePrivate));
2853
2854 object_class->get_property = get_property;
2855 object_class->set_property = set_property;
2856 object_class->finalize = finalize;
2857 object_class->dispose = dispose;
2858
2859 /**
2860 * MbimDevice:device-file
2861 *
2862 * Since: 1.0
2863 */
2864 properties[PROP_FILE] =
2865 g_param_spec_object (MBIM_DEVICE_FILE,
2866 "Device file",
2867 "File to the underlying MBIM device",
2868 G_TYPE_FILE,
2869 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
2870 g_object_class_install_property (object_class, PROP_FILE, properties[PROP_FILE]);
2871
2872 /**
2873 * MbimDevice:device-transaction-id
2874 *
2875 * Since: 1.2
2876 */
2877 properties[PROP_TRANSACTION_ID] =
2878 g_param_spec_uint (MBIM_DEVICE_TRANSACTION_ID,
2879 "Transaction ID",
2880 "Current transaction ID",
2881 0x01,
2882 G_MAXUINT32,
2883 0x01,
2884 G_PARAM_READWRITE);
2885 g_object_class_install_property (object_class, PROP_TRANSACTION_ID, properties[PROP_TRANSACTION_ID]);
2886
2887 /**
2888 * MbimDevice:device-in-session
2889 *
2890 * Since: 1.4
2891 */
2892 properties[PROP_IN_SESSION] =
2893 g_param_spec_boolean (MBIM_DEVICE_IN_SESSION,
2894 "In session",
2895 "Flag to specify if the device is within a session",
2896 FALSE,
2897 G_PARAM_READWRITE);
2898 g_object_class_install_property (object_class, PROP_IN_SESSION, properties[PROP_IN_SESSION]);
2899
2900 /**
2901 * MbimDevice:device-consecutive-timeouts:
2902 *
2903 * Since: 1.28
2904 */
2905 properties[PROP_CONSECUTIVE_TIMEOUTS] =
2906 g_param_spec_uint (MBIM_DEVICE_CONSECUTIVE_TIMEOUTS,
2907 "Consecutive timeouts",
2908 "Number of consecutive timeouts detected in requests sent to the device",
2909 0, G_MAXUINT, 0,
2910 G_PARAM_READABLE);
2911 g_object_class_install_property (object_class, PROP_CONSECUTIVE_TIMEOUTS, properties[PROP_CONSECUTIVE_TIMEOUTS]);
2912
2913 /**
2914 * MbimDevice::device-indicate-status:
2915 * @self: the #MbimDevice
2916 * @message: the #MbimMessage indication
2917 *
2918 * The ::device-indication-status signal is emitted when a MBIM indication is received.
2919 *
2920 * Since: 1.0
2921 */
2922 signals[SIGNAL_INDICATE_STATUS] =
2923 g_signal_new (MBIM_DEVICE_SIGNAL_INDICATE_STATUS,
2924 G_OBJECT_CLASS_TYPE (G_OBJECT_CLASS (klass)),
2925 G_SIGNAL_RUN_LAST,
2926 0,
2927 NULL,
2928 NULL,
2929 NULL,
2930 G_TYPE_NONE,
2931 1,
2932 MBIM_TYPE_MESSAGE);
2933
2934 /**
2935 * MbimDevice::device-error:
2936 * @self: the #MbimDevice
2937 * @message: the #MbimMessage error
2938 *
2939 * The ::device-error signal is emitted when a MBIM error is received.
2940 *
2941 * Since: 1.0
2942 */
2943 signals[SIGNAL_ERROR] =
2944 g_signal_new (MBIM_DEVICE_SIGNAL_ERROR,
2945 G_OBJECT_CLASS_TYPE (G_OBJECT_CLASS (klass)),
2946 G_SIGNAL_RUN_LAST,
2947 0,
2948 NULL,
2949 NULL,
2950 NULL,
2951 G_TYPE_NONE,
2952 1,
2953 G_TYPE_ERROR);
2954
2955 /**
2956 * MbimDevice::device-removed:
2957 * @self: the #MbimDevice
2958 * @message: None
2959 *
2960 * The ::device-removed signal is emitted when an unexpected port hang-up is received.
2961 *
2962 * Since: 1.10
2963 */
2964 signals[SIGNAL_REMOVED] =
2965 g_signal_new (MBIM_DEVICE_SIGNAL_REMOVED,
2966 G_OBJECT_CLASS_TYPE (G_OBJECT_CLASS (klass)),
2967 G_SIGNAL_RUN_LAST,
2968 0,
2969 NULL,
2970 NULL,
2971 NULL,
2972 G_TYPE_NONE,
2973 0);
2974 }