diff --git a/include/globals.h b/include/globals.h index 1e8f43d0..73c11287 100644 --- a/include/globals.h +++ b/include/globals.h @@ -92,13 +92,15 @@ typedef struct { float gas; // raw gas sensor signal } bmeStatus_t; +enum sendprio_t { prio_low, prio_normal, prio_high }; + // global variables extern configData_t cfg; // current device configuration extern char display_line6[], display_line7[]; // screen buffers extern uint8_t volatile channel; // wifi channel rotation counter extern uint16_t volatile macs_total, macs_wifi, macs_ble, batt_voltage; // display values -extern hw_timer_t *channelSwitch, *sendCycle, *displaytimer; +extern hw_timer_t *sendCycle, *displaytimer; extern SemaphoreHandle_t I2Caccess; extern std::set, Mallocator> macs; @@ -150,4 +152,8 @@ extern Timezone myTZ; #include "if482.h" #endif +#ifdef HAS_DCF77 +#include "dcf77.h" +#endif + #endif \ No newline at end of file diff --git a/include/lorawan.h b/include/lorawan.h index 05aba7cd..4beee6c4 100644 --- a/include/lorawan.h +++ b/include/lorawan.h @@ -33,7 +33,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); +void lora_enqueuedata(MessageBuffer_t *message, sendprio_t prio); void lora_queuereset(void); void lora_housekeeping(void); void user_request_network_time_callback(void *pVoidUserUTCTime, diff --git a/include/senddata.h b/include/senddata.h index da368f71..82cf4581 100644 --- a/include/senddata.h +++ b/include/senddata.h @@ -5,7 +5,7 @@ #include "lorawan.h" #include "cyclic.h" -void SendPayload(uint8_t port); +void SendPayload(uint8_t port, sendprio_t prio); void sendCounter(void); void checkSendQueues(void); void flushQueues(); diff --git a/include/spislave.h b/include/spislave.h index f459b2fd..74b77db0 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); +void spi_enqueuedata(MessageBuffer_t *message, sendprio_t prio); void spi_queuereset(); void spi_housekeeping(); diff --git a/src/button.cpp b/src/button.cpp index 29eff252..d7d5c2a4 100644 --- a/src/button.cpp +++ b/src/button.cpp @@ -10,6 +10,6 @@ void readButton() { ESP_LOGI(TAG, "Button pressed"); payload.reset(); payload.addButton(0x01); - SendPayload(BUTTONPORT); + SendPayload(BUTTONPORT, prio_normal); } #endif \ No newline at end of file diff --git a/src/cyclic.cpp b/src/cyclic.cpp index 93c200d7..67fcc99c 100644 --- a/src/cyclic.cpp +++ b/src/cyclic.cpp @@ -85,7 +85,7 @@ void doHousekeeping() { "Memory full, counter cleared (heap low water mark = %d Bytes / " "free heap = %d bytes)", ESP.getMinFreeHeap(), ESP.getFreeHeap()); - SendPayload(COUNTERPORT); // send data before clearing counters + SendPayload(COUNTERPORT, prio_high); // send data before clearing counters reset_counters(); // clear macs container and reset all counters get_salt(); // get new salt for salting hashes @@ -97,7 +97,7 @@ void doHousekeeping() { #ifdef BOARD_HAS_PSRAM if (ESP.getMinFreePsram() <= MEM_LOW) { ESP_LOGI(TAG, "PSRAM full, counter cleared"); - SendPayload(COUNTERPORT); // send data before clearing counters + SendPayload(COUNTERPORT, prio_high); // send data before clearing counters reset_counters(); // clear macs container and reset all counters get_salt(); // get new salt for salting hashes diff --git a/src/lorawan.cpp b/src/lorawan.cpp index 77c08c76..bc8565de 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -358,6 +358,7 @@ esp_err_t lora_stack_init() { #ifndef HAS_LORA return ESP_OK; // continue main program #else + assert(SEND_QUEUE_SIZE); LoraSendQueue = xQueueCreate(SEND_QUEUE_SIZE, sizeof(MessageBuffer_t)); if (LoraSendQueue == 0) { ESP_LOGE(TAG, "Could not create LORA send queue. Aborting."); @@ -397,11 +398,20 @@ esp_err_t lora_stack_init() { #endif } -void lora_enqueuedata(MessageBuffer_t *message) { +void lora_enqueuedata(MessageBuffer_t *message, sendprio_t prio) { // enqueue message in LORA send queue #ifdef HAS_LORA - BaseType_t ret = - xQueueSendToBack(LoraSendQueue, (void *)message, (TickType_t)0); + BaseType_t ret; + switch (prio) { + case prio_high: + ret = xQueueSendToFront(LoraSendQueue, (void *)message, (TickType_t)0); + break; + case prio_low: + case prio_normal: + default: + ret = xQueueSendToBack(LoraSendQueue, (void *)message, (TickType_t)0); + break; + } if (ret == pdTRUE) { ESP_LOGI(TAG, "%d bytes enqueued for LORA interface", message->MessageSize); } else { diff --git a/src/macsniff.cpp b/src/macsniff.cpp index b1e5ffcc..5cc02c64 100644 --- a/src/macsniff.cpp +++ b/src/macsniff.cpp @@ -107,7 +107,7 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) { #endif payload.reset(); payload.addAlarm(rssi, beaconID); - SendPayload(BEACONPORT); + SendPayload(BEACONPORT, prio_high); } }; diff --git a/src/paxcounter.conf b/src/paxcounter.conf index 1c3c5618..9ca0766e 100644 --- a/src/paxcounter.conf +++ b/src/paxcounter.conf @@ -47,7 +47,7 @@ #define PAYLOAD_BUFFER_SIZE 51 // maximum size of payload block per transmit #define LORASFDEFAULT 9 // 7 ... 12 SF, according to LoRaWAN specs #define MAXLORARETRY 500 // maximum count of TX retries if LoRa busy -#define SEND_QUEUE_SIZE 10 // maximum number of messages in payload send queue +#define SEND_QUEUE_SIZE 10 // maximum number of messages in payload send queue [1 = no queue] // Ports on which the device sends and listenes on LoRaWAN and SPI #define COUNTERPORT 1 // Port on which device sends counts diff --git a/src/rcommand.cpp b/src/rcommand.cpp index 90a2e20f..038db7db 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -233,7 +233,7 @@ void get_config(uint8_t val[]) { ESP_LOGI(TAG, "Remote command: get device configuration"); payload.reset(); payload.addConfig(cfg); - SendPayload(CONFIGPORT); + SendPayload(CONFIGPORT, prio_high); }; void get_status(uint8_t val[]) { @@ -247,7 +247,7 @@ void get_status(uint8_t val[]) { payload.addStatus(voltage, uptime() / 1000, temperatureRead(), getFreeRAM(), rtc_get_reset_reason(0), rtc_get_reset_reason(1)); - SendPayload(STATUSPORT); + SendPayload(STATUSPORT, prio_high); }; void get_gps(uint8_t val[]) { @@ -256,7 +256,7 @@ void get_gps(uint8_t val[]) { gps_read(); payload.reset(); payload.addGPS(gps_status); - SendPayload(GPSPORT); + SendPayload(GPSPORT, prio_high); #else ESP_LOGW(TAG, "GPS function not supported"); #endif @@ -267,7 +267,7 @@ void get_bme(uint8_t val[]) { #ifdef HAS_BME payload.reset(); payload.addBME(bme_status); - SendPayload(BMEPORT); + SendPayload(BMEPORT, prio_high); #else ESP_LOGW(TAG, "BME680 sensor not supported"); #endif diff --git a/src/senddata.cpp b/src/senddata.cpp index fe03be25..c13d907f 100644 --- a/src/senddata.cpp +++ b/src/senddata.cpp @@ -2,7 +2,7 @@ #include "senddata.h" // put data to send in RTos Queues used for transmit over channels Lora and SPI -void SendPayload(uint8_t port) { +void SendPayload(uint8_t port, sendprio_t prio) { MessageBuffer_t SendBuffer; // contains MessageSize, MessagePort, Message[] @@ -24,8 +24,8 @@ void SendPayload(uint8_t port) { memcpy(SendBuffer.Message, payload.getBuffer(), payload.getSize()); // enqueue message in device's send queues - lora_enqueuedata(&SendBuffer); - spi_enqueuedata(&SendBuffer); + lora_enqueuedata(&SendBuffer, prio); + spi_enqueuedata(&SendBuffer, prio); } // SendPayload @@ -55,7 +55,7 @@ void sendCounter() { } #endif - SendPayload(COUNTERPORT); + SendPayload(COUNTERPORT, prio_normal); // clear counter if not in cumulative counter mode if (cfg.countermode != 1) { reset_counters(); // clear macs container and reset all counters @@ -68,7 +68,7 @@ void sendCounter() { case MEMS_DATA: payload.reset(); payload.addBME(bme_status); - SendPayload(BMEPORT); + SendPayload(BMEPORT, prio_normal); break; #endif @@ -79,7 +79,7 @@ void sendCounter() { gps_read(); payload.reset(); payload.addGPS(gps_status); - SendPayload(GPSPORT); + SendPayload(GPSPORT, prio_high); } else ESP_LOGD(TAG, "No valid GPS position"); break; @@ -89,17 +89,17 @@ void sendCounter() { case SENSOR1_DATA: payload.reset(); payload.addSensor(sensor_read(1)); - SendPayload(SENSOR1PORT); + SendPayload(SENSOR1PORT, prio_normal); break; case SENSOR2_DATA: payload.reset(); payload.addSensor(sensor_read(2)); - SendPayload(SENSOR2PORT); + SendPayload(SENSOR2PORT, prio_normal); break; case SENSOR3_DATA: payload.reset(); payload.addSensor(sensor_read(3)); - SendPayload(SENSOR3PORT); + SendPayload(SENSOR3PORT, prio_normal); break; #endif @@ -107,7 +107,7 @@ void sendCounter() { case BATT_DATA: payload.reset(); payload.addVoltage(read_voltage()); - SendPayload(BATTPORT); + SendPayload(BATTPORT, prio_normal); break; #endif diff --git a/src/spislave.cpp b/src/spislave.cpp index 5e9ebb1c..5f6a1b60 100644 --- a/src/spislave.cpp +++ b/src/spislave.cpp @@ -66,7 +66,8 @@ void spi_slave_task(void *param) { uint8_t *messageSize = txbuf + 3; *messageSize = msg.MessageSize; memcpy(txbuf + HEADER_SIZE, &msg.Message, msg.MessageSize); - // calculate crc16 checksum over txbuf and insert checksum at pos 0+1 of txbuf + // calculate crc16 checksum over txbuf and insert checksum at pos 0+1 of + // txbuf uint16_t *crc = (uint16_t *)txbuf; *crc = crc16_be(0, messageType, msg.MessageSize + HEADER_SIZE - 2); @@ -87,7 +88,8 @@ void spi_slave_task(void *param) { // wait until spi master clocks out the data, and read results in rx buffer ESP_LOGI(TAG, "Prepared SPI transaction for %zu byte(s)", transaction_size); ESP_LOG_BUFFER_HEXDUMP(TAG, txbuf, transaction_size, ESP_LOG_DEBUG); - ESP_ERROR_CHECK_WITHOUT_ABORT(spi_slave_transmit(HSPI_HOST, &spi_transaction, portMAX_DELAY)); + ESP_ERROR_CHECK_WITHOUT_ABORT( + spi_slave_transmit(HSPI_HOST, &spi_transaction, portMAX_DELAY)); ESP_LOG_BUFFER_HEXDUMP(TAG, rxbuf, transaction_size, ESP_LOG_DEBUG); ESP_LOGI(TAG, "Transaction finished with size %zu bits", spi_transaction.trans_len); @@ -103,7 +105,7 @@ esp_err_t spi_init() { #ifndef HAS_SPI return ESP_OK; #else - + assert(SEND_QUEUE_SIZE); SPISendQueue = xQueueCreate(SEND_QUEUE_SIZE, sizeof(MessageBuffer_t)); if (SPISendQueue == 0) { ESP_LOGE(TAG, "Could not create SPI send queue. Aborting."); @@ -148,11 +150,20 @@ esp_err_t spi_init() { #endif } -void spi_enqueuedata(MessageBuffer_t *message) { +void spi_enqueuedata(MessageBuffer_t *message, sendprio_t prio) { // enqueue message in SPI send queue #ifdef HAS_SPI - BaseType_t ret = - xQueueSendToBack(SPISendQueue, (void *)message, (TickType_t)0); + BaseType_t ret; + switch (prio) { + case prio_high: + ret = xQueueSendToFront(SPISendQueue, (void *)message, (TickType_t)0); + break; + case prio_low: + case prio_normal: + default: + ret = xQueueSendToBack(SPISendQueue, (void *)message, (TickType_t)0); + break; + } if (ret == pdTRUE) { ESP_LOGI(TAG, "%d byte(s) enqueued for SPI interface", message->MessageSize);