From 829ab0ea0551ead6abf64f37c3c051499bc9b193 Mon Sep 17 00:00:00 2001 From: Verkehrsrot Date: Wed, 28 Aug 2019 10:48:39 +0200 Subject: [PATCH] bugfix message queuing to LMIC while LMIC is busy --- include/globals.h | 7 ++++--- include/lorawan.h | 2 +- include/spislave.h | 2 +- src/lorawan.cpp | 29 +++++++++++++---------------- src/senddata.cpp | 8 +++++--- src/spislave.cpp | 4 +++- 6 files changed, 27 insertions(+), 25 deletions(-) diff --git a/include/globals.h b/include/globals.h index e8e8bb48..a112e481 100644 --- a/include/globals.h +++ b/include/globals.h @@ -45,6 +45,9 @@ #define I2C_MUTEX_LOCK() (xSemaphoreTake(I2Caccess, pdMS_TO_TICKS(10)) == pdTRUE) #define I2C_MUTEX_UNLOCK() (xSemaphoreGive(I2Caccess)) +enum sendprio_t { prio_low, prio_normal, prio_high }; +enum timesource_t { _gps, _rtc, _lora, _unsynced }; + // Struct holding devices's runtime configuration typedef struct { uint8_t lorasf; // 7-12, lora spreadfactor @@ -73,6 +76,7 @@ typedef struct { typedef struct { uint8_t MessageSize; uint8_t MessagePort; + sendprio_t MessagePrio; uint8_t Message[PAYLOAD_BUFFER_SIZE]; } MessageBuffer_t; @@ -95,9 +99,6 @@ typedef struct { float gas; // raw gas sensor signal } bmeStatus_t; -enum sendprio_t { prio_low, prio_normal, prio_high }; -enum timesource_t { _gps, _rtc, _lora, _unsynced }; - extern std::set, Mallocator> macs; extern std::array::iterator it; extern std::array beacons; diff --git a/include/lorawan.h b/include/lorawan.h index b3331751..5762297c 100644 --- a/include/lorawan.h +++ b/include/lorawan.h @@ -35,7 +35,7 @@ void os_getDevEui(u1_t *buf); void showLoraKeys(void); void switch_lora(uint8_t sf, uint8_t tx); void lora_send(osjob_t *job); -void lora_enqueuedata(MessageBuffer_t *message, sendprio_t prio); +void lora_enqueuedata(MessageBuffer_t *message); void lora_queuereset(void); #if (TIME_SYNC_LORAWAN) void user_request_network_time_callback(void *pVoidUserUTCTime, diff --git a/include/spislave.h b/include/spislave.h index 44c58455..0d85a09e 100644 --- a/include/spislave.h +++ b/include/spislave.h @@ -28,7 +28,7 @@ licenses. Refer to LICENSE.txt file in repository for more details. esp_err_t spi_init(); -void spi_enqueuedata(MessageBuffer_t *message, sendprio_t prio); +void spi_enqueuedata(MessageBuffer_t *message); void spi_queuereset(); #endif // _SPISLAVE_H diff --git a/src/lorawan.cpp b/src/lorawan.cpp index e82ac1f5..3974c7f4 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -391,21 +391,16 @@ void switch_lora(uint8_t sf, uint8_t tx) { void lora_send(osjob_t *job) { MessageBuffer_t SendBuffer; - // Check if there is a pending TX/RX job running, if yes don't eat data - // since it cannot be sent right now - if ((LMIC.opmode & (OP_JOINING | OP_REJOIN | OP_TXDATA | OP_POLL)) != 0) { - // waiting for LoRa getting ready - } else { - if (xQueueReceive(LoraSendQueue, &SendBuffer, (TickType_t)0) == pdTRUE) { - // SendBuffer now filled with next payload from queue - if (!LMIC_setTxData2(SendBuffer.MessagePort, SendBuffer.Message, - SendBuffer.MessageSize, (cfg.countermode & 0x02))) { - ESP_LOGI(TAG, "%d byte(s) sent to LoRa", SendBuffer.MessageSize); - } else { - ESP_LOGE(TAG, "could not send %d byte(s) to LoRa", - SendBuffer.MessageSize); - } - // sprintf(display_line7, "PACKET QUEUED"); + if (xQueueReceive(LoraSendQueue, &SendBuffer, (TickType_t)0) == pdTRUE) { + // SendBuffer now filled with next payload from queue + if (LMIC_setTxData2(SendBuffer.MessagePort, SendBuffer.Message, + SendBuffer.MessageSize, + (cfg.countermode & 0x02)) == 0) { + ESP_LOGI(TAG, "%d byte(s) sent to LoRa", SendBuffer.MessageSize); + } else { + lora_enqueuedata(&SendBuffer); + // ESP_LOGE(TAG, "could not send %d byte(s) to LoRa, rescheduled", + // SendBuffer.MessageSize); } } // reschedule job every 0,5 - 1 sec. including a bit of random to prevent @@ -441,10 +436,12 @@ esp_err_t lora_stack_init() { return ESP_OK; // continue main program } -void lora_enqueuedata(MessageBuffer_t *message, sendprio_t prio) { +void lora_enqueuedata(MessageBuffer_t *message) { // enqueue message in LORA send queue BaseType_t ret; MessageBuffer_t DummyBuffer; + sendprio_t prio = message->MessagePrio; + switch (prio) { case prio_high: // clear space in queue if full, then fallthrough to normal diff --git a/src/senddata.cpp b/src/senddata.cpp index fd5df963..95577b24 100644 --- a/src/senddata.cpp +++ b/src/senddata.cpp @@ -10,9 +10,11 @@ void sendcycle() { // put data to send in RTos Queues used for transmit over channels Lora and SPI void SendPayload(uint8_t port, sendprio_t prio) { - MessageBuffer_t SendBuffer; // contains MessageSize, MessagePort, Message[] + MessageBuffer_t SendBuffer; // contains MessageSize, MessagePort, MessagePrio, Message[] SendBuffer.MessageSize = payload.getSize(); + SendBuffer.MessagePrio = prio; + switch (PAYLOAD_ENCODER) { case 1: // plain -> no mapping case 2: // packed -> no mapping @@ -42,10 +44,10 @@ void SendPayload(uint8_t port, sendprio_t prio) { // enqueue message in device's send queues #if (HAS_LORA) - lora_enqueuedata(&SendBuffer, prio); + lora_enqueuedata(&SendBuffer); #endif #ifdef HAS_SPI - spi_enqueuedata(&SendBuffer, prio); + spi_enqueuedata(&SendBuffer); #endif } // SendPayload diff --git a/src/spislave.cpp b/src/spislave.cpp index 9801058b..32bad108 100644 --- a/src/spislave.cpp +++ b/src/spislave.cpp @@ -147,10 +147,12 @@ esp_err_t spi_init() { return ret; } -void spi_enqueuedata(MessageBuffer_t *message, sendprio_t prio) { +void spi_enqueuedata(MessageBuffer_t *message) { // enqueue message in SPI send queue BaseType_t ret; MessageBuffer_t DummyBuffer; + sendprio_t prio = message->MessagePrio; + switch (prio) { case prio_high: // clear space in queue if full, then fallthrough to normal