1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file of the libqmi library.
*/
/*
* libqmi-glib -- GLib/GIO based library to control QMI devices
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
* Copyright (C) 2012 Aleksander Morgado <aleksander@lanedo.com>
*/
#include <glib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <endian.h>
#include "qmi-message.h"
#include "qmi-utils.h"
#include "qmi-error-types.h"
#define PACKED __attribute__((packed))
struct qmux {
uint16_t length;
uint8_t flags;
uint8_t service;
uint8_t client;
} PACKED;
struct control_header {
uint8_t flags;
uint8_t transaction;
uint16_t message;
uint16_t tlv_length;
} PACKED;
struct service_header {
uint8_t flags;
uint16_t transaction;
uint16_t message;
uint16_t tlv_length;
} PACKED;
struct tlv {
uint8_t type;
uint16_t length;
char value[];
} PACKED;
struct control_message {
struct control_header header;
struct tlv tlv[];
} PACKED;
struct service_message {
struct service_header header;
struct tlv tlv[];
} PACKED;
struct full_message {
uint8_t marker;
struct qmux qmux;
union {
struct control_message control;
struct service_message service;
} qmi;
} PACKED;
struct _QmiMessage {
/* TODO: avoid memory split here */
struct full_message *buf; /* buf allocated using g_malloc, not g_slice_alloc */
gsize len; /* cached size of *buf; not part of message. */
volatile gint ref_count; /* the ref count */
};
static inline uint16_t
qmux_length (QmiMessage *self)
{
return le16toh (self->buf->qmux.length);
}
static inline void
set_qmux_length (QmiMessage *self,
uint16_t length)
{
self->buf->qmux.length = htole16 (length);
}
gboolean
qmi_message_is_control (QmiMessage *self)
{
g_return_val_if_fail (self != NULL, FALSE);
return self->buf->qmux.service == QMI_SERVICE_CTL;
}
guint8
qmi_message_get_qmux_flags (QmiMessage *self)
{
g_return_val_if_fail (self != NULL, 0);
return self->buf->qmux.flags;
}
QmiService
qmi_message_get_service (QmiMessage *self)
{
g_return_val_if_fail (self != NULL, QMI_SERVICE_UNKNOWN);
return (QmiService)self->buf->qmux.service;
}
guint8
qmi_message_get_client_id (QmiMessage *self)
{
g_return_val_if_fail (self != NULL, 0);
return self->buf->qmux.client;
}
guint8
qmi_message_get_qmi_flags (QmiMessage *self)
{
g_return_val_if_fail (self != NULL, 0);
if (qmi_message_is_control (self))
return self->buf->qmi.control.header.flags;
return self->buf->qmi.service.header.flags;
}
guint16
qmi_message_get_transaction_id (QmiMessage *self)
{
g_return_val_if_fail (self != NULL, 0);
if (qmi_message_is_control (self))
return (guint16)self->buf->qmi.control.header.transaction;
return le16toh (self->buf->qmi.service.header.transaction);
}
guint16
qmi_message_get_message_id (QmiMessage *self)
{
g_return_val_if_fail (self != NULL, 0);
if (qmi_message_is_control (self))
return le16toh (self->buf->qmi.control.header.message);
return le16toh (self->buf->qmi.service.header.message);
}
gsize
qmi_message_get_length (QmiMessage *self)
{
g_return_val_if_fail (self != NULL, 0);
return self->len;
}
static uint16_t
qmi_tlv_length (QmiMessage *self)
{
if (qmi_message_is_control (self))
return le16toh (self->buf->qmi.control.header.tlv_length);
return le16toh (self->buf->qmi.service.header.tlv_length);
}
static void
set_qmi_tlv_length (QmiMessage *self,
uint16_t length)
{
if (qmi_message_is_control (self))
self->buf->qmi.control.header.tlv_length = htole16 (length);
else
self->buf->qmi.service.header.tlv_length = htole16 (length);
}
static struct tlv *
qmi_tlv (QmiMessage *self)
{
if (qmi_message_is_control (self))
return self->buf->qmi.control.tlv;
return self->buf->qmi.service.tlv;
}
static char *
qmi_end (QmiMessage *self)
{
return (char *) self->buf + self->len;
}
static struct tlv *
tlv_next (struct tlv *tlv)
{
return (struct tlv *)((char *)tlv + sizeof(struct tlv) + tlv->length);
}
static struct tlv *
qmi_tlv_first (QmiMessage *self)
{
if (qmi_tlv_length (self))
return qmi_tlv (self);
return NULL;
}
static struct tlv *
qmi_tlv_next (QmiMessage *self,
struct tlv *tlv)
{
struct tlv *end;
struct tlv *next;
end = (struct tlv *) qmi_end (self);
next = tlv_next (tlv);
return (next < end ? next : NULL);
}
/**
* Checks the validity of a QMI message.
*
* In particular, checks:
* 1. The message has space for all required headers.
* 2. The length of the buffer, the qmux length field, and the QMI tlv_length
* field are all consistent.
* 3. The TLVs in the message fit exactly in the payload size.
*
* Returns non-zero if the message is valid, zero if invalid.
*/
gboolean
qmi_message_check (QmiMessage *self,
GError **error)
{
size_t header_length;
char *end;
struct tlv *tlv;
g_assert (self != NULL);
g_assert (self->buf != NULL);
if (self->buf->marker != QMI_MESSAGE_QMUX_MARKER) {
g_set_error (error,
QMI_CORE_ERROR,
QMI_CORE_ERROR_INVALID_MESSAGE,
"Marker is incorrect");
return FALSE;
}
if (qmux_length (self) < sizeof (struct qmux)) {
g_set_error (error,
QMI_CORE_ERROR,
QMI_CORE_ERROR_INVALID_MESSAGE,
"QMUX length too short for QMUX header");
return FALSE;
}
/*
* qmux length is one byte shorter than buffer length because qmux
* length does not include the qmux frame marker.
*/
if (qmux_length (self) != self->len - 1) {
g_set_error (error,
QMI_CORE_ERROR,
QMI_CORE_ERROR_INVALID_MESSAGE,
"QMUX length and buffer length don't match");
return FALSE;
}
header_length = sizeof (struct qmux) + (qmi_message_is_control (self) ?
sizeof (struct control_header) :
sizeof (struct service_header));
if (qmux_length (self) < header_length) {
g_set_error (error,
QMI_CORE_ERROR,
QMI_CORE_ERROR_INVALID_MESSAGE,
"QMUX length too short for QMI header");
return FALSE;
}
if (qmux_length (self) - header_length != qmi_tlv_length (self)) {
g_set_error (error,
QMI_CORE_ERROR,
QMI_CORE_ERROR_INVALID_MESSAGE,
"QMUX length and QMI TLV lengths don't match");
return FALSE;
}
end = qmi_end (self);
for (tlv = qmi_tlv (self); tlv < (struct tlv *)end; tlv = tlv_next (tlv)) {
if (tlv->value > end) {
g_set_error (error,
QMI_CORE_ERROR,
QMI_CORE_ERROR_INVALID_MESSAGE,
"TLV header runs over buffer");
return FALSE;
}
if (tlv->value + tlv->length > end) {
g_set_error (error,
QMI_CORE_ERROR,
QMI_CORE_ERROR_INVALID_MESSAGE,
"TLV value runs over buffer");
return FALSE;
}
}
/*
* If this assert triggers, one of the if statements in the loop is wrong.
* (It shouldn't be reached on malformed QMI messages.)
*/
g_assert (tlv == (struct tlv *)end);
return TRUE;
}
QmiMessage *
qmi_message_new (QmiService service,
guint8 client_id,
guint16 transaction_id,
guint16 message_id)
{
QmiMessage *self;
/* Transaction ID in the control service is 8bit only */
g_assert (service != QMI_SERVICE_CTL ||
transaction_id <= G_MAXUINT8);
self = g_slice_new (QmiMessage);
self->ref_count = 1;
self->len = 1 + sizeof (struct qmux) + (service == QMI_SERVICE_CTL ?
sizeof (struct control_header) :
sizeof (struct service_header));
/* TODO: Allocate both the message and the buffer together */
self->buf = g_malloc (self->len);
self->buf->marker = QMI_MESSAGE_QMUX_MARKER;
self->buf->qmux.flags = 0;
self->buf->qmux.service = service;
self->buf->qmux.client = client_id;
set_qmux_length (self, self->len - 1);
if (service == QMI_SERVICE_CTL) {
self->buf->qmi.control.header.flags = 0;
self->buf->qmi.control.header.transaction = (uint8_t)transaction_id;
self->buf->qmi.control.header.message = htole16 (message_id);
} else {
self->buf->qmi.service.header.flags = 0;
self->buf->qmi.service.header.transaction = htole16 (transaction_id);
self->buf->qmi.service.header.message = htole16 (message_id);
}
set_qmi_tlv_length (self, 0);
g_assert (qmi_message_check (self, NULL));
return self;
}
QmiMessage *
qmi_message_ref (QmiMessage *self)
{
g_assert (self != NULL);
g_atomic_int_inc (&self->ref_count);
return self;
}
void
qmi_message_unref (QmiMessage *self)
{
g_assert (self != NULL);
if (g_atomic_int_dec_and_test (&self->ref_count)) {
g_free (self->buf);
g_slice_free (QmiMessage, self);
}
}
gconstpointer
qmi_message_get_raw (QmiMessage *self,
gsize *len,
GError **error)
{
g_assert (self != NULL);
g_assert (len != NULL);
if (!qmi_message_check (self, error))
return NULL;
*len = self->len;
return self->buf;
}
static gboolean
qmimsg_tlv_get_internal (QmiMessage *self,
guint8 type,
guint16 *length,
gpointer value,
gboolean length_exact,
GError **error)
{
struct tlv *tlv;
g_assert (self != NULL);
g_assert (self->buf != NULL);
g_assert (length != NULL);
/* note: we allow querying only for the exact length */
for (tlv = qmi_tlv_first (self); tlv; tlv = qmi_tlv_next (self, tlv)) {
if (tlv->type == type) {
if (length_exact && (tlv->length != *length)) {
g_set_error (error,
QMI_CORE_ERROR,
QMI_CORE_ERROR_TLV_NOT_FOUND,
"TLV found but wrong length (%u != %u)",
tlv->length,
*length);
return FALSE;
} else if (value && tlv->length > *length) {
g_set_error (error,
QMI_CORE_ERROR,
QMI_CORE_ERROR_TLV_TOO_LONG,
"TLV found but too long (%u > %u)",
tlv->length,
*length);
return FALSE;
}
*length = tlv->length;
if (value)
memcpy (value, tlv->value, tlv->length);
return TRUE;
}
}
g_set_error (error,
QMI_CORE_ERROR,
QMI_CORE_ERROR_TLV_TOO_LONG,
"TLV not found");
return FALSE;
}
gboolean
qmi_message_tlv_get (QmiMessage *self,
guint8 type,
guint16 length,
gpointer value,
GError **error)
{
return qmimsg_tlv_get_internal (self, type, &length, value, TRUE, error);
}
gboolean
qmi_message_tlv_get_varlen (QmiMessage *self,
guint8 type,
guint16 *length,
gpointer value,
GError **error)
{
return qmimsg_tlv_get_internal (self, type, length, value, FALSE, error);
}
void
qmi_message_tlv_foreach (QmiMessage *self,
QmiMessageForeachTlvFn callback,
gpointer user_data)
{
struct tlv *tlv;
g_assert (self != NULL);
g_assert (callback != NULL);
for (tlv = qmi_tlv_first (self); tlv; tlv = qmi_tlv_next (self, tlv)) {
callback (tlv->type,
(gsize)(tlv->length),
(gconstpointer)tlv->value,
user_data);
}
}
gboolean
qmi_message_tlv_add (QmiMessage *self,
guint8 type,
gsize length,
gconstpointer value,
GError **error)
{
size_t tlv_len;
struct tlv *tlv;
g_assert (self != NULL);
g_assert ((length == 0) || value != NULL);
/* Make sure nothing's broken to start. */
if (!qmi_message_check (self, error)) {
g_prefix_error (error, "Invalid QMI message detected: ");
return FALSE;
}
/* Find length of new TLV. */
tlv_len = sizeof (struct tlv) + length;
/* Check for overflow of message size. */
if (qmux_length (self) + tlv_len > UINT16_MAX) {
g_set_error (error,
QMI_CORE_ERROR,
QMI_CORE_ERROR_TLV_TOO_LONG,
"TLV to add is too long");
return FALSE;
}
/* Resize buffer. */
self->len += tlv_len;
self->buf = g_realloc (self->buf, self->len);
/* Fill in new TLV. */
tlv = (struct tlv *)(qmi_end (self) - tlv_len);
tlv->type = type;
tlv->length = length;
if (value)
memcpy (tlv->value, value, length);
/* Update length fields. */
set_qmux_length (self, (uint16_t)(qmux_length (self) + tlv_len));
set_qmi_tlv_length (self, (uint16_t)(qmi_tlv_length(self) + tlv_len));
/* Make sure we didn't break anything. */
if (!qmi_message_check (self, error)) {
g_prefix_error (error, "Invalid QMI message built: ");
return FALSE;
}
return TRUE;
}
QmiMessage *
qmi_message_new_from_raw (const guint8 *raw,
gsize raw_len)
{
QmiMessage *self;
gsize message_len;
struct qmux *qmux;
/* If we didn't even read the header, leave */
if (raw_len < (sizeof (struct qmux) + 1))
return NULL;
/* We need to have read the length reported by the header.
* Otherwise, return. */
message_len = le16toh (((struct full_message *)raw)->qmux.length);
if (raw_len < (message_len - 1))
return NULL;
/* Ok, so we should have all the data available already */
self = g_slice_new (QmiMessage);
self->ref_count = 1;
self->len = message_len + 1;
self->buf = g_malloc (self->len);
memcpy (self->buf, raw, self->len);
/* NOTE: we don't check if the message is valid here, let the caller do it */
return self;
}
gchar *
qmi_message_get_printable (QmiMessage *self)
{
GString *printable;
struct tlv *tlv;
if (!qmi_message_check (self, NULL))
return NULL;
printable = g_string_new ("");
g_string_append_printf (printable,
"QMUX:\n"
"\tlength=0x%04x\n"
"\tflags=0x%02x\n"
"\tservice=0x%02x\n"
"\tclient=0x%02x\n",
qmux_length (self),
qmi_message_get_qmux_flags (self),
qmi_message_get_service (self),
qmi_message_get_client_id (self));
g_string_append_printf (printable,
"QMI:\n"
"\tflags=0x%02x\n"
"\ttransaction=0x%04x\n"
"\tmessage=0x%04x\n"
"\ttlv_length=0x%04x\n",
qmi_message_get_qmi_flags (self),
qmi_message_get_transaction_id (self),
qmi_message_get_message_id (self),
qmi_tlv_length (self));
for (tlv = qmi_tlv_first (self); tlv; tlv = qmi_tlv_next (self, tlv)) {
gchar *value_hex;
value_hex = qmi_utils_str_hex (tlv->value, tlv->length, ':');
g_string_append_printf (printable,
"TLV:\n"
"\ttype=0x%02x\n"
"\tlength=0x%04x\n"
"\tvalue=%s\n",
tlv->type,
tlv->length,
value_hex);
g_free (value_hex);
}
return g_string_free (printable, FALSE);
}
/*****************************************************************************/
/* QMI protocol errors handling */
struct qmi_result {
uint16_t status;
uint16_t error;
} PACKED;
/* 0x02 is the TLV for the result of any request.
(grep "Result Code" [Gobi3000API]/Database/QMI/Entity.txt) */
#define QMI_TLV_RESULT_CODE ((uint8_t)0x02)
enum {
QMI_STATUS_SUCCESS = 0x0000,
QMI_STATUS_FAILURE = 0x0001
};
gboolean
qmi_message_get_result (QmiMessage *self,
GError **error)
{
struct qmi_result msg_result;
g_assert (self != NULL);
if (!qmi_message_tlv_get (self,
QMI_TLV_RESULT_CODE,
sizeof (msg_result),
&msg_result,
error)) {
g_prefix_error (error, "Couldn't get result code: ");
return FALSE;
}
switch (msg_result.status) {
case QMI_STATUS_SUCCESS:
/* Operation succeeded */
return TRUE;
case QMI_STATUS_FAILURE:
/* Report a QMI protocol error */
g_set_error (error,
QMI_PROTOCOL_ERROR,
(QmiProtocolError)msg_result.error,
"");
return FALSE;
default:
g_set_error (error,
QMI_CORE_ERROR,
QMI_CORE_ERROR_INVALID_MESSAGE,
"Unexpected result status (%u)", msg_result.status);
return FALSE;
}
}
|