10000 lte: refactor lteppp_get_modem_conn_state() · pycom/pycom-micropython-sigfox@e5a00fe · GitHub
[go: up one dir, main page]

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

Commit e5a00fe

Browse files
committed
lte: refactor lteppp_get_modem_conn_state()
no functional change, but make the code a little more readable by pulling the semaphore and edits into a function and use more intuitive fct names
1 parent 6c52763 commit e5a00fe

File tree

4 files changed

+23
-29
lines changed

4 files changed

+23
-29
lines changed

esp32/lte/lteppp.c

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ void lteppp_init(void) {
174174
lteppp_pcb = pppapi_pppos_create(&lteppp_netif, lteppp_output_callback, lteppp_status_cb, NULL);
175175

176176
//wait on connecting modem until it is allowed
177-
lteppp_modem_conn_state = E_LTE_MODEM_DISCONNECTED;
177+
lteppp_set_modem_conn_state(E_LTE_MODEM_DISCONNECTED);
178178

179179
xTaskCreatePinnedToCore(TASK_LTE, "LTE", LTE_TASK_STACK_SIZE / sizeof(StackType_t), NULL, LTE_TASK_PRIORITY, &xLTETaskHndl, 1);
180180

@@ -211,7 +211,7 @@ char* lteppp_get_log_buff(void)
211211
}
212212
#endif
213213

