forked from kmilo17pet/quectel-cm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mbim-cm.c
2282 lines (1940 loc) · 84.7 KB
/
mbim-cm.c
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
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
@file mbim-cm.c
@brief MIBIM drivers.
DESCRIPTION
Connectivity Management Tool for USB network adapter of Quectel wireless cellular modules.
INITIALIZATION AND SEQUENCING REQUIREMENTS
None.
---------------------------------------------------------------------------
Copyright (c) 2016 - 2020 Quectel Wireless Solution, Co., Ltd. All Rights Reserved.
Quectel Wireless Solution Proprietary and Confidential.
---------------------------------------------------------------------------
******************************************************************************/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <stddef.h>
#include <pthread.h>
#include <errno.h>
#include <time.h>
#include <signal.h>
#include <getopt.h>
#include <poll.h>
#include <sys/time.h>
#include <endian.h>
#include <time.h>
#include <sys/types.h>
#include <limits.h>
#include <inttypes.h>
#include "QMIThread.h"
#ifndef htole32
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define htole16(x) (uint16_t)(x)
#define le16toh(x) (uint16_t)(x)
#define letoh16(x) (uint16_t)(x)
#define htole32(x) (uint32_t)(x)
#define le32toh(x) (uint32_t)(x)
#define letoh32(x) (uint32_t)(x)
#define htole64(x) (uint64_t)(x)
#define le64toh(x) (uint64_t)(x)
#define letoh64(x) (uint64_t)(x)
#else
static __inline uint16_t __bswap16(uint16_t __x) {
return (__x<<8) | (__x>>8);
}
static __inline uint32_t __bswap32(uint32_t __x) {
return (__x>>24) | (__x>>8&0xff00) | (__x<<8&0xff0000) | (__x<<24);
}
static __inline uint64_t __bswap64(uint64_t __x) {
return (__bswap32(__x)+0ULL<<32) | (__bswap32(__x>>32));
}
#define htole16(x) __bswap16(x)
#define le16toh(x) __bswap16(x)
#define letoh16(x) __bswap16(x)
#define htole32(x) __bswap32(x)
#define le32toh(x) __bswap32(x)
#define letoh32(x) __bswap32(x)
#define htole64(x) __bswap64(x)
#define le64toh(x) __bswap64(x)
#define letoh64(x) __bswap64(x)
#endif
#endif
#define mbim_debug dbg_time
#define UUID_BASIC_CONNECT "a289cc33-bcbb-8b4f-b6b0-133ec2aae6df"
//https://docs.microsoft.com/en-us/windows-hardware/drivers/network/mb-5g-data-class-support
#define UUID_BASIC_CONNECT_EXT "3d01dcc5-fef5-4d05-0d3a-bef7058e9aaf"
#define UUID_SMS "533fbeeb-14fe-4467-9f90-33a223e56c3f"
#define UUID_USSD "e550a0c8-5e82-479e-82f7-10abf4c3351f"
#define UUID_PHONEBOOK "4bf38476-1e6a-41db-b1d8-bed289c25bdb"
#define UUID_STK "d8f20131-fcb5-4e17-8602-d6ed3816164c"
#define UUID_AUTH "1d2b5ff7-0aa1-48b2-aa52-50f15767174e"
#define UUID_DSS "c08a26dd-7718-4382-8482-6e0d583c4d0e"
#define uuid_ext_qmux "d1a30bc2-f97a-6e43-bf65-c7e24fb0f0d3"
#define uuid_mshsd "883b7c26-985f-43fa-9804-27d7fb80959c"
#define uuid_qmbe "2d0c12c9-0e6a-495a-915c-8d174fe5d63c"
#define UUID_MSFWID "e9f7dea2-feaf-4009-93ce-90a3694103b6"
#define uuid_atds "5967bdcc-7fd2-49a2-9f5c-b2e70e527db3"
#define uuid_qdu "6427015f-579d-48f5-8c54-f43ed1e76f83"
#define UUID_MS_UICC_LOW_LEVEL "c2f6588e-f037-4bc9-8665-f4d44bd09367"
#define UUID_MS_SARControl "68223D04-9F6C-4E0F-822D-28441FB72340"
#define UUID_VOICEEXTENSIONS "8d8b9eba-37be-449b-8f1e-61cb034a702e"
#define UUID_LIBMBIM_PROXY "838cf7fb-8d0d-4d7f-871e-d71dbefbb39b"
#define UUID_MBIMContextTypeInternet "7E5E2A7E-4E6F-7272-736B-656E7E5E2A7E"
typedef unsigned char UINT8;
typedef unsigned short UINT16;
typedef unsigned int UINT32;
typedef unsigned long long UINT64;
#define STRINGFY(v) #v
/* The function name will be _ENUM_NAMEStr */
#define enumstrfunc(_ENUM_NAME, _ENUM_MEMS) \
static const char *_ENUM_NAME##Str(int _val) { \
struct { int val;char *name;} _enumstr[] = { _ENUM_MEMS }; \
int idx; for (idx = 0; idx < (int)(sizeof(_enumstr)/sizeof(_enumstr[0])); idx++) { \
if (_val == _enumstr[idx].val) return _enumstr[idx].name;} \
return STRINGFY(_ENUM_NAME##Unknow); \
}
#pragma pack(4)
typedef enum {
MBIM_CID_CMD_TYPE_QUERY = 0,
MBIM_CID_CMD_TYPE_SET = 1,
} MBIM_CID_CMD_TYPE_E;
//Set Query Notification
#define UUID_BASIC_CONNECT_CIDs \
MBIM_ENUM_HELPER(MBIM_CID_DEVICE_CAPS, 1) \
MBIM_ENUM_HELPER(MBIM_CID_SUBSCRIBER_READY_STATUS, 2) \
MBIM_ENUM_HELPER(MBIM_CID_RADIO_STATE, 3) \
MBIM_ENUM_HELPER(MBIM_CID_PIN, 4) \
MBIM_ENUM_HELPER(MBIM_CID_PIN_LIS, 5) \
MBIM_ENUM_HELPER(MBIM_CID_HOME_PROVIDER, 6) \
MBIM_ENUM_HELPER(MBIM_CID_PREFERRED_PROVIDERS, 7) \
MBIM_ENUM_HELPER(MBIM_CID_VISIBLE_PROVIDERS, 8) \
MBIM_ENUM_HELPER(MBIM_CID_REGISTER_STATE, 9) \
MBIM_ENUM_HELPER(MBIM_CID_PACKET_SERVICE, 10) \
MBIM_ENUM_HELPER(MBIM_CID_SIGNAL_STATE, 11) \
MBIM_ENUM_HELPER(MBIM_CID_CONNECT, 12) \
MBIM_ENUM_HELPER(MBIM_CID_PROVISIONED_CONTEXTS, 13) \
MBIM_ENUM_HELPER(MBIM_CID_SERVICE_ACTIVATION, 14) \
MBIM_ENUM_HELPER(MBIM_CID_IP_CONFIGURATION, 15) \
MBIM_ENUM_HELPER(MBIM_CID_DEVICE_SERVICES, 16) \
MBIM_ENUM_HELPER(MBIM_CID_DEVICE_SERVICE_SUBSCRIBE_LIST, 19) \
MBIM_ENUM_HELPER(MBIM_CID_PACKET_STATISTICS, 20) \
MBIM_ENUM_HELPER(MBIM_CID_NETWORK_IDLE_HINT, 21) \
MBIM_ENUM_HELPER(MBIM_CID_EMERGENCY_MODE, 22) \
MBIM_ENUM_HELPER(MBIM_CID_IP_PACKET_FILTERS, 23) \
MBIM_ENUM_HELPER(MBIM_CID_MULTICARRIER_PROVIDERS, 24)
#define MBIM_ENUM_HELPER(k, v) k = v,
typedef enum{
UUID_BASIC_CONNECT_CIDs
} UUID_BASIC_CONNECT_CID_E;
#undef MBIM_ENUM_HELPER
#define MBIM_ENUM_HELPER(k, v) {k, #k},
enumstrfunc(CID2, UUID_BASIC_CONNECT_CIDs);
#undef MBIM_ENUM_HELPER
static int mbim_ms_version = 1;
#define UUID_BASIC_CONNECT_EXT_CIDs \
MBIM_ENUM_HELPER(MBIM_CID_MS_PROVISIONED_CONTEXT_V2, 1) \
MBIM_ENUM_HELPER(MBIM_CID_MS_NETWORK_BLACKLIST, 2) \
MBIM_ENUM_HELPER(MBIM_CID_MS_LTE_ATTACH_CONFIG, 3) \
MBIM_ENUM_HELPER(MBIM_CID_MS_LTE_ATTACH_STATUS , 4) \
MBIM_ENUM_HELPER(MBIM_CID_MS_SYS_CAPS , 5) \
MBIM_ENUM_HELPER(MBIM_CID_MS_DEVICE_CAPS_V2, 6) \
MBIM_ENUM_HELPER(MBIM_CID_MS_DEVICE_SLOT_MAPPING, 7) \
MBIM_ENUM_HELPER(MBIM_CID_MS_SLOT_INFO_STATUS, 8) \
MBIM_ENUM_HELPER(MBIM_CID_MS_PCO, 9) \
MBIM_ENUM_HELPER(MBIM_CID_MS_DEVICE_RESET, 10) \
MBIM_ENUM_HELPER(MBIM_CID_MS_BASE_STATIONS_INFO, 11) \
MBIM_ENUM_HELPER(MBIM_CID_MS_LOCATION_INFO_STATUS, 12) \
MBIM_ENUM_HELPER(MBIM_CID_NOT_DEFINED, 13) \
MBIM_ENUM_HELPER(MBIM_CID_MS_PIN_EX, 14) \
MBIM_ENUM_HELPER(MBIM_CID_MS_VERSION , 15)
#define MBIM_ENUM_HELPER(k, v) k = v,
typedef enum{
UUID_BASIC_CONNECT_EXT_CIDs
} UUID_BASIC_CONNECT_EXT_CID_E;
#undef MBIM_ENUM_HELPER
#define MBIM_ENUM_HELPER(k, v) {k, #k},
enumstrfunc(MS_CID2, UUID_BASIC_CONNECT_EXT_CIDs);
#undef MBIM_ENUM_HELPER
typedef enum {
MBIM_CID_SMS_CONFIGURATION = 1, // Y Y Y
MBIM_CID_SMS_READ = 2, // N Y Y
MBIM_CID_SMS_SEND = 3, // Y N N
MBIM_CID_SMS_DELETE = 4, // Y N N
MBIM_CID_SMS_MESSAGE_STORE_STATUS = 5, // N Y Y
} UUID_SMS_CID_E;
typedef enum {
MBIM_CID_DSS_CONNECT = 1, // Y N N
} UUID_DSS_CID_E;
#define MBIM_MSGS \
MBIM_ENUM_HELPER(MBIM_OPEN_MSG, 1) \
MBIM_ENUM_HELPER(MBIM_CLOSE_MSG, 2) \
MBIM_ENUM_HELPER(MBIM_COMMAND_MSG, 3) \
MBIM_ENUM_HELPER(MBIM_HOST_ERROR_MSG, 4) \
\
MBIM_ENUM_HELPER(MBIM_OPEN_DONE, 0x80000001) \
MBIM_ENUM_HELPER(MBIM_CLOSE_DONE, 0x80000002) \
MBIM_ENUM_HELPER(MBIM_COMMAND_DONE, 0x80000003) \
MBIM_ENUM_HELPER(MBIM_FUNCTION_ERROR_MSG, 0x80000004) \
MBIM_ENUM_HELPER(MBIM_INDICATE_STATUS_MSG, 0x80000007)
#define MBIM_ENUM_HELPER(k, v) k = v,
typedef enum{
MBIM_MSGS
} MBIM_MSG_Type_E;
#undef MBIM_ENUM_HELPER
#define MBIM_ENUM_HELPER(k, v) {k, #k},
enumstrfunc(MBIMMSGType, MBIM_MSGS);
#undef MBIM_ENUM_HELPER
typedef enum { /*< since=1.10 >*/
MBIM_CID_PROXY_CONTROL_UNKNOWN = 0,
MBIM_CID_PROXY_CONTROL_CONFIGURATION = 1
} UUID_LIBMBIM_PROXY_CID_E;
typedef enum {
MBIM_ERROR_TIMEOUT_FRAGMENT = 1,
MBIM_ERROR_FRAGMENT_OUT_OF_SEQUENCE = 2,
MBIM_ERROR_LENGTH_MISMATCH = 3,
MBIM_ERROR_DUPLICATED_TID = 4,
MBIM_ERROR_NOT_OPENED = 5,
MBIM_ERROR_UNKNOWN = 6,
MBIM_ERROR_CANCEL = 7,
MBIM_ERROR_MAX_TRANSFER = 8,
} MBIM_ERROR_E;
typedef enum {
MBIM_STATUS_SUCCESS = 0,
MBIM_STATUS_BUSY = 1,
MBIM_STATUS_FAILURE = 2,
MBIM_STATUS_SIM_NOT_INSERTED = 3,
MBIM_STATUS_BAD_SIM = 4,
MBIM_STATUS_PIN_REQUIRED = 5,
MBIM_STATUS_PIN_DISABLED = 6,
MBIM_STATUS_NOT_REGISTERED = 7,
MBIM_STATUS_PROVIDERS_NOT_FOUND = 8,
MBIM_STATUS_NO_DEVICE_SUPPORT = 9,
MBIM_STATUS_PROVIDER_NOT_VISIBLE = 10,
MBIM_STATUS_DATA_CLASS_NOT_AVAILABL = 11,
MBIM_STATUS_PACKET_SERVICE_DETACHED = 12,
} MBIM_STATUS_CODES_E;
typedef enum {
MBIMPacketServiceActionAttach = 0,
MBIMPacketServiceActionDetach = 1,
} MBIM_PACKET_SERVICE_ACTION_E;
typedef enum {
MBIMPacketServiceStateUnknown = 0,
MBIMPacketServiceStateAttaching = 1,
MBIMPacketServiceStateAttached = 2,
MBIMPacketServiceStateDetaching = 3,
MBIMPacketServiceStateDetached = 4,
} MBIM_PACKET_SERVICE_STATE_E;
static const char *MBIMPacketServiceStateStr(int _val) {
struct { int val;char *name;} _enumstr[] = {
{MBIMPacketServiceStateUnknown, "Unknown"},
{MBIMPacketServiceStateAttaching, "Attaching"},
{MBIMPacketServiceStateAttached, "Attached"},
{MBIMPacketServiceStateDetaching, "Detaching"},
{MBIMPacketServiceStateDetached, "Detached"},
};
int idx;
for (idx = 0; idx < (int)(sizeof(_enumstr)/sizeof(_enumstr[0])); idx++) {
if (_val == _enumstr[idx].val)
return _enumstr[idx].name;
}
return "Undefined";
};
typedef enum {
MBIMDataClassNone = 0x0,
MBIMDataClassGPRS = 0x1,
MBIMDataClassEDGE = 0x2,
MBIMDataClassUMTS = 0x4,
MBIMDataClassHSDPA = 0x8,
MBIMDataClassHSUPA = 0x10,
MBIMDataClassLTE = 0x20,
MBIMDataClass5G_NSA = 0x40,
MBIMDataClass5G_SA = 0x80,
MBIMDataClass1XRTT = 0x10000,
MBIMDataClass1XEVDO = 0x20000,
MBIMDataClass1XEVDORevA = 0x40000,
MBIMDataClass1XEVDV = 0x80000,
MBIMDataClass3XRTT = 0x100000,
MBIMDataClass1XEVDORevB = 0x200000,
MBIMDataClassUMB = 0x400000,
MBIMDataClassCustom = 0x80000000,
} MBIM_DATA_CLASS_E;
static const char *MBIMDataClassStr(int _val) {
struct { int val;char *name;} _enumstr[] = {
{MBIMDataClassNone, "None"},
{MBIMDataClassGPRS, "GPRS"},
{MBIMDataClassEDGE, "EDGE"},
{MBIMDataClassUMTS, "UMTS"},
{MBIMDataClassHSDPA, "HSDPA"},
{MBIMDataClassHSUPA, "HSUPA"},
{MBIMDataClassLTE, "LTE"},
{MBIMDataClass5G_NSA, "5G_NSA"},
{MBIMDataClass5G_SA, "5G_SA"},
{MBIMDataClass1XRTT, "1XRTT"},
{MBIMDataClass1XEVDO, "1XEVDO"},
{MBIMDataClass1XEVDORevA, "1XEVDORevA"},
{MBIMDataClass1XEVDV, "1XEVDV"},
{MBIMDataClass3XRTT, "3XRTT"},
{MBIMDataClass1XEVDORevB, "1XEVDORevB"},
{MBIMDataClassUMB, "UMB"},
{MBIMDataClassCustom, "Custom"},
};
int idx;
for (idx = 0; idx < (int)(sizeof(_enumstr)/sizeof(_enumstr[0])); idx++) {
if (_val == _enumstr[idx].val)
return _enumstr[idx].name;
}
return "Unknow";
};
typedef struct {
UINT32 NwError;
UINT32 PacketServiceState; //MBIM_PACKET_SERVICE_STATE_E
UINT32 HighestAvailableDataClass; //MBIM_DATA_CLASS_E
UINT64 UplinkSpeed;
UINT64 DownlinkSpeed;
} MBIM_PACKET_SERVICE_INFO_T;
typedef struct {
UINT32 NwError;
UINT32 PacketServiceState; //MBIM_PACKET_SERVICE_STATE_E
UINT32 CurrentDataClass; //MBIM_DATA_CLASS_E
UINT64 UplinkSpeed;
UINT64 DownlinkSpeed;
UINT32 FrequencyRange;
} MBIM_PACKET_SERVICE_INFO_V2_T;
typedef enum {
MBIMSubscriberReadyStateNotInitialized = 0,
MBIMSubscriberReadyStateInitialized = 1,
MBIMSubscriberReadyStateSimNotInserted = 2,
MBIMSubscriberReadyStateBadSim = 3,
MBIMSubscriberReadyStateFailure = 4,
MBIMSubscriberReadyStateNotActivated = 5,
MBIMSubscriberReadyStateDeviceLocked = 6,
}MBIM_SUBSCRIBER_READY_STATE_E;
static const char *MBIMSubscriberReadyStateStr(int _val) {
struct { int val;char *name;} _enumstr[] = {
{MBIMSubscriberReadyStateNotInitialized, "NotInitialized"},
{MBIMSubscriberReadyStateInitialized, "Initialized"},
{MBIMSubscriberReadyStateSimNotInserted, "NotInserted"},
{MBIMSubscriberReadyStateBadSim, "BadSim"},
{MBIMSubscriberReadyStateFailure, "Failure"},
{MBIMSubscriberReadyStateNotActivated, "NotActivated"},
{MBIMSubscriberReadyStateDeviceLocked, "DeviceLocked"},
};
int idx;
for (idx = 0; idx < (int)(sizeof(_enumstr)/sizeof(_enumstr[0])); idx++) {
if (_val == _enumstr[idx].val)
return _enumstr[idx].name;
}
return "Undefined";
};
typedef struct {
UINT32 DeviceType; //MBIM_DEVICE_TYPE
UINT32 CellularClass; //MBIM_CELLULAR_CLASS
UINT32 VoiceClass; //MBIM_VOICE_CLASS
UINT32 SimClass; //MBIM_SIM_CLASS
UINT32 DataClass; //MBIM_DATA_CLASS
UINT32 SmsCaps; //MBIM_SMS_CAPS
UINT32 ControlCaps; //MBIM_CTRL_CAPS
UINT32 MaxSessions;
UINT32 CustomDataClassOffset;
UINT32 CustomDataClassSize;
UINT32 DeviceIdOffset;
UINT32 DeviceIdSize;
UINT32 FirmwareInfoOffset;
UINT32 FirmwareInfoSize;
UINT32 HardwareInfoOffset;
UINT32 HardwareInfoSize;
UINT8 DataBuffer[0]; //DeviceId FirmwareInfo HardwareInfo
} MBIM_DEVICE_CAPS_INFO_T;
typedef enum {
MBIMRadioOff = 0,
MBIMRadioOn = 1,
} MBIM_RADIO_SWITCH_STATE_E;
typedef struct {
MBIM_RADIO_SWITCH_STATE_E RadioState;
} MBIM_SET_RADIO_STATE_T;
typedef struct {
MBIM_RADIO_SWITCH_STATE_E HwRadioState;
MBIM_RADIO_SWITCH_STATE_E SwRadioState;
} MBIM_RADIO_STATE_INFO_T;
typedef enum {
MBIMReadyInfoFlagsNone,
MBIMReadyInfoFlagsProtectUniqueID,
}MBIM_UNIQUE_ID_FLAGS;
typedef struct {
UINT32 ReadyState;
UINT32 SubscriberIdOffset;
UINT32 SubscriberIdSize;
UINT32 SimIccIdOffset;
UINT32 SimIccIdSize;
UINT32 ReadyInfo;
UINT32 ElementCount;
UINT8 *TelephoneNumbersRefList;
UINT8 *DataBuffer;
} MBIM_SUBSCRIBER_READY_STATUS_T;
typedef enum {
MBIMRegisterActionAutomatic,
MBIMRegisterActionManual,
}MBIM_REGISTER_ACTION_E;
typedef enum {
MBIMRegisterStateUnknown = 0,
MBIMRegisterStateDeregistered = 1,
MBIMRegisterStateSearching = 2,
MBIMRegisterStateHome = 3,
MBIMRegisterStateRoaming = 4,
MBIMRegisterStatePartner = 5,
MBIMRegisterStateDenied = 6,
}MBIM_REGISTER_STATE_E;
typedef enum {
MBIMRegisterModeUnknown = 0,
MBIMRegisterModeAutomatic = 1,
MBIMRegisterModeManual = 2,
}MBIM_REGISTER_MODE_E;
static const char *MBIMRegisterStateStr(int _val) {
struct { int val;char *name;} _enumstr[] ={
{MBIMRegisterStateUnknown, "Unknown"},
{MBIMRegisterStateDeregistered, "Deregistered"},
{MBIMRegisterStateSearching, "Searching"},
{MBIMRegisterStateHome, "Home"},
{MBIMRegisterStateRoaming, "Roaming"},
{MBIMRegisterStatePartner, "Partner"},
{MBIMRegisterStateDenied, "Denied"},
};
int idx;
for (idx = 0; idx < (int)(sizeof(_enumstr)/sizeof(_enumstr[0])); idx++) {
if (_val == _enumstr[idx].val)
return _enumstr[idx].name;
}
return "Undefined";
};
static const char *MBIMRegisterModeStr(int _val) {
struct { int val;char *name;} _enumstr[] = {
{MBIMRegisterModeUnknown, "Unknown"},
{MBIMRegisterModeAutomatic, "Automatic"},
{MBIMRegisterModeManual, "Manual"},
};
int idx;
for (idx = 0; idx < (int)(sizeof(_enumstr)/sizeof(_enumstr[0])); idx++) {
if (_val == _enumstr[idx].val)
return _enumstr[idx].name;
}
return "Undefined";
};
typedef enum {
MBIM_REGISTRATION_NONE,
MBIM_REGISTRATION_MANUAL_SELECTION_NOT_AVAILABLE,
MBIM_REGISTRATION_PACKET_SERVICE_AUTOMATIC_ATTACH,
}MBIM_REGISTRATION_FLAGS_E;
typedef struct {
UINT32 NwError;
UINT32 RegisterState; //MBIM_REGISTER_STATE_E
UINT32 RegisterMode;
UINT32 AvailableDataClasses;
UINT32 CurrentCellularClass;
UINT32 ProviderIdOffset;
UINT32 ProviderIdSize;
UINT32 ProviderNameOffset;
UINT32 ProviderNameSize;
UINT32 RoamingTextOffset;
UINT32 RoamingTextSize;
UINT32 RegistrationFlag;
UINT8 *DataBuffer;
} MBIM_REGISTRATION_STATE_INFO_T;
typedef struct {
UINT32 NwError;
UINT32 RegisterState; //MBIM_REGISTER_STATE_E
UINT32 RegisterMode;
UINT32 AvailableDataClasses;
UINT32 CurrentCellularClass;
UINT32 ProviderIdOffset;
UINT32 ProviderIdSize;
UINT32 ProviderNameOffset;
UINT32 ProviderNameSize;
UINT32 RoamingTextOffset;
UINT32 RoamingTextSize;
UINT32 RegistrationFlag;
UINT32 PreferredDataClass;
UINT8 *DataBuffer;
} MBIM_REGISTRATION_STATE_INFO_V2_T;
typedef struct {
UINT32 MessageType; //Specifies the MBIM message type.
UINT32 MessageLength; //Specifies the total length of this MBIM message in bytes.
/* Specifies the MBIM message id value. This value is used to match host sent messages with function responses.
This value must be unique among all outstanding transactions.
For notifications, the TransactionId must be set to 0 by the function */
UINT32 TransactionId;
} MBIM_MESSAGE_HEADER;
typedef struct {
UINT32 TotalFragments; //this field indicates how many fragments there are intotal.
UINT32 CurrentFragment; //This field indicates which fragment this message is. Values are 0 to TotalFragments?\1
} MBIM_FRAGMENT_HEADER;
typedef struct {
MBIM_MESSAGE_HEADER MessageHeader;
UINT32 MaxControlTransfer;
} MBIM_OPEN_MSG_T;
typedef struct {
MBIM_MESSAGE_HEADER MessageHeader;
UINT32 Status;
} MBIM_OPEN_DONE_T;
typedef struct {
MBIM_MESSAGE_HEADER MessageHeader;
} MBIM_CLOSE_MSG_T;
typedef struct {
MBIM_MESSAGE_HEADER MessageHeader;
UINT32 Status;
} MBIM_CLOSE_DONE_T;
typedef struct {
UINT8 uuid[16];
} UUID_T;
typedef struct {
MBIM_MESSAGE_HEADER MessageHeader;
MBIM_FRAGMENT_HEADER FragmentHeader;
UUID_T DeviceServiceId; //A 16 byte UUID that identifies the device service the following CID value applies.
UINT32 CID; //Specifies the CID that identifies the parameter being queried for
UINT32 CommandType; //0 for a query operation, 1 for a Set operation
UINT32 InformationBufferLength; //Size of the Total InformationBuffer, may be larger than current message if fragmented.
UINT8 InformationBuffer[0]; //Data supplied to device specific to the CID
} MBIM_COMMAND_MSG_T;
typedef struct {
MBIM_MESSAGE_HEADER MessageHeader;
MBIM_FRAGMENT_HEADER FragmentHeader;
UUID_T DeviceServiceId; //A 16 byte UUID that identifies the device service the following CID value applies.
UINT32 CID; //Specifies the CID that identifies the parameter being queried for
UINT32 Status;
UINT32 InformationBufferLength; //Size of the Total InformationBuffer, may be larger than current message if fragmented.
UINT8 InformationBuffer[0]; //Data supplied to device specific to the CID
} MBIM_COMMAND_DONE_T;
typedef struct {
MBIM_MESSAGE_HEADER MessageHeader;
UINT32 ErrorStatusCode;
} MBIM_HOST_ERROR_MSG_T;
typedef struct {
MBIM_MESSAGE_HEADER MessageHeader;
UINT32 ErrorStatusCode;
} MBIM_FUNCTION_ERROR_MSG_T;
typedef struct {
MBIM_MESSAGE_HEADER MessageHeader;
MBIM_FRAGMENT_HEADER FragmentHeader;
UUID_T DeviceServiceId; //A 16 byte UUID that identifies the device service the following CID value applies.
UINT32 CID; //Specifies the CID that identifies the parameter being queried for
UINT32 InformationBufferLength; //Size of the Total InformationBuffer, may be larger than current message if fragmented.
UINT8 InformationBuffer[0]; //Data supplied to device specific to the CID
} MBIM_INDICATE_STATUS_MSG_T;
typedef struct {
UINT32 offset;
UINT32 size;
} OL_PAIR_LIST;
typedef struct {
UUID_T DeviceServiceId;
UINT32 DssPayload;
UINT32 MaxDssInstances;
UINT32 CidCount;
UINT32 CidList[];
} MBIM_DEVICE_SERVICE_ELEMENT_T;
typedef struct {
UINT32 DeviceServicesCount;
UINT32 MaxDssSessions;
OL_PAIR_LIST DeviceServicesRefList[];
} MBIM_DEVICE_SERVICES_INFO_T;
typedef enum {
MBIMActivationCommandDeactivate = 0,
MBIMActivationCommandActivate = 1,
} MBIM_ACTIVATION_COMMAND_E;
typedef enum {
MBIMCompressionNone = 0,
MBIMCompressionEnable = 1,
} MBIM_COMPRESSION_E;
typedef enum {
MBIMAuthProtocolNone = 0,
MBIMAuthProtocolPap = 1,
MBIMAuthProtocolChap = 2,
MBIMAuthProtocolMsChapV2 = 3,
} MBIM_AUTH_PROTOCOL_E;
#define MBIMContextIPTypes \
MBIM_ENUM_HELPER(MBIMContextIPTypeDefault, 0) \
MBIM_ENUM_HELPER(MBIMContextIPTypeIPv4, 1) \
MBIM_ENUM_HELPER(MBIMContextIPTypeIPv6, 2) \
MBIM_ENUM_HELPER(MBIMContextIPTypeIPv4v6, 3) \
MBIM_ENUM_HELPER(MBIMContextIPTypeIPv4AndIPv6, 4)
#define MBIM_ENUM_HELPER(k, v) k = v,
typedef enum {
MBIMContextIPTypes
} MBIM_CONTEXT_IP_TYPE_E;
#undef MBIM_ENUM_HELPER
#define MBIM_ENUM_HELPER(k, v) {k, #k},
enumstrfunc(MBIMContextIPType, MBIMContextIPTypes);
#undef MBIM_ENUM_HELPER
typedef enum {
MBIMActivationStateUnknown = 0,
MBIMActivationStateActivated = 1,
MBIMActivationStateActivating = 2,
MBIMActivationStateDeactivated = 3,
MBIMActivationStateDeactivating = 4,
} MBIM_ACTIVATION_STATE_E;
typedef enum {
MBIMVoiceCallStateNone = 0,
MBIMVoiceCallStateInProgress = 1,
MBIMVoiceCallStateHangUp = 2,
} MBIM_VOICECALL_STATE_E;
static const char *MBIMActivationStateStr(int _val) {
struct { int val;char *name;} _enumstr[] = {
{MBIMActivationStateUnknown, "Unknown"},
{MBIMActivationStateActivated, "Activated"},
{MBIMActivationStateActivating, "Activating"},
{MBIMActivationStateDeactivated, "Deactivated"},
{MBIMActivationStateDeactivating, "Deactivating"},
};
int idx;
for (idx = 0; idx < (int)(sizeof(_enumstr)/sizeof(_enumstr[0])); idx++) {
if (_val == _enumstr[idx].val)
return _enumstr[idx].name;
}
return "Undefined";
};
static const char *MBIMVoiceCallStateStr(int _val) {
struct { int val;char *name;} _enumstr[] = {
{MBIMVoiceCallStateNone, "None"},
{MBIMVoiceCallStateInProgress, "InProgress"},
{MBIMVoiceCallStateHangUp, "HangUp"},
};
int idx;
for (idx = 0; idx < (int)(sizeof(_enumstr)/sizeof(_enumstr[0])); idx++) {
if (_val == _enumstr[idx].val)
return _enumstr[idx].name;
}
return "Undefined";
};
typedef struct {
UINT32 SessionId;
UINT32 ActivationCommand; //MBIM_ACTIVATION_COMMAND_E
UINT32 AccessStringOffset;
UINT32 AccessStringSize;
UINT32 UserNameOffset;
UINT32 UserNameSize;
UINT32 PasswordOffset;
UINT32 PasswordSize;
UINT32 Compression; //MBIM_COMPRESSION_E
UINT32 AuthProtocol; //MBIM_AUTH_PROTOCOL_E
UINT32 IPType; //MBIM_CONTEXT_IP_TYPE_E
UUID_T ContextType;
UINT8 DataBuffer[0]; /* apn, username, password */
} MBIM_SET_CONNECT_T;
typedef struct {
UINT32 SessionId;
UINT32 ActivationState; //MBIM_ACTIVATION_STATE_E
UINT32 VoiceCallState;
UINT32 IPType; //MBIM_CONTEXT_IP_TYPE_E
UUID_T ContextType;
UINT32 NwError;
} MBIM_CONNECT_T;
typedef struct {
UINT32 OnLinkPrefixLength;
UINT8 IPv4Address[4];
} MBIM_IPV4_ELEMENT_T;
typedef struct {
UINT32 OnLinkPrefixLength;
UINT8 IPv6Address[16];
} MBIM_IPV6_ELEMENT_T;
typedef struct {
UINT32 SessionId;
UINT32 IPv4ConfigurationAvailable; //bit0~Address, bit1~gateway, bit2~DNS, bit3~MTU
UINT32 IPv6ConfigurationAvailable; //bit0~Address, bit1~gateway, bit2~DNS, bit3~MTU
UINT32 IPv4AddressCount;
UINT32 IPv4AddressOffset;
UINT32 IPv6AddressCount;
UINT32 IPv6AddressOffset;
UINT32 IPv4GatewayOffset;
UINT32 IPv6GatewayOffset;
UINT32 IPv4DnsServerCount;
UINT32 IPv4DnsServerOffset;
UINT32 IPv6DnsServerCount;
UINT32 IPv6DnsServerOffset;
UINT32 IPv4Mtu;
UINT32 IPv6Mtu;
UINT8 DataBuffer[];
} MBIM_IP_CONFIGURATION_INFO_T;
typedef struct {
UINT32 RSRP;
UINT32 SNR;
UINT32 RSRPThreshold;
UINT32 SNRThreshold;
UINT32 SystemType;
} MBIM_RSRP_SNR_INFO_T;
typedef struct {
UINT32 Elementcount;
MBIM_RSRP_SNR_INFO_T RsrpSnr[0];
} MBIM_RSRP_SNR_T;
typedef struct {
UINT32 Rssi;
UINT32 ErrorRate;
UINT32 SignalStrengthInterval;
UINT32 RssiThreshold;
UINT32 ErrorRateThreshold;
} MBIM_SIGNAL_STATE_INFO_T;
typedef struct {
UINT32 Rssi;
UINT32 ErrorRate;
UINT32 SignalStrengthInterval;
UINT32 RssiThreshold;
UINT32 ErrorRateThreshold;
UINT32 RsrpSnrOffset;
UINT32 RsrpSnrSize;
UINT8 DataBuffer[];
} MBIM_SIGNAL_STATE_INFO_V2_T;
typedef struct {
UINT32 SignalStrengthInterval;
UINT32 RssiThreshold;
UINT32 ErrorRateThreshold;
} MBIM_SET_SIGNAL_STATE_T;
typedef struct {
UINT32 DevicePathOffset;
UINT32 DevicePathSize;
UINT32 Timeout;
UINT8 DataBuffer[];
} MBIM_LIBQMI_PROXY_CONFIG_T;
#pragma pack()
static pthread_t s_tid_reader = 0;
static int mbim_verbose = 0;
static UINT32 TransactionId = 1;
static unsigned mbim_default_timeout = 30000;
static const char *mbim_apn = NULL;
static const char *mbim_user = NULL;
static const char *mbim_passwd = NULL;
static int mbim_iptype = MBIMContextIPTypeDefault;
static int mbim_auth = MBIMAuthProtocolNone;
static int mbim_sessionID = 0;
static int mbim_fd = -1;
static MBIM_MESSAGE_HEADER *mbim_pRequest;
static MBIM_MESSAGE_HEADER *mbim_pResponse;
static const UUID_T * str2uuid(const char *str) {
static UUID_T uuid;
UINT32 d[16];
char tmp[16*2+4+1];
unsigned i = 0;
while (str[i]) {
tmp[i] = tolower(str[i]);
i++;
}
tmp[i] = '\0';
sscanf(tmp, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
&d[0], &d[1], &d[2], &d[3], &d[4], &d[5], &d[6], &d[7],
&d[8], &d[9], &d[10], &d[11], &d[12], &d[13], &d[14], &d[15]);
for (i = 0; i < 16; i++) {
uuid.uuid[i] = d[i]&0xFF;
}
return &uuid;
}
static void wchar2char(const char *src, size_t src_size, char *dst, size_t dst_len) {
size_t i;
for (i = 0; i < (dst_len-1) && i < (src_size/2); i++)
dst[i] = src[i*2];
dst[i] = 0;
}
static size_t char2wchar(const char *src, size_t src_len, uint8_t *dst, size_t dst_len) {
size_t i;
if (src_len > (dst_len/2))
src_len = (dst_len/2);
for (i = 0; i < src_len; i++) {
*dst++ = *src++;
*dst++ = 0;
}
return i*2;
}
#define mbim_alloc( _size) malloc(_size)
#define mbim_free(_mem) do { if (_mem) { free(_mem); _mem = NULL;}} while(0)
static int mbim_open_state = 0;
static MBIM_SUBSCRIBER_READY_STATE_E ReadyState = MBIMSubscriberReadyStateNotInitialized;
static MBIM_REGISTER_STATE_E RegisterState = MBIMRegisterStateUnknown;
static MBIM_PACKET_SERVICE_STATE_E PacketServiceState = MBIMPacketServiceStateUnknown;
static MBIM_ACTIVATION_STATE_E ActivationState = MBIMActivationStateUnknown;
static MBIM_SUBSCRIBER_READY_STATE_E oldReadyState = MBIMSubscriberReadyStateNotInitialized;
static MBIM_REGISTER_STATE_E oldRegisterState = MBIMRegisterStateUnknown;
static MBIM_PACKET_SERVICE_STATE_E oldPacketServiceState = MBIMPacketServiceStateUnknown;
static MBIM_ACTIVATION_STATE_E oldActivationState = MBIMActivationStateUnknown;
static int mbim_update_state(void);
static __inline uint32_t mbim2qmi_ipv4addr(uint32_t addr) {
return (addr>>24) | (addr>>8&0xff00) | (addr<<8&0xff0000) | (addr<<24);
}
static __inline void mbim2qmi_ipv6addr(const unsigned char *src, unsigned char *dst) {
int i;
for (i = 0; i < 16 ; i++) {
dst[i] = src[i];
}
}
static MBIM_MESSAGE_HEADER *compose_open_command(UINT32 MaxControlTransfer)
{
MBIM_OPEN_MSG_T *pRequest = (MBIM_OPEN_MSG_T *)mbim_alloc(sizeof(MBIM_OPEN_MSG_T));
if(!pRequest)
return NULL;
pRequest->MessageHeader.MessageType = htole32(MBIM_OPEN_MSG);
pRequest->MessageHeader.MessageLength = htole32(sizeof(MBIM_COMMAND_MSG_T));
pRequest->MessageHeader.TransactionId = htole32(TransactionId++);
pRequest->MaxControlTransfer = htole32(MaxControlTransfer);
return &pRequest->MessageHeader;
}
static MBIM_MESSAGE_HEADER *compose_close_command(void)
{
MBIM_CLOSE_MSG_T *pRequest = (MBIM_CLOSE_MSG_T *)mbim_alloc(sizeof(MBIM_CLOSE_MSG_T));
if(!pRequest)
return NULL;
pRequest->MessageHeader.MessageType = htole32(MBIM_CLOSE_MSG);
pRequest->MessageHeader.MessageLength = htole32(sizeof(MBIM_CLOSE_MSG_T));
pRequest->MessageHeader.TransactionId = htole32(TransactionId++);
return &pRequest->MessageHeader;
}
static MBIM_MESSAGE_HEADER *compose_basic_connect_command(UINT32 CID, UINT32 CommandType, void *pInformationBuffer, UINT32 InformationBufferLength)
{
MBIM_COMMAND_MSG_T *pRequest = (MBIM_COMMAND_MSG_T *)mbim_alloc(sizeof(MBIM_COMMAND_MSG_T) + InformationBufferLength);
if (!pRequest)
return NULL;
pRequest->MessageHeader.MessageType = htole32(MBIM_COMMAND_MSG);
pRequest->MessageHeader.MessageLength = htole32((sizeof(MBIM_COMMAND_MSG_T) + InformationBufferLength));
pRequest->MessageHeader.TransactionId = htole32(TransactionId++);
pRequest->FragmentHeader.TotalFragments = htole32(1);
pRequest->FragmentHeader.CurrentFragment= htole32(0);
memcpy(pRequest->DeviceServiceId.uuid, str2uuid(UUID_BASIC_CONNECT), 16);
pRequest->CID = htole32(CID);
pRequest->CommandType = htole32(CommandType);
if (InformationBufferLength && pInformationBuffer) {
pRequest->InformationBufferLength = htole32(InformationBufferLength);
memcpy(pRequest->InformationBuffer, pInformationBuffer, InformationBufferLength);
} else {
pRequest->InformationBufferLength = htole32(0);
}
return &pRequest->MessageHeader;
}
static MBIM_MESSAGE_HEADER *compose_basic_connect_ext_command(UINT32 CID, UINT32 CommandType, void *pInformationBuffer, UINT32 InformationBufferLength)
{
MBIM_COMMAND_MSG_T *pRequest = (MBIM_COMMAND_MSG_T *)compose_basic_connect_command(CID, CommandType, pInformationBuffer, InformationBufferLength);
if (!pRequest)
return NULL;
memcpy(pRequest->DeviceServiceId.uuid, str2uuid(UUID_BASIC_CONNECT_EXT), 16);
return &pRequest->MessageHeader;
}
static const char * uuid2str(const UUID_T *pUUID) {
static char str[16*2+4+1];
const UINT8 *d = pUUID->uuid;
snprintf(str, sizeof(str), "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7],
d[8], d[9], d[10], d[11], d[12], d[13], d[14], d[15]);
return str;
}
static const char *DeviceServiceId2str(const UUID_T *pUUID) {
const char *str = uuid2str(pUUID);
struct { char *val;char *name;} _enumstr[] = {
{UUID_BASIC_CONNECT, "UUID_BASIC_CONNECT"},
{UUID_BASIC_CONNECT_EXT, "UUID_BASIC_CONNECT_EXT"},
{UUID_SMS, "UUID_SMS"},
{UUID_USSD, "UUID_USSD"},
{UUID_PHONEBOOK, "UUID_PHONEBOOK"},
{UUID_STK, "UUID_STK"},
{UUID_AUTH, "UUID_AUTH"},
{UUID_DSS, "UUID_DSS"},
{uuid_ext_qmux, "uuid_ext_qmux"},
{uuid_mshsd, "uuid_mshsd"},
{uuid_qmbe, "uuid_qmbe"},
{UUID_MSFWID, "UUID_MSFWID"},
{uuid_atds, "uuid_atds"},
{uuid_qdu, "uuid_qdu"},
{UUID_MS_UICC_LOW_LEVEL, "UUID_MS_UICC_LOW_LEVEL"},
{UUID_MS_SARControl, "UUID_MS_SARControl"},
{UUID_VOICEEXTENSIONS, "UUID_VOICEEXTENSIONS"},
{UUID_LIBMBIM_PROXY, "UUID_LIBMBIM_PROXY"},
};
int idx;
for (idx = 0; idx < (int)(sizeof(_enumstr)/sizeof(_enumstr[0])); idx++) {
if (!strcasecmp(str, _enumstr[idx].val))
return _enumstr[idx].name;
}
return str;
}
static const char *mbim_get_segment(void *_pMsg, UINT32 offset, UINT32 len)
{
int idx;
static char buff[256] = {'\0'};
UINT8 *pMsg = (UINT8*)_pMsg;