-
Notifications
You must be signed in to change notification settings - Fork 81
/
u_cell_private.c
1705 lines (1550 loc) · 74.2 KB
/
u_cell_private.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
/*
* Copyright 2019-2024 u-blox
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* Only #includes of u_* and the C standard library are allowed here,
* no platform stuff and no OS stuff. Anything required from
* the platform/OS must be brought in through u_port* to maintain
* portability.
*/
/** @file
* @brief Implementation of functions that are private to cellular.
*/
#ifdef U_CFG_OVERRIDE
# include "u_cfg_override.h" // For a customer's configuration override
#endif
#include "stdio.h" // snprintf()
#include "stddef.h" // NULL, size_t etc.
#include "stdint.h" // int32_t etc.
#include "stdbool.h"
#include "string.h" // memset()
#include "ctype.h" // isdigit()
#include "u_error_common.h"
#include "u_port.h"
#include "u_port_os.h"
#include "u_port_heap.h"
#include "u_port_uart.h"
#include "u_port_gpio.h"
#include "u_port_event_queue.h"
#include "u_port_debug.h"
#include "u_at_client.h"
#include "u_sock.h"
#include "u_security.h"
#include "u_cell_module_type.h"
#include "u_cell_file.h" // U_CELL_FILE_NAME_MAX_LENGTH
#include "u_cell.h" // Order is
#include "u_cell_net.h" // important here
#include "u_cell_private.h" // don't change it
#include "u_cell_pwr.h"
#include "u_cell_cfg.h"
#include "u_cell_http.h"
#include "u_cell_time.h"
#include "u_cell_http_private.h"
#include "u_cell_pwr_private.h"
#include "u_cell_time_private.h"
/* ----------------------------------------------------------------
* COMPILE-TIME MACROS
* -------------------------------------------------------------- */
#ifndef U_CELL_PRIVATE_DTR_PIN_HYSTERESIS_INTERVAL_MS
/** When performing hysteresis of the DTR pin, the interval to use for each
* wait step; value in milliseconds.
*/
# define U_CELL_PRIVATE_DTR_PIN_HYSTERESIS_INTERVAL_MS U_AT_CLIENT_ACTIVITY_PIN_HYSTERESIS_INTERVAL_MS
#endif
/* ----------------------------------------------------------------
* TYPES
* -------------------------------------------------------------- */
/* ----------------------------------------------------------------
* VARIABLES THAT ARE SHARED THROUGHOUT THE CELLULAR IMPLEMENTATION
* -------------------------------------------------------------- */
/** Root for the linked list of instances.
*/
uCellPrivateInstance_t *gpUCellPrivateInstanceList = NULL;
/** Mutex to protect the linked list.
*/
uPortMutexHandle_t gUCellPrivateMutex = NULL;
/** The characteristics of the modules supported by this driver,
* compiled into the driver.
*/
const uCellPrivateModule_t gUCellPrivateModuleList[] = {
{
U_CELL_MODULE_TYPE_SARA_U201, 1 /* Pwr On pull ms */, 1500 /* Pwr off pull ms */,
5 /* Boot wait */, 5 /* Min awake */, 5 /* Pwr down wait */, 5 /* Reboot wait */, 10 /* AT timeout */,
50 /* Cmd wait ms */, 2000 /* Resp max wait ms */, 0 /* radioOffCfun */, 75 /* resetHoldMilliseconds */,
2 /* Simultaneous RATs */,
((1ULL << (int32_t) U_CELL_NET_RAT_GSM_GPRS_EGPRS) |
(1ULL << (int32_t) U_CELL_NET_RAT_UTRAN)) /* RATs */,
((1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_USE_UPSD_CONTEXT_ACTIVATION) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_CONTEXT_MAPPING_REQUIRED) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_AUTO_BAUDING) |
// In theory SARA-U201 does support DTR power saving however we do not
// have this in our regression test farm and hence it is not marked
// as supported for now
// (1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_DTR_POWER_SAVING)
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_AT_PROFILES) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_CTS_CONTROL) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_SOCK_SET_LOCAL_PORT) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_UART_POWER_SAVING) |
// CMUX is supported here but we do not test it hence it is not marked as supported
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_UCGED) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_AUTHENTICATION_MODE_AUTOMATIC) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_HTTP) /* features */
// PPP is supported by the module but we do not test its integration with
// ubxlib and hence it is not marked as supported
),
6, /* Default CMUX channel for GNSS */
15 /* AT+CFUN reboot command */
},
{
U_CELL_MODULE_TYPE_SARA_R410M_02B, 300 /* Pwr On pull ms */, 2000 /* Pwr off pull ms */,
6 /* Boot wait */, 30 /* Min awake */, 35 /* Pwr down wait */, 5 /* Reboot wait */, 10 /* AT timeout */,
100 /* Cmd wait ms */, 3000 /* Resp max wait ms */, 4 /* radioOffCfun */, 16500 /* resetHoldMilliseconds */,
2 /* Simultaneous RATs */,
((1ULL << (int32_t) U_CELL_NET_RAT_CATM1) |
(1ULL << (int32_t) U_CELL_NET_RAT_NB1)) /* RATs */,
((1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MNO_PROFILE) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_ASYNC_SOCK_CLOSE) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTT) |
// In theory SARA-R410M does support keep alive but I have been
// unable to make it work (always returns error) and hence this is
// not marked as supported for now
// (1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTT_KEEP_ALIVE) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTT_SARA_R4_OLD_SYNTAX) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_UCGED5) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_FILE_SYSTEM_TAG) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_3GPP_POWER_SAVING) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_EDRX) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_FOTA) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_UART_POWER_SAVING) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_CMUX) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_LWM2M) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_UCGED) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_HTTP) /* features */
// PPP is supported by the module but we do not test its integration with
// ubxlib and hence it is not marked as supported
),
3, /* Default CMUX channel for GNSS */
15 /* AT+CFUN reboot command */
},
{
U_CELL_MODULE_TYPE_SARA_R412M_02B, 300 /* Pwr On pull ms */, 2000 /* Pwr off pull ms */,
5 /* Boot wait */, 30 /* Min awake */, 35 /* Pwr down wait */, 10 /* Reboot wait */, 10 /* AT timeout */,
100 /* Cmd wait ms */, 3000 /* Resp max wait ms */, 4 /* radioOffCfun */, 16500 /* resetHoldMilliseconds */,
3 /* Simultaneous RATs */,
((1ULL << (int32_t) U_CELL_NET_RAT_GSM_GPRS_EGPRS) |
(1ULL << (int32_t) U_CELL_NET_RAT_CATM1) |
(1ULL << (int32_t) U_CELL_NET_RAT_NB1)) /* RATs */,
((1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MNO_PROFILE) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_CSCON) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_ASYNC_SOCK_CLOSE) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_SECURITY_TLS_SERVER_NAME_INDICATION) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTT) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTT_SARA_R4_OLD_SYNTAX) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTT_SET_LOCAL_PORT) |
// In theory SARA-R412M does support keep alive but I have been
// unable to make it work (always returns error) and hence this is
// not marked as supported for now
// (1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTT_KEEP_ALIVE) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTT_SESSION_RETAIN) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_UCGED5) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_FILE_SYSTEM_TAG) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_3GPP_POWER_SAVING) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_EDRX) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_FOTA) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_UART_POWER_SAVING) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_CMUX) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_LWM2M) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_UCGED) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_HTTP) /* features */
// PPP is supported by the module but we do not test its integration with
// ubxlib and hence it is not marked as supported
),
3, /* Default CMUX channel for GNSS */
15 /* AT+CFUN reboot command */
},
{
U_CELL_MODULE_TYPE_SARA_R412M_03B, 300 /* Pwr On pull ms */, 2000 /* Pwr off pull ms */,
6 /* Boot wait */, 30 /* Min awake */, 35 /* Pwr down wait */, 5 /* Reboot wait */, 10 /* AT timeout */,
100 /* Cmd wait ms */, 2000 /* Resp max wait ms */, 4 /* radioOffCfun */, 16500 /* resetHoldMilliseconds */,
3 /* Simultaneous RATs */,
((1ULL << (int32_t) U_CELL_NET_RAT_GSM_GPRS_EGPRS) |
(1ULL << (int32_t) U_CELL_NET_RAT_CATM1) |
(1ULL << (int32_t) U_CELL_NET_RAT_NB1)) /* RATs */,
((1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MNO_PROFILE) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_CSCON) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_SECURITY_TLS_SERVER_NAME_INDICATION) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_UCGED5) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_FILE_SYSTEM_TAG) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_DEEP_SLEEP_URC) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_3GPP_POWER_SAVING) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_EDRX) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_UART_POWER_SAVING) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_CMUX) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_UCGED) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_HTTP) /* features */
// PPP is supported by the module but we do not test its integration with
// ubxlib and hence it is not marked as supported
),
3, /* Default CMUX channel for GNSS */
15 /* AT+CFUN reboot command */
},
{
U_CELL_MODULE_TYPE_SARA_R5, 1500 /* Pwr On pull ms */, 2000 /* Pwr off pull ms */,
6 /* Boot wait */, 10 /* Min awake */, 20 /* Pwr down wait */, 15 /* Reboot wait */, 10 /* AT timeout */,
20 /* Cmd wait ms */, 3000 /* Resp max wait ms */, 4 /* radioOffCfun */, 150 /* resetHoldMilliseconds */,
1 /* Simultaneous RATs */,
#ifdef U_CELL_CFG_SARA_R5_00B
(1ULL << (int32_t) U_CELL_NET_RAT_CATM1) /* RATs */,
#else
((1ULL << (int32_t) U_CELL_NET_RAT_CATM1) |
(1ULL << (int32_t) U_CELL_NET_RAT_NB1)) /* RATs */,
#endif
((1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MNO_PROFILE) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_CSCON) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_ROOT_OF_TRUST) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_DATA_COUNTERS) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_SECURITY_TLS_IANA_NUMBERING) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_SECURITY_TLS_CIPHER_LIST) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_SECURITY_TLS_SERVER_NAME_INDICATION) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTT) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTT_BINARY_PUBLISH) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTT_WILL) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTT_KEEP_ALIVE) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTT_SECURITY) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_CONTEXT_MAPPING_REQUIRED) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_AUTO_BAUDING) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_AT_PROFILES) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_SECURITY_ZTP) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_FILE_SYSTEM_TAG) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_DTR_POWER_SAVING) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_DEEP_SLEEP_URC) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_3GPP_POWER_SAVING) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_3GPP_POWER_SAVING_PAGING_WINDOW_SET) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_EDRX) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTTSN) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTTSN_SECURITY) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_CTS_CONTROL) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_SOCK_SET_LOCAL_PORT) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_FOTA) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_UART_POWER_SAVING) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_CMUX) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_SNR_REPORTED) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_AUTHENTICATION_MODE_AUTOMATIC) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_LWM2M) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_UCGED) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_HTTP) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_PPP) /* features */
),
4, /* Default CMUX channel for GNSS */
16 /* AT+CFUN reboot command */
},
{
U_CELL_MODULE_TYPE_SARA_R410M_03B, 300 /* Pwr On pull ms */, 2000 /* Pwr off pull ms */,
6 /* Boot wait */, 30 /* Min awake */, 35 /* Pwr down wait */, 5 /* Reboot wait */, 10 /* AT timeout */,
100 /* Cmd wait ms */, 2000 /* Resp max wait ms */, 4 /* radioOffCfun */, 16500 /* resetHoldMilliseconds */,
2 /* Simultaneous RATs */,
((1ULL << (int32_t) U_CELL_NET_RAT_CATM1) |
(1ULL << (int32_t) U_CELL_NET_RAT_NB1)) /* RATs */,
((1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MNO_PROFILE) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_CSCON) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_SECURITY_TLS_SERVER_NAME_INDICATION) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTT) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTT_KEEP_ALIVE) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTT_SECURITY) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_UCGED5) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_FILE_SYSTEM_TAG) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_DEEP_SLEEP_URC) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_3GPP_POWER_SAVING) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_EDRX) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_UART_POWER_SAVING) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_CMUX) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_LWM2M) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_UCGED) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_HTTP) /* features */
// PPP is supported by the module but we do not test its integration with
// ubxlib and hence it is not marked as supported
),
3, /* Default CMUX channel for GNSS */
15 /* AT+CFUN reboot command */
},
{
U_CELL_MODULE_TYPE_SARA_R422, 300 /* Pwr On pull ms */, 2000 /* Pwr off pull ms */,
5 /* Boot wait */, 30 /* Min awake */, 35 /* Pwr down wait */, 10 /* Reboot wait */, 10 /* AT timeout */,
// Note: "Cmd wait ms" is set to 100 for the other SARA-R4 series modules;
// testing has shown that 20 works for SARA-R422.
20 /* Cmd wait ms */, 3000 /* Resp max wait ms */, 4 /* radioOffCfun */, 16500 /* resetHoldMilliseconds */,
3 /* Simultaneous RATs */,
((1ULL << (int32_t) U_CELL_NET_RAT_GSM_GPRS_EGPRS) |
(1ULL << (int32_t) U_CELL_NET_RAT_CATM1) |
(1ULL << (int32_t) U_CELL_NET_RAT_NB1)) /* RATs */,
((1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MNO_PROFILE) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_CSCON) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_ROOT_OF_TRUST) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_ASYNC_SOCK_CLOSE) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_SECURITY_TLS_IANA_NUMBERING) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_SECURITY_TLS_SERVER_NAME_INDICATION) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTT) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTT_BINARY_PUBLISH) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTT_WILL) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTT_KEEP_ALIVE) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTT_SECURITY) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_CONTEXT_MAPPING_REQUIRED) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_FILE_SYSTEM_TAG) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_DEEP_SLEEP_URC) |
// SARA-R422 _does_ support 3GPP power saving, however the tests fail at the
// moment because a second attempt to enter 3GPP power saving, after waking-up
// from sleep to do something, fails, hence the support is disabled until
// we determine why that is
//(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_3GPP_POWER_SAVING) |
//(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_3GPP_POWER_SAVING_PAGING_WINDOW_SET) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_EDRX) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTTSN) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTTSN_SECURITY) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_FOTA) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_UART_POWER_SAVING) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_CMUX) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_SNR_REPORTED) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_LWM2M) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_UCGED) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_HTTP) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_PPP) /* features */
),
3, /* Default CMUX channel for GNSS */
15 /* AT+CFUN reboot command */
},
{
U_CELL_MODULE_TYPE_LARA_R6, 300 /* Pwr On pull ms */, 2000 /* Pwr off pull ms */,
10 /* Boot wait */, 30 /* Min awake */, 35 /* Pwr down wait */, 10 /* Reboot wait */, 10 /* AT timeout */,
20 /* Cmd wait ms */, 3000 /* Resp max wait ms */, 4 /* radioOffCfun */, 150 /* resetHoldMilliseconds */,
3 /* Simultaneous RATs */,
((1ULL << (int32_t) U_CELL_NET_RAT_GSM_GPRS_EGPRS) |
(1ULL << (int32_t) U_CELL_NET_RAT_LTE) |
(1ULL << (int32_t) U_CELL_NET_RAT_UTRAN)) /* RATs */,
((1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MNO_PROFILE) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_CSCON) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_ROOT_OF_TRUST) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_SECURITY_TLS_IANA_NUMBERING) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_SECURITY_TLS_SERVER_NAME_INDICATION) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTT) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTT_BINARY_PUBLISH) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTT_WILL) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTT_KEEP_ALIVE) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTT_SECURITY) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_FILE_SYSTEM_TAG) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_DTR_POWER_SAVING) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTTSN) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTTSN_SECURITY) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_SOCK_SET_LOCAL_PORT) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_FOTA) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_CMUX) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_SNR_REPORTED) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_LWM2M) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_UCGED) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_HTTP) /* features */
// PPP is supported by the module but we do not test its integration with
// ubxlib and hence it is not marked as supported
),
3, /* Default CMUX channel for GNSS */
15 /* AT+CFUN reboot command */
},
{
U_CELL_MODULE_TYPE_LENA_R8, 2000 /* Pwr On pull ms */, 3100 /* Pwr off pull ms */,
5 /* Boot wait */, 30 /* Min awake */, 35 /* Pwr down wait */, 5 /* Reboot wait */, 10 /* AT timeout */,
20 /* Cmd wait ms */, 3000 /* Resp max wait ms */, 4 /* radioOffCfun */, 50 /* resetHoldMilliseconds */,
2 /* Simultaneous RATs */,
((1ULL << (int32_t) U_CELL_NET_RAT_GSM_GPRS_EGPRS) |
(1ULL << (int32_t) U_CELL_NET_RAT_LTE)) /* RATs */,
((1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_CSCON) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_ASYNC_SOCK_CLOSE) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_SECURITY_TLS_IANA_NUMBERING) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_SECURITY_TLS_SERVER_NAME_INDICATION) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTT) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTT_BINARY_PUBLISH) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTT_WILL) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTT_KEEP_ALIVE) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTT_SECURITY) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_FILE_SYSTEM_TAG) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_DTR_POWER_SAVING) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MQTTSN) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_SOCK_SET_LOCAL_PORT) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_UART_POWER_SAVING) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_CMUX) /* features */
// LENA-R8 does, in theory, support HTTP, however the implementation is such that,
// should the HTTP server respond with a non-2xx error code, the module does not
// save the response to file (in fact it leaves any previous response file there,
// unchanged) and returns error rather than success. This makes it impossible to
// support proper HTTP operation.
// Similarly, LENA-R8 supports PPP but it fails LCP negotiation at start of day
// and hence it is not marked as supported
),
-1, /* Default CMUX channel for GNSS */
16 /* AT+CFUN reboot command */
},
// Add new module types here, before the U_CELL_MODULE_TYPE_ANY entry (since
// the uCellModuleType_t value is used as an index into this array).
{
// The module attributes set here are such that they help in identifying
// the actual module type.
U_CELL_MODULE_TYPE_ANY, 2000 /* Pwr On pull ms */, 3100 /* Pwr off pull ms */,
5 /* Boot wait */, 30 /* Min awake */, 35 /* Pwr down wait */, 5 /* Reboot wait */, 10 /* AT timeout */,
100 /* Cmd wait ms */, 3000 /* Resp max wait ms */, 4 /* radioOffCfun */, 16500 /* resetHoldMilliseconds */,
1 /* Simultaneous RATs */,
(1ULL << (int32_t) U_CELL_NET_RAT_CATM1) /* RATs */,
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_MNO_PROFILE) |
(1ULL << (int32_t) U_CELL_PRIVATE_FEATURE_DTR_POWER_SAVING) /* features */,
3, /* Default CMUX channel for GNSS */
15 /* AT+CFUN reboot command */
}
};
/** Number of items in the gUCellPrivateModuleList array, has to be
* done in this file and externed or GCC complains about asking
* for the size of a partially defined type.
*/
const size_t gUCellPrivateModuleListSize = sizeof(gUCellPrivateModuleList) /
sizeof(gUCellPrivateModuleList[0]);
/** Table to convert the RAT values used in the module to
* uCellNetRat_t, U201 version. As well as being used when reading
* the RAT configuration this is also used when the module has read
* the active RAT (AT+COPS) and hence has more nuance than the
* table going in the other direction: for instance the module
* could determine that it has EDGE coverage but EDGE is not
* a RAT that can be configured by itself.
*/
static const uCellNetRat_t gModuleRatToCellRatU201[] = {
U_CELL_NET_RAT_GSM_GPRS_EGPRS, // 0: 2G
U_CELL_NET_RAT_UNKNOWN_OR_NOT_USED, // 1: GSM compact
U_CELL_NET_RAT_UTRAN, // 2: UTRAN
U_CELL_NET_RAT_EGPRS, // 3: EDGE
U_CELL_NET_RAT_HSDPA, // 4: UTRAN with HSDPA
U_CELL_NET_RAT_HSUPA, // 5: UTRAN with HSUPA
U_CELL_NET_RAT_HSDPA_HSUPA, // 6: UTRAN with HSDPA and HSUPA
U_CELL_NET_RAT_UNKNOWN_OR_NOT_USED, // 7: LTE cat-M1
U_CELL_NET_RAT_UNKNOWN_OR_NOT_USED, // 8: LTE NB1
U_CELL_NET_RAT_UNKNOWN_OR_NOT_USED // 9: 2G again
};
/** Table to convert the RAT values used in the
* module to uCellNetRat_t, R4/R5 version.
*/
static const uCellNetRat_t gModuleRatToCellRatR4R5[] = {
U_CELL_NET_RAT_UNKNOWN_OR_NOT_USED, // 0: 2G
U_CELL_NET_RAT_UNKNOWN_OR_NOT_USED, // 1: GSM compact
U_CELL_NET_RAT_UNKNOWN_OR_NOT_USED, // 2: UTRAN
U_CELL_NET_RAT_UNKNOWN_OR_NOT_USED, // 3: EDGE
U_CELL_NET_RAT_UNKNOWN_OR_NOT_USED, // 4: UTRAN with HSDPA
U_CELL_NET_RAT_UNKNOWN_OR_NOT_USED, // 5: UTRAN with HSUPA
U_CELL_NET_RAT_UNKNOWN_OR_NOT_USED, // 6: UTRAN with HSDPA and HSUPA
U_CELL_NET_RAT_CATM1, // 7: LTE cat-M1
U_CELL_NET_RAT_NB1, // 8: LTE NB1
U_CELL_NET_RAT_GSM_GPRS_EGPRS // 9: 2G again
};
/** Table to convert the RAT values used in the
* module to uCellNetRat_t, R6 version.
*/
static const uCellNetRat_t gModuleRatToCellRatR6[] = {
U_CELL_NET_RAT_GSM_GPRS_EGPRS, // 0: 2G
U_CELL_NET_RAT_UNKNOWN_OR_NOT_USED, // 1: GSM compact
U_CELL_NET_RAT_UTRAN, // 2: UTRAN
U_CELL_NET_RAT_LTE, // 3: LTE
U_CELL_NET_RAT_HSDPA, // 4: UTRAN with HSDPA
U_CELL_NET_RAT_HSUPA, // 5: UTRAN with HSUPA
U_CELL_NET_RAT_HSDPA_HSUPA, // 6: UTRAN with HSDPA and HSUPA
U_CELL_NET_RAT_UNKNOWN_OR_NOT_USED, // 7: LTE cat-M1
U_CELL_NET_RAT_UNKNOWN_OR_NOT_USED, // 8: LTE NB1
U_CELL_NET_RAT_UNKNOWN_OR_NOT_USED // 9: 2G again
};
/** Table to convert the RAT values used in the
* module to uCellNetRat_t, R8 version.
*/
static const uCellNetRat_t gModuleRatToCellRatR8[] = {
U_CELL_NET_RAT_GSM_GPRS_EGPRS, // 0: 2G
U_CELL_NET_RAT_GSM_UMTS, // 1: GSM/UMTS dual mode
U_CELL_NET_RAT_UTRAN, // 2: UMTS single mode
U_CELL_NET_RAT_LTE, // 3: LTE single mode
U_CELL_NET_RAT_GSM_UMTS_LTE, // 4: GSM/UMTS/LTE tri-mode
U_CELL_NET_RAT_GSM_LTE, // 5: GSM/LTE dual mode
U_CELL_NET_RAT_UMTS_LTE // 6: UMTS/LTE dual mode
};
/* ----------------------------------------------------------------
* VARIABLES
* -------------------------------------------------------------- */
/* ----------------------------------------------------------------
* STATIC FUNCTIONS
* -------------------------------------------------------------- */
// Context activation result, UPSDA style, where the parameter is
// a pointer to an int32_t which will be set to the result code from
// the UPSDA action, should be 0 for success.
static void UUPSDA_urc(uAtClientHandle_t atHandle, void *pParameter)
{
int32_t result;
result = uAtClientReadInt(atHandle);
if (result == 0) {
// Tidy up by reading and throwing away the IP address
uAtClientReadString(atHandle, NULL,
U_CELL_NET_IP_ADDRESS_SIZE, false);
}
*((int32_t *) pParameter) = result;
}
// Do a wake-up from deep sleep.
static int32_t deepSleepWakeUp(uCellPrivateInstance_t *pInstance)
{
int32_t errorCode;
uCellNetRat_t rat;
errorCode = uCellPwrPrivateOn(pInstance, NULL, false);
if (errorCode == 0) {
rat = uCellPrivateGetActiveRat(pInstance);
if (U_CELL_PRIVATE_RAT_IS_EUTRAN(rat)) {
// If we're on an EUTRAN RAT, so we must have been context-activated,
// the PDP context will still be there but the internal "profile" used
// by the on-board IP stack and MQTT stack etc. of the module, needs
// to be re-attached to the PDP context on return from power saving
uCellPrivateActivateProfile(pInstance, U_CELL_NET_CONTEXT_ID,
U_CELL_NET_PROFILE_ID, 1, NULL);
}
}
return errorCode;
}
// Add an entry to the end of the linked list
// of files and count how many are in it once added.
static size_t filelListAddCount(uCellPrivateFileListContainer_t **ppFileContainer,
uCellPrivateFileListContainer_t *pAdd)
{
size_t count = 0;
uCellPrivateFileListContainer_t **ppTmp = ppFileContainer;
while (*ppTmp != NULL) {
ppTmp = &((*ppTmp)->pNext);
count++;
}
if (pAdd != NULL) {
*ppTmp = pAdd;
pAdd->pNext = NULL;
count++;
}
return count;
}
// Get an entry from the start of the linked list of files
// and remove it from the list, returning the number left
static int32_t fileListGetRemove(uCellPrivateFileListContainer_t **ppFileContainer,
char *pFile)
{
int32_t errorOrCount = (int32_t) U_ERROR_COMMON_NOT_FOUND;
uCellPrivateFileListContainer_t *pTmp = *ppFileContainer;
size_t fileNameLength;
if (pTmp != NULL) {
if (pFile != NULL) {
fileNameLength = pTmp->fileNameLength;
if (fileNameLength > U_CELL_FILE_NAME_MAX_LENGTH) {
fileNameLength = U_CELL_FILE_NAME_MAX_LENGTH;
}
memcpy(pFile, pTmp->pFileName, fileNameLength);
// Add a terminator
*(pFile + fileNameLength) = 0;
}
pTmp = (*ppFileContainer)->pNext;
uPortFree(*ppFileContainer);
*ppFileContainer = pTmp;
errorOrCount = 0;
while (pTmp != NULL) {
pTmp = pTmp->pNext;
errorOrCount++;
}
}
return errorOrCount;
}
// Clear the file list
static void fileListClear(uCellPrivateFileListContainer_t **ppFileContainer)
{
uCellPrivateFileListContainer_t *pTmp;
while (*ppFileContainer != NULL) {
pTmp = (*ppFileContainer)->pNext;
uPortFree(*ppFileContainer);
*ppFileContainer = pTmp;
}
}
// [Re]attach a PDP context to an internal module profile with an
// option on whether the AT client is locked/released or not.
int32_t privateActivateProfile(const uCellPrivateInstance_t *pInstance,
int32_t contextId, int32_t profileId, size_t tries,
bool (*pKeepGoing) (const uCellPrivateInstance_t *),
bool noAtLock)
{
int32_t errorCode = (int32_t) U_ERROR_COMMON_SUCCESS;
uAtClientHandle_t atHandle = pInstance->atHandle;
int32_t uupsdaUrcResult = -1;
if (U_CELL_PRIVATE_HAS(pInstance->pModule,
U_CELL_PRIVATE_FEATURE_CONTEXT_MAPPING_REQUIRED)) {
errorCode = (int32_t) U_CELL_ERROR_CONTEXT_ACTIVATION_FAILURE;
for (size_t x = tries; (x > 0) && (errorCode != 0) &&
((pKeepGoing == NULL) || pKeepGoing(pInstance)); x--) {
// Need to map the context to an internal modem profile
// e.g. AT+UPSD=0,100,1
if (noAtLock) {
uAtClientLockExtend(atHandle);
} else {
uAtClientLock(atHandle);
}
// The IP type used here must be the same as
// that used by AT+CGDCONT, hence set it to IP
// to be sure as some versions of SARA-R5 software
// have the default as IPV4V6.
uAtClientCommandStart(atHandle, "AT+UPSD=");
uAtClientWriteInt(atHandle, profileId);
uAtClientWriteInt(atHandle, 0);
uAtClientWriteInt(atHandle, 0);
uAtClientCommandStopReadResponse(atHandle);
uAtClientCommandStart(atHandle, "AT+UPSD=");
uAtClientWriteInt(atHandle, profileId);
uAtClientWriteInt(atHandle, 100);
uAtClientWriteInt(atHandle, contextId);
uAtClientCommandStopReadResponse(atHandle);
if (noAtLock) {
errorCode = uAtClientErrorGet(atHandle);
} else {
errorCode = uAtClientUnlock(atHandle);
}
if ((errorCode == 0) &&
(pInstance->pModule->moduleType == U_CELL_MODULE_TYPE_SARA_R5)) {
errorCode = (int32_t) U_CELL_ERROR_CONTEXT_ACTIVATION_FAILURE;
// SARA-R5 pattern: the context also has to be
// activated and we're not actually done
// until the +UUPSDA URC comes back,
if (noAtLock) {
uAtClientLockExtend(atHandle);
} else {
uAtClientLock(atHandle);
}
uAtClientCommandStart(atHandle, "AT+UPSDA=");
uAtClientWriteInt(atHandle, profileId);
uAtClientWriteInt(atHandle, 3);
uAtClientCommandStopReadResponse(atHandle);
// We wait for the URC "in-line" because this
// function may be called when waking the
// module up from sleep, at which point URCs
// handled asynchronously would be held back
// Should be pretty quick
uAtClientTimeoutSet(atHandle, 3000);
uAtClientUrcDirect(atHandle, "+UUPSDA:", UUPSDA_urc,
&uupsdaUrcResult);
if (uupsdaUrcResult == 0) {
errorCode = (int32_t) U_ERROR_COMMON_SUCCESS;
}
if (!noAtLock) {
uAtClientUnlock(atHandle);
}
}
}
}
return errorCode;
}
/* ----------------------------------------------------------------
* PUBLIC FUNCTIONS THAT ARE PRIVATE TO CELLULAR
* -------------------------------------------------------------- */
// Return true if the given buffer contains only numeric characters
// (i.e. 0 to 9)
bool uCellPrivateIsNumeric(const char *pBuffer, size_t bufferSize)
{
bool numeric = true;
for (size_t x = 0; (x < bufferSize) && numeric; x++) {
numeric = (isdigit((int32_t) * (pBuffer + x)) != 0);
}
return numeric;
}
// Find a cellular instance in the list by instance handle.
uCellPrivateInstance_t *pUCellPrivateGetInstance(uDeviceHandle_t cellHandle)
{
uCellPrivateInstance_t *pInstance = gpUCellPrivateInstanceList;
while ((pInstance != NULL) && (pInstance->cellHandle != cellHandle)) {
pInstance = pInstance->pNext;
}
return pInstance;
}
// Convert RSRP in 3GPP TS 36.133 format to dBm.
int32_t uCellPrivateRsrpToDbm(int32_t rsrp)
{
int32_t rsrpDbm = 0;
if ((rsrp >= 0) && (rsrp <= 97)) {
rsrpDbm = rsrp - (97 + 44);
if (rsrpDbm < -141) {
rsrpDbm = -141;
}
}
return rsrpDbm;
}
// Convert RSRQ in 3GPP TS 36.133 format to dB.
int32_t uCellPrivateRsrqToDb(int32_t rsrq)
{
int32_t rsrqDb = 0x7FFFFFFF;
if ((rsrq >= -30) && (rsrq <= 46)) {
rsrqDb = (rsrq - 39) / 2;
if (rsrqDb < -34) {
rsrqDb = -34;
}
}
return rsrqDb;
}
// Set the radio parameters back to defaults.
void uCellPrivateClearRadioParameters(uCellPrivateRadioParameters_t *pParameters,
bool leaveCellIdLogicalAlone)
{
pParameters->rssiDbm = 0;
pParameters->rsrpDbm = 0;
pParameters->rsrqDb = 0x7FFFFFFF;
pParameters->cellIdPhysical = -1;
if (!leaveCellIdLogicalAlone) {
pParameters->cellIdLogical = -1;
}
pParameters->earfcn = -1;
pParameters->snrDb = 0x7FFFFFFF;
}
// Clear the dynamic parameters of an instance,
// so the network status, the active RAT and
// the radio parameters.
void uCellPrivateClearDynamicParameters(uCellPrivateInstance_t *pInstance)
{
for (size_t x = 0;
x < sizeof(pInstance->networkStatus) / sizeof(pInstance->networkStatus[0]);
x++) {
pInstance->networkStatus[x] = U_CELL_NET_STATUS_UNKNOWN;
}
for (size_t x = 0;
x < sizeof(pInstance->rat) / sizeof(pInstance->rat[0]);
x++) {
pInstance->rat[x] = U_CELL_NET_RAT_UNKNOWN_OR_NOT_USED;
}
uCellPrivateClearRadioParameters(&(pInstance->radioParameters), false);
}
// Get the current CFUN mode.
int32_t uCellPrivateCFunGet(const uCellPrivateInstance_t *pInstance)
{
int32_t errorCodeOrMode;
int32_t x;
uAtClientHandle_t atHandle = pInstance->atHandle;
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+CFUN?");
uAtClientCommandStop(atHandle);
uAtClientResponseStart(atHandle, "+CFUN:");
x = uAtClientReadInt(atHandle);
uAtClientResponseStop(atHandle);
errorCodeOrMode = uAtClientUnlock(atHandle);
if ((errorCodeOrMode == 0) && (x >= 0)) {
errorCodeOrMode = x;
}
return errorCodeOrMode;
}
// Ensure that a module is powered up.
int32_t uCellPrivateCFunOne(uCellPrivateInstance_t *pInstance)
{
int32_t errorCodeOrMode;
uAtClientHandle_t atHandle = pInstance->atHandle;
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+CFUN?");
uAtClientCommandStop(atHandle);
uAtClientResponseStart(atHandle, "+CFUN:");
errorCodeOrMode = uAtClientReadInt(atHandle);
uAtClientResponseStop(atHandle);
uAtClientUnlock(atHandle);
// Set powered-up mode if it wasn't already
if (errorCodeOrMode != 1) {
// Wait for flip time to expire
while (uPortGetTickTimeMs() - pInstance->lastCfunFlipTimeMs <
(U_CELL_PRIVATE_AT_CFUN_FLIP_DELAY_SECONDS * 1000)) {
uPortTaskBlock(1000);
}
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+CFUN=1");
uAtClientCommandStopReadResponse(atHandle);
if (uAtClientUnlock(atHandle) == 0) {
pInstance->lastCfunFlipTimeMs = uPortGetTickTimeMs();
// And don't do anything for a second,
// as the module might not be quite ready yet
uPortTaskBlock(1000);
}
}
return errorCodeOrMode;
}
// Do the opposite of uCellPrivateCFunOne(), put the mode back.
void uCellPrivateCFunMode(uCellPrivateInstance_t *pInstance,
int32_t mode)
{
uAtClientHandle_t atHandle = pInstance->atHandle;
// Wait for flip time to expire
while (uPortGetTickTimeMs() - pInstance->lastCfunFlipTimeMs <
(U_CELL_PRIVATE_AT_CFUN_FLIP_DELAY_SECONDS * 1000)) {
uPortTaskBlock(1000);
}
uAtClientLock(atHandle);
if (mode != 1) {
// If we're doing anything other than powering up,
// i.e. AT+CFUN=0 or AT+CFUN=4, this can take
// longer than your average response time
uAtClientTimeoutSet(atHandle,
U_CELL_PRIVATE_AT_CFUN_OFF_RESPONSE_TIME_SECONDS * 1000);
}
uAtClientCommandStart(atHandle, "AT+CFUN=");
uAtClientWriteInt(atHandle, mode);
uAtClientCommandStopReadResponse(atHandle);
if (uAtClientUnlock(atHandle) == 0) {
pInstance->lastCfunFlipTimeMs = uPortGetTickTimeMs();
}
}
// Get the IMSI of the SIM.
int32_t uCellPrivateGetImsi(const uCellPrivateInstance_t *pInstance,
char *pImsi)
{
int32_t errorCode = (int32_t) U_CELL_ERROR_AT;
uAtClientHandle_t atHandle = pInstance->atHandle;
int32_t bytesRead;
// Try this ten times: unfortunately
// the module can spit out a URC just when
// we're expecting the IMSI and, since there
// is no prefix on the response, we have
// no way of telling the difference. Hence
// check the length and that length being
// made up entirely of numerals
for (size_t x = 10; (x > 0) && (errorCode != 0); x--) {
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+CIMI");
uAtClientCommandStop(atHandle);
uAtClientResponseStart(atHandle, NULL);
bytesRead = uAtClientReadBytes(atHandle, pImsi,
15, false);
uAtClientResponseStop(atHandle);
if ((uAtClientUnlock(atHandle) == 0) &&
(bytesRead == 15) &&
uCellPrivateIsNumeric(pImsi, 15)) {
errorCode = (int32_t) U_ERROR_COMMON_SUCCESS;
} else {
uPortTaskBlock(1000);
}
}
return errorCode;
}
// Get the IMEI of the cellular module.
int32_t uCellPrivateGetImei(const uCellPrivateInstance_t *pInstance,
char *pImei)
{
int32_t errorCode = (int32_t) U_CELL_ERROR_AT;
uAtClientHandle_t atHandle = pInstance->atHandle;
int32_t bytesRead;
// Try this ten times: unfortunately
// the module can spit out a URC just when
// we're expecting the IMEI and, since there
// is no prefix on the response, we have
// no way of telling the difference. Hence
// check the length and that length being
// made up entirely of numerals
for (size_t x = 10; (x > 0) && (errorCode != 0); x--) {
uAtClientLock(atHandle);
uAtClientCommandStart(atHandle, "AT+CGSN");
uAtClientCommandStop(atHandle);
uAtClientResponseStart(atHandle, NULL);
bytesRead = uAtClientReadBytes(atHandle, pImei,
15, false);
uAtClientResponseStop(atHandle);
if ((uAtClientUnlock(atHandle) == 0) &&
(bytesRead == 15) &&
uCellPrivateIsNumeric(pImei, 15)) {
errorCode = (int32_t) U_ERROR_COMMON_SUCCESS;
}
}
return errorCode;
}
// Get whether the given instance is registered with the network.
// Needs to be in the packet switched domain, circuit switched is
// no use for this API.
bool uCellPrivateIsRegistered(const uCellPrivateInstance_t *pInstance)
{
return U_CELL_NET_STATUS_MEANS_REGISTERED(
pInstance->networkStatus[U_CELL_PRIVATE_NET_REG_TYPE_CGREG]) ||
U_CELL_NET_STATUS_MEANS_REGISTERED(pInstance->networkStatus[U_CELL_PRIVATE_NET_REG_TYPE_CEREG]);
}
// Convert module RAT to our RAT.
uCellNetRat_t uCellPrivateModuleRatToCellRat(uCellModuleType_t moduleType,
int32_t moduleRat)
{
uCellNetRat_t cellRat = U_CELL_NET_RAT_UNKNOWN_OR_NOT_USED;
if (moduleRat >= 0) {
switch (moduleType) {
case U_CELL_MODULE_TYPE_SARA_U201:
if (moduleRat < (int32_t) (sizeof(gModuleRatToCellRatU201) / sizeof(gModuleRatToCellRatU201[0]))) {
cellRat = gModuleRatToCellRatU201[moduleRat];
}
break;
case U_CELL_MODULE_TYPE_LARA_R6:
if (moduleRat < (int32_t) (sizeof(gModuleRatToCellRatR6) / sizeof(gModuleRatToCellRatR6[0]))) {
cellRat = gModuleRatToCellRatR6[moduleRat];
}
break;
case U_CELL_MODULE_TYPE_LENA_R8:
if (moduleRat < (int32_t) (sizeof(gModuleRatToCellRatR8) / sizeof(gModuleRatToCellRatR8[0]))) {
cellRat = gModuleRatToCellRatR8[moduleRat];
}
break;
default:
if (moduleRat < (int32_t) (sizeof(gModuleRatToCellRatR4R5) / sizeof(gModuleRatToCellRatR4R5[0]))) {
cellRat = gModuleRatToCellRatR4R5[moduleRat];
}
break;
}
}
return cellRat;
}
// Get the active RAT.
uCellNetRat_t uCellPrivateGetActiveRat(const uCellPrivateInstance_t *pInstance)
{
uCellNetRat_t rat = pInstance->rat[U_CELL_PRIVATE_NET_REG_TYPE_CREG];
// Return the RAT for the registration type we have, prioritising the
// packet-switched domain, LTE first
if (U_CELL_NET_STATUS_MEANS_REGISTERED(
pInstance->networkStatus[U_CELL_PRIVATE_NET_REG_TYPE_CEREG])) {
rat = pInstance->rat[U_CELL_PRIVATE_NET_REG_TYPE_CEREG];
} else if (U_CELL_NET_STATUS_MEANS_REGISTERED(
pInstance->networkStatus[U_CELL_PRIVATE_NET_REG_TYPE_CGREG])) {
rat = pInstance->rat[U_CELL_PRIVATE_NET_REG_TYPE_CGREG];
}
return rat;
}
// Get the operator name.
int32_t uCellPrivateGetOperatorStr(const uCellPrivateInstance_t *pInstance,
char *pStr, size_t size)
{