Debian Codegraph @

src/libqmi-glib/qmi-device.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-2021 Aleksander Morgado <[email protected]>
22 * Copyright (c) 2022 Qualcomm Innovation Center, Inc.
23 */
24
25 #include <config.h>
26
27 #include <stdio.h>
28 #include <errno.h>
29 #include <string.h>
30 #include <fcntl.h>
31 #include <termios.h>
32 #include <unistd.h>
33
34 #include "qmi-common.h"
35 #include "qmi-device.h"
36 #include "qmi-message.h"
37 #include "qmi-file.h"
38 #include "qmi-endpoint.h"
39 #include "qmi-endpoint-mbim.h"
40 #include "qmi-endpoint-qmux.h"
41 #include "qmi-ctl.h"
42 #include "qmi-dms.h"
43 #include "qmi-wds.h"
44 #include "qmi-nas.h"
45 #include "qmi-wms.h"
46 #include "qmi-pdc.h"
47 #include "qmi-pds.h"
48 #include "qmi-pbm.h"
49 #include "qmi-uim.h"
50 #include "qmi-sar.h"
51 #include "qmi-oma.h"
52 #include "qmi-wda.h"
53 #include "qmi-voice.h"
54 #include "qmi-loc.h"
55 #include "qmi-qos.h"
56 #include "qmi-gas.h"
57 #include "qmi-gms.h"
58 #include "qmi-dsd.h"
59 #include "qmi-dpm.h"
60 #include "qmi-fox.h"
61 #include "qmi-atr.h"
62 #include "qmi-ims.h"
63 #include "qmi-imsp.h"
64 #include "qmi-imsa.h"
65 #include "qmi-ssc.h"
66 #include "qmi-utils.h"
67 #include "qmi-helpers.h"
68 #include "qmi-error-types.h"
69 #include "qmi-enum-types.h"
70 #include "qmi-flag-types.h"
71 #include "qmi-proxy.h"
72 #include "qmi-net-port-manager-qmiwwan.h"
73 #include "qmi-version.h"
74
75 #if QMI_QRTR_SUPPORTED
76 # include "qmi-endpoint-qrtr.h"
77 # include <libqrtr-glib.h>
78 #endif
79
80 #if defined RMNET_SUPPORT_ENABLED
81 # include "qmi-net-port-manager-rmnet.h"
82 #endif
83
84 /* maximum number of printed data bytes when personal info
85 * should be hidden */
86 #define MAX_PRINTED_BYTES 12
87
88 static void async_initable_iface_init (GAsyncInitableIface *iface);
89
90 G_DEFINE_TYPE_EXTENDED (QmiDevice, qmi_device, G_TYPE_OBJECT, 0,
91 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init))
92
93 enum {
94 PROP_0,
95 PROP_FILE,
96 PROP_NO_FILE_CHECK,
97 PROP_PROXY_PATH,
98 PROP_WWAN_IFACE,
99 PROP_CONSECUTIVE_TIMEOUTS,
100 #if QMI_QRTR_SUPPORTED
101 PROP_NODE,
102 #endif
103 PROP_LAST
104 };
105
106 enum {
107 SIGNAL_INDICATION,
108 SIGNAL_REMOVED,
109 SIGNAL_LAST
110 };
111
112 static GParamSpec *properties[PROP_LAST];
113 static guint signals [SIGNAL_LAST] = { 0 };
114
115 struct _QmiDevicePrivate {
116 /* File or node */
117 QmiFile *file;
118 #if QMI_QRTR_SUPPORTED
119 QrtrNode *node;
120 #endif
121 gboolean no_file_check;
122
123 /* WWAN interface */
124 gboolean no_wwan_check;
125 gchar *wwan_iface;
126
127 /* Information necessary for data port */
128 QmiNetPortManager *net_port_manager;
129
130 /* Implicit CTL client */
131 QmiClientCtl *client_ctl;
132 guint sync_indication_id;
133
134 /* Supported services */
135 GArray *supported_services;
136
137 /* Lower-level transport */
138 QmiEndpoint *endpoint;
139 guint endpoint_new_data_id;
140 guint endpoint_hangup_id;
141
142 /* Support for qmi-proxy */
143 gchar *proxy_path;
144
145 /* HT to keep track of ongoing transactions */
146 GHashTable *transactions;
147
148 /* HT of clients that want to get indications */
149 GHashTable *registered_clients;
150
151 /* Number of consecutive timeouts detected */
152 guint consecutive_timeouts;
153 };
154
155 #if QMI_QRTR_SUPPORTED
156 # define QMI_CLIENT_VERSION_UNKNOWN 99
157 #endif
158
159 /*****************************************************************************/
160 /* Message transactions (private) */
161
162 typedef struct {
163 QmiDevice *self;
164 gpointer key;
165 } TransactionWaitContext;
166
167 typedef struct {
168 QmiMessage *message;
169 QmiMessageContext *message_context;
170 GSimpleAsyncResult *result;
171 GSource *timeout_source;
172 GCancellable *cancellable;
173 gulong cancellable_id;
174 TransactionWaitContext *wait_ctx;
175
176 /* abortable support */
177 GError *abort_error;
178 GCancellable *abort_cancellable;
179 QmiDeviceCommandAbortableBuildRequestFn abort_build_request_fn;
180 QmiDeviceCommandAbortableParseResponseFn abort_parse_response_fn;
181 gpointer abort_user_data;
182 GDestroyNotify abort_user_data_free;
183 } Transaction;
184
185 static Transaction *
186 transaction_new (QmiDevice *self,
187 QmiMessage *message,
188 QmiMessageContext *message_context,
189 GCancellable *cancellable,
190 GAsyncReadyCallback callback,
191 gpointer user_data)
192 {
193 Transaction *tr;
194
195 tr = g_slice_new0 (Transaction);
196 tr->message = qmi_message_ref (message);
197 tr->message_context = (message_context ? qmi_message_context_ref (message_context) : NULL);
198 tr->result = g_simple_async_result_new (G_OBJECT (self),
199 callback,
200 user_data,
201 transaction_new);
202 if (cancellable)
203 tr->cancellable = g_object_ref (cancellable);
204
205 return tr;
206 }
207
208 static void
209 transaction_complete_and_free (Transaction *tr,
210 QmiMessage *reply,
211 const GError *error)
212 {
213 g_assert (reply != NULL || error != NULL);
214
215 /* always set result first, as we may be using one of the GErrors
216 * stored in the Transaction as result itself */
217 if (reply) {
218 /* if we got a valid response, we can cancel any ongoing abort
219 * operation for this request */
220 if (tr->abort_cancellable)
221 g_cancellable_cancel (tr->abort_cancellable);
222 g_simple_async_result_set_op_res_gpointer (tr->result,
223 qmi_message_ref (reply),
224 (GDestroyNotify)qmi_message_unref);
225 } else if (error)
226 g_simple_async_result_set_from_error (tr->result, error);
227 else
228 g_assert_not_reached ();
229
230 if (tr->timeout_source)
231 g_source_destroy (tr->timeout_source);
232
233 if (tr->cancellable) {
234 if (tr->cancellable_id)
235 g_cancellable_disconnect (tr->cancellable, tr->cancellable_id);
236 g_object_unref (tr->cancellable);
237 }
238
239 if (tr->wait_ctx)
240 g_slice_free (TransactionWaitContext, tr->wait_ctx);
241
242 if (tr->abort_error)
243 g_error_free (tr->abort_error);
244
245 if (tr->abort_cancellable)
246 g_object_unref (tr->abort_cancellable);
247
248 if (tr->abort_user_data && tr->abort_user_data_free)
249 tr->abort_user_data_free (tr->abort_user_data);
250
251 g_simple_async_result_complete_in_idle (tr->result);
252 g_object_unref (tr->result);
253 if (tr->message_context)
254 qmi_message_context_unref (tr->message_context);
255 qmi_message_unref (tr->message);
256 g_slice_free (Transaction, tr);
257 }
258
259 static inline gpointer
260 build_transaction_key (QmiMessage *message)
261 {
262 gpointer key;
263 guint8 service;
264 guint8 client_id;
265 guint16 transaction_id;
266
267 service = (guint8)qmi_message_get_service (message);
268 client_id = qmi_message_get_client_id (message);
269 transaction_id = qmi_message_get_transaction_id (message);
270
271 /* We're putting a 32 bit value into a gpointer */
272 key = GUINT_TO_POINTER ((((service << 8) | client_id) << 16) | transaction_id);
273
274 return key;
275 }
276
277 static Transaction *
278 device_peek_transaction (QmiDevice *self,
279 gconstpointer key)
280 {
281 return g_hash_table_lookup (self->priv->transactions, key);
282 }
283
284 static Transaction *
285 device_release_transaction (QmiDevice *self,
286 gconstpointer key)
287 {
288 Transaction *tr = NULL;
289
290 /* If found, remove it from the HT */
291 tr = device_peek_transaction (self, key);
292 if (tr)
293 g_hash_table_remove (self->priv->transactions, key);
294
295 return tr;
296 }
297
298 static void
299 transaction_abort_ready (QmiDevice *self,
300 GAsyncResult *res,
301 gpointer key)
302 {
303 Transaction *tr;
304 GError *error = NULL;
305 QmiMessage *abort_response;
306
307 /* Always try to remove from the HT at this point. Note that the transaction
308 * pointer may be NULL already, e.g. if the abort was cancelled. In this case
309 * we totally ignore the result of the abort operation. */
310 tr = device_release_transaction (self, key);
311 if (!tr) {
312 g_debug ("[%s] not processing abort response, operation has already been completed",
313 qmi_file_get_path_display (self->priv->file));
314 return;
315 }
316
317 g_assert (tr->abort_parse_response_fn);
318
319 abort_response = qmi_device_command_full_finish (self, res, &error);
320 if (!abort_response ||
321 !tr->abort_parse_response_fn (self,
322 abort_response,
323 tr->abort_user_data,
324 &error)) {
325 GError *built_error;
326
327 g_debug ("[%s] abort operation failed: %s",
328 qmi_file_get_path_display (self->priv->file), error->message);
329
330 /* We don't want to return any kind of error, because what failed here
331 * is the abort operation for the user request, so always return
332 * QMI_CORE_ERROR_FAILED and provide the description on the error
333 * in the message. */
334 built_error = g_error_new (QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED,
335 "operation failed and couldn't be aborted: %s",
336 error->message);
337 g_error_free (error);
338
339 transaction_complete_and_free (tr, NULL, built_error);
340 g_error_free (built_error);
341 } else {
342 /* aborted successfully */
343 g_debug ("operation aborted successfully");
344 g_assert (tr->abort_error);
345 transaction_complete_and_free (tr, NULL, tr->abort_error);
346 }
347
348 if (abort_response)
349 qmi_message_unref (abort_response);
350 }
351
352 static void
353 transaction_abort (QmiDevice *self,
354 Transaction *tr,
355 GError *abort_error_take)
356 {
357 QmiMessage *abort_request;
358 GError *error = NULL;
359 guint16 transaction_id;
360
361 transaction_id = qmi_message_get_transaction_id (tr->message);
362
363 /* If the command is not abortable, we'll return the error right away
364 * to the user. */
365 if (!__qmi_message_is_abortable (tr->message, tr->message_context)) {
366 g_debug ("[%s] transaction 0x%x aborted, but message is not abortable",
367 qmi_file_get_path_display (self->priv->file), transaction_id);
368 device_release_transaction (self, tr->wait_ctx->key);
369 transaction_complete_and_free (tr, NULL, abort_error_take);
370 g_error_free (abort_error_take);
371 return;
372 }
373
374 /* if the command is abortable but the user didn't use qmi_device_command_abortable(),
375 * then return the error right away anyway */
376 if (!tr->abort_build_request_fn || !tr->abort_parse_response_fn) {
377 g_debug ("[%s] transaction 0x%x aborted, but no way to build abort request",
378 qmi_file_get_path_display (self->priv->file), transaction_id);
379 device_release_transaction (self, tr->wait_ctx->key);
380 transaction_complete_and_free (tr, NULL, abort_error_take);
381 g_error_free (abort_error_take);
382 return;
383 }
384
385 g_debug ("[%s] transaction 0x%x aborted, building abort request...",
386 qmi_file_get_path_display (self->priv->file), transaction_id);
387
388 /* Try to build abort request */
389 abort_request = tr->abort_build_request_fn (self,
390 tr->message,
391 tr->abort_user_data,
392 &error);
393 if (!abort_request) {
394 /* complete the transaction with the error we got while building the
395 * abort request */
396 g_debug ("[%s] transaction 0x%x aborted, but building abort request failed",
397 qmi_file_get_path_display (self->priv->file), transaction_id);
398 device_release_transaction (self, tr->wait_ctx->key);
399 transaction_complete_and_free (tr, NULL, error);
400 g_error_free (error);
401 return;
402 }
403
404 /* If command is abortable, let's abort the operation in the
405 * device. We'll store the specific abort error to use once the abort
406 * operation has been acknowledged by the device. */
407 tr->abort_error = abort_error_take;
408 tr->abort_cancellable = g_cancellable_new ();
409
410 qmi_device_command_full (self,
411 abort_request,
412 NULL,
413 30,
414 tr->abort_cancellable,
415 (GAsyncReadyCallback) transaction_abort_ready,
416 tr->wait_ctx->key);
417
418 qmi_message_unref (abort_request);
419 }
420
421 static gboolean
422 transaction_timed_out (TransactionWaitContext *ctx)
423 {
424 Transaction *tr;
425 GError *error = NULL;
426
427 /* A timed out transaction is always tracked */
428 tr = device_peek_transaction (ctx->self, ctx->key);
429 g_assert (tr);
430
431 tr->timeout_source = NULL;
432
433 /* Increase number of consecutive timeouts */
434 ctx->self->priv->consecutive_timeouts++;
435 g_object_notify_by_pspec (G_OBJECT (ctx->self), properties[PROP_CONSECUTIVE_TIMEOUTS]);
436 g_debug ("[%s] number of consecutive timeouts: %u",
437 qmi_file_get_path_display (ctx->self->priv->file),
438 ctx->self->priv->consecutive_timeouts);
439
440 error = g_error_new (QMI_CORE_ERROR, QMI_CORE_ERROR_TIMEOUT, "Transaction timed out");
441 transaction_abort (ctx->self, tr, error);
442
443 return G_SOURCE_REMOVE;
444 }
445
446 static void
447 transaction_cancelled (GCancellable *cancellable,
448 TransactionWaitContext *ctx)
449 {
450 Transaction *tr;
451 GError *error = NULL;
452
453 tr = device_peek_transaction (ctx->self, ctx->key);
454
455 /* The transaction may have already been cancelled before we stored it in
456 * the tracking table, which means the command was NOT sent to the device
457 * and we can safely ignore the cancellation request. */
458 if (!tr)
459 return;
460
461 tr->cancellable_id = 0;
462
463 error = g_error_new (QMI_PROTOCOL_ERROR, QMI_PROTOCOL_ERROR_ABORTED, "Transaction aborted");
464 transaction_abort (ctx->self, tr, error);
465 }
466
467 static gboolean
468 device_store_transaction (QmiDevice *self,
469 Transaction *tr,
470 guint timeout,
471 GError **error)
472 {
473 gpointer key;
474 Transaction *existing;
475
476 key = build_transaction_key (tr->message);
477
478 /* Setup the timeout and cancellation */
479
480 tr->wait_ctx = g_slice_new (TransactionWaitContext);
481 tr->wait_ctx->self = self;
482 tr->wait_ctx->key = key; /* valid as long as the transaction is in the HT */
483
484 /* Timeout is optional (e.g. disabled when MBIM is used) */
485 if (timeout > 0) {
486 tr->timeout_source = g_timeout_source_new_seconds (timeout);
487 g_source_set_callback (tr->timeout_source, (GSourceFunc)transaction_timed_out, tr->wait_ctx, NULL);
488 g_source_attach (tr->timeout_source, g_main_context_get_thread_default ());
489 g_source_unref (tr->timeout_source);
490 }
491
492 if (tr->cancellable) {
493 /* Note: transaction_cancelled() will also be called directly if the
494 * cancellable is already cancelled */
495 tr->cancellable_id = g_cancellable_connect (tr->cancellable,
496 (GCallback)transaction_cancelled,
497 tr->wait_ctx,
498 NULL);
499 if (!tr->cancellable_id) {
500 g_set_error (error,
501 QMI_PROTOCOL_ERROR,
502 QMI_PROTOCOL_ERROR_ABORTED,
503 "Request is already cancelled");
504 return FALSE;
505 }
506 }
507
508 /* If we have already a transaction with the same ID complete the existing
509 * one with an error before the new one is added, or we'll end up with
510 * dangling timeouts and cancellation handlers that may be fired off later
511 * on. */
512 existing = device_release_transaction (self, key);
513 if (existing) {
514 GError *inner_error;
515
516 /* Complete transaction with an abort error */
517 inner_error = g_error_new (QMI_PROTOCOL_ERROR,
518 QMI_PROTOCOL_ERROR_ABORTED,
519 "Transaction overwritten");
520 transaction_complete_and_free (existing, NULL, inner_error);
521 g_error_free (inner_error);
522 }
523
524 /* Keep in the HT */
525 g_hash_table_insert (self->priv->transactions, key, tr);
526
527 return TRUE;
528 }
529
530 static Transaction *
531 device_match_transaction (QmiDevice *self,
532 QmiMessage *message)
533 {
534 /* msg can be either the original message or the response */
535 return device_release_transaction (self, build_transaction_key (message));
536 }
537
538 /*****************************************************************************/
539
540 static void
541 device_hangup_transactions (QmiDevice *self)
542 {
543 GHashTableIter iter;
544 gpointer key, value;
545 g_autoptr(GError) common_error = NULL;
546
547 common_error = g_error_new (QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED, "endpoint hangup");
548
549 g_hash_table_iter_init (&iter, self->priv->transactions);
550 while (g_hash_table_iter_next (&iter, &key, &value)) {
551 Transaction *tr = value;
552
553 transaction_complete_and_free (tr, NULL, common_error);
554 g_hash_table_iter_remove (&iter);
555 }
556 }
557
558 /*****************************************************************************/
559
560 guint
561 qmi_device_get_consecutive_timeouts (QmiDevice *self)
562 {
563 g_return_val_if_fail (QMI_IS_DEVICE (self), 0);
564
565 return self->priv->consecutive_timeouts;
566 }
567
568 /*****************************************************************************/
569 /* Version info request */
570
571 GArray *
572 qmi_device_get_service_version_info_finish (QmiDevice *self,
573 GAsyncResult *res,
574 GError **error)
575 {
576 return g_task_propagate_pointer (G_TASK (res), error);
577 }
578
579 static void
580 version_info_ready (QmiClientCtl *client_ctl,
581 GAsyncResult *res,
582 GTask *task)
583 {
584 GArray *service_list = NULL;
585 GArray *out;
586 QmiMessageCtlGetVersionInfoOutput *output;
587 GError *error = NULL;
588 guint i;
589
590 /* Check result of the async operation */
591 output = qmi_client_ctl_get_version_info_finish (client_ctl, res, &error);
592 if (!output) {
593 g_task_return_error (task, error);
594 g_object_unref (task);
595 return;
596 }
597
598 /* Check result of the QMI operation */
599 if (!qmi_message_ctl_get_version_info_output_get_result (output, &error)) {
600 qmi_message_ctl_get_version_info_output_unref (output);
601 g_task_return_error (task, error);
602 g_object_unref (task);
603 return;
604 }
605
606 /* QMI operation succeeded, we can now get the outputs */
607 qmi_message_ctl_get_version_info_output_get_service_list (output, &service_list, NULL);
608 out = g_array_sized_new (FALSE, FALSE, sizeof (QmiDeviceServiceVersionInfo), service_list->len);
609 for (i = 0; i < service_list->len; i++) {
610 QmiMessageCtlGetVersionInfoOutputServiceListService *info;
611 QmiDeviceServiceVersionInfo outinfo;
612
613 info = &g_array_index (service_list,
614 QmiMessageCtlGetVersionInfoOutputServiceListService,
615 i);
616 outinfo.service = info->service;
617 outinfo.major_version = info->major_version;
618 outinfo.minor_version = info->minor_version;
619 g_array_append_val (out, outinfo);
620 }
621
622 qmi_message_ctl_get_version_info_output_unref (output);
623 g_task_return_pointer (task, out, (GDestroyNotify)g_array_unref);
624 g_object_unref (task);
625 }
626
627 void
628 qmi_device_get_service_version_info (QmiDevice *self,
629 guint timeout,
630 GCancellable *cancellable,
631 GAsyncReadyCallback callback,
632 gpointer user_data)
633 {
634 qmi_client_ctl_get_version_info (
635 self->priv->client_ctl,
636 NULL,
637 timeout,
638 cancellable,
639 (GAsyncReadyCallback)version_info_ready,
640 g_task_new (self, cancellable, callback, user_data));
641 }
642
643 /*****************************************************************************/
644 /* Version info checks (private) */
645
646 static const QmiMessageCtlGetVersionInfoOutputServiceListService *
647 find_service_version_info (QmiDevice *self,
648 QmiService service)
649 {
650 guint i;
651
652 if (!self->priv->supported_services)
653 return NULL;
654
655 for (i = 0; i < self->priv->supported_services->len; i++) {
656 const QmiMessageCtlGetVersionInfoOutputServiceListService *info;
657
658 info = &g_array_index (self->priv->supported_services,
659 QmiMessageCtlGetVersionInfoOutputServiceListService,
660 i);
661
662 if (service == info->service)
663 return info;
664 }
665
666 return NULL;
667 }
668
669 static gboolean
670 check_service_supported (QmiDevice *self,
671 QmiService service)
672 {
673 /* If we didn't check supported services, just assume it is supported */
674 if (!self->priv->supported_services) {
675 g_debug ("[%s] assuming service '%s' is supported...",
676 qmi_file_get_path_display (self->priv->file),
677 qmi_service_get_string (service));
678 return TRUE;
679 }
680
681 return !!find_service_version_info (self, service);
682 }
683
684 /*****************************************************************************/
685
686 GFile *
687 qmi_device_get_file (QmiDevice *self)
688 {
689 g_return_val_if_fail (QMI_IS_DEVICE (self), NULL);
690
691 return qmi_file_get_file (self->priv->file);
692 }
693
694 GFile *
695 qmi_device_peek_file (QmiDevice *self)
696 {
697 g_return_val_if_fail (QMI_IS_DEVICE (self), NULL);
698
699 return qmi_file_peek_file (self->priv->file);
700 }
701
702 const gchar *
703 qmi_device_get_path (QmiDevice *self)
704 {
705 g_return_val_if_fail (QMI_IS_DEVICE (self), NULL);
706
707 return qmi_file_get_path (self->priv->file);
708 }
709
710 const gchar *
711 qmi_device_get_path_display (QmiDevice *self)
712 {
713 g_return_val_if_fail (QMI_IS_DEVICE (self), NULL);
714
715 return qmi_file_get_path_display (self->priv->file);
716 }
717
718 gboolean
719 qmi_device_is_open (QmiDevice *self)
720 {
721 g_return_val_if_fail (QMI_IS_DEVICE (self), FALSE);
722
723 return !!self->priv->endpoint && qmi_endpoint_is_open (self->priv->endpoint);
724 }
725
726 /*****************************************************************************/
727 /* WWAN iface name
728 * Always reload from scratch, to handle possible net interface renames */
729
730 static void
731 reload_wwan_iface_name (QmiDevice *self)
732 {
733 g_autofree gchar *cdc_wdm_device_name = NULL;
734 guint i;
735 g_autoptr(GError) error = NULL;
736 static const gchar *driver_names[] = { "usbmisc", /* kernel >= 3.6 */
737 "usb" }; /* kernel < 3.6 */
738
739 #if QMI_QRTR_SUPPORTED
740 /* QRTR doesn't have a device file in sysfs, exit right away */
741 if (self->priv->node)
742 return;
743 #endif
744
745 g_clear_pointer (&self->priv->wwan_iface, g_free);
746
747 cdc_wdm_device_name = qmi_helpers_get_devname (qmi_file_get_path (self->priv->file), &error);
748 if (!cdc_wdm_device_name) {
749 g_warning ("[%s] invalid path for cdc-wdm control port: %s",
750 qmi_file_get_path_display (self->priv->file),
751 error->message);
752 return;
753 }
754
755 for (i = 0; i < G_N_ELEMENTS (driver_names) && !self->priv->wwan_iface; i++) {
756 g_autofree gchar *sysfs_path = NULL;
757 g_autoptr(GFile) sysfs_file = NULL;
758 g_autoptr(GFileEnumerator) enumerator = NULL;
759 GFileInfo *file_info = NULL;
760
761 /* WWAN iface name loading only applicable for qmi_wwan driver right now
762 * (so QMI port exposed by the cdc-wdm driver in the usbmisc subsystem),
763 * not for QRTR or any other subsystem or driver */
764 sysfs_path = g_strdup_printf ("/sys/class/%s/%s/device/net/", driver_names[i], cdc_wdm_device_name);
765 sysfs_file = g_file_new_for_path (sysfs_path);
766 enumerator = g_file_enumerate_children (sysfs_file,
767 G_FILE_ATTRIBUTE_STANDARD_NAME,
768 G_FILE_QUERY_INFO_NONE,
769 NULL,
770 NULL);
771 if (!enumerator)
772 continue;
773
774 /* Ignore errors when enumerating */
775 while ((file_info = g_file_enumerator_next_file (enumerator, NULL, NULL)) != NULL) {
776 const gchar *name;
777
778 name = g_file_info_get_name (file_info);
779 if (name) {
780 /* We only expect ONE file in the sysfs directory corresponding
781 * to this control port, if more found for any reason, warn about it */
782 if (self->priv->wwan_iface)
783 g_warning ("[%s] invalid additional wwan iface found: %s",
784 qmi_file_get_path_display (self->priv->file), name);
785 else
786 self->priv->wwan_iface = g_strdup (name);
787 }
788 g_object_unref (file_info);
789 }
790 if (!self->priv->wwan_iface)
791 g_warning ("[%s] wwan iface not found", qmi_file_get_path_display (self->priv->file));
792 }
793
794 /* wwan_iface won't be set at this point if the kernel driver in use isn't in
795 * the usbmisc subsystem */
796 }
797
798 const gchar *
799 qmi_device_get_wwan_iface (QmiDevice *self)
800 {
801 g_return_val_if_fail (QMI_IS_DEVICE (self), NULL);
802
803 reload_wwan_iface_name (self);
804 return self->priv->wwan_iface;
805 }
806
807 /*****************************************************************************/
808 /* Expected data format */
809
810 static gboolean
811 validate_yes_or_no (const gchar value,
812 GError **error)
813 {
814 if (value != 'Y' && value != 'N') {
815 g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED,
816 "Unexpected sysfs file contents: %c", value);
817 return FALSE;
818 }
819
820 return TRUE;
821 }
822
823 static gchar *
824 build_raw_ip_sysfs_path (QmiDevice *self)
825 {
826 return g_strdup_printf ("/sys/class/net/%s/qmi/raw_ip", self->priv->wwan_iface);
827 }
828
829 static gchar *
830 build_pass_through_sysfs_path (QmiDevice *self)
831 {
832 return g_strdup_printf ("/sys/class/net/%s/qmi/pass_through", self->priv->wwan_iface);
833 }
834
835 static gboolean
836 get_expected_data_format (QmiDevice *self,
837 const gchar *raw_ip_sysfs_path,
838 const gchar *pass_through_sysfs_path,
839 GError **error)
840 {
841 gchar raw_ip_value = '\0';
842 gchar pass_through_value = '\0';
843
844 if (!qmi_helpers_read_sysfs_file (raw_ip_sysfs_path, &raw_ip_value, 1, error) ||
845 !validate_yes_or_no (raw_ip_value, error))
846 return QMI_DEVICE_EXPECTED_DATA_FORMAT_UNKNOWN;
847
848 if (raw_ip_value == 'N')
849 return QMI_DEVICE_EXPECTED_DATA_FORMAT_802_3;
850
851 if (qmi_helpers_read_sysfs_file (pass_through_sysfs_path, &pass_through_value, 1, NULL) &&
852 (pass_through_value == 'Y'))
853 return QMI_DEVICE_EXPECTED_DATA_FORMAT_QMAP_PASS_THROUGH;
854
855 return QMI_DEVICE_EXPECTED_DATA_FORMAT_RAW_IP;
856 }
857
858 static gboolean
859 set_expected_data_format (QmiDevice *self,
860 const gchar *raw_ip_sysfs_path,
861 const gchar *pass_through_sysfs_path,
862 QmiDeviceExpectedDataFormat requested,
863 GError **error)
864 {
865 if (requested == QMI_DEVICE_EXPECTED_DATA_FORMAT_802_3) {
866 qmi_helpers_write_sysfs_file (pass_through_sysfs_path, "N", NULL);
867 return qmi_helpers_write_sysfs_file (raw_ip_sysfs_path, "N", error);
868 }
869
870 if (requested == QMI_DEVICE_EXPECTED_DATA_FORMAT_RAW_IP) {
871 qmi_helpers_write_sysfs_file (pass_through_sysfs_path, "N", NULL);
872 return qmi_helpers_write_sysfs_file (raw_ip_sysfs_path, "Y", error);
873 }
874
875 if (requested == QMI_DEVICE_EXPECTED_DATA_FORMAT_QMAP_PASS_THROUGH) {
876 return (qmi_helpers_write_sysfs_file (raw_ip_sysfs_path, "Y", error) &&
877 qmi_helpers_write_sysfs_file (pass_through_sysfs_path, "Y", error));
878 }
879
880 g_assert_not_reached ();
881 }
882
883 static QmiDeviceExpectedDataFormat
884 common_get_set_expected_data_format (QmiDevice *self,
885 QmiDeviceExpectedDataFormat requested,
886 GError **error)
887 {
888 g_autofree gchar *raw_ip = NULL;
889 g_autofree gchar *pass_through = NULL;
890 QmiDeviceExpectedDataFormat expected = QMI_DEVICE_EXPECTED_DATA_FORMAT_UNKNOWN;
891 gboolean readonly;
892
893 /* Make sure we load the WWAN iface name */
894 reload_wwan_iface_name (self);
895
896 /* Expected data format setting and getting is only supported in the qmi_wwan
897 * driver, same as the WWAN iface name detection. Therefore, if we cannot load
898 * the WWAN iface name we can safely assume that we're not using the qmi_wwan
899 * driver. */
900 if (!self->priv->wwan_iface) {
901 g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_UNSUPPORTED,
902 "Setting expected data format management is unsupported by the driver");
903 return QMI_DEVICE_EXPECTED_DATA_FORMAT_UNKNOWN;
904 }
905
906 readonly = (requested == QMI_DEVICE_EXPECTED_DATA_FORMAT_UNKNOWN);
907
908 /* Build sysfs file paths */
909 raw_ip = build_raw_ip_sysfs_path (self);
910 pass_through = build_pass_through_sysfs_path (self);
911
912 /* Set operation? */
913 if (!readonly && !set_expected_data_format (self, raw_ip, pass_through, requested, error))
914 return QMI_DEVICE_EXPECTED_DATA_FORMAT_UNKNOWN;
915
916 /* Get/Set operations */
917 if ((expected = get_expected_data_format (self, raw_ip, pass_through, error)) == QMI_DEVICE_EXPECTED_DATA_FORMAT_UNKNOWN)
918 return QMI_DEVICE_EXPECTED_DATA_FORMAT_UNKNOWN;
919
920 /* If we requested an update but we didn't read that value, report an error */
921 if (!readonly && (requested != expected)) {
922 g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED,
923 "Expected data format not updated properly to '%s': got '%s' instead",
924 qmi_device_expected_data_format_get_string (requested),
925 qmi_device_expected_data_format_get_string (expected));
926 return QMI_DEVICE_EXPECTED_DATA_FORMAT_UNKNOWN;
927 }
928
929 /* If the set operation succeeds, we clear the net port manager, as we may need to use a
930 * different one */
931 g_clear_object (&self->priv->net_port_manager);
932
933 return expected;
934 }
935
936 QmiDeviceExpectedDataFormat
937 qmi_device_get_expected_data_format (QmiDevice *self,
938 GError **error)
939 {
940 g_return_val_if_fail (QMI_IS_DEVICE (self), QMI_DEVICE_EXPECTED_DATA_FORMAT_UNKNOWN);
941
942 return common_get_set_expected_data_format (self, QMI_DEVICE_EXPECTED_DATA_FORMAT_UNKNOWN, error);
943 }
944
945 gboolean
946 qmi_device_set_expected_data_format (QmiDevice *self,
947 QmiDeviceExpectedDataFormat format,
948 GError **error)
949 {
950 g_return_val_if_fail (QMI_IS_DEVICE (self), FALSE);
951
952 return (common_get_set_expected_data_format (self, format, error) != QMI_DEVICE_EXPECTED_DATA_FORMAT_UNKNOWN);
953 }
954
955 gboolean
956 qmi_device_check_expected_data_format_supported (QmiDevice *self,
957 QmiDeviceExpectedDataFormat format,
958 GError **error)
959 {
960 g_autofree gchar *sysfs_path = NULL;
961 gchar value = '\0';
962
963 g_return_val_if_fail (QMI_IS_DEVICE (self), FALSE);
964
965 switch (format) {
966 case QMI_DEVICE_EXPECTED_DATA_FORMAT_802_3:
967 return TRUE;
968 case QMI_DEVICE_EXPECTED_DATA_FORMAT_RAW_IP:
969 reload_wwan_iface_name (self);
970 sysfs_path = build_raw_ip_sysfs_path (self);
971 break;
972 case QMI_DEVICE_EXPECTED_DATA_FORMAT_QMAP_PASS_THROUGH:
973 reload_wwan_iface_name (self);
974 sysfs_path = build_pass_through_sysfs_path (self);
975 break;
976 default:
977 case QMI_DEVICE_EXPECTED_DATA_FORMAT_UNKNOWN:
978 g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED,
979 "Unknown expected data format given: 0x%x", format);
980 return FALSE;
981 }
982
983 g_assert (sysfs_path);
984 return (qmi_helpers_read_sysfs_file (sysfs_path, &value, 1, error) &&
985 validate_yes_or_no (value, error));
986 }
987
988 /*****************************************************************************/
989 /* Register/Unregister clients that want to receive indications */
990
991 static gpointer
992 build_registered_client_key (guint8 cid,
993 QmiService service)
994 {
995 return GUINT_TO_POINTER (((guint8)service << 8) | cid);
996 }
997
998 static gboolean
999 register_client (QmiDevice *self,
1000 QmiClient *client,
1001 GError **error)
1002 {
1003 gpointer key;
1004
1005 key = build_registered_client_key (qmi_client_get_cid (client),
1006 qmi_client_get_service (client));
1007 /* Only add the new client if not already registered one with the same CID
1008 * for the same service */
1009 if (g_hash_table_lookup (self->priv->registered_clients, key)) {
1010 g_set_error (error,
1011 QMI_CORE_ERROR,
1012 QMI_CORE_ERROR_FAILED,
1013 "A client with CID '%u' and service '%s' is already registered",
1014 qmi_client_get_cid (client),
1015 qmi_service_get_string (qmi_client_get_service (client)));
1016 return FALSE;
1017 }
1018
1019 g_hash_table_insert (self->priv->registered_clients,
1020 key,
1021 g_object_ref (client));
1022 return TRUE;
1023 }
1024
1025 static void
1026 unregister_client (QmiDevice *self,
1027 QmiClient *client)
1028 {
1029 g_hash_table_remove (self->priv->registered_clients,
1030 build_registered_client_key (qmi_client_get_cid (client),
1031 qmi_client_get_service (client)));
1032 }
1033
1034 /*****************************************************************************/
1035 /* Allocate new client */
1036
1037 typedef struct {
1038 GType client_type;
1039 QmiService service;
1040 guint8 cid;
1041 } AllocateClientContext;
1042
1043 static void
1044 allocate_client_context_free (AllocateClientContext *ctx)
1045 {
1046 g_slice_free (AllocateClientContext, ctx);
1047 }
1048
1049 QmiClient *
1050 qmi_device_allocate_client_finish (QmiDevice *self,
1051 GAsyncResult *res,
1052 GError **error)
1053 {
1054 return g_task_propagate_pointer (G_TASK (res), error);
1055 }
1056
1057 static void
1058 build_client_object (GTask *task)
1059 {
1060 const QmiMessageCtlGetVersionInfoOutputServiceListService *version_info;
1061 g_autofree gchar *version_string = NULL;
1062 QmiDevice *self;
1063 AllocateClientContext *ctx;
1064 QmiClient *client;
1065 GError *error = NULL;
1066
1067 self = g_task_get_source_object (task);
1068 ctx = g_task_get_task_data (task);
1069
1070 /* We now have a proper CID for the client, we should be able to create it
1071 * right away */
1072 client = g_object_new (ctx->client_type,
1073 QMI_CLIENT_DEVICE, self,
1074 QMI_CLIENT_SERVICE, ctx->service,
1075 QMI_CLIENT_CID, ctx->cid,
1076 NULL);
1077
1078 /* Add version info to the client if it was retrieved */
1079 version_info = find_service_version_info (self, ctx->service);
1080 if (version_info)
1081 g_object_set (client,
1082 QMI_CLIENT_VERSION_MAJOR, version_info->major_version,
1083 QMI_CLIENT_VERSION_MINOR, version_info->minor_version,
1084 NULL);
1085 #if QMI_QRTR_SUPPORTED
1086 else if (self->priv->node) {
1087 /* QRTR does not have any way of fetching version information. Assume
1088 * all services can handle all message types and TLVs. */
1089 g_debug ("[%s] client version cannot be retrieved when using QRTR",
1090 qmi_file_get_path_display (self->priv->file));
1091 g_object_set (client,
1092 QMI_CLIENT_VERSION_MAJOR, QMI_CLIENT_VERSION_UNKNOWN,
1093 QMI_CLIENT_VERSION_MINOR, QMI_CLIENT_VERSION_UNKNOWN,
1094 NULL);
1095 }
1096 #endif
1097
1098 /* Register the client to get indications */
1099 if (!register_client (self, client, &error)) {
1100 g_prefix_error (&error,
1101 "Cannot register new client with CID '%u' and service '%s'",
1102 ctx->cid,
1103 qmi_service_get_string (ctx->service));
1104 g_task_return_error (task, error);
1105 g_object_unref (task);
1106 g_object_unref (client);
1107 return;
1108 }
1109
1110 /* Build version string for the logging */
1111 if (self->priv->supported_services) {
1112 const QmiMessageCtlGetVersionInfoOutputServiceListService *info;
1113
1114 info = find_service_version_info (self, ctx->service);
1115 if (info)
1116 version_string = g_strdup_printf ("%u.%u", info->major_version, info->minor_version);
1117 }
1118
1119 g_debug ("[%s] registered '%s' (version %s) client with ID '%u'",
1120 qmi_file_get_path_display (self->priv->file),
1121 qmi_service_get_string (ctx->service),
1122 version_string ? version_string : "unknown",
1123 ctx->cid);
1124
1125 /* Client created and registered, complete successfully */
1126 g_task_return_pointer (task, client, g_object_unref);
1127 g_object_unref (task);
1128 }
1129
1130 #define ALLOCATE_CID_READY(MessageName, message_name) \
1131 static void \
1132 message_name##_ready (QmiClientCtl *client_ctl, \
1133 GAsyncResult *res, \
1134 GTask *task) \
1135 { \
1136 g_autoptr(QmiMessageCtl##MessageName##Output) output = NULL; \
1137 QmiService service; \
1138 guint8 cid; \
1139 GError *error = NULL; \
1140 AllocateClientContext *ctx; \
1141 \
1142 /* Check result of the async operation */ \
1143 output = qmi_client_ctl_##message_name##_finish (client_ctl, res, &error); \
1144 if (!output) { \
1145 g_prefix_error (&error, "CID allocation failed in the CTL client: "); \
1146 g_task_return_error (task, error); \
1147 g_object_unref (task); \
1148 return; \
1149 } \
1150 \
1151 /* Check result of the QMI operation */ \
1152 if (!qmi_message_ctl_##message_name##_output_get_result (output, &error)) { \
1153 g_task_return_error (task, error); \
1154 g_object_unref (task); \
1155 return; \
1156 } \
1157 \
1158 /* Allocation info is mandatory when result is success */ \
1159 g_assert (qmi_message_ctl_##message_name##_output_get_allocation_info ( \
1160 output, &service, &cid, NULL)); \
1161 \
1162 ctx = g_task_get_task_data (task); \
1163 \
1164 if (service != ctx->service) { \
1165 g_task_return_new_error ( \
1166 task, \
1167 QMI_CORE_ERROR, \
1168 QMI_CORE_ERROR_FAILED, \
1169 "CID allocation failed in the CTL client: " \
1170 "Service mismatch (requested '%s', got '%s')", \
1171 qmi_service_get_string (ctx->service), \
1172 qmi_service_get_string (service)); \
1173 g_object_unref (task); \
1174 return; \
1175 } \
1176 \
1177 ctx->cid = cid; \
1178 build_client_object (task); \
1179 }
1180
1181 ALLOCATE_CID_READY (AllocateCid, allocate_cid)
1182 ALLOCATE_CID_READY (InternalAllocateCidQrtr, internal_allocate_cid_qrtr)
1183
1184 void
1185 qmi_device_allocate_client (QmiDevice *self,
1186 QmiService service,
1187 guint8 cid,
1188 guint timeout,
1189 GCancellable *cancellable,
1190 GAsyncReadyCallback callback,
1191 gpointer user_data)
1192 {
1193 AllocateClientContext *ctx;
1194 GTask *task;
1195
1196 g_return_if_fail (QMI_IS_DEVICE (self));
1197 g_return_if_fail (service != QMI_SERVICE_UNKNOWN);
1198
1199 ctx = g_slice_new0 (AllocateClientContext);
1200 ctx->service = service;
1201 ctx->client_type = G_TYPE_INVALID;
1202
1203 task = g_task_new (self, cancellable, callback, user_data);
1204 g_task_set_task_data (task,
1205 ctx,
1206 (GDestroyNotify)allocate_client_context_free);
1207
1208 /* Check if the requested service is supported by the device */
1209 if (!check_service_supported (self, service)) {
1210 g_task_return_new_error (task,
1211 QMI_CORE_ERROR,
1212 QMI_CORE_ERROR_UNSUPPORTED,
1213 "Service '%s' not supported by the device",
1214 qmi_service_get_string (service));
1215 g_object_unref (task);
1216 return;
1217 }
1218
1219 switch (service) {
1220 case QMI_SERVICE_CTL:
1221 g_task_return_new_error (task,
1222 QMI_CORE_ERROR,
1223 QMI_CORE_ERROR_INVALID_ARGS,
1224 "Cannot create additional clients for the CTL service");
1225 g_object_unref (task);
1226 return;
1227 case QMI_SERVICE_DMS:
1228 #if defined HAVE_QMI_SERVICE_DMS
1229 ctx->client_type = QMI_TYPE_CLIENT_DMS;
1230 #endif
1231 break;
1232 case QMI_SERVICE_WDS:
1233 #if defined HAVE_QMI_SERVICE_WDS
1234 ctx->client_type = QMI_TYPE_CLIENT_WDS;
1235 #endif
1236 break;
1237 case QMI_SERVICE_NAS:
1238 #if defined HAVE_QMI_SERVICE_NAS
1239 ctx->client_type = QMI_TYPE_CLIENT_NAS;
1240 #endif
1241 break;
1242 case QMI_SERVICE_WMS:
1243 #if defined HAVE_QMI_SERVICE_WMS
1244 ctx->client_type = QMI_TYPE_CLIENT_WMS;
1245 #endif
1246 break;
1247 case QMI_SERVICE_PDS:
1248 #if defined HAVE_QMI_SERVICE_PDS
1249 ctx->client_type = QMI_TYPE_CLIENT_PDS;
1250 #endif
1251 break;
1252 case QMI_SERVICE_PDC:
1253 #if defined HAVE_QMI_SERVICE_PDC
1254 ctx->client_type = QMI_TYPE_CLIENT_PDC;
1255 #endif
1256 break;
1257 case QMI_SERVICE_PBM:
1258 #if defined HAVE_QMI_SERVICE_PBM
1259 ctx->client_type = QMI_TYPE_CLIENT_PBM;
1260 #endif
1261 break;
1262 case QMI_SERVICE_UIM:
1263 #if defined HAVE_QMI_SERVICE_UIM
1264 ctx->client_type = QMI_TYPE_CLIENT_UIM;
1265 #endif
1266 break;
1267 case QMI_SERVICE_OMA:
1268 #if defined HAVE_QMI_SERVICE_OMA
1269 ctx->client_type = QMI_TYPE_CLIENT_OMA;
1270 #endif
1271 break;
1272 case QMI_SERVICE_GAS:
1273 #if defined HAVE_QMI_SERVICE_GAS
1274 ctx->client_type = QMI_TYPE_CLIENT_GAS;
1275 #endif
1276 break;
1277 case QMI_SERVICE_GMS:
1278 #if defined HAVE_QMI_SERVICE_GMS
1279 ctx->client_type = QMI_TYPE_CLIENT_GMS;
1280 #endif
1281 break;
1282 case QMI_SERVICE_WDA:
1283 #if defined HAVE_QMI_SERVICE_WDA
1284 ctx->client_type = QMI_TYPE_CLIENT_WDA;
1285 #endif
1286 break;
1287 case QMI_SERVICE_VOICE:
1288 #if defined HAVE_QMI_SERVICE_VOICE
1289 ctx->client_type = QMI_TYPE_CLIENT_VOICE;
1290 #endif
1291 break;
1292 case QMI_SERVICE_LOC:
1293 #if defined HAVE_QMI_SERVICE_LOC
1294 ctx->client_type = QMI_TYPE_CLIENT_LOC;
1295 #endif
1296 break;
1297 case QMI_SERVICE_QOS:
1298 #if defined HAVE_QMI_SERVICE_QOS
1299 ctx->client_type = QMI_TYPE_CLIENT_QOS;
1300 #endif
1301 break;
1302 case QMI_SERVICE_DSD:
1303 #if defined HAVE_QMI_SERVICE_DSD
1304 ctx->client_type = QMI_TYPE_CLIENT_DSD;
1305 #endif
1306 break;
1307 case QMI_SERVICE_SAR:
1308 #if defined HAVE_QMI_SERVICE_SAR
1309 ctx->client_type = QMI_TYPE_CLIENT_SAR;
1310 #endif
1311 break;
1312 case QMI_SERVICE_DPM:
1313 #if defined HAVE_QMI_SERVICE_DPM
1314 ctx->client_type = QMI_TYPE_CLIENT_DPM;
1315 #endif
1316 break;
1317 case QMI_SERVICE_FOX:
1318 #if defined HAVE_QMI_SERVICE_FOX
1319 ctx->client_type = QMI_TYPE_CLIENT_FOX;
1320 #endif
1321 break;
1322 case QMI_SERVICE_ATR:
1323 #if defined HAVE_QMI_SERVICE_ATR
1324 ctx->client_type = QMI_TYPE_CLIENT_ATR;
1325 #endif
1326 break;
1327 case QMI_SERVICE_IMSP:
1328 #if defined HAVE_QMI_SERVICE_IMSP
1329 ctx->client_type = QMI_TYPE_CLIENT_IMSP;
1330 #endif
1331 break;
1332 case QMI_SERVICE_IMSA:
1333 #if defined HAVE_QMI_SERVICE_IMSA
1334 ctx->client_type = QMI_TYPE_CLIENT_IMSA;
1335 #endif
1336 break;
1337 case QMI_SERVICE_IMS:
1338 #if defined HAVE_QMI_SERVICE_IMS
1339 ctx->client_type = QMI_TYPE_CLIENT_IMS;
1340 #endif
1341 break;
1342 case QMI_SERVICE_SSC:
1343 #if defined HAVE_QMI_SERVICE_SSC
1344 ctx->client_type = QMI_TYPE_CLIENT_SSC;
1345 #endif
1346 break;
1347 case QMI_SERVICE_UNKNOWN:
1348 g_assert_not_reached ();
1349
1350 case QMI_SERVICE_AUTH:
1351 case QMI_SERVICE_AT:
1352 case QMI_SERVICE_CAT2:
1353 case QMI_SERVICE_QCHAT:
1354 case QMI_SERVICE_RMTFS:
1355 case QMI_SERVICE_TEST:
1356 case QMI_SERVICE_ADC:
1357 case QMI_SERVICE_CSD:
1358 case QMI_SERVICE_MFS:
1359 case QMI_SERVICE_TIME:
1360 case QMI_SERVICE_TS:
1361 case QMI_SERVICE_TMD:
1362 case QMI_SERVICE_SAP:
1363 case QMI_SERVICE_TSYNC:
1364 case QMI_SERVICE_RFSA:
1365 case QMI_SERVICE_CSVT:
1366 case QMI_SERVICE_QCMAP:
1367 case QMI_SERVICE_IMSVT:
1368 case QMI_SERVICE_COEX:
1369 case QMI_SERVICE_STX:
1370 case QMI_SERVICE_BIT:
1371 case QMI_SERVICE_IMSRTP:
1372 case QMI_SERVICE_IMSDCM:
1373 case QMI_SERVICE_RFRPE:
1374 case QMI_SERVICE_SSCTL:
1375 case QMI_SERVICE_CAT:
1376 case QMI_SERVICE_RMS:
1377 case QMI_SERVICE_FOTA:
1378 default:
1379 break;
1380 }
1381
1382 if (ctx->client_type == G_TYPE_INVALID) {
1383 g_task_return_new_error (task, QMI_CORE_ERROR, QMI_CORE_ERROR_INVALID_ARGS,
1384 "Clients for service '%s' not supported",
1385 qmi_service_get_string (service));
1386 g_object_unref (task);
1387 return;
1388 }
1389
1390 /* Allocate a new CID for the client to be created */
1391 if (cid == QMI_CID_NONE) {
1392 g_debug ("[%s] allocating new client ID...",
1393 qmi_file_get_path_display (self->priv->file));
1394
1395 /* 8-bit service */
1396 if (ctx->service <= G_MAXUINT8) {
1397 g_autoptr(QmiMessageCtlAllocateCidInput) input = NULL;
1398
1399 input = qmi_message_ctl_allocate_cid_input_new ();
1400 qmi_message_ctl_allocate_cid_input_set_service (input, ctx->service, NULL);
1401 qmi_client_ctl_allocate_cid (self->priv->client_ctl,
1402 input,
1403 timeout,
1404 cancellable,
1405 (GAsyncReadyCallback)allocate_cid_ready,
1406 task);
1407 }
1408 /* 16-bit service */
1409 else if (ctx->service > G_MAXUINT8 && ctx->service <= G_MAXUINT16) {
1410 g_autoptr(QmiMessageCtlInternalAllocateCidQrtrInput) input = NULL;
1411
1412 input = qmi_message_ctl_internal_allocate_cid_qrtr_input_new ();
1413 qmi_message_ctl_internal_allocate_cid_qrtr_input_set_service (input, ctx->service, NULL);
1414 qmi_client_ctl_internal_allocate_cid_qrtr (self->priv->client_ctl,
1415 input,
1416 timeout,
1417 cancellable,
1418 (GAsyncReadyCallback)internal_allocate_cid_qrtr_ready,
1419 task);
1420 } else
1421 g_assert_not_reached ();
1422 return;
1423 }
1424
1425 /* Reuse the given CID */
1426 g_debug ("[%s] reusing client CID '%u'...", qmi_file_get_path_display (self->priv->file), cid);
1427 ctx->cid = cid;
1428 build_client_object (task);
1429 }
1430
1431 /*****************************************************************************/
1432 /* Release client */
1433
1434 gboolean
1435 qmi_device_release_client_finish (QmiDevice *self,
1436 GAsyncResult *res,
1437 GError **error)
1438 {
1439 return g_task_propagate_boolean (G_TASK (res), error);
1440 }
1441
1442 #define RELEASE_CID_READY(MessageName, message_name) \
1443 static void \
1444 message_name##_ready (QmiClientCtl *client_ctl, \
1445 GAsyncResult *res, \
1446 GTask *task) \
1447 { \
1448 g_autoptr(QmiMessageCtl##MessageName##Output) output = NULL; \
1449 GError *error = NULL; \
1450 \
1451 /* Note: even if we return an error, the client is to be considered \
1452 * released! (so shouldn't be used) */ \
1453 \
1454 /* Check result of the async operation */ \
1455 output = qmi_client_ctl_##message_name##_finish (client_ctl, res, &error); \
1456 if (!output) { \
1457 g_task_return_error (task, error); \
1458 g_object_unref (task); \
1459 return; \
1460 } \
1461 \
1462 /* Check result of the QMI operation */ \
1463 if (!qmi_message_ctl_##message_name##_output_get_result (output, &error)) { \
1464 g_task_return_error (task, error); \
1465 g_object_unref (task); \
1466 return; \
1467 } \
1468 \
1469 g_task_return_boolean (task, TRUE); \
1470 g_object_unref (task); \
1471 }
1472
1473 RELEASE_CID_READY (ReleaseCid, release_cid)
1474 RELEASE_CID_READY (InternalReleaseCidQrtr, internal_release_cid_qrtr)
1475
1476 void
1477 qmi_device_release_client (QmiDevice *self,
1478 QmiClient *client,
1479 QmiDeviceReleaseClientFlags flags,
1480 guint timeout,
1481 GCancellable *cancellable,
1482 GAsyncReadyCallback callback,
1483 gpointer user_data)
1484 {
1485 g_autofree gchar *flags_str = NULL;
1486 GTask *task;
1487 QmiService service;
1488 guint8 cid;
1489
1490 g_return_if_fail (QMI_IS_DEVICE (self));
1491 g_return_if_fail (QMI_IS_CLIENT (client));
1492
1493 cid = qmi_client_get_cid (client);
1494 service = qmi_client_get_service (client);
1495
1496 /* The CTL client should not have been created out of the QmiDevice */
1497 g_return_if_fail (service != QMI_SERVICE_CTL);
1498
1499 flags_str = qmi_device_release_client_flags_build_string_from_mask (flags);
1500 g_debug ("[%s] releasing '%s' client with flags '%s'...",
1501 qmi_file_get_path_display (self->priv->file),
1502 qmi_service_get_string (service),
1503 flags_str);
1504
1505 task = g_task_new (self, cancellable, callback, user_data);
1506
1507 /* Do not try to release an already released client */
1508 if (cid == QMI_CID_NONE) {
1509 g_task_return_new_error (task,
1510 QMI_CORE_ERROR,
1511 QMI_CORE_ERROR_INVALID_ARGS,
1512 "Client is already released");
1513 g_object_unref (task);
1514 return;
1515 }
1516
1517 /* Keep the client object valid until after we reset its contents below */
1518 g_object_ref (client);
1519 {
1520 /* Unregister from device */
1521 unregister_client (self, client);
1522
1523 g_debug ("[%s] unregistered '%s' client with ID '%u'",
1524 qmi_file_get_path_display (self->priv->file),
1525 qmi_service_get_string (service),
1526 cid);
1527
1528 /* Reset the contents of the client object, making it invalid */
1529 g_object_set (client,
1530 QMI_CLIENT_CID, QMI_CID_NONE,
1531 QMI_CLIENT_SERVICE, QMI_SERVICE_UNKNOWN,
1532 QMI_CLIENT_DEVICE, NULL,
1533 NULL);
1534 }
1535 g_object_unref (client);
1536
1537 /* And now, really try to release the CID */
1538 if (flags & QMI_DEVICE_RELEASE_CLIENT_FLAGS_RELEASE_CID) {
1539 /* 8-bit service */
1540 if (service <= G_MAXUINT8) {
1541 g_autoptr(QmiMessageCtlReleaseCidInput)input = NULL;
1542
1543 input = qmi_message_ctl_release_cid_input_new ();
1544 qmi_message_ctl_release_cid_input_set_release_info (input, service, cid, NULL);
1545 qmi_client_ctl_release_cid (self->priv->client_ctl,
1546 input,
1547 timeout,
1548 cancellable,
1549 (GAsyncReadyCallback)release_cid_ready,
1550 task);
1551 }
1552 /* 16-bit service */
1553 else if (service > G_MAXUINT8 && service <= G_MAXUINT16) {
1554 g_autoptr(QmiMessageCtlInternalReleaseCidQrtrInput) input = NULL;
1555
1556 input = qmi_message_ctl_internal_release_cid_qrtr_input_new ();
1557 qmi_message_ctl_internal_release_cid_qrtr_input_set_release_info (input, service, cid, NULL);
1558 qmi_client_ctl_internal_release_cid_qrtr (self->priv->client_ctl,
1559 input,
1560 timeout,
1561 cancellable,
1562 (GAsyncReadyCallback)internal_release_cid_qrtr_ready,
1563 task);
1564 } else
1565 g_assert_not_reached ();
1566 return;
1567 }
1568
1569 /* No need to release the CID, so just done */
1570 g_task_return_boolean (task, TRUE);
1571 g_object_unref (task);
1572 return;
1573 }
1574
1575 /*****************************************************************************/
1576 /* Set instance ID */
1577
1578 gboolean
1579 qmi_device_set_instance_id_finish (QmiDevice *self,
1580 GAsyncResult *res,
1581 guint16 *link_id,
1582 GError **error)
1583 {
1584 gssize value;
1585
1586 value = g_task_propagate_int (G_TASK (res), error);
1587 if (value == -1)
1588 return FALSE;
1589
1590 if (link_id)
1591 *link_id = (guint16)value;
1592
1593 return TRUE;
1594 }
1595
1596 static void
1597 set_instance_id_ready (QmiClientCtl *client_ctl,
1598 GAsyncResult *res,
1599 GTask *task)
1600 {
1601 QmiMessageCtlSetInstanceIdOutput *output;
1602 GError *error = NULL;
1603
1604 /* Check result of the async operation */
1605 output = qmi_client_ctl_set_instance_id_finish (client_ctl, res, &error);
1606 if (!output)
1607 g_task_return_error (task, error);
1608 else {
1609 /* Check result of the QMI operation */
1610 if (!qmi_message_ctl_set_instance_id_output_get_result (output, &error))
1611 g_task_return_error (task, error);
1612 else {
1613 guint16 link_id;
1614
1615 qmi_message_ctl_set_instance_id_output_get_link_id (output, &link_id, NULL);
1616 g_task_return_int (task, link_id);
1617 }
1618 qmi_message_ctl_set_instance_id_output_unref (output);
1619 }
1620
1621 g_object_unref (task);
1622 }
1623
1624 void
1625 qmi_device_set_instance_id (QmiDevice *self,
1626 guint8 instance_id,
1627 guint timeout,
1628 GCancellable *cancellable,
1629 GAsyncReadyCallback callback,
1630 gpointer user_data)
1631 {
1632 GTask *task;
1633 QmiMessageCtlSetInstanceIdInput *input;
1634
1635 g_return_if_fail (QMI_IS_DEVICE (self));
1636
1637 task = g_task_new (self, cancellable, callback, user_data);
1638
1639 input = qmi_message_ctl_set_instance_id_input_new ();
1640 qmi_message_ctl_set_instance_id_input_set_id (
1641 input,
1642 instance_id,
1643 NULL);
1644 qmi_client_ctl_set_instance_id (self->priv->client_ctl,
1645 input,
1646 timeout,
1647 cancellable,
1648 (GAsyncReadyCallback)set_instance_id_ready,
1649 task);
1650 qmi_message_ctl_set_instance_id_input_unref (input);
1651 }
1652
1653 /*****************************************************************************/
1654 /* Input channel processing */
1655
1656 static void process_message (QmiMessage *message, QmiDevice *self);
1657
1658 static void
1659 endpoint_new_data_cb (QmiEndpoint *endpoint,
1660 QmiDevice *self)
1661 {
1662 GError *error = NULL;
1663
1664 if (!qmi_endpoint_parse_buffer (endpoint,
1665 (QmiMessageHandler)process_message,
1666 self,
1667 &error)) {
1668 g_warning ("[%s] parsing error: %s",
1669 qmi_file_get_path_display (self->priv->file), error->message);
1670 g_error_free (error);
1671 }
1672 }
1673
1674 static void
1675 endpoint_hangup_cb (QmiEndpoint *endpoint,
1676 QmiDevice *self)
1677 {
1678 g_debug ("[%s] endpoint hangup: removed",
1679 qmi_file_get_path_display (self->priv->file));
1680
1681 /* cancel all ongoing transactions as the endpoing hangup happened */
1682 device_hangup_transactions (self);
1683
1684 g_signal_emit (self, signals[SIGNAL_REMOVED], 0);
1685 }
1686
1687 typedef struct {
1688 QmiClient *client;
1689 QmiMessage *message;
1690 } IdleIndicationContext;
1691
1692 static gboolean
1693 process_indication_idle (IdleIndicationContext *ctx)
1694 {
1695 g_assert (ctx->client != NULL);
1696 g_assert (ctx->message != NULL);
1697
1698 __qmi_client_process_indication (ctx->client, ctx->message);
1699
1700 g_object_unref (ctx->client);
1701 qmi_message_unref (ctx->message);
1702 g_slice_free (IdleIndicationContext, ctx);
1703 return FALSE;
1704 }
1705
1706 static void
1707 report_indication (QmiClient *client,
1708 QmiMessage *message)
1709 {
1710 IdleIndicationContext *ctx;
1711 GSource *source;
1712
1713 /* Setup an idle to Pass the indication down to the client */
1714 ctx = g_slice_new (IdleIndicationContext);
1715 ctx->client = g_object_ref (client);
1716 ctx->message = qmi_message_ref (message);
1717
1718 source = g_idle_source_new ();
1719 g_source_set_priority (source, G_PRIORITY_DEFAULT);
1720 g_source_set_callback (source, (GSourceFunc)process_indication_idle, ctx, NULL);
1721 g_source_attach (source, g_main_context_get_thread_default ());
1722 g_source_unref (source);
1723 }
1724
1725 static void
1726 trace_message (QmiDevice *self,
1727 QmiMessage *message,
1728 gboolean sent_or_received,
1729 const gchar *message_str,
1730 QmiMessageContext *message_context)
1731 {
1732 gchar *printable;
1733 const gchar *prefix_str;
1734 const gchar *action_str;
1735 gchar *vendor_str = NULL;
1736
1737 if (!qmi_utils_get_traces_enabled ())
1738 return;
1739
1740 if (sent_or_received) {
1741 prefix_str = "<<<<<< ";
1742 action_str = "sent";
1743 } else {
1744 prefix_str = "<<<<<< ";
1745 action_str = "received";
1746 }
1747
1748 if (qmi_utils_get_show_personal_info () || (((GByteArray *)message)->len < MAX_PRINTED_BYTES)) {
1749 printable = qmi_common_str_hex (((GByteArray *)message)->data,
1750 ((GByteArray *)message)->len,
1751 ':');
1752 } else {
1753 g_autofree gchar *tmp = NULL;
1754
1755 tmp = qmi_common_str_hex (((GByteArray *)message)->data, MAX_PRINTED_BYTES, ':');
1756 printable = g_strdup_printf ("%s...", tmp);
1757 }
1758
1759 g_debug ("[%s] %s message...\n"
1760 "%sRAW:\n"
1761 "%s length = %u\n"
1762 "%s data = %s\n",
1763 qmi_file_get_path_display (self->priv->file), action_str,
1764 prefix_str,
1765 prefix_str, ((GByteArray *)message)->len,
1766 prefix_str, printable);
1767 g_free (printable);
1768
1769 if (message_context) {
1770 guint16 vendor_id;
1771
1772 vendor_id = qmi_message_context_get_vendor_id (message_context);
1773 if (vendor_id != QMI_MESSAGE_VENDOR_GENERIC)
1774 vendor_str = g_strdup_printf ("vendor-specific (0x%04x)", vendor_id);
1775 }
1776
1777 printable = qmi_message_get_printable_full (message, message_context, prefix_str);
1778 g_debug ("[%s] %s %s %s (translated)...\n%s",
1779 qmi_file_get_path_display (self->priv->file),
1780 action_str,
1781 vendor_str ? vendor_str : "generic",
1782 message_str,
1783 printable);
1784 g_free (printable);
1785
1786 g_free (vendor_str);
1787 }
1788
1789 static void
1790 process_message (QmiMessage *message,
1791 QmiDevice *self)
1792 {
1793 if (qmi_message_is_indication (message)) {
1794 /* Indication traces translated without an explicit vendor */
1795 trace_message (self, message, FALSE, "indication", NULL);
1796
1797 /* Generic emission of the indication */
1798 g_signal_emit (self, signals[SIGNAL_INDICATION], 0, message);
1799
1800 if (qmi_message_get_client_id (message) == QMI_CID_BROADCAST) {
1801 GHashTableIter iter;
1802 gpointer key;
1803 QmiClient *client;
1804
1805 g_hash_table_iter_init (&iter, self->priv->registered_clients);
1806 while (g_hash_table_iter_next (&iter, &key, (gpointer *)&client)) {
1807 /* For broadcast messages, report them just if the service matches */
1808 if (qmi_message_get_service (message) == qmi_client_get_service (client))
1809 report_indication (client, message);
1810 }
1811 } else {
1812 QmiClient *client;
1813
1814 client = g_hash_table_lookup (self->priv->registered_clients,
1815 build_registered_client_key (qmi_message_get_client_id (message),
1816 qmi_message_get_service (message)));
1817 if (client)
1818 report_indication (client, message);
1819 }
1820
1821 return;
1822 }
1823
1824 if (qmi_message_is_response (message)) {
1825 Transaction *tr;
1826
1827 tr = device_match_transaction (self, message);
1828 if (!tr) {
1829 /* Unmatched transactions translated without an explicit context */
1830 trace_message (self, message, FALSE, "response", NULL);
1831 g_debug ("[%s] no transaction matched in received message",
1832 qmi_file_get_path_display (self->priv->file));
1833 return;
1834 }
1835
1836 /* Did the transaction sequence get out of sync? e.g. if the module reboots itself
1837 * while we're talking to it. If so, fail the transaction right away without setting the
1838 * message as response, or otherwise the parsers will complain */
1839 if (qmi_message_get_message_id (tr->message) != qmi_message_get_message_id (message)) {
1840 g_autoptr(GError) error = NULL;
1841
1842 error = g_error_new (QMI_CORE_ERROR,
1843 QMI_CORE_ERROR_UNEXPECTED_MESSAGE,
1844 "Unexpected response of type 0x%04x received matching transaction for request of type 0x%04x",
1845 qmi_message_get_message_id (message),
1846 qmi_message_get_message_id (tr->message));
1847
1848 /* Translate without an explicit context as this message has nothing to do with the
1849 * request. */
1850 trace_message (self, message, FALSE, "response", NULL);
1851 g_debug ("[%s] mismatched message id in received message for transaction 0x%04x (expected 0x%04x, received 0x%04x)",
1852 qmi_file_get_path_display (self->priv->file),
1853 qmi_message_get_transaction_id (message),
1854 qmi_message_get_message_id (tr->message),
1855 qmi_message_get_message_id (message));
1856 transaction_complete_and_free (tr, NULL, error);
1857 return;
1858 }
1859
1860 /* Reset number of consecutive timeouts */
1861 if (self->priv->consecutive_timeouts > 0) {
1862 g_debug ("[%s] reseted number of consecutive timeouts",
1863 qmi_file_get_path_display (self->priv->file));
1864 self->priv->consecutive_timeouts = 0;
1865 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_CONSECUTIVE_TIMEOUTS]);
1866 }
1867
1868 /* Matched transactions translated with the same context as the request */
1869 trace_message (self, message, FALSE, "response", tr->message_context);
1870 /* Report the reply message */
1871 transaction_complete_and_free (tr, message, NULL);
1872 return;
1873 }
1874
1875 /* Unexpected message types translated without an explicit context */
1876 trace_message (self, message, FALSE, "unexpected message", NULL);
1877 g_debug ("[%s] message received but it is neither an indication nor a response. Skipping it.",
1878 qmi_file_get_path_display (self->priv->file));
1879 }
1880
1881 /*****************************************************************************/
1882
1883 static gboolean
1884 setup_net_port_manager (QmiDevice *self,
1885 GError **error)
1886 {
1887 QmiDeviceExpectedDataFormat expected_data_format;
1888
1889 /* If we have a valid one already, use that one */
1890 if (self->priv->net_port_manager)
1891 return TRUE;
1892
1893 /* The qmi_wwan driver allows configuring the expected data format,
1894 * and depending on the configured one, we'll use one link management
1895 * api or another one. */
1896 expected_data_format = qmi_device_get_expected_data_format (self, NULL);
1897
1898 switch (expected_data_format) {
1899 case QMI_DEVICE_EXPECTED_DATA_FORMAT_802_3:
1900 g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED,
1901 "Link management not supported with the expected data format configured as 802.3");
1902 break;
1903 case QMI_DEVICE_EXPECTED_DATA_FORMAT_RAW_IP:
1904 self->priv->net_port_manager = QMI_NET_PORT_MANAGER (qmi_net_port_manager_qmiwwan_new (self->priv->wwan_iface, error));
1905 break;
1906 case QMI_DEVICE_EXPECTED_DATA_FORMAT_QMAP_PASS_THROUGH:
1907 case QMI_DEVICE_EXPECTED_DATA_FORMAT_UNKNOWN:
1908 default:
1909 #if defined RMNET_SUPPORT_ENABLED
1910 /* when the data format is unknown, it very likely is because this
1911 * is not the qmi_wwan driver; fallback to plain rmnet in that
1912 * case. */
1913 self->priv->net_port_manager = QMI_NET_PORT_MANAGER (qmi_net_port_manager_rmnet_new (error));
1914 #else
1915 g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED,
1916 "Link management not supported");
1917 #endif
1918 break;
1919 }
1920
1921 return !!self->priv->net_port_manager;
1922 }
1923
1924 /*****************************************************************************/
1925 /* Link management APIs */
1926
1927 typedef struct {
1928 guint mux_id;
1929 gchar *ifname;
1930 } AddLinkResult;
1931
1932 static void
1933 add_link_result_free (AddLinkResult *ctx)
1934 {
1935 g_free (ctx->ifname);
1936 g_free (ctx);
1937 }
1938
1939 gchar *
1940 qmi_device_add_link_with_flags_finish (QmiDevice *self,
1941 GAsyncResult *res,
1942 guint *mux_id,
1943 GError **error)
1944 {
1945 AddLinkResult *ctx;
1946 gchar *ifname;
1947
1948 ctx = g_task_propagate_pointer (G_TASK (res), error);
1949 if (!ctx)
1950 return NULL;
1951
1952 if (mux_id)
1953 *mux_id = ctx->mux_id;
1954
1955 ifname = g_steal_pointer (&ctx->ifname);
1956 add_link_result_free (ctx);
1957 return ifname;
1958 }
1959
1960 gchar *
1961 qmi_device_add_link_finish (QmiDevice *self,
1962 GAsyncResult *res,
1963 guint *mux_id,
1964 GError **error)
1965 {
1966 return qmi_device_add_link_with_flags_finish (self, res, mux_id, error);
1967 }
1968
1969 gchar *
1970 qmi_device_add_link_with_flags_and_initial_mux_id_finish (QmiDevice *self,
1971 GAsyncResult *res,
1972 guint *mux_id,
1973 GError **error)
1974 {
1975 return qmi_device_add_link_with_flags_finish (self, res, mux_id, error);
1976 }
1977
1978 static void
1979 device_add_link_ready (QmiNetPortManager *net_port_manager,
1980 GAsyncResult *res,
1981 GTask *task)
1982 {
1983 GError *error = NULL;
1984 AddLinkResult *ctx;
1985
1986 ctx = g_new0 (AddLinkResult, 1);
1987 ctx->ifname = qmi_net_port_manager_add_link_finish (net_port_manager, &ctx->mux_id, res, &error);
1988
1989 if (!ctx->ifname) {
1990 g_prefix_error (&error, "Could not allocate link: ");
1991 g_task_return_error (task, error);
1992 add_link_result_free (ctx);
1993 } else
1994 g_task_return_pointer (task, ctx, (GDestroyNotify) add_link_result_free);
1995
1996 g_object_unref (task);
1997 }
1998
1999 void
2000 qmi_device_add_link_with_flags (QmiDevice *self,
2001 guint mux_id,
2002 const gchar *base_ifname,
2003 const gchar *ifname_prefix,
2004 QmiDeviceAddLinkFlags flags,
2005 GCancellable *cancellable,
2006 GAsyncReadyCallback callback,
2007 gpointer user_data)
2008 {
2009 GTask *task;
2010 GError *error = NULL;
2011
2012 g_return_if_fail (QMI_IS_DEVICE (self));
2013 g_return_if_fail (base_ifname);
2014 g_return_if_fail (mux_id >= QMI_DEVICE_MUX_ID_MIN);
2015 g_return_if_fail ((mux_id <= QMI_DEVICE_MUX_ID_MAX) || (mux_id == QMI_DEVICE_MUX_ID_AUTOMATIC));
2016
2017 task = g_task_new (self, cancellable, callback, user_data);
2018
2019 if (!setup_net_port_manager (self, &error)) {
2020 g_task_return_error (task, error);
2021 g_object_unref (task);
2022 return;
2023 }
2024
2025 g_assert (self->priv->net_port_manager);
2026 qmi_net_port_manager_add_link (self->priv->net_port_manager,
2027 mux_id,
2028 QMI_DEVICE_MUX_ID_MIN,
2029 base_ifname,
2030 ifname_prefix,
2031 flags,
2032 5,
2033 cancellable,
2034 (GAsyncReadyCallback) device_add_link_ready,
2035 task);
2036 }
2037
2038 void qmi_device_add_link_with_flags_and_initial_mux_id (QmiDevice *self,
2039 guint initial_mux_id,
2040 const gchar *base_ifname,
2041 const gchar *ifname_prefix,
2042 QmiDeviceAddLinkFlags flags,
2043 GCancellable *cancellable,
2044 GAsyncReadyCallback callback,
2045 gpointer user_data)
2046 {
2047 GTask *task;
2048 GError *error = NULL;
2049
2050 g_return_if_fail (QMI_IS_DEVICE (self));
2051 g_return_if_fail (base_ifname);
2052 g_return_if_fail (initial_mux_id >= QMI_DEVICE_MUX_ID_MIN);
2053
2054 task = g_task_new (self, cancellable, callback, user_data);
2055
2056 if (!setup_net_port_manager (self, &error)) {
2057 g_task_return_error (task, error);
2058 g_object_unref (task);
2059 return;
2060 }
2061
2062 g_assert (self->priv->net_port_manager);
2063 qmi_net_port_manager_add_link (self->priv->net_port_manager,
2064 QMI_DEVICE_MUX_ID_AUTOMATIC,
2065 initial_mux_id,
2066 base_ifname,
2067 ifname_prefix,
2068 flags,
2069 5,
2070 cancellable,
2071 (GAsyncReadyCallback) device_add_link_ready,
2072 task);
2073 }
2074
2075 void
2076 qmi_device_add_link (QmiDevice *self,
2077 guint mux_id,
2078 const gchar *base_ifname,
2079 const gchar *ifname_prefix,
2080 GCancellable *cancellable,
2081 GAsyncReadyCallback callback,
2082 gpointer user_data)
2083 {
2084 qmi_device_add_link_with_flags (self, mux_id, base_ifname, ifname_prefix,
2085 QMI_DEVICE_ADD_LINK_FLAGS_NONE,
2086 cancellable, callback, user_data);
2087 }
2088
2089 gboolean
2090 qmi_device_delete_link_finish (QmiDevice *self,
2091 GAsyncResult *res,
2092 GError **error)
2093 {
2094 return g_task_propagate_boolean (G_TASK (res), error);
2095 }
2096
2097 static void
2098 device_del_link_ready (QmiNetPortManager *net_port_manager,
2099 GAsyncResult *res,
2100 GTask *task)
2101 {
2102 GError *error = NULL;
2103
2104 if (!qmi_net_port_manager_del_link_finish (net_port_manager, res, &error))
2105 g_task_return_error (task, error);
2106 else
2107 g_task_return_boolean (task, TRUE);
2108 g_object_unref (task);
2109 }
2110
2111 void
2112 qmi_device_delete_link (QmiDevice *self,
2113 const gchar *ifname,
2114 guint mux_id,
2115 GCancellable *cancellable,
2116 GAsyncReadyCallback callback,
2117 gpointer user_data)
2118 {
2119 GTask *task;
2120 GError *error = NULL;
2121
2122 g_return_if_fail (QMI_IS_DEVICE (self));
2123 g_return_if_fail (ifname);
2124
2125 task = g_task_new (self, cancellable, callback, user_data);
2126
2127 if (!setup_net_port_manager (self, &error)) {
2128 g_task_return_error (task, error);
2129 g_object_unref (task);
2130 return;
2131 }
2132
2133 g_assert (self->priv->net_port_manager);
2134 qmi_net_port_manager_del_link (self->priv->net_port_manager,
2135 ifname,
2136 mux_id,
2137 5, /* timeout */
2138 cancellable,
2139 (GAsyncReadyCallback) device_del_link_ready,
2140 task);
2141 }
2142
2143 /*****************************************************************************/
2144
2145 gboolean
2146 qmi_device_delete_all_links_finish (QmiDevice *self,
2147 GAsyncResult *res,
2148 GError **error)
2149 {
2150 return g_task_propagate_boolean (G_TASK (res), error);
2151 }
2152
2153 static void
2154 device_del_all_links_ready (QmiNetPortManager *net_port_manager,
2155 GAsyncResult *res,
2156 GTask *task)
2157 {
2158 GError *error = NULL;
2159
2160 if (!qmi_net_port_manager_del_all_links_finish (net_port_manager, res, &error))
2161 g_task_return_error (task, error);
2162 else
2163 g_task_return_boolean (task, TRUE);
2164 g_object_unref (task);
2165 }
2166
2167 void
2168 qmi_device_delete_all_links (QmiDevice *self,
2169 const gchar *base_ifname,
2170 GCancellable *cancellable,
2171 GAsyncReadyCallback callback,
2172 gpointer user_data)
2173 {
2174 GTask *task;
2175 GError *error = NULL;
2176
2177 g_return_if_fail (QMI_IS_DEVICE (self));
2178 g_return_if_fail (base_ifname);
2179
2180 task = g_task_new (self, cancellable, callback, user_data);
2181
2182 if (!setup_net_port_manager (self, &error)) {
2183 g_task_return_error (task, error);
2184 g_object_unref (task);
2185 return;
2186 }
2187
2188 g_assert (self->priv->net_port_manager);
2189 qmi_net_port_manager_del_all_links (self->priv->net_port_manager,
2190 base_ifname,
2191 cancellable,
2192 (GAsyncReadyCallback) device_del_all_links_ready,
2193 task);
2194 }
2195
2196 /*****************************************************************************/
2197
2198 gboolean
2199 qmi_device_list_links (QmiDevice *self,
2200 const gchar *base_ifname,
2201 GPtrArray **out_links,
2202 GError **error)
2203 {
2204 g_return_val_if_fail (QMI_IS_DEVICE (self), FALSE);
2205 g_return_val_if_fail (base_ifname, FALSE);
2206
2207 if (!setup_net_port_manager (self, error))
2208 return FALSE;
2209
2210 g_assert (self->priv->net_port_manager);
2211 return qmi_net_port_manager_list_links (self->priv->net_port_manager,
2212 base_ifname,
2213 out_links,
2214 error);
2215 }
2216
2217 /*****************************************************************************/
2218
2219 gboolean
2220 qmi_device_check_link_supported (QmiDevice *self,
2221 GError **error)
2222 {
2223 g_return_val_if_fail (QMI_IS_DEVICE (self), FALSE);
2224
2225 /* if we can setup a net port manager, link management is supported */
2226 return setup_net_port_manager (self, error);
2227 }
2228
2229 /*****************************************************************************/
2230 /* Open device */
2231
2232 #define SYNC_TIMEOUT_SECS 2
2233
2234 typedef enum {
2235 DEVICE_OPEN_CONTEXT_STEP_FIRST = 0,
2236 DEVICE_OPEN_CONTEXT_STEP_DRIVER,
2237 DEVICE_OPEN_CONTEXT_STEP_CREATE_ENDPOINT,
2238 DEVICE_OPEN_CONTEXT_STEP_OPEN_ENDPOINT,
2239 DEVICE_OPEN_CONTEXT_STEP_FLAGS_VERSION_INFO,
2240 DEVICE_OPEN_CONTEXT_STEP_FLAGS_SYNC,
2241 DEVICE_OPEN_CONTEXT_STEP_FLAGS_NETPORT,
2242 DEVICE_OPEN_CONTEXT_STEP_FLAGS_EXPECT_INDICATIONS,
2243 DEVICE_OPEN_CONTEXT_STEP_LAST
2244 } DeviceOpenContextStep;
2245
2246 typedef struct {
2247 DeviceOpenContextStep step;
2248 QmiDeviceOpenFlags flags;
2249 guint timeout;
2250 guint version_check_retries;
2251 guint sync_retries;
2252 } DeviceOpenContext;
2253
2254 static void
2255 device_open_context_free (DeviceOpenContext *ctx)
2256 {
2257 g_slice_free (DeviceOpenContext, ctx);
2258 }
2259
2260 gboolean
2261 qmi_device_open_finish (QmiDevice *self,
2262 GAsyncResult *res,
2263 GError **error)
2264 {
2265 return g_task_propagate_boolean (G_TASK (res), error);
2266 }
2267
2268 static void device_open_step (GTask *task);
2269
2270 static void
2271 setup_indications_ready (QmiEndpoint *endpoint,
2272 GAsyncResult *res,
2273 GTask *task)
2274 {
2275 GError *error = NULL;
2276 DeviceOpenContext *ctx;
2277
2278 ctx = g_task_get_task_data (task);
2279
2280 if (!qmi_endpoint_setup_indications_finish (endpoint, res, &error)) {
2281 g_task_return_error (task, error);
2282 g_object_unref (task);
2283 return;
2284 }
2285
2286 ctx->step++;
2287 device_open_step (task);
2288 }
2289
2290 static void
2291 ctl_set_data_format_ready (QmiClientCtl *client,
2292 GAsyncResult *res,
2293 GTask *task)
2294 {
2295 QmiDevice *self;
2296 DeviceOpenContext *ctx;
2297 QmiMessageCtlSetDataFormatOutput *output = NULL;
2298 GError *error = NULL;
2299
2300 output = qmi_client_ctl_set_data_format_finish (client, res, &error);
2301 /* Check result of the async operation */
2302 if (!output) {
2303 g_task_return_error (task, error);
2304 g_object_unref (task);
2305 return;
2306 }
2307
2308 /* Check result of the QMI operation */
2309 if (!qmi_message_ctl_set_data_format_output_get_result (output, &error)) {
2310 g_task_return_error (task, error);
2311 g_object_unref (task);
2312 qmi_message_ctl_set_data_format_output_unref (output);
2313 return;
2314 }
2315
2316 self = g_task_get_source_object (task);
2317
2318 g_debug ("[%s] network port data format operation finished",
2319 qmi_file_get_path_display (self->priv->file));
2320
2321 qmi_message_ctl_set_data_format_output_unref (output);
2322
2323 /* Go on */
2324 ctx = g_task_get_task_data (task);
2325 ctx->step++;
2326 device_open_step (task);
2327 }
2328
2329 static void
2330 sync_ready (QmiClientCtl *client_ctl,
2331 GAsyncResult *res,
2332 GTask *task)
2333 {
2334 QmiDevice *self;
2335 DeviceOpenContext *ctx;
2336 GError *error = NULL;
2337 QmiMessageCtlSyncOutput *output;
2338
2339 self = g_task_get_source_object (task);
2340 ctx = g_task_get_task_data (task);
2341
2342 /* Check result of the async operation */
2343 output = qmi_client_ctl_sync_finish (client_ctl, res, &error);
2344 if (!output) {
2345 if (g_error_matches (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TIMEOUT)) {
2346 /* Update retries... */
2347 ctx->sync_retries--;
2348 /* If retries left, retry */
2349 if (ctx->sync_retries > 0) {
2350 g_error_free (error);
2351 qmi_client_ctl_sync (self->priv->client_ctl,
2352 NULL,
2353 SYNC_TIMEOUT_SECS,
2354 g_task_get_cancellable (task),
2355 (GAsyncReadyCallback)sync_ready,
2356 task);
2357 return;
2358 }
2359
2360 /* Otherwise, propagate the error */
2361 }
2362 g_task_return_error (task, error);
2363 g_object_unref (task);
2364 return;
2365 }
2366
2367 /* Check result of the QMI operation */
2368 if (!qmi_message_ctl_sync_output_get_result (output, &error)) {
2369 g_task_return_error (task, error);
2370 g_object_unref (task);
2371 qmi_message_ctl_sync_output_unref (output);
2372 return;
2373 }
2374
2375 g_debug ("[%s] sync operation finished",
2376 qmi_file_get_path_display (self->priv->file));
2377
2378 qmi_message_ctl_sync_output_unref (output);
2379
2380 /* Go on */
2381 ctx->step++;
2382 device_open_step (task);
2383 }
2384
2385 static void
2386 open_version_info_ready (QmiClientCtl *client_ctl,
2387 GAsyncResult *res,
2388 GTask *task)
2389 {
2390 QmiDevice *self;
2391 DeviceOpenContext *ctx;
2392 GArray *service_list;
2393 QmiMessageCtlGetVersionInfoOutput *output;
2394 GError *error = NULL;
2395 guint i;
2396
2397 self = g_task_get_source_object (task);
2398 ctx = g_task_get_task_data (task);
2399
2400 /* Check result of the async operation */
2401 output = qmi_client_ctl_get_version_info_finish (client_ctl, res, &error);
2402 if (!output) {
2403 if (g_error_matches (error, QMI_CORE_ERROR, QMI_CORE_ERROR_TIMEOUT)) {
2404 /* Update retries... */
2405 ctx->version_check_retries--;
2406 /* If retries left, retry */
2407 if (ctx->version_check_retries > 0) {
2408 g_error_free (error);
2409 qmi_client_ctl_get_version_info (self->priv->client_ctl,
2410 NULL,
2411 1,
2412 g_task_get_cancellable (task),
2413 (GAsyncReadyCallback)open_version_info_ready,
2414 task);
2415 return;
2416 }
2417
2418 /* Otherwise, propagate the error */
2419 }
2420
2421 g_task_return_error (task, error);
2422 g_object_unref (task);
2423 return;
2424 }
2425
2426 /* Check result of the QMI operation */
2427 if (!qmi_message_ctl_get_version_info_output_get_result (output, &error)) {
2428 g_task_return_error (task, error);
2429 g_object_unref (task);
2430 qmi_message_ctl_get_version_info_output_unref (output);
2431 return;
2432 }
2433
2434 /* QMI operation succeeded, we can now get the outputs */
2435 service_list = NULL;
2436 qmi_message_ctl_get_version_info_output_get_service_list (output,
2437 &service_list,
2438 NULL);
2439
2440 g_clear_pointer (&self->priv->supported_services, g_array_unref);
2441 self->priv->supported_services = g_array_ref (service_list);
2442
2443 g_debug ("[%s] device supports %u services:",
2444 qmi_file_get_path_display (self->priv->file),
2445 self->priv->supported_services->len);
2446 for (i = 0; i < self->priv->supported_services->len; i++) {
2447 QmiMessageCtlGetVersionInfoOutputServiceListService *info;
2448 const gchar *service_str;
2449
2450 info = &g_array_index (self->priv->supported_services,
2451 QmiMessageCtlGetVersionInfoOutputServiceListService,
2452 i);
2453 service_str = qmi_service_get_string (info->service);
2454 if (service_str)
2455 g_debug ("[%s] %s (%u.%u)",
2456 qmi_file_get_path_display (self->priv->file),
2457 service_str,
2458 info->major_version,
2459 info->minor_version);
2460 else
2461 g_debug ("[%s] unknown [0x%02x] (%u.%u)",
2462 qmi_file_get_path_display (self->priv->file),
2463 info->service,
2464 info->major_version,
2465 info->minor_version);
2466 }
2467
2468 qmi_message_ctl_get_version_info_output_unref (output);
2469
2470 /* Go on */
2471 ctx->step++;
2472 device_open_step (task);
2473 }
2474
2475 #if QMI_QRTR_SUPPORTED
2476
2477 static void
2478 build_services_from_qrtr_node (GTask *task)
2479 {
2480 QmiDevice *self;
2481 DeviceOpenContext *ctx;
2482 GList *services;
2483 guint n_services;
2484 GList *elem;
2485 QrtrNodeServiceInfo *qrtr_serv_info;
2486
2487 self = g_task_get_source_object (task);
2488 ctx = g_task_get_task_data (task);
2489
2490 g_assert (self->priv->node);
2491 services = qrtr_node_peek_service_info_list (self->priv->node);
2492 n_services = g_list_length (services);
2493
2494 g_clear_pointer (&self->priv->supported_services, g_array_unref);
2495 self->priv->supported_services = g_array_sized_new (FALSE,
2496 FALSE,
2497 sizeof (QmiMessageCtlGetVersionInfoOutputServiceListService),
2498 n_services);
2499
2500 g_debug ("[%s] device supports %u services:",
2501 qmi_file_get_path_display (self->priv->file),
2502 n_services);
2503
2504 for (elem = services; elem; elem = elem->next) {
2505 const gchar *service_str;
2506 QmiMessageCtlGetVersionInfoOutputServiceListService info;
2507
2508 qrtr_serv_info = elem->data;
2509 info.service = qrtr_node_service_info_get_service (qrtr_serv_info);
2510 info.major_version = qrtr_node_service_info_get_version (qrtr_serv_info);
2511 g_array_append_val (self->priv->supported_services, info);
2512
2513 service_str = qmi_service_get_string (info.service);
2514 if (service_str)
2515 g_debug ("[%s] %s (%u) ",
2516 qmi_file_get_path_display (self->priv->file),
2517 service_str,
2518 info.major_version);
2519 else
2520 g_debug ("[%s] unknown [0x%04x] (%u)",
2521 qmi_file_get_path_display (self->priv->file),
2522 info.service,
2523 info.major_version);
2524 }
2525
2526 ctx->step++;
2527 device_open_step (task);
2528 }
2529 #endif
2530
2531 static void
2532 endpoint_ready (QmiEndpoint *endpoint,
2533 GAsyncResult *res,
2534 GTask *task)
2535 {
2536 GError *error = NULL;
2537 DeviceOpenContext *ctx;
2538
2539 ctx = g_task_get_task_data (task);
2540
2541 if (!qmi_endpoint_open_finish (endpoint, res, &error)) {
2542 g_task_return_error (task, error);
2543 g_object_unref (task);
2544 return;
2545 }
2546
2547 ctx->step++;
2548 device_open_step (task);
2549 }
2550
2551 #define NETPORT_FLAGS (QMI_DEVICE_OPEN_FLAGS_NET_802_3 | \
2552 QMI_DEVICE_OPEN_FLAGS_NET_RAW_IP | \
2553 QMI_DEVICE_OPEN_FLAGS_NET_QOS_HEADER | \
2554 QMI_DEVICE_OPEN_FLAGS_NET_NO_QOS_HEADER)
2555
2556 static void
2557 device_create_endpoint (QmiDevice *self,
2558 DeviceOpenContext *ctx)
2559 {
2560 if (!(ctx->flags & QMI_DEVICE_OPEN_FLAGS_MBIM)) {
2561 #if QMI_QRTR_SUPPORTED
2562 /* We talk to proxies over QMUX even if they are proxying a QRTR device. */
2563 if (self->priv->node && !(ctx->flags & QMI_DEVICE_OPEN_FLAGS_PROXY)) {
2564 self->priv->endpoint = QMI_ENDPOINT (qmi_endpoint_qrtr_new (self->priv->node));
2565 } else
2566 #endif
2567 {
2568 self->priv->endpoint = QMI_ENDPOINT (qmi_endpoint_qmux_new (self->priv->file,
2569 self->priv->proxy_path,
2570 self->priv->client_ctl));
2571 }
2572 }
2573 #if defined MBIM_QMUX_ENABLED
2574 else {
2575 self->priv->endpoint = QMI_ENDPOINT (qmi_endpoint_mbim_new (self->priv->file));
2576 }
2577 #endif /* MBIM_QMUX_ENABLED */
2578
2579 if (!self->priv->endpoint)
2580 return;
2581
2582 self->priv->endpoint_new_data_id = g_signal_connect (self->priv->endpoint,
2583 QMI_ENDPOINT_SIGNAL_NEW_DATA,
2584 G_CALLBACK (endpoint_new_data_cb),
2585 self);
2586 self->priv->endpoint_hangup_id = g_signal_connect (self->priv->endpoint,
2587 QMI_ENDPOINT_SIGNAL_HANGUP,
2588 G_CALLBACK (endpoint_hangup_cb),
2589 self);
2590 g_debug ("[%s] created endpoint", qmi_file_get_path_display (self->priv->file));
2591 }
2592
2593 static gboolean
2594 device_setup_open_flags_by_transport (QmiDevice *self,
2595 DeviceOpenContext *ctx,
2596 GError **error)
2597 {
2598 QmiHelpersTransportType transport;
2599 GError *inner_error = NULL;
2600
2601 transport = qmi_helpers_get_transport_type (qmi_file_get_path (self->priv->file), &inner_error);
2602 if ((transport == QMI_HELPERS_TRANSPORT_TYPE_UNKNOWN) && !self->priv->no_file_check)
2603 g_warning ("[%s] couldn't detect transport type of port: %s",
2604 qmi_file_get_path_display (self->priv->file), inner_error->message);
2605 g_clear_error (&inner_error);
2606
2607 #if defined MBIM_QMUX_ENABLED
2608
2609 /* Auto mode requested? */
2610 if (ctx->flags & QMI_DEVICE_OPEN_FLAGS_AUTO) {
2611 switch (transport) {
2612 case QMI_HELPERS_TRANSPORT_TYPE_MBIM:
2613 g_debug ("[%s] automatically selecting MBIM mode", qmi_file_get_path_display (self->priv->file));
2614 ctx->flags |= QMI_DEVICE_OPEN_FLAGS_MBIM;
2615 break;
2616 case QMI_HELPERS_TRANSPORT_TYPE_QMUX:
2617 g_debug ("[%s] automatically selecting QMI mode", qmi_file_get_path_display (self->priv->file));
2618 ctx->flags &= ~QMI_DEVICE_OPEN_FLAGS_MBIM;
2619 break;
2620 case QMI_HELPERS_TRANSPORT_TYPE_UNKNOWN:
2621 g_set_error (&inner_error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED,
2622 "Cannot automatically select QMI/MBIM mode");
2623 break;
2624 default:
2625 g_assert_not_reached ();
2626 }
2627 goto out;
2628 }
2629
2630 /* MBIM mode requested? */
2631 if (ctx->flags & QMI_DEVICE_OPEN_FLAGS_MBIM) {
2632 if ((transport != QMI_HELPERS_TRANSPORT_TYPE_MBIM) && !self->priv->no_file_check)
2633 g_warning ("[%s] requested MBIM mode but unexpected transport type found", qmi_file_get_path_display (self->priv->file));
2634 goto out;
2635 }
2636
2637 #else
2638
2639 /* MBIM mode requested? */
2640 if (ctx->flags & QMI_DEVICE_OPEN_FLAGS_MBIM) {
2641 g_warning ("[%s] requested MBIM mode but no MBIM QMUX support available", qmi_file_get_path_display (self->priv->file));
2642 goto out;
2643 }
2644
2645 /* Treat AUTO as QMI mode, without warnings */
2646
2647 #endif /* MBIM_QMUX_ENABLED */
2648
2649 /* QMI mode requested? */
2650 if ((transport != QMI_HELPERS_TRANSPORT_TYPE_QMUX) && !self->priv->no_file_check)
2651 g_warning ("[%s] requested QMI mode but unexpected transport type found",
2652 qmi_file_get_path_display (self->priv->file));
2653
2654 out:
2655
2656 if (inner_error) {
2657 g_propagate_error (error, inner_error);
2658 return FALSE;
2659 }
2660
2661 return TRUE;
2662 }
2663
2664 static void
2665 device_open_step (GTask *task)
2666 {
2667 QmiDevice *self;
2668 DeviceOpenContext *ctx;
2669 GError *error = NULL;
2670
2671 self = g_task_get_source_object (task);
2672 ctx = g_task_get_task_data (task);
2673
2674 switch (ctx->step) {
2675 case DEVICE_OPEN_CONTEXT_STEP_FIRST:
2676 ctx->step++;
2677 /* Fall through */
2678
2679 case DEVICE_OPEN_CONTEXT_STEP_DRIVER:
2680 #if QMI_QRTR_SUPPORTED
2681 if (self->priv->node) {
2682 g_debug ("[%s] selecting QMI mode for QRTR endpoint",
2683 qmi_file_get_path_display (self->priv->file));
2684 ctx->flags &= ~QMI_DEVICE_OPEN_FLAGS_MBIM;
2685 } else
2686 #endif
2687 if (!device_setup_open_flags_by_transport (self, ctx, &error)) {
2688 g_task_return_error (task, error);
2689 g_object_unref (task);
2690 return;
2691 }
2692 ctx->step++;
2693 /* Fall through */
2694
2695 case DEVICE_OPEN_CONTEXT_STEP_CREATE_ENDPOINT:
2696 device_create_endpoint (self, ctx);
2697 if (!self->priv->endpoint) {
2698 g_task_return_new_error (task,
2699 QMI_CORE_ERROR,
2700 QMI_CORE_ERROR_FAILED,
2701 "Could not create endpoint");
2702 g_object_unref (task);
2703 return;
2704 }
2705 ctx->step++;
2706 /* Fall through */
2707
2708 case DEVICE_OPEN_CONTEXT_STEP_OPEN_ENDPOINT:
2709 qmi_endpoint_open (self->priv->endpoint,
2710 !!(ctx->flags & QMI_DEVICE_OPEN_FLAGS_PROXY),
2711 10,
2712 g_task_get_cancellable (task),
2713 (GAsyncReadyCallback)endpoint_ready,
2714 task);
2715 return;
2716
2717 case DEVICE_OPEN_CONTEXT_STEP_FLAGS_VERSION_INFO:
2718 /* Query version info? */
2719 if (ctx->flags & QMI_DEVICE_OPEN_FLAGS_VERSION_INFO) {
2720 /* Setup how many times to retry... We'll retry once per second */
2721 ctx->version_check_retries = ctx->timeout > 0 ? ctx->timeout : 1;
2722 g_debug ("[%s] checking version info (%u retries)...",
2723 qmi_file_get_path_display (self->priv->file),
2724 ctx->version_check_retries);
2725 #if QMI_QRTR_SUPPORTED
2726 if (self->priv->node) {
2727 g_debug ("[%s] QRTR does not support version info check: checking only for available services",
2728 qmi_file_get_path_display (self->priv->file));
2729 build_services_from_qrtr_node (task);
2730 }
2731 else
2732 #endif
2733 qmi_client_ctl_get_version_info (self->priv->client_ctl,
2734 NULL,
2735 1,
2736 g_task_get_cancellable (task),
2737 (GAsyncReadyCallback)open_version_info_ready,
2738 task);
2739
2740 return;
2741 }
2742 ctx->step++;
2743 /* Fall through */
2744
2745 case DEVICE_OPEN_CONTEXT_STEP_FLAGS_SYNC:
2746 /* Sync? */
2747 if (ctx->flags & QMI_DEVICE_OPEN_FLAGS_SYNC) {
2748 /* Setup how many times to retry... We'll retry once per second */
2749 ctx->sync_retries = ctx->timeout > SYNC_TIMEOUT_SECS ? (ctx->timeout / SYNC_TIMEOUT_SECS) : 1;
2750 g_debug ("[%s] running sync (%u retries)...",
2751 qmi_file_get_path_display (self->priv->file),
2752 ctx->sync_retries);
2753 qmi_client_ctl_sync (self->priv->client_ctl,
2754 NULL,
2755 SYNC_TIMEOUT_SECS,
2756 g_task_get_cancellable (task),
2757 (GAsyncReadyCallback)sync_ready,
2758 task);
2759 return;
2760 }
2761 ctx->step++;
2762 /* Fall through */
2763
2764 case DEVICE_OPEN_CONTEXT_STEP_FLAGS_NETPORT:
2765 /* Network port setup */
2766 if (ctx->flags & NETPORT_FLAGS) {
2767 QmiMessageCtlSetDataFormatInput *input;
2768 QmiCtlDataFormat qos = QMI_CTL_DATA_FORMAT_QOS_FLOW_HEADER_ABSENT;
2769 QmiCtlDataLinkProtocol link_protocol = QMI_CTL_DATA_LINK_PROTOCOL_802_3;
2770
2771 g_debug ("[%s] setting network port data format...",
2772 qmi_file_get_path_display (self->priv->file));
2773
2774 input = qmi_message_ctl_set_data_format_input_new ();
2775
2776 if (ctx->flags & QMI_DEVICE_OPEN_FLAGS_NET_QOS_HEADER)
2777 qos = QMI_CTL_DATA_FORMAT_QOS_FLOW_HEADER_PRESENT;
2778 qmi_message_ctl_set_data_format_input_set_format (input, qos, NULL);
2779
2780 if (ctx->flags & QMI_DEVICE_OPEN_FLAGS_NET_RAW_IP)
2781 link_protocol = QMI_CTL_DATA_LINK_PROTOCOL_RAW_IP;
2782 qmi_message_ctl_set_data_format_input_set_protocol (input, link_protocol, NULL);
2783
2784 qmi_client_ctl_set_data_format (self->priv->client_ctl,
2785 input,
2786 5,
2787 NULL,
2788 (GAsyncReadyCallback)ctl_set_data_format_ready,
2789 task);
2790 qmi_message_ctl_set_data_format_input_unref (input);
2791 return;
2792 }
2793 ctx->step++;
2794 /* Fall through */
2795
2796 case DEVICE_OPEN_CONTEXT_STEP_FLAGS_EXPECT_INDICATIONS:
2797 qmi_endpoint_setup_indications (self->priv->endpoint,
2798 10,
2799 g_task_get_cancellable (task),
2800 (GAsyncReadyCallback)setup_indications_ready,
2801 task);
2802 return;
2803 /* Fall through */
2804
2805 case DEVICE_OPEN_CONTEXT_STEP_LAST:
2806 /* Nothing else to process, done we are */
2807 g_task_return_boolean (task, TRUE);
2808 g_object_unref (task);
2809 return;
2810
2811 default:
2812 break;
2813 }
2814
2815 g_assert_not_reached ();
2816 }
2817
2818 void
2819 qmi_device_open (QmiDevice *self,
2820 QmiDeviceOpenFlags flags,
2821 guint timeout,
2822 GCancellable *cancellable,
2823 GAsyncReadyCallback callback,
2824 gpointer user_data)
2825 {
2826 DeviceOpenContext *ctx;
2827 gchar *flags_str;
2828 GTask *task;
2829
2830 /* Raw IP and 802.3 are mutually exclusive */
2831 g_return_if_fail (!((flags & QMI_DEVICE_OPEN_FLAGS_NET_802_3) &&
2832 (flags & QMI_DEVICE_OPEN_FLAGS_NET_RAW_IP)));
2833 /* QoS and no QoS are mutually exclusive */
2834 g_return_if_fail (!((flags & QMI_DEVICE_OPEN_FLAGS_NET_QOS_HEADER) &&
2835 (flags & QMI_DEVICE_OPEN_FLAGS_NET_NO_QOS_HEADER)));
2836 /* At least one of both link protocol and QoS must be specified */
2837 if (flags & (QMI_DEVICE_OPEN_FLAGS_NET_802_3 | QMI_DEVICE_OPEN_FLAGS_NET_RAW_IP))
2838 g_return_if_fail (flags & (QMI_DEVICE_OPEN_FLAGS_NET_QOS_HEADER | QMI_DEVICE_OPEN_FLAGS_NET_NO_QOS_HEADER));
2839 if (flags & (QMI_DEVICE_OPEN_FLAGS_NET_QOS_HEADER | QMI_DEVICE_OPEN_FLAGS_NET_NO_QOS_HEADER))
2840 g_return_if_fail (flags & (QMI_DEVICE_OPEN_FLAGS_NET_802_3 | QMI_DEVICE_OPEN_FLAGS_NET_RAW_IP));
2841
2842 g_return_if_fail (QMI_IS_DEVICE (self));
2843
2844 flags_str = qmi_device_open_flags_build_string_from_mask (flags);
2845 g_debug ("[%s] opening device with flags '%s'...",
2846 qmi_file_get_path_display (self->priv->file),
2847 flags_str);
2848 g_free (flags_str);
2849
2850 ctx = g_slice_new (DeviceOpenContext);
2851 ctx->step = DEVICE_OPEN_CONTEXT_STEP_FIRST;
2852 ctx->flags = flags;
2853 ctx->timeout = timeout;
2854
2855 task = g_task_new (self, cancellable, callback, user_data);
2856 g_task_set_task_data (task, ctx, (GDestroyNotify)device_open_context_free);
2857
2858 /* Start processing */
2859 device_open_step (task);
2860 }
2861
2862 /*****************************************************************************/
2863 /* Close stream */
2864
2865 typedef struct {
2866 QmiEndpoint *endpoint;
2867 guint endpoint_new_data_id;
2868 guint endpoint_hangup_id;
2869 } CloseContext;
2870
2871 static void
2872 close_context_free (CloseContext *ctx)
2873 {
2874 if (ctx->endpoint_hangup_id)
2875 g_signal_handler_disconnect (ctx->endpoint, ctx->endpoint_hangup_id);
2876 if (ctx->endpoint_new_data_id)
2877 g_signal_handler_disconnect (ctx->endpoint, ctx->endpoint_new_data_id);
2878 g_object_unref (ctx->endpoint);
2879 g_slice_free (CloseContext, ctx);
2880 }
2881
2882 gboolean
2883 qmi_device_close_finish (QmiDevice *self,
2884 GAsyncResult *res,
2885 GError **error)
2886 {
2887 return g_task_propagate_boolean (G_TASK (res), error);
2888 }
2889
2890 static void
2891 endpoint_close_ready (QmiEndpoint *endpoint,
2892 GAsyncResult *res,
2893 GTask *task)
2894 {
2895 GError *error = NULL;
2896
2897 if (!qmi_endpoint_close_finish (endpoint, res, &error))
2898 g_task_return_error (task, error);
2899 else
2900 g_task_return_boolean (task, TRUE);
2901 g_object_unref (task);
2902 }
2903
2904 void
2905 qmi_device_close_async (QmiDevice *self,
2906 guint timeout,
2907 GCancellable *cancellable,
2908 GAsyncReadyCallback callback,
2909 gpointer user_data)
2910 {
2911 GTask *task;
2912 CloseContext *ctx;
2913
2914 task = g_task_new (self, cancellable, callback, user_data);
2915
2916 /* if already closed, we're done */
2917 if (!self->priv->endpoint) {
2918 g_task_return_boolean (task, TRUE);
2919 g_object_unref (task);
2920 return;
2921 }
2922
2923 /* Steal endpoint setup from private info, it will be freed once
2924 * the task is completed and disposed */
2925 ctx = g_slice_new0 (CloseContext);
2926 ctx->endpoint = g_steal_pointer (&self->priv->endpoint);
2927 ctx->endpoint_new_data_id = self->priv->endpoint_new_data_id;
2928 self->priv->endpoint_new_data_id = 0;
2929 ctx->endpoint_hangup_id = self->priv->endpoint_hangup_id;
2930 self->priv->endpoint_hangup_id = 0;
2931 g_task_set_task_data (task, ctx, (GDestroyNotify) close_context_free);
2932
2933 qmi_endpoint_close (ctx->endpoint,
2934 timeout,
2935 cancellable,
2936 (GAsyncReadyCallback)endpoint_close_ready,
2937 task);
2938 }
2939
2940 /*****************************************************************************/
2941 /* Command */
2942
2943 QmiMessage *
2944 qmi_device_command_abortable_finish (QmiDevice *self,
2945 GAsyncResult *res,
2946 GError **error)
2947 {
2948 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error))
2949 return NULL;
2950
2951 return qmi_message_ref (g_simple_async_result_get_op_res_gpointer (
2952 G_SIMPLE_ASYNC_RESULT (res)));
2953 }
2954
2955 static void
2956 transaction_early_error (QmiDevice *self,
2957 Transaction *tr,
2958 gboolean stored,
2959 GError *error)
2960 {
2961 g_assert (error);
2962
2963 if (stored) {
2964 /* Match transaction so that we remove it from our tracking table */
2965 tr = device_match_transaction (self, tr->message);
2966 g_assert (tr);
2967 }
2968 transaction_complete_and_free (tr, NULL, error);
2969 g_error_free (error);
2970 }
2971
2972 void
2973 qmi_device_command_abortable (QmiDevice *self,
2974 QmiMessage *message,
2975 QmiMessageContext *message_context,
2976 guint timeout,
2977 QmiDeviceCommandAbortableBuildRequestFn abort_build_request_fn,
2978 QmiDeviceCommandAbortableParseResponseFn abort_parse_response_fn,
2979 gpointer abort_user_data,
2980 GDestroyNotify abort_user_data_free,
2981 GCancellable *cancellable,
2982 GAsyncReadyCallback callback,
2983 gpointer user_data)
2984 {
2985 GError *error = NULL;
2986 Transaction *tr;
2987
2988 g_return_if_fail (QMI_IS_DEVICE (self));
2989 g_return_if_fail (message != NULL);
2990 g_return_if_fail (timeout > 0);
2991
2992 /* either none or both set */
2993 g_return_if_fail ((!abort_build_request_fn && !abort_parse_response_fn) ||
2994 (abort_build_request_fn && abort_parse_response_fn));
2995
2996 /* Use a proper transaction id for CTL messages if they don't have one */
2997 if (qmi_message_get_service (message) == QMI_SERVICE_CTL &&
2998 qmi_message_get_transaction_id (message) == 0) {
2999 qmi_message_set_transaction_id (
3000 message,
3001 qmi_client_get_next_transaction_id (
3002 QMI_CLIENT (
3003 self->priv->client_ctl)));
3004 }
3005
3006 tr = transaction_new (self, message, message_context, cancellable, callback, user_data);
3007
3008 /* Device must be open */
3009 if (!qmi_device_is_open (self)) {
3010 error = g_error_new (QMI_CORE_ERROR,
3011 QMI_CORE_ERROR_WRONG_STATE,
3012 "Device must be open to send commands");
3013 transaction_early_error (self, tr, FALSE, error);
3014 return;
3015 }
3016
3017 /* Non-CTL services should use a proper CID */
3018 if (qmi_message_get_service (message) != QMI_SERVICE_CTL &&
3019 qmi_message_get_client_id (message) == 0) {
3020 error = g_error_new (QMI_CORE_ERROR,
3021 QMI_CORE_ERROR_FAILED,
3022 "Cannot send message in service '%s' without a CID",
3023 qmi_service_get_string (qmi_message_get_service (message)));
3024 transaction_early_error (self, tr, FALSE, error);
3025 return;
3026 }
3027
3028 /* If message is not abortable, we should not allow using the abortable() interface */
3029 if (!__qmi_message_is_abortable (message, message_context)) {
3030 if (abort_build_request_fn || abort_parse_response_fn) {
3031 error = g_error_new (QMI_CORE_ERROR,
3032 QMI_CORE_ERROR_FAILED,
3033 "Message is not abortable");
3034 transaction_early_error (self, tr, FALSE, error);
3035 return;
3036 }
3037 } else {
3038 /* Store abortable info, if any */
3039 tr->abort_build_request_fn = abort_build_request_fn;
3040 tr->abort_parse_response_fn = abort_parse_response_fn;
3041 tr->abort_user_data = abort_user_data;
3042 tr->abort_user_data_free = abort_user_data_free;
3043 }
3044
3045 /* Setup context to match response */
3046 if (!device_store_transaction (self, tr, timeout, &error)) {
3047 g_prefix_error (&error, "Cannot store transaction: ");
3048 transaction_early_error (self, tr, FALSE, error);
3049 return;
3050 }
3051
3052 /* From now on, if we want to complete the transaction with an early error,
3053 * it needs to be removed from the tracking table as well. */
3054
3055 trace_message (self, message, TRUE, "request", message_context);
3056
3057 if (!qmi_endpoint_send (self->priv->endpoint, message, timeout, cancellable, &error)) {
3058 transaction_early_error (self, tr, TRUE, error);
3059 return;
3060 }
3061 }
3062
3063 /*****************************************************************************/
3064 /* Non-abortable standard command */
3065
3066 QmiMessage *
3067 qmi_device_command_full_finish (QmiDevice *self,
3068 GAsyncResult *res,
3069 GError **error)
3070 {
3071 return qmi_device_command_abortable_finish (self, res, error);
3072 }
3073
3074 void
3075 qmi_device_command_full (QmiDevice *self,
3076 QmiMessage *message,
3077 QmiMessageContext *message_context,
3078 guint timeout,
3079 GCancellable *cancellable,
3080 GAsyncReadyCallback callback,
3081 gpointer user_data)
3082 {
3083 qmi_device_command_abortable (self,
3084 message,
3085 message_context,
3086 timeout,
3087 NULL, /* abort_build_request_fn */
3088 NULL, /* abort_parse_response_fn */
3089 NULL, /* abort_user_data */
3090 NULL, /* abort_user_data_free */
3091 cancellable,
3092 callback,
3093 user_data);
3094 }
3095
3096 /*****************************************************************************/
3097 /* New QMI device */
3098
3099 static QmiDevice *
3100 common_device_new_finish (GAsyncResult *res,
3101 GError **error)
3102 {
3103 g_autoptr(GObject) source_object = NULL;
3104
3105 source_object = g_async_result_get_source_object (res);
3106 return QMI_DEVICE (g_async_initable_new_finish (G_ASYNC_INITABLE (source_object), res, error));
3107 }
3108
3109 #if QMI_QRTR_SUPPORTED
3110
3111 QmiDevice *
3112 qmi_device_new_from_node_finish (GAsyncResult *res,
3113 GError **error)
3114 {
3115 return common_device_new_finish (res, error);
3116 }
3117
3118 void
3119 qmi_device_new_from_node (QrtrNode *node,
3120 GCancellable *cancellable,
3121 GAsyncReadyCallback callback,
3122 gpointer user_data)
3123 {
3124 g_return_if_fail (QRTR_IS_NODE (node));
3125
3126 g_async_initable_new_async (QMI_TYPE_DEVICE,
3127 G_PRIORITY_DEFAULT,
3128 cancellable,
3129 callback,
3130 user_data,
3131 QMI_DEVICE_NODE, node,
3132 NULL);
3133 }
3134
3135 QrtrNode *
3136 qmi_device_get_node (QmiDevice *self)
3137 {
3138 g_return_val_if_fail (QMI_IS_DEVICE (self), NULL);
3139
3140 return self->priv->node ? g_object_ref (self->priv->node) : NULL;
3141 }
3142
3143 QrtrNode *
3144 qmi_device_peek_node (QmiDevice *self)
3145 {
3146 g_return_val_if_fail (QMI_IS_DEVICE (self), NULL);
3147
3148 return self->priv->node;
3149 }
3150 #endif
3151
3152 QmiDevice *
3153 qmi_device_new_finish (GAsyncResult *res,
3154 GError **error)
3155 {
3156 return common_device_new_finish (res, error);
3157 }
3158
3159 void
3160 qmi_device_new (GFile *file,
3161 GCancellable *cancellable,
3162 GAsyncReadyCallback callback,
3163 gpointer user_data)
3164 {
3165 g_return_if_fail (G_IS_FILE (file));
3166
3167 g_async_initable_new_async (QMI_TYPE_DEVICE,
3168 G_PRIORITY_DEFAULT,
3169 cancellable,
3170 callback,
3171 user_data,
3172 QMI_DEVICE_FILE, file,
3173 NULL);
3174 }
3175
3176 /*****************************************************************************/
3177 /* Async init */
3178
3179 static gboolean
3180 initable_init_finish (GAsyncInitable *initable,
3181 GAsyncResult *result,
3182 GError **error)
3183 {
3184 return g_task_propagate_boolean (G_TASK (result), error);
3185 }
3186
3187 static void
3188 sync_indication_cb (QmiClientCtl *client_ctl,
3189 QmiDevice *self)
3190 {
3191 /* Just log about it */
3192 g_debug ("[%s] sync indication received",
3193 qmi_file_get_path_display (self->priv->file));
3194 }
3195
3196 static void
3197 client_ctl_setup (GTask *task)
3198 {
3199 QmiDevice *self;
3200 GError *error = NULL;
3201
3202 self = g_task_get_source_object (task);
3203
3204 /* Create the implicit CTL client */
3205 self->priv->client_ctl = g_object_new (QMI_TYPE_CLIENT_CTL,
3206 QMI_CLIENT_DEVICE, self,
3207 QMI_CLIENT_SERVICE, QMI_SERVICE_CTL,
3208 QMI_CLIENT_CID, QMI_CID_NONE,
3209 NULL);
3210
3211 /* Register the CTL client to get indications */
3212 register_client (self,
3213 QMI_CLIENT (self->priv->client_ctl),
3214 &error);
3215 g_assert_no_error (error);
3216
3217 /* Connect to 'Sync' indications */
3218 self->priv->sync_indication_id =
3219 g_signal_connect (self->priv->client_ctl,
3220 "sync",
3221 G_CALLBACK (sync_indication_cb),
3222 self);
3223
3224 /* Done we are */
3225 g_task_return_boolean (task, TRUE);
3226 g_object_unref (task);
3227 }
3228
3229 static void
3230 check_type_async_ready (QmiFile *file,
3231 GAsyncResult *res,
3232 GTask *task)
3233 {
3234 GError *error = NULL;
3235
3236 if (!qmi_file_check_type_finish (file, res, &error)) {
3237 g_task_return_error (task, error);
3238 g_object_unref (task);
3239 return;
3240 }
3241
3242 /* Go on with client CTL setup */
3243 client_ctl_setup (task);
3244 }
3245
3246 static void
3247 initable_init_async (GAsyncInitable *initable,
3248 int io_priority,
3249 GCancellable *cancellable,
3250 GAsyncReadyCallback callback,
3251 gpointer user_data)
3252 {
3253 QmiDevice *self;
3254 GTask *task;
3255
3256 self = QMI_DEVICE (initable);
3257 task = g_task_new (self, cancellable, callback, user_data);
3258
3259 /* We need a proper file to initialize */
3260 g_assert (QMI_IS_FILE (self->priv->file));
3261
3262 #if QMI_QRTR_SUPPORTED
3263 /* If we have a node, just skip to setting up the control client */
3264 if (self->priv->node) {
3265 client_ctl_setup (task);
3266 return;
3267 }
3268 #endif
3269
3270 /* If no file check requested, don't do it */
3271 if (self->priv->no_file_check) {
3272 client_ctl_setup (task);
3273 return;
3274 }
3275
3276 /* Check the file type. Note that this is just a quick check to avoid
3277 * creating QmiDevices pointing to a location already known not to be a QMI
3278 * device. */
3279 qmi_file_check_type_async (self->priv->file,
3280 cancellable,
3281 (GAsyncReadyCallback)check_type_async_ready,
3282 task);
3283 }
3284
3285 /*****************************************************************************/
3286
3287 #if QMI_QRTR_SUPPORTED
3288 static QmiFile *
3289 get_file_for_node (QrtrNode *node)
3290 {
3291 g_autofree gchar *uri = NULL;
3292
3293 uri = qrtr_get_uri_for_node (qrtr_node_get_id (node));
3294 return qmi_file_new (g_file_new_for_uri (uri));
3295 }
3296 #endif
3297
3298 static void
3299 set_property (GObject *object,
3300 guint prop_id,
3301 const GValue *value,
3302 GParamSpec *pspec)
3303 {
3304 QmiDevice *self = QMI_DEVICE (object);
3305
3306 switch (prop_id) {
3307 case PROP_FILE: {
3308 GFile *file;
3309
3310 file = g_value_get_object (value);
3311 g_assert (!self->priv->file);
3312 self->priv->file = file ? qmi_file_new (file) : NULL;
3313 break;
3314 }
3315 case PROP_NO_FILE_CHECK:
3316 self->priv->no_file_check = g_value_get_boolean (value);
3317 break;
3318 case PROP_PROXY_PATH:
3319 g_free (self->priv->proxy_path);
3320 self->priv->proxy_path = g_value_dup_string (value);
3321 break;
3322 case PROP_CONSECUTIVE_TIMEOUTS:
3323 g_assert_not_reached ();
3324 break;
3325 #if QMI_QRTR_SUPPORTED
3326 case PROP_NODE:
3327 g_assert (!self->priv->node);
3328 self->priv->node = g_value_dup_object (value);
3329 if (self->priv->node) {
3330 g_assert (!self->priv->file);
3331 self->priv->file = get_file_for_node (self->priv->node);
3332 }
3333 break;
3334 #endif
3335 default:
3336 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3337 break;
3338 }
3339 }
3340
3341 static void
3342 get_property (GObject *object,
3343 guint prop_id,
3344 GValue *value,
3345 GParamSpec *pspec)
3346 {
3347 QmiDevice *self = QMI_DEVICE (object);
3348
3349 switch (prop_id) {
3350 case PROP_FILE:
3351 g_assert (QMI_IS_FILE (self->priv->file));
3352 g_value_set_object (value, qmi_file_get_file (self->priv->file));
3353 break;
3354 case PROP_WWAN_IFACE:
3355 reload_wwan_iface_name (self);
3356 g_value_set_string (value, self->priv->wwan_iface);
3357 break;
3358 case PROP_CONSECUTIVE_TIMEOUTS:
3359 g_value_set_uint (value, self->priv->consecutive_timeouts);
3360 break;
3361 #if QMI_QRTR_SUPPORTED
3362 case PROP_NODE:
3363 g_value_set_object (value, self->priv->node);
3364 break;
3365 #endif
3366 default:
3367 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3368 break;
3369 }
3370 }
3371
3372 static void
3373 qmi_device_init (QmiDevice *self)
3374 {
3375 self->priv = G_TYPE_INSTANCE_GET_PRIVATE ((self),
3376 QMI_TYPE_DEVICE,
3377 QmiDevicePrivate);
3378
3379 self->priv->transactions = g_hash_table_new (g_direct_hash,
3380 g_direct_equal);
3381
3382 self->priv->registered_clients = g_hash_table_new_full (g_direct_hash,
3383 g_direct_equal,
3384 NULL,
3385 g_object_unref);
3386 self->priv->proxy_path = g_strdup (QMI_PROXY_SOCKET_PATH);
3387 }
3388
3389 static gboolean
3390 foreach_warning (gpointer key,
3391 QmiClient *client,
3392 QmiDevice *self)
3393 {
3394 g_warning ("[%s] client for service '%s' with CID '%u' wasn't released",
3395 qmi_file_get_path_display (self->priv->file),
3396 qmi_service_get_string (qmi_client_get_service (client)),
3397 qmi_client_get_cid (client));
3398
3399 return TRUE;
3400 }
3401
3402 static void
3403 dispose (GObject *object)
3404 {
3405 QmiDevice *self = QMI_DEVICE (object);
3406
3407 /* unregister our CTL client */
3408 if (self->priv->client_ctl)
3409 unregister_client (self, QMI_CLIENT (self->priv->client_ctl));
3410
3411 /* If clients were left unreleased, we'll just warn about it.
3412 * There is no point in trying to request CID releases, as the device
3413 * itself is being disposed. */
3414 g_hash_table_foreach_remove (self->priv->registered_clients,
3415 (GHRFunc)foreach_warning,
3416 self);
3417
3418 if (self->priv->sync_indication_id &&
3419 self->priv->client_ctl) {
3420 g_signal_handler_disconnect (self->priv->client_ctl,
3421 self->priv->sync_indication_id);
3422 self->priv->sync_indication_id = 0;
3423 }
3424 g_clear_object (&self->priv->client_ctl);
3425
3426 if (self->priv->endpoint) {
3427 if (self->priv->endpoint_hangup_id) {
3428 g_signal_handler_disconnect (self->priv->endpoint, self->priv->endpoint_hangup_id);
3429 self->priv->endpoint_hangup_id = 0;
3430 }
3431 if (self->priv->endpoint_new_data_id) {
3432 g_signal_handler_disconnect (self->priv->endpoint, self->priv->endpoint_new_data_id);
3433 self->priv->endpoint_new_data_id = 0;
3434 }
3435 g_clear_object (&self->priv->endpoint);
3436 }
3437
3438 g_clear_object (&self->priv->net_port_manager);
3439 g_clear_object (&self->priv->file);
3440
3441 #if QMI_QRTR_SUPPORTED
3442 g_clear_object (&self->priv->node);
3443 #endif
3444
3445 G_OBJECT_CLASS (qmi_device_parent_class)->dispose (object);
3446 }
3447
3448 static void
3449 finalize (GObject *object)
3450 {
3451 QmiDevice *self = QMI_DEVICE (object);
3452
3453 /* Transactions keep refs to the device, so it's actually
3454 * impossible to have any content in the HT */
3455 if (self->priv->transactions) {
3456 g_assert (g_hash_table_size (self->priv->transactions) == 0);
3457 g_hash_table_unref (self->priv->transactions);
3458 }
3459
3460 g_hash_table_unref (self->priv->registered_clients);
3461
3462 if (self->priv->supported_services)
3463 g_array_unref (self->priv->supported_services);
3464
3465 g_free (self->priv->proxy_path);
3466 g_free (self->priv->wwan_iface);
3467
3468 G_OBJECT_CLASS (qmi_device_parent_class)->finalize (object);
3469 }
3470
3471 static void
3472 async_initable_iface_init (GAsyncInitableIface *iface)
3473 {
3474 iface->init_async = initable_init_async;
3475 iface->init_finish = initable_init_finish;
3476 }
3477
3478 static void
3479 qmi_device_class_init (QmiDeviceClass *klass)
3480 {
3481 GObjectClass *object_class = G_OBJECT_CLASS (klass);
3482
3483 g_type_class_add_private (object_class, sizeof (QmiDevicePrivate));
3484
3485 object_class->get_property = get_property;
3486 object_class->set_property = set_property;
3487 object_class->finalize = finalize;
3488 object_class->dispose = dispose;
3489
3490 /**
3491 * QmiDevice:device-file:
3492 *
3493 * Since: 1.0
3494 */
3495 properties[PROP_FILE] =
3496 g_param_spec_object (QMI_DEVICE_FILE,
3497 "Device file",
3498 "File to the underlying QMI device",
3499 G_TYPE_FILE,
3500 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
3501 g_object_class_install_property (object_class, PROP_FILE, properties[PROP_FILE]);
3502
3503 /**
3504 * QmiDevice:device-no-file-check:
3505 *
3506 * Since: 1.12
3507 */
3508 properties[PROP_NO_FILE_CHECK] =
3509 g_param_spec_boolean (QMI_DEVICE_NO_FILE_CHECK,
3510 "No file check",
3511 "Don't check for file existence when creating the Qmi device.",
3512 FALSE,
3513 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY);
3514 g_object_class_install_property (object_class, PROP_NO_FILE_CHECK, properties[PROP_NO_FILE_CHECK]);
3515
3516 /**
3517 * QmiDevice:device-proxy-path:
3518 *
3519 * Since: 1.12
3520 */
3521 properties[PROP_PROXY_PATH] =
3522 g_param_spec_string (QMI_DEVICE_PROXY_PATH,
3523 "Proxy path",
3524 "Path of the abstract socket where the proxy is available.",
3525 QMI_PROXY_SOCKET_PATH,
3526 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY);
3527 g_object_class_install_property (object_class, PROP_PROXY_PATH, properties[PROP_PROXY_PATH]);
3528
3529 /**
3530 * QmiDevice:device-wwan-iface:
3531 *
3532 * Since: 1.14
3533 */
3534 properties[PROP_WWAN_IFACE] =
3535 g_param_spec_string (QMI_DEVICE_WWAN_IFACE,
3536 "WWAN iface",
3537 "Name of the WWAN network interface associated with the control port.",
3538 NULL,
3539 G_PARAM_READABLE);
3540 g_object_class_install_property (object_class, PROP_WWAN_IFACE, properties[PROP_WWAN_IFACE]);
3541
3542
3543 /**
3544 * QmiDevice:device-consecutive-timeouts:
3545 *
3546 * Since: 1.32
3547 */
3548 properties[PROP_CONSECUTIVE_TIMEOUTS] =
3549 g_param_spec_uint (QMI_DEVICE_CONSECUTIVE_TIMEOUTS,
3550 "Consecutive timeouts",
3551 "Number of consecutive timeouts detected in requests sent to the device",
3552 0, G_MAXUINT, 0,
3553 G_PARAM_READABLE);
3554 g_object_class_install_property (object_class, PROP_CONSECUTIVE_TIMEOUTS, properties[PROP_CONSECUTIVE_TIMEOUTS]);
3555
3556 /**
3557 * QmiDevice:device-node:
3558 *
3559 * Since: 1.24
3560 */
3561 #if QMI_QRTR_SUPPORTED
3562 properties[PROP_NODE] =
3563 g_param_spec_object (QMI_DEVICE_NODE,
3564 "QRTR node",
3565 "Remote node on the QRTR bus",
3566 QRTR_TYPE_NODE,
3567 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
3568 g_object_class_install_property (object_class, PROP_NODE, properties[PROP_NODE]);
3569 #endif
3570
3571 /**
3572 * QmiDevice::indication:
3573 * @object: A #QmiDevice.
3574 * @output: A #QmiMessage.
3575 *
3576 * The ::indication signal gets emitted when a QMI indication is received.
3577 *
3578 * Since: 1.8
3579 */
3580 signals[SIGNAL_INDICATION] =
3581 g_signal_new (QMI_DEVICE_SIGNAL_INDICATION,
3582 G_OBJECT_CLASS_TYPE (G_OBJECT_CLASS (klass)),
3583 G_SIGNAL_RUN_LAST,
3584 0,
3585 NULL,
3586 NULL,
3587 NULL,
3588 G_TYPE_NONE,
3589 1,
3590 G_TYPE_BYTE_ARRAY);
3591
3592 /**
3593 * QmiDevice::device-removed:
3594 * @object: A #QmiDevice.
3595 * @output: none
3596 *
3597 * The ::device-removed signal is emitted when an unexpected port hang-up is received.
3598 *
3599 * Since: 1.20
3600 */
3601 signals[SIGNAL_REMOVED] =
3602 g_signal_new (QMI_DEVICE_SIGNAL_REMOVED,
3603 G_OBJECT_CLASS_TYPE (G_OBJECT_CLASS (klass)),
3604 G_SIGNAL_RUN_LAST,
3605 0,
3606 NULL,
3607 NULL,
3608 NULL,
3609 G_TYPE_NONE,
3610 0);
3611 }