Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.
Prev Previous commit
Next Next commit
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
  • Loading branch information
peter-pycom committed Dec 22, 2020
commit e5a00fe43a3a486321da4aeebd74d7c329ccc090
39 changes: 16 additions & 23 deletions esp32/lte/lteppp.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ void lteppp_init(void) {
lteppp_pcb = pppapi_pppos_create(&lteppp_netif, lteppp_output_callback, lteppp_status_cb, NULL);

//wait on connecting modem until it is allowed
lteppp_modem_conn_state = E_LTE_MODEM_DISCONNECTED;
lteppp_set_modem_conn_state(E_LTE_MODEM_DISCONNECTED);

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

Expand Down Expand Up @@ -211,7 +211,7 @@ char* lteppp_get_log_buff(void)
}
#endif

lte_modem_conn_state_t lteppp_modem_state(void)
lte_modem_conn_state_t lteppp_get_modem_conn_state(void)
{
lte_modem_conn_state_t state;
if (!xLTESem){
Expand All @@ -224,6 +224,13 @@ lte_modem_conn_state_t lteppp_modem_state(void)
return state;
}

void lteppp_set_modem_conn_state(lte_modem_conn_state_t state)
{
xSemaphoreTake(xLTESem, portMAX_DELAY);
lteppp_modem_conn_state = state;
xSemaphoreGive(xLTESem);
}

void lteppp_set_state(lte_state_t state) {
xSemaphoreTake(xLTESem, portMAX_DELAY);
lteppp_lte_state = state;
Expand Down Expand Up @@ -455,9 +462,7 @@ static void TASK_LTE (void *pvParameters) {
{
MSG("notif\n");
xSemaphoreTake(xLTE_modem_Conn_Sem, portMAX_DELAY);
xSemaphoreTake(xLTESem, portMAX_DELAY);
lteppp_modem_conn_state = E_LTE_MODEM_CONNECTING;
xSemaphoreGive(xLTESem);
lteppp_set_modem_conn_state(E_LTE_MODEM_CONNECTING);
uart_set_rts(LTE_UART_ID, true);
vTaskDelay(500/portTICK_PERIOD_MS);
uart_set_hw_flow_ctrl(LTE_UART_ID, UART_HW_FLOWCTRL_CTS_RTS, 64);
Expand All @@ -471,9 +476,7 @@ static void TASK_LTE (void *pvParameters) {
if (at_trials >= LTE_AT_CMD_TRIALS) {
uart_set_hw_flow_ctrl(LTE_UART_ID, UART_HW_FLOWCTRL_DISABLE, 0);
uart_set_rts(LTE_UART_ID, false);
xSemaphoreTake(xLTESem, portMAX_DELAY);
lteppp_modem_conn_state = E_LTE_MODEM_DISCONNECTED;
xSemaphoreGive(xLTESem);
lteppp_set_modem_conn_state(E_LTE_MODEM_DISCONNECTED);
xSemaphoreGive(xLTE_modem_Conn_Sem);
at_trials = 0;
goto modem_init;
Expand All @@ -494,9 +497,7 @@ static void TASK_LTE (void *pvParameters) {
if (at_trials >= LTE_AT_CMD_TRIALS) {
uart_set_hw_flow_ctrl(LTE_UART_ID, UART_HW_FLOWCTRL_DISABLE, 0);
uart_set_rts(LTE_UART_ID, false);
xSemaphoreTake(xLTESem, portMAX_DELAY);
lteppp_modem_conn_state = E_LTE_MODEM_DISCONNECTED;
xSemaphoreGive(xLTESem);
lteppp_set_modem_conn_state(E_LTE_MODEM_DISCONNECTED);
xSemaphoreGive(xLTE_modem_Conn_Sem);
at_trials = 0;
goto modem_init;
Expand All @@ -511,9 +512,7 @@ static void TASK_LTE (void *pvParameters) {
if (at_trials >= LTE_AT_CMD_TRIALS) {
uart_set_hw_flow_ctrl(LTE_UART_ID, UART_HW_FLOWCTRL_DISABLE, 0);
uart_set_rts(LTE_UART_ID, false);
xSemaphoreTake(xLTESem, portMAX_DELAY);
lteppp_modem_conn_state = E_LTE_MODEM_DISCONNECTED;
xSemaphoreGive(xLTESem);
lteppp_set_modem_conn_state(E_LTE_MODEM_DISCONNECTED);
xSemaphoreGive(xLTE_modem_Conn_Sem);
at_trials = 0;
goto modem_init;
Expand Down Expand Up @@ -555,22 +554,16 @@ static void TASK_LTE (void *pvParameters) {
{
lteppp_send_at_cmd("AT+SQNIBRCFG=1,100", LTE_RX_TIMEOUT_MAX_MS);
}
xSemaphoreTake(xLTESem, portMAX_DELAY);
lteppp_modem_conn_state = E_LTE_MODEM_CONNECTED;
xSemaphoreGive(xLTESem);
lteppp_set_modem_conn_state(E_LTE_MODEM_CONNECTED);
xSemaphoreGive(xLTE_modem_Conn_Sem);
MSG("forever\n");
lte_state_t state;
for (;;) {
vTaskDelay(LTE_TASK_PERIOD_MS);
xSemaphoreTake(xLTESem, portMAX_DELAY);
if(E_LTE_MODEM_DISCONNECTED == lteppp_modem_conn_state)
{
xSemaphoreGive(xLTESem);
if(lteppp_get_modem_conn_state() == E_LTE_MODEM_DISCONNECTED ){
// restart the task
goto modem_init;
}
xSemaphoreGive(xLTESem);
state = lteppp_get_state();
if (xQueueReceive(xCmdQueue, lteppp_trx_buffer, 0)) {
MSG("cmd\n");
Expand Down Expand Up @@ -868,7 +861,7 @@ static void lteppp_print_states(){
if (!xLTESem)
return;
static lte_modem_conn_state_t last_c = 0xff;
lte_modem_conn_state_t c = lteppp_modem_state();
lte_modem_conn_state_t c = lteppp_get_modem_conn_state();
static lte_state_t last_s = 0xff;
lte_state_t s = lteppp_get_state();
static bool last_u = false;
Expand Down
3 changes: 2 additions & 1 deletion esp32/lte/lteppp.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ extern void lteppp_send_at_command (lte_task_cmd_data_t *cmd, lte_task_rsp_data_

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

lte_modem_conn_state_t lteppp_modem_state(void);
lte_modem_conn_state_t lteppp_get_modem_conn_state(void);
void lteppp_set_modem_conn_state(lte_modem_conn_state_t state);

extern void connect_lte_uart (void);

Expand Down
6 changes: 3 additions & 3 deletions esp32/mods/modlte.c
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ static mp_obj_t lte_init_helper(lte_obj_t *self, const mp_arg_val_t *args) {
//printf("All done since we were already initialised.\n");
return mp_const_none;
}
modem_state = lteppp_modem_state();
modem_state = lteppp_get_modem_conn_state();
switch(modem_state)
{
case E_LTE_MODEM_DISCONNECTED:
Expand All @@ -463,15 +463,15 @@ static mp_obj_t lte_init_helper(lte_obj_t *self, const mp_arg_val_t *args) {
MP_THREAD_GIL_EXIT();
xSemaphoreTake(xLTE_modem_Conn_Sem, portMAX_DELAY);
MP_THREAD_GIL_ENTER();
if (E_LTE_MODEM_DISCONNECTED == lteppp_modem_state()) {
if (E_LTE_MODEM_DISCONNECTED == lteppp_get_modem_conn_state()) {
xSemaphoreGive(xLTE_modem_Conn_Sem);
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Couldn't connect to Modem (modem_state=disconnected)"));
}
break;
case E_LTE_MODEM_CONNECTING:
// Block till modem is connected
xSemaphoreTake(xLTE_modem_Conn_Sem, portMAX_DELAY);
if (E_LTE_MODEM_DISCONNECTED == lteppp_modem_state()) {
if (E_LTE_MODEM_DISCONNECTED == lteppp_get_modem_conn_state()) {
xSemaphoreGive(xLTE_modem_Conn_Sem);
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Couldn't connect to Modem (modem_state=connecting)"));
}
Expand Down
4 changes: 2 additions & 2 deletions esp32/mods/modmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ STATIC mp_obj_t machine_sleep (uint n_args, const mp_obj_t *arg) {
bool reconnect = false;

#if defined(FIPY) || defined(GPY)
if (lteppp_modem_state() < E_LTE_MODEM_DISCONNECTED) {
if (lteppp_get_modem_conn_state() < E_LTE_MODEM_DISCONNECTED) {
lteppp_deinit();
}
#endif
Expand Down Expand Up @@ -466,7 +466,7 @@ STATIC mp_obj_t machine_deepsleep (uint n_args, const mp_obj_t *arg) {
modbt_deinit(false);
wlan_deinit(NULL);
#if defined(FIPY) || defined(GPY)
if (lteppp_modem_state() < E_LTE_MODEM_DISCONNECTED) {
if (lteppp_get_modem_conn_state() < E_LTE_MODEM_DISCONNECTED) {
lteppp_deinit();
}
#endif
Expand Down