/
lte_lc.c
1926 lines (1608 loc) · 49.4 KB
/
lte_lc.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 (c) 2018 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
#include <zephyr/kernel.h>
#include <zephyr/types.h>
#include <errno.h>
#include <zephyr/net/socket.h>
#include <string.h>
#include <stdio.h>
#include <zephyr/device.h>
#include <nrf_errno.h>
#include <nrf_modem_at.h>
#include <modem/lte_lc.h>
#include <modem/lte_lc_trace.h>
#include <modem/at_cmd_parser.h>
#include <modem/at_params.h>
#include <modem/at_monitor.h>
#include <modem/nrf_modem_lib.h>
#include <zephyr/logging/log.h>
#include "lte_lc_helpers.h"
LOG_MODULE_REGISTER(lte_lc, CONFIG_LTE_LINK_CONTROL_LOG_LEVEL);
/* Internal system mode value used when CONFIG_LTE_NETWORK_MODE_DEFAULT is enabled. */
#define LTE_LC_SYSTEM_MODE_DEFAULT 0xff
#define SYS_MODE_PREFERRED \
(IS_ENABLED(CONFIG_LTE_NETWORK_MODE_LTE_M) ? \
LTE_LC_SYSTEM_MODE_LTEM : \
IS_ENABLED(CONFIG_LTE_NETWORK_MODE_NBIOT) ? \
LTE_LC_SYSTEM_MODE_NBIOT : \
IS_ENABLED(CONFIG_LTE_NETWORK_MODE_LTE_M_GPS) ? \
LTE_LC_SYSTEM_MODE_LTEM_GPS : \
IS_ENABLED(CONFIG_LTE_NETWORK_MODE_NBIOT_GPS) ? \
LTE_LC_SYSTEM_MODE_NBIOT_GPS : \
IS_ENABLED(CONFIG_LTE_NETWORK_MODE_LTE_M_NBIOT) ? \
LTE_LC_SYSTEM_MODE_LTEM_NBIOT : \
IS_ENABLED(CONFIG_LTE_NETWORK_MODE_LTE_M_NBIOT_GPS) ? \
LTE_LC_SYSTEM_MODE_LTEM_NBIOT_GPS : \
LTE_LC_SYSTEM_MODE_DEFAULT)
/* Length for eDRX and PTW values */
#define LTE_LC_EDRX_VALUE_LEN 5
/* Internal enums */
enum feaconf_oper {
FEACONF_OPER_WRITE = 0,
FEACONF_OPER_READ = 1,
FEACONF_OPER_LIST = 2
};
enum feaconf_feat {
FEACONF_FEAT_PROPRIETARY_PSM = 0
};
/* Static variables */
/* Previously received LTE mode as indicated by the modem */
static enum lte_lc_lte_mode prev_lte_mode = LTE_LC_LTE_MODE_NONE;
/* Requested eDRX state (enabled/disabled) */
static bool requested_edrx_enable;
/* Requested eDRX setting */
static char requested_edrx_value_ltem[LTE_LC_EDRX_VALUE_LEN] = CONFIG_LTE_EDRX_REQ_VALUE_LTE_M;
static char requested_edrx_value_nbiot[LTE_LC_EDRX_VALUE_LEN] = CONFIG_LTE_EDRX_REQ_VALUE_NBIOT;
/* Requested PTW setting */
static char requested_ptw_value_ltem[LTE_LC_EDRX_VALUE_LEN] = CONFIG_LTE_PTW_VALUE_LTE_M;
static char requested_ptw_value_nbiot[LTE_LC_EDRX_VALUE_LEN] = CONFIG_LTE_PTW_VALUE_NBIOT;
/* Currently used eDRX setting as indicated by the modem */
static char edrx_value_ltem[LTE_LC_EDRX_VALUE_LEN];
static char edrx_value_nbiot[LTE_LC_EDRX_VALUE_LEN];
/* Currently used PTW setting as indicated by the modem */
static char ptw_value_ltem[LTE_LC_EDRX_VALUE_LEN];
static char ptw_value_nbiot[LTE_LC_EDRX_VALUE_LEN];
/* Requested PSM RAT setting */
static char requested_psm_param_rat[9] = CONFIG_LTE_PSM_REQ_RAT;
/* Requested PSM RPTAU setting */
static char requested_psm_param_rptau[9] = CONFIG_LTE_PSM_REQ_RPTAU;
/* Request PSM to be disabled and timers set to default values */
static const char psm_disable[] = "AT+CPSMS=";
/* Enable CSCON (RRC mode) notifications */
static const char cscon[] = "AT+CSCON=1";
/* Requested NCELLMEAS params */
static struct lte_lc_ncellmeas_params ncellmeas_params;
/* Sempahore value 1 means ncellmeas is not ongoing, and 0 means it's ongoing. */
K_SEM_DEFINE(ncellmeas_idle_sem, 1, 1);
/* Network attach semaphore */
static K_SEM_DEFINE(link, 0, 1);
/* The preferred system mode to use when connecting to LTE network. Can be changed by calling
* lte_lc_system_mode_set().
*
* extern in lte_lc_modem_hooks.c
*/
enum lte_lc_system_mode lte_lc_sys_mode = SYS_MODE_PREFERRED;
/* System mode preference to set when configuring system mode. Can be changed by calling
* lte_lc_system_mode_set().
*
* extern in lte_lc_modem_hooks.c
*/
enum lte_lc_system_mode_preference lte_lc_sys_mode_pref = CONFIG_LTE_MODE_PREFERENCE_VALUE;
/* Parameters to be passed using AT%XSYSTEMMMODE=<params>,<preference> */
static const char *const system_mode_params[] = {
[LTE_LC_SYSTEM_MODE_LTEM] = "1,0,0",
[LTE_LC_SYSTEM_MODE_NBIOT] = "0,1,0",
[LTE_LC_SYSTEM_MODE_GPS] = "0,0,1",
[LTE_LC_SYSTEM_MODE_LTEM_GPS] = "1,0,1",
[LTE_LC_SYSTEM_MODE_NBIOT_GPS] = "0,1,1",
[LTE_LC_SYSTEM_MODE_LTEM_NBIOT] = "1,1,0",
[LTE_LC_SYSTEM_MODE_LTEM_NBIOT_GPS] = "1,1,1",
};
/* LTE preference to be passed using AT%XSYSTEMMMODE=<params>,<preference> */
static const char system_mode_preference[] = {
/* No LTE preference, automatically selected by the modem. */
[LTE_LC_SYSTEM_MODE_PREFER_AUTO] = '0',
/* LTE-M has highest priority. */
[LTE_LC_SYSTEM_MODE_PREFER_LTEM] = '1',
/* NB-IoT has highest priority. */
[LTE_LC_SYSTEM_MODE_PREFER_NBIOT] = '2',
/* Equal priority, but prefer LTE-M. */
[LTE_LC_SYSTEM_MODE_PREFER_LTEM_PLMN_PRIO] = '3',
/* Equal priority, but prefer NB-IoT. */
[LTE_LC_SYSTEM_MODE_PREFER_NBIOT_PLMN_PRIO] = '4',
};
static void lte_lc_psm_get_work_fn(struct k_work *work_item);
K_WORK_DEFINE(lte_lc_psm_get_work, lte_lc_psm_get_work_fn);
static void lte_lc_edrx_ptw_send_work_fn(struct k_work *work_item);
K_WORK_DEFINE(lte_lc_edrx_ptw_send_work, lte_lc_edrx_ptw_send_work_fn);
static void lte_lc_edrx_req_work_fn(struct k_work *work_item);
K_WORK_DEFINE(lte_lc_edrx_req_work, lte_lc_edrx_req_work_fn);
K_THREAD_STACK_DEFINE(lte_lc_work_q_stack, 1024);
static struct k_work_q lte_lc_work_q;
static bool is_cellid_valid(uint32_t cellid)
{
if (cellid == LTE_LC_CELL_EUTRAN_ID_INVALID) {
return false;
}
return true;
}
static void lte_lc_evt_psm_update_send(struct lte_lc_psm_cfg *psm_cfg)
{
static struct lte_lc_psm_cfg prev_psm_cfg;
struct lte_lc_evt evt = {0};
/* PSM configuration update event */
if ((psm_cfg->tau != prev_psm_cfg.tau) ||
(psm_cfg->active_time != prev_psm_cfg.active_time)) {
evt.type = LTE_LC_EVT_PSM_UPDATE;
memcpy(&prev_psm_cfg, psm_cfg, sizeof(struct lte_lc_psm_cfg));
memcpy(&evt.psm_cfg, psm_cfg, sizeof(struct lte_lc_psm_cfg));
event_handler_list_dispatch(&evt);
}
}
static void lte_lc_psm_get_work_fn(struct k_work *work_item)
{
int err;
struct lte_lc_psm_cfg psm_cfg = {
.active_time = -1,
.tau = -1
};
err = lte_lc_psm_get(&psm_cfg.tau, &psm_cfg.active_time);
if (err) {
if (err != -EBADMSG) {
LOG_ERR("Failed to get PSM information");
}
return;
}
lte_lc_evt_psm_update_send(&psm_cfg);
}
static void lte_lc_edrx_current_values_clear(void)
{
memset(edrx_value_ltem, 0, sizeof(edrx_value_ltem));
memset(ptw_value_ltem, 0, sizeof(ptw_value_ltem));
memset(edrx_value_nbiot, 0, sizeof(edrx_value_nbiot));
memset(ptw_value_nbiot, 0, sizeof(ptw_value_nbiot));
}
static void lte_lc_edrx_values_store(
enum lte_lc_lte_mode mode,
char *edrx_value,
char *ptw_value)
{
switch (mode) {
case LTE_LC_LTE_MODE_LTEM:
strcpy(edrx_value_ltem, edrx_value);
strcpy(ptw_value_ltem, ptw_value);
break;
case LTE_LC_LTE_MODE_NBIOT:
strcpy(edrx_value_nbiot, edrx_value);
strcpy(ptw_value_nbiot, ptw_value);
break;
default:
lte_lc_edrx_current_values_clear();
break;
}
}
static void lte_lc_edrx_ptw_send_work_fn(struct k_work *work_item)
{
int err;
int actt[] = {AT_CEDRXS_ACTT_WB, AT_CEDRXS_ACTT_NB};
/* Apply the configurations for both LTE-M and NB-IoT. */
for (size_t i = 0; i < ARRAY_SIZE(actt); i++) {
char *requested_ptw_value = (actt[i] == AT_CEDRXS_ACTT_WB) ?
requested_ptw_value_ltem : requested_ptw_value_nbiot;
char *ptw_value = (actt[i] == AT_CEDRXS_ACTT_WB) ?
ptw_value_ltem : ptw_value_nbiot;
if (strlen(requested_ptw_value) == 4 &&
strcmp(ptw_value, requested_ptw_value) != 0) {
err = nrf_modem_at_printf(
"AT%%XPTW=%d,\"%s\"", actt[i], requested_ptw_value);
if (err) {
LOG_ERR("Failed to request PTW, reported error: %d", err);
}
}
}
}
AT_MONITOR(ltelc_atmon_cereg, "+CEREG", at_handler_cereg);
AT_MONITOR(ltelc_atmon_cscon, "+CSCON", at_handler_cscon);
AT_MONITOR(ltelc_atmon_cedrxp, "+CEDRXP", at_handler_cedrxp);
AT_MONITOR(ltelc_atmon_xt3412, "%XT3412", at_handler_xt3412);
AT_MONITOR(ltelc_atmon_ncellmeas, "%NCELLMEAS", at_handler_ncellmeas);
AT_MONITOR(ltelc_atmon_xmodemsleep, "%XMODEMSLEEP", at_handler_xmodemsleep);
AT_MONITOR(ltelc_atmon_mdmev, "%MDMEV", at_handler_mdmev);
static void at_handler_cereg(const char *response)
{
int err;
struct lte_lc_evt evt = {0};
__ASSERT_NO_MSG(response != NULL);
static enum lte_lc_nw_reg_status prev_reg_status = LTE_LC_NW_REG_NOT_REGISTERED;
static struct lte_lc_cell prev_cell;
enum lte_lc_nw_reg_status reg_status = 0;
struct lte_lc_cell cell = {0};
enum lte_lc_lte_mode lte_mode;
struct lte_lc_psm_cfg psm_cfg = {
.active_time = -1,
.tau = -1
};
LOG_DBG("+CEREG notification: %.*s", strlen(response) - strlen("\r\n"), response);
err = parse_cereg(response, true, ®_status, &cell, <e_mode, &psm_cfg);
if (err) {
LOG_ERR("Failed to parse notification (error %d): %s",
err, response);
return;
}
if ((reg_status == LTE_LC_NW_REG_REGISTERED_HOME) ||
(reg_status == LTE_LC_NW_REG_REGISTERED_ROAMING)) {
/* Set the network registration status to UNKNOWN if the cell ID is parsed
* to UINT32_MAX (FFFFFFFF) when the registration status is either home or
* roaming.
*/
if (!is_cellid_valid(cell.id)) {
reg_status = LTE_LC_NW_REG_UNKNOWN;
} else {
k_sem_give(&link);
}
}
switch (reg_status) {
case LTE_LC_NW_REG_NOT_REGISTERED:
LTE_LC_TRACE(LTE_LC_TRACE_NW_REG_NOT_REGISTERED);
break;
case LTE_LC_NW_REG_REGISTERED_HOME:
LTE_LC_TRACE(LTE_LC_TRACE_NW_REG_REGISTERED_HOME);
break;
case LTE_LC_NW_REG_SEARCHING:
LTE_LC_TRACE(LTE_LC_TRACE_NW_REG_SEARCHING);
break;
case LTE_LC_NW_REG_REGISTRATION_DENIED:
LTE_LC_TRACE(LTE_LC_TRACE_NW_REG_REGISTRATION_DENIED);
break;
case LTE_LC_NW_REG_UNKNOWN:
LTE_LC_TRACE(LTE_LC_TRACE_NW_REG_UNKNOWN);
break;
case LTE_LC_NW_REG_REGISTERED_ROAMING:
LTE_LC_TRACE(LTE_LC_TRACE_NW_REG_REGISTERED_ROAMING);
break;
case LTE_LC_NW_REG_UICC_FAIL:
LTE_LC_TRACE(LTE_LC_TRACE_NW_REG_UICC_FAIL);
break;
}
if (event_handler_list_is_empty()) {
return;
}
/* Network registration status event */
if (reg_status != prev_reg_status) {
prev_reg_status = reg_status;
evt.type = LTE_LC_EVT_NW_REG_STATUS;
evt.nw_reg_status = reg_status;
event_handler_list_dispatch(&evt);
}
/* Cell update event */
if ((cell.id != prev_cell.id) || (cell.tac != prev_cell.tac)) {
evt.type = LTE_LC_EVT_CELL_UPDATE;
memcpy(&prev_cell, &cell, sizeof(struct lte_lc_cell));
memcpy(&evt.cell, &cell, sizeof(struct lte_lc_cell));
event_handler_list_dispatch(&evt);
}
if (lte_mode != prev_lte_mode) {
prev_lte_mode = lte_mode;
evt.type = LTE_LC_EVT_LTE_MODE_UPDATE;
evt.lte_mode = lte_mode;
switch (lte_mode) {
case LTE_LC_LTE_MODE_LTEM:
LTE_LC_TRACE(LTE_LC_TRACE_LTE_MODE_UPDATE_LTEM);
break;
case LTE_LC_LTE_MODE_NBIOT:
LTE_LC_TRACE(LTE_LC_TRACE_LTE_MODE_UPDATE_NBIOT);
break;
case LTE_LC_LTE_MODE_NONE:
LTE_LC_TRACE(LTE_LC_TRACE_LTE_MODE_UPDATE_NONE);
break;
}
event_handler_list_dispatch(&evt);
}
if ((reg_status != LTE_LC_NW_REG_REGISTERED_HOME) &&
(reg_status != LTE_LC_NW_REG_REGISTERED_ROAMING)) {
return;
}
if (psm_cfg.tau == -1) {
/* Need to get legacy T3412 value as TAU using AT%XMONITOR.
*
* As we are in an AT notification handler that is run from the system work queue,
* we shall not send AT commands here because another AT command might be ongoing,
* and the second command will be blocked until the first one completes.
* Further AT notifications from the modem will gradually exhaust AT monitor
* library's heap, and eventually it will run out causing an assert or
* AT notifications not being dispatched.
*/
k_work_submit_to_queue(<e_lc_work_q, <e_lc_psm_get_work);
return;
}
lte_lc_evt_psm_update_send(&psm_cfg);
}
static void at_handler_cscon(const char *response)
{
int err;
struct lte_lc_evt evt = {0};
__ASSERT_NO_MSG(response != NULL);
LOG_DBG("+CSCON notification");
err = parse_rrc_mode(response, &evt.rrc_mode, AT_CSCON_RRC_MODE_INDEX);
if (err) {
LOG_ERR("Can't parse signalling mode, error: %d", err);
return;
}
if (evt.rrc_mode == LTE_LC_RRC_MODE_IDLE) {
LTE_LC_TRACE(LTE_LC_TRACE_RRC_IDLE);
} else if (evt.rrc_mode == LTE_LC_RRC_MODE_CONNECTED) {
LTE_LC_TRACE(LTE_LC_TRACE_RRC_CONNECTED);
}
evt.type = LTE_LC_EVT_RRC_UPDATE;
event_handler_list_dispatch(&evt);
}
static void at_handler_cedrxp(const char *response)
{
int err;
struct lte_lc_evt evt = {0};
char edrx_value[LTE_LC_EDRX_VALUE_LEN] = {0};
char ptw_value[LTE_LC_EDRX_VALUE_LEN] = {0};
__ASSERT_NO_MSG(response != NULL);
LOG_DBG("+CEDRXP notification");
err = parse_edrx(response, &evt.edrx_cfg, edrx_value, ptw_value);
if (err) {
LOG_ERR("Can't parse eDRX, error: %d", err);
return;
}
/* PTW must be requested after eDRX is enabled */
lte_lc_edrx_values_store(evt.edrx_cfg.mode, edrx_value, ptw_value);
/* Send PTW setting if eDRX is enabled, i.e., we have network mode */
if (evt.edrx_cfg.mode != LTE_LC_LTE_MODE_NONE) {
k_work_submit_to_queue(<e_lc_work_q, <e_lc_edrx_ptw_send_work);
}
evt.type = LTE_LC_EVT_EDRX_UPDATE;
event_handler_list_dispatch(&evt);
}
static void at_handler_xt3412(const char *response)
{
int err;
struct lte_lc_evt evt = {0};
__ASSERT_NO_MSG(response != NULL);
LOG_DBG("%%XT3412 notification");
err = parse_xt3412(response, &evt.time);
if (err) {
LOG_ERR("Can't parse TAU pre-warning notification, error: %d", err);
return;
}
if (evt.time != CONFIG_LTE_LC_TAU_PRE_WARNING_TIME_MS) {
/* Only propagate TAU pre-warning notifications when the received time
* parameter is the duration of the set pre-warning time.
*/
return;
}
evt.type = LTE_LC_EVT_TAU_PRE_WARNING;
event_handler_list_dispatch(&evt);
}
static void at_handler_ncellmeas_gci(const char *response)
{
int err;
struct lte_lc_evt evt = {0};
const char *resp = response;
__ASSERT_NO_MSG(response != NULL);
int max_cell_count = ncellmeas_params.gci_count;
struct lte_lc_cell *cells = NULL;
LOG_DBG("%%NCELLMEAS GCI notification parsing starts");
if (max_cell_count != 0) {
cells = k_calloc(max_cell_count, sizeof(struct lte_lc_cell));
if (cells == NULL) {
LOG_ERR("Failed to allocate memory for the GCI cells");
return;
}
}
evt.cells_info.gci_cells = cells;
err = parse_ncellmeas_gci(&ncellmeas_params, resp, &evt.cells_info);
LOG_DBG("parse_ncellmeas_gci returned %d", err);
switch (err) {
case -E2BIG:
LOG_WRN("Not all neighbor cells could be parsed. "
"More cells than the configured max count of %d were found",
CONFIG_LTE_NEIGHBOR_CELLS_MAX);
/* Fall through */
case 0: /* Fall through */
case 1:
LOG_DBG("Neighbor cell count: %d, GCI cells count: %d",
evt.cells_info.ncells_count,
evt.cells_info.gci_cells_count);
evt.type = LTE_LC_EVT_NEIGHBOR_CELL_MEAS;
event_handler_list_dispatch(&evt);
break;
default:
LOG_ERR("Parsing of neighbor cells failed, err: %d", err);
break;
}
k_free(cells);
k_free(evt.cells_info.neighbor_cells);
}
static void at_handler_ncellmeas(const char *response)
{
int err;
struct lte_lc_evt evt = {0};
__ASSERT_NO_MSG(response != NULL);
if (event_handler_list_is_empty()) {
/* No need to parse the response if there is no handler
* to receive the parsed data.
*/
goto exit;
}
if (ncellmeas_params.search_type > LTE_LC_NEIGHBOR_SEARCH_TYPE_EXTENDED_COMPLETE) {
at_handler_ncellmeas_gci(response);
goto exit;
}
int ncell_count = neighborcell_count_get(response);
struct lte_lc_ncell *neighbor_cells = NULL;
LOG_DBG("%%NCELLMEAS notification: neighbor cell count: %d", ncell_count);
if (ncell_count != 0) {
neighbor_cells = k_calloc(ncell_count, sizeof(struct lte_lc_ncell));
if (neighbor_cells == NULL) {
LOG_ERR("Failed to allocate memory for neighbor cells");
goto exit;
}
}
evt.cells_info.neighbor_cells = neighbor_cells;
err = parse_ncellmeas(response, &evt.cells_info);
switch (err) {
case -E2BIG:
LOG_WRN("Not all neighbor cells could be parsed");
LOG_WRN("More cells than the configured max count of %d were found",
CONFIG_LTE_NEIGHBOR_CELLS_MAX);
/* Fall through */
case 0: /* Fall through */
case 1:
evt.type = LTE_LC_EVT_NEIGHBOR_CELL_MEAS;
event_handler_list_dispatch(&evt);
break;
default:
LOG_ERR("Parsing of neighbor cells failed, err: %d", err);
break;
}
if (neighbor_cells) {
k_free(neighbor_cells);
}
exit:
k_sem_give(&ncellmeas_idle_sem);
}
static void at_handler_xmodemsleep(const char *response)
{
int err;
struct lte_lc_evt evt = {0};
__ASSERT_NO_MSG(response != NULL);
LOG_DBG("%%XMODEMSLEEP notification");
err = parse_xmodemsleep(response, &evt.modem_sleep);
if (err) {
LOG_ERR("Can't parse modem sleep pre-warning notification, error: %d", err);
return;
}
/* Link controller only supports PSM, RF inactivity, limited service, flight mode
* and proprietary PSM modem sleep types.
*/
if ((evt.modem_sleep.type != LTE_LC_MODEM_SLEEP_PSM) &&
(evt.modem_sleep.type != LTE_LC_MODEM_SLEEP_RF_INACTIVITY) &&
(evt.modem_sleep.type != LTE_LC_MODEM_SLEEP_LIMITED_SERVICE) &&
(evt.modem_sleep.type != LTE_LC_MODEM_SLEEP_FLIGHT_MODE) &&
(evt.modem_sleep.type != LTE_LC_MODEM_SLEEP_PROPRIETARY_PSM)) {
return;
}
/* Propagate the appropriate event depending on the parsed time parameter. */
if (evt.modem_sleep.time == CONFIG_LTE_LC_MODEM_SLEEP_PRE_WARNING_TIME_MS) {
evt.type = LTE_LC_EVT_MODEM_SLEEP_EXIT_PRE_WARNING;
} else if (evt.modem_sleep.time == 0) {
LTE_LC_TRACE(LTE_LC_TRACE_MODEM_SLEEP_EXIT);
evt.type = LTE_LC_EVT_MODEM_SLEEP_EXIT;
} else {
LTE_LC_TRACE(LTE_LC_TRACE_MODEM_SLEEP_ENTER);
evt.type = LTE_LC_EVT_MODEM_SLEEP_ENTER;
}
event_handler_list_dispatch(&evt);
}
static void at_handler_mdmev(const char *response)
{
int err;
struct lte_lc_evt evt = {0};
__ASSERT_NO_MSG(response != NULL);
LOG_DBG("%%MDMEV notification");
err = parse_mdmev(response, &evt.modem_evt);
if (err) {
LOG_ERR("Can't parse modem event notification, error: %d", err);
return;
}
evt.type = LTE_LC_EVT_MODEM_EVENT;
event_handler_list_dispatch(&evt);
}
static int enable_notifications(void)
{
int err;
/* +CEREG notifications, level 5 */
err = nrf_modem_at_printf(AT_CEREG_5);
if (err) {
LOG_ERR("Failed to subscribe to CEREG notifications, error: %d", err);
return -EFAULT;
}
if (IS_ENABLED(CONFIG_LTE_LC_TAU_PRE_WARNING_NOTIFICATIONS)) {
err = nrf_modem_at_printf(AT_XT3412_SUB,
CONFIG_LTE_LC_TAU_PRE_WARNING_TIME_MS,
CONFIG_LTE_LC_TAU_PRE_WARNING_THRESHOLD_MS);
if (err) {
LOG_WRN("Enabling TAU pre-warning notifications failed, error: %d", err);
LOG_WRN("TAU pre-warning notifications require nRF9160 modem >= v1.3.0");
}
}
if (IS_ENABLED(CONFIG_LTE_LC_MODEM_SLEEP_NOTIFICATIONS)) {
/* %XMODEMSLEEP notifications subscribe */
err = nrf_modem_at_printf(AT_XMODEMSLEEP_SUB,
CONFIG_LTE_LC_MODEM_SLEEP_PRE_WARNING_TIME_MS,
CONFIG_LTE_LC_MODEM_SLEEP_NOTIFICATIONS_THRESHOLD_MS);
if (err) {
LOG_WRN("Enabling modem sleep notifications failed, error: %d", err);
LOG_WRN("Modem sleep notifications require nRF9160 modem >= v1.3.0");
}
}
/* +CSCON notifications */
err = nrf_modem_at_printf(cscon);
if (err) {
LOG_WRN("Failed to enable RRC notifications (+CSCON), error %d", err);
return -EFAULT;
}
return 0;
}
static int fallback_system_mode_set(void)
{
int err;
enum lte_lc_system_mode fallback_sys_mode;
switch (lte_lc_sys_mode) {
case LTE_LC_SYSTEM_MODE_LTEM:
fallback_sys_mode = LTE_LC_SYSTEM_MODE_NBIOT;
break;
case LTE_LC_SYSTEM_MODE_NBIOT:
fallback_sys_mode = LTE_LC_SYSTEM_MODE_LTEM;
break;
case LTE_LC_SYSTEM_MODE_LTEM_GPS:
fallback_sys_mode = LTE_LC_SYSTEM_MODE_NBIOT_GPS;
break;
case LTE_LC_SYSTEM_MODE_NBIOT_GPS:
fallback_sys_mode = LTE_LC_SYSTEM_MODE_LTEM_GPS;
break;
default:
LOG_WRN("Fallback enabled, but not possible from current system mode %d",
lte_lc_sys_mode);
return -EINVAL;
}
err = nrf_modem_at_printf("AT%%XSYSTEMMODE=%s,%c",
system_mode_params[fallback_sys_mode],
system_mode_preference[lte_lc_sys_mode_pref]);
if (err) {
LOG_ERR("Failed to set system mode, error: %d", err);
return -EFAULT;
}
LOG_DBG("Trying fallback, system mode set to %d, preference %d",
fallback_sys_mode, lte_lc_sys_mode_pref);
return 0;
}
static int connect_lte(bool blocking)
{
int err;
enum lte_lc_func_mode original_func_mode;
enum lte_lc_nw_reg_status reg_status;
static atomic_t in_progress;
/* Check if a connection attempt is already in progress */
if (atomic_set(&in_progress, 1)) {
return -EINPROGRESS;
}
err = lte_lc_nw_reg_status_get(®_status);
if (err) {
LOG_ERR("Failed to get current registration status");
err = -EFAULT;
goto exit;
}
/* Do not attempt to register with an LTE network if the device already is registered.
* This check is needed for blocking _connect() calls to avoid hanging for
* CONFIG_LTE_NETWORK_TIMEOUT seconds waiting for a semaphore that will not be given.
*/
if ((reg_status == LTE_LC_NW_REG_REGISTERED_HOME) ||
(reg_status == LTE_LC_NW_REG_REGISTERED_ROAMING)) {
LOG_DBG("The device is already registered with an LTE network");
err = 0;
goto exit;
}
err = lte_lc_func_mode_get(&original_func_mode);
if (err) {
err = -EFAULT;
goto exit;
}
if (lte_lc_sys_mode != LTE_LC_SYSTEM_MODE_DEFAULT &&
IS_ENABLED(CONFIG_LTE_NETWORK_USE_FALLBACK)) {
/* Set system mode, because it may have been changed by fallback. */
if (original_func_mode != LTE_LC_FUNC_MODE_POWER_OFF &&
original_func_mode != LTE_LC_FUNC_MODE_OFFLINE) {
err = lte_lc_func_mode_set(LTE_LC_FUNC_MODE_OFFLINE);
if (err) {
err = -EFAULT;
goto exit;
}
}
err = lte_lc_system_mode_set(lte_lc_sys_mode, lte_lc_sys_mode_pref);
if (err) {
err = -EFAULT;
goto exit;
}
}
/* Reset the semaphore, it may have already been given by an earlier +CEREG notification. */
k_sem_reset(&link);
err = lte_lc_func_mode_set(LTE_LC_FUNC_MODE_NORMAL);
if (err || !blocking) {
goto exit;
}
err = k_sem_take(&link, K_SECONDS(CONFIG_LTE_NETWORK_TIMEOUT));
if (err == -EAGAIN) {
LOG_INF("Network connection attempt timed out");
err = -ETIMEDOUT;
if (IS_ENABLED(CONFIG_LTE_NETWORK_USE_FALLBACK)) {
err = lte_lc_func_mode_set(LTE_LC_FUNC_MODE_OFFLINE);
if (err) {
err = -EFAULT;
goto exit;
}
err = fallback_system_mode_set();
if (err) {
err = -ETIMEDOUT;
goto exit;
}
k_sem_reset(&link);
err = lte_lc_func_mode_set(LTE_LC_FUNC_MODE_NORMAL);
if (err || !blocking) {
goto exit;
}
err = k_sem_take(&link, K_SECONDS(CONFIG_LTE_NETWORK_TIMEOUT));
if (err == -EAGAIN) {
LOG_INF("Network connection attempt timed out");
err = -ETIMEDOUT;
}
}
}
exit:
if (err) {
/* Connecting to LTE network failed, restore original functional mode. */
lte_lc_func_mode_set(original_func_mode);
}
atomic_clear(&in_progress);
return err;
}
static int feaconf_write(enum feaconf_feat feat, bool state)
{
return nrf_modem_at_printf("AT%%FEACONF=%d,%d,%u", FEACONF_OPER_WRITE, feat, state);
}
/* Public API */
int lte_lc_init(void)
{
return 0;
}
void lte_lc_register_handler(lte_lc_evt_handler_t handler)
{
if (handler == NULL) {
LOG_INF("NULL as a handler received: Nothing to be done.\n"
"The handler can be deregistered using lte_lc_deregister_handler()");
return;
}
event_handler_list_append_handler(handler);
}
int lte_lc_deregister_handler(lte_lc_evt_handler_t handler)
{
if (handler == NULL) {
LOG_ERR("Invalid handler (handler=0x%08X)", (uint32_t)handler);
return -EINVAL;
}
return event_handler_list_remove_handler(handler);
}
int lte_lc_connect(void)
{
return connect_lte(true);
}
int lte_lc_init_and_connect(void)
{
return connect_lte(true);
}
int lte_lc_connect_async(lte_lc_evt_handler_t handler)
{
if (handler) {
event_handler_list_append_handler(handler);
} else if (event_handler_list_is_empty()) {
LOG_ERR("No handler registered");
return -EINVAL;
}
return connect_lte(false);
}
int lte_lc_init_and_connect_async(lte_lc_evt_handler_t handler)
{
return lte_lc_connect_async(handler);
}
int lte_lc_deinit(void)
{
return lte_lc_power_off();
}
int lte_lc_normal(void)
{
return lte_lc_func_mode_set(LTE_LC_FUNC_MODE_NORMAL) ? -EFAULT : 0;
}
int lte_lc_offline(void)
{
return lte_lc_func_mode_set(LTE_LC_FUNC_MODE_OFFLINE) ? -EFAULT : 0;
}
int lte_lc_power_off(void)
{
return lte_lc_func_mode_set(LTE_LC_FUNC_MODE_POWER_OFF) ? -EFAULT : 0;
}
int lte_lc_psm_param_set(const char *rptau, const char *rat)
{
if ((rptau != NULL && strlen(rptau) != 8) ||
(rat != NULL && strlen(rat) != 8)) {
return -EINVAL;
}
if (rptau != NULL) {
strcpy(requested_psm_param_rptau, rptau);
LOG_DBG("RPTAU set to %s", requested_psm_param_rptau);
} else {
*requested_psm_param_rptau = '\0';
LOG_DBG("Using modem default value for RPTAU");
}
if (rat != NULL) {
strcpy(requested_psm_param_rat, rat);
LOG_DBG("RAT set to %s", requested_psm_param_rat);
} else {
*requested_psm_param_rat = '\0';
LOG_DBG("Using modem default value for RAT");
}
return 0;
}
int lte_lc_psm_param_set_seconds(int rptau, int rat)
{
int ret;
ret = encode_psm(requested_psm_param_rptau, requested_psm_param_rat, rptau, rat);
if (ret != 0) {
*requested_psm_param_rptau = '\0';
*requested_psm_param_rat = '\0';
}
LOG_DBG("RPTAU=%d (%s), RAT=%d (%s), ret=%d",
rptau, requested_psm_param_rptau, rat, requested_psm_param_rat, ret);
return ret;
}
int lte_lc_psm_req(bool enable)
{
int err;
LOG_DBG("enable=%d, tau=%s, rat=%s",
enable, requested_psm_param_rptau, requested_psm_param_rat);
if (enable) {
if (strlen(requested_psm_param_rptau) == 8 &&
strlen(requested_psm_param_rat) == 8) {
err = nrf_modem_at_printf("AT+CPSMS=1,,,\"%s\",\"%s\"",
requested_psm_param_rptau,
requested_psm_param_rat);
} else if (strlen(requested_psm_param_rptau) == 8) {
err = nrf_modem_at_printf("AT+CPSMS=1,,,\"%s\"", requested_psm_param_rptau);
} else if (strlen(requested_psm_param_rat) == 8) {
err = nrf_modem_at_printf("AT+CPSMS=1,,,,\"%s\"", requested_psm_param_rat);
} else {
err = nrf_modem_at_printf("AT+CPSMS=1");
}
} else {
err = nrf_modem_at_printf(psm_disable);
}
if (err) {
LOG_ERR("nrf_modem_at_printf failed, reported error: %d", err);
return -EFAULT;
}
return 0;
}
int lte_lc_psm_get(int *tau, int *active_time)
{
int err;
struct lte_lc_psm_cfg psm_cfg;
char active_time_str[9] = {0};
char tau_ext_str[9] = {0};
char tau_legacy_str[9] = {0};
static char response[160] = { 0 };
const char ch = ',';
char *comma_ptr;
if ((tau == NULL) || (active_time == NULL)) {
return -EINVAL;
}
/* Format of XMONITOR AT command response:
* %XMONITOR: <reg_status>,[<full_name>,<short_name>,<plmn>,<tac>,<AcT>,<band>,<cell_id>,
* <phys_cell_id>,<EARFCN>,<rsrp>,<snr>,<NW-provided_eDRX_value>,<Active-Time>,
* <Periodic-TAUext>,<Periodic-TAU>]
* We need to parse the three last parameters, Active-Time, Periodic-TAU-ext and
* Periodic-TAU.
*/
response[0] = '\0';
err = nrf_modem_at_cmd(response, sizeof(response), "AT%%XMONITOR");
if (err) {
LOG_ERR("AT command failed, error: %d", err);