From 3df387a207206d6969262f2bd911d558dd2e7e2e Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Sun, 20 Nov 2022 17:17:07 +0100 Subject: [PATCH] reset.cpp lora code sanitization --- include/lorawan.h | 1 + src/lorawan.cpp | 12 ++++++++++++ src/reset.cpp | 14 +++----------- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/include/lorawan.h b/include/lorawan.h index a8e7e588..8e92c7ef 100644 --- a/include/lorawan.h +++ b/include/lorawan.h @@ -37,6 +37,7 @@ void os_getDevEui(u1_t *buf); void lora_send(void *pvParameters); void lora_enqueuedata(MessageBuffer_t *message); void lora_queuereset(void); +void lora_waitforidle(uint16_t timeout_sec); uint32_t lora_queuewaiting(void); void myEventCallback(void *pUserData, ev_t ev); void myRxCallback(void *pUserData, uint8_t port, const uint8_t *pMsg, diff --git a/src/lorawan.cpp b/src/lorawan.cpp index 23c73ebf..58ec02ad 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -334,6 +334,18 @@ uint32_t lora_queuewaiting(void) { return uxQueueMessagesWaiting(LoraSendQueue); } +// blocking wait until LMIC is idle +void lora_waitforidle(uint16_t timeout_sec) { + ESP_LOGI(TAG, "Waiting until LMIC is idle..."); + for (int i = timeout_sec; i > 0; i--) { + if ((LMIC.opmode & (OP_JOINING | OP_TXDATA | OP_POLL | OP_TXRXPEND)) || + os_queryTimeCriticalJobs(sec2osticks(timeout_sec))) + vTaskDelay(pdMS_TO_TICKS(1000)); + else + break; + } +} + // LMIC loop task void lmictask(void *pvParameters) { _ASSERT((uint32_t)pvParameters == 1); diff --git a/src/reset.cpp b/src/reset.cpp index 32843f0a..93ee4f45 100644 --- a/src/reset.cpp +++ b/src/reset.cpp @@ -88,7 +88,6 @@ void enter_deepsleep(const uint32_t wakeup_sec, gpio_num_t wakeup_gpio) { ESP_LOGI(TAG, "Preparing to sleep..."); RTC_runmode = RUNMODE_SLEEP; - int i; // validate wake up pin, if we have if (!GPIO_IS_VALID_GPIO(wakeup_gpio)) @@ -108,22 +107,15 @@ void enter_deepsleep(const uint32_t wakeup_sec, gpio_num_t wakeup_gpio) { // wait a while (max 100 sec) to clear send queues ESP_LOGI(TAG, "Waiting until send queues are empty..."); - for (i = 100; i > 0; i--) { + for (int i = 100; i > 0; i--) { if (allQueuesEmtpy()) break; vTaskDelay(pdMS_TO_TICKS(1000)); } -/// wait until LMIC is idle +// wait up to 100secs until LMIC is idle #if (HAS_LORA) - ESP_LOGI(TAG, "Waiting until LMIC is idle..."); - for (i = 100; i > 0; i--) { - if ((LMIC.opmode & (OP_JOINING | OP_TXDATA | OP_POLL | OP_TXRXPEND)) || - os_queryTimeCriticalJobs(sec2osticks(wakeup_sec))) - vTaskDelay(pdMS_TO_TICKS(1000)); - else - break; - } + lora_waitforidle(100); #endif // (HAS_LORA) // shutdown MQTT safely