214-
lte_modem_conn_state_t lteppp_modem_state(void)
214+
lte_modem_conn_state_t lteppp_get_modem_conn_state(void)
215215
{
216216
lte_modem_conn_state_t state;
217217
if (!xLTESem){
@@ -224,6 +224,13 @@ lte_modem_conn_state_t lteppp_modem_state(void)
224224
return state;
225225
}
226226

227+
void lteppp_set_modem_conn_state(lte_modem_conn_state_t state)
228+
{
229+
xSemaphoreTake(xLTESem, portMAX_DELAY);
230+
lteppp_modem_conn_state = state;
231+
xSemaphoreGive(xLTESem);
232+
}
233+
227234
void lteppp_set_state(lte_state_t state) {
228235
xSemaphoreTake(xLTESem, portMAX_DELAY);
229236
lteppp_lte_state = state;
@@ -455,9 +462,7 @@ static void TASK_LTE (void *pvParameters) {
455462
{
456463
MSG("notif\n");
457464
xSemaphoreTake(xLTE_modem_Conn_Sem, portMAX_DELAY);
458-
xSemaphoreTake(xLTESem, portMAX_DELAY);
459-
lteppp_modem_conn_state = E_LTE_MODEM_CONNECTING;
460-
xSemaphoreGive(xLTESem);
465+
lteppp_set_modem_conn_state(E_LTE_MODEM_CONNECTING);
461466
uart_set_rts(LTE_UART_ID, true);
462467
vTaskDelay(500/portTICK_PERIOD_MS);
463468
uart_set_hw_flow_ctrl(LTE_UART_ID, UART_HW_FLOWCTRL_CTS_RTS, 64);
@@ -471,9 +476,7 @@ static void TASK_LTE (void *pvParameters) {
471476
if (at_trials >= LTE_AT_CMD_TRIALS) {
472477
uart_set_hw_flow_ctrl(LTE_UART_ID, UART_HW_FLOWCTRL_DISABLE, 0);
473478
uart_set_rts(LTE_UART_ID, false);
474-
xSemaphoreTake(xLTESem, portMAX_DELAY);
475-
lteppp_modem_conn_state = E_LTE_MODEM_DISCONNECTED;
476-
xSemaphoreGive(xLTESem);
479+
lteppp_set_modem_conn_state(E_LTE_MODEM_DISCONNECTED);
477480
xSemaphoreGive(xLTE_modem_Conn_Sem);
478481
at_trials = 0;
479482
goto modem_init;
@@ -494,9 +497,7 @@ static void TASK_LTE (void *pvParameters) {
494497
if (at_trials >= LTE_AT_CMD_TRIALS) {
495498
uart_set_hw_flow_ctrl(LTE_UART_ID, UART_HW_FLOWCTRL_DISABLE, 0);
496499
uart_set_rts(LTE_UART_ID, false);
497-
xSemaphoreTake(xLTESem, portMAX_DELAY);
498-
lteppp_modem_conn_state = E_LTE_MODEM_DISCONNECTED;
499-
xSemaphoreGive(xLTESem);
500+
lteppp_set_modem_conn_state(E_LTE_MODEM_DISCONNECTED);
500501
xSemaphoreGive(xLTE_modem_Conn_Sem);
501502
at_trials = 0;
502503
goto modem_init;
@@ -511,9 +512,7 @@ static void TASK_LTE (void *pvParameters) {
511512
if (at_trials >= LTE_AT_CMD_TRIALS) {
512513
uart_set_hw_flow_ctrl(LTE_UART_ID, UART_HW_FLOWCTRL_DISABLE, 0);
513514
uart_set_rts(LTE_UART_ID, false);
514-
xSemaphoreTake(xLTESem, portMAX_DELAY);
515-
lteppp_modem_conn_state = E_LTE_MODEM_DISCONNECTED;
516-
xSemaphoreGive(xLTESem);
515+
lteppp_set_modem_conn_state(E_LTE_MODEM_DISCONNECTED);
517516
xSemaphoreGive(xLTE_modem_Conn_Sem);
518517
at_trials = 0;
519518
goto modem_init;
@@ -555,22 +554,16 @@ static void TASK_LTE (void *pvParameters) {
555554
{
556555
lteppp_send_at_cmd("AT+SQNIBRCFG=1,100", LTE_RX_TIMEOUT_MAX_MS);
557556
}
558-
xSemaphoreTake(xLTESem, portMAX_DELAY);
559-
lteppp_modem_conn_state = E_LTE_MODEM_CONNECTED;
560-
xSemaphoreGive(xLTESem);
557+
lteppp_set_modem_conn_state(E_LTE_MODEM_CONNECTED);
561558
xSemaphoreGive(xLTE_modem_Conn_Sem);
562559
MSG("forever\n");
563560
lte_state_t state;
564561
for (;;) {
565562
vTaskDelay(LTE_TASK_PERIOD_MS);
566-
xSemaphoreTake(xLTESem, portMAX_DELAY);
567-
if(E_LTE_MODEM_DISCONNECTED == lteppp_modem_conn_state)
568-
{
569-
xSemaphoreGive(xLTESem);
563+
if(lteppp_get_modem_conn_state() == E_LTE_MODEM_DISCONNECTED ){
570564
// restart the task
571565
goto modem_init;
572566
}
573-
xSemaphoreGive(xLTESem);
574567
state = lteppp_get_state();
575568
if (xQueueReceive(xCmdQueue, lteppp_trx_buffer, 0)) {
576569
MSG("cmd\n");
@@ -868,7 +861,7 @@ static void lteppp_print_states(){
868861
if (!xLTESem)
869862
return;
870863
static lte_modem_conn_state_t last_c = 0xff;
871-
lte_modem_conn_state_t c = lteppp_modem_state();
864+
lte_modem_conn_state_t c = lteppp_get_modem_conn_state();
872865
static lte_state_t last_s = 0xff;
873866
lte_state_t s = lteppp_get_state();
874867
static bool last_u = false;

esp32/lte/lteppp.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ extern void lteppp_send_at_command (lte_task_cmd_data_t *cmd, lte_task_rsp_data_
119119

120120
extern bool lteppp_wait_at_rsp (const char *expected_rsp, uint32_t timeout, bool from_mp, void* data_rem);
121121

122-
lte_modem_conn_state_t lteppp_modem_state(void);
122+
lte_modem_conn_state_t lteppp_get_modem_conn_state(void);
123+
void lteppp_set_modem_conn_state(lte_modem_conn_state_t state);
123124

124125
extern void connect_lte_uart (void);
125126

esp32/mods/modlte.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ static mp_obj_t lte_init_helper(lte_obj_t *self, const mp_arg_val_t *args) {
454454
//printf("All done since we were already initialised.\n");
455455
return mp_const_none;
456456
}
457-
modem_state = lteppp_modem_state();
457+
modem_state = lteppp_get_modem_conn_state();
458458
switch(modem_state)
459459
{
460460
case E_LTE_MODEM_DISCONNECTED:
@@ -463,15 +463,15 @@ static mp_obj_t lte_init_helper(lte_obj_t *self, const mp_arg_val_t *args) {
463463
MP_THREAD_GIL_EXIT();
464464
xSemaphoreTake(xLTE_modem_Conn_Sem, portMAX_DELAY);
465465
MP_THREAD_GIL_ENTER();
466-
if (E_LTE_MODEM_DISCONNECTED == lteppp_modem_state()) {
466+
if (E_LTE_MODEM_DISCONNECTED == lteppp_get_modem_conn_state()) {
467467
xSemaphoreGive(xLTE_modem_Conn_Sem);
468468
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Couldn't connect to Modem (modem_state=disconnected)"));
469469
}
470470
break;
471471
case E_LTE_MODEM_CONNECTING:
472472
// Block till modem is connected
473473
xSemaphoreTake(xLTE_modem_Conn_Sem, portMAX_DELAY);
474-
if (E_LTE_MODEM_DISCONNECTED == lteppp_modem_state()) {
474+
if (E_LTE_MODEM_DISCONNECTED == lteppp_get_modem_conn_state()) {
475475
xSemaphoreGive(xLTE_modem_Conn_Sem);
476476
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Couldn't connect to Modem (modem_state=connecting)"));
477477
}

esp32/mods/modmachine.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ STATIC mp_obj_t machine_sleep (uint n_args, const mp_obj_t *arg) {
405405
bool reconnect = false;
406406

407407
#if defined(FIPY) || defined(GPY)
408-
if (lteppp_modem_state() < E_LTE_MODEM_DISCONNECTED) {
408+
if (lteppp_get_modem_conn_state() < E_LTE_MODEM_DISCONNECTED) {
409409
lteppp_deinit();
410410
}
411411
#endif
@@ -466,7 +466,7 @@ STATIC mp_obj_t machine_deepsleep (uint n_args, const mp_obj_t *arg) {
466466
modbt_deinit(false);
467467
wlan_deinit(NULL);
468468
#if defined(FIPY) || defined(GPY)
469-
if (lteppp_modem_state() < E_LTE_MODEM_DISCONNECTED) {
469+
if (lteppp_get_modem_conn_state() < E_LTE_MODEM_DISCONNECTED) {
470470
lteppp_deinit();
471471
}
472472
#endif

0 commit comments

Comments
 (0)
0