Skip to content

Commit 62f1300

Browse files
blavkahubmartin
authored andcommitted
subsys: ctr_lte: add connectivity check
1 parent 3077632 commit 62f1300

File tree

4 files changed

+269
-5
lines changed

4 files changed

+269
-5
lines changed

subsys/ctr_lte/Kconfig

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ config CTR_LTE_THREAD_PRIORITY
2929
int "CTR_LTE_THREAD_PRIORITY"
3030
default 10
3131

32+
config CTR_LTE_CONNECTIVITY_CHECK
33+
bool "CTR_LTE_CONNECTIVITY_CHECK"
34+
default n
35+
3236
module = CTR_LTE
3337
module-str = CHESTER LTE Subsystem
3438
source "subsys/logging/Kconfig.template.log_config"

subsys/ctr_lte/ctr_lte.c

+117-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ LOG_MODULE_REGISTER(ctr_lte, CONFIG_CTR_LTE_LOG_LEVEL);
3838

3939
#define SETTINGS_PFX "lte"
4040

41-
#define XSLEEP_PAUSE K_MSEC(100)
42-
#define BOOT_TIMEOUT K_SECONDS(5)
41+
#define XSLEEP_PAUSE K_MSEC(100)
42+
#define BOOT_TIMEOUT K_SECONDS(5)
4343
#define BOOT_RETRY_COUNT 3
4444
#define BOOT_RETRY_DELAY K_SECONDS(10)
4545
#define SETUP_RETRY_COUNT 1
@@ -992,6 +992,93 @@ static int eval(int retries, k_timeout_t delay, struct ctr_lte_eval *eval)
992992
return err;
993993
}
994994

995+
#if defined(CONFIG_CTR_LTE_CONNECTIVITY_CHECK)
996+
997+
static int check_functional_mode(void)
998+
{
999+
int ret;
1000+
char buf[16];
1001+
1002+
ret = ctr_lte_talk_at_cfun_read(buf, sizeof(buf));
1003+
if (ret < 0) {
1004+
LOG_ERR("Call `ctr_lte_talk_at_cfun_read` failed: %d", ret);
1005+
return ret;
1006+
}
1007+
1008+
if (strcmp(buf, "+CFUN: 1") != 0) {
1009+
return -ENOTCONN;
1010+
}
1011+
1012+
return 0;
1013+
}
1014+
1015+
static int check_network_registration_status(void)
1016+
{
1017+
int ret;
1018+
char buf[128];
1019+
ret = ctr_lte_talk_at_cereg_read(buf, sizeof(buf));
1020+
if (ret < 0) {
1021+
LOG_ERR("Call `ctr_lte_talk_at_cereg_read` failed: %d", ret);
1022+
return ret;
1023+
}
1024+
1025+
LOG_INF("cereg buf: %s", buf);
1026+
1027+
if (strncmp(buf, "+CEREG: ", 8) != 0) {
1028+
LOG_ERR("Unexpected response");
1029+
return -ENOTCONN;
1030+
}
1031+
1032+
if (buf[8] == '0') {
1033+
LOG_ERR("CEREG unsubscribe unsolicited result codes");
1034+
return -ENOTCONN;
1035+
}
1036+
1037+
if (buf[10] != '1' && buf[10] != '5') {
1038+
return -ENOTCONN;
1039+
}
1040+
1041+
return 0;
1042+
}
1043+
1044+
static int check_pdn_is_active(void)
1045+
{
1046+
int ret;
1047+
char buf[32];
1048+
1049+
ret = ctr_lte_talk_at_cgatt_read(buf, sizeof(buf));
1050+
if (ret < 0) {
1051+
LOG_ERR("Call `ctr_lte_talk_at_cgatt_read` failed: %d", ret);
1052+
return ret;
1053+
}
1054+
1055+
if (strcmp(buf, "+CGATT: 1") != 0) {
1056+
return -ENOTCONN;
1057+
}
1058+
1059+
return 0;
1060+
}
1061+
1062+
static int check_pdn_connections(void)
1063+
{
1064+
int ret;
1065+
char buf[32];
1066+
1067+
ret = ctr_lte_talk_at_cgact_read(buf, sizeof(buf));
1068+
if (ret < 0) {
1069+
LOG_ERR("Call `ctr_lte_talk_at_cgact_read` failed: %d", ret);
1070+
return ret;
1071+
}
1072+
1073+
if (strcmp(buf, "+CGACT: 0,1") != 0) {
1074+
return -ENOTCONN;
1075+
}
1076+
1077+
return 0;
1078+
}
1079+
1080+
#endif
1081+
9951082
static int send_once(const struct send_msgq_data *data)
9961083
{
9971084
int ret;
@@ -1003,6 +1090,34 @@ static int send_once(const struct send_msgq_data *data)
10031090
return ret;
10041091
}
10051092

