refactoring LMIC send queueing
This commit is contained in:
parent
1c5edbb6a4
commit
78c8ed3e97
@ -248,6 +248,11 @@ void onEvent(ev_t ev) {
|
|||||||
: PSTR("TX COMPLETE"));
|
: PSTR("TX COMPLETE"));
|
||||||
sprintf(display_line6, " "); // clear previous lmic status
|
sprintf(display_line6, " "); // clear previous lmic status
|
||||||
|
|
||||||
|
// schedule next transmission with some random delay to prevent systematic
|
||||||
|
// collisions
|
||||||
|
os_setTimedCallback(&sendjob, os_getTime() + ms2osticks(random(500)),
|
||||||
|
lora_send);
|
||||||
|
|
||||||
if (LMIC.dataLen) { // did we receive payload data -> display info
|
if (LMIC.dataLen) { // did we receive payload data -> display info
|
||||||
ESP_LOGI(TAG, "Received %d bytes of payload, RSSI %d SNR %d",
|
ESP_LOGI(TAG, "Received %d bytes of payload, RSSI %d SNR %d",
|
||||||
LMIC.dataLen, LMIC.rssi, LMIC.snr / 4);
|
LMIC.dataLen, LMIC.rssi, LMIC.snr / 4);
|
||||||
@ -391,22 +396,22 @@ void switch_lora(uint8_t sf, uint8_t tx) {
|
|||||||
|
|
||||||
void lora_send(osjob_t *job) {
|
void lora_send(osjob_t *job) {
|
||||||
MessageBuffer_t SendBuffer;
|
MessageBuffer_t SendBuffer;
|
||||||
if (xQueueReceive(LoraSendQueue, &SendBuffer, (TickType_t)0) == pdTRUE) {
|
|
||||||
// SendBuffer now filled with next payload from queue
|
// Check if there is not a current TX/RX job running
|
||||||
|
if (LMIC.opmode & OP_TXRXPEND) {
|
||||||
|
ESP_LOGE(TAG, "LMIC busy, data not sent and lost");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// fetch next payload to send from queue or wait until new payload shows up in
|
||||||
|
// queue
|
||||||
|
if (xQueueReceive(LoraSendQueue, &SendBuffer, portMAX_DELAY) == pdTRUE) {
|
||||||
if (LMIC_setTxData2(SendBuffer.MessagePort, SendBuffer.Message,
|
if (LMIC_setTxData2(SendBuffer.MessagePort, SendBuffer.Message,
|
||||||
SendBuffer.MessageSize,
|
SendBuffer.MessageSize, (cfg.countermode & 0x02)) == 0)
|
||||||
(cfg.countermode & 0x02)) == 0) {
|
ESP_LOGI(TAG, "%d byte(s) delivered to LMIC", SendBuffer.MessageSize);
|
||||||
ESP_LOGI(TAG, "%d byte(s) sent to LoRa", SendBuffer.MessageSize);
|
else
|
||||||
} else {
|
lora_enqueuedata(&SendBuffer); // re-enqueue unsent message
|
||||||
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
|
|
||||||
// systematic collisions
|
|
||||||
os_setTimedCallback(job, os_getTime() + 500 + ms2osticks(random(500)),
|
|
||||||
lora_send);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t lora_stack_init() {
|
esp_err_t lora_stack_init() {
|
||||||
@ -438,15 +443,17 @@ esp_err_t lora_stack_init() {
|
|||||||
|
|
||||||
void lora_enqueuedata(MessageBuffer_t *message) {
|
void lora_enqueuedata(MessageBuffer_t *message) {
|
||||||
// enqueue message in LORA send queue
|
// enqueue message in LORA send queue
|
||||||
BaseType_t ret;
|
BaseType_t ret = pdFALSE;
|
||||||
MessageBuffer_t DummyBuffer;
|
MessageBuffer_t DummyBuffer;
|
||||||
sendprio_t prio = message->MessagePrio;
|
sendprio_t prio = message->MessagePrio;
|
||||||
|
|
||||||
switch (prio) {
|
switch (prio) {
|
||||||
case prio_high:
|
case prio_high:
|
||||||
// clear space in queue if full, then fallthrough to normal
|
// clear some space in queue if full, then fallthrough to prio_normal
|
||||||
if (uxQueueSpacesAvailable(LoraSendQueue) == 0)
|
if (uxQueueSpacesAvailable(LoraSendQueue) == 0) {
|
||||||
xQueueReceive(LoraSendQueue, &DummyBuffer, (TickType_t)0);
|
xQueueReceive(LoraSendQueue, &DummyBuffer, (TickType_t)0);
|
||||||
|
ESP_LOGW(TAG, "LORA sendqueue purged, data is lost");
|
||||||
|
}
|
||||||
case prio_normal:
|
case prio_normal:
|
||||||
ret = xQueueSendToFront(LoraSendQueue, (void *)message, (TickType_t)0);
|
ret = xQueueSendToFront(LoraSendQueue, (void *)message, (TickType_t)0);
|
||||||
break;
|
break;
|
||||||
@ -455,7 +462,9 @@ void lora_enqueuedata(MessageBuffer_t *message) {
|
|||||||
ret = xQueueSendToBack(LoraSendQueue, (void *)message, (TickType_t)0);
|
ret = xQueueSendToBack(LoraSendQueue, (void *)message, (TickType_t)0);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (ret != pdTRUE)
|
if (ret == pdTRUE)
|
||||||
|
ESP_LOGD(TAG, "LORA sendqueue data enqueued");
|
||||||
|
else
|
||||||
ESP_LOGW(TAG, "LORA sendqueue is full");
|
ESP_LOGW(TAG, "LORA sendqueue is full");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,7 +40,7 @@ void SendPayload(uint8_t port, sendprio_t prio) {
|
|||||||
default:
|
default:
|
||||||
SendBuffer.MessagePort = port;
|
SendBuffer.MessagePort = port;
|
||||||
}
|
}
|
||||||
memcpy(SendBuffer.Message, payload.getBuffer(), payload.getSize());
|
memcpy(SendBuffer.Message, payload.getBuffer(), SendBuffer.MessageSize);
|
||||||
|
|
||||||
// enqueue message in device's send queues
|
// enqueue message in device's send queues
|
||||||
#if (HAS_LORA)
|
#if (HAS_LORA)
|
||||||
|
Loading…
Reference in New Issue
Block a user