Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit b241201

Browse files
author
Daniel Campora
committed
esp32: Improve the robustness of lte.connect() when resuming a previous session.
1 parent 83e2f7f commit b241201

File tree

3 files changed

+51
-24
lines changed

3 files changed

+51
-24
lines changed

esp32/lte/lteppp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,8 @@ static void TASK_LTE (void *pvParameters) {
268268

269269
// enable PSM if not already enabled
270270
lteppp_send_at_cmd("AT+CPSMS?", LTE_RX_TIMEOUT_MAX_MS);
271-
if (!strstr(lteppp_trx_buffer, "+CPSMS: 1")) {
272-
lteppp_send_at_cmd("AT+CPSMS=1", LTE_RX_TIMEOUT_MIN_MS);
271+
if (!strstr(lteppp_trx_buffer, "+CPSMS: 0")) {
272+
lteppp_send_at_cmd("AT+CPSMS=0", LTE_RX_TIMEOUT_MIN_MS);
273273
}
274274
// enable low power mode
275275
lteppp_send_at_cmd("AT!=\"setlpm airplane=1 enable=1\"", LTE_RX_TIMEOUT_MAX_MS);

esp32/lte/lteppp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
#define LTE_OK_RSP "OK"
2525
#define LTE_CONNECT_RSP "CONNECT"
26-
#define LTE_RX_TIMEOUT_MAX_MS (9500)
26+
#define LTE_RX_TIMEOUT_MAX_MS (20000)
2727
#define LTE_RX_TIMEOUT_MIN_MS (250)
2828
#define LTE_PPP_BACK_OFF_TIME_MS (1150)
2929

esp32/mods/modlte.c

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ static bool lte_check_attached(void);
9393
static void lte_check_init(void);
9494
static bool lte_check_sim_present(void);
9595

96+
STATIC mp_obj_t lte_isconnected(mp_obj_t self_in);
9697
STATIC mp_obj_t lte_disconnect(mp_obj_t self_in);
9798

9899
/******************************************************************************
@@ -139,7 +140,7 @@ static bool lte_check_attached(void) {
139140
bool inppp = false;;
140141
bool attached = false;
141142

142-
if (lteppp_get_state() == E_LTE_PPP) {
143+
if (lte_isconnected(NULL) == mp_const_true) {
143144
inppp = true;
144145
lte_pause_ppp();
145146
while (true) {
@@ -273,7 +274,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(lte_init_obj, 1, lte_init);
273274
mp_obj_t lte_deinit(mp_obj_t self_in) {
274275
lte_check_init();
275276
lte_obj_t *self = self_in;
276-
if (lteppp_get_state() == E_LTE_PPP) {
277+
if (lte_isconnected(self) == mp_const_true) {
277278
lte_disconnect(self);
278279
}
279280

@@ -348,7 +349,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(lte_attach_obj, 1, lte_attach);
348349
mp_obj_t lte_dettach(mp_obj_t self_in) {
349350
lte_check_init();
350351
lte_obj_t *self = self_in;
351-
if (lteppp_get_state() == E_LTE_PPP) {
352+
if (lte_isconnected(self) == mp_const_true) {
352353
lte_disconnect(self);
353354
}
354355
if (lte_check_sim_present()) {
@@ -392,24 +393,57 @@ STATIC mp_obj_t lte_connect(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t
392393
char at_cmd[LTE_AT_CMD_SIZE_MAX - 4];
393394
lte_obj.cid = args[0].u_int;
394395
sprintf(at_cmd, "AT+CGDATA=\"PPP\",%d", lte_obj.cid);
395-
// set the PPP state in advance, to avoid CEREG? to be sent right after PPP is entered
396-
if (lte_push_at_command_ext(at_cmd, LTE_RX_TIMEOUT_MIN_MS, LTE_CONNECT_RSP) ||
397-
lte_push_at_command_ext("ATO", LTE_RX_TIMEOUT_MIN_MS, LTE_CONNECT_RSP)) {
398-
lteppp_connect();
399-
lteppp_set_state(E_LTE_PPP);
396+
// try to stablish a new connection first
397+
if (lte_push_at_command_ext(at_cmd, LTE_RX_TIMEOUT_MAX_MS, LTE_CONNECT_RSP)) {
398+
if (!lte_push_at_command_ext("AT", LTE_RX_TIMEOUT_MIN_MS, "ERROR")) {
399+
mp_hal_delay_ms(LTE_PPP_BACK_OFF_TIME_MS);
400+
goto connect;
401+
}
402+
}
403+
// that failed, so try to resume the connection
404+
if (lte_push_at_command_ext("ATO", LTE_RX_TIMEOUT_MAX_MS, LTE_CONNECT_RSP)) {
405+
if (!lte_push_at_command_ext("AT", LTE_RX_TIMEOUT_MIN_MS, "ERROR")) {
406+
mp_hal_delay_ms(LTE_PPP_BACK_OFF_TIME_MS);
407+
goto connect;
408+
}
400409
} else {
401-
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
410+
// try a new connection once again..
411+
if (lte_push_at_command_ext(at_cmd, LTE_RX_TIMEOUT_MAX_MS, LTE_CONNECT_RSP)) {
412+
if (!lte_push_at_command_ext("AT", LTE_RX_TIMEOUT_MIN_MS, "ERROR")) {
413+
mp_hal_delay_ms(LTE_PPP_BACK_OFF_TIME_MS);
414+
goto connect;
415+
}
416+
} else {
417+
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
418+
}
402419
}
403420
} else {
421+
if (lte_isconnected(NULL) == mp_const_true) {
422+
return mp_const_none;
423+
}
404424
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "modem not attached"));
405425
}
426+
427+
connect:
428+
lteppp_connect();
429+
lteppp_set_state(E_LTE_PPP);
406430
return mp_const_none;
407431
}
408432
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(lte_connect_obj, 1, lte_connect);
409433

434+
STATIC mp_obj_t lte_isconnected(mp_obj_t self_in) {
435+
lte_check_init();
436+
if (lteppp_get_state() == E_LTE_PPP && lteppp_ipv4() > 0) {
437+
return mp_const_true;
438+
}
439+
return mp_const_false;
440+
}
441+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(lte_isconnected_obj, lte_isconnected);
442+
410443
STATIC mp_obj_t lte_disconnect(mp_obj_t self_in) {
411444
lte_check_init();
412-
if (lteppp_get_state() == E_LTE_PPP) {
445+
lte_obj_t *self = self_in;
446+
if (lte_isconnected(self) == mp_const_true) {
413447
lteppp_disconnect();
414448
lte_pause_ppp();
415449
lteppp_set_state(E_LTE_ATTACHED);
@@ -419,19 +453,11 @@ STATIC mp_obj_t lte_disconnect(mp_obj_t self_in) {
419453
}
420454
STATIC MP_DEFINE_CONST_FUN_OBJ_1(lte_disconnect_obj, lte_disconnect);
421455

422-
STATIC mp_obj_t lte_isconnected(mp_obj_t self_in) {
423-
lte_check_init();
424-
if (lteppp_get_state() == E_LTE_PPP && lteppp_ipv4() > 0) {
425-
return mp_const_true;
426-
}
427-
return mp_const_false;
428-
}
429-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(lte_isconnected_obj, lte_isconnected);
430-
431456
STATIC mp_obj_t lte_send_raw_at(mp_obj_t self_in, mp_obj_t cmd_o) {
432457
lte_check_init();
458+
lte_obj_t *self = self_in;
433459
bool inppp = false;
434-
if (lteppp_get_state() == E_LTE_PPP) {
460+
if (lte_isconnected(self) == mp_const_true) {
435461
inppp = true;
436462
lte_pause_ppp();
437463
while (true) {
@@ -486,7 +512,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(lte_iccid_obj, lte_iccid);
486512

487513
STATIC mp_obj_t lte_reset(mp_obj_t self_in) {
488514
lte_check_init();
489-
if (lteppp_get_state() == E_LTE_PPP) {
515+
lte_obj_t *self = self_in;
516+
if (lte_isconnected(self) == mp_const_true) {
490517
lte_pause_ppp();
491518
while (true) {
492519
mp_hal_delay_ms(LTE_RX_TIMEOUT_MIN_MS);

0 commit comments

Comments
 (0)