| 1 |
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* libqmi-glib -- GLib/GIO based library to control QMI devices |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library; if not, write to the |
| 17 |
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 18 |
* Boston, MA 02110-1301 USA. |
| 19 |
* |
| 20 |
* Copyright (C) 2012 Lanedo GmbH |
| 21 |
* Copyright (C) 2012-2017 Aleksander Morgado <[email protected]> |
| 22 |
* Copyright (C) 2019 Eric Caruso <[email protected]> |
| 23 |
*/ |
| 24 |
|
| 25 |
#include <errno.h> |
| 26 |
#include <fcntl.h> |
| 27 |
#include <gio/gio.h> |
| 28 |
#include <gio/gunixinputstream.h> |
| 29 |
#include <gio/gunixoutputstream.h> |
| 30 |
#include <gio/gunixsocketaddress.h> |
| 31 |
#include <string.h> |
| 32 |
#include <sys/stat.h> |
| 33 |
#include <sys/types.h> |
| 34 |
|
| 35 |
#include "qmi-endpoint-qmux.h" |
| 36 |
#include "qmi-ctl.h" |
| 37 |
#include "qmi-errors.h" |
| 38 |
#include "qmi-error-types.h" |
| 39 |
#include "qmi-helpers.h" |
| 40 |
|
| 41 |
G_DEFINE_TYPE (QmiEndpointQmux, qmi_endpoint_qmux, QMI_TYPE_ENDPOINT) |
| 42 |
|
| 43 |
struct _QmiEndpointQmuxPrivate { |
| 44 |
/* I/O streams */ |
| 45 |
gint fd; |
| 46 |
GInputStream *istream; |
| 47 |
GOutputStream *ostream; |
| 48 |
GSource *input_source; |
| 49 |
|
| 50 |
/* Proxy socket */ |
| 51 |
gchar *proxy_path; |
| 52 |
GSocketClient *socket_client; |
| 53 |
GSocketConnection *socket_connection; |
| 54 |
|
| 55 |
/* Control client */ |
| 56 |
QmiClientCtl *client_ctl; |
| 57 |
}; |
| 58 |
|
| 59 |
#define BUFFER_SIZE 2048 |
| 60 |
#define MAX_SPAWN_RETRIES 10 |
| 61 |
|
| 62 |
static void destroy_iostream (QmiEndpointQmux *self); |
| 63 |
|
| 64 |
/*****************************************************************************/ |
| 65 |
|
| 66 |
static gboolean |
| 67 |
input_ready_cb (GInputStream *istream, |
| 68 |
QmiEndpointQmux *self) |
| 69 |
{ |
| 70 |
guint8 buffer[BUFFER_SIZE]; |
| 71 |
GError *error = NULL; |
| 72 |
gssize r; |
| 73 |
|
| 74 |
r = g_pollable_input_stream_read_nonblocking (G_POLLABLE_INPUT_STREAM (istream), |
| 75 |
buffer, |
| 76 |
BUFFER_SIZE, |
| 77 |
NULL, |
| 78 |
&error); |
| 79 |
if (r < 0) { |
| 80 |
g_warning ("Error reading from istream: %s", error ? error->message : "unknown"); |
| 81 |
if (error) |
| 82 |
g_error_free (error); |
| 83 |
/* Hang up the endpoint */ |
| 84 |
g_signal_emit_by_name (QMI_ENDPOINT (self), QMI_ENDPOINT_SIGNAL_HANGUP); |
| 85 |
return G_SOURCE_REMOVE; |
| 86 |
} |
| 87 |
|
| 88 |
if (r == 0) { |
| 89 |
/* HUP! */ |
| 90 |
g_warning ("Cannot read from istream: connection broken"); |
| 91 |
g_signal_emit_by_name (QMI_ENDPOINT (self), QMI_ENDPOINT_SIGNAL_HANGUP); |
| 92 |
return G_SOURCE_REMOVE; |
| 93 |
} |
| 94 |
|
| 95 |
/* else, r > 0 */ |
| 96 |
qmi_endpoint_add_message (QMI_ENDPOINT (self), buffer, r); |
| 97 |
return G_SOURCE_CONTINUE; |
| 98 |
} |
| 99 |
|
| 100 |
/*****************************************************************************/ |
| 101 |
|
| 102 |
typedef struct { |
| 103 |
gboolean use_proxy; |
| 104 |
guint spawn_retries; |
| 105 |
} QmuxDeviceOpenContext; |
| 106 |
|
| 107 |
static void |
| 108 |
qmux_device_open_context_free (QmuxDeviceOpenContext *ctx) |
| 109 |
{ |
| 110 |
g_slice_free (QmuxDeviceOpenContext, ctx); |
| 111 |
} |
| 112 |
|
| 113 |
static gboolean |
| 114 |
endpoint_open_finish (QmiEndpoint *self, |
| 115 |
GAsyncResult *res, |
| 116 |
GError **error) |
| 117 |
{ |
| 118 |
return g_task_propagate_boolean (G_TASK (res), error); |
| 119 |
} |
| 120 |
|
| 121 |
static void |
| 122 |
internal_proxy_open_ready (QmiClientCtl *client_ctl, |
| 123 |
GAsyncResult *res, |
| 124 |
GTask *task) |
| 125 |
{ |
| 126 |
QmiMessageCtlInternalProxyOpenOutput *output; |
| 127 |
GError *error = NULL; |
| 128 |
|
| 129 |
/* Check result of the async operation */ |
| 130 |
output = qmi_client_ctl_internal_proxy_open_finish (client_ctl, res, &error); |
| 131 |
if (!output) { |
| 132 |
g_task_return_error (task, error); |
| 133 |
g_object_unref (task); |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
/* Check result of the QMI operation */ |
| 138 |
if (!qmi_message_ctl_internal_proxy_open_output_get_result (output, &error)) { |
| 139 |
g_task_return_error (task, error); |
| 140 |
g_object_unref (task); |
| 141 |
qmi_message_ctl_internal_proxy_open_output_unref (output); |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
qmi_message_ctl_internal_proxy_open_output_unref (output); |
| 146 |
g_task_return_boolean (task, TRUE); |
| 147 |
g_object_unref (task); |
| 148 |
} |
| 149 |
|
| 150 |
static void |
| 151 |
setup_proxy (GTask *task) |
| 152 |
{ |
| 153 |
QmiEndpointQmux *self; |
| 154 |
QmiMessageCtlInternalProxyOpenInput *input; |
| 155 |
QmiFile *file; |
| 156 |
|
| 157 |
self = g_task_get_source_object (task); |
| 158 |
|
| 159 |
g_object_get (self, QMI_ENDPOINT_FILE, &file, NULL); |
| 160 |
input = qmi_message_ctl_internal_proxy_open_input_new (); |
| 161 |
qmi_message_ctl_internal_proxy_open_input_set_device_path (input, qmi_file_get_path (file), NULL); |
| 162 |
qmi_client_ctl_internal_proxy_open (self->priv->client_ctl, |
| 163 |
input, |
| 164 |
5, |
| 165 |
g_task_get_cancellable (task), |
| 166 |
(GAsyncReadyCallback)internal_proxy_open_ready, |
| 167 |
task); |
| 168 |
qmi_message_ctl_internal_proxy_open_input_unref (input); |
| 169 |
g_object_unref (file); |
| 170 |
} |
| 171 |
|
| 172 |
static void |
| 173 |
setup_iostream (GTask *task) |
| 174 |
{ |
| 175 |
QmiEndpointQmux *self; |
| 176 |
QmuxDeviceOpenContext *ctx; |
| 177 |
|
| 178 |
self = g_task_get_source_object (task); |
| 179 |
ctx = g_task_get_task_data (task); |
| 180 |
|
| 181 |
/* Check in/out streams */ |
| 182 |
if (!self->priv->istream || !self->priv->ostream) { |
| 183 |
destroy_iostream (self); |
| 184 |
g_task_return_new_error (task, |
| 185 |
QMI_CORE_ERROR, |
| 186 |
QMI_CORE_ERROR_FAILED, |
| 187 |
"Cannot get input/output streams"); |
| 188 |
g_object_unref (task); |
| 189 |
return; |
| 190 |
} |
| 191 |
|
| 192 |
/* Setup input events */ |
| 193 |
self->priv->input_source = g_pollable_input_stream_create_source ( |
| 194 |
G_POLLABLE_INPUT_STREAM (self->priv->istream), |
| 195 |
NULL); |
| 196 |
g_source_set_callback (self->priv->input_source, |
| 197 |
(GSourceFunc)input_ready_cb, |
| 198 |
self, |
| 199 |
NULL); |
| 200 |
g_source_attach (self->priv->input_source, g_main_context_get_thread_default ()); |
| 201 |
|
| 202 |
if (!ctx->use_proxy) { |
| 203 |
/* We're done here */ |
| 204 |
g_task_return_boolean (task, TRUE); |
| 205 |
g_object_unref (task); |
| 206 |
return; |
| 207 |
} |
| 208 |
|
| 209 |
setup_proxy (task); |
| 210 |
} |
| 211 |
|
| 212 |
static void |
| 213 |
create_iostream_with_fd (GTask *task) |
| 214 |
{ |
| 215 |
QmiEndpointQmux *self; |
| 216 |
QmiFile *file; |
| 217 |
gint fd; |
| 218 |
|
| 219 |
self = g_task_get_source_object (task); |
| 220 |
g_object_get (self, QMI_ENDPOINT_FILE, &file, NULL); |
| 221 |
fd = open (qmi_file_get_path (file), O_RDWR | O_EXCL | O_NONBLOCK | O_NOCTTY); |
| 222 |
|
| 223 |
if (fd < 0) { |
| 224 |
g_task_return_new_error (task, |
| 225 |
QMI_CORE_ERROR, |
| 226 |
QMI_CORE_ERROR_FAILED, |
| 227 |
"Cannot open device file '%s': %s", |
| 228 |
qmi_file_get_path_display (file), |
| 229 |
strerror (errno)); |
| 230 |
g_object_unref (file); |
| 231 |
g_object_unref (task); |
| 232 |
return; |
| 233 |
} |
| 234 |
|
| 235 |
g_object_unref (file); |
| 236 |
g_assert (self->priv->fd < 0); |
| 237 |
self->priv->fd = fd; |
| 238 |
self->priv->istream = g_unix_input_stream_new (fd, FALSE); |
| 239 |
self->priv->ostream = g_unix_output_stream_new (fd, FALSE); |
| 240 |
|
| 241 |
setup_iostream (task); |
| 242 |
} |
| 243 |
|
| 244 |
static void create_iostream_with_socket (GTask *task); |
| 245 |
|
| 246 |
static gboolean |
| 247 |
wait_for_proxy_cb (GTask *task) |
| 248 |
{ |
| 249 |
create_iostream_with_socket (task); |
| 250 |
return FALSE; |
| 251 |
} |
| 252 |
|
| 253 |
static void |
| 254 |
spawn_child_setup (void) |
| 255 |
{ |
| 256 |
if (setpgid (0, 0) < 0) |
| 257 |
g_warning ("couldn't setup proxy specific process group"); |
| 258 |
} |
| 259 |
|
| 260 |
static void |
| 261 |
create_iostream_with_socket (GTask *task) |
| 262 |
{ |
| 263 |
QmiEndpointQmux *self; |
| 264 |
QmuxDeviceOpenContext *ctx; |
| 265 |
GSocketAddress *socket_address; |
| 266 |
GError *error = NULL; |
| 267 |
|
| 268 |
self = g_task_get_source_object (task); |
| 269 |
ctx = g_task_get_task_data (task); |
| 270 |
|
| 271 |
/* Create socket client */ |
| 272 |
self->priv->socket_client = g_socket_client_new (); |
| 273 |
g_socket_client_set_family (self->priv->socket_client, G_SOCKET_FAMILY_UNIX); |
| 274 |
g_socket_client_set_socket_type (self->priv->socket_client, G_SOCKET_TYPE_STREAM); |
| 275 |
g_socket_client_set_protocol (self->priv->socket_client, G_SOCKET_PROTOCOL_DEFAULT); |
| 276 |
|
| 277 |
/* Setup socket address */ |
| 278 |
socket_address = g_unix_socket_address_new_with_type ( |
| 279 |
self->priv->proxy_path, |
| 280 |
-1, |
| 281 |
G_UNIX_SOCKET_ADDRESS_ABSTRACT); |
| 282 |
|
| 283 |
/* Connect to address */ |
| 284 |
self->priv->socket_connection = g_socket_client_connect ( |
| 285 |
self->priv->socket_client, |
| 286 |
G_SOCKET_CONNECTABLE (socket_address), |
| 287 |
NULL, |
| 288 |
&error); |
| 289 |
g_object_unref (socket_address); |
| 290 |
|
| 291 |
if (!self->priv->socket_connection) { |
| 292 |
gchar **argc; |
| 293 |
GSource *source; |
| 294 |
|
| 295 |
g_debug ("cannot connect to proxy: %s", error->message); |
| 296 |
g_clear_error (&error); |
| 297 |
g_clear_object (&self->priv->socket_client); |
| 298 |
|
| 299 |
/* Don't retry forever */ |
| 300 |
ctx->spawn_retries++; |
| 301 |
if (ctx->spawn_retries > MAX_SPAWN_RETRIES) { |
| 302 |
g_task_return_new_error (task, |
| 303 |
QMI_CORE_ERROR, |
| 304 |
QMI_CORE_ERROR_FAILED, |
| 305 |
"Couldn't spawn the qmi-proxy"); |
| 306 |
g_object_unref (task); |
| 307 |
return; |
| 308 |
} |
| 309 |
|
| 310 |
g_debug ("spawning new qmi-proxy (try %u)...", ctx->spawn_retries); |
| 311 |
|
| 312 |
argc = g_new0 (gchar *, 2); |
| 313 |
argc[0] = g_strdup (LIBEXEC_PATH "/qmi-proxy"); |
| 314 |
if (!g_spawn_async (NULL, /* working directory */ |
| 315 |
argc, |
| 316 |
NULL, /* envp */ |
| 317 |
G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL, |
| 318 |
(GSpawnChildSetupFunc) spawn_child_setup, |
| 319 |
NULL, /* child_setup_user_data */ |
| 320 |
NULL, |
| 321 |
&error)) { |
| 322 |
g_debug ("error spawning qmi-proxy: %s", error->message); |
| 323 |
g_clear_error (&error); |
| 324 |
} |
| 325 |
g_strfreev (argc); |
| 326 |
|
| 327 |
/* Wait some ms and retry */ |
| 328 |
source = g_timeout_source_new (100); |
| 329 |
g_source_set_callback (source, (GSourceFunc)wait_for_proxy_cb, task, NULL); |
| 330 |
g_source_attach (source, g_main_context_get_thread_default ()); |
| 331 |
g_source_unref (source); |
| 332 |
return; |
| 333 |
} |
| 334 |
|
| 335 |
self->priv->istream = g_io_stream_get_input_stream (G_IO_STREAM (self->priv->socket_connection)); |
| 336 |
if (self->priv->istream) |
| 337 |
g_object_ref (self->priv->istream); |
| 338 |
|
| 339 |
self->priv->ostream = g_io_stream_get_output_stream (G_IO_STREAM (self->priv->socket_connection)); |
| 340 |
if (self->priv->ostream) |
| 341 |
g_object_ref (self->priv->ostream); |
| 342 |
|
| 343 |
setup_iostream (task); |
| 344 |
} |
| 345 |
|
| 346 |
static void |
| 347 |
create_iostream_with_socket_direct (GTask *task) |
| 348 |
{ |
| 349 |
QmiEndpointQmux *self; |
| 350 |
QmiFile *file; |
| 351 |
GSocketAddress *socket_address; |
| 352 |
GError *error = NULL; |
| 353 |
|
| 354 |
self = g_task_get_source_object (task); |
| 355 |
|
| 356 |
/* Create socket client */ |
| 357 |
self->priv->socket_client = g_socket_client_new (); |
| 358 |
g_socket_client_set_family (self->priv->socket_client, G_SOCKET_FAMILY_UNIX); |
| 359 |
g_socket_client_set_socket_type (self->priv->socket_client, G_SOCKET_TYPE_STREAM); |
| 360 |
g_socket_client_set_protocol (self->priv->socket_client, G_SOCKET_PROTOCOL_DEFAULT); |
| 361 |
|
| 362 |
/* Setup socket address (using a system path) */ |
| 363 |
g_object_get (self, QMI_ENDPOINT_FILE, &file, NULL); |
| 364 |
socket_address = g_unix_socket_address_new_with_type ( |
| 365 |
qmi_file_get_path (file), |
| 366 |
-1, |
| 367 |
G_UNIX_SOCKET_ADDRESS_PATH); |
| 368 |
|
| 369 |
/* Connect to address */ |
| 370 |
self->priv->socket_connection = g_socket_client_connect ( |
| 371 |
self->priv->socket_client, |
| 372 |
G_SOCKET_CONNECTABLE (socket_address), |
| 373 |
NULL, |
| 374 |
&error); |
| 375 |
g_object_unref (socket_address); |
| 376 |
|
| 377 |
/* Display an error if the socket is not reachable */ |
| 378 |
if (!self->priv->socket_connection) { |
| 379 |
g_task_return_new_error (task, |
| 380 |
QMI_CORE_ERROR, |
| 381 |
QMI_CORE_ERROR_FAILED, |
| 382 |
"Cannot connect to the socket '%s': '%s'", |
| 383 |
qmi_file_get_path (file), |
| 384 |
error->message); |
| 385 |
g_object_unref (file); |
| 386 |
g_object_unref (task); |
| 387 |
return; |
| 388 |
} |
| 389 |
|
| 390 |
g_object_unref (file); |
| 391 |
|
| 392 |
/* Use the input and output streams of the socket */ |
| 393 |
self->priv->istream = g_io_stream_get_input_stream (G_IO_STREAM (self->priv->socket_connection)); |
| 394 |
if (self->priv->istream) |
| 395 |
g_object_ref (self->priv->istream); |
| 396 |
|
| 397 |
self->priv->ostream = g_io_stream_get_output_stream (G_IO_STREAM (self->priv->socket_connection)); |
| 398 |
if (self->priv->ostream) |
| 399 |
g_object_ref (self->priv->ostream); |
| 400 |
|
| 401 |
setup_iostream (task); |
| 402 |
} |
| 403 |
|
| 404 |
static void |
| 405 |
endpoint_open (QmiEndpoint *endpoint, |
| 406 |
gboolean use_proxy, |
| 407 |
guint timeout, |
| 408 |
GCancellable *cancellable, |
| 409 |
GAsyncReadyCallback callback, |
| 410 |
gpointer user_data) |
| 411 |
{ |
| 412 |
QmiEndpointQmux *self; |
| 413 |
QmuxDeviceOpenContext *ctx; |
| 414 |
QmiFile *file; |
| 415 |
GTask *task; |
| 416 |
|
| 417 |
ctx = g_slice_new (QmuxDeviceOpenContext); |
| 418 |
ctx->use_proxy = use_proxy; |
| 419 |
ctx->spawn_retries = 0; |
| 420 |
|
| 421 |
self = QMI_ENDPOINT_QMUX (endpoint); |
| 422 |
task = g_task_new (self, NULL, callback, user_data); |
| 423 |
g_task_set_task_data (task, |
| 424 |
ctx, |
| 425 |
(GDestroyNotify)qmux_device_open_context_free); |
| 426 |
|
| 427 |
if (self->priv->istream || self->priv->ostream) { |
| 428 |
g_task_return_new_error (task, |
| 429 |
QMI_CORE_ERROR, |
| 430 |
QMI_CORE_ERROR_WRONG_STATE, |
| 431 |
"Already open"); |
| 432 |
g_object_unref (task); |
| 433 |
return; |
| 434 |
} |
| 435 |
|
| 436 |
/* Get the file name to determine whether a unix domain socket should be used instead of a device file */ |
| 437 |
g_object_get (self, QMI_ENDPOINT_FILE, &file, NULL); |
| 438 |
|
| 439 |
if (use_proxy) |
| 440 |
create_iostream_with_socket (task); |
| 441 |
else if (g_strrstr (qmi_file_get_path (file), QMI_QMUX_SOCKET_FILE_NAME)) |
| 442 |
create_iostream_with_socket_direct (task); |
| 443 |
else |
| 444 |
create_iostream_with_fd (task); |
| 445 |
|
| 446 |
g_object_unref (file); |
| 447 |
} |
| 448 |
|
| 449 |
/*****************************************************************************/ |
| 450 |
|
| 451 |
static gboolean |
| 452 |
endpoint_is_open (QmiEndpoint *self) |
| 453 |
{ |
| 454 |
return !!(QMI_ENDPOINT_QMUX (self)->priv->istream || |
| 455 |
QMI_ENDPOINT_QMUX (self)->priv->ostream); |
| 456 |
} |
| 457 |
|
| 458 |
static gboolean |
| 459 |
endpoint_send (QmiEndpoint *self, |
| 460 |
QmiMessage *message, |
| 461 |
guint timeout, |
| 462 |
GCancellable *cancellable, |
| 463 |
GError **error) |
| 464 |
{ |
| 465 |
gconstpointer raw_message; |
| 466 |
gsize raw_message_len; |
| 467 |
GError *inner_error = NULL; |
| 468 |
|
| 469 |
/* QMUX endpoint allows only QMUX messages */ |
| 470 |
if (qmi_message_get_marker (message) != QMI_MESSAGE_QMUX_MARKER) { |
| 471 |
g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED, |
| 472 |
"QMUX endpoint expects only QMUX messages"); |
| 473 |
return FALSE; |
| 474 |
} |
| 475 |
|
| 476 |
/* Disallow private CTL operations */ |
| 477 |
if ((qmi_message_get_service (message) == QMI_SERVICE_CTL) && |
| 478 |
(qmi_message_get_message_id (message) == QMI_MESSAGE_CTL_INTERNAL_ALLOCATE_CID_QRTR || |
| 479 |
qmi_message_get_message_id (message) == QMI_MESSAGE_CTL_INTERNAL_RELEASE_CID_QRTR)) { |
| 480 |
g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED, |
| 481 |
"QMUX endpoint expects only 8bit QMI services"); |
| 482 |
return FALSE; |
| 483 |
} |
| 484 |
|
| 485 |
/* Get raw message */ |
| 486 |
raw_message = qmi_message_get_raw (message, &raw_message_len, &inner_error); |
| 487 |
if (!raw_message) { |
| 488 |
g_propagate_prefixed_error (error, inner_error, "Cannot get raw message: "); |
| 489 |
return FALSE; |
| 490 |
} |
| 491 |
|
| 492 |
if (!g_output_stream_write_all (QMI_ENDPOINT_QMUX (self)->priv->ostream, |
| 493 |
raw_message, |
| 494 |
raw_message_len, |
| 495 |
NULL, /* bytes_written */ |
| 496 |
NULL, /* cancellable */ |
| 497 |
&inner_error)) { |
| 498 |
g_propagate_prefixed_error (error, inner_error, "Cannot write message: "); |
| 499 |
return FALSE; |
| 500 |
} |
| 501 |
|
| 502 |
/* Flush explicitly if correctly written */ |
| 503 |
g_output_stream_flush (QMI_ENDPOINT_QMUX (self)->priv->ostream, NULL, NULL); |
| 504 |
return TRUE; |
| 505 |
} |
| 506 |
|
| 507 |
/*****************************************************************************/ |
| 508 |
|
| 509 |
static gboolean |
| 510 |
endpoint_close_finish (QmiEndpoint *self, |
| 511 |
GAsyncResult *res, |
| 512 |
GError **error) |
| 513 |
{ |
| 514 |
return g_task_propagate_boolean (G_TASK (res), error); |
| 515 |
} |
| 516 |
|
| 517 |
static void |
| 518 |
destroy_iostream (QmiEndpointQmux *self) |
| 519 |
{ |
| 520 |
if (self->priv->input_source) { |
| 521 |
g_source_destroy (self->priv->input_source); |
| 522 |
g_clear_pointer (&self->priv->input_source, g_source_unref); |
| 523 |
} |
| 524 |
g_clear_object (&self->priv->istream); |
| 525 |
g_clear_object (&self->priv->ostream); |
| 526 |
g_clear_object (&self->priv->socket_connection); |
| 527 |
g_clear_object (&self->priv->socket_client); |
| 528 |
if (self->priv->fd >= 0) { |
| 529 |
close (self->priv->fd); |
| 530 |
self->priv->fd = -1; |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
static void |
| 535 |
endpoint_close (QmiEndpoint *endpoint, |
| 536 |
guint timeout, |
| 537 |
GCancellable *cancellable, |
| 538 |
GAsyncReadyCallback callback, |
| 539 |
gpointer user_data) |
| 540 |
{ |
| 541 |
QmiEndpointQmux *self; |
| 542 |
GTask *task; |
| 543 |
|
| 544 |
self = QMI_ENDPOINT_QMUX (endpoint); |
| 545 |
task = g_task_new (self, cancellable, callback, user_data); |
| 546 |
|
| 547 |
destroy_iostream (self); |
| 548 |
|
| 549 |
g_task_return_boolean (task, TRUE); |
| 550 |
g_object_unref (task); |
| 551 |
} |
| 552 |
|
| 553 |
/*****************************************************************************/ |
| 554 |
|
| 555 |
QmiEndpointQmux * |
| 556 |
qmi_endpoint_qmux_new (QmiFile *file, |
| 557 |
const gchar *proxy_path, |
| 558 |
QmiClientCtl *client_ctl) |
| 559 |
{ |
| 560 |
QmiEndpointQmux *self; |
| 561 |
|
| 562 |
if (!file) |
| 563 |
return NULL; |
| 564 |
|
| 565 |
self = g_object_new (QMI_TYPE_ENDPOINT_QMUX, |
| 566 |
QMI_ENDPOINT_FILE, file, |
| 567 |
NULL); |
| 568 |
self->priv->proxy_path = g_strdup (proxy_path); |
| 569 |
self->priv->client_ctl = g_object_ref (client_ctl); |
| 570 |
return self; |
| 571 |
} |
| 572 |
|
| 573 |
/*****************************************************************************/ |
| 574 |
|
| 575 |
static void |
| 576 |
qmi_endpoint_qmux_init (QmiEndpointQmux *self) |
| 577 |
{ |
| 578 |
self->priv = G_TYPE_INSTANCE_GET_PRIVATE ((self), |
| 579 |
QMI_TYPE_ENDPOINT_QMUX, |
| 580 |
QmiEndpointQmuxPrivate); |
| 581 |
self->priv->fd = -1; |
| 582 |
} |
| 583 |
|
| 584 |
static void |
| 585 |
dispose (GObject *object) |
| 586 |
{ |
| 587 |
QmiEndpointQmux *self = QMI_ENDPOINT_QMUX (object); |
| 588 |
|
| 589 |
destroy_iostream (self); |
| 590 |
g_clear_object (&self->priv->client_ctl); |
| 591 |
g_clear_pointer (&self->priv->proxy_path, g_free); |
| 592 |
|
| 593 |
G_OBJECT_CLASS (qmi_endpoint_qmux_parent_class)->dispose (object); |
| 594 |
} |
| 595 |
|
| 596 |
static void |
| 597 |
qmi_endpoint_qmux_class_init (QmiEndpointQmuxClass *klass) |
| 598 |
{ |
| 599 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 600 |
QmiEndpointClass *endpoint_class = QMI_ENDPOINT_CLASS (klass); |
| 601 |
|
| 602 |
g_type_class_add_private (object_class, sizeof (QmiEndpointQmuxPrivate)); |
| 603 |
|
| 604 |
object_class->dispose = dispose; |
| 605 |
|
| 606 |
endpoint_class->open = endpoint_open; |
| 607 |
endpoint_class->open_finish = endpoint_open_finish; |
| 608 |
endpoint_class->is_open = endpoint_is_open; |
| 609 |
endpoint_class->send = endpoint_send; |
| 610 |
endpoint_class->close = endpoint_close; |
| 611 |
endpoint_class->close_finish = endpoint_close_finish; |
| 612 |
} |