| 1 |
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 3 |
/* |
| 4 |
* libmbim-glib -- GLib/GIO based library to control MBIM devices |
| 5 |
* |
| 6 |
* Copyright (C) 2014 Aleksander Morgado <[email protected]> |
| 7 |
* Copyright (C) 2014 Smith Micro Software, Inc. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <string.h> |
| 11 |
#include <ctype.h> |
| 12 |
#include <sys/file.h> |
| 13 |
#include <sys/types.h> |
| 14 |
#include <errno.h> |
| 15 |
|
| 16 |
#include <glib.h> |
| 17 |
#include <glib/gstdio.h> |
| 18 |
#include <gio/gunixsocketaddress.h> |
| 19 |
|
| 20 |
#include "config.h" |
| 21 |
#include "mbim-device.h" |
| 22 |
#include "mbim-utils.h" |
| 23 |
#include "mbim-helpers.h" |
| 24 |
#include "mbim-proxy.h" |
| 25 |
#include "mbim-message-private.h" |
| 26 |
#include "mbim-cid.h" |
| 27 |
#include "mbim-enum-types.h" |
| 28 |
#include "mbim-error-types.h" |
| 29 |
#include "mbim-basic-connect.h" |
| 30 |
#include "mbim-ms-basic-connect-extensions.h" |
| 31 |
#include "mbim-proxy-helpers.h" |
| 32 |
|
| 33 |
/* The mbim-proxy may be used for bulk data transfer, such as modem |
| 34 |
* firmware upgrade, and the BUFFER_SIZE should be at least equal |
| 35 |
* to MAX_CONTROL_TRANSFER which defined in mbim-device.c, which |
| 36 |
* will bring better performance in such case. |
| 37 |
*/ |
| 38 |
#define BUFFER_SIZE 4096 |
| 39 |
|
| 40 |
/* The proxy control "Version" indication reporting the last agreed |
| 41 |
* MBIMEx version, if any */ |
| 42 |
#define MBIM_DEVICE_PROXY_CONTROL_VERSION "mbim-device-proxy-control-version" |
| 43 |
|
| 44 |
G_DEFINE_TYPE (MbimProxy, mbim_proxy, G_TYPE_OBJECT) |
| 45 |
|
| 46 |
enum { |
| 47 |
PROP_0, |
| 48 |
PROP_N_CLIENTS, |
| 49 |
PROP_N_DEVICES, |
| 50 |
PROP_LAST |
| 51 |
}; |
| 52 |
|
| 53 |
static GParamSpec *properties[PROP_LAST]; |
| 54 |
|
| 55 |
struct _MbimProxyPrivate { |
| 56 |
/* Unix socket service */ |
| 57 |
GSocketService *socket_service; |
| 58 |
|
| 59 |
/* Clients */ |
| 60 |
GList *clients; |
| 61 |
|
| 62 |
/* Devices */ |
| 63 |
GList *devices; |
| 64 |
GList *opening_devices; |
| 65 |
}; |
| 66 |
|
| 67 |
static void track_device (MbimProxy *self, MbimDevice *device); |
| 68 |
static void untrack_device (MbimProxy *self, MbimDevice *device); |
| 69 |
static MbimDevice *peek_device_for_path (MbimProxy *self, const gchar *path); |
| 70 |
|
| 71 |
/*****************************************************************************/ |
| 72 |
|
| 73 |
guint |
| 74 |
mbim_proxy_get_n_clients (MbimProxy *self) |
| 75 |
{ |
| 76 |
g_return_val_if_fail (MBIM_IS_PROXY (self), 0); |
| 77 |
|
| 78 |
return g_list_length (self->priv->clients); |
| 79 |
} |
| 80 |
|
| 81 |
guint |
| 82 |
mbim_proxy_get_n_devices (MbimProxy *self) |
| 83 |
{ |
| 84 |
g_return_val_if_fail (MBIM_IS_PROXY (self), 0); |
| 85 |
|
| 86 |
return g_list_length (self->priv->devices); |
| 87 |
} |
| 88 |
|
| 89 |
/*****************************************************************************/ |
| 90 |
/* Client info */ |
| 91 |
|
| 92 |
typedef struct { |
| 93 |
volatile gint ref_count; |
| 94 |
gulong id; |
| 95 |
|
| 96 |
MbimProxy *self; /* not full ref */ |
| 97 |
GSocketConnection *connection; |
| 98 |
GSource *connection_readable_source; |
| 99 |
GByteArray *buffer; |
| 100 |
|
| 101 |
/* Only one proxy config allowed at a time */ |
| 102 |
gboolean config_ongoing; |
| 103 |
|
| 104 |
MbimDevice *device; |
| 105 |
guint indication_id; |
| 106 |
MbimEventEntry **mbim_event_entry_array; |
| 107 |
gsize mbim_event_entry_array_size; |
| 108 |
} Client; |
| 109 |
|
| 110 |
static gboolean connection_readable_cb (GSocket *socket, GIOCondition condition, Client *client); |
| 111 |
static void track_client (MbimProxy *self, Client *client); |
| 112 |
static void untrack_client (MbimProxy *self, Client *client); |
| 113 |
|
| 114 |
static void |
| 115 |
client_disconnect (Client *client) |
| 116 |
{ |
| 117 |
g_clear_pointer (&client->mbim_event_entry_array, mbim_event_entry_array_free); |
| 118 |
client->mbim_event_entry_array_size = 0; |
| 119 |
|
| 120 |
if (client->connection_readable_source) { |
| 121 |
g_source_destroy (client->connection_readable_source); |
| 122 |
g_source_unref (client->connection_readable_source); |
| 123 |
client->connection_readable_source = 0; |
| 124 |
} |
| 125 |
|
| 126 |
if (client->connection) { |
| 127 |
g_debug ("[client %lu] connection closed", client->id); |
| 128 |
g_output_stream_close (g_io_stream_get_output_stream (G_IO_STREAM (client->connection)), NULL, NULL); |
| 129 |
g_object_unref (client->connection); |
| 130 |
client->connection = NULL; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
static void client_indication_cb (MbimDevice *device, |
| 135 |
MbimMessage *message, |
| 136 |
Client *client); |
| 137 |
|
| 138 |
static void |
| 139 |
client_set_device (Client *client, |
| 140 |
MbimDevice *device) |
| 141 |
{ |
| 142 |
if (client->device) { |
| 143 |
if (g_signal_handler_is_connected (client->device, client->indication_id)) |
| 144 |
g_signal_handler_disconnect (client->device, client->indication_id); |
| 145 |
g_object_unref (client->device); |
| 146 |
} |
| 147 |
|
| 148 |
if (device) { |
| 149 |
client->device = g_object_ref (device); |
| 150 |
client->indication_id = g_signal_connect (client->device, |
| 151 |
MBIM_DEVICE_SIGNAL_INDICATE_STATUS, |
| 152 |
G_CALLBACK (client_indication_cb), |
| 153 |
client); |
| 154 |
} else { |
| 155 |
client->device = NULL; |
| 156 |
client->indication_id = 0; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
static void |
| 161 |
client_unref (Client *client) |
| 162 |
{ |
| 163 |
if (g_atomic_int_dec_and_test (&client->ref_count)) { |
| 164 |
/* Ensure disconnected */ |
| 165 |
client_disconnect (client); |
| 166 |
/* Reset device */ |
| 167 |
client_set_device (client, NULL); |
| 168 |
|
| 169 |
if (client->buffer) |
| 170 |
g_byte_array_unref (client->buffer); |
| 171 |
|
| 172 |
if (client->mbim_event_entry_array) |
| 173 |
mbim_event_entry_array_free (client->mbim_event_entry_array); |
| 174 |
|
| 175 |
g_slice_free (Client, client); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
G_DEFINE_AUTOPTR_CLEANUP_FUNC (Client, client_unref) |
| 180 |
|
| 181 |
static Client * |
| 182 |
client_ref (Client *client) |
| 183 |
{ |
| 184 |
g_atomic_int_inc (&client->ref_count); |
| 185 |
return client; |
| 186 |
} |
| 187 |
|
| 188 |
static gboolean |
| 189 |
client_send_message (Client *client, |
| 190 |
MbimMessage *message, |
| 191 |
GError **error) |
| 192 |
{ |
| 193 |
if (!client->connection) { |
| 194 |
g_set_error (error, |
| 195 |
MBIM_CORE_ERROR, |
| 196 |
MBIM_CORE_ERROR_WRONG_STATE, |
| 197 |
"Cannot send message: not connected"); |
| 198 |
return FALSE; |
| 199 |
} |
| 200 |
|
| 201 |
if (!g_output_stream_write_all (g_io_stream_get_output_stream (G_IO_STREAM (client->connection)), |
| 202 |
message->data, |
| 203 |
message->len, |
| 204 |
NULL, /* bytes_written */ |
| 205 |
NULL, /* cancellable */ |
| 206 |
error)) { |
| 207 |
g_prefix_error (error, "Cannot send message to client: "); |
| 208 |
return FALSE; |
| 209 |
} |
| 210 |
|
| 211 |
return TRUE; |
| 212 |
} |
| 213 |
|
| 214 |
/*****************************************************************************/ |
| 215 |
/* Track/untrack clients */ |
| 216 |
|
| 217 |
static void |
| 218 |
track_client (MbimProxy *self, |
| 219 |
Client *client) |
| 220 |
{ |
| 221 |
self->priv->clients = g_list_append (self->priv->clients, client_ref (client)); |
| 222 |
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_N_CLIENTS]); |
| 223 |
} |
| 224 |
|
| 225 |
static void |
| 226 |
untrack_client (MbimProxy *self, |
| 227 |
Client *client) |
| 228 |
{ |
| 229 |
/* Disconnect the client explicitly when untracking */ |
| 230 |
client_disconnect (client); |
| 231 |
|
| 232 |
if (g_list_find (self->priv->clients, client)) { |
| 233 |
self->priv->clients = g_list_remove (self->priv->clients, client); |
| 234 |
client_unref (client); |
| 235 |
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_N_CLIENTS]); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
/*****************************************************************************/ |
| 240 |
/* Client indications */ |
| 241 |
|
| 242 |
static void |
| 243 |
forward_indication (Client *client, |
| 244 |
MbimMessage *message) |
| 245 |
{ |
| 246 |
g_autoptr(GError) error = NULL; |
| 247 |
|
| 248 |
if (!client_send_message (client, message, &error)) |
| 249 |
g_warning ("[client %lu] couldn't forward indication: %s", client->id, error->message); |
| 250 |
} |
| 251 |
|
| 252 |
static void |
| 253 |
client_indication_cb (MbimDevice *device, |
| 254 |
MbimMessage *message, |
| 255 |
Client *client) |
| 256 |
{ |
| 257 |
MbimEventEntry *entry; |
| 258 |
guint i; |
| 259 |
|
| 260 |
/* if client doesn't have a subscribe list, we're done. */ |
| 261 |
if (!client->mbim_event_entry_array) |
| 262 |
return; |
| 263 |
|
| 264 |
/* Look for the event list associated to the service */ |
| 265 |
entry = NULL; |
| 266 |
for (i = 0; i < client->mbim_event_entry_array_size; i++) { |
| 267 |
if (mbim_uuid_cmp (mbim_message_indicate_status_get_service_id (message), |
| 268 |
&client->mbim_event_entry_array[i]->device_service_id)) { |
| 269 |
entry = client->mbim_event_entry_array[i]; |
| 270 |
break; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
/* if client didn't subscribe to anything in this service, we're done */ |
| 275 |
if (!entry) |
| 276 |
return; |
| 277 |
|
| 278 |
/* if client subscribed using the wildcard, no need to match specific cid */ |
| 279 |
if (entry->cids_count == 0) { |
| 280 |
forward_indication (client, message); |
| 281 |
return; |
| 282 |
} |
| 283 |
|
| 284 |
/* Look for the specific cid in the event list */ |
| 285 |
for (i = 0; i < entry->cids_count; i++) { |
| 286 |
if (mbim_message_indicate_status_get_cid (message) == entry->cids[i]) { |
| 287 |
/* exact match in the subscription */ |
| 288 |
forward_indication (client, message); |
| 289 |
return; |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
/*****************************************************************************/ |
| 295 |
/* Request info */ |
| 296 |
|
| 297 |
typedef struct { |
| 298 |
MbimProxy *self; |
| 299 |
Client *client; |
| 300 |
MbimMessage *message; |
| 301 |
MbimMessage *response; |
| 302 |
guint32 original_transaction_id; |
| 303 |
/* Only used in proxy config */ |
| 304 |
guint32 timeout_secs; |
| 305 |
} Request; |
| 306 |
|
| 307 |
static void |
| 308 |
request_complete_and_free (Request *request) |
| 309 |
{ |
| 310 |
if (request->response) { |
| 311 |
g_autoptr(GError) error = NULL; |
| 312 |
|
| 313 |
/* Try to send response to client; if it fails, always assume we have |
| 314 |
* to close the connection */ |
| 315 |
if (!client_send_message (request->client, request->response, &error)) { |
| 316 |
g_warning ("[client %lu,0x%08x] couldn't send response back to client: %s", |
| 317 |
request->client->id, request->original_transaction_id, error->message); |
| 318 |
/* Disconnect and untrack client */ |
| 319 |
untrack_client (request->self, request->client); |
| 320 |
} |
| 321 |
|
| 322 |
mbim_message_unref (request->response); |
| 323 |
} |
| 324 |
|
| 325 |
if (request->message) |
| 326 |
mbim_message_unref (request->message); |
| 327 |
client_unref (request->client); |
| 328 |
g_object_unref (request->self); |
| 329 |
g_slice_free (Request, request); |
| 330 |
} |
| 331 |
|
| 332 |
static Request * |
| 333 |
request_new (MbimProxy *self, |
| 334 |
Client *client, |
| 335 |
MbimMessage *message) |
| 336 |
{ |
| 337 |
Request *request; |
| 338 |
|
| 339 |
request = g_slice_new0 (Request); |
| 340 |
request->self = g_object_ref (self); |
| 341 |
request->client = client_ref (client); |
| 342 |
request->message = mbim_message_ref (message); |
| 343 |
request->original_transaction_id = mbim_message_get_transaction_id (message); |
| 344 |
|
| 345 |
return request; |
| 346 |
} |
| 347 |
|
| 348 |
/*****************************************************************************/ |
| 349 |
/* Internal proxy device opening operation */ |
| 350 |
|
| 351 |
static MbimEventEntry **merge_client_service_subscribe_lists (MbimProxy *self, |
| 352 |
MbimDevice *device, |
| 353 |
gsize *out_size); |
| 354 |
static void reset_client_service_subscribe_lists (MbimProxy *self, |
| 355 |
MbimDevice *device); |
| 356 |
|
| 357 |
typedef struct { |
| 358 |
MbimDevice *device; |
| 359 |
guint32 timeout_secs; |
| 360 |
} InternalDeviceOpenContext; |
| 361 |
|
| 362 |
static void |
| 363 |
internal_device_open_context_free (InternalDeviceOpenContext *ctx) |
| 364 |
{ |
| 365 |
g_object_unref (ctx->device); |
| 366 |
g_slice_free (InternalDeviceOpenContext, ctx); |
| 367 |
} |
| 368 |
|
| 369 |
static gboolean |
| 370 |
internal_device_open_finish (MbimProxy *self, |
| 371 |
GAsyncResult *res, |
| 372 |
GError **error) |
| 373 |
{ |
| 374 |
return g_task_propagate_boolean (G_TASK (res), error); |
| 375 |
} |
| 376 |
|
| 377 |
typedef struct { |
| 378 |
MbimDevice *device; |
| 379 |
GList *pending; |
| 380 |
} OpeningDevice; |
| 381 |
|
| 382 |
static void |
| 383 |
opening_device_complete_and_free (OpeningDevice *info, |
| 384 |
const GError *error) |
| 385 |
{ |
| 386 |
GList *l; |
| 387 |
|
| 388 |
/* Complete all pending open tasks */ |
| 389 |
for (l = info->pending; l; l = g_list_next (l)) { |
| 390 |
GTask *task = (GTask *)(l->data); |
| 391 |
|
| 392 |
if (error) |
| 393 |
g_task_return_error (task, g_error_copy (error)); |
| 394 |
else |
| 395 |
g_task_return_boolean (task, TRUE); |
| 396 |
g_object_unref (task); |
| 397 |
} |
| 398 |
|
| 399 |
g_list_free (info->pending); |
| 400 |
g_object_unref (info->device); |
| 401 |
g_slice_free (OpeningDevice, info); |
| 402 |
} |
| 403 |
|
| 404 |
static OpeningDevice * |
| 405 |
peek_opening_device_info (MbimProxy *self, |
| 406 |
MbimDevice *device) |
| 407 |
{ |
| 408 |
GList *l; |
| 409 |
|
| 410 |
/* If already being opened, queue it up */ |
| 411 |
for (l = self->priv->opening_devices; l; l = g_list_next (l)) { |
| 412 |
OpeningDevice *info; |
| 413 |
|
| 414 |
info = (OpeningDevice *)(l->data); |
| 415 |
if (device == info->device) |
| 416 |
return info; |
| 417 |
} |
| 418 |
|
| 419 |
return NULL; |
| 420 |
} |
| 421 |
|
| 422 |
static void |
| 423 |
complete_opening_device (MbimProxy *self, |
| 424 |
MbimDevice *device, |
| 425 |
const GError *error) |
| 426 |
{ |
| 427 |
OpeningDevice *info; |
| 428 |
|
| 429 |
info = peek_opening_device_info (self, device); |
| 430 |
if (!info) |
| 431 |
return; |
| 432 |
|
| 433 |
self->priv->opening_devices = g_list_remove (self->priv->opening_devices, info); |
| 434 |
opening_device_complete_and_free (info, error); |
| 435 |
} |
| 436 |
|
| 437 |
static void |
| 438 |
cancel_opening_device (MbimProxy *self, |
| 439 |
MbimDevice *device) |
| 440 |
{ |
| 441 |
OpeningDevice *info; |
| 442 |
GError *error; |
| 443 |
|
| 444 |
info = peek_opening_device_info (self, device); |
| 445 |
if (!info) |
| 446 |
return; |
| 447 |
|
| 448 |
error = g_error_new (MBIM_CORE_ERROR, MBIM_CORE_ERROR_ABORTED, "Device is gone"); |
| 449 |
complete_opening_device (self, device, error); |
| 450 |
g_error_free (error); |
| 451 |
} |
| 452 |
|
| 453 |
static void |
| 454 |
device_open_ready (MbimDevice *device, |
| 455 |
GAsyncResult *res, |
| 456 |
MbimProxy *self) |
| 457 |
{ |
| 458 |
GError *error = NULL; |
| 459 |
|
| 460 |
mbim_device_open_finish (device, res, &error); |
| 461 |
|
| 462 |
/* Complete all pending open actions */ |
| 463 |
complete_opening_device (self, device, error); |
| 464 |
|
| 465 |
if (error) { |
| 466 |
/* Fully untrack the device as it wasn't correctly open */ |
| 467 |
untrack_device (self, device); |
| 468 |
g_error_free (error); |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
static void |
| 473 |
internal_open (GTask *task) |
| 474 |
{ |
| 475 |
MbimProxy *self; |
| 476 |
InternalDeviceOpenContext *ctx; |
| 477 |
OpeningDevice *info; |
| 478 |
|
| 479 |
self = g_task_get_source_object (task); |
| 480 |
ctx = g_task_get_task_data (task); |
| 481 |
|
| 482 |
/* If already being opened, queue it up */ |
| 483 |
info = peek_opening_device_info (self, ctx->device); |
| 484 |
if (info) { |
| 485 |
info->pending = g_list_append (info->pending, task); |
| 486 |
return; |
| 487 |
} |
| 488 |
|
| 489 |
/* First time opening */ |
| 490 |
info = g_slice_new0 (OpeningDevice); |
| 491 |
info->device = g_object_ref (ctx->device); |
| 492 |
info->pending = g_list_append (info->pending, task); |
| 493 |
self->priv->opening_devices = g_list_prepend (self->priv->opening_devices, info); |
| 494 |
|
| 495 |
/* Note: for now, only the first timeout request is taken into account */ |
| 496 |
|
| 497 |
/* Need to open the device; and we must make sure the proxy only does this once, even |
| 498 |
* when multiple clients request it */ |
| 499 |
mbim_device_open (ctx->device, |
| 500 |
ctx->timeout_secs, |
| 501 |
NULL, |
| 502 |
(GAsyncReadyCallback)device_open_ready, |
| 503 |
g_object_ref (self)); |
| 504 |
} |
| 505 |
|
| 506 |
static void |
| 507 |
internal_device_open_caps_query_ready (MbimDevice *device, |
| 508 |
GAsyncResult *res, |
| 509 |
GTask *task) |
| 510 |
{ |
| 511 |
MbimProxy *self; |
| 512 |
g_autoptr(MbimMessage) response = NULL; |
| 513 |
g_autoptr(GError) error = NULL; |
| 514 |
|
| 515 |
self = g_task_get_source_object (task); |
| 516 |
|
| 517 |
response = mbim_device_command_finish (device, res, &error); |
| 518 |
if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) { |
| 519 |
/* If we get a not-opened error, well, force closing right away and reopen */ |
| 520 |
if (g_error_matches (error, MBIM_PROTOCOL_ERROR, MBIM_PROTOCOL_ERROR_NOT_OPENED)) { |
| 521 |
g_debug ("[%s] device not-opened error reported, reopening", mbim_device_get_path (device)); |
| 522 |
reset_client_service_subscribe_lists (self, device); |
| 523 |
mbim_device_close_force (device, NULL); |
| 524 |
internal_open (task); |
| 525 |
return; |
| 526 |
} |
| 527 |
|
| 528 |
/* Warn other (unlikely!) errors, but keep on anyway */ |
| 529 |
g_warning ("[%s] device caps query during internal open failed: %s", |
| 530 |
mbim_device_get_path (device), error->message); |
| 531 |
} |
| 532 |
|
| 533 |
g_debug ("[%s] device caps query during internal open succeeded", |
| 534 |
mbim_device_get_path (device)); |
| 535 |
|
| 536 |
g_task_return_boolean (task, TRUE); |
| 537 |
g_object_unref (task); |
| 538 |
} |
| 539 |
|
| 540 |
static void |
| 541 |
internal_device_open (MbimProxy *self, |
| 542 |
MbimDevice *device, |
| 543 |
guint32 timeout_secs, |
| 544 |
GAsyncReadyCallback callback, |
| 545 |
gpointer user_data) |
| 546 |
{ |
| 547 |
InternalDeviceOpenContext *ctx; |
| 548 |
GTask *task; |
| 549 |
|
| 550 |
task = g_task_new (self, NULL, callback, user_data); |
| 551 |
|
| 552 |
ctx = g_slice_new0 (InternalDeviceOpenContext); |
| 553 |
ctx->device = g_object_ref (device); |
| 554 |
ctx->timeout_secs = timeout_secs; |
| 555 |
g_task_set_task_data (task, ctx, (GDestroyNotify) internal_device_open_context_free); |
| 556 |
|
| 557 |
/* If the device is flagged as already open, we still want to check |
| 558 |
* whether that's totally true, and we do that with a standard command |
| 559 |
* (loading caps in this case). */ |
| 560 |
if (mbim_device_is_open (device)) { |
| 561 |
MbimMessage *message; |
| 562 |
|
| 563 |
g_debug ("[%s] checking device caps during client device open...", |
| 564 |
mbim_device_get_path (device)); |
| 565 |
message = mbim_message_device_caps_query_new (NULL); |
| 566 |
mbim_device_command (device, |
| 567 |
message, |
| 568 |
5, |
| 569 |
NULL, |
| 570 |
(GAsyncReadyCallback)internal_device_open_caps_query_ready, |
| 571 |
task); |
| 572 |
mbim_message_unref (message); |
| 573 |
return; |
| 574 |
} |
| 575 |
|
| 576 |
internal_open (task); |
| 577 |
} |
| 578 |
|
| 579 |
/*****************************************************************************/ |
| 580 |
/* Proxy open */ |
| 581 |
|
| 582 |
static gboolean |
| 583 |
process_internal_proxy_open (MbimProxy *self, |
| 584 |
Client *client, |
| 585 |
MbimMessage *message) |
| 586 |
{ |
| 587 |
Request *request; |
| 588 |
MbimStatusError status = MBIM_STATUS_ERROR_FAILURE; |
| 589 |
|
| 590 |
/* create request holder */ |
| 591 |
request = request_new (self, client, message); |
| 592 |
|
| 593 |
if (!client->device) |
| 594 |
g_warning ("[client %lu] cannot process MBIM open: device not set", client->id); |
| 595 |
else if (!mbim_device_is_open (client->device)) |
| 596 |
g_warning ("[client %lu] cannot process MBIM open: device not opened by proxy", client->id); |
| 597 |
else { |
| 598 |
g_debug ("[client %lu] connection to MBIM device '%s' established", client->id, mbim_device_get_path (client->device)); |
| 599 |
status = MBIM_STATUS_ERROR_NONE; |
| 600 |
} |
| 601 |
|
| 602 |
request->response = mbim_message_open_done_new (mbim_message_get_transaction_id (request->message), status); |
| 603 |
request_complete_and_free (request); |
| 604 |
return TRUE; |
| 605 |
} |
| 606 |
|
| 607 |
/*****************************************************************************/ |
| 608 |
/* Proxy close */ |
| 609 |
|
| 610 |
static gboolean |
| 611 |
process_internal_proxy_close (MbimProxy *self, |
| 612 |
Client *client, |
| 613 |
MbimMessage *message) |
| 614 |
{ |
| 615 |
Request *request; |
| 616 |
guint32 original_transaction_id; |
| 617 |
|
| 618 |
original_transaction_id = mbim_message_get_transaction_id (message); |
| 619 |
g_debug ("[client %lu,0x%08x] requested explicit MBIM channel close", |
| 620 |
client->id, original_transaction_id); |
| 621 |
|
| 622 |
request = request_new (self, client, message); |
| 623 |
request->response = mbim_message_close_done_new (original_transaction_id, MBIM_STATUS_ERROR_NONE); |
| 624 |
request_complete_and_free (request); |
| 625 |
return TRUE; |
| 626 |
} |
| 627 |
|
| 628 |
/*****************************************************************************/ |
| 629 |
/* Proxy config */ |
| 630 |
|
| 631 |
static MbimMessage * |
| 632 |
build_proxy_control_command_done (MbimMessage *message, |
| 633 |
MbimStatusError status) |
| 634 |
{ |
| 635 |
MbimMessage *response; |
| 636 |
struct command_done_message *command_done; |
| 637 |
|
| 638 |
response = (MbimMessage *) _mbim_message_allocate (MBIM_MESSAGE_TYPE_COMMAND_DONE, |
| 639 |
mbim_message_get_transaction_id (message), |
| 640 |
sizeof (struct command_done_message)); |
| 641 |
command_done = &(((struct full_message *)(response->data))->message.command_done); |
| 642 |
command_done->fragment_header.total = GUINT32_TO_LE (1); |
| 643 |
command_done->fragment_header.current = 0; |
| 644 |
memcpy (command_done->service_id, MBIM_UUID_PROXY_CONTROL, sizeof (MbimUuid)); |
| 645 |
command_done->command_id = GUINT32_TO_LE (mbim_message_command_get_cid (message)); |
| 646 |
command_done->status_code = GUINT32_TO_LE (status); |
| 647 |
command_done->buffer_length = 0; |
| 648 |
|
| 649 |
return response; |
| 650 |
} |
| 651 |
|
| 652 |
static void |
| 653 |
proxy_config_internal_device_open_ready (MbimProxy *self, |
| 654 |
GAsyncResult *res, |
| 655 |
Request *request) |
| 656 |
{ |
| 657 |
g_autoptr(GError) error = NULL; |
| 658 |
MbimMessage *indication; |
| 659 |
|
| 660 |
if (!internal_device_open_finish (self, res, &error)) { |
| 661 |
g_warning ("[client %lu,0x%08x] cannot configure proxy: couldn't open MBIM device: %s", |
| 662 |
request->client->id, request->original_transaction_id, error->message); |
| 663 |
/* Untrack client and complete without response */ |
| 664 |
untrack_client (request->self, request->client); |
| 665 |
request_complete_and_free (request); |
| 666 |
return; |
| 667 |
} |
| 668 |
|
| 669 |
g_debug ("[client %lu,0x%08x] proxy configured", |
| 670 |
request->client->id, request->original_transaction_id); |
| 671 |
|
| 672 |
/* notify the client about the MBIMEx version */ |
| 673 |
indication = (MbimMessage *) g_object_get_data (G_OBJECT (request->client->device), MBIM_DEVICE_PROXY_CONTROL_VERSION); |
| 674 |
if (indication) { |
| 675 |
if (!client_send_message (request->client, indication, &error)) |
| 676 |
g_warning ("[client %lu] couldn't report MBIMEx version update: %s", request->client->id, error->message); |
| 677 |
else |
| 678 |
g_debug ("[client %lu] reported MBIMEx version update", request->client->id); |
| 679 |
} |
| 680 |
|
| 681 |
if (request->client->config_ongoing == TRUE) |
| 682 |
request->client->config_ongoing = FALSE; |
| 683 |
request->response = build_proxy_control_command_done (request->message, MBIM_STATUS_ERROR_NONE); |
| 684 |
request_complete_and_free (request); |
| 685 |
} |
| 686 |
|
| 687 |
static void |
| 688 |
device_new_ready (GObject *source, |
| 689 |
GAsyncResult *res, |
| 690 |
Request *request) |
| 691 |
{ |
| 692 |
g_autoptr(GError) error = NULL; |
| 693 |
MbimDevice *existing; |
| 694 |
MbimDevice *device; |
| 695 |
|
| 696 |
device = mbim_device_new_finish (res, &error); |
| 697 |
if (!device) { |
| 698 |
g_warning ("[client %lu,0x%08x] cannot configure proxy: couldn't create MBIM device: %s", |
| 699 |
request->client->id, request->original_transaction_id, error->message); |
| 700 |
/* Untrack client and complete without response */ |
| 701 |
untrack_client (request->self, request->client); |
| 702 |
request_complete_and_free (request); |
| 703 |
return; |
| 704 |
} |
| 705 |
|
| 706 |
/* Store device in the proxy independently */ |
| 707 |
existing = peek_device_for_path (request->self, mbim_device_get_path (device)); |
| 708 |
if (existing) { |
| 709 |
/* Race condition, we created two MbimDevices for the same port, just skip ours, no big deal */ |
| 710 |
client_set_device (request->client, existing); |
| 711 |
} else { |
| 712 |
/* Keep the newly added device in the proxy */ |
| 713 |
track_device (request->self, device); |
| 714 |
/* Also keep track of the device in the client */ |
| 715 |
client_set_device (request->client, device); |
| 716 |
} |
| 717 |
g_object_unref (device); |
| 718 |
|
| 719 |
internal_device_open (request->self, |
| 720 |
request->client->device, |
| 721 |
request->timeout_secs, |
| 722 |
(GAsyncReadyCallback)proxy_config_internal_device_open_ready, |
| 723 |
request); |
| 724 |
} |
| 725 |
|
| 726 |
static gboolean |
| 727 |
process_internal_proxy_config (MbimProxy *self, |
| 728 |
Client *client, |
| 729 |
MbimMessage *message) |
| 730 |
{ |
| 731 |
Request *request; |
| 732 |
MbimDevice *device; |
| 733 |
g_autofree gchar *incoming_path = NULL; |
| 734 |
g_autofree gchar *path = NULL; |
| 735 |
g_autoptr(GFile) file = NULL; |
| 736 |
g_autoptr(GError) error = NULL; |
| 737 |
|
| 738 |
/* create request holder */ |
| 739 |
request = request_new (self, client, message); |
| 740 |
|
| 741 |
g_debug ("[client %lu,0x%08x] request to configure proxy", |
| 742 |
request->client->id, request->original_transaction_id); |
| 743 |
|
| 744 |
/* Error out if there is already a proxy config ongoing */ |
| 745 |
if (client->config_ongoing) { |
| 746 |
g_warning ("[client %lu,0x%08x] cannot configure proxy: another request already ongoing", |
| 747 |
request->client->id, request->original_transaction_id); |
| 748 |
request->response = build_proxy_control_command_done (message, MBIM_STATUS_ERROR_BUSY); |
| 749 |
request_complete_and_free (request); |
| 750 |
return TRUE; |
| 751 |
} |
| 752 |
|
| 753 |
/* Only allow SET command */ |
| 754 |
if (mbim_message_command_get_command_type (message) != MBIM_MESSAGE_COMMAND_TYPE_SET) { |
| 755 |
g_warning ("[client %lu,0x%08x] cannot configure proxy: invalid request type", |
| 756 |
request->client->id, request->original_transaction_id); |
| 757 |
request->response = build_proxy_control_command_done (message, MBIM_STATUS_ERROR_INVALID_PARAMETERS); |
| 758 |
request_complete_and_free (request); |
| 759 |
return TRUE; |
| 760 |
} |
| 761 |
|
| 762 |
/* Retrieve path from request */ |
| 763 |
if (!_mbim_message_read_string (message, 0, 0, MBIM_STRING_ENCODING_UTF16, &incoming_path, NULL, &error)) { |
| 764 |
g_warning ("[client %lu,0x%08x] cannot configure proxy: couldn't read device path from request: %s", |
| 765 |
request->client->id, request->original_transaction_id, error->message); |
| 766 |
request->response = build_proxy_control_command_done (message, MBIM_STATUS_ERROR_INVALID_PARAMETERS); |
| 767 |
request_complete_and_free (request); |
| 768 |
return TRUE; |
| 769 |
} |
| 770 |
|
| 771 |
/* The incoming path may be a symlink. In the proxy, we always use the real path of the |
| 772 |
* device, so that clients using different symlinks for the same file don't collide with |
| 773 |
* each other. */ |
| 774 |
path = mbim_helpers_get_devpath (incoming_path, &error); |
| 775 |
if (!path) { |
| 776 |
g_warning ("[client %lu,0x%08x] cannot configure proxy: couldn't lookup real device path: %s", |
| 777 |
request->client->id, request->original_transaction_id, error->message); |
| 778 |
request->response = build_proxy_control_command_done (message, MBIM_STATUS_ERROR_INVALID_PARAMETERS); |
| 779 |
request_complete_and_free (request); |
| 780 |
return TRUE; |
| 781 |
} |
| 782 |
|
| 783 |
/* Only allow subsequent requests with the same path */ |
| 784 |
if (client->device) { |
| 785 |
if (g_str_equal (path, mbim_device_get_path (client->device))) { |
| 786 |
g_debug ("[client %lu,0x%08x] proxy re-configured", |
| 787 |
request->client->id, request->original_transaction_id); |
| 788 |
request->response = build_proxy_control_command_done (message, MBIM_STATUS_ERROR_NONE); |
| 789 |
} else { |
| 790 |
g_warning ("[client %lu,0x%08x] cannot configure proxy: different device path given", |
| 791 |
request->client->id, request->original_transaction_id); |
| 792 |
request->response = build_proxy_control_command_done (message, MBIM_STATUS_ERROR_FAILURE); |
| 793 |
} |
| 794 |
request_complete_and_free (request); |
| 795 |
return TRUE; |
| 796 |
} |
| 797 |
|
| 798 |
/* Read requested timeout value */ |
| 799 |
if (!_mbim_message_read_guint32 (message, 8, &request->timeout_secs, &error)) { |
| 800 |
g_warning ("[client %lu,0x%08x] cannot configure proxy: couldn't read timeout from request: %s", |
| 801 |
request->client->id, request->original_transaction_id, error->message); |
| 802 |
request->response = build_proxy_control_command_done (message, MBIM_STATUS_ERROR_INVALID_PARAMETERS); |
| 803 |
request_complete_and_free (request); |
| 804 |
return TRUE; |
| 805 |
} |
| 806 |
|
| 807 |
/* Check if some other client already handled the same device */ |
| 808 |
device = peek_device_for_path (self, path); |
| 809 |
if (device) { |
| 810 |
/* Keep reference and continue */ |
| 811 |
client_set_device (client, device); |
| 812 |
internal_device_open (self, |
| 813 |
device, |
| 814 |
request->timeout_secs, |
| 815 |
(GAsyncReadyCallback)proxy_config_internal_device_open_ready, |
| 816 |
request); |
| 817 |
return TRUE; |
| 818 |
} |
| 819 |
|
| 820 |
/* Flag as ongoing */ |
| 821 |
client->config_ongoing = TRUE; |
| 822 |
|
| 823 |
/* Create new MBIM device */ |
| 824 |
file = g_file_new_for_path (path); |
| 825 |
mbim_device_new (file, |
| 826 |
NULL, |
| 827 |
(GAsyncReadyCallback)device_new_ready, |
| 828 |
request); |
| 829 |
return TRUE; |
| 830 |
} |
| 831 |
|
| 832 |
/*****************************************************************************/ |
| 833 |
/* Subscriber list */ |
| 834 |
|
| 835 |
static void |
| 836 |
track_service_subscribe_list (Client *client, |
| 837 |
MbimMessage *message) |
| 838 |
{ |
| 839 |
g_autoptr(GError) error = NULL; |
| 840 |
g_autoptr(MbimEventEntryArray) mbim_event_entry_array = NULL; |
| 841 |
gsize mbim_event_entry_array_size; |
| 842 |
|
| 843 |
mbim_event_entry_array = _mbim_proxy_helper_service_subscribe_request_parse (message, &mbim_event_entry_array_size, &error); |
| 844 |
if (error) { |
| 845 |
g_warning ("[client %lu] invalid subscribe request message: %s", client->id, error->message); |
| 846 |
return; |
| 847 |
} |
| 848 |
|
| 849 |
/* On each new request from the client, it should provide the FULL list of |
| 850 |
* events it's subscribed to, so we can safely recreate the whole array each |
| 851 |
* time. */ |
| 852 |
g_clear_pointer (&client->mbim_event_entry_array, mbim_event_entry_array_free); |
| 853 |
client->mbim_event_entry_array = g_steal_pointer (&mbim_event_entry_array); |
| 854 |
client->mbim_event_entry_array_size = mbim_event_entry_array_size; |
| 855 |
|
| 856 |
if (mbim_utils_get_traces_enabled ()) { |
| 857 |
g_debug ("[client %lu] service subscribe list built", client->id); |
| 858 |
_mbim_proxy_helper_service_subscribe_list_debug ((const MbimEventEntry * const *)client->mbim_event_entry_array, |
| 859 |
client->mbim_event_entry_array_size); |
| 860 |
} |
| 861 |
} |
| 862 |
|
| 863 |
static void |
| 864 |
device_service_subscribe_list_set_complete (Request *request, |
| 865 |
MbimStatusError status) |
| 866 |
{ |
| 867 |
struct command_done_message *command_done; |
| 868 |
guint32 raw_len; |
| 869 |
const guint8 *raw_data; |
| 870 |
|
| 871 |
/* The raw message data to send back as response to client */ |
| 872 |
raw_data = mbim_message_command_get_raw_information_buffer (request->message, &raw_len); |
| 873 |
|
| 874 |
request->response = (MbimMessage *)_mbim_message_allocate (MBIM_MESSAGE_TYPE_COMMAND_DONE, |
| 875 |
mbim_message_get_transaction_id (request->message), |
| 876 |
sizeof (struct command_done_message) + |
| 877 |
raw_len); |
| 878 |
command_done = &(((struct full_message *)(request->response->data))->message.command_done); |
| 879 |
command_done->fragment_header.total = GUINT32_TO_LE (1); |
| 880 |
command_done->fragment_header.current = 0; |
| 881 |
memcpy (command_done->service_id, MBIM_UUID_BASIC_CONNECT, sizeof (MbimUuid)); |
| 882 |
command_done->command_id = GUINT32_TO_LE (MBIM_CID_BASIC_CONNECT_DEVICE_SERVICE_SUBSCRIBE_LIST); |
| 883 |
command_done->status_code = GUINT32_TO_LE (status); |
| 884 |
command_done->buffer_length = GUINT32_TO_LE (raw_len); |
| 885 |
memcpy (&command_done->buffer[0], raw_data, raw_len); |
| 886 |
|
| 887 |
request_complete_and_free (request); |
| 888 |
} |
| 889 |
|
| 890 |
static void |
| 891 |
device_service_subscribe_list_set_ready (MbimDevice *device, |
| 892 |
GAsyncResult *res, |
| 893 |
Request *request) |
| 894 |
{ |
| 895 |
g_autoptr(MbimMessage) tmp_response = NULL; |
| 896 |
g_autoptr(GError) error = NULL; |
| 897 |
MbimStatusError error_status_code; |
| 898 |
|
| 899 |
tmp_response = mbim_device_command_finish (device, res, &error); |
| 900 |
if (!tmp_response) { |
| 901 |
/* Translate a MbimDevice wrong state error into a Not-Opened function error. */ |
| 902 |
if (g_error_matches (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_WRONG_STATE)) { |
| 903 |
g_debug ("[client %lu,0x%08x] sending request to device failed: wrong state", |
| 904 |
request->client->id, request->original_transaction_id); |
| 905 |
request->response = mbim_message_function_error_new (mbim_message_get_transaction_id (request->message), MBIM_PROTOCOL_ERROR_NOT_OPENED); |
| 906 |
request_complete_and_free (request); |
| 907 |
return; |
| 908 |
} |
| 909 |
|
| 910 |
/* Don't disconnect client, just let the request timeout in its side */ |
| 911 |
g_debug ("[client %lu,0x%08x] sending request to device failed: %s", |
| 912 |
request->client->id, request->original_transaction_id, error->message); |
| 913 |
request_complete_and_free (request); |
| 914 |
return; |
| 915 |
} |
| 916 |
|
| 917 |
g_debug ("[client %lu,0x%08x] response from device received", |
| 918 |
request->client->id, request->original_transaction_id); |
| 919 |
error_status_code = GUINT32_FROM_LE (((struct full_message *)(tmp_response->data))->message.command_done.status_code); |
| 920 |
device_service_subscribe_list_set_complete (request, error_status_code); |
| 921 |
} |
| 922 |
|
| 923 |
static gboolean |
| 924 |
process_device_service_subscribe_list (MbimProxy *self, |
| 925 |
Client *client, |
| 926 |
MbimMessage *message) |
| 927 |
{ |
| 928 |
MbimEventEntry **updated; |
| 929 |
gsize updated_size = 0; |
| 930 |
Request *request; |
| 931 |
g_autoptr(MbimMessage) request_message = NULL; |
| 932 |
|
| 933 |
/* create request holder */ |
| 934 |
request = request_new (self, client, message); |
| 935 |
|
| 936 |
g_debug ("[client %lu,0x%08x] request to update service subscribe list received", |
| 937 |
request->client->id, request->original_transaction_id); |
| 938 |
|
| 939 |
/* trace the service subscribe list for the client */ |
| 940 |
track_service_subscribe_list (client, message); |
| 941 |
|
| 942 |
/* merge all service subscribe list for all clients to set on device */ |
| 943 |
updated = merge_client_service_subscribe_lists (self, client->device, &updated_size); |
| 944 |
if (!updated) { |
| 945 |
g_debug ("[client %lu,0x%08x] service subscribe list update in device not needed", |
| 946 |
request->client->id, request->original_transaction_id); |
| 947 |
device_service_subscribe_list_set_complete (request, MBIM_STATUS_ERROR_NONE); |
| 948 |
return TRUE; |
| 949 |
} |
| 950 |
|
| 951 |
/* the 'updated' array was given to us as transfer-none!!!! */ |
| 952 |
request_message = mbim_message_device_service_subscribe_list_set_new (updated_size, (const MbimEventEntry *const *)updated, NULL); |
| 953 |
mbim_message_set_transaction_id (request_message, mbim_device_get_next_transaction_id (client->device)); |
| 954 |
|
| 955 |
g_debug ("[client %lu,0x%08x] updating service subscribe list in device...", |
| 956 |
request->client->id, request->original_transaction_id); |
| 957 |
mbim_device_command (client->device, |
| 958 |
request_message, |
| 959 |
300, |
| 960 |
NULL, |
| 961 |
(GAsyncReadyCallback)device_service_subscribe_list_set_ready, |
| 962 |
request); |
| 963 |
return TRUE; |
| 964 |
} |
| 965 |
|
| 966 |
/*****************************************************************************/ |
| 967 |
/* MBIMEx version detection */ |
| 968 |
|
| 969 |
static MbimMessage * |
| 970 |
build_proxy_control_version_notification (guint16 mbim_version, |
| 971 |
guint16 ms_mbimex_version) |
| 972 |
{ |
| 973 |
MbimMessage *message; |
| 974 |
struct indicate_status_message *indicate_status; |
| 975 |
guint16 tmp; |
| 976 |
guint buffer_i = 0; |
| 977 |
gsize buffer_length = 0; |
| 978 |
|
| 979 |
buffer_length = sizeof (mbim_version) + sizeof (ms_mbimex_version); |
| 980 |
|
| 981 |
message = (MbimMessage *) _mbim_message_allocate (MBIM_MESSAGE_TYPE_INDICATE_STATUS, |
| 982 |
0, |
| 983 |
sizeof (struct indicate_status_message) + buffer_length); |
| 984 |
indicate_status = &(((struct full_message *)(message->data))->message.indicate_status); |
| 985 |
indicate_status->fragment_header.total = GUINT32_TO_LE (1); |
| 986 |
indicate_status->fragment_header.current = 0; |
| 987 |
memcpy (indicate_status->service_id, MBIM_UUID_PROXY_CONTROL, sizeof (MbimUuid)); |
| 988 |
indicate_status->command_id = GUINT32_TO_LE (MBIM_CID_PROXY_CONTROL_VERSION); |
| 989 |
indicate_status->buffer_length = GUINT32_TO_LE (buffer_length); |
| 990 |
|
| 991 |
tmp = GUINT16_TO_LE (mbim_version); |
| 992 |
memcpy (&indicate_status->buffer[buffer_i], &tmp, sizeof (tmp)); |
| 993 |
buffer_i += sizeof (tmp); |
| 994 |
tmp = GUINT16_TO_LE (ms_mbimex_version); |
| 995 |
memcpy (&indicate_status->buffer[buffer_i], &tmp, sizeof (tmp)); |
| 996 |
buffer_i += sizeof (tmp); |
| 997 |
g_assert (buffer_i == buffer_length); |
| 998 |
|
| 999 |
return message; |
| 1000 |
} |
| 1001 |
|
| 1002 |
static void |
| 1003 |
monitor_ms_basic_connect_extensions_version_response (MbimProxy *self, |
| 1004 |
MbimDevice *device, |
| 1005 |
MbimMessage *response) |
| 1006 |
{ |
| 1007 |
g_autoptr(MbimMessage) indication = NULL; |
| 1008 |
guint16 mbim_version; |
| 1009 |
guint16 ms_mbimex_version; |
| 1010 |
guint8 ms_mbimex_version_major; |
| 1011 |
guint8 ms_mbimex_version_minor; |
| 1012 |
GList *l; |
| 1013 |
|
| 1014 |
/* monitor the MBIMEx version agreed between the clients and the device */ |
| 1015 |
if (!mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, NULL) || |
| 1016 |
(mbim_message_command_done_get_service (response) != MBIM_SERVICE_MS_BASIC_CONNECT_EXTENSIONS) || |
| 1017 |
(mbim_message_command_done_get_cid (response) != MBIM_CID_MS_BASIC_CONNECT_EXTENSIONS_VERSION) || |
| 1018 |
!mbim_message_ms_basic_connect_extensions_v2_version_response_parse (response, &mbim_version, &ms_mbimex_version, NULL)) |
| 1019 |
return; |
| 1020 |
|
| 1021 |
ms_mbimex_version_major = ms_mbimex_version >> 8; |
| 1022 |
ms_mbimex_version_minor = ms_mbimex_version & 0xFF; |
| 1023 |
|
| 1024 |
g_message ("Proxy monitoring detected MBIMEx version agreed with device: %x.%02x", |
| 1025 |
ms_mbimex_version_major, ms_mbimex_version_minor); |
| 1026 |
mbim_device_set_ms_mbimex_version (device, ms_mbimex_version_major, ms_mbimex_version_minor, NULL); |
| 1027 |
|
| 1028 |
/* notify to all clients about the MBIMEx version update */ |
| 1029 |
indication = build_proxy_control_version_notification (mbim_version, ms_mbimex_version); |
| 1030 |
for (l = self->priv->clients; l; l = g_list_next (l)) { |
| 1031 |
g_autoptr(GError) error = NULL; |
| 1032 |
Client *client; |
| 1033 |
|
| 1034 |
client = l->data; |
| 1035 |
if (client->device != device) |
| 1036 |
continue; |
| 1037 |
|
| 1038 |
if (!client_send_message (client, indication, &error)) |
| 1039 |
g_warning ("[client %lu] couldn't report MBIMEx version update to %x.%02x: %s", |
| 1040 |
client->id, ms_mbimex_version_major, ms_mbimex_version_minor, error->message); |
| 1041 |
else |
| 1042 |
g_debug ("[client %lu] reported MBIMEx version update to %x.%02x", |
| 1043 |
client->id, ms_mbimex_version_major, ms_mbimex_version_minor); |
| 1044 |
} |
| 1045 |
|
| 1046 |
/* the indication is stored as data associated to the device, so that it can |
| 1047 |
* be reused any time new clients attempt an open */ |
| 1048 |
g_object_set_data_full (G_OBJECT (device), |
| 1049 |
MBIM_DEVICE_PROXY_CONTROL_VERSION, |
| 1050 |
mbim_message_ref (indication), |
| 1051 |
(GDestroyNotify)mbim_message_unref); |
| 1052 |
} |
| 1053 |
|
| 1054 |
/*****************************************************************************/ |
| 1055 |
/* Standard command */ |
| 1056 |
|
| 1057 |
static void |
| 1058 |
device_command_ready (MbimDevice *device, |
| 1059 |
GAsyncResult *res, |
| 1060 |
Request *request) |
| 1061 |
{ |
| 1062 |
g_autoptr(GError) error = NULL; |
| 1063 |
|
| 1064 |
request->response = mbim_device_command_finish (device, res, &error); |
| 1065 |
if (!request->response) { |
| 1066 |
/* Translate a MbimDevice wrong state error into a Not-Opened function error. */ |
| 1067 |
if (g_error_matches (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_WRONG_STATE)) { |
| 1068 |
g_debug ("[client %lu,0x%08x] sending request to device failed: wrong state", |
| 1069 |
request->client->id, request->original_transaction_id); |
| 1070 |
request->response = mbim_message_function_error_new (request->original_transaction_id, MBIM_PROTOCOL_ERROR_NOT_OPENED); |
| 1071 |
request_complete_and_free (request); |
| 1072 |
return; |
| 1073 |
} |
| 1074 |
|
| 1075 |
/* Don't disconnect client, just let the request timeout in its side */ |
| 1076 |
g_debug ("[client %lu,0x%08x] sending request to device failed: %s", |
| 1077 |
request->client->id, request->original_transaction_id, error->message); |
| 1078 |
request_complete_and_free (request); |
| 1079 |
return; |
| 1080 |
} |
| 1081 |
|
| 1082 |
/* replace reponse transaction id with the requested transaction id */ |
| 1083 |
g_debug ("[client %lu,0x%08x] response from device received", |
| 1084 |
request->client->id, request->original_transaction_id); |
| 1085 |
|
| 1086 |
/* try to match the MBIMEx version exchange */ |
| 1087 |
monitor_ms_basic_connect_extensions_version_response (request->self, device, request->response); |
| 1088 |
|
| 1089 |
mbim_message_set_transaction_id (request->response, request->original_transaction_id); |
| 1090 |
request_complete_and_free (request); |
| 1091 |
} |
| 1092 |
|
| 1093 |
static gboolean |
| 1094 |
process_command (MbimProxy *self, |
| 1095 |
Client *client, |
| 1096 |
MbimMessage *message) |
| 1097 |
{ |
| 1098 |
Request *request; |
| 1099 |
const gchar *command; |
| 1100 |
const gchar *command_type; |
| 1101 |
const gchar *service; |
| 1102 |
|
| 1103 |
command = mbim_cid_get_printable (mbim_message_command_get_service (message), |
| 1104 |
mbim_message_command_get_cid (message)); |
| 1105 |
command_type = mbim_message_command_type_get_string (mbim_message_command_get_command_type (message)); |
| 1106 |
service = mbim_service_get_string (mbim_message_command_get_service (message)); |
| 1107 |
|
| 1108 |
/* create request holder */ |
| 1109 |
request = request_new (self, client, message); |
| 1110 |
|
| 1111 |
g_debug ("[client %lu,0x%08x] forwarding request to device: %s, %s, %s", |
| 1112 |
client->id, request->original_transaction_id, |
| 1113 |
service ? service : "unknown service", |
| 1114 |
command_type ? command_type : "unknown command type", |
| 1115 |
command ? command : "unknown command"); |
| 1116 |
|
| 1117 |
if (_mbim_message_fragment_get_current (message) == _mbim_message_fragment_get_total (message) - 1) |
| 1118 |
/* replace command transaction id with internal proxy transaction id to avoid collision */ |
| 1119 |
mbim_message_set_transaction_id (message, mbim_device_get_next_transaction_id (client->device)); |
| 1120 |
else |
| 1121 |
/* avoid incrementing transaction until the last fragment is processed */ |
| 1122 |
mbim_message_set_transaction_id (message, mbim_device_get_transaction_id (client->device)); |
| 1123 |
|
| 1124 |
/* The timeout needs to be big enough for any kind of transaction to |
| 1125 |
* complete, otherwise the remote clients will lose the reply if they |
| 1126 |
* configured a timeout bigger than this internal one. We should likely |
| 1127 |
* make this value configurable per-client, instead of a hardcoded value. |
| 1128 |
*/ |
| 1129 |
mbim_device_command (client->device, |
| 1130 |
message, |
| 1131 |
300, |
| 1132 |
NULL, |
| 1133 |
(GAsyncReadyCallback)device_command_ready, |
| 1134 |
request); |
| 1135 |
return TRUE; |
| 1136 |
} |
| 1137 |
|
| 1138 |
/*****************************************************************************/ |
| 1139 |
|
| 1140 |
static gboolean |
| 1141 |
process_message (MbimProxy *self, |
| 1142 |
Client *client, |
| 1143 |
MbimMessage *message) |
| 1144 |
{ |
| 1145 |
/* Filter by message type */ |
| 1146 |
switch (mbim_message_get_message_type (message)) { |
| 1147 |
case MBIM_MESSAGE_TYPE_OPEN: |
| 1148 |
return process_internal_proxy_open (self, client, message); |
| 1149 |
case MBIM_MESSAGE_TYPE_CLOSE: |
| 1150 |
return process_internal_proxy_close (self, client, message); |
| 1151 |
case MBIM_MESSAGE_TYPE_COMMAND: |
| 1152 |
/* Proxy control message? */ |
| 1153 |
if (mbim_message_command_get_service (message) == MBIM_SERVICE_PROXY_CONTROL && |
| 1154 |
mbim_message_command_get_cid (message) == MBIM_CID_PROXY_CONTROL_CONFIGURATION) |
| 1155 |
return process_internal_proxy_config (self, client, message); |
| 1156 |
/* device service subscribe list message? */ |
| 1157 |
if (mbim_message_command_get_service (message) == MBIM_SERVICE_BASIC_CONNECT && |
| 1158 |
mbim_message_command_get_cid (message) == MBIM_CID_BASIC_CONNECT_DEVICE_SERVICE_SUBSCRIBE_LIST) |
| 1159 |
return process_device_service_subscribe_list (self, client, message); |
| 1160 |
/* Otherwise, standard command to forward */ |
| 1161 |
return process_command (self, client, message); |
| 1162 |
case MBIM_MESSAGE_TYPE_INVALID: |
| 1163 |
case MBIM_MESSAGE_TYPE_COMMAND_DONE: |
| 1164 |
case MBIM_MESSAGE_TYPE_INDICATE_STATUS: |
| 1165 |
case MBIM_MESSAGE_TYPE_HOST_ERROR: |
| 1166 |
case MBIM_MESSAGE_TYPE_OPEN_DONE: |
| 1167 |
case MBIM_MESSAGE_TYPE_CLOSE_DONE: |
| 1168 |
case MBIM_MESSAGE_TYPE_FUNCTION_ERROR: |
| 1169 |
default: |
| 1170 |
g_debug ("[client %lu] invalid message: not a command", client->id); |
| 1171 |
return FALSE; |
| 1172 |
} |
| 1173 |
|
| 1174 |
g_assert_not_reached (); |
| 1175 |
} |
| 1176 |
|
| 1177 |
static void |
| 1178 |
parse_request (MbimProxy *self, |
| 1179 |
Client *client) |
| 1180 |
{ |
| 1181 |
do { |
| 1182 |
g_autoptr(MbimMessage) message = NULL; |
| 1183 |
g_autoptr(GError) error = NULL; |
| 1184 |
|
| 1185 |
/* Invalid message? */ |
| 1186 |
if (!_mbim_message_validate_internal ((const MbimMessage *)client->buffer, TRUE, &error)) { |
| 1187 |
/* No full message yet */ |
| 1188 |
if (g_error_matches (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INCOMPLETE_MESSAGE)) |
| 1189 |
return; |
| 1190 |
/* Invalid message */ |
| 1191 |
g_byte_array_remove_range (client->buffer, 0, client->buffer->len); |
| 1192 |
return; |
| 1193 |
} |
| 1194 |
|
| 1195 |
message = mbim_message_dup ((const MbimMessage *)client->buffer); |
| 1196 |
g_assert (message); |
| 1197 |
|
| 1198 |
g_byte_array_remove_range (client->buffer, 0, mbim_message_get_message_length (message)); |
| 1199 |
process_message (self, client, message); |
| 1200 |
} while (client->buffer->len > 0); |
| 1201 |
} |
| 1202 |
|
| 1203 |
static gboolean |
| 1204 |
connection_readable_cb (GSocket *socket, |
| 1205 |
GIOCondition condition, |
| 1206 |
Client *_client) |
| 1207 |
{ |
| 1208 |
g_autoptr(Client) client = NULL; |
| 1209 |
MbimProxy *self; |
| 1210 |
guint8 buffer[BUFFER_SIZE]; |
| 1211 |
g_autoptr(GError) error = NULL; |
| 1212 |
gssize r; |
| 1213 |
|
| 1214 |
/* Recover proxy pointer soon */ |
| 1215 |
client = client_ref (_client); |
| 1216 |
self = client->self; |
| 1217 |
|
| 1218 |
if (condition & G_IO_HUP || condition & G_IO_ERR) { |
| 1219 |
untrack_client (self, client); |
| 1220 |
return FALSE; |
| 1221 |
} |
| 1222 |
|
| 1223 |
if (!(condition & G_IO_IN || condition & G_IO_PRI)) |
| 1224 |
return TRUE; |
| 1225 |
|
| 1226 |
r = g_input_stream_read (g_io_stream_get_input_stream (G_IO_STREAM (client->connection)), |
| 1227 |
buffer, |
| 1228 |
BUFFER_SIZE, |
| 1229 |
NULL, |
| 1230 |
&error); |
| 1231 |
if (r < 0) { |
| 1232 |
g_warning ("[client %lu] error reading from istream: %s", client->id, error ? error->message : "unknown"); |
| 1233 |
/* Close the device */ |
| 1234 |
untrack_client (self, client); |
| 1235 |
return FALSE; |
| 1236 |
} |
| 1237 |
|
| 1238 |
if (r == 0) |
| 1239 |
return TRUE; |
| 1240 |
|
| 1241 |
/* else, r > 0 */ |
| 1242 |
if (!G_UNLIKELY (client->buffer)) |
| 1243 |
client->buffer = g_byte_array_sized_new (r); |
| 1244 |
g_byte_array_append (client->buffer, buffer, r); |
| 1245 |
|
| 1246 |
/* Try to parse input messages */ |
| 1247 |
parse_request (self, client); |
| 1248 |
|
| 1249 |
return TRUE; |
| 1250 |
} |
| 1251 |
|
| 1252 |
static void |
| 1253 |
incoming_cb (GSocketService *service, |
| 1254 |
GSocketConnection *connection, |
| 1255 |
GObject *unused, |
| 1256 |
MbimProxy *self) |
| 1257 |
{ |
| 1258 |
static gulong client_id = 0; |
| 1259 |
Client *client; |
| 1260 |
g_autoptr(GCredentials) credentials = NULL; |
| 1261 |
g_autoptr(GError) error = NULL; |
| 1262 |
g_autoptr(GError) gr_error = NULL; |
| 1263 |
uid_t uid; |
| 1264 |
|
| 1265 |
/* Each new incoming request updates the client id, even if the request is |
| 1266 |
* not accepted */ |
| 1267 |
client_id++; |
| 1268 |
|
| 1269 |
g_debug ("[client %lu] connection open...", client_id); |
| 1270 |
|
| 1271 |
credentials = g_socket_get_credentials (g_socket_connection_get_socket (connection), &error); |
| 1272 |
if (!credentials) { |
| 1273 |
g_warning ("[client %lu] not allowed: error getting socket credentials: %s", client_id, error->message); |
| 1274 |
return; |
| 1275 |
} |
| 1276 |
|
| 1277 |
uid = g_credentials_get_unix_user (credentials, &error); |
| 1278 |
if (error) { |
| 1279 |
g_warning ("[client %lu] not allowed: error getting unix user id: %s", client_id, error->message); |
| 1280 |
return; |
| 1281 |
} |
| 1282 |
|
| 1283 |
if (!mbim_helpers_check_user_allowed (uid, &error)) { |
| 1284 |
if (!mbim_helpers_check_group_allowed (uid, &gr_error)) { |
| 1285 |
if (gr_error) |
| 1286 |
g_warning ("[client %lu] group not allowed: %s", client_id, gr_error->message); |
| 1287 |
else |
| 1288 |
g_warning ("[client %lu] not allowed: %s", client_id, error->message); |
| 1289 |
return; |
| 1290 |
} |
| 1291 |
|
| 1292 |
g_debug ("[client %lu] member of allowed group", client_id); |
| 1293 |
} |
| 1294 |
|
| 1295 |
/* Create client */ |
| 1296 |
client = g_slice_new0 (Client); |
| 1297 |
client->self = self; |
| 1298 |
client->ref_count = 1; |
| 1299 |
client->id = client_id; |
| 1300 |
client->connection = g_object_ref (connection); |
| 1301 |
|
| 1302 |
/* By default, a new client has all the standard services enabled for indications */ |
| 1303 |
client->mbim_event_entry_array = _mbim_proxy_helper_service_subscribe_list_new_standard (&client->mbim_event_entry_array_size); |
| 1304 |
|
| 1305 |
client->connection_readable_source = g_socket_create_source (g_socket_connection_get_socket (client->connection), |
| 1306 |
G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP, |
| 1307 |
NULL); |
| 1308 |
g_source_set_callback (client->connection_readable_source, |
| 1309 |
(GSourceFunc)connection_readable_cb, |
| 1310 |
client, |
| 1311 |
NULL); |
| 1312 |
g_source_attach (client->connection_readable_source, g_main_context_get_thread_default ()); |
| 1313 |
|
| 1314 |
/* Keep the client info around */ |
| 1315 |
track_client (self, client); |
| 1316 |
|
| 1317 |
client_unref (client); |
| 1318 |
} |
| 1319 |
|
| 1320 |
static gboolean |
| 1321 |
setup_socket_service (MbimProxy *self, |
| 1322 |
GError **error) |
| 1323 |
{ |
| 1324 |
g_autoptr(GSocketAddress) socket_address = NULL; |
| 1325 |
g_autoptr(GSocket) socket = NULL; |
| 1326 |
|
| 1327 |
socket = g_socket_new (G_SOCKET_FAMILY_UNIX, |
| 1328 |
G_SOCKET_TYPE_STREAM, |
| 1329 |
G_SOCKET_PROTOCOL_DEFAULT, |
| 1330 |
error); |
| 1331 |
if (!socket) |
| 1332 |
return FALSE; |
| 1333 |
|
| 1334 |
/* Bind to address */ |
| 1335 |
socket_address = (g_unix_socket_address_new_with_type ( |
| 1336 |
MBIM_PROXY_SOCKET_PATH, |
| 1337 |
-1, |
| 1338 |
G_UNIX_SOCKET_ADDRESS_ABSTRACT)); |
| 1339 |
if (!g_socket_bind (socket, socket_address, TRUE, error)) |
| 1340 |
return FALSE; |
| 1341 |
|
| 1342 |
g_debug ("creating UNIX socket service..."); |
| 1343 |
|
| 1344 |
/* Listen */ |
| 1345 |
if (!g_socket_listen (socket, error)) |
| 1346 |
return FALSE; |
| 1347 |
|
| 1348 |
/* Create socket service */ |
| 1349 |
self->priv->socket_service = g_socket_service_new (); |
| 1350 |
g_signal_connect (self->priv->socket_service, "incoming", G_CALLBACK (incoming_cb), self); |
| 1351 |
if (!g_socket_listener_add_socket (G_SOCKET_LISTENER (self->priv->socket_service), |
| 1352 |
socket, |
| 1353 |
NULL, /* don't pass an object, will take a reference */ |
| 1354 |
error)) { |
| 1355 |
g_prefix_error (error, "Error adding socket at '%s' to socket service: ", MBIM_PROXY_SOCKET_PATH); |
| 1356 |
return FALSE; |
| 1357 |
} |
| 1358 |
|
| 1359 |
g_debug ("starting UNIX socket service at '%s'...", MBIM_PROXY_SOCKET_PATH); |
| 1360 |
g_socket_service_start (self->priv->socket_service); |
| 1361 |
return TRUE; |
| 1362 |
} |
| 1363 |
|
| 1364 |
/*****************************************************************************/ |
| 1365 |
/* Device tracking */ |
| 1366 |
|
| 1367 |
#define DEVICE_CONTEXT_TAG "device-context-tag" |
| 1368 |
static GQuark device_context_quark; |
| 1369 |
|
| 1370 |
typedef struct { |
| 1371 |
/* Combined events array */ |
| 1372 |
MbimEventEntry **mbim_event_entry_array; |
| 1373 |
gsize mbim_event_entry_array_size; |
| 1374 |
} DeviceContext; |
| 1375 |
|
| 1376 |
static void |
| 1377 |
device_context_free (DeviceContext *ctx) |
| 1378 |
{ |
| 1379 |
mbim_event_entry_array_free (ctx->mbim_event_entry_array); |
| 1380 |
g_slice_free (DeviceContext, ctx); |
| 1381 |
} |
| 1382 |
|
| 1383 |
static DeviceContext * |
| 1384 |
device_context_get (MbimDevice *device) |
| 1385 |
{ |
| 1386 |
DeviceContext *ctx; |
| 1387 |
|
| 1388 |
if (G_UNLIKELY (!device_context_quark)) |
| 1389 |
device_context_quark = g_quark_from_static_string (DEVICE_CONTEXT_TAG); |
| 1390 |
|
| 1391 |
ctx = g_object_get_qdata (G_OBJECT (device), device_context_quark); |
| 1392 |
if (!ctx) { |
| 1393 |
ctx = g_slice_new0 (DeviceContext); |
| 1394 |
ctx->mbim_event_entry_array = _mbim_proxy_helper_service_subscribe_list_new_standard (&ctx->mbim_event_entry_array_size); |
| 1395 |
|
| 1396 |
g_debug ("[%s] initial device subscribe list...", mbim_device_get_path (device)); |
| 1397 |
_mbim_proxy_helper_service_subscribe_list_debug ((const MbimEventEntry * const *)ctx->mbim_event_entry_array, ctx->mbim_event_entry_array_size); |
| 1398 |
|
| 1399 |
g_object_set_qdata_full (G_OBJECT (device), device_context_quark, ctx, (GDestroyNotify)device_context_free); |
| 1400 |
} |
| 1401 |
|
| 1402 |
return ctx; |
| 1403 |
} |
| 1404 |
|
| 1405 |
static MbimEventEntry ** |
| 1406 |
merge_client_service_subscribe_lists (MbimProxy *self, |
| 1407 |
MbimDevice *device, |
| 1408 |
gsize *out_size) |
| 1409 |
{ |
| 1410 |
GList *l; |
| 1411 |
g_autoptr(MbimEventEntryArray) updated = NULL; |
| 1412 |
gsize updated_size = 0; |
| 1413 |
DeviceContext *ctx; |
| 1414 |
|
| 1415 |
g_debug ("[%s] merging client service subscribe lists...", mbim_device_get_path (device)); |
| 1416 |
ctx = device_context_get (device); |
| 1417 |
g_assert (ctx); |
| 1418 |
|
| 1419 |
g_assert (out_size != NULL); |
| 1420 |
|
| 1421 |
/* Init default list */ |
| 1422 |
updated = _mbim_proxy_helper_service_subscribe_list_new_standard (&updated_size); |
| 1423 |
|
| 1424 |
/* Lookup all clients with this device */ |
| 1425 |
for (l = self->priv->clients; l; l = g_list_next (l)) { |
| 1426 |
Client *client; |
| 1427 |
|
| 1428 |
client = l->data; |
| 1429 |
if (!client->mbim_event_entry_array) |
| 1430 |
continue; |
| 1431 |
|
| 1432 |
/* Add per-client list */ |
| 1433 |
if (client->device == device) |
| 1434 |
updated = _mbim_proxy_helper_service_subscribe_list_merge (updated, updated_size, |
| 1435 |
client->mbim_event_entry_array, client->mbim_event_entry_array_size, |
| 1436 |
&updated_size); |
| 1437 |
} |
| 1438 |
|
| 1439 |
/* If lists are equal, ignore re-setting them up */ |
| 1440 |
if (_mbim_proxy_helper_service_subscribe_list_cmp ( |
| 1441 |
(const MbimEventEntry *const *)updated, updated_size, |
| 1442 |
(const MbimEventEntry *const *)ctx->mbim_event_entry_array, ctx->mbim_event_entry_array_size)) { |
| 1443 |
g_debug ("[%s] merged service subscribe list not updated", mbim_device_get_path (device)); |
| 1444 |
return NULL; |
| 1445 |
} |
| 1446 |
|
| 1447 |
/* Lists are different, update stored one */ |
| 1448 |
g_clear_pointer (&ctx->mbim_event_entry_array, mbim_event_entry_array_free); |
| 1449 |
ctx->mbim_event_entry_array = g_steal_pointer (&updated); |
| 1450 |
ctx->mbim_event_entry_array_size = updated_size; |
| 1451 |
|
| 1452 |
if (mbim_utils_get_traces_enabled ()) { |
| 1453 |
g_debug ("[%s] merged service subscribe list built", mbim_device_get_path (device)); |
| 1454 |
_mbim_proxy_helper_service_subscribe_list_debug ((const MbimEventEntry * const *)ctx->mbim_event_entry_array, |
| 1455 |
ctx->mbim_event_entry_array_size); |
| 1456 |
} |
| 1457 |
|
| 1458 |
*out_size = ctx->mbim_event_entry_array_size; |
| 1459 |
return ctx->mbim_event_entry_array; |
| 1460 |
} |
| 1461 |
|
| 1462 |
static void |
| 1463 |
reset_client_service_subscribe_lists (MbimProxy *self, |
| 1464 |
MbimDevice *device) |
| 1465 |
{ |
| 1466 |
DeviceContext *ctx; |
| 1467 |
GList *l; |
| 1468 |
|
| 1469 |
g_debug ("[%s] reseting client service subscribe lists...", mbim_device_get_path (device)); |
| 1470 |
ctx = device_context_get (device); |
| 1471 |
g_assert (ctx); |
| 1472 |
|
| 1473 |
/* make sure that all clients of this device don't track any event registered */ |
| 1474 |
for (l = self->priv->clients; l; l = g_list_next (l)) { |
| 1475 |
Client *client; |
| 1476 |
|
| 1477 |
client = l->data; |
| 1478 |
if (!client->mbim_event_entry_array) |
| 1479 |
continue; |
| 1480 |
|
| 1481 |
if (client->device == device) { |
| 1482 |
g_clear_pointer (&client->mbim_event_entry_array, mbim_event_entry_array_free); |
| 1483 |
client->mbim_event_entry_array = _mbim_proxy_helper_service_subscribe_list_new_standard (&client->mbim_event_entry_array_size); |
| 1484 |
} |
| 1485 |
} |
| 1486 |
|
| 1487 |
/* And reset the device-specific merged list */ |
| 1488 |
g_clear_pointer (&ctx->mbim_event_entry_array, mbim_event_entry_array_free); |
| 1489 |
ctx->mbim_event_entry_array = _mbim_proxy_helper_service_subscribe_list_new_standard (&ctx->mbim_event_entry_array_size); |
| 1490 |
} |
| 1491 |
|
| 1492 |
static void |
| 1493 |
proxy_device_error_cb (MbimDevice *device, |
| 1494 |
GError *error, |
| 1495 |
MbimProxy *self) |
| 1496 |
{ |
| 1497 |
if (g_error_matches (error, MBIM_PROTOCOL_ERROR, MBIM_PROTOCOL_ERROR_NOT_OPENED)) { |
| 1498 |
g_debug ("[%s] reports as being closed...", mbim_device_get_path (device)); |
| 1499 |
reset_client_service_subscribe_lists (self, device); |
| 1500 |
mbim_device_close_force (device, NULL); |
| 1501 |
} |
| 1502 |
} |
| 1503 |
|
| 1504 |
static MbimDevice * |
| 1505 |
peek_device_for_path (MbimProxy *self, |
| 1506 |
const gchar *path) |
| 1507 |
{ |
| 1508 |
GList *l; |
| 1509 |
|
| 1510 |
for (l = self->priv->devices; l; l = g_list_next (l)) { |
| 1511 |
/* Return if found */ |
| 1512 |
if (g_str_equal (mbim_device_get_path ((MbimDevice *)l->data), path)) |
| 1513 |
return (MbimDevice *)l->data; |
| 1514 |
} |
| 1515 |
|
| 1516 |
return NULL; |
| 1517 |
} |
| 1518 |
|
| 1519 |
static void |
| 1520 |
proxy_device_removed_cb (MbimDevice *device, |
| 1521 |
MbimProxy *self) |
| 1522 |
{ |
| 1523 |
untrack_device (self, device); |
| 1524 |
} |
| 1525 |
|
| 1526 |
static void |
| 1527 |
untrack_device (MbimProxy *self, |
| 1528 |
MbimDevice *device) |
| 1529 |
{ |
| 1530 |
GList *l; |
| 1531 |
GList *to_remove = NULL; |
| 1532 |
|
| 1533 |
g_debug ("[%s] untracking device...", mbim_device_get_path (device)); |
| 1534 |
|
| 1535 |
if (!g_list_find (self->priv->devices, device)) |
| 1536 |
return; |
| 1537 |
|
| 1538 |
/* Disconnect right away */ |
| 1539 |
g_signal_handlers_disconnect_by_func (device, proxy_device_error_cb, self); |
| 1540 |
g_signal_handlers_disconnect_by_func (device, proxy_device_removed_cb, self); |
| 1541 |
|
| 1542 |
/* If pending openings ongoing, complete them with error */ |
| 1543 |
cancel_opening_device (self, device); |
| 1544 |
|
| 1545 |
/* Lookup all clients with this device */ |
| 1546 |
for (l = self->priv->clients; l; l = g_list_next (l)) { |
| 1547 |
if (((Client *)(l->data))->device == device) |
| 1548 |
to_remove = g_list_append (to_remove, l->data); |
| 1549 |
} |
| 1550 |
|
| 1551 |
/* Remove all these clients */ |
| 1552 |
for (l = to_remove; l; l = g_list_next (l)) |
| 1553 |
untrack_client (self, (Client *)(l->data)); |
| 1554 |
g_list_free (to_remove); |
| 1555 |
|
| 1556 |
/* And finally, remove the device */ |
| 1557 |
self->priv->devices = g_list_remove (self->priv->devices, device); |
| 1558 |
g_object_unref (device); |
| 1559 |
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_N_DEVICES]); |
| 1560 |
} |
| 1561 |
|
| 1562 |
static void |
| 1563 |
track_device (MbimProxy *self, |
| 1564 |
MbimDevice *device) |
| 1565 |
{ |
| 1566 |
g_signal_connect (device, |
| 1567 |
MBIM_DEVICE_SIGNAL_REMOVED, |
| 1568 |
G_CALLBACK (proxy_device_removed_cb), |
| 1569 |
self); |
| 1570 |
|
| 1571 |
g_signal_connect (device, |
| 1572 |
MBIM_DEVICE_SIGNAL_ERROR, |
| 1573 |
G_CALLBACK (proxy_device_error_cb), |
| 1574 |
self); |
| 1575 |
|
| 1576 |
self->priv->devices = g_list_append (self->priv->devices, g_object_ref (device)); |
| 1577 |
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_N_DEVICES]); |
| 1578 |
} |
| 1579 |
|
| 1580 |
/*****************************************************************************/ |
| 1581 |
|
| 1582 |
MbimProxy * |
| 1583 |
mbim_proxy_new (GError **error) |
| 1584 |
{ |
| 1585 |
g_autoptr(MbimProxy) self = NULL; |
| 1586 |
uid_t uid; |
| 1587 |
|
| 1588 |
uid = getuid (); |
| 1589 |
|
| 1590 |
/* Allow proxy creation if one of [uid, group] are allowed */ |
| 1591 |
if (!mbim_helpers_check_user_allowed (uid, error)) { |
| 1592 |
g_clear_error (error); |
| 1593 |
|
| 1594 |
if (!mbim_helpers_check_group_allowed (uid, error)) |
| 1595 |
return NULL; |
| 1596 |
} |
| 1597 |
|
| 1598 |
self = g_object_new (MBIM_TYPE_PROXY, NULL); |
| 1599 |
if (!setup_socket_service (self, error)) |
| 1600 |
return NULL; |
| 1601 |
|
| 1602 |
return g_steal_pointer (&self); |
| 1603 |
} |
| 1604 |
|
| 1605 |
static void |
| 1606 |
mbim_proxy_init (MbimProxy *self) |
| 1607 |
{ |
| 1608 |
self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, MBIM_TYPE_PROXY, MbimProxyPrivate); |
| 1609 |
} |
| 1610 |
|
| 1611 |
static void |
| 1612 |
get_property (GObject *object, |
| 1613 |
guint prop_id, |
| 1614 |
GValue *value, |
| 1615 |
GParamSpec *pspec) |
| 1616 |
{ |
| 1617 |
MbimProxy *self = MBIM_PROXY (object); |
| 1618 |
|
| 1619 |
switch (prop_id) { |
| 1620 |
case PROP_N_CLIENTS: |
| 1621 |
g_value_set_uint (value, g_list_length (self->priv->clients)); |
| 1622 |
break; |
| 1623 |
case PROP_N_DEVICES: |
| 1624 |
g_value_set_uint (value, g_list_length (self->priv->devices)); |
| 1625 |
break; |
| 1626 |
default: |
| 1627 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 1628 |
break; |
| 1629 |
} |
| 1630 |
} |
| 1631 |
|
| 1632 |
static void |
| 1633 |
dispose (GObject *object) |
| 1634 |
{ |
| 1635 |
MbimProxyPrivate *priv = MBIM_PROXY (object)->priv; |
| 1636 |
|
| 1637 |
/* This list should always be empty when disposing */ |
| 1638 |
g_assert (priv->opening_devices == NULL); |
| 1639 |
|
| 1640 |
if (priv->clients) { |
| 1641 |
g_list_free_full (priv->clients, (GDestroyNotify) client_unref); |
| 1642 |
priv->clients = NULL; |
| 1643 |
} |
| 1644 |
|
| 1645 |
if (priv->devices) { |
| 1646 |
g_list_free_full (priv->devices, g_object_unref); |
| 1647 |
priv->devices = NULL; |
| 1648 |
} |
| 1649 |
|
| 1650 |
if (priv->socket_service) { |
| 1651 |
if (g_socket_service_is_active (priv->socket_service)) |
| 1652 |
g_socket_service_stop (priv->socket_service); |
| 1653 |
g_clear_object (&priv->socket_service); |
| 1654 |
g_debug ("UNIX socket service at '%s' stopped", MBIM_PROXY_SOCKET_PATH); |
| 1655 |
} |
| 1656 |
|
| 1657 |
G_OBJECT_CLASS (mbim_proxy_parent_class)->dispose (object); |
| 1658 |
} |
| 1659 |
|
| 1660 |
static void |
| 1661 |
mbim_proxy_class_init (MbimProxyClass *proxy_class) |
| 1662 |
{ |
| 1663 |
GObjectClass *object_class = G_OBJECT_CLASS (proxy_class); |
| 1664 |
|
| 1665 |
g_type_class_add_private (object_class, sizeof (MbimProxyPrivate)); |
| 1666 |
|
| 1667 |
/* Virtual methods */ |
| 1668 |
object_class->get_property = get_property; |
| 1669 |
object_class->dispose = dispose; |
| 1670 |
|
| 1671 |
/** |
| 1672 |
* MbimProxy:mbim-proxy-n-clients |
| 1673 |
* |
| 1674 |
* Since: 1.10 |
| 1675 |
*/ |
| 1676 |
properties[PROP_N_CLIENTS] = |
| 1677 |
g_param_spec_uint (MBIM_PROXY_N_CLIENTS, |
| 1678 |
"Number of clients", |
| 1679 |
"Number of clients currently connected to the proxy", |
| 1680 |
0, |
| 1681 |
G_MAXUINT, |
| 1682 |
0, |
| 1683 |
G_PARAM_READABLE); |
| 1684 |
g_object_class_install_property (object_class, PROP_N_CLIENTS, properties[PROP_N_CLIENTS]); |
| 1685 |
|
| 1686 |
/** |
| 1687 |
* MbimProxy:mbim-proxy-n-devices |
| 1688 |
* |
| 1689 |
* Since: 1.10 |
| 1690 |
*/ |
| 1691 |
properties[PROP_N_DEVICES] = |
| 1692 |
g_param_spec_uint (MBIM_PROXY_N_DEVICES, |
| 1693 |
"Number of devices", |
| 1694 |
"Number of devices currently managed by the proxy", |
| 1695 |
0, |
| 1696 |
G_MAXUINT, |
| 1697 |
0, |
| 1698 |
G_PARAM_READABLE); |
| 1699 |
g_object_class_install_property (object_class, PROP_N_DEVICES, properties[PROP_N_DEVICES]); |
| 1700 |
} |