| 1 |
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 3 |
/* |
| 4 |
* libmbim-glib -- GLib/GIO based library to control MBIM devices |
| 5 |
* |
| 6 |
* Copyright (C) 2013 - 2022 Aleksander Morgado <[email protected]> |
| 7 |
* Copyright (C) 2022 Google, Inc. |
| 8 |
* Copyright (C) 2022 Intel Corporation |
| 9 |
*/ |
| 10 |
|
| 11 |
#include <glib.h> |
| 12 |
#include <stdint.h> |
| 13 |
#include <stdio.h> |
| 14 |
#include <string.h> |
| 15 |
#include <endian.h> |
| 16 |
|
| 17 |
#include "mbim-message.h" |
| 18 |
#include "mbim-message-private.h" |
| 19 |
#include "mbim-error-types.h" |
| 20 |
#include "mbim-enum-types.h" |
| 21 |
#include "mbim-tlv-private.h" |
| 22 |
#include "mbim-helpers.h" |
| 23 |
|
| 24 |
#include "mbim-basic-connect.h" |
| 25 |
#include "mbim-auth.h" |
| 26 |
#include "mbim-dss.h" |
| 27 |
#include "mbim-phonebook.h" |
| 28 |
#include "mbim-sms.h" |
| 29 |
#include "mbim-stk.h" |
| 30 |
#include "mbim-ussd.h" |
| 31 |
#include "mbim-proxy-control.h" |
| 32 |
#include "mbim-qmi.h" |
| 33 |
#include "mbim-ms-firmware-id.h" |
| 34 |
#include "mbim-ms-host-shutdown.h" |
| 35 |
#include "mbim-ms-sar.h" |
| 36 |
#include "mbim-atds.h" |
| 37 |
#include "mbim-intel-firmware-update.h" |
| 38 |
#include "mbim-qdu.h" |
| 39 |
#include "mbim-ms-basic-connect-extensions.h" |
| 40 |
#include "mbim-ms-uicc-low-level-access.h" |
| 41 |
#include "mbim-quectel.h" |
| 42 |
#include "mbim-intel-thermal-rf.h" |
| 43 |
#include "mbim-ms-voice-extensions.h" |
| 44 |
#include "mbim-intel-mutual-authentication.h" |
| 45 |
#include "mbim-intel-tools.h" |
| 46 |
#include "mbim-google.h" |
| 47 |
#include "mbim-fibocom.h" |
| 48 |
#include "mbim-compal.h" |
| 49 |
#include "mbim-intel-at-tunnel.h" |
| 50 |
|
| 51 |
/*****************************************************************************/ |
| 52 |
|
| 53 |
#define MBIM_MESSAGE_IS_FRAGMENT(self) \ |
| 54 |
(MBIM_MESSAGE_GET_MESSAGE_TYPE (self) == MBIM_MESSAGE_TYPE_COMMAND || \ |
| 55 |
MBIM_MESSAGE_GET_MESSAGE_TYPE (self) == MBIM_MESSAGE_TYPE_COMMAND_DONE || \ |
| 56 |
MBIM_MESSAGE_GET_MESSAGE_TYPE (self) == MBIM_MESSAGE_TYPE_INDICATE_STATUS) |
| 57 |
|
| 58 |
#define MBIM_MESSAGE_FRAGMENT_GET_TOTAL(self) \ |
| 59 |
GUINT32_FROM_LE (((struct full_message *)(self->data))->message.fragment.fragment_header.total) |
| 60 |
|
| 61 |
#define MBIM_MESSAGE_FRAGMENT_GET_CURRENT(self) \ |
| 62 |
GUINT32_FROM_LE (((struct full_message *)(self->data))->message.fragment.fragment_header.current) |
| 63 |
|
| 64 |
static void |
| 65 |
bytearray_apply_padding (GByteArray *buffer, |
| 66 |
guint32 *len) |
| 67 |
{ |
| 68 |
static const guint8 padding = 0; |
| 69 |
|
| 70 |
g_assert (buffer); |
| 71 |
g_assert (len); |
| 72 |
|
| 73 |
/* Apply padding to the requested length until multiple of 4 */ |
| 74 |
while (*len % 4 != 0) { |
| 75 |
g_byte_array_append (buffer, &padding, 1); |
| 76 |
(*len)++; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
static void |
| 81 |
set_error_from_status (GError **error, |
| 82 |
MbimStatusError status) |
| 83 |
{ |
| 84 |
const gchar *error_string; |
| 85 |
|
| 86 |
error_string = mbim_status_error_get_string (status); |
| 87 |
if (error_string) |
| 88 |
g_set_error_literal (error, |
| 89 |
MBIM_STATUS_ERROR, |
| 90 |
status, |
| 91 |
error_string); |
| 92 |
else |
| 93 |
g_set_error (error, |
| 94 |
MBIM_STATUS_ERROR, |
| 95 |
status, |
| 96 |
"Unknown status 0x%08x", |
| 97 |
status); |
| 98 |
} |
| 99 |
|
| 100 |
/*****************************************************************************/ |
| 101 |
|
| 102 |
GType |
| 103 |
mbim_message_get_type (void) |
| 104 |
{ |
| 105 |
static gsize g_define_type_id_initialized = 0; |
| 106 |
|
| 107 |
if (g_once_init_enter (&g_define_type_id_initialized)) { |
| 108 |
GType g_define_type_id = |
| 109 |
g_boxed_type_register_static (g_intern_static_string ("MbimMessage"), |
| 110 |
(GBoxedCopyFunc) mbim_message_ref, |
| 111 |
(GBoxedFreeFunc) mbim_message_unref); |
| 112 |
|
| 113 |
g_once_init_leave (&g_define_type_id_initialized, g_define_type_id); |
| 114 |
} |
| 115 |
|
| 116 |
return g_define_type_id_initialized; |
| 117 |
} |
| 118 |
|
| 119 |
/*****************************************************************************/ |
| 120 |
|
| 121 |
GByteArray * |
| 122 |
_mbim_message_allocate (MbimMessageType message_type, |
| 123 |
guint32 transaction_id, |
| 124 |
guint32 additional_size) |
| 125 |
{ |
| 126 |
GByteArray *self; |
| 127 |
guint32 len; |
| 128 |
|
| 129 |
/* Compute size of the basic empty message and allocate heap for it */ |
| 130 |
len = sizeof (struct header) + additional_size; |
| 131 |
self = g_byte_array_sized_new (len); |
| 132 |
g_byte_array_set_size (self, len); |
| 133 |
|
| 134 |
/* Set MBIM header */ |
| 135 |
((struct header *)(self->data))->type = GUINT32_TO_LE (message_type); |
| 136 |
((struct header *)(self->data))->length = GUINT32_TO_LE (len); |
| 137 |
((struct header *)(self->data))->transaction_id = GUINT32_TO_LE (transaction_id); |
| 138 |
|
| 139 |
return self; |
| 140 |
} |
| 141 |
|
| 142 |
/*****************************************************************************/ |
| 143 |
|
| 144 |
static gboolean |
| 145 |
_mbim_message_validate_generic_header (const MbimMessage *self, |
| 146 |
GError **error) |
| 147 |
{ |
| 148 |
/* Validate that the generic header can be read before reading the message |
| 149 |
* type and length */ |
| 150 |
if (self->len < sizeof (struct header)) { |
| 151 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INCOMPLETE_MESSAGE, |
| 152 |
"Message is shorter than the minimum header (%u < %u)", |
| 153 |
self->len, (guint) sizeof (struct header)); |
| 154 |
return FALSE; |
| 155 |
} |
| 156 |
|
| 157 |
/* Validate that the size reported in the message header is available */ |
| 158 |
if (self->len < MBIM_MESSAGE_GET_MESSAGE_LENGTH (self)) { |
| 159 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INCOMPLETE_MESSAGE, |
| 160 |
"Message is incomplete (%u < %u)", |
| 161 |
self->len, MBIM_MESSAGE_GET_MESSAGE_LENGTH (self)); |
| 162 |
return FALSE; |
| 163 |
} |
| 164 |
|
| 165 |
return TRUE; |
| 166 |
} |
| 167 |
|
| 168 |
static gboolean |
| 169 |
_mbim_message_validate_type_header (const MbimMessage *self, |
| 170 |
GError **error) |
| 171 |
{ |
| 172 |
gsize message_header_size = 0; |
| 173 |
|
| 174 |
if (!_mbim_message_validate_generic_header (self, error)) |
| 175 |
return FALSE; |
| 176 |
|
| 177 |
/* Validate message type and get additional minimum required size based on |
| 178 |
* message type. At this stage, we only check the fragment header validity for |
| 179 |
* Command, Command Done and Indication messages, because only the 'complete' |
| 180 |
* fragment will have the additional header contents specific to each message |
| 181 |
* type. */ |
| 182 |
switch (MBIM_MESSAGE_GET_MESSAGE_TYPE (self)) { |
| 183 |
case MBIM_MESSAGE_TYPE_OPEN: |
| 184 |
message_header_size = sizeof (struct header) + sizeof (struct open_message); |
| 185 |
break; |
| 186 |
case MBIM_MESSAGE_TYPE_CLOSE: |
| 187 |
/* ignore check */ |
| 188 |
break; |
| 189 |
case MBIM_MESSAGE_TYPE_COMMAND: |
| 190 |
message_header_size = sizeof (struct header) + sizeof (struct fragment_header); |
| 191 |
break; |
| 192 |
case MBIM_MESSAGE_TYPE_OPEN_DONE: |
| 193 |
message_header_size = sizeof (struct header) + sizeof (struct open_done_message); |
| 194 |
break; |
| 195 |
case MBIM_MESSAGE_TYPE_CLOSE_DONE: |
| 196 |
message_header_size = sizeof (struct header) + sizeof (struct close_done_message); |
| 197 |
break; |
| 198 |
case MBIM_MESSAGE_TYPE_COMMAND_DONE: |
| 199 |
message_header_size = sizeof (struct header) + sizeof (struct fragment_header); |
| 200 |
break; |
| 201 |
case MBIM_MESSAGE_TYPE_FUNCTION_ERROR: |
| 202 |
case MBIM_MESSAGE_TYPE_HOST_ERROR: |
| 203 |
message_header_size = sizeof (struct header) + sizeof (struct error_message); |
| 204 |
break; |
| 205 |
case MBIM_MESSAGE_TYPE_INDICATE_STATUS: |
| 206 |
message_header_size = sizeof (struct header) + sizeof (struct fragment_header); |
| 207 |
break; |
| 208 |
default: |
| 209 |
case MBIM_MESSAGE_TYPE_INVALID: |
| 210 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 211 |
"Message type unknown: 0x%08x", MBIM_MESSAGE_GET_MESSAGE_TYPE (self)); |
| 212 |
return FALSE; |
| 213 |
} |
| 214 |
|
| 215 |
/* Validate that the message type specific header can be read. */ |
| 216 |
if (message_header_size && (MBIM_MESSAGE_GET_MESSAGE_LENGTH (self) < message_header_size)) { |
| 217 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 218 |
"Invalid message size: message type header incomplete"); |
| 219 |
return FALSE; |
| 220 |
} |
| 221 |
|
| 222 |
return TRUE; |
| 223 |
} |
| 224 |
|
| 225 |
static gboolean |
| 226 |
_mbim_message_validate_partial_fragment (const MbimMessage *self, |
| 227 |
GError **error) |
| 228 |
{ |
| 229 |
if (MBIM_MESSAGE_FRAGMENT_GET_CURRENT (self) >= MBIM_MESSAGE_FRAGMENT_GET_TOTAL (self)) { |
| 230 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 231 |
"Invalid message fragment (%u/%u)", |
| 232 |
MBIM_MESSAGE_FRAGMENT_GET_CURRENT (self), |
| 233 |
MBIM_MESSAGE_FRAGMENT_GET_TOTAL (self)); |
| 234 |
return FALSE; |
| 235 |
} |
| 236 |
|
| 237 |
return TRUE; |
| 238 |
} |
| 239 |
|
| 240 |
static gboolean |
| 241 |
_mbim_message_validate_complete_fragment (const MbimMessage *self, |
| 242 |
GError **error) |
| 243 |
{ |
| 244 |
gsize message_size = 0; |
| 245 |
gsize message_header_size = 0; |
| 246 |
|
| 247 |
if (MBIM_MESSAGE_FRAGMENT_GET_CURRENT (self) != 0) { |
| 248 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 249 |
"Invalid current fragment in complete message"); |
| 250 |
return FALSE; |
| 251 |
} |
| 252 |
|
| 253 |
/* Before reading the information buffer length we must validate that the |
| 254 |
* message type header is readable. */ |
| 255 |
switch (MBIM_MESSAGE_GET_MESSAGE_TYPE (self)) { |
| 256 |
case MBIM_MESSAGE_TYPE_COMMAND: |
| 257 |
message_header_size = sizeof (struct header) + sizeof (struct command_message); |
| 258 |
break; |
| 259 |
case MBIM_MESSAGE_TYPE_COMMAND_DONE: |
| 260 |
message_header_size = sizeof (struct header) + sizeof (struct command_done_message); |
| 261 |
break; |
| 262 |
case MBIM_MESSAGE_TYPE_INDICATE_STATUS: |
| 263 |
message_header_size = sizeof (struct header) + sizeof (struct indicate_status_message); |
| 264 |
break; |
| 265 |
case MBIM_MESSAGE_TYPE_OPEN: |
| 266 |
case MBIM_MESSAGE_TYPE_CLOSE: |
| 267 |
case MBIM_MESSAGE_TYPE_HOST_ERROR: |
| 268 |
case MBIM_MESSAGE_TYPE_OPEN_DONE: |
| 269 |
case MBIM_MESSAGE_TYPE_CLOSE_DONE: |
| 270 |
case MBIM_MESSAGE_TYPE_FUNCTION_ERROR: |
| 271 |
case MBIM_MESSAGE_TYPE_INVALID: |
| 272 |
default: |
| 273 |
g_assert_not_reached (); |
| 274 |
break; |
| 275 |
} |
| 276 |
|
| 277 |
/* Validate that the message type specific header can be read. */ |
| 278 |
if (message_header_size && (MBIM_MESSAGE_GET_MESSAGE_LENGTH (self) < message_header_size)) { |
| 279 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 280 |
"Invalid message size: fragment type header incomplete"); |
| 281 |
return FALSE; |
| 282 |
} |
| 283 |
|
| 284 |
/* Get information buffer size */ |
| 285 |
switch (MBIM_MESSAGE_GET_MESSAGE_TYPE (self)) { |
| 286 |
case MBIM_MESSAGE_TYPE_COMMAND: |
| 287 |
message_size = sizeof (struct header) + sizeof (struct command_message) + |
| 288 |
GUINT32_FROM_LE (((struct full_message *)(self->data))->message.command.buffer_length); |
| 289 |
break; |
| 290 |
case MBIM_MESSAGE_TYPE_COMMAND_DONE: |
| 291 |
message_size = sizeof (struct header) + sizeof (struct command_done_message) + |
| 292 |
GUINT32_FROM_LE (((struct full_message *)(self->data))->message.command_done.buffer_length); |
| 293 |
break; |
| 294 |
case MBIM_MESSAGE_TYPE_INDICATE_STATUS: |
| 295 |
message_size = sizeof (struct header) + sizeof (struct indicate_status_message) + |
| 296 |
GUINT32_FROM_LE (((struct full_message *)(self->data))->message.indicate_status.buffer_length); |
| 297 |
break; |
| 298 |
case MBIM_MESSAGE_TYPE_OPEN: |
| 299 |
case MBIM_MESSAGE_TYPE_CLOSE: |
| 300 |
case MBIM_MESSAGE_TYPE_HOST_ERROR: |
| 301 |
case MBIM_MESSAGE_TYPE_OPEN_DONE: |
| 302 |
case MBIM_MESSAGE_TYPE_CLOSE_DONE: |
| 303 |
case MBIM_MESSAGE_TYPE_FUNCTION_ERROR: |
| 304 |
case MBIM_MESSAGE_TYPE_INVALID: |
| 305 |
default: |
| 306 |
g_assert_not_reached (); |
| 307 |
break; |
| 308 |
} |
| 309 |
|
| 310 |
/* The information buffer must fit within the message contents */ |
| 311 |
if (MBIM_MESSAGE_GET_MESSAGE_LENGTH (self) < message_size) { |
| 312 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 313 |
"Invalid complete fragment size: type header or information buffer incomplete (%u < %" G_GSIZE_FORMAT ")", |
| 314 |
MBIM_MESSAGE_GET_MESSAGE_LENGTH (self), message_size); |
| 315 |
return FALSE; |
| 316 |
} |
| 317 |
|
| 318 |
return TRUE; |
| 319 |
} |
| 320 |
|
| 321 |
gboolean |
| 322 |
_mbim_message_validate_internal (const MbimMessage *self, |
| 323 |
gboolean allow_fragment, |
| 324 |
GError **error) |
| 325 |
{ |
| 326 |
if (!_mbim_message_validate_type_header (self, error)) |
| 327 |
return FALSE; |
| 328 |
|
| 329 |
if (!MBIM_MESSAGE_IS_FRAGMENT (self)) |
| 330 |
return TRUE; |
| 331 |
|
| 332 |
if (MBIM_MESSAGE_FRAGMENT_GET_TOTAL (self) > 1) { |
| 333 |
if (allow_fragment) |
| 334 |
return _mbim_message_validate_partial_fragment (self, error); |
| 335 |
|
| 336 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 337 |
"Incomplete partial fragment message"); |
| 338 |
return FALSE; |
| 339 |
} |
| 340 |
|
| 341 |
if (MBIM_MESSAGE_FRAGMENT_GET_TOTAL (self) == 1) |
| 342 |
return _mbim_message_validate_complete_fragment (self, error); |
| 343 |
|
| 344 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 345 |
"Invalid total fragment number"); |
| 346 |
return FALSE; |
| 347 |
} |
| 348 |
|
| 349 |
gboolean |
| 350 |
mbim_message_validate (const MbimMessage *self, |
| 351 |
GError **error) |
| 352 |
{ |
| 353 |
/* No fragments allowed in the API message validation */ |
| 354 |
return _mbim_message_validate_internal (self, FALSE, error); |
| 355 |
} |
| 356 |
|
| 357 |
/*****************************************************************************/ |
| 358 |
|
| 359 |
static guint32 |
| 360 |
_mbim_message_get_information_buffer_offset (const MbimMessage *self) |
| 361 |
{ |
| 362 |
g_return_val_if_fail (MBIM_MESSAGE_GET_MESSAGE_TYPE (self) == MBIM_MESSAGE_TYPE_COMMAND || |
| 363 |
MBIM_MESSAGE_GET_MESSAGE_TYPE (self) == MBIM_MESSAGE_TYPE_COMMAND_DONE || |
| 364 |
MBIM_MESSAGE_GET_MESSAGE_TYPE (self) == MBIM_MESSAGE_TYPE_INDICATE_STATUS, 0); |
| 365 |
|
| 366 |
switch (MBIM_MESSAGE_GET_MESSAGE_TYPE (self)) { |
| 367 |
case MBIM_MESSAGE_TYPE_COMMAND: |
| 368 |
return (sizeof (struct header) + |
| 369 |
G_STRUCT_OFFSET (struct command_message, buffer)); |
| 370 |
|
| 371 |
case MBIM_MESSAGE_TYPE_COMMAND_DONE: |
| 372 |
return (sizeof (struct header) + |
| 373 |
G_STRUCT_OFFSET (struct command_done_message, buffer)); |
| 374 |
break; |
| 375 |
|
| 376 |
case MBIM_MESSAGE_TYPE_INDICATE_STATUS: |
| 377 |
return (sizeof (struct header) + |
| 378 |
G_STRUCT_OFFSET (struct indicate_status_message, buffer)); |
| 379 |
break; |
| 380 |
|
| 381 |
case MBIM_MESSAGE_TYPE_INVALID: |
| 382 |
case MBIM_MESSAGE_TYPE_OPEN: |
| 383 |
case MBIM_MESSAGE_TYPE_CLOSE: |
| 384 |
case MBIM_MESSAGE_TYPE_HOST_ERROR: |
| 385 |
case MBIM_MESSAGE_TYPE_OPEN_DONE: |
| 386 |
case MBIM_MESSAGE_TYPE_CLOSE_DONE: |
| 387 |
case MBIM_MESSAGE_TYPE_FUNCTION_ERROR: |
| 388 |
default: |
| 389 |
g_assert_not_reached (); |
| 390 |
return 0; |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
gboolean |
| 395 |
_mbim_message_read_guint16 (const MbimMessage *self, |
| 396 |
guint32 relative_offset, |
| 397 |
guint16 *value, |
| 398 |
GError **error) |
| 399 |
{ |
| 400 |
guint64 required_size; |
| 401 |
guint32 information_buffer_offset; |
| 402 |
|
| 403 |
g_assert (value); |
| 404 |
|
| 405 |
information_buffer_offset = _mbim_message_get_information_buffer_offset (self); |
| 406 |
|
| 407 |
required_size = (guint64)information_buffer_offset + (guint64)relative_offset + 2; |
| 408 |
if ((guint64)self->len < required_size) { |
| 409 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 410 |
"cannot read 16bit unsigned integer (2 bytes) (%u < %" G_GUINT64_FORMAT ")", |
| 411 |
self->len, required_size); |
| 412 |
return FALSE; |
| 413 |
} |
| 414 |
|
| 415 |
*value = mbim_helpers_read_unaligned_guint16 (self->data + information_buffer_offset + relative_offset); |
| 416 |
return TRUE; |
| 417 |
} |
| 418 |
|
| 419 |
gboolean |
| 420 |
_mbim_message_read_guint32 (const MbimMessage *self, |
| 421 |
guint32 relative_offset, |
| 422 |
guint32 *value, |
| 423 |
GError **error) |
| 424 |
{ |
| 425 |
guint64 required_size; |
| 426 |
guint32 information_buffer_offset; |
| 427 |
|
| 428 |
g_assert (value); |
| 429 |
|
| 430 |
information_buffer_offset = _mbim_message_get_information_buffer_offset (self); |
| 431 |
|
| 432 |
required_size = (guint64)information_buffer_offset + (guint64)relative_offset + 4; |
| 433 |
if ((guint64)self->len < required_size) { |
| 434 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 435 |
"cannot read 32bit unsigned integer (4 bytes) (%u < %" G_GUINT64_FORMAT ")", |
| 436 |
self->len, required_size); |
| 437 |
return FALSE; |
| 438 |
} |
| 439 |
|
| 440 |
*value = mbim_helpers_read_unaligned_guint32 (self->data + information_buffer_offset + relative_offset); |
| 441 |
return TRUE; |
| 442 |
} |
| 443 |
|
| 444 |
gboolean |
| 445 |
_mbim_message_read_gint32 (const MbimMessage *self, |
| 446 |
guint32 relative_offset, |
| 447 |
gint32 *value, |
| 448 |
GError **error) |
| 449 |
{ |
| 450 |
guint64 required_size; |
| 451 |
guint32 information_buffer_offset; |
| 452 |
|
| 453 |
g_assert (value); |
| 454 |
|
| 455 |
information_buffer_offset = _mbim_message_get_information_buffer_offset (self); |
| 456 |
|
| 457 |
required_size = (guint64)information_buffer_offset + (guint64)relative_offset + 4; |
| 458 |
if ((guint64)self->len < required_size) { |
| 459 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 460 |
"cannot read 32bit signed integer (4 bytes) (%u < %" G_GUINT64_FORMAT ")", |
| 461 |
self->len, required_size); |
| 462 |
return FALSE; |
| 463 |
} |
| 464 |
|
| 465 |
*value = mbim_helpers_read_unaligned_guint32 (self->data + information_buffer_offset + relative_offset); |
| 466 |
return TRUE; |
| 467 |
} |
| 468 |
|
| 469 |
gboolean |
| 470 |
_mbim_message_read_guint32_array (const MbimMessage *self, |
| 471 |
guint32 array_size, |
| 472 |
guint32 relative_offset_array_start, |
| 473 |
guint32 **array, |
| 474 |
GError **error) |
| 475 |
{ |
| 476 |
guint64 required_size; |
| 477 |
guint i; |
| 478 |
guint32 information_buffer_offset; |
| 479 |
|
| 480 |
g_assert (array != NULL); |
| 481 |
|
| 482 |
if (!array_size) { |
| 483 |
*array = NULL; |
| 484 |
return TRUE; |
| 485 |
} |
| 486 |
|
| 487 |
information_buffer_offset = _mbim_message_get_information_buffer_offset (self); |
| 488 |
|
| 489 |
required_size = (guint64)information_buffer_offset + (guint64)relative_offset_array_start + (4 * (guint64)array_size); |
| 490 |
if ((guint64)self->len < required_size) { |
| 491 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 492 |
"cannot read 32bit unsigned integer array (%" G_GUINT64_FORMAT " bytes) (%u < %" G_GUINT64_FORMAT ")", |
| 493 |
(4 * (guint64)array_size), self->len, (guint64)required_size); |
| 494 |
return FALSE; |
| 495 |
} |
| 496 |
|
| 497 |
*array = g_new (guint32, array_size + 1); |
| 498 |
for (i = 0; i < array_size; i++) |
| 499 |
(*array)[i] = mbim_helpers_read_unaligned_guint32 (self->data + information_buffer_offset + relative_offset_array_start + (4 * i)); |
| 500 |
(*array)[array_size] = 0; |
| 501 |
return TRUE; |
| 502 |
} |
| 503 |
|
| 504 |
gboolean |
| 505 |
_mbim_message_read_guint64 (const MbimMessage *self, |
| 506 |
guint32 relative_offset, |
| 507 |
guint64 *value, |
| 508 |
GError **error) |
| 509 |
{ |
| 510 |
guint64 required_size; |
| 511 |
guint32 information_buffer_offset; |
| 512 |
|
| 513 |
g_assert (value != NULL); |
| 514 |
|
| 515 |
information_buffer_offset = _mbim_message_get_information_buffer_offset (self); |
| 516 |
|
| 517 |
required_size = (guint64)information_buffer_offset + (guint64)relative_offset + 8; |
| 518 |
if ((guint64)self->len < required_size) { |
| 519 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 520 |
"cannot read 64bit unsigned integer (8 bytes) (%u < %" G_GUINT64_FORMAT ")", |
| 521 |
self->len, required_size); |
| 522 |
return FALSE; |
| 523 |
} |
| 524 |
|
| 525 |
*value = mbim_helpers_read_unaligned_guint64 (self->data + information_buffer_offset + relative_offset); |
| 526 |
return TRUE; |
| 527 |
} |
| 528 |
|
| 529 |
gboolean |
| 530 |
_mbim_message_read_string (const MbimMessage *self, |
| 531 |
guint32 struct_start_offset, |
| 532 |
guint32 relative_offset, |
| 533 |
MbimStringEncoding encoding, |
| 534 |
gchar **str, |
| 535 |
guint32 *bytes_read, |
| 536 |
GError **error) |
| 537 |
{ |
| 538 |
g_autofree gchar *tmp = NULL; |
| 539 |
guint64 required_size; |
| 540 |
guint32 offset; |
| 541 |
guint32 size; |
| 542 |
guint32 information_buffer_offset; |
| 543 |
|
| 544 |
information_buffer_offset = _mbim_message_get_information_buffer_offset (self); |
| 545 |
|
| 546 |
required_size = (guint64)information_buffer_offset + (guint64)relative_offset + 8; |
| 547 |
if ((guint64)self->len < required_size) { |
| 548 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 549 |
"cannot read string offset and size (%u < %" G_GUINT64_FORMAT ")", |
| 550 |
self->len, required_size); |
| 551 |
return FALSE; |
| 552 |
} |
| 553 |
|
| 554 |
offset = mbim_helpers_read_unaligned_guint32 (self->data + information_buffer_offset + relative_offset); |
| 555 |
size = mbim_helpers_read_unaligned_guint32 (self->data + information_buffer_offset + relative_offset + 4); |
| 556 |
if (!size) { |
| 557 |
*str = NULL; |
| 558 |
if (bytes_read) |
| 559 |
*bytes_read = 0; |
| 560 |
return TRUE; |
| 561 |
} |
| 562 |
|
| 563 |
if (bytes_read) |
| 564 |
*bytes_read = size; |
| 565 |
|
| 566 |
required_size = (guint64)information_buffer_offset + (guint64)struct_start_offset + (guint64)offset + (guint64)size; |
| 567 |
if ((guint64)self->len < required_size) { |
| 568 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 569 |
"cannot read string data (%u bytes) (%u < %" G_GUINT64_FORMAT ")", |
| 570 |
size, self->len, required_size); |
| 571 |
return FALSE; |
| 572 |
} |
| 573 |
|
| 574 |
if (encoding == MBIM_STRING_ENCODING_UTF16) { |
| 575 |
g_autofree gunichar2 *utf16d = NULL; |
| 576 |
|
| 577 |
/* Always duplicate to avoid memory alignment issues */ |
| 578 |
utf16d = g_memdup (self->data + information_buffer_offset + struct_start_offset + offset, size); |
| 579 |
|
| 580 |
/* For BE systems, convert from LE to BE */ |
| 581 |
if (G_BYTE_ORDER == G_BIG_ENDIAN) { |
| 582 |
guint i; |
| 583 |
|
| 584 |
for (i = 0; i < (size / 2); i++) |
| 585 |
utf16d[i] = GUINT16_FROM_LE (utf16d[i]); |
| 586 |
} |
| 587 |
|
| 588 |
tmp = g_utf16_to_utf8 (utf16d, size / 2, NULL, NULL, error); |
| 589 |
if (!tmp) { |
| 590 |
g_prefix_error (error, "Error converting string to UTF-8: "); |
| 591 |
return FALSE; |
| 592 |
} |
| 593 |
size = strlen (tmp); |
| 594 |
} else if (encoding == MBIM_STRING_ENCODING_UTF8) { |
| 595 |
const gchar *utf8; |
| 596 |
|
| 597 |
utf8 = (const gchar *) (self->data + information_buffer_offset + struct_start_offset + offset); |
| 598 |
|
| 599 |
/* size may include the trailing NUL byte, skip it from the check */ |
| 600 |
while (size > 0 && utf8[size - 1] == '\0') |
| 601 |
size--; |
| 602 |
|
| 603 |
tmp = g_strndup (utf8, size); |
| 604 |
} else |
| 605 |
g_assert_not_reached (); |
| 606 |
|
| 607 |
if (!g_utf8_validate (tmp, size, NULL)) { |
| 608 |
g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA, "Error validating UTF-8 string"); |
| 609 |
return FALSE; |
| 610 |
} |
| 611 |
|
| 612 |
*str = g_steal_pointer (&tmp); |
| 613 |
return TRUE; |
| 614 |
} |
| 615 |
|
| 616 |
gboolean |
| 617 |
_mbim_message_read_string_array (const MbimMessage *self, |
| 618 |
guint32 array_size, |
| 619 |
guint32 struct_start_offset, |
| 620 |
guint32 relative_offset_array_start, |
| 621 |
MbimStringEncoding encoding, |
| 622 |
gchar ***out_array, |
| 623 |
GError **error) |
| 624 |
{ |
| 625 |
guint32 offset; |
| 626 |
guint32 i; |
| 627 |
g_autoptr(GPtrArray) array = NULL; |
| 628 |
|
| 629 |
g_assert (out_array != NULL); |
| 630 |
|
| 631 |
if (!array_size) { |
| 632 |
*out_array = NULL; |
| 633 |
return TRUE; |
| 634 |
} |
| 635 |
|
| 636 |
array = g_ptr_array_new_with_free_func (g_free); |
| 637 |
for (i = 0, offset = relative_offset_array_start; i < array_size; offset += 8, i++) { |
| 638 |
gchar *str; |
| 639 |
|
| 640 |
/* Read next string in the OL pair list */ |
| 641 |
if (!_mbim_message_read_string (self, struct_start_offset, offset, encoding, &str, NULL, error)) |
| 642 |
return FALSE; |
| 643 |
|
| 644 |
/* When an empty string is given as part of the array, we don't want to |
| 645 |
* add the NULL pointer, we should be adding the empty string explicitly. |
| 646 |
* Otherwise, if more elements are added after that one, they will leak |
| 647 |
* once the GPtrArray is freed and its contents converted into a GStrv. */ |
| 648 |
if (!str) |
| 649 |
str = g_strdup (""); |
| 650 |
|
| 651 |
g_ptr_array_add (array, str); |
| 652 |
} |
| 653 |
|
| 654 |
g_ptr_array_add (array, NULL); |
| 655 |
*out_array = (gchar **) g_ptr_array_free (g_steal_pointer (&array), FALSE); |
| 656 |
return TRUE; |
| 657 |
} |
| 658 |
|
| 659 |
/* |
| 660 |
* Byte arrays may be given in very different ways: |
| 661 |
* - (a) Offset + Length pair in static buffer, data in variable buffer. |
| 662 |
* - (b) Just length in static buffer, data just afterwards. |
| 663 |
* - (c) Just offset in static buffer, length given in another variable, data in variable buffer. |
| 664 |
* - (d) Fixed-sized array directly in the static buffer. |
| 665 |
* - (e) Unsized array directly in the variable buffer, length is assumed until end of message. |
| 666 |
*/ |
| 667 |
gboolean |
| 668 |
_mbim_message_read_byte_array (const MbimMessage *self, |
| 669 |
guint32 struct_start_offset, |
| 670 |
guint32 relative_offset, |
| 671 |
gboolean has_offset, |
| 672 |
gboolean has_length, |
| 673 |
guint32 explicit_array_size, |
| 674 |
const guint8 **array, |
| 675 |
guint32 *array_size, |
| 676 |
GError **error, |
| 677 |
gboolean swapped_offset_length) |
| 678 |
{ |
| 679 |
guint32 information_buffer_offset; |
| 680 |
|
| 681 |
information_buffer_offset = _mbim_message_get_information_buffer_offset (self); |
| 682 |
|
| 683 |
/* (a) Offset + Length pair in static buffer, data in variable buffer. */ |
| 684 |
if (has_offset && has_length) { |
| 685 |
guint32 offset; |
| 686 |
guint64 required_size; |
| 687 |
|
| 688 |
g_assert (array_size != NULL); |
| 689 |
g_assert (explicit_array_size == 0); |
| 690 |
|
| 691 |
/* requires 8 bytes in relative offset */ |
| 692 |
required_size = (guint64)information_buffer_offset + (guint64)relative_offset + 8; |
| 693 |
if ((guint64)self->len < required_size) { |
| 694 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 695 |
"cannot read byte array offset and size (%u < %" G_GUINT64_FORMAT ")", |
| 696 |
self->len, required_size); |
| 697 |
return FALSE; |
| 698 |
} |
| 699 |
|
| 700 |
if (!swapped_offset_length) { |
| 701 |
/* (b) Offset followed by length encoding format. */ |
| 702 |
offset = mbim_helpers_read_unaligned_guint32 (self->data + information_buffer_offset + relative_offset); |
| 703 |
*array_size = mbim_helpers_read_unaligned_guint32 (self->data + information_buffer_offset + relative_offset + 4); |
| 704 |
} else { |
| 705 |
/* (b) length followed by offset encoding format. */ |
| 706 |
*array_size = mbim_helpers_read_unaligned_guint32 (self->data + information_buffer_offset + relative_offset); |
| 707 |
offset = mbim_helpers_read_unaligned_guint32 (self->data + information_buffer_offset + relative_offset + 4); |
| 708 |
} |
| 709 |
|
| 710 |
/* requires array_size bytes in offset */ |
| 711 |
required_size = (guint64)information_buffer_offset + (guint64)struct_start_offset + (guint64)offset + (guint64)(*array_size); |
| 712 |
if ((guint64)self->len < required_size) { |
| 713 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 714 |
"cannot read byte array data (%u bytes) (%u < %" G_GUINT64_FORMAT ")", |
| 715 |
*array_size, self->len, required_size); |
| 716 |
return FALSE; |
| 717 |
} |
| 718 |
|
| 719 |
*array = self->data + information_buffer_offset + struct_start_offset + offset; |
| 720 |
return TRUE; |
| 721 |
} |
| 722 |
|
| 723 |
/* (b) Just length in static buffer, data just afterwards. */ |
| 724 |
if (!has_offset && has_length) { |
| 725 |
guint64 required_size; |
| 726 |
|
| 727 |
g_assert (array_size != NULL); |
| 728 |
g_assert (explicit_array_size == 0); |
| 729 |
|
| 730 |
/* requires 4 bytes in relative offset */ |
| 731 |
required_size = (guint64)information_buffer_offset + (guint64)relative_offset + 4; |
| 732 |
if ((guint64)self->len < required_size) { |
| 733 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 734 |
"cannot read byte array size (%u < %" G_GUINT64_FORMAT ")", |
| 735 |
self->len, required_size); |
| 736 |
return FALSE; |
| 737 |
} |
| 738 |
|
| 739 |
*array_size = mbim_helpers_read_unaligned_guint32 (self->data + information_buffer_offset + relative_offset); |
| 740 |
|
| 741 |
/* requires array_size bytes in after the array_size variable */ |
| 742 |
required_size += (guint64)(*array_size); |
| 743 |
if ((guint64)self->len < required_size) { |
| 744 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 745 |
"cannot read byte array data (%u bytes) (%u < %" G_GUINT64_FORMAT ")", |
| 746 |
*array_size, self->len, required_size); |
| 747 |
return FALSE; |
| 748 |
} |
| 749 |
|
| 750 |
*array = self->data + information_buffer_offset + relative_offset + 4; |
| 751 |
return TRUE; |
| 752 |
} |
| 753 |
|
| 754 |
/* (c) Just offset in static buffer, length given in another variable, data in variable buffer. */ |
| 755 |
if (has_offset && !has_length) { |
| 756 |
guint64 required_size; |
| 757 |
guint32 offset; |
| 758 |
|
| 759 |
g_assert (array_size == NULL); |
| 760 |
|
| 761 |
/* requires 4 bytes in relative offset */ |
| 762 |
required_size = (guint64)information_buffer_offset + (guint64)relative_offset + 4; |
| 763 |
if ((guint64)self->len < required_size) { |
| 764 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 765 |
"cannot read byte array offset (%u < %" G_GUINT64_FORMAT ")", |
| 766 |
self->len, required_size); |
| 767 |
return FALSE; |
| 768 |
} |
| 769 |
|
| 770 |
offset = mbim_helpers_read_unaligned_guint32 (self->data + information_buffer_offset + relative_offset); |
| 771 |
|
| 772 |
/* requires explicit_array_size bytes in offset */ |
| 773 |
required_size = (guint64)information_buffer_offset + (guint64)struct_start_offset + (guint64)offset + (guint64)explicit_array_size; |
| 774 |
if ((guint64)self->len < required_size) { |
| 775 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 776 |
"cannot read byte array data (%u bytes) (%u < %" G_GUINT64_FORMAT ")", |
| 777 |
explicit_array_size, self->len, required_size); |
| 778 |
return FALSE; |
| 779 |
} |
| 780 |
|
| 781 |
*array = self->data + information_buffer_offset + struct_start_offset + offset; |
| 782 |
return TRUE; |
| 783 |
} |
| 784 |
|
| 785 |
/* (d) Fixed-sized array directly in the static buffer. |
| 786 |
* (e) Unsized array directly in the variable buffer, length is assumed until end of message. */ |
| 787 |
if (!has_offset && !has_length) { |
| 788 |
/* If array size is requested, it's case (e) */ |
| 789 |
if (array_size) { |
| 790 |
if ((guint64)self->len < (information_buffer_offset + relative_offset)) { |
| 791 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 792 |
"cannot compute byte array length: wrong offsets"); |
| 793 |
return FALSE; |
| 794 |
} |
| 795 |
*array_size = self->len - (information_buffer_offset + relative_offset); |
| 796 |
} else { |
| 797 |
guint64 required_size; |
| 798 |
|
| 799 |
/* requires explicit_array_size bytes in offset */ |
| 800 |
required_size = (guint64)information_buffer_offset + (guint64)relative_offset + (guint64)explicit_array_size; |
| 801 |
if ((guint64)self->len < required_size) { |
| 802 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 803 |
"cannot read byte array data (%u bytes) (%u < %" G_GUINT64_FORMAT ")", |
| 804 |
explicit_array_size, self->len, required_size); |
| 805 |
return FALSE; |
| 806 |
} |
| 807 |
} |
| 808 |
|
| 809 |
*array = self->data + information_buffer_offset + relative_offset; |
| 810 |
return TRUE; |
| 811 |
} |
| 812 |
|
| 813 |
g_assert_not_reached (); |
| 814 |
} |
| 815 |
|
| 816 |
gboolean |
| 817 |
_mbim_message_read_uuid (const MbimMessage *self, |
| 818 |
guint32 relative_offset, |
| 819 |
const MbimUuid **uuid_ptr, /* unsafe if unaligned */ |
| 820 |
MbimUuid *uuid_value, |
| 821 |
GError **error) |
| 822 |
{ |
| 823 |
guint64 required_size; |
| 824 |
guint32 information_buffer_offset; |
| 825 |
|
| 826 |
g_assert (uuid_ptr || uuid_value); |
| 827 |
g_assert (!(uuid_ptr && uuid_value)); |
| 828 |
|
| 829 |
information_buffer_offset = _mbim_message_get_information_buffer_offset (self); |
| 830 |
|
| 831 |
required_size = (guint64)information_buffer_offset + (guint64)relative_offset + 16; |
| 832 |
if ((guint64)self->len < required_size) { |
| 833 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 834 |
"cannot read UUID (16 bytes) (%u < %" G_GUINT64_FORMAT ")", |
| 835 |
self->len, required_size); |
| 836 |
return FALSE; |
| 837 |
} |
| 838 |
|
| 839 |
/* This explicit cast to a MbimUuid may fail if the contents are not correctly aligned, |
| 840 |
* but so far in the currently supported MBIM messages where a MbimUuid is returned |
| 841 |
* the 32bit alignment is ensured: |
| 842 |
* - MBIM Basic Connect "Connect" response. |
| 843 |
* - MBIM Basic Connect "Connect" notification. |
| 844 |
* - MBIM MS Basic Connect v3 "Connect" response. |
| 845 |
* - MBIM MS Basic Connect v3 "Connect" notification. |
| 846 |
* - MBIM MS Firmware ID "Get" response. |
| 847 |
* |
| 848 |
* We keep this limitation instead of making it an alignment-safe read because otherwise |
| 849 |
* it would require new API/ABI compat methods. |
| 850 |
*/ |
| 851 |
if (uuid_ptr) |
| 852 |
*uuid_ptr = (const MbimUuid *) (self->data + information_buffer_offset + relative_offset); |
| 853 |
|
| 854 |
if (uuid_value) |
| 855 |
memcpy (uuid_value, self->data + information_buffer_offset + relative_offset, 16); |
| 856 |
return TRUE; |
| 857 |
} |
| 858 |
|
| 859 |
gboolean |
| 860 |
_mbim_message_read_ipv4 (const MbimMessage *self, |
| 861 |
guint32 relative_offset, |
| 862 |
gboolean ref, |
| 863 |
const MbimIPv4 **ipv4_ptr, /* unsafe if unaligned */ |
| 864 |
MbimIPv4 *ipv4_value, |
| 865 |
GError **error) |
| 866 |
{ |
| 867 |
guint64 required_size; |
| 868 |
guint32 information_buffer_offset; |
| 869 |
guint32 offset; |
| 870 |
|
| 871 |
g_assert (ipv4_ptr || ipv4_value); |
| 872 |
g_assert (!(ipv4_ptr && ipv4_value)); |
| 873 |
|
| 874 |
information_buffer_offset = _mbim_message_get_information_buffer_offset (self); |
| 875 |
|
| 876 |
if (ref) { |
| 877 |
/* Read operations with a reference are expected only on certain message |
| 878 |
* API methods, where the output is required to be a pointer as well, to |
| 879 |
* indicate whether the field exists or not. */ |
| 880 |
g_assert (ipv4_ptr); |
| 881 |
|
| 882 |
required_size = (guint64)information_buffer_offset + (guint64)relative_offset + 4; |
| 883 |
if ((guint64)self->len < required_size) { |
| 884 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 885 |
"cannot read IPv4 offset (4 bytes) (%u < %" G_GUINT64_FORMAT ")", |
| 886 |
self->len, required_size); |
| 887 |
return FALSE; |
| 888 |
} |
| 889 |
|
| 890 |
offset = mbim_helpers_read_unaligned_guint32 (self->data + information_buffer_offset + relative_offset); |
| 891 |
if (!offset) { |
| 892 |
*ipv4_ptr = NULL; |
| 893 |
return TRUE; |
| 894 |
} |
| 895 |
} else |
| 896 |
offset = relative_offset; |
| 897 |
|
| 898 |
required_size = (guint64)information_buffer_offset + (guint64)offset + 4; |
| 899 |
if ((guint64)self->len < required_size) { |
| 900 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 901 |
"cannot read IPv4 (4 bytes) (%u < %" G_GUINT64_FORMAT ")", |
| 902 |
self->len, required_size); |
| 903 |
return FALSE; |
| 904 |
} |
| 905 |
|
| 906 |
/* This explicit cast to a MbimIPv4 may fail if the contents are not correctly aligned, |
| 907 |
* but so far in the currently supported MBIM messages where a MbimIPv4 is returned |
| 908 |
* the 32bit alignment is ensured: |
| 909 |
* - MBIM Basic Connect "IP Configuration" response. |
| 910 |
* - MBIM Basic Connect "IP Configuration" notification. |
| 911 |
* |
| 912 |
* We keep this limitation instead of making it an alignment-safe read because otherwise |
| 913 |
* it would require new API/ABI compat methods. |
| 914 |
*/ |
| 915 |
if (ipv4_ptr) |
| 916 |
*ipv4_ptr = (const MbimIPv4 *) (self->data +information_buffer_offset + offset); |
| 917 |
|
| 918 |
if (ipv4_value) |
| 919 |
memcpy (ipv4_value, self->data +information_buffer_offset + offset, 4); |
| 920 |
return TRUE; |
| 921 |
} |
| 922 |
|
| 923 |
gboolean |
| 924 |
_mbim_message_read_ipv4_array (const MbimMessage *self, |
| 925 |
guint32 array_size, |
| 926 |
guint32 relative_offset_array_start, |
| 927 |
MbimIPv4 **array, |
| 928 |
GError **error) |
| 929 |
{ |
| 930 |
guint64 required_size; |
| 931 |
guint32 offset; |
| 932 |
guint32 i; |
| 933 |
guint32 information_buffer_offset; |
| 934 |
|
| 935 |
g_assert (array != NULL); |
| 936 |
|
| 937 |
if (!array_size) { |
| 938 |
*array = NULL; |
| 939 |
return TRUE; |
| 940 |
} |
| 941 |
|
| 942 |
information_buffer_offset = _mbim_message_get_information_buffer_offset (self); |
| 943 |
|
| 944 |
required_size = (guint64)information_buffer_offset + (guint64)relative_offset_array_start + 4; |
| 945 |
if ((guint64)self->len < required_size) { |
| 946 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 947 |
"cannot read IPv4 array offset (4 bytes) (%u < %" G_GUINT64_FORMAT ")", |
| 948 |
self->len, required_size); |
| 949 |
return FALSE; |
| 950 |
} |
| 951 |
|
| 952 |
offset = mbim_helpers_read_unaligned_guint32 (self->data + information_buffer_offset + relative_offset_array_start); |
| 953 |
|
| 954 |
required_size = (guint64)information_buffer_offset + (guint64)offset + (4 * (guint64)array_size); |
| 955 |
if ((guint64)self->len < required_size) { |
| 956 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 957 |
"cannot read IPv4 array data (%" G_GUINT64_FORMAT " bytes) (%u < %" G_GUINT64_FORMAT ")", |
| 958 |
(4 * (guint64)array_size), self->len, required_size); |
| 959 |
return FALSE; |
| 960 |
} |
| 961 |
|
| 962 |
*array = g_new (MbimIPv4, array_size); |
| 963 |
for (i = 0; i < array_size; i++, offset += 4) |
| 964 |
memcpy (&((*array)[i]), self->data + information_buffer_offset + offset, 4); |
| 965 |
|
| 966 |
return TRUE; |
| 967 |
} |
| 968 |
|
| 969 |
gboolean |
| 970 |
_mbim_message_read_ipv6 (const MbimMessage *self, |
| 971 |
guint32 relative_offset, |
| 972 |
gboolean ref, |
| 973 |
const MbimIPv6 **ipv6_ptr, /* unsafe if unaligned */ |
| 974 |
MbimIPv6 *ipv6_value, |
| 975 |
GError **error) |
| 976 |
{ |
| 977 |
guint64 required_size; |
| 978 |
guint32 information_buffer_offset; |
| 979 |
guint32 offset; |
| 980 |
|
| 981 |
g_assert (ipv6_ptr || ipv6_value); |
| 982 |
g_assert (!(ipv6_ptr && ipv6_value)); |
| 983 |
|
| 984 |
information_buffer_offset = _mbim_message_get_information_buffer_offset (self); |
| 985 |
|
| 986 |
if (ref) { |
| 987 |
/* Read operations with a reference are expected only on certain message |
| 988 |
* API methods, where the output is required to be a pointer as well, to |
| 989 |
* indicate whether the field exists or not. */ |
| 990 |
g_assert (ipv6_ptr); |
| 991 |
|
| 992 |
required_size = (guint64)information_buffer_offset + (guint64)relative_offset + 4; |
| 993 |
if ((guint64)self->len < required_size) { |
| 994 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 995 |
"cannot read IPv6 offset (4 bytes) (%u < %" G_GUINT64_FORMAT ")", |
| 996 |
self->len, required_size); |
| 997 |
return FALSE; |
| 998 |
} |
| 999 |
|
| 1000 |
offset = mbim_helpers_read_unaligned_guint32 (self->data + information_buffer_offset + relative_offset); |
| 1001 |
if (!offset) { |
| 1002 |
*ipv6_ptr = NULL; |
| 1003 |
return TRUE; |
| 1004 |
} |
| 1005 |
} else |
| 1006 |
offset = relative_offset; |
| 1007 |
|
| 1008 |
required_size = (guint64)information_buffer_offset + (guint64)offset + 16; |
| 1009 |
if ((guint64)self->len < required_size) { |
| 1010 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 1011 |
"cannot read IPv6 (16 bytes) (%u < %" G_GUINT64_FORMAT ")", |
| 1012 |
self->len, required_size); |
| 1013 |
return FALSE; |
| 1014 |
} |
| 1015 |
|
| 1016 |
/* This explicit cast to a MbimIPv6 may fail if the contents are not correctly aligned, |
| 1017 |
* but so far in the currently supported MBIM messages where a MbimIPv6 is returned |
| 1018 |
* the 32bit alignment is ensured: |
| 1019 |
* - MBIM Basic Connect "IP Configuration" response. |
| 1020 |
* - MBIM Basic Connect "IP Configuration" notification. |
| 1021 |
* |
| 1022 |
* We keep this limitation instead of making it an alignment-safe read because otherwise |
| 1023 |
* it would require new API/ABI compat methods. |
| 1024 |
*/ |
| 1025 |
if (ipv6_ptr) |
| 1026 |
*ipv6_ptr = (const MbimIPv6 *) (self->data +information_buffer_offset + offset); |
| 1027 |
|
| 1028 |
if (ipv6_value) |
| 1029 |
memcpy (ipv6_value, self->data +information_buffer_offset + offset, 16); |
| 1030 |
return TRUE; |
| 1031 |
} |
| 1032 |
|
| 1033 |
gboolean |
| 1034 |
_mbim_message_read_ipv6_array (const MbimMessage *self, |
| 1035 |
guint32 array_size, |
| 1036 |
guint32 relative_offset_array_start, |
| 1037 |
MbimIPv6 **array, |
| 1038 |
GError **error) |
| 1039 |
{ |
| 1040 |
guint64 required_size; |
| 1041 |
guint32 offset; |
| 1042 |
guint32 i; |
| 1043 |
guint32 information_buffer_offset; |
| 1044 |
|
| 1045 |
g_assert (array != NULL); |
| 1046 |
|
| 1047 |
if (!array_size) { |
| 1048 |
*array = NULL; |
| 1049 |
return TRUE; |
| 1050 |
} |
| 1051 |
|
| 1052 |
information_buffer_offset = _mbim_message_get_information_buffer_offset (self); |
| 1053 |
|
| 1054 |
required_size = (guint64)information_buffer_offset + (guint64)relative_offset_array_start + 4; |
| 1055 |
if ((guint64)self->len < required_size) { |
| 1056 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 1057 |
"cannot read IPv6 array offset (4 bytes) (%u < %" G_GUINT64_FORMAT ")", |
| 1058 |
self->len, required_size); |
| 1059 |
return FALSE; |
| 1060 |
} |
| 1061 |
|
| 1062 |
offset = mbim_helpers_read_unaligned_guint32 (self->data + information_buffer_offset + relative_offset_array_start); |
| 1063 |
|
| 1064 |
required_size = (guint64)information_buffer_offset + (guint64)offset + (16 * (guint64)array_size); |
| 1065 |
if ((guint64)self->len < required_size) { |
| 1066 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 1067 |
"cannot read IPv6 array data (%" G_GUINT64_FORMAT " bytes) (%u < %" G_GUINT64_FORMAT ")", |
| 1068 |
(16 * (guint64)array_size), self->len, required_size); |
| 1069 |
return FALSE; |
| 1070 |
} |
| 1071 |
|
| 1072 |
*array = g_new (MbimIPv6, array_size); |
| 1073 |
for (i = 0; i < array_size; i++, offset += 16) |
| 1074 |
memcpy (&((*array)[i]), self->data + information_buffer_offset + offset, 16); |
| 1075 |
|
| 1076 |
return TRUE; |
| 1077 |
} |
| 1078 |
|
| 1079 |
gboolean |
| 1080 |
_mbim_message_read_tlv (const MbimMessage *self, |
| 1081 |
guint32 relative_offset, |
| 1082 |
MbimTlv **tlv, |
| 1083 |
guint32 *bytes_read, |
| 1084 |
GError **error) |
| 1085 |
{ |
| 1086 |
guint32 information_buffer_offset; |
| 1087 |
guint64 tlv_offset; |
| 1088 |
const guint8 *tlv_raw; |
| 1089 |
guint32 tlv_raw_size; |
| 1090 |
|
| 1091 |
information_buffer_offset = _mbim_message_get_information_buffer_offset (self); |
| 1092 |
tlv_offset = (guint64)information_buffer_offset + (guint64)relative_offset; |
| 1093 |
|
| 1094 |
if ((guint64)self->len < tlv_offset) { |
| 1095 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 1096 |
"TLV has invalid offset %" G_GUINT64_FORMAT |
| 1097 |
" and will exceed message bounds (%u)", |
| 1098 |
tlv_offset, self->len); |
| 1099 |
return FALSE; |
| 1100 |
} |
| 1101 |
|
| 1102 |
tlv_raw_size = self->len - (guint32)tlv_offset; |
| 1103 |
tlv_raw = self->data + tlv_offset; |
| 1104 |
|
| 1105 |
*tlv = _mbim_tlv_new_from_raw (tlv_raw, tlv_raw_size, bytes_read, error); |
| 1106 |
return (*tlv) ? TRUE : FALSE; |
| 1107 |
} |
| 1108 |
|
| 1109 |
gboolean |
| 1110 |
_mbim_message_read_tlv_string (const MbimMessage *self, |
| 1111 |
guint32 relative_offset, |
| 1112 |
gchar **str, |
| 1113 |
guint32 *bytes_read, |
| 1114 |
GError **error) |
| 1115 |
{ |
| 1116 |
g_autoptr(MbimTlv) tlv = NULL; |
| 1117 |
guint32 tlv_bytes_read = 0; |
| 1118 |
gchar *tlv_str; |
| 1119 |
|
| 1120 |
if (!_mbim_message_read_tlv (self, |
| 1121 |
relative_offset, |
| 1122 |
&tlv, |
| 1123 |
&tlv_bytes_read, |
| 1124 |
error)) |
| 1125 |
return FALSE; |
| 1126 |
|
| 1127 |
tlv_str = mbim_tlv_string_get (tlv, error); |
| 1128 |
if (!tlv_str) |
| 1129 |
return FALSE; |
| 1130 |
|
| 1131 |
*str = tlv_str; |
| 1132 |
*bytes_read = tlv_bytes_read; |
| 1133 |
return TRUE; |
| 1134 |
} |
| 1135 |
|
| 1136 |
gboolean |
| 1137 |
_mbim_message_read_tlv_guint16_array (const MbimMessage *self, |
| 1138 |
guint32 relative_offset, |
| 1139 |
guint32 *array_size, |
| 1140 |
guint16 **array, |
| 1141 |
guint32 *bytes_read, |
| 1142 |
GError **error) |
| 1143 |
{ |
| 1144 |
g_autoptr(MbimTlv) tlv = NULL; |
| 1145 |
guint32 tlv_bytes_read = 0; |
| 1146 |
|
| 1147 |
if (!_mbim_message_read_tlv (self, |
| 1148 |
relative_offset, |
| 1149 |
&tlv, |
| 1150 |
&tlv_bytes_read, |
| 1151 |
error)) |
| 1152 |
return FALSE; |
| 1153 |
|
| 1154 |
if (!mbim_tlv_guint16_array_get (tlv, array_size, array, error)) |
| 1155 |
return FALSE; |
| 1156 |
|
| 1157 |
*bytes_read = tlv_bytes_read; |
| 1158 |
return TRUE; |
| 1159 |
} |
| 1160 |
|
| 1161 |
gboolean |
| 1162 |
_mbim_message_read_tlv_list (const MbimMessage *self, |
| 1163 |
guint32 relative_offset, |
| 1164 |
GList **tlv_list, |
| 1165 |
guint32 *bytes_read, |
| 1166 |
GError **error) |
| 1167 |
{ |
| 1168 |
guint32 information_buffer_offset; |
| 1169 |
guint64 tlv_list_offset; |
| 1170 |
const guint8 *tlv_list_raw; |
| 1171 |
guint32 tlv_list_raw_size; |
| 1172 |
GList *list = NULL; |
| 1173 |
guint32 total_bytes_read = 0; |
| 1174 |
GError *inner_error = NULL; |
| 1175 |
|
| 1176 |
information_buffer_offset = _mbim_message_get_information_buffer_offset (self); |
| 1177 |
tlv_list_offset = (guint64)information_buffer_offset + (guint64)relative_offset; |
| 1178 |
|
| 1179 |
/* TLV list always at the end of the message */ |
| 1180 |
if ((guint64)self->len < tlv_list_offset) { |
| 1181 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 1182 |
"TLV list item has invalid offset %" G_GUINT64_FORMAT |
| 1183 |
" and will exceed message bounds (%u)", |
| 1184 |
tlv_list_offset, self->len); |
| 1185 |
return FALSE; |
| 1186 |
} |
| 1187 |
|
| 1188 |
tlv_list_raw_size = self->len - (guint32)tlv_list_offset; |
| 1189 |
tlv_list_raw = self->data + tlv_list_offset; |
| 1190 |
|
| 1191 |
while ((tlv_list_raw_size > 0) && !inner_error) { |
| 1192 |
MbimTlv *tlv; |
| 1193 |
guint32 tlv_bytes_read; |
| 1194 |
|
| 1195 |
if (tlv_list_raw_size < sizeof (struct tlv)) { |
| 1196 |
g_debug ("Ignored %u bytes unused after the TLV list", tlv_list_raw_size); |
| 1197 |
break; |
| 1198 |
} |
| 1199 |
|
| 1200 |
tlv = _mbim_tlv_new_from_raw (tlv_list_raw, tlv_list_raw_size, &tlv_bytes_read, &inner_error); |
| 1201 |
if (!tlv) |
| 1202 |
break; |
| 1203 |
|
| 1204 |
list = g_list_append (list, tlv); |
| 1205 |
total_bytes_read += tlv_bytes_read; |
| 1206 |
|
| 1207 |
g_assert (tlv_list_raw_size >= tlv_bytes_read); |
| 1208 |
tlv_list_raw += tlv_bytes_read; |
| 1209 |
tlv_list_raw_size -= tlv_bytes_read; |
| 1210 |
} |
| 1211 |
|
| 1212 |
if (inner_error) { |
| 1213 |
g_propagate_error (error, inner_error); |
| 1214 |
g_list_free_full (list, (GDestroyNotify)mbim_tlv_unref); |
| 1215 |
return FALSE; |
| 1216 |
} |
| 1217 |
|
| 1218 |
*bytes_read = total_bytes_read; |
| 1219 |
*tlv_list = list; |
| 1220 |
return TRUE; |
| 1221 |
} |
| 1222 |
|
| 1223 |
/*****************************************************************************/ |
| 1224 |
/* Struct builder interface |
| 1225 |
* |
| 1226 |
* Types like structs consist of a fixed sized prefix plus a variable length |
| 1227 |
* data buffer. Items of variable size are usually given as an offset (with |
| 1228 |
* respect to the start of the struct) plus a size field. */ |
| 1229 |
|
| 1230 |
MbimStructBuilder * |
| 1231 |
_mbim_struct_builder_new (void) |
| 1232 |
{ |
| 1233 |
MbimStructBuilder *builder; |
| 1234 |
|
| 1235 |
builder = g_slice_new (MbimStructBuilder); |
| 1236 |
builder->fixed_buffer = g_byte_array_new (); |
| 1237 |
builder->variable_buffer = g_byte_array_new (); |
| 1238 |
builder->offsets = g_array_new (FALSE, FALSE, sizeof (guint32)); |
| 1239 |
return builder; |
| 1240 |
} |
| 1241 |
|
| 1242 |
GByteArray * |
| 1243 |
_mbim_struct_builder_complete (MbimStructBuilder *builder) |
| 1244 |
{ |
| 1245 |
GByteArray *out; |
| 1246 |
guint i; |
| 1247 |
|
| 1248 |
/* Update offsets with the length of the information buffer, and store them |
| 1249 |
* in LE. */ |
| 1250 |
for (i = 0; i < builder->offsets->len; i++) { |
| 1251 |
guint32 offset_offset; |
| 1252 |
guint32 offset_value; |
| 1253 |
|
| 1254 |
offset_offset = g_array_index (builder->offsets, guint32, i); |
| 1255 |
memcpy (&offset_value, &(builder->fixed_buffer->data[offset_offset]), sizeof (guint32)); |
| 1256 |
offset_value = GUINT32_TO_LE (offset_value + builder->fixed_buffer->len); |
| 1257 |
memcpy (&(builder->fixed_buffer->data[offset_offset]), &offset_value, sizeof (guint32)); |
| 1258 |
} |
| 1259 |
|
| 1260 |
/* Merge both buffers */ |
| 1261 |
g_byte_array_append (builder->fixed_buffer, |
| 1262 |
(const guint8 *)builder->variable_buffer->data, |
| 1263 |
(guint32)builder->variable_buffer->len); |
| 1264 |
|
| 1265 |
/* Steal the buffer to return */ |
| 1266 |
out = builder->fixed_buffer; |
| 1267 |
|
| 1268 |
/* Dispose the builder */ |
| 1269 |
g_array_unref (builder->offsets); |
| 1270 |
g_byte_array_unref (builder->variable_buffer); |
| 1271 |
g_slice_free (MbimStructBuilder, builder); |
| 1272 |
|
| 1273 |
return out; |
| 1274 |
} |
| 1275 |
|
| 1276 |
/* |
| 1277 |
* Byte arrays may be given in very different ways: |
| 1278 |
* - (a) Offset + Length pair in static buffer, data in variable buffer. |
| 1279 |
* - (b) Just length in static buffer, data just afterwards. |
| 1280 |
* - (c) Just offset in static buffer, length given in another variable, data in variable buffer. |
| 1281 |
* - (d) Fixed-sized array directly in the static buffer. |
| 1282 |
* - (e) Unsized array directly in the variable buffer, length is assumed until end of message. |
| 1283 |
*/ |
| 1284 |
void |
| 1285 |
_mbim_struct_builder_append_byte_array (MbimStructBuilder *builder, |
| 1286 |
gboolean with_offset, |
| 1287 |
gboolean with_length, |
| 1288 |
gboolean pad_buffer, |
| 1289 |
const guint8 *buffer, |
| 1290 |
guint32 buffer_len, |
| 1291 |
gboolean swapped_offset_length) |
| 1292 |
{ |
| 1293 |
/* |
| 1294 |
* (d) Fixed-sized array directly in the static buffer. |
| 1295 |
* (e) Unsized array directly in the variable buffer (here end of static buffer is also beginning of variable) |
| 1296 |
*/ |
| 1297 |
if (!with_offset && !with_length) { |
| 1298 |
g_byte_array_append (builder->fixed_buffer, buffer, buffer_len); |
| 1299 |
if (pad_buffer) |
| 1300 |
bytearray_apply_padding (builder->fixed_buffer, &buffer_len); |
| 1301 |
return; |
| 1302 |
} |
| 1303 |
|
| 1304 |
/* (a) Offset + Length pair in static buffer, data in variable buffer. |
| 1305 |
* This case is the sum of cases b+c */ |
| 1306 |
|
| 1307 |
/* (b) Just length in static buffer, data just afterwards. */ |
| 1308 |
if (swapped_offset_length && with_length) { |
| 1309 |
guint32 length; |
| 1310 |
|
| 1311 |
/* Add the length value in beginning and offset in the end later*/ |
| 1312 |
length = GUINT32_TO_LE (buffer_len); |
| 1313 |
g_byte_array_append (builder->fixed_buffer, (guint8 *)&length, sizeof (length)); |
| 1314 |
} |
| 1315 |
|
| 1316 |
/* (c) Just offset in static buffer, length given in another variable, data in variable buffer. */ |
| 1317 |
|
| 1318 |
if (with_offset) { |
| 1319 |
guint32 offset; |
| 1320 |
|
| 1321 |
/* If string length is greater than 0, add the offset to fix, otherwise set |
| 1322 |
* the offset to 0 and don't configure the update */ |
| 1323 |
if (buffer_len == 0) { |
| 1324 |
offset = 0; |
| 1325 |
g_byte_array_append (builder->fixed_buffer, (guint8 *)&offset, sizeof (offset)); |
| 1326 |
} else { |
| 1327 |
guint32 offset_offset; |
| 1328 |
|
| 1329 |
/* Offset of the offset */ |
| 1330 |
offset_offset = builder->fixed_buffer->len; |
| 1331 |
|
| 1332 |
/* Length *not* in LE yet */ |
| 1333 |
offset = builder->variable_buffer->len; |
| 1334 |
/* Add the offset value */ |
| 1335 |
g_byte_array_append (builder->fixed_buffer, (guint8 *)&offset, sizeof (offset)); |
| 1336 |
/* Configure the value to get updated */ |
| 1337 |
g_array_append_val (builder->offsets, offset_offset); |
| 1338 |
} |
| 1339 |
} |
| 1340 |
|
| 1341 |
/* (b) Just length in static buffer, data just afterwards. */ |
| 1342 |
if (!swapped_offset_length && with_length) { |
| 1343 |
guint32 length; |
| 1344 |
|
| 1345 |
/* Add the length value */ |
| 1346 |
length = GUINT32_TO_LE (buffer_len); |
| 1347 |
g_byte_array_append (builder->fixed_buffer, (guint8 *)&length, sizeof (length)); |
| 1348 |
} |
| 1349 |
|
| 1350 |
/* And finally, the bytearray itself to the variable buffer */ |
| 1351 |
if (buffer_len) { |
| 1352 |
g_byte_array_append (builder->variable_buffer, (const guint8 *)buffer, (guint)buffer_len); |
| 1353 |
|
| 1354 |
/* Note: adding zero padding causes trouble for QMI service */ |
| 1355 |
if (pad_buffer) |
| 1356 |
bytearray_apply_padding (builder->variable_buffer, &buffer_len); |
| 1357 |
} |
| 1358 |
} |
| 1359 |
|
| 1360 |
void |
| 1361 |
_mbim_struct_builder_append_uuid (MbimStructBuilder *builder, |
| 1362 |
const MbimUuid *value) |
| 1363 |
{ |
| 1364 |
static const MbimUuid uuid_invalid = { |
| 1365 |
.a = { 0x00, 0x00, 0x00, 0x00 }, |
| 1366 |
.b = { 0x00, 0x00 }, |
| 1367 |
.c = { 0x00, 0x00 }, |
| 1368 |
.d = { 0x00, 0x00 }, |
| 1369 |
.e = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } |
| 1370 |
}; |
| 1371 |
|
| 1372 |
/* uuids are added in the static buffer only */ |
| 1373 |
g_byte_array_append (builder->fixed_buffer, |
| 1374 |
value ? (guint8 *)value : (guint8 *)&uuid_invalid, |
| 1375 |
sizeof (MbimUuid)); |
| 1376 |
} |
| 1377 |
|
| 1378 |
void |
| 1379 |
_mbim_struct_builder_append_guint16 (MbimStructBuilder *builder, |
| 1380 |
guint16 value) |
| 1381 |
{ |
| 1382 |
guint16 tmp; |
| 1383 |
|
| 1384 |
/* guint16 values are added in the static buffer only */ |
| 1385 |
tmp = GUINT16_TO_LE (value); |
| 1386 |
g_byte_array_append (builder->fixed_buffer, (guint8 *)&tmp, sizeof (tmp)); |
| 1387 |
} |
| 1388 |
|
| 1389 |
void |
| 1390 |
_mbim_struct_builder_append_guint32 (MbimStructBuilder *builder, |
| 1391 |
guint32 value) |
| 1392 |
{ |
| 1393 |
guint32 tmp; |
| 1394 |
|
| 1395 |
/* guint32 values are added in the static buffer only */ |
| 1396 |
tmp = GUINT32_TO_LE (value); |
| 1397 |
g_byte_array_append (builder->fixed_buffer, (guint8 *)&tmp, sizeof (tmp)); |
| 1398 |
} |
| 1399 |
|
| 1400 |
void |
| 1401 |
_mbim_struct_builder_append_gint32 (MbimStructBuilder *builder, |
| 1402 |
gint32 value) |
| 1403 |
{ |
| 1404 |
gint32 tmp; |
| 1405 |
|
| 1406 |
/* gint32 values are added in the static buffer only */ |
| 1407 |
tmp = GINT32_TO_LE (value); |
| 1408 |
g_byte_array_append (builder->fixed_buffer, (guint8 *)&tmp, sizeof (tmp)); |
| 1409 |
} |
| 1410 |
|
| 1411 |
void |
| 1412 |
_mbim_struct_builder_append_guint32_array (MbimStructBuilder *builder, |
| 1413 |
const guint32 *values, |
| 1414 |
guint32 n_values) |
| 1415 |
{ |
| 1416 |
guint i; |
| 1417 |
|
| 1418 |
/* guint32 array added directly in the static buffer */ |
| 1419 |
for (i = 0; i < n_values; i++) |
| 1420 |
_mbim_struct_builder_append_guint32 (builder, values[i]); |
| 1421 |
} |
| 1422 |
|
| 1423 |
void |
| 1424 |
_mbim_struct_builder_append_guint64 (MbimStructBuilder *builder, |
| 1425 |
guint64 value) |
| 1426 |
{ |
| 1427 |
guint64 tmp; |
| 1428 |
|
| 1429 |
/* guint64 values are added in the static buffer only */ |
| 1430 |
tmp = GUINT64_TO_LE (value); |
| 1431 |
g_byte_array_append (builder->fixed_buffer, (guint8 *)&tmp, sizeof (tmp)); |
| 1432 |
} |
| 1433 |
|
| 1434 |
void |
| 1435 |
_mbim_struct_builder_append_string (MbimStructBuilder *builder, |
| 1436 |
const gchar *value) |
| 1437 |
{ |
| 1438 |
guint32 offset; |
| 1439 |
guint32 length; |
| 1440 |
g_autofree gunichar2 *utf16 = NULL; |
| 1441 |
guint32 utf16_bytes = 0; |
| 1442 |
|
| 1443 |
/* A string consists of Offset+Size in the static buffer, plus the |
| 1444 |
* string itself in the variable buffer */ |
| 1445 |
|
| 1446 |
/* Convert the string from UTF-8 to UTF-16HE */ |
| 1447 |
if (value && value[0]) { |
| 1448 |
g_autoptr(GError) error = NULL; |
| 1449 |
glong items_written = 0; |
| 1450 |
|
| 1451 |
utf16 = g_utf8_to_utf16 (value, |
| 1452 |
-1, |
| 1453 |
NULL, /* bytes */ |
| 1454 |
&items_written, /* gunichar2 */ |
| 1455 |
&error); |
| 1456 |
if (!utf16) { |
| 1457 |
g_warning ("Error converting string: %s", error->message); |
| 1458 |
return; |
| 1459 |
} |
| 1460 |
|
| 1461 |
utf16_bytes = items_written * 2; |
| 1462 |
} |
| 1463 |
|
| 1464 |
/* If string length is greater than 0, add the offset to fix, otherwise set |
| 1465 |
* the offset to 0 and don't configure the update */ |
| 1466 |
if (utf16_bytes == 0) { |
| 1467 |
offset = 0; |
| 1468 |
g_byte_array_append (builder->fixed_buffer, (guint8 *)&offset, sizeof (offset)); |
| 1469 |
} else { |
| 1470 |
guint32 offset_offset; |
| 1471 |
|
| 1472 |
/* Offset of the offset */ |
| 1473 |
offset_offset = builder->fixed_buffer->len; |
| 1474 |
|
| 1475 |
/* Length *not* in LE yet */ |
| 1476 |
offset = builder->variable_buffer->len; |
| 1477 |
/* Add the offset value */ |
| 1478 |
g_byte_array_append (builder->fixed_buffer, (guint8 *)&offset, sizeof (offset)); |
| 1479 |
/* Configure the value to get updated */ |
| 1480 |
g_array_append_val (builder->offsets, offset_offset); |
| 1481 |
} |
| 1482 |
|
| 1483 |
/* Add the length value */ |
| 1484 |
length = GUINT32_TO_LE (utf16_bytes); |
| 1485 |
g_byte_array_append (builder->fixed_buffer, (guint8 *)&length, sizeof (length)); |
| 1486 |
|
| 1487 |
/* And finally, the string itself to the variable buffer */ |
| 1488 |
if (utf16_bytes) { |
| 1489 |
/* For BE systems, convert from BE to LE */ |
| 1490 |
if (G_BYTE_ORDER == G_BIG_ENDIAN) { |
| 1491 |
guint i; |
| 1492 |
|
| 1493 |
for (i = 0; i < (utf16_bytes / 2); i++) |
| 1494 |
utf16[i] = GUINT16_TO_LE (utf16[i]); |
| 1495 |
} |
| 1496 |
g_byte_array_append (builder->variable_buffer, (const guint8 *)utf16, (guint)utf16_bytes); |
| 1497 |
bytearray_apply_padding (builder->variable_buffer, &utf16_bytes); |
| 1498 |
} |
| 1499 |
} |
| 1500 |
|
| 1501 |
void |
| 1502 |
_mbim_struct_builder_append_string_tlv (MbimStructBuilder *builder, |
| 1503 |
const gchar *value) |
| 1504 |
{ |
| 1505 |
guint8 reserved = 0; |
| 1506 |
guint8 padding = 0; |
| 1507 |
guint32 length; |
| 1508 |
gunichar2 *utf16 = NULL; |
| 1509 |
guint32 utf16_bytes = 0; |
| 1510 |
GError *error = NULL; |
| 1511 |
|
| 1512 |
/* Add the reserved value */ |
| 1513 |
g_byte_array_append (builder->fixed_buffer, (guint8 *)&reserved, sizeof (reserved)); |
| 1514 |
|
| 1515 |
/* Convert the string from UTF-8 to UTF-16HE */ |
| 1516 |
if (value && value[0]) { |
| 1517 |
glong items_written = 0; |
| 1518 |
|
| 1519 |
utf16 = g_utf8_to_utf16 (value, |
| 1520 |
-1, |
| 1521 |
NULL, /* bytes */ |
| 1522 |
&items_written, /* gunichar2 */ |
| 1523 |
&error); |
| 1524 |
|
| 1525 |
if (!utf16) { |
| 1526 |
g_warning ("Error converting string: %s", error->message); |
| 1527 |
g_error_free (error); |
| 1528 |
return; |
| 1529 |
} |
| 1530 |
utf16_bytes = items_written * 2; |
| 1531 |
|
| 1532 |
/* Add the padding length value */ |
| 1533 |
padding = utf16_bytes % 4; |
| 1534 |
g_byte_array_append (builder->fixed_buffer, (guint8 *)&padding, sizeof (padding)); |
| 1535 |
} |
| 1536 |
|
| 1537 |
/* Add the length value */ |
| 1538 |
length = GUINT32_TO_LE (utf16_bytes); |
| 1539 |
g_byte_array_append (builder->fixed_buffer, (guint8 *)&length, sizeof (length)); |
| 1540 |
|
| 1541 |
/* And finally, the string itself to the variable buffer */ |
| 1542 |
if (utf16_bytes) { |
| 1543 |
/* For BE systems, convert from BE to LE */ |
| 1544 |
if (G_BYTE_ORDER == G_BIG_ENDIAN) { |
| 1545 |
guint i; |
| 1546 |
|
| 1547 |
for (i = 0; i < (utf16_bytes / 2); i++) |
| 1548 |
utf16[i] = GUINT16_TO_LE (utf16[i]); |
| 1549 |
} |
| 1550 |
g_byte_array_append (builder->variable_buffer, (const guint8 *)utf16, (guint)utf16_bytes); |
| 1551 |
bytearray_apply_padding (builder->variable_buffer, &utf16_bytes); |
| 1552 |
} |
| 1553 |
g_free (utf16); |
| 1554 |
} |
| 1555 |
|
| 1556 |
void |
| 1557 |
_mbim_struct_builder_append_string_array (MbimStructBuilder *builder, |
| 1558 |
const gchar *const *values, |
| 1559 |
guint32 n_values) |
| 1560 |
{ |
| 1561 |
/* TODO */ |
| 1562 |
g_warn_if_reached (); |
| 1563 |
} |
| 1564 |
|
| 1565 |
void |
| 1566 |
_mbim_struct_builder_append_ipv4 (MbimStructBuilder *builder, |
| 1567 |
const MbimIPv4 *value, |
| 1568 |
gboolean ref) |
| 1569 |
{ |
| 1570 |
if (ref) |
| 1571 |
_mbim_struct_builder_append_ipv4_array (builder, value, value ? 1 : 0); |
| 1572 |
else |
| 1573 |
g_byte_array_append (builder->fixed_buffer, (guint8 *)value, sizeof (MbimIPv4)); |
| 1574 |
} |
| 1575 |
|
| 1576 |
void |
| 1577 |
_mbim_struct_builder_append_ipv4_array (MbimStructBuilder *builder, |
| 1578 |
const MbimIPv4 *values, |
| 1579 |
guint32 n_values) |
| 1580 |
{ |
| 1581 |
guint32 offset; |
| 1582 |
|
| 1583 |
if (!n_values) { |
| 1584 |
offset = 0; |
| 1585 |
g_byte_array_append (builder->fixed_buffer, (guint8 *)&offset, sizeof (offset)); |
| 1586 |
} else { |
| 1587 |
guint32 offset_offset; |
| 1588 |
|
| 1589 |
/* Offset of the offset */ |
| 1590 |
offset_offset = builder->fixed_buffer->len; |
| 1591 |
|
| 1592 |
/* Length *not* in LE yet */ |
| 1593 |
offset = builder->variable_buffer->len; |
| 1594 |
/* Add the offset value */ |
| 1595 |
g_byte_array_append (builder->fixed_buffer, (guint8 *)&offset, sizeof (offset)); |
| 1596 |
/* Configure the value to get updated */ |
| 1597 |
g_array_append_val (builder->offsets, offset_offset); |
| 1598 |
|
| 1599 |
/* NOTE: length of the array must be given in a separate variable */ |
| 1600 |
|
| 1601 |
/* And finally, the array of IPs itself to the variable buffer */ |
| 1602 |
g_byte_array_append (builder->variable_buffer, (guint8 *)values, n_values * sizeof (MbimIPv4)); |
| 1603 |
} |
| 1604 |
} |
| 1605 |
|
| 1606 |
void |
| 1607 |
_mbim_struct_builder_append_ipv6 (MbimStructBuilder *builder, |
| 1608 |
const MbimIPv6 *value, |
| 1609 |
gboolean ref) |
| 1610 |
{ |
| 1611 |
if (ref) |
| 1612 |
_mbim_struct_builder_append_ipv6_array (builder, value, value ? 1 : 0); |
| 1613 |
else |
| 1614 |
g_byte_array_append (builder->fixed_buffer, (guint8 *)value, sizeof (MbimIPv6)); |
| 1615 |
} |
| 1616 |
|
| 1617 |
void |
| 1618 |
_mbim_struct_builder_append_ipv6_array (MbimStructBuilder *builder, |
| 1619 |
const MbimIPv6 *values, |
| 1620 |
guint32 n_values) |
| 1621 |
{ |
| 1622 |
guint32 offset; |
| 1623 |
|
| 1624 |
if (!n_values) { |
| 1625 |
offset = 0; |
| 1626 |
g_byte_array_append (builder->fixed_buffer, (guint8 *)&offset, sizeof (offset)); |
| 1627 |
} else { |
| 1628 |
guint32 offset_offset; |
| 1629 |
|
| 1630 |
/* Offset of the offset */ |
| 1631 |
offset_offset = builder->fixed_buffer->len; |
| 1632 |
|
| 1633 |
/* Length *not* in LE yet */ |
| 1634 |
offset = builder->variable_buffer->len; |
| 1635 |
/* Add the offset value */ |
| 1636 |
g_byte_array_append (builder->fixed_buffer, (guint8 *)&offset, sizeof (offset)); |
| 1637 |
/* Configure the value to get updated */ |
| 1638 |
g_array_append_val (builder->offsets, offset_offset); |
| 1639 |
|
| 1640 |
/* NOTE: length of the array must be given in a separate variable */ |
| 1641 |
|
| 1642 |
/* And finally, the array of IPs itself to the variable buffer */ |
| 1643 |
g_byte_array_append (builder->variable_buffer, (guint8 *)values, n_values * sizeof (MbimIPv6)); |
| 1644 |
} |
| 1645 |
} |
| 1646 |
|
| 1647 |
/*****************************************************************************/ |
| 1648 |
/* Command message builder interface */ |
| 1649 |
|
| 1650 |
MbimMessageCommandBuilder * |
| 1651 |
_mbim_message_command_builder_new (guint32 transaction_id, |
| 1652 |
MbimService service, |
| 1653 |
guint32 cid, |
| 1654 |
MbimMessageCommandType command_type) |
| 1655 |
{ |
| 1656 |
MbimMessageCommandBuilder *builder; |
| 1657 |
|
| 1658 |
builder = g_slice_new (MbimMessageCommandBuilder); |
| 1659 |
builder->message = mbim_message_command_new (transaction_id, service, cid, command_type); |
| 1660 |
builder->contents_builder = _mbim_struct_builder_new (); |
| 1661 |
return builder; |
| 1662 |
} |
| 1663 |
|
| 1664 |
MbimMessage * |
| 1665 |
_mbim_message_command_builder_complete (MbimMessageCommandBuilder *builder) |
| 1666 |
{ |
| 1667 |
MbimMessage *message; |
| 1668 |
GByteArray *contents; |
| 1669 |
|
| 1670 |
/* Complete contents, which disposes the builder itself */ |
| 1671 |
contents = _mbim_struct_builder_complete (builder->contents_builder); |
| 1672 |
|
| 1673 |
/* Merge both buffers */ |
| 1674 |
mbim_message_command_append (builder->message, |
| 1675 |
(const guint8 *)contents->data, |
| 1676 |
(guint32)contents->len); |
| 1677 |
g_byte_array_unref (contents); |
| 1678 |
|
| 1679 |
/* Steal the message to return */ |
| 1680 |
message = builder->message; |
| 1681 |
|
| 1682 |
/* Dispose the remaining stuff from the message builder */ |
| 1683 |
g_slice_free (MbimMessageCommandBuilder, builder); |
| 1684 |
|
| 1685 |
return message; |
| 1686 |
} |
| 1687 |
|
| 1688 |
void |
| 1689 |
_mbim_message_command_builder_append_byte_array (MbimMessageCommandBuilder *builder, |
| 1690 |
gboolean with_offset, |
| 1691 |
gboolean with_length, |
| 1692 |
gboolean pad_buffer, |
| 1693 |
const guint8 *buffer, |
| 1694 |
guint32 buffer_len, |
| 1695 |
gboolean swapped_offset_length) |
| 1696 |
{ |
| 1697 |
_mbim_struct_builder_append_byte_array (builder->contents_builder, with_offset, with_length, pad_buffer, buffer, buffer_len, swapped_offset_length); |
| 1698 |
} |
| 1699 |
|
| 1700 |
void |
| 1701 |
_mbim_message_command_builder_append_uuid (MbimMessageCommandBuilder *builder, |
| 1702 |
const MbimUuid *value) |
| 1703 |
{ |
| 1704 |
_mbim_struct_builder_append_uuid (builder->contents_builder, value); |
| 1705 |
} |
| 1706 |
|
| 1707 |
void |
| 1708 |
_mbim_message_command_builder_append_guint32 (MbimMessageCommandBuilder *builder, |
| 1709 |
guint32 value) |
| 1710 |
{ |
| 1711 |
_mbim_struct_builder_append_guint32 (builder->contents_builder, value); |
| 1712 |
} |
| 1713 |
|
| 1714 |
void |
| 1715 |
_mbim_message_command_builder_append_guint16 (MbimMessageCommandBuilder *builder, |
| 1716 |
guint16 value) |
| 1717 |
{ |
| 1718 |
_mbim_struct_builder_append_guint16 (builder->contents_builder, value); |
| 1719 |
} |
| 1720 |
|
| 1721 |
void |
| 1722 |
_mbim_message_command_builder_append_guint32_array (MbimMessageCommandBuilder *builder, |
| 1723 |
const guint32 *values, |
| 1724 |
guint32 n_values) |
| 1725 |
{ |
| 1726 |
_mbim_struct_builder_append_guint32_array (builder->contents_builder, values, n_values); |
| 1727 |
} |
| 1728 |
|
| 1729 |
void |
| 1730 |
_mbim_message_command_builder_append_guint64 (MbimMessageCommandBuilder *builder, |
| 1731 |
guint64 value) |
| 1732 |
{ |
| 1733 |
_mbim_struct_builder_append_guint64 (builder->contents_builder, value); |
| 1734 |
} |
| 1735 |
|
| 1736 |
void |
| 1737 |
_mbim_message_command_builder_append_string (MbimMessageCommandBuilder *builder, |
| 1738 |
const gchar *value) |
| 1739 |
{ |
| 1740 |
_mbim_struct_builder_append_string (builder->contents_builder, value); |
| 1741 |
} |
| 1742 |
|
| 1743 |
void |
| 1744 |
_mbim_message_command_builder_append_string_array (MbimMessageCommandBuilder *builder, |
| 1745 |
const gchar *const *values, |
| 1746 |
guint32 n_values) |
| 1747 |
{ |
| 1748 |
_mbim_struct_builder_append_string_array (builder->contents_builder, values, n_values); |
| 1749 |
} |
| 1750 |
|
| 1751 |
void |
| 1752 |
_mbim_message_command_builder_append_ipv4 (MbimMessageCommandBuilder *builder, |
| 1753 |
const MbimIPv4 *value, |
| 1754 |
gboolean ref) |
| 1755 |
{ |
| 1756 |
_mbim_struct_builder_append_ipv4 (builder->contents_builder, value, ref); |
| 1757 |
} |
| 1758 |
|
| 1759 |
void |
| 1760 |
_mbim_message_command_builder_append_ipv4_array (MbimMessageCommandBuilder *builder, |
| 1761 |
const MbimIPv4 *values, |
| 1762 |
guint32 n_values) |
| 1763 |
{ |
| 1764 |
_mbim_struct_builder_append_ipv4_array (builder->contents_builder, values, n_values); |
| 1765 |
} |
| 1766 |
|
| 1767 |
void |
| 1768 |
_mbim_message_command_builder_append_ipv6 (MbimMessageCommandBuilder *builder, |
| 1769 |
const MbimIPv6 *value, |
| 1770 |
gboolean ref) |
| 1771 |
{ |
| 1772 |
_mbim_struct_builder_append_ipv6 (builder->contents_builder, value, ref); |
| 1773 |
} |
| 1774 |
|
| 1775 |
void |
| 1776 |
_mbim_message_command_builder_append_ipv6_array (MbimMessageCommandBuilder *builder, |
| 1777 |
const MbimIPv6 *values, |
| 1778 |
guint32 n_values) |
| 1779 |
{ |
| 1780 |
_mbim_struct_builder_append_ipv6_array (builder->contents_builder, values, n_values); |
| 1781 |
} |
| 1782 |
|
| 1783 |
/*****************************************************************************/ |
| 1784 |
/* TLVs only expected as primary message fields, not inside structs */ |
| 1785 |
|
| 1786 |
void |
| 1787 |
_mbim_message_command_builder_append_tlv (MbimMessageCommandBuilder *builder, |
| 1788 |
const MbimTlv *tlv) |
| 1789 |
{ |
| 1790 |
const guint8 *raw_tlv; |
| 1791 |
guint32 raw_tlv_size; |
| 1792 |
|
| 1793 |
raw_tlv = mbim_tlv_get_raw (tlv, &raw_tlv_size, NULL); |
| 1794 |
_mbim_struct_builder_append_byte_array (builder->contents_builder, |
| 1795 |
FALSE, FALSE, FALSE, |
| 1796 |
raw_tlv, raw_tlv_size, |
| 1797 |
FALSE); |
| 1798 |
} |
| 1799 |
|
| 1800 |
void |
| 1801 |
_mbim_message_command_builder_append_tlv_string (MbimMessageCommandBuilder *builder, |
| 1802 |
const gchar *str) |
| 1803 |
{ |
| 1804 |
g_autoptr(MbimTlv) tlv = NULL; |
| 1805 |
g_autoptr(GError) error = NULL; |
| 1806 |
|
| 1807 |
tlv = mbim_tlv_string_new (str, &error); |
| 1808 |
if (!tlv) |
| 1809 |
g_warning ("Error appending TLV: %s", error->message); |
| 1810 |
else |
| 1811 |
_mbim_message_command_builder_append_tlv (builder, tlv); |
| 1812 |
} |
| 1813 |
|
| 1814 |
void |
| 1815 |
_mbim_message_command_builder_append_tlv_list (MbimMessageCommandBuilder *builder, |
| 1816 |
const GList *tlvs) |
| 1817 |
{ |
| 1818 |
const GList *l; |
| 1819 |
|
| 1820 |
for (l = tlvs; l; l = g_list_next (l)) |
| 1821 |
_mbim_message_command_builder_append_tlv (builder, (MbimTlv *)(l->data)); |
| 1822 |
} |
| 1823 |
|
| 1824 |
/*****************************************************************************/ |
| 1825 |
/* Generic message interface */ |
| 1826 |
|
| 1827 |
MbimMessage * |
| 1828 |
mbim_message_ref (MbimMessage *self) |
| 1829 |
{ |
| 1830 |
g_return_val_if_fail (self != NULL, NULL); |
| 1831 |
|
| 1832 |
return (MbimMessage *) g_byte_array_ref ((GByteArray *)self); |
| 1833 |
} |
| 1834 |
|
| 1835 |
void |
| 1836 |
mbim_message_unref (MbimMessage *self) |
| 1837 |
{ |
| 1838 |
g_return_if_fail (self != NULL); |
| 1839 |
|
| 1840 |
g_byte_array_unref ((GByteArray *)self); |
| 1841 |
} |
| 1842 |
|
| 1843 |
MbimMessageType |
| 1844 |
mbim_message_get_message_type (const MbimMessage *self) |
| 1845 |
{ |
| 1846 |
g_return_val_if_fail (self != NULL, MBIM_MESSAGE_TYPE_INVALID); |
| 1847 |
g_return_val_if_fail (_mbim_message_validate_generic_header (self, NULL), MBIM_MESSAGE_TYPE_INVALID); |
| 1848 |
|
| 1849 |
return MBIM_MESSAGE_GET_MESSAGE_TYPE (self); |
| 1850 |
} |
| 1851 |
|
| 1852 |
guint32 |
| 1853 |
mbim_message_get_message_length (const MbimMessage *self) |
| 1854 |
{ |
| 1855 |
g_return_val_if_fail (self != NULL, 0); |
| 1856 |
g_return_val_if_fail (_mbim_message_validate_generic_header (self, NULL), 0); |
| 1857 |
|
| 1858 |
return MBIM_MESSAGE_GET_MESSAGE_LENGTH (self); |
| 1859 |
} |
| 1860 |
|
| 1861 |
guint32 |
| 1862 |
mbim_message_get_transaction_id (const MbimMessage *self) |
| 1863 |
{ |
| 1864 |
g_return_val_if_fail (self != NULL, 0); |
| 1865 |
g_return_val_if_fail (_mbim_message_validate_generic_header (self, NULL), 0); |
| 1866 |
|
| 1867 |
return MBIM_MESSAGE_GET_TRANSACTION_ID (self); |
| 1868 |
} |
| 1869 |
|
| 1870 |
void |
| 1871 |
mbim_message_set_transaction_id (MbimMessage *self, |
| 1872 |
guint32 transaction_id) |
| 1873 |
{ |
| 1874 |
g_return_if_fail (self != NULL); |
| 1875 |
g_return_if_fail (_mbim_message_validate_generic_header (self, NULL)); |
| 1876 |
|
| 1877 |
((struct header *)(self->data))->transaction_id = GUINT32_TO_LE (transaction_id); |
| 1878 |
} |
| 1879 |
|
| 1880 |
MbimMessage * |
| 1881 |
mbim_message_new (const guint8 *data, |
| 1882 |
guint32 data_length) |
| 1883 |
{ |
| 1884 |
GByteArray *out; |
| 1885 |
|
| 1886 |
/* Create output MbimMessage */ |
| 1887 |
out = g_byte_array_sized_new (data_length); |
| 1888 |
g_byte_array_append (out, data, data_length); |
| 1889 |
|
| 1890 |
return (MbimMessage *)out; |
| 1891 |
} |
| 1892 |
|
| 1893 |
MbimMessage * |
| 1894 |
mbim_message_dup (const MbimMessage *self) |
| 1895 |
{ |
| 1896 |
g_return_val_if_fail (self != NULL, NULL); |
| 1897 |
|
| 1898 |
return mbim_message_new (((GByteArray *)self)->data, |
| 1899 |
MBIM_MESSAGE_GET_MESSAGE_LENGTH (self)); |
| 1900 |
} |
| 1901 |
|
| 1902 |
const guint8 * |
| 1903 |
mbim_message_get_raw (const MbimMessage *self, |
| 1904 |
guint32 *length, |
| 1905 |
GError **error) |
| 1906 |
{ |
| 1907 |
g_return_val_if_fail (self != NULL, NULL); |
| 1908 |
g_return_val_if_fail (length != NULL, NULL); |
| 1909 |
|
| 1910 |
if (!self->data || !self->len) { |
| 1911 |
g_set_error_literal (error, |
| 1912 |
MBIM_CORE_ERROR, |
| 1913 |
MBIM_CORE_ERROR_FAILED, |
| 1914 |
"Message is empty"); |
| 1915 |
return NULL; |
| 1916 |
} |
| 1917 |
|
| 1918 |
*length = (guint32) self->len; |
| 1919 |
return self->data; |
| 1920 |
} |
| 1921 |
|
| 1922 |
gchar * |
| 1923 |
mbim_message_get_printable (const MbimMessage *self, |
| 1924 |
const gchar *line_prefix, |
| 1925 |
gboolean headers_only) |
| 1926 |
{ |
| 1927 |
return mbim_message_get_printable_full (self, 1, 0, line_prefix, headers_only, NULL); |
| 1928 |
} |
| 1929 |
|
| 1930 |
gchar * |
| 1931 |
mbim_message_get_printable_full (const MbimMessage *self, |
| 1932 |
guint8 mbimex_version_major, |
| 1933 |
guint8 mbimex_version_minor, |
| 1934 |
const gchar *line_prefix, |
| 1935 |
gboolean headers_only, |
| 1936 |
GError **error) |
| 1937 |
{ |
| 1938 |
GString *printable = NULL; |
| 1939 |
MbimService service_read_fields = MBIM_SERVICE_INVALID; |
| 1940 |
|
| 1941 |
g_return_val_if_fail (self != NULL, NULL); |
| 1942 |
g_return_val_if_fail (line_prefix != NULL, NULL); |
| 1943 |
g_return_val_if_fail (_mbim_message_validate_internal (self, TRUE, NULL), NULL); |
| 1944 |
|
| 1945 |
if (mbimex_version_major > 3) { |
| 1946 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_ARGS, |
| 1947 |
"MBIMEx version %x.%02x is unsupported", |
| 1948 |
mbimex_version_major, mbimex_version_minor); |
| 1949 |
return NULL; |
| 1950 |
} |
| 1951 |
|
| 1952 |
if (!line_prefix) |
| 1953 |
line_prefix = ""; |
| 1954 |
|
| 1955 |
printable = g_string_new (""); |
| 1956 |
g_string_append_printf (printable, |
| 1957 |
"%sHeader:\n" |
| 1958 |
"%s length = %u\n" |
| 1959 |
"%s type = %s (0x%08x)\n" |
| 1960 |
"%s transaction = %u\n", |
| 1961 |
line_prefix, |
| 1962 |
line_prefix, MBIM_MESSAGE_GET_MESSAGE_LENGTH (self), |
| 1963 |
line_prefix, mbim_message_type_get_string (MBIM_MESSAGE_GET_MESSAGE_TYPE (self)), MBIM_MESSAGE_GET_MESSAGE_TYPE (self), |
| 1964 |
line_prefix, MBIM_MESSAGE_GET_TRANSACTION_ID (self)); |
| 1965 |
|
| 1966 |
switch (MBIM_MESSAGE_GET_MESSAGE_TYPE (self)) { |
| 1967 |
case MBIM_MESSAGE_TYPE_INVALID: |
| 1968 |
g_warn_if_reached (); |
| 1969 |
break; |
| 1970 |
|
| 1971 |
case MBIM_MESSAGE_TYPE_OPEN: |
| 1972 |
if (!headers_only) |
| 1973 |
g_string_append_printf (printable, |
| 1974 |
"%sContents:\n" |
| 1975 |
"%s max control transfer = %u\n", |
| 1976 |
line_prefix, |
| 1977 |
line_prefix, mbim_message_open_get_max_control_transfer (self)); |
| 1978 |
break; |
| 1979 |
|
| 1980 |
case MBIM_MESSAGE_TYPE_CLOSE: |
| 1981 |
break; |
| 1982 |
|
| 1983 |
case MBIM_MESSAGE_TYPE_OPEN_DONE: |
| 1984 |
if (!headers_only) { |
| 1985 |
MbimStatusError status; |
| 1986 |
|
| 1987 |
status = mbim_message_open_done_get_status_code (self); |
| 1988 |
g_string_append_printf (printable, |
| 1989 |
"%sContents:\n" |
| 1990 |
"%s status error = '%s' (0x%08x)\n", |
| 1991 |
line_prefix, |
| 1992 |
line_prefix, mbim_status_error_get_string (status), status); |
| 1993 |
} |
| 1994 |
break; |
| 1995 |
|
| 1996 |
case MBIM_MESSAGE_TYPE_CLOSE_DONE: |
| 1997 |
if (!headers_only) { |
| 1998 |
MbimStatusError status; |
| 1999 |
|
| 2000 |
status = mbim_message_close_done_get_status_code (self); |
| 2001 |
g_string_append_printf (printable, |
| 2002 |
"%sContents:\n" |
| 2003 |
"%s status error = '%s' (0x%08x)\n", |
| 2004 |
line_prefix, |
| 2005 |
line_prefix, mbim_status_error_get_string (status), status); |
| 2006 |
} |
| 2007 |
break; |
| 2008 |
|
| 2009 |
case MBIM_MESSAGE_TYPE_HOST_ERROR: |
| 2010 |
case MBIM_MESSAGE_TYPE_FUNCTION_ERROR: |
| 2011 |
if (!headers_only) { |
| 2012 |
MbimProtocolError protocol_error; |
| 2013 |
|
| 2014 |
protocol_error = mbim_message_error_get_error_status_code (self); |
| 2015 |
g_string_append_printf (printable, |
| 2016 |
"%sContents:\n" |
| 2017 |
"%s error = '%s' (0x%08x)\n", |
| 2018 |
line_prefix, |
| 2019 |
line_prefix, mbim_protocol_error_get_string (protocol_error), protocol_error); |
| 2020 |
} |
| 2021 |
break; |
| 2022 |
|
| 2023 |
case MBIM_MESSAGE_TYPE_COMMAND: |
| 2024 |
g_string_append_printf (printable, |
| 2025 |
"%sFragment header:\n" |
| 2026 |
"%s total = %u\n" |
| 2027 |
"%s current = %u\n", |
| 2028 |
line_prefix, |
| 2029 |
line_prefix, _mbim_message_fragment_get_total (self), |
| 2030 |
line_prefix, _mbim_message_fragment_get_current (self)); |
| 2031 |
if (!headers_only) { |
| 2032 |
g_autofree gchar *uuid_printable = NULL; |
| 2033 |
const gchar *cid_printable; |
| 2034 |
|
| 2035 |
service_read_fields = mbim_message_command_get_service (self); |
| 2036 |
|
| 2037 |
uuid_printable = mbim_uuid_get_printable (mbim_message_command_get_service_id (self)); |
| 2038 |
cid_printable = mbim_cid_get_printable (mbim_message_command_get_service (self), |
| 2039 |
mbim_message_command_get_cid (self)); |
| 2040 |
g_string_append_printf (printable, |
| 2041 |
"%sContents:\n" |
| 2042 |
"%s service = '%s' (%s)\n" |
| 2043 |
"%s cid = '%s' (0x%08x)\n" |
| 2044 |
"%s type = '%s' (0x%08x)\n", |
| 2045 |
line_prefix, |
| 2046 |
line_prefix, mbim_service_lookup_name (mbim_message_command_get_service (self)), uuid_printable, |
| 2047 |
line_prefix, cid_printable, mbim_message_command_get_cid (self), |
| 2048 |
line_prefix, mbim_message_command_type_get_string (mbim_message_command_get_command_type (self)), mbim_message_command_get_command_type (self)); |
| 2049 |
} |
| 2050 |
break; |
| 2051 |
|
| 2052 |
case MBIM_MESSAGE_TYPE_COMMAND_DONE: |
| 2053 |
g_string_append_printf (printable, |
| 2054 |
"%sFragment header:\n" |
| 2055 |
"%s total = %u\n" |
| 2056 |
"%s current = %u\n", |
| 2057 |
line_prefix, |
| 2058 |
line_prefix, _mbim_message_fragment_get_total (self), |
| 2059 |
line_prefix, _mbim_message_fragment_get_current (self)); |
| 2060 |
if (!headers_only) { |
| 2061 |
g_autofree gchar *uuid_printable = NULL; |
| 2062 |
MbimStatusError status; |
| 2063 |
const gchar *cid_printable; |
| 2064 |
|
| 2065 |
service_read_fields = mbim_message_command_done_get_service (self); |
| 2066 |
|
| 2067 |
status = mbim_message_command_done_get_status_code (self); |
| 2068 |
uuid_printable = mbim_uuid_get_printable (mbim_message_command_done_get_service_id (self)); |
| 2069 |
cid_printable = mbim_cid_get_printable (mbim_message_command_done_get_service (self), |
| 2070 |
mbim_message_command_done_get_cid (self)); |
| 2071 |
g_string_append_printf (printable, |
| 2072 |
"%sContents:\n" |
| 2073 |
"%s status error = '%s' (0x%08x)\n" |
| 2074 |
"%s service = '%s' (%s)\n" |
| 2075 |
"%s cid = '%s' (0x%08x)\n", |
| 2076 |
line_prefix, |
| 2077 |
line_prefix, mbim_status_error_get_string (status), status, |
| 2078 |
line_prefix, mbim_service_lookup_name (mbim_message_command_done_get_service (self)), uuid_printable, |
| 2079 |
line_prefix, cid_printable, mbim_message_command_done_get_cid (self)); |
| 2080 |
} |
| 2081 |
break; |
| 2082 |
|
| 2083 |
case MBIM_MESSAGE_TYPE_INDICATE_STATUS: |
| 2084 |
g_string_append_printf (printable, |
| 2085 |
"%sFragment header:\n" |
| 2086 |
"%s total = %u\n" |
| 2087 |
"%s current = %u\n", |
| 2088 |
line_prefix, |
| 2089 |
line_prefix, _mbim_message_fragment_get_total (self), |
| 2090 |
line_prefix, _mbim_message_fragment_get_current (self)); |
| 2091 |
if (!headers_only) { |
| 2092 |
g_autofree gchar *uuid_printable = NULL; |
| 2093 |
const gchar *cid_printable; |
| 2094 |
|
| 2095 |
service_read_fields = mbim_message_indicate_status_get_service (self); |
| 2096 |
|
| 2097 |
uuid_printable = mbim_uuid_get_printable (mbim_message_indicate_status_get_service_id (self)); |
| 2098 |
cid_printable = mbim_cid_get_printable (mbim_message_indicate_status_get_service (self), |
| 2099 |
mbim_message_indicate_status_get_cid (self)); |
| 2100 |
g_string_append_printf (printable, |
| 2101 |
"%sContents:\n" |
| 2102 |
"%s service = '%s' (%s)\n" |
| 2103 |
"%s cid = '%s' (0x%08x)\n", |
| 2104 |
line_prefix, |
| 2105 |
line_prefix, mbim_service_lookup_name (mbim_message_indicate_status_get_service (self)), uuid_printable, |
| 2106 |
line_prefix, cid_printable, mbim_message_indicate_status_get_cid (self)); |
| 2107 |
} |
| 2108 |
break; |
| 2109 |
|
| 2110 |
default: |
| 2111 |
g_assert_not_reached (); |
| 2112 |
} |
| 2113 |
|
| 2114 |
if (service_read_fields != MBIM_SERVICE_INVALID) { |
| 2115 |
g_autofree gchar *fields_printable = NULL; |
| 2116 |
g_autoptr(GError) inner_error = NULL; |
| 2117 |
|
| 2118 |
switch (service_read_fields) { |
| 2119 |
case MBIM_SERVICE_BASIC_CONNECT: |
| 2120 |
if (mbimex_version_major < 2) |
| 2121 |
fields_printable = __mbim_message_basic_connect_get_printable_fields (self, line_prefix, &inner_error); |
| 2122 |
else if (mbimex_version_major == 2) { |
| 2123 |
fields_printable = __mbim_message_ms_basic_connect_v2_get_printable_fields (self, line_prefix, &inner_error); |
| 2124 |
/* attempt fallback to v1 printable */ |
| 2125 |
if (g_error_matches (inner_error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_UNSUPPORTED)) { |
| 2126 |
g_clear_error (&inner_error); |
| 2127 |
fields_printable = __mbim_message_basic_connect_get_printable_fields (self, line_prefix, &inner_error); |
| 2128 |
} |
| 2129 |
} else if (mbimex_version_major == 3) { |
| 2130 |
fields_printable = __mbim_message_ms_basic_connect_v3_get_printable_fields (self, line_prefix, &inner_error); |
| 2131 |
/* attempt fallback to v2 printable */ |
| 2132 |
if (g_error_matches (inner_error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_UNSUPPORTED)) { |
| 2133 |
g_clear_error (&inner_error); |
| 2134 |
fields_printable = __mbim_message_ms_basic_connect_v2_get_printable_fields (self, line_prefix, &inner_error); |
| 2135 |
/* attempt fallback to v1 printable */ |
| 2136 |
if (g_error_matches (inner_error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_UNSUPPORTED)) { |
| 2137 |
g_clear_error (&inner_error); |
| 2138 |
fields_printable = __mbim_message_basic_connect_get_printable_fields (self, line_prefix, &inner_error); |
| 2139 |
} |
| 2140 |
} |
| 2141 |
} else |
| 2142 |
g_assert_not_reached (); |
| 2143 |
break; |
| 2144 |
case MBIM_SERVICE_SMS: |
| 2145 |
fields_printable = __mbim_message_sms_get_printable_fields (self, line_prefix, &inner_error); |
| 2146 |
break; |
| 2147 |
case MBIM_SERVICE_USSD: |
| 2148 |
fields_printable = __mbim_message_ussd_get_printable_fields (self, line_prefix, &inner_error); |
| 2149 |
break; |
| 2150 |
case MBIM_SERVICE_PHONEBOOK: |
| 2151 |
fields_printable = __mbim_message_phonebook_get_printable_fields (self, line_prefix, &inner_error); |
| 2152 |
break; |
| 2153 |
case MBIM_SERVICE_STK: |
| 2154 |
fields_printable = __mbim_message_stk_get_printable_fields (self, line_prefix, &inner_error); |
| 2155 |
break; |
| 2156 |
case MBIM_SERVICE_AUTH: |
| 2157 |
fields_printable = __mbim_message_auth_get_printable_fields (self, line_prefix, &inner_error); |
| 2158 |
break; |
| 2159 |
case MBIM_SERVICE_DSS: |
| 2160 |
fields_printable = __mbim_message_dss_get_printable_fields (self, line_prefix, &inner_error); |
| 2161 |
break; |
| 2162 |
case MBIM_SERVICE_MS_FIRMWARE_ID: |
| 2163 |
fields_printable = __mbim_message_ms_firmware_id_get_printable_fields (self, line_prefix, &inner_error); |
| 2164 |
break; |
| 2165 |
case MBIM_SERVICE_MS_HOST_SHUTDOWN: |
| 2166 |
fields_printable = __mbim_message_ms_host_shutdown_get_printable_fields (self, line_prefix, &inner_error); |
| 2167 |
break; |
| 2168 |
case MBIM_SERVICE_MS_SAR: |
| 2169 |
fields_printable = __mbim_message_ms_sar_get_printable_fields (self, line_prefix, &inner_error); |
| 2170 |
break; |
| 2171 |
case MBIM_SERVICE_PROXY_CONTROL: |
| 2172 |
fields_printable = __mbim_message_proxy_control_get_printable_fields (self, line_prefix, &inner_error); |
| 2173 |
break; |
| 2174 |
case MBIM_SERVICE_QMI: |
| 2175 |
fields_printable = __mbim_message_qmi_get_printable_fields (self, line_prefix, &inner_error); |
| 2176 |
break; |
| 2177 |
case MBIM_SERVICE_ATDS: |
| 2178 |
fields_printable = __mbim_message_atds_get_printable_fields (self, line_prefix, &inner_error); |
| 2179 |
break; |
| 2180 |
case MBIM_SERVICE_INTEL_FIRMWARE_UPDATE: |
| 2181 |
if (mbimex_version_major < 2) |
| 2182 |
fields_printable = __mbim_message_intel_firmware_update_get_printable_fields (self, line_prefix, &inner_error); |
| 2183 |
else if (mbimex_version_major >= 2) { |
| 2184 |
fields_printable = __mbim_message_intel_firmware_update_v2_get_printable_fields (self, line_prefix, &inner_error); |
| 2185 |
if (g_error_matches (inner_error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_UNSUPPORTED)) { |
| 2186 |
g_clear_error (&inner_error); |
| 2187 |
fields_printable = __mbim_message_intel_firmware_update_get_printable_fields (self, line_prefix, &inner_error); |
| 2188 |
} |
| 2189 |
} |
| 2190 |
break; |
| 2191 |
case MBIM_SERVICE_QDU: |
| 2192 |
fields_printable = __mbim_message_qdu_get_printable_fields (self, line_prefix, &inner_error); |
| 2193 |
break; |
| 2194 |
case MBIM_SERVICE_MS_BASIC_CONNECT_EXTENSIONS: |
| 2195 |
if (mbimex_version_major < 2) |
| 2196 |
fields_printable = __mbim_message_ms_basic_connect_extensions_get_printable_fields (self, line_prefix, &inner_error); |
| 2197 |
else if (mbimex_version_major == 2) { |
| 2198 |
fields_printable = __mbim_message_ms_basic_connect_extensions_v2_get_printable_fields (self, line_prefix, &inner_error); |
| 2199 |
/* attempt fallback to v1 printable */ |
| 2200 |
if (g_error_matches (inner_error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_UNSUPPORTED)) { |
| 2201 |
g_clear_error (&inner_error); |
| 2202 |
fields_printable = __mbim_message_ms_basic_connect_extensions_get_printable_fields (self, line_prefix, &inner_error); |
| 2203 |
} |
| 2204 |
} else if (mbimex_version_major == 3) { |
| 2205 |
fields_printable = __mbim_message_ms_basic_connect_extensions_v3_get_printable_fields (self, line_prefix, &inner_error); |
| 2206 |
/* attempt fallback to v2 printable */ |
| 2207 |
if (g_error_matches (inner_error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_UNSUPPORTED)) { |
| 2208 |
g_clear_error (&inner_error); |
| 2209 |
fields_printable = __mbim_message_ms_basic_connect_extensions_v2_get_printable_fields (self, line_prefix, &inner_error); |
| 2210 |
/* attempt fallback to v1 printable */ |
| 2211 |
if (g_error_matches (inner_error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_UNSUPPORTED)) { |
| 2212 |
g_clear_error (&inner_error); |
| 2213 |
fields_printable = __mbim_message_ms_basic_connect_extensions_get_printable_fields (self, line_prefix, &inner_error); |
| 2214 |
} |
| 2215 |
} |
| 2216 |
} else |
| 2217 |
g_assert_not_reached (); |
| 2218 |
break; |
| 2219 |
case MBIM_SERVICE_MS_UICC_LOW_LEVEL_ACCESS: |
| 2220 |
fields_printable = __mbim_message_ms_uicc_low_level_access_get_printable_fields (self, line_prefix, &inner_error); |
| 2221 |
break; |
| 2222 |
case MBIM_SERVICE_QUECTEL: |
| 2223 |
fields_printable = __mbim_message_quectel_get_printable_fields (self, line_prefix, &inner_error); |
| 2224 |
break; |
| 2225 |
case MBIM_SERVICE_INTEL_THERMAL_RF: |
| 2226 |
fields_printable = __mbim_message_intel_thermal_rf_get_printable_fields (self, line_prefix, &inner_error); |
| 2227 |
break; |
| 2228 |
case MBIM_SERVICE_MS_VOICE_EXTENSIONS: |
| 2229 |
fields_printable = __mbim_message_ms_voice_extensions_get_printable_fields (self, line_prefix, &inner_error); |
| 2230 |
break; |
| 2231 |
case MBIM_SERVICE_INTEL_MUTUAL_AUTHENTICATION: |
| 2232 |
fields_printable = __mbim_message_intel_mutual_authentication_get_printable_fields (self, line_prefix, &inner_error); |
| 2233 |
break; |
| 2234 |
case MBIM_SERVICE_INTEL_TOOLS: |
| 2235 |
fields_printable = __mbim_message_intel_tools_get_printable_fields (self, line_prefix, &inner_error); |
| 2236 |
break; |
| 2237 |
case MBIM_SERVICE_GOOGLE: |
| 2238 |
fields_printable = __mbim_message_google_get_printable_fields (self, line_prefix, &inner_error); |
| 2239 |
break; |
| 2240 |
case MBIM_SERVICE_FIBOCOM: |
| 2241 |
fields_printable = __mbim_message_fibocom_get_printable_fields (self, line_prefix, &inner_error); |
| 2242 |
break; |
| 2243 |
case MBIM_SERVICE_COMPAL: |
| 2244 |
fields_printable = __mbim_message_compal_get_printable_fields (self, line_prefix, &inner_error); |
| 2245 |
break; |
| 2246 |
case MBIM_SERVICE_INTEL_AT_TUNNEL: |
| 2247 |
fields_printable = __mbim_message_intel_at_tunnel_get_printable_fields (self, line_prefix, &inner_error); |
| 2248 |
break; |
| 2249 |
case MBIM_SERVICE_INVALID: |
| 2250 |
case MBIM_SERVICE_LAST: |
| 2251 |
g_assert_not_reached (); |
| 2252 |
default: |
| 2253 |
break; |
| 2254 |
} |
| 2255 |
|
| 2256 |
if (inner_error) |
| 2257 |
g_string_append_printf (printable, |
| 2258 |
"%sFields: %s\n", |
| 2259 |
line_prefix, inner_error->message); |
| 2260 |
else if (fields_printable && fields_printable[0]) |
| 2261 |
g_string_append_printf (printable, |
| 2262 |
"%sFields:\n" |
| 2263 |
"%s", |
| 2264 |
line_prefix, fields_printable); |
| 2265 |
} |
| 2266 |
|
| 2267 |
return g_string_free (printable, FALSE); |
| 2268 |
} |
| 2269 |
|
| 2270 |
/*****************************************************************************/ |
| 2271 |
/* Fragment interface */ |
| 2272 |
|
| 2273 |
gboolean |
| 2274 |
_mbim_message_is_fragment (const MbimMessage *self) |
| 2275 |
{ |
| 2276 |
return MBIM_MESSAGE_IS_FRAGMENT (self); |
| 2277 |
} |
| 2278 |
|
| 2279 |
guint32 |
| 2280 |
_mbim_message_fragment_get_total (const MbimMessage *self) |
| 2281 |
{ |
| 2282 |
g_assert (MBIM_MESSAGE_IS_FRAGMENT (self)); |
| 2283 |
|
| 2284 |
return MBIM_MESSAGE_FRAGMENT_GET_TOTAL (self); |
| 2285 |
} |
| 2286 |
|
| 2287 |
guint32 |
| 2288 |
_mbim_message_fragment_get_current (const MbimMessage *self) |
| 2289 |
{ |
| 2290 |
g_assert (MBIM_MESSAGE_IS_FRAGMENT (self)); |
| 2291 |
|
| 2292 |
return MBIM_MESSAGE_FRAGMENT_GET_CURRENT (self); |
| 2293 |
} |
| 2294 |
|
| 2295 |
const guint8 * |
| 2296 |
_mbim_message_fragment_get_payload (const MbimMessage *self, |
| 2297 |
guint32 *length) |
| 2298 |
{ |
| 2299 |
g_assert (MBIM_MESSAGE_IS_FRAGMENT (self)); |
| 2300 |
|
| 2301 |
*length = (MBIM_MESSAGE_GET_MESSAGE_LENGTH (self) - \ |
| 2302 |
sizeof (struct header) - \ |
| 2303 |
sizeof (struct fragment_header)); |
| 2304 |
return ((struct full_message *)(self->data))->message.fragment.buffer; |
| 2305 |
} |
| 2306 |
|
| 2307 |
MbimMessage * |
| 2308 |
_mbim_message_fragment_collector_init (const MbimMessage *fragment, |
| 2309 |
GError **error) |
| 2310 |
{ |
| 2311 |
g_assert (MBIM_MESSAGE_IS_FRAGMENT (fragment)); |
| 2312 |
|
| 2313 |
/* Collector must start with fragment #0 */ |
| 2314 |
if (MBIM_MESSAGE_FRAGMENT_GET_CURRENT (fragment) != 0) { |
| 2315 |
g_set_error (error, |
| 2316 |
MBIM_PROTOCOL_ERROR, |
| 2317 |
MBIM_PROTOCOL_ERROR_FRAGMENT_OUT_OF_SEQUENCE, |
| 2318 |
"Expecting fragment '0/%u', got '%u/%u'", |
| 2319 |
MBIM_MESSAGE_FRAGMENT_GET_TOTAL (fragment), |
| 2320 |
MBIM_MESSAGE_FRAGMENT_GET_CURRENT (fragment), |
| 2321 |
MBIM_MESSAGE_FRAGMENT_GET_TOTAL (fragment)); |
| 2322 |
return NULL; |
| 2323 |
} |
| 2324 |
|
| 2325 |
return mbim_message_dup (fragment); |
| 2326 |
} |
| 2327 |
|
| 2328 |
gboolean |
| 2329 |
_mbim_message_fragment_collector_add (MbimMessage *self, |
| 2330 |
const MbimMessage *fragment, |
| 2331 |
GError **error) |
| 2332 |
{ |
| 2333 |
guint32 buffer_len; |
| 2334 |
const guint8 *buffer; |
| 2335 |
|
| 2336 |
g_assert (MBIM_MESSAGE_IS_FRAGMENT (self)); |
| 2337 |
g_assert (MBIM_MESSAGE_IS_FRAGMENT (fragment)); |
| 2338 |
|
| 2339 |
/* We can only add a fragment if it is the next one we're expecting. |
| 2340 |
* Otherwise, we return an error. */ |
| 2341 |
if (MBIM_MESSAGE_FRAGMENT_GET_CURRENT (self) != (MBIM_MESSAGE_FRAGMENT_GET_CURRENT (fragment) - 1)) { |
| 2342 |
g_set_error (error, |
| 2343 |
MBIM_PROTOCOL_ERROR, |
| 2344 |
MBIM_PROTOCOL_ERROR_FRAGMENT_OUT_OF_SEQUENCE, |
| 2345 |
"Expecting fragment '%u/%u', got '%u/%u'", |
| 2346 |
MBIM_MESSAGE_FRAGMENT_GET_CURRENT (self) + 1, |
| 2347 |
MBIM_MESSAGE_FRAGMENT_GET_TOTAL (self), |
| 2348 |
MBIM_MESSAGE_FRAGMENT_GET_CURRENT (fragment), |
| 2349 |
MBIM_MESSAGE_FRAGMENT_GET_TOTAL (fragment)); |
| 2350 |
return FALSE; |
| 2351 |
} |
| 2352 |
|
| 2353 |
buffer = _mbim_message_fragment_get_payload (fragment, &buffer_len); |
| 2354 |
if (buffer_len) { |
| 2355 |
/* Concatenate information buffers */ |
| 2356 |
g_byte_array_append ((GByteArray *)self, buffer, buffer_len); |
| 2357 |
/* Update the whole message length */ |
| 2358 |
((struct header *)(self->data))->length = |
| 2359 |
GUINT32_TO_LE (MBIM_MESSAGE_GET_MESSAGE_LENGTH (self) + buffer_len); |
| 2360 |
} |
| 2361 |
|
| 2362 |
/* Update the current fragment info in the main message; skip endian changes */ |
| 2363 |
((struct full_message *)(self->data))->message.fragment.fragment_header.current = |
| 2364 |
((struct full_message *)(fragment->data))->message.fragment.fragment_header.current; |
| 2365 |
|
| 2366 |
return TRUE; |
| 2367 |
} |
| 2368 |
|
| 2369 |
gboolean |
| 2370 |
_mbim_message_fragment_collector_complete (MbimMessage *self) |
| 2371 |
{ |
| 2372 |
g_assert (MBIM_MESSAGE_IS_FRAGMENT (self)); |
| 2373 |
|
| 2374 |
if (MBIM_MESSAGE_FRAGMENT_GET_CURRENT (self) != (MBIM_MESSAGE_FRAGMENT_GET_TOTAL (self) - 1)) |
| 2375 |
/* Not complete yet */ |
| 2376 |
return FALSE; |
| 2377 |
|
| 2378 |
/* Reset current & total */ |
| 2379 |
((struct full_message *)(self->data))->message.fragment.fragment_header.current = 0; |
| 2380 |
((struct full_message *)(self->data))->message.fragment.fragment_header.total = GUINT32_TO_LE (1); |
| 2381 |
return TRUE; |
| 2382 |
} |
| 2383 |
|
| 2384 |
struct fragment_info * |
| 2385 |
_mbim_message_split_fragments (const MbimMessage *self, |
| 2386 |
guint32 max_fragment_size, |
| 2387 |
guint *n_fragments) |
| 2388 |
{ |
| 2389 |
GArray *array; |
| 2390 |
guint32 total_message_length; |
| 2391 |
guint32 total_payload_length; |
| 2392 |
guint32 fragment_header_length; |
| 2393 |
guint32 fragment_payload_length; |
| 2394 |
guint32 total_fragments; |
| 2395 |
guint i; |
| 2396 |
const guint8 *data; |
| 2397 |
guint32 data_length; |
| 2398 |
|
| 2399 |
/* A message which is longer than the maximum fragment size needs to be |
| 2400 |
* split in different fragments before sending it. */ |
| 2401 |
|
| 2402 |
total_message_length = mbim_message_get_message_length (self); |
| 2403 |
|
| 2404 |
/* If a single fragment is enough, don't try to split */ |
| 2405 |
if (total_message_length <= max_fragment_size) |
| 2406 |
return NULL; |
| 2407 |
|
| 2408 |
/* Total payload length is the total length minus the headers of the |
| 2409 |
* input message */ |
| 2410 |
fragment_header_length = sizeof (struct header) + sizeof (struct fragment_header); |
| 2411 |
total_payload_length = total_message_length - fragment_header_length; |
| 2412 |
|
| 2413 |
/* Fragment payload length is the maximum amount of data that can fit in a |
| 2414 |
* single fragment */ |
| 2415 |
fragment_payload_length = max_fragment_size - fragment_header_length; |
| 2416 |
|
| 2417 |
/* We can now compute the number of fragments that we'll get */ |
| 2418 |
total_fragments = total_payload_length / fragment_payload_length; |
| 2419 |
if (total_payload_length % fragment_payload_length) |
| 2420 |
total_fragments++; |
| 2421 |
|
| 2422 |
array = g_array_sized_new (FALSE, |
| 2423 |
FALSE, |
| 2424 |
sizeof (struct fragment_info), |
| 2425 |
total_fragments); |
| 2426 |
|
| 2427 |
/* Initialize data walkers */ |
| 2428 |
data = ((struct full_message *)(((GByteArray *)self)->data))->message.fragment.buffer; |
| 2429 |
data_length = total_payload_length; |
| 2430 |
|
| 2431 |
/* Create fragment infos */ |
| 2432 |
for (i = 0; i < total_fragments; i++) { |
| 2433 |
struct fragment_info info; |
| 2434 |
|
| 2435 |
/* Set data info */ |
| 2436 |
info.data = data; |
| 2437 |
info.data_length = (data_length > fragment_payload_length ? fragment_payload_length : data_length); |
| 2438 |
|
| 2439 |
/* Set header info */ |
| 2440 |
info.header.type = GUINT32_TO_LE (MBIM_MESSAGE_GET_MESSAGE_TYPE (self)); |
| 2441 |
info.header.length = GUINT32_TO_LE (fragment_header_length + info.data_length); |
| 2442 |
info.header.transaction_id = GUINT32_TO_LE (MBIM_MESSAGE_GET_TRANSACTION_ID (self)); |
| 2443 |
info.fragment_header.total = GUINT32_TO_LE (total_fragments); |
| 2444 |
info.fragment_header.current = GUINT32_TO_LE (i); |
| 2445 |
|
| 2446 |
g_array_insert_val (array, i, info); |
| 2447 |
|
| 2448 |
/* Update walkers */ |
| 2449 |
data = &data[info.data_length]; |
| 2450 |
data_length -= info.data_length; |
| 2451 |
} |
| 2452 |
|
| 2453 |
g_warn_if_fail (data_length == 0); |
| 2454 |
|
| 2455 |
*n_fragments = total_fragments; |
| 2456 |
return (struct fragment_info *) g_array_free (array, FALSE); |
| 2457 |
} |
| 2458 |
|
| 2459 |
/*****************************************************************************/ |
| 2460 |
/* 'Open' message interface */ |
| 2461 |
|
| 2462 |
MbimMessage * |
| 2463 |
mbim_message_open_new (guint32 transaction_id, |
| 2464 |
guint32 max_control_transfer) |
| 2465 |
{ |
| 2466 |
GByteArray *self; |
| 2467 |
|
| 2468 |
self = _mbim_message_allocate (MBIM_MESSAGE_TYPE_OPEN, |
| 2469 |
transaction_id, |
| 2470 |
sizeof (struct open_message)); |
| 2471 |
|
| 2472 |
/* Open header */ |
| 2473 |
((struct full_message *)(self->data))->message.open.max_control_transfer = GUINT32_TO_LE (max_control_transfer); |
| 2474 |
|
| 2475 |
return (MbimMessage *)self; |
| 2476 |
} |
| 2477 |
|
| 2478 |
guint32 |
| 2479 |
mbim_message_open_get_max_control_transfer (const MbimMessage *self) |
| 2480 |
{ |
| 2481 |
g_return_val_if_fail (self != NULL, 0); |
| 2482 |
g_return_val_if_fail (_mbim_message_validate_type_header (self, NULL), 0); |
| 2483 |
g_return_val_if_fail (MBIM_MESSAGE_GET_MESSAGE_TYPE (self) == MBIM_MESSAGE_TYPE_OPEN, 0); |
| 2484 |
|
| 2485 |
return GUINT32_FROM_LE (((struct full_message *)(self->data))->message.open.max_control_transfer); |
| 2486 |
} |
| 2487 |
|
| 2488 |
/*****************************************************************************/ |
| 2489 |
/* 'Open Done' message interface */ |
| 2490 |
|
| 2491 |
MbimMessage * |
| 2492 |
mbim_message_open_done_new (guint32 transaction_id, |
| 2493 |
MbimStatusError error_status_code) |
| 2494 |
{ |
| 2495 |
GByteArray *self; |
| 2496 |
|
| 2497 |
self = _mbim_message_allocate (MBIM_MESSAGE_TYPE_OPEN_DONE, |
| 2498 |
transaction_id, |
| 2499 |
sizeof (struct open_done_message)); |
| 2500 |
|
| 2501 |
/* Open header */ |
| 2502 |
((struct full_message *)(self->data))->message.open_done.status_code = GUINT32_TO_LE (error_status_code); |
| 2503 |
|
| 2504 |
return (MbimMessage *)self; |
| 2505 |
} |
| 2506 |
|
| 2507 |
MbimStatusError |
| 2508 |
mbim_message_open_done_get_status_code (const MbimMessage *self) |
| 2509 |
{ |
| 2510 |
g_return_val_if_fail (self != NULL, MBIM_STATUS_ERROR_FAILURE); |
| 2511 |
g_return_val_if_fail (_mbim_message_validate_type_header (self, NULL), MBIM_STATUS_ERROR_FAILURE); |
| 2512 |
g_return_val_if_fail (MBIM_MESSAGE_GET_MESSAGE_TYPE (self) == MBIM_MESSAGE_TYPE_OPEN_DONE, MBIM_STATUS_ERROR_FAILURE); |
| 2513 |
|
| 2514 |
return (MbimStatusError) GUINT32_FROM_LE (((struct full_message *)(self->data))->message.open_done.status_code); |
| 2515 |
} |
| 2516 |
|
| 2517 |
gboolean |
| 2518 |
mbim_message_open_done_get_result (const MbimMessage *self, |
| 2519 |
GError **error) |
| 2520 |
{ |
| 2521 |
MbimStatusError status; |
| 2522 |
|
| 2523 |
g_return_val_if_fail (self != NULL, FALSE); |
| 2524 |
g_return_val_if_fail (_mbim_message_validate_type_header (self, NULL), FALSE); |
| 2525 |
g_return_val_if_fail (MBIM_MESSAGE_GET_MESSAGE_TYPE (self) == MBIM_MESSAGE_TYPE_OPEN_DONE, FALSE); |
| 2526 |
|
| 2527 |
status = (MbimStatusError) GUINT32_FROM_LE (((struct full_message *)(self->data))->message.open_done.status_code); |
| 2528 |
if (status == MBIM_STATUS_ERROR_NONE) |
| 2529 |
return TRUE; |
| 2530 |
|
| 2531 |
set_error_from_status (error, status); |
| 2532 |
return FALSE; |
| 2533 |
} |
| 2534 |
|
| 2535 |
/*****************************************************************************/ |
| 2536 |
/* 'Close' message interface */ |
| 2537 |
|
| 2538 |
MbimMessage * |
| 2539 |
mbim_message_close_new (guint32 transaction_id) |
| 2540 |
{ |
| 2541 |
return (MbimMessage *) _mbim_message_allocate (MBIM_MESSAGE_TYPE_CLOSE, |
| 2542 |
transaction_id, |
| 2543 |
0); |
| 2544 |
} |
| 2545 |
|
| 2546 |
/*****************************************************************************/ |
| 2547 |
/* 'Close Done' message interface */ |
| 2548 |
|
| 2549 |
MbimMessage * |
| 2550 |
mbim_message_close_done_new (guint32 transaction_id, |
| 2551 |
MbimStatusError error_status_code) |
| 2552 |
{ |
| 2553 |
GByteArray *self; |
| 2554 |
|
| 2555 |
self = _mbim_message_allocate (MBIM_MESSAGE_TYPE_CLOSE_DONE, |
| 2556 |
transaction_id, |
| 2557 |
sizeof (struct close_done_message)); |
| 2558 |
|
| 2559 |
/* Open header */ |
| 2560 |
((struct full_message *)(self->data))->message.close_done.status_code = GUINT32_TO_LE (error_status_code); |
| 2561 |
|
| 2562 |
return (MbimMessage *)self; |
| 2563 |
} |
| 2564 |
|
| 2565 |
MbimStatusError |
| 2566 |
mbim_message_close_done_get_status_code (const MbimMessage *self) |
| 2567 |
{ |
| 2568 |
g_return_val_if_fail (self != NULL, MBIM_STATUS_ERROR_FAILURE); |
| 2569 |
g_return_val_if_fail (_mbim_message_validate_type_header (self, NULL), MBIM_STATUS_ERROR_FAILURE); |
| 2570 |
g_return_val_if_fail (MBIM_MESSAGE_GET_MESSAGE_TYPE (self) == MBIM_MESSAGE_TYPE_CLOSE_DONE, MBIM_STATUS_ERROR_FAILURE); |
| 2571 |
|
| 2572 |
return (MbimStatusError) GUINT32_FROM_LE (((struct full_message *)(self->data))->message.close_done.status_code); |
| 2573 |
} |
| 2574 |
|
| 2575 |
gboolean |
| 2576 |
mbim_message_close_done_get_result (const MbimMessage *self, |
| 2577 |
GError **error) |
| 2578 |
{ |
| 2579 |
MbimStatusError status; |
| 2580 |
|
| 2581 |
g_return_val_if_fail (self != NULL, FALSE); |
| 2582 |
g_return_val_if_fail (_mbim_message_validate_type_header (self, NULL), FALSE); |
| 2583 |
g_return_val_if_fail (MBIM_MESSAGE_GET_MESSAGE_TYPE (self) == MBIM_MESSAGE_TYPE_CLOSE_DONE, FALSE); |
| 2584 |
|
| 2585 |
status = (MbimStatusError) GUINT32_FROM_LE (((struct full_message *)(self->data))->message.close_done.status_code); |
| 2586 |
if (status == MBIM_STATUS_ERROR_NONE) |
| 2587 |
return TRUE; |
| 2588 |
|
| 2589 |
set_error_from_status (error, status); |
| 2590 |
return FALSE; |
| 2591 |
} |
| 2592 |
|
| 2593 |
/*****************************************************************************/ |
| 2594 |
/* 'Error' message interface */ |
| 2595 |
|
| 2596 |
MbimMessage * |
| 2597 |
mbim_message_error_new (guint32 transaction_id, |
| 2598 |
MbimProtocolError error_status_code) |
| 2599 |
{ |
| 2600 |
GByteArray *self; |
| 2601 |
|
| 2602 |
self = _mbim_message_allocate (MBIM_MESSAGE_TYPE_HOST_ERROR, |
| 2603 |
transaction_id, |
| 2604 |
sizeof (struct error_message)); |
| 2605 |
|
| 2606 |
/* Open header */ |
| 2607 |
((struct full_message *)(self->data))->message.error.error_status_code = GUINT32_TO_LE (error_status_code); |
| 2608 |
|
| 2609 |
return (MbimMessage *)self; |
| 2610 |
} |
| 2611 |
|
| 2612 |
MbimMessage * |
| 2613 |
mbim_message_function_error_new (guint32 transaction_id, |
| 2614 |
MbimProtocolError error_status_code) |
| 2615 |
{ |
| 2616 |
GByteArray *self; |
| 2617 |
|
| 2618 |
self = _mbim_message_allocate (MBIM_MESSAGE_TYPE_FUNCTION_ERROR, |
| 2619 |
transaction_id, |
| 2620 |
sizeof (struct error_message)); |
| 2621 |
|
| 2622 |
/* Open header */ |
| 2623 |
((struct full_message *)(self->data))->message.error.error_status_code = GUINT32_TO_LE (error_status_code); |
| 2624 |
|
| 2625 |
return (MbimMessage *)self; |
| 2626 |
} |
| 2627 |
|
| 2628 |
MbimProtocolError |
| 2629 |
mbim_message_error_get_error_status_code (const MbimMessage *self) |
| 2630 |
{ |
| 2631 |
g_return_val_if_fail (self != NULL, MBIM_PROTOCOL_ERROR_INVALID); |
| 2632 |
g_return_val_if_fail (_mbim_message_validate_type_header (self, NULL), MBIM_PROTOCOL_ERROR_INVALID); |
| 2633 |
g_return_val_if_fail ((MBIM_MESSAGE_GET_MESSAGE_TYPE (self) == MBIM_MESSAGE_TYPE_HOST_ERROR || |
| 2634 |
MBIM_MESSAGE_GET_MESSAGE_TYPE (self) == MBIM_MESSAGE_TYPE_FUNCTION_ERROR), |
| 2635 |
MBIM_PROTOCOL_ERROR_INVALID); |
| 2636 |
|
| 2637 |
return (MbimProtocolError) GUINT32_FROM_LE (((struct full_message *)(self->data))->message.error.error_status_code); |
| 2638 |
} |
| 2639 |
|
| 2640 |
GError * |
| 2641 |
mbim_message_error_get_error (const MbimMessage *self) |
| 2642 |
{ |
| 2643 |
MbimProtocolError error_status_code; |
| 2644 |
|
| 2645 |
g_return_val_if_fail (self != NULL, NULL); |
| 2646 |
g_return_val_if_fail (_mbim_message_validate_type_header (self, NULL), NULL); |
| 2647 |
g_return_val_if_fail ((MBIM_MESSAGE_GET_MESSAGE_TYPE (self) == MBIM_MESSAGE_TYPE_HOST_ERROR || |
| 2648 |
MBIM_MESSAGE_GET_MESSAGE_TYPE (self) == MBIM_MESSAGE_TYPE_FUNCTION_ERROR), |
| 2649 |
NULL); |
| 2650 |
|
| 2651 |
error_status_code = (MbimProtocolError) GUINT32_FROM_LE (((struct full_message *)(self->data))->message.error.error_status_code); |
| 2652 |
|
| 2653 |
return g_error_new (MBIM_PROTOCOL_ERROR, |
| 2654 |
error_status_code, |
| 2655 |
"MBIM protocol error: %s", |
| 2656 |
mbim_protocol_error_get_string (error_status_code)); |
| 2657 |
} |
| 2658 |
|
| 2659 |
/*****************************************************************************/ |
| 2660 |
/* 'Command' message interface */ |
| 2661 |
|
| 2662 |
MbimMessage * |
| 2663 |
mbim_message_command_new (guint32 transaction_id, |
| 2664 |
MbimService service, |
| 2665 |
guint32 cid, |
| 2666 |
MbimMessageCommandType command_type) |
| 2667 |
{ |
| 2668 |
GByteArray *self; |
| 2669 |
const MbimUuid *service_id; |
| 2670 |
|
| 2671 |
/* Known service required */ |
| 2672 |
service_id = mbim_uuid_from_service (service); |
| 2673 |
g_return_val_if_fail (service_id != NULL, NULL); |
| 2674 |
|
| 2675 |
self = _mbim_message_allocate (MBIM_MESSAGE_TYPE_COMMAND, |
| 2676 |
transaction_id, |
| 2677 |
sizeof (struct command_message)); |
| 2678 |
|
| 2679 |
/* Fragment header */ |
| 2680 |
((struct full_message *)(self->data))->message.command.fragment_header.total = GUINT32_TO_LE (1); |
| 2681 |
((struct full_message *)(self->data))->message.command.fragment_header.current = 0; |
| 2682 |
|
| 2683 |
/* Command header */ |
| 2684 |
memcpy (((struct full_message *)(self->data))->message.command.service_id, service_id, sizeof (*service_id)); |
| 2685 |
((struct full_message *)(self->data))->message.command.command_id = GUINT32_TO_LE (cid); |
| 2686 |
((struct full_message *)(self->data))->message.command.command_type = GUINT32_TO_LE (command_type); |
| 2687 |
((struct full_message *)(self->data))->message.command.buffer_length = 0; |
| 2688 |
|
| 2689 |
return (MbimMessage *)self; |
| 2690 |
} |
| 2691 |
|
| 2692 |
void |
| 2693 |
mbim_message_command_append (MbimMessage *self, |
| 2694 |
const guint8 *buffer, |
| 2695 |
guint32 buffer_size) |
| 2696 |
{ |
| 2697 |
g_byte_array_append ((GByteArray *)self, buffer, buffer_size); |
| 2698 |
|
| 2699 |
/* Update message and buffer length */ |
| 2700 |
((struct header *)(self->data))->length = |
| 2701 |
GUINT32_TO_LE (MBIM_MESSAGE_GET_MESSAGE_LENGTH (self) + buffer_size); |
| 2702 |
((struct full_message *)(self->data))->message.command.buffer_length = |
| 2703 |
GUINT32_TO_LE (GUINT32_FROM_LE (((struct full_message *)(self->data))->message.command.buffer_length) + buffer_size); |
| 2704 |
} |
| 2705 |
|
| 2706 |
MbimService |
| 2707 |
mbim_message_command_get_service (const MbimMessage *self) |
| 2708 |
{ |
| 2709 |
g_return_val_if_fail (self != NULL, MBIM_SERVICE_INVALID); |
| 2710 |
g_return_val_if_fail (_mbim_message_validate_type_header (self, NULL), MBIM_SERVICE_INVALID); |
| 2711 |
g_return_val_if_fail (MBIM_MESSAGE_GET_MESSAGE_TYPE (self) == MBIM_MESSAGE_TYPE_COMMAND, MBIM_SERVICE_INVALID); |
| 2712 |
g_return_val_if_fail (MBIM_MESSAGE_FRAGMENT_GET_TOTAL (self) == 1, MBIM_SERVICE_INVALID); |
| 2713 |
g_return_val_if_fail (_mbim_message_validate_complete_fragment (self, NULL), MBIM_SERVICE_INVALID); |
| 2714 |
|
| 2715 |
return mbim_uuid_to_service ((const MbimUuid *)&(((struct full_message *)(self->data))->message.command.service_id)); |
| 2716 |
} |
| 2717 |
|
| 2718 |
const MbimUuid * |
| 2719 |
mbim_message_command_get_service_id (const MbimMessage *self) |
| 2720 |
{ |
| 2721 |
g_return_val_if_fail (self != NULL, MBIM_UUID_INVALID); |
| 2722 |
g_return_val_if_fail (_mbim_message_validate_type_header (self, NULL), MBIM_UUID_INVALID); |
| 2723 |
g_return_val_if_fail (MBIM_MESSAGE_GET_MESSAGE_TYPE (self) == MBIM_MESSAGE_TYPE_COMMAND, MBIM_UUID_INVALID); |
| 2724 |
g_return_val_if_fail (MBIM_MESSAGE_FRAGMENT_GET_TOTAL (self) == 1, MBIM_UUID_INVALID); |
| 2725 |
g_return_val_if_fail (_mbim_message_validate_complete_fragment (self, NULL), MBIM_UUID_INVALID); |
| 2726 |
|
| 2727 |
return (const MbimUuid *)&(((struct full_message *)(self->data))->message.command.service_id); |
| 2728 |
} |
| 2729 |
|
| 2730 |
guint32 |
| 2731 |
mbim_message_command_get_cid (const MbimMessage *self) |
| 2732 |
{ |
| 2733 |
g_return_val_if_fail (self != NULL, 0); |
| 2734 |
g_return_val_if_fail (_mbim_message_validate_type_header (self, NULL), 0); |
| 2735 |
g_return_val_if_fail (MBIM_MESSAGE_GET_MESSAGE_TYPE (self) == MBIM_MESSAGE_TYPE_COMMAND, 0); |
| 2736 |
g_return_val_if_fail (MBIM_MESSAGE_FRAGMENT_GET_TOTAL (self) == 1, 0); |
| 2737 |
g_return_val_if_fail (_mbim_message_validate_complete_fragment (self, NULL), 0); |
| 2738 |
|
| 2739 |
return GUINT32_FROM_LE (((struct full_message *)(self->data))->message.command.command_id); |
| 2740 |
} |
| 2741 |
|
| 2742 |
MbimMessageCommandType |
| 2743 |
mbim_message_command_get_command_type (const MbimMessage *self) |
| 2744 |
{ |
| 2745 |
g_return_val_if_fail (self != NULL, MBIM_MESSAGE_COMMAND_TYPE_UNKNOWN); |
| 2746 |
g_return_val_if_fail (_mbim_message_validate_type_header (self, NULL), MBIM_MESSAGE_COMMAND_TYPE_UNKNOWN); |
| 2747 |
g_return_val_if_fail (MBIM_MESSAGE_GET_MESSAGE_TYPE (self) == MBIM_MESSAGE_TYPE_COMMAND, MBIM_MESSAGE_COMMAND_TYPE_UNKNOWN); |
| 2748 |
g_return_val_if_fail (MBIM_MESSAGE_FRAGMENT_GET_TOTAL (self) == 1, MBIM_MESSAGE_COMMAND_TYPE_UNKNOWN); |
| 2749 |
g_return_val_if_fail (_mbim_message_validate_complete_fragment (self, NULL), MBIM_MESSAGE_COMMAND_TYPE_UNKNOWN); |
| 2750 |
|
| 2751 |
return (MbimMessageCommandType) GUINT32_FROM_LE (((struct full_message *)(self->data))->message.command.command_type); |
| 2752 |
} |
| 2753 |
|
| 2754 |
const guint8 * |
| 2755 |
mbim_message_command_get_raw_information_buffer (const MbimMessage *self, |
| 2756 |
guint32 *out_length) |
| 2757 |
{ |
| 2758 |
guint32 length; |
| 2759 |
|
| 2760 |
g_return_val_if_fail (self != NULL, NULL); |
| 2761 |
g_return_val_if_fail (_mbim_message_validate_type_header (self, NULL), NULL); |
| 2762 |
g_return_val_if_fail (MBIM_MESSAGE_GET_MESSAGE_TYPE (self) == MBIM_MESSAGE_TYPE_COMMAND, NULL); |
| 2763 |
g_return_val_if_fail (MBIM_MESSAGE_FRAGMENT_GET_TOTAL (self) == 1, NULL); |
| 2764 |
g_return_val_if_fail (_mbim_message_validate_complete_fragment (self, NULL), NULL); |
| 2765 |
|
| 2766 |
length = GUINT32_FROM_LE (((struct full_message *)(self->data))->message.command.buffer_length); |
| 2767 |
if (out_length) |
| 2768 |
*out_length = length; |
| 2769 |
|
| 2770 |
return (length > 0 ? |
| 2771 |
((struct full_message *)(self->data))->message.command.buffer : |
| 2772 |
NULL); |
| 2773 |
} |
| 2774 |
|
| 2775 |
/*****************************************************************************/ |
| 2776 |
/* 'Command Done' message interface */ |
| 2777 |
|
| 2778 |
MbimService |
| 2779 |
mbim_message_command_done_get_service (const MbimMessage *self) |
| 2780 |
{ |
| 2781 |
g_return_val_if_fail (self != NULL, MBIM_SERVICE_INVALID); |
| 2782 |
g_return_val_if_fail (_mbim_message_validate_type_header (self, NULL), MBIM_SERVICE_INVALID); |
| 2783 |
g_return_val_if_fail (MBIM_MESSAGE_GET_MESSAGE_TYPE (self) == MBIM_MESSAGE_TYPE_COMMAND_DONE, MBIM_SERVICE_INVALID); |
| 2784 |
g_return_val_if_fail (MBIM_MESSAGE_FRAGMENT_GET_TOTAL (self) == 1, MBIM_SERVICE_INVALID); |
| 2785 |
g_return_val_if_fail (_mbim_message_validate_complete_fragment (self, NULL), MBIM_SERVICE_INVALID); |
| 2786 |
|
| 2787 |
return mbim_uuid_to_service ((const MbimUuid *)&(((struct full_message *)(self->data))->message.command_done.service_id)); |
| 2788 |
} |
| 2789 |
|
| 2790 |
const MbimUuid * |
| 2791 |
mbim_message_command_done_get_service_id (const MbimMessage *self) |
| 2792 |
{ |
| 2793 |
g_return_val_if_fail (self != NULL, MBIM_UUID_INVALID); |
| 2794 |
g_return_val_if_fail (_mbim_message_validate_type_header (self, NULL), MBIM_UUID_INVALID); |
| 2795 |
g_return_val_if_fail (MBIM_MESSAGE_GET_MESSAGE_TYPE (self) == MBIM_MESSAGE_TYPE_COMMAND_DONE, MBIM_UUID_INVALID); |
| 2796 |
g_return_val_if_fail (MBIM_MESSAGE_FRAGMENT_GET_TOTAL (self) == 1, MBIM_UUID_INVALID); |
| 2797 |
g_return_val_if_fail (_mbim_message_validate_complete_fragment (self, NULL), MBIM_UUID_INVALID); |
| 2798 |
|
| 2799 |
return (const MbimUuid *)&(((struct full_message *)(self->data))->message.command_done.service_id); |
| 2800 |
} |
| 2801 |
|
| 2802 |
guint32 |
| 2803 |
mbim_message_command_done_get_cid (const MbimMessage *self) |
| 2804 |
{ |
| 2805 |
g_return_val_if_fail (self != NULL, 0); |
| 2806 |
g_return_val_if_fail (_mbim_message_validate_type_header (self, NULL), 0); |
| 2807 |
g_return_val_if_fail (MBIM_MESSAGE_GET_MESSAGE_TYPE (self) == MBIM_MESSAGE_TYPE_COMMAND_DONE, 0); |
| 2808 |
g_return_val_if_fail (MBIM_MESSAGE_FRAGMENT_GET_TOTAL (self) == 1, 0); |
| 2809 |
g_return_val_if_fail (_mbim_message_validate_complete_fragment (self, NULL), 0); |
| 2810 |
|
| 2811 |
return GUINT32_FROM_LE (((struct full_message *)(self->data))->message.command_done.command_id); |
| 2812 |
} |
| 2813 |
|
| 2814 |
MbimStatusError |
| 2815 |
mbim_message_command_done_get_status_code (const MbimMessage *self) |
| 2816 |
{ |
| 2817 |
g_return_val_if_fail (self != NULL, MBIM_STATUS_ERROR_FAILURE); |
| 2818 |
g_return_val_if_fail (_mbim_message_validate_type_header (self, NULL), MBIM_STATUS_ERROR_FAILURE); |
| 2819 |
g_return_val_if_fail (MBIM_MESSAGE_GET_MESSAGE_TYPE (self) == MBIM_MESSAGE_TYPE_COMMAND_DONE, MBIM_STATUS_ERROR_FAILURE); |
| 2820 |
g_return_val_if_fail (MBIM_MESSAGE_FRAGMENT_GET_TOTAL (self) == 1, MBIM_STATUS_ERROR_FAILURE); |
| 2821 |
g_return_val_if_fail (_mbim_message_validate_complete_fragment (self, NULL), MBIM_STATUS_ERROR_FAILURE); |
| 2822 |
|
| 2823 |
return (MbimStatusError) GUINT32_FROM_LE (((struct full_message *)(self->data))->message.command_done.status_code); |
| 2824 |
} |
| 2825 |
|
| 2826 |
gboolean |
| 2827 |
mbim_message_command_done_get_result (const MbimMessage *self, |
| 2828 |
GError **error) |
| 2829 |
{ |
| 2830 |
MbimStatusError status; |
| 2831 |
|
| 2832 |
g_return_val_if_fail (self != NULL, FALSE); |
| 2833 |
g_return_val_if_fail (_mbim_message_validate_type_header (self, NULL), FALSE); |
| 2834 |
g_return_val_if_fail (MBIM_MESSAGE_GET_MESSAGE_TYPE (self) == MBIM_MESSAGE_TYPE_COMMAND_DONE, FALSE); |
| 2835 |
g_return_val_if_fail (MBIM_MESSAGE_FRAGMENT_GET_TOTAL (self) == 1, FALSE); |
| 2836 |
g_return_val_if_fail (_mbim_message_validate_complete_fragment (self, NULL), FALSE); |
| 2837 |
|
| 2838 |
status = (MbimStatusError) GUINT32_FROM_LE (((struct full_message *)(self->data))->message.command_done.status_code); |
| 2839 |
if (status == MBIM_STATUS_ERROR_NONE) |
| 2840 |
return TRUE; |
| 2841 |
|
| 2842 |
set_error_from_status (error, status); |
| 2843 |
return FALSE; |
| 2844 |
} |
| 2845 |
|
| 2846 |
const guint8 * |
| 2847 |
mbim_message_command_done_get_raw_information_buffer (const MbimMessage *self, |
| 2848 |
guint32 *out_length) |
| 2849 |
{ |
| 2850 |
guint32 length; |
| 2851 |
|
| 2852 |
g_return_val_if_fail (self != NULL, NULL); |
| 2853 |
g_return_val_if_fail (_mbim_message_validate_type_header (self, NULL), NULL); |
| 2854 |
g_return_val_if_fail (MBIM_MESSAGE_GET_MESSAGE_TYPE (self) == MBIM_MESSAGE_TYPE_COMMAND_DONE, NULL); |
| 2855 |
g_return_val_if_fail (MBIM_MESSAGE_FRAGMENT_GET_TOTAL (self) == 1, NULL); |
| 2856 |
g_return_val_if_fail (_mbim_message_validate_complete_fragment (self, NULL), NULL); |
| 2857 |
|
| 2858 |
length = GUINT32_FROM_LE (((struct full_message *)(self->data))->message.command_done.buffer_length); |
| 2859 |
if (out_length) |
| 2860 |
*out_length = length; |
| 2861 |
|
| 2862 |
return (length > 0 ? |
| 2863 |
((struct full_message *)(self->data))->message.command_done.buffer : |
| 2864 |
NULL); |
| 2865 |
} |
| 2866 |
|
| 2867 |
/*****************************************************************************/ |
| 2868 |
/* 'Indicate Status' message interface */ |
| 2869 |
|
| 2870 |
MbimService |
| 2871 |
mbim_message_indicate_status_get_service (const MbimMessage *self) |
| 2872 |
{ |
| 2873 |
g_return_val_if_fail (self != NULL, MBIM_SERVICE_INVALID); |
| 2874 |
g_return_val_if_fail (_mbim_message_validate_type_header (self, NULL), MBIM_SERVICE_INVALID); |
| 2875 |
g_return_val_if_fail (MBIM_MESSAGE_GET_MESSAGE_TYPE (self) == MBIM_MESSAGE_TYPE_INDICATE_STATUS, MBIM_SERVICE_INVALID); |
| 2876 |
g_return_val_if_fail (MBIM_MESSAGE_FRAGMENT_GET_TOTAL (self) == 1, MBIM_SERVICE_INVALID); |
| 2877 |
g_return_val_if_fail (_mbim_message_validate_complete_fragment (self, NULL), MBIM_SERVICE_INVALID); |
| 2878 |
|
| 2879 |
return mbim_uuid_to_service ((const MbimUuid *)&(((struct full_message *)(self->data))->message.indicate_status.service_id)); |
| 2880 |
} |
| 2881 |
|
| 2882 |
const MbimUuid * |
| 2883 |
mbim_message_indicate_status_get_service_id (const MbimMessage *self) |
| 2884 |
{ |
| 2885 |
g_return_val_if_fail (self != NULL, MBIM_UUID_INVALID); |
| 2886 |
g_return_val_if_fail (_mbim_message_validate_type_header (self, NULL), MBIM_UUID_INVALID); |
| 2887 |
g_return_val_if_fail (MBIM_MESSAGE_GET_MESSAGE_TYPE (self) == MBIM_MESSAGE_TYPE_INDICATE_STATUS, MBIM_UUID_INVALID); |
| 2888 |
g_return_val_if_fail (MBIM_MESSAGE_FRAGMENT_GET_TOTAL (self) == 1, MBIM_UUID_INVALID); |
| 2889 |
g_return_val_if_fail (_mbim_message_validate_complete_fragment (self, NULL), MBIM_UUID_INVALID); |
| 2890 |
|
| 2891 |
return (const MbimUuid *)&(((struct full_message *)(self->data))->message.indicate_status.service_id); |
| 2892 |
} |
| 2893 |
|
| 2894 |
guint32 |
| 2895 |
mbim_message_indicate_status_get_cid (const MbimMessage *self) |
| 2896 |
{ |
| 2897 |
g_return_val_if_fail (self != NULL, 0); |
| 2898 |
g_return_val_if_fail (_mbim_message_validate_type_header (self, NULL), 0); |
| 2899 |
g_return_val_if_fail (MBIM_MESSAGE_GET_MESSAGE_TYPE (self) == MBIM_MESSAGE_TYPE_INDICATE_STATUS, 0); |
| 2900 |
g_return_val_if_fail (MBIM_MESSAGE_FRAGMENT_GET_TOTAL (self) == 1, 0); |
| 2901 |
g_return_val_if_fail (_mbim_message_validate_complete_fragment (self, NULL), 0); |
| 2902 |
|
| 2903 |
return GUINT32_FROM_LE (((struct full_message *)(self->data))->message.indicate_status.command_id); |
| 2904 |
} |
| 2905 |
|
| 2906 |
const guint8 * |
| 2907 |
mbim_message_indicate_status_get_raw_information_buffer (const MbimMessage *self, |
| 2908 |
guint32 *out_length) |
| 2909 |
{ |
| 2910 |
guint32 length; |
| 2911 |
|
| 2912 |
g_return_val_if_fail (self != NULL, NULL); |
| 2913 |
g_return_val_if_fail (_mbim_message_validate_type_header (self, NULL), NULL); |
| 2914 |
g_return_val_if_fail (MBIM_MESSAGE_GET_MESSAGE_TYPE (self) == MBIM_MESSAGE_TYPE_INDICATE_STATUS, NULL); |
| 2915 |
g_return_val_if_fail (MBIM_MESSAGE_FRAGMENT_GET_TOTAL (self) == 1, NULL); |
| 2916 |
g_return_val_if_fail (_mbim_message_validate_complete_fragment (self, NULL), NULL); |
| 2917 |
|
| 2918 |
length = GUINT32_FROM_LE (((struct full_message *)(self->data))->message.indicate_status.buffer_length); |
| 2919 |
if (out_length) |
| 2920 |
*out_length = length; |
| 2921 |
|
| 2922 |
return (length > 0 ? |
| 2923 |
((struct full_message *)(self->data))->message.indicate_status.buffer : |
| 2924 |
NULL); |
| 2925 |
} |
| 2926 |
|
| 2927 |
/*****************************************************************************/ |
| 2928 |
/* Other helpers */ |
| 2929 |
|
| 2930 |
gboolean |
| 2931 |
mbim_message_response_get_result (const MbimMessage *self, |
| 2932 |
MbimMessageType expected, |
| 2933 |
GError **error) |
| 2934 |
{ |
| 2935 |
MbimStatusError status = MBIM_STATUS_ERROR_NONE; |
| 2936 |
MbimMessageType type; |
| 2937 |
|
| 2938 |
g_return_val_if_fail (self != NULL, FALSE); |
| 2939 |
g_return_val_if_fail (expected == MBIM_MESSAGE_TYPE_OPEN_DONE || |
| 2940 |
expected == MBIM_MESSAGE_TYPE_CLOSE_DONE || |
| 2941 |
expected == MBIM_MESSAGE_TYPE_COMMAND_DONE, FALSE); |
| 2942 |
g_return_val_if_fail (_mbim_message_validate_type_header (self, NULL), FALSE); |
| 2943 |
|
| 2944 |
type = MBIM_MESSAGE_GET_MESSAGE_TYPE (self); |
| 2945 |
if (type != MBIM_MESSAGE_TYPE_FUNCTION_ERROR && type != expected) { |
| 2946 |
g_set_error (error, |
| 2947 |
MBIM_CORE_ERROR, |
| 2948 |
MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 2949 |
"Unexpected response message type: 0x%04X", (guint32) type); |
| 2950 |
return FALSE; |
| 2951 |
} |
| 2952 |
|
| 2953 |
switch (type) { |
| 2954 |
case MBIM_MESSAGE_TYPE_OPEN_DONE: |
| 2955 |
status = (MbimStatusError) GUINT32_FROM_LE (((struct full_message *)(self->data))->message.open_done.status_code); |
| 2956 |
break; |
| 2957 |
|
| 2958 |
case MBIM_MESSAGE_TYPE_CLOSE_DONE: |
| 2959 |
status = (MbimStatusError) GUINT32_FROM_LE (((struct full_message *)(self->data))->message.close_done.status_code); |
| 2960 |
break; |
| 2961 |
|
| 2962 |
case MBIM_MESSAGE_TYPE_COMMAND_DONE: |
| 2963 |
g_return_val_if_fail (MBIM_MESSAGE_FRAGMENT_GET_TOTAL (self) == 1, FALSE); |
| 2964 |
g_return_val_if_fail (_mbim_message_validate_complete_fragment (self, NULL), FALSE); |
| 2965 |
status = (MbimStatusError) GUINT32_FROM_LE (((struct full_message *)(self->data))->message.command_done.status_code); |
| 2966 |
break; |
| 2967 |
|
| 2968 |
case MBIM_MESSAGE_TYPE_FUNCTION_ERROR: |
| 2969 |
if (error) |
| 2970 |
*error = mbim_message_error_get_error (self); |
| 2971 |
return FALSE; |
| 2972 |
|
| 2973 |
case MBIM_MESSAGE_TYPE_INVALID: |
| 2974 |
case MBIM_MESSAGE_TYPE_OPEN: |
| 2975 |
case MBIM_MESSAGE_TYPE_CLOSE: |
| 2976 |
case MBIM_MESSAGE_TYPE_COMMAND: |
| 2977 |
case MBIM_MESSAGE_TYPE_HOST_ERROR: |
| 2978 |
case MBIM_MESSAGE_TYPE_INDICATE_STATUS: |
| 2979 |
default: |
| 2980 |
g_assert_not_reached (); |
| 2981 |
} |
| 2982 |
|
| 2983 |
if (status == MBIM_STATUS_ERROR_NONE) |
| 2984 |
return TRUE; |
| 2985 |
|
| 2986 |
/* Build error */ |
| 2987 |
set_error_from_status (error, status); |
| 2988 |
return FALSE; |
| 2989 |
} |