1093+
#if defined(CONFIG_CTR_LTE_CONNECTIVITY_CHECK)
1094+
1095+
ret = check_functional_mode();
1096+
if (ret) {
1097+
LOG_ERR("Call `check_functional_mode` failed: %d", ret);
1098+
return ret;
1099+
}
1100+
1101+
ret = check_network_registration_status();
1102+
if (ret) {
1103+
LOG_ERR("Call `check_network_registration_status` failed: %d", ret);
1104+
return ret;
1105+
}
1106+
1107+
ret = check_pdn_is_active();
1108+
if (ret) {
1109+
LOG_ERR("Call `check_pdn_is_active` failed: %d", ret);
1110+
return ret;
1111+
}
1112+
1113+
ret = check_pdn_connections();
1114+
if (ret) {
1115+
LOG_ERR("Call `check_pdn_connections` failed: %d", ret);
1116+
return ret;
1117+
}
1118+
1119+
#endif
1120+
10061121
ret = ctr_lte_talk_at_xsocketopt(1, 50, NULL);
10071122

10081123
if (ret < 0) {

subsys/ctr_lte/ctr_lte_talk.c

+142-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
LOG_MODULE_REGISTER(ctr_lte_talk, CONFIG_CTR_LTE_LOG_LEVEL);
2626

27-
#define SEND_GUARD_TIME K_MSEC(50)
27+
#define SEND_GUARD_TIME K_MSEC(50)
2828
#define RESPONSE_TIMEOUT_S K_SECONDS(3)
2929
#define RESPONSE_TIMEOUT_L K_SECONDS(30)
3030

@@ -265,7 +265,7 @@ static void release_response(struct response *response)
265265

266266
struct response_item *item;
267267

268-
SYS_SLIST_FOR_EACH_CONTAINER (&response->list, item, node) {
268+
SYS_SLIST_FOR_EACH_CONTAINER(&response->list, item, node) {
269269
if (item->s != NULL) {
270270
k_free(item->s);
271271
}
@@ -384,7 +384,7 @@ static int talk_cmd_response_ok(k_timeout_t timeout, response_cb cb, void *p1, v
384384
int idx = 0;
385385
struct response_item *item;
386386

387-
SYS_SLIST_FOR_EACH_CONTAINER (&response.list, item, node) {
387+
SYS_SLIST_FOR_EACH_CONTAINER(&response.list, item, node) {
388388
ret = cb(idx++, response.count, item->s, p1, p2, p3);
389389
if (ret < 0) {
390390
LOG_ERR("Call `cb` failed: %d", ret);
@@ -1317,3 +1317,142 @@ int ctr_lte_talk_at_xtime(int p1)
13171317

13181318
return 0;
13191319
}
1320+
1321+
#if defined(CONFIG_CTR_LTE_CONNECTIVITY_CHECK)
1322+
1323+
static int at_cfun_read_response_handler(int idx, int count, const char *s, void *p1, void *p2,
1324+
void *p3)
1325+
{
1326+
char *p = p1;
1327+
size_t *size = p2;
1328+
1329+
if (idx == 0 && count == 1) {
1330+
if (strlen(s) >= *size) {
1331+
return -ENOBUFS;
1332+
}
1333+
1334+
strcpy(p, s);
1335+
1336+
return 0;
1337+
}
1338+
1339+
return -EINVAL;
1340+
}
1341+
1342+
int ctr_lte_talk_at_cfun_read(char *buf, size_t size)
1343+
{
1344+
int ret;
1345+
ret = talk_cmd_response_ok(RESPONSE_TIMEOUT_S, at_cfun_read_response_handler, buf, &size,
1346+
NULL, "AT+CFUN?");
1347+
1348+
if (ret < 0) {
1349+
LOG_ERR("Call `talk_cmd_response_ok` failed: %d", ret);
1350+
return ret;
1351+
}
1352+
1353+
return 0;
1354+
}
1355+
1356+
static int at_cereg_read_response_handler(int idx, int count, const char *s, void *p1, void *p2,
1357+
void *p3)
1358+
{
1359+
char *p = p1;
1360+
size_t *size = p2;
1361+
1362+
if (idx == 0 && count == 1) {
1363+
if (strlen(s) >= *size) {
1364+
return -ENOBUFS;
1365+
}
1366+
1367+
strcpy(p, s);
1368+
1369+
return 0;
1370+
}
1371+
1372+
return -EINVAL;
1373+
}
1374+
1375+
int ctr_lte_talk_at_cereg_read(char *buf, size_t size)
1376+
{
1377+
int ret;
1378+
1379+
ret = talk_cmd_response_ok(RESPONSE_TIMEOUT_S, at_cereg_read_response_handler, buf, &size,
1380+
NULL, "AT+CEREG?");
1381+
1382+
if (ret < 0) {
1383+
LOG_ERR("Call `talk_cmd_response_ok` failed: %d", ret);
1384+
return ret;
1385+
}
1386+
1387+
return 0;
1388+
}
1389+
1390+
static int at_cgatt_read_response_handler(int idx, int count, const char *s, void *p1, void *p2,
1391+
void *p3)
1392+
{
1393+
char *p = p1;
1394+
size_t *size = p2;
1395+
1396+
if (idx == 0 && count == 1) {
1397+
if (strlen(s) >= *size) {
1398+
return -ENOBUFS;
1399+
}
1400+
1401+
strcpy(p, s);
1402+
1403+
return 0;
1404+
}
1405+
1406+
return -EINVAL;
1407+
}
1408+
1409+
int ctr_lte_talk_at_cgatt_read(char *buf, size_t size)
1410+
{
1411+
int ret;
1412+
1413+
ret = talk_cmd_response_ok(RESPONSE_TIMEOUT_S, at_cgatt_read_response_handler, buf, &size,
1414+
NULL, "AT+CGATT?");
1415+
1416+
if (ret < 0) {
1417+
LOG_ERR("Call `talk_cmd_response_ok` failed: %d", ret);
1418+
return ret;
1419+
}
1420+
1421+
return 0;
1422+
}
1423+
1424+
static int at_cgact_read_response_handler(int idx, int count, const char *s, void *p1, void *p2,
1425+
void *p3)
1426+
{
1427+
char *p = p1;
1428+
size_t *size = p2;
1429+
1430+
if (idx == 0 && count == 1) {
1431+
if (strlen(s) >= *size) {
1432+
return -ENOBUFS;
1433+
}
1434+
1435+
strcpy(p, s);
1436+
1437+
return 0;
1438+
}
1439+
1440+
return -EINVAL;
1441+
}
1442+
1443+
int ctr_lte_talk_at_cgact_read(char *buf, size_t size)
1444+
{
1445+
int ret;
1446+
1447+
ret = talk_cmd_response_ok(RESPONSE_TIMEOUT_S, at_cgact_read_response_handler, buf, &size,
1448+
NULL, "AT+CGACT?");
1449+
1450+
if (ret < 0) {
1451+
LOG_ERR("Call `talk_cmd_response_ok` failed: %d", ret);
1452+
return ret;
1453+
}
1454+
1455+
return 0;
1456+
}
1457+
1458+
#endif /* CONFIG_CTR_LTE_CONNECTIVITY_CHECK */

subsys/ctr_lte/ctr_lte_talk.h

+6
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ int ctr_lte_talk_at_xsleep(int p1);
6565
int ctr_lte_talk_at_xsocket(int p1, int *p2, int *p3, char *buf, size_t size);
6666
int ctr_lte_talk_at_xsystemmode(int p1, int p2, int p3, int p4);
6767
int ctr_lte_talk_at_xtime(int p1);
68+
#if defined(CONFIG_CTR_LTE_CONNECTIVITY_CHECK)
69+
int ctr_lte_talk_at_cfun_read(char *buf, size_t size);
70+
int ctr_lte_talk_at_cereg_read(char *buf, size_t size);
71+
int ctr_lte_talk_at_cgatt_read(char *buf, size_t size);
72+
int ctr_lte_talk_at_cgact_read(char *buf, size_t size);
73+
#endif
6874

6975
#ifdef __cplusplus
7076
}

0 commit comments

Comments
 (0)