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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
* Copyright (C) 2011 - 2012 Red Hat, Inc.
* Copyright (C) 2010 - 2012 Google, Inc.
*/
#include <glib.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
typedef u_int16_t u16;
typedef u_int8_t u8;
typedef u_int32_t u32;
typedef u_int64_t u64;
typedef unsigned int qbool;
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
/***********************************************************/
static void
print_buf (const char *detail, const char *buf, size_t len)
{
int i = 0, z;
qbool newline = FALSE, indent = FALSE;
char *f;
guint flen;
f = g_strdup_printf ("%s (%zu) ", detail, len);
flen = strlen (f);
g_print ("%s", f);
for (i = 0; i < len; i++) {
if (indent) {
z = flen;
while (z--)
g_print (" ");
indent = FALSE;
}
g_print ("%02x ", buf[i] & 0xFF);
if (((i + 1) % 16) == 0) {
g_print ("\n");
newline = TRUE;
indent = TRUE;
} else
newline = FALSE;
}
if (!newline)
g_print ("\n");
}
/**************************************************************/
typedef enum {
QMI_SVC_CTL = 0x00,
QMI_SVC_WDS = 0x01,
QMI_SVC_DMS = 0x02,
QMI_SVC_NAS = 0x03,
QMI_SVC_QOS = 0x04,
QMI_SVC_WMS = 0x05,
QMI_SVC_PDS = 0x06,
QMI_SVC_AUTH = 0x07,
QMI_SVC_AT = 0x08,
QMI_SVC_VOICE = 0x09,
QMI_SVC_CAT2 = 0x0A,
QMI_SVC_UIM = 0x0B,
QMI_SVC_PBM = 0x0C,
QMI_SVC_LOC = 0x10,
QMI_SVC_SAR = 0x11,
QMI_SVC_RMTFS = 0x14,
QMI_SVC_CAT = 0xE0,
QMI_SVC_RMS = 0xE1,
QMI_SVC_OMA = 0xE2
} qmi_service_type;
struct qmux {
u8 tf; /* always 1 */
u16 len;
u8 ctrl;
u8 service;
u8 qmicid;
} __attribute__((__packed__));
struct qmi_tlv_status {
u16 status;
u16 error;
} __attribute__((__packed__));
struct getcid_req {
struct qmux header;
u8 req;
u8 tid;
u16 msgid;
u16 tlvsize;
u8 service;
u16 size;
u8 qmisvc;
} __attribute__((__packed__));
struct releasecid_req {
struct qmux header;
u8 req;
u8 tid;
u16 msgid;
u16 tlvsize;
u8 rlscid;
u16 size;
u16 cid;
} __attribute__((__packed__));
struct version_info_req {
struct qmux header;
u8 req;
u8 tid;
u16 msgid;
u16 tlvsize;
} __attribute__((__packed__));
struct qmi_ctl_version_info_list_service {
u8 service_type; /* QMI_SVC_xxx */
u16 major_version;
u16 minor_version;
} __attribute__((__packed__));
struct qmi_tlv_ctl_version_info_list {
u8 count;
struct qmi_ctl_version_info_list_service services[0];
}__attribute__((__packed__));
struct seteventreport_req {
struct qmux header;
u8 req;
u16 tid;
u16 msgid;
u16 tlvsize;
u8 reportchanrate;
u16 size;
u8 period;
u32 mask;
} __attribute__((__packed__));
struct getpkgsrvcstatus_req {
struct qmux header;
u8 req;
u16 tid;
u16 msgid;
u16 tlvsize;
} __attribute__((__packed__));
struct getmeid_req {
struct qmux header;
u8 req;
u16 tid;
u16 msgid;
u16 tlvsize;
} __attribute__((__packed__));
struct qmiwds_stats {
u32 txok;
u32 rxok;
u32 txerr;
u32 rxerr;
u32 txofl;
u32 rxofl;
u64 txbytesok;
u64 rxbytesok;
qbool linkstate;
qbool reconfigure;
};
struct nas_signal_req {
struct qmux header;
u8 req;
u16 tid;
u16 msgid;
u16 tlvsize;
} __attribute__((__packed__));
const size_t qmux_size = sizeof(struct qmux);
static void
qmux_fill(struct qmux *qmux, u8 service, u16 cid, u16 size)
{
qmux->tf = 1;
qmux->len = size - 1;
qmux->ctrl = 0;
qmux->service = cid & 0xff;
qmux->qmicid = cid >> 8;
}
static void *
qmictl_new_getcid(u8 tid, u8 svctype, size_t *size)
{
struct getcid_req *req;
req = g_malloc0 (sizeof (*req));
req->req = 0x00;
req->tid = tid;
req->msgid = 0x0022;
req->tlvsize = 0x0004;
req->service = 0x01;
req->size = 0x0001;
req->qmisvc = svctype;
*size = sizeof(*req);
qmux_fill (&req->header, QMI_SVC_CTL, 0, *size);
return req;
}
static void *
qmictl_new_releasecid(u8 tid, u16 cid, size_t *size)
{
struct releasecid_req *req;
req = g_malloc0 (sizeof (*req));
req->req = 0x00;
req->tid = tid;
req->msgid = 0x0023;
req->tlvsize = 0x05;
req->rlscid = 0x01;
req->size = 0x0002;
req->cid = cid;
*size = sizeof(*req);
qmux_fill (&req->header, QMI_SVC_CTL, 0, *size);
return req;
}
static void *
qmictl_new_version_info(u8 tid, size_t *size)
{
struct version_info_req *req;
req = g_malloc0 (sizeof (*req));
req->req = 0x00;
req->tid = tid;
req->msgid = 0x21;
req->tlvsize = 0;
*size = sizeof(*req);
qmux_fill (&req->header, QMI_SVC_CTL, 0, *size);
return req;
}
static void *
qmiwds_new_seteventreport(u8 tid, size_t *size)
{
struct seteventreport_req *req;
req = g_malloc0 (sizeof (*req));
req->req = 0x00;
req->tid = tid;
req->msgid = 0x0001;
req->tlvsize = 0x0008;
req->reportchanrate = 0x11;
req->size = 0x0005;
req->period = 0x01;
req->mask = 0x000000ff;
*size = sizeof(*req);
return req;
}
static void *
qmiwds_new_getpkgsrvcstatus(u8 tid, size_t *size)
{
struct getpkgsrvcstatus_req *req;
req = g_malloc0 (sizeof (*req));
req->req = 0x00;
req->tid = tid;
req->msgid = 0x22;
req->tlvsize = 0x0000;
*size = sizeof(*req);
return req;
}
static void *
qmidms_new_getmeid(u16 cid, u8 tid, size_t *size)
{
struct getmeid_req *req;
req = g_malloc0 (sizeof (*req));
req->req = 0x00;
req->tid = tid;
req->msgid = 0x25;
req->tlvsize = 0x0000;
*size = sizeof(*req);
qmux_fill (&req->header, QMI_SVC_WDS, cid, *size);
return req;
}
static int
qmux_parse(u16 *cid, void *buf, size_t size)
{
struct qmux *qmux = buf;
if (!buf || size < 12)
return -ENOMEM;
if (qmux->tf != 1 || qmux->len != size - 1 || qmux->ctrl != 0x80)
return -EINVAL;
*cid = (qmux->qmicid << 8) + qmux->service;
return sizeof(*qmux);
}
static u16
tlv_get(void *msg, u16 msgsize, u8 type, void *buf, u16 bufsize)
{
u16 pos;
u16 msize = 0;
if (!msg || !buf)
return -ENOMEM;
for (pos = 4; pos + 3 < msgsize; pos += msize + 3) {
msize = *(u16 *)(msg + pos + 1);
if (*(u8 *)(msg + pos) == type) {
if (bufsize < msize)
return -ENOMEM;
memcpy(buf, msg + pos + 3, msize);
return msize;
}
}
return -ENOMSG;
}
static int
qmi_msgisvalid(void *msg, u16 size)
{
struct qmi_tlv_status status;
if (tlv_get(msg, size, 2, &status, sizeof (status)) == sizeof (status)) {
if (le16toh (status.status != 0))
return le16toh (status.error);
else
return 0;
}
return -ENOMSG;
}
static int
qmi_msgid(void *msg, u16 size)
{
return size < 2 ? -ENODATA : *(u16 *)msg;
}
static int
qmictl_version_info_resp(void *buf, u16 size)
{
int result, i;
u8 offset = sizeof(struct qmux) + 2;
u8 svcbuf[100];
struct qmi_tlv_ctl_version_info_list *service_list;
struct qmi_ctl_version_info_list_service *svc;
if (!buf || size < offset)
return -ENOMEM;
buf = buf + offset;
size -= offset;
result = qmi_msgid(buf, size);
if (result != 0x21)
return -EFAULT;
result = qmi_msgisvalid(buf, size);
if (result != 0)
return -EFAULT;
/* Get the services TLV */
result = tlv_get(buf, size, 0x01, svcbuf, sizeof (svcbuf));
if (result < 0)
return -EFAULT;
service_list = (struct qmi_tlv_ctl_version_info_list *) svcbuf;
if (result < (service_list->count * sizeof (struct qmi_ctl_version_info_list_service)))
return -EFAULT;
svc = &(service_list->services[0]);
for (i = 0; i < service_list->count; i++, svc++) {
g_message ("SVC: %d v%d.%d",
svc->service_type,
le16toh (svc->major_version),
le16toh (svc->minor_version));
}
return 0;
}
static int
qmictl_getcid_resp(void *buf, u16 size, u16 *cid)
{
int result;
u8 offset = sizeof(struct qmux) + 2;
if (!buf || size < offset)
return -ENOMEM;
buf = buf + offset;
size -= offset;
result = qmi_msgid(buf, size);
if (result != 0x22)
return -EFAULT;
result = qmi_msgisvalid(buf, size);
if (result != 0)
return -EFAULT;
result = tlv_get(buf, size, 0x01, cid, 2);
if (result != 2)
return -EFAULT;
return 0;
}
static int
qmictl_releasecid_resp(void *buf, u16 size)
{
int result;
u8 offset = sizeof(struct qmux) + 2;
if (!buf || size < offset)
return -ENOMEM;
buf = buf + offset;
size -= offset;
result = qmi_msgid(buf, size);
if (result != 0x23)
return -EFAULT;
result = qmi_msgisvalid(buf, size);
if (result != 0)
return -EFAULT;
return 0;
}
static int
qmiwds_event_resp(void *buf, u16 size, struct qmiwds_stats *stats)
{
int result;
u8 status[2];
u8 offset = sizeof(struct qmux) + 3;
if (!buf || size < offset || !stats)
return -ENOMEM;
buf = buf + offset;
size -= offset;
result = qmi_msgid(buf, size);
if (result == 0x01) {
tlv_get(buf, size, 0x10, &stats->txok, 4);
tlv_get(buf, size, 0x11, &stats->rxok, 4);
tlv_get(buf, size, 0x12, &stats->txerr, 4);
tlv_get(buf, size, 0x13, &stats->rxerr, 4);
tlv_get(buf, size, 0x14, &stats->txofl, 4);
tlv_get(buf, size, 0x15, &stats->rxofl, 4);
tlv_get(buf, size, 0x19, &stats->txbytesok, 8);
tlv_get(buf, size, 0x1A, &stats->rxbytesok, 8);
} else if (result == 0x22) {
result = tlv_get(buf, size, 0x01, &status[0], 2);
if (result >= 1)
stats->linkstate = status[0] == 0x02;
if (result == 2)
stats->reconfigure = status[1] == 0x01;
if (result < 0)
return result;
} else {
return -EFAULT;
}
return 0;
}
static int
qmidms_meid_resp(void *buf, u16 size, char *meid, int meidsize)
{
int result;
u8 offset = sizeof(struct qmux) + 3;
if (!buf || size < offset || meidsize < 14)
return -ENOMEM;
buf = buf + offset;
size -= offset;
result = qmi_msgid(buf, size);
if (result != 0x25)
return -EFAULT;
result = qmi_msgisvalid(buf, size);
if (result)
return -EFAULT;
result = tlv_get(buf, size, 0x12, meid, 14);
if (result != 14)
return -EFAULT;
return 0;
}
static void *
qminas_new_signal(u16 cid, u8 tid, size_t *size)
{
struct nas_signal_req *req;
req = g_malloc0 (sizeof (*req));
req->req = 0x00;
req->tid = tid;
req->msgid = 0x20;
req->tlvsize = 0x0000;
*size = sizeof(*req);
qmux_fill (&req->header, QMI_SVC_NAS, cid, *size);
return req;
}
/* NAS/Get Signal Strength TLV 0x01 */
struct qminas_resp_signalstrength {
u8 dbm;
u8 act;
} __attribute__((__packed__));
static int
qminas_signal_resp(void *buf, u16 size, u8 *dbm, u8 *act)
{
int result;
struct qminas_resp_signalstrength signal;
u8 offset = sizeof(struct qmux) + 3;
if (!buf || size < offset)
return -ENOMEM;
buf = buf + offset;
size -= offset;
result = qmi_msgid(buf, size);
if (result != 0x20)
return -EFAULT;
result = qmi_msgisvalid(buf, size);
if (result)
return -EFAULT;
result = tlv_get(buf, size, 0x01, &signal, sizeof (signal));
if (result != sizeof (signal))
return -EFAULT;
*dbm = signal.dbm;
*act = signal.act;
return 0;
}
/*****************************************************/
static size_t
send_and_wait_reply (int fd, void *b, size_t blen, char *reply, size_t rlen)
{
ssize_t num;
fd_set in;
int result;
struct timeval timeout = { 1, 0 };
print_buf (">>>", b, blen);
num = write (fd, b, blen);
if (num != blen) {
g_warning ("Failed to write: wrote %zd err %d", num, errno);
return 0;
}
FD_ZERO (&in);
FD_SET (fd, &in);
result = select (fd + 1, &in, NULL, NULL, &timeout);
if (result != 1 || !FD_ISSET (fd, &in)) {
g_warning ("No data pending");
return 0;
}
errno = 0;
num = read (fd, reply, rlen);
if (num < 0) {
g_warning ("read error %d", errno);
return 0;
}
print_buf ("<<<", reply, num);
return num;
}
/* CTL service transaction ID */
static u8 ctl_tid = 1;
static int
get_meid (int fd)
{
void *b;
size_t blen;
u8 dms_tid = 1;
u16 dms_cid = 0;
char reply[2048];
size_t rlen;
char meid[16];
int err;
/* Allocate a DMS client ID */
b = qmictl_new_getcid (ctl_tid++, QMI_SVC_DMS, &blen);
g_assert (b);
rlen = send_and_wait_reply (fd, b, blen, reply, sizeof (reply));
g_free (b);
if (rlen <= 0)
return 1;
err = qmictl_getcid_resp (reply, rlen, &dms_cid);
if (err < 0) {
g_warning ("Failed to get DMS client ID: %d", err);
return 1;
}
g_message ("DMS CID %d 0x%X", dms_cid, dms_cid);
/* Get the MEID */
b = qmidms_new_getmeid(dms_cid, dms_tid++, &blen);
g_assert (b);
rlen = send_and_wait_reply (fd, b, blen, reply, sizeof (reply));
g_free (b);
if (rlen <= 0)
return 1;
memset (meid, 0, sizeof (meid));
err = qmidms_meid_resp (reply, rlen, meid, sizeof (meid) - 1);
if (err < 0)
g_warning ("Failed to get MEID: %d", err);
else
g_message ("MEID: %s", meid);
/* Relese the DMS client ID */
b = qmictl_new_releasecid (ctl_tid++, dms_cid, &blen);
g_assert (b);
rlen = send_and_wait_reply (fd, b, blen, reply, sizeof (reply));
g_free (b);
if (rlen <= 0)
return 1;
err = qmictl_releasecid_resp (reply, rlen);
if (err < 0) {
g_warning ("Failed to release DMS client ID: %d", err);
return 1;
}
return 0;
}
static const char *
act_to_string (u8 act)
{
switch (act) {
case 0:
return "no service";
case 1:
return "CDMA2000 1x";
case 2:
return "CDMA2000 HRPD/EVDO";
case 3:
return "AMPS";
case 4:
return "GSM";
case 5:
return "UMTS";
case 8:
return "LTE";
default:
break;
}
return "unknown";
}
static int
get_signal (int fd)
{
void *b;
size_t blen;
u8 nas_tid = 1;
u16 nas_cid = 0;
char reply[2048];
size_t rlen;
int err;
u8 dbm = 0, act = 0;
/* Allocate a NAS client ID */
b = qmictl_new_getcid (ctl_tid++, QMI_SVC_NAS, &blen);
g_assert (b);
rlen = send_and_wait_reply (fd, b, blen, reply, sizeof (reply));
g_free (b);
if (rlen <= 0)
return 1;
err = qmictl_getcid_resp (reply, rlen, &nas_cid);
if (err < 0) {
g_warning ("Failed to get NAS client ID: %d", err);
return 1;
}
g_message ("NAS CID %d 0x%X", nas_cid, nas_cid);
/* Get the signal strength */
b = qminas_new_signal(nas_cid, nas_tid++, &blen);
g_assert (b);
rlen = send_and_wait_reply (fd, b, blen, reply, sizeof (reply));
g_free (b);
if (rlen <= 0)
return 1;
err = qminas_signal_resp (reply, rlen, &dbm, &act);
if (err < 0)
g_warning ("Failed to get signal: %d", err);
else {
g_message ("dBm: -%d", 0x100 - (dbm & 0xFF));
g_message ("AcT: %s", act_to_string (act));
}
/* Relese the NAS client ID */
b = qmictl_new_releasecid (ctl_tid++, nas_cid, &blen);
g_assert (b);
rlen = send_and_wait_reply (fd, b, blen, reply, sizeof (reply));
g_free (b);
if (rlen <= 0)
return 1;
err = qmictl_releasecid_resp (reply, rlen);
if (err < 0) {
g_warning ("Failed to release NAS client ID: %d", err);
return 1;
}
return 0;
}
int main(int argc, char *argv[])
{
int fd;
void *b;
size_t blen;
u8 ctl_tid = 1;
char reply[2048];
size_t rlen;
int err;
if (argc != 2) {
g_warning ("usage: %s <port>", argv[0]);
return 1;
}
errno = 0;
fd = open (argv[1], O_RDWR | O_EXCL | O_NONBLOCK | O_NOCTTY);
if (fd < 0) {
g_warning ("%s: open failed: %d", argv[1], errno);
return 1;
}
/* Send the ready request */
b = qmictl_new_version_info (ctl_tid++, &blen);
g_assert (b);
rlen = send_and_wait_reply (fd, b, blen, reply, sizeof (reply));
g_free (b);
if (rlen <= 0)
goto out;
err = qmictl_version_info_resp (reply, rlen);
if (err < 0) {
g_warning ("Failed to get version info: %d", err);
goto out;
}
get_meid (fd);
get_signal (fd);
out:
close (fd);
return 0;
}
|