From 95983bd3fcb2c3ac2c678bb4cd9930d41d11ff2f Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sun, 5 Aug 2018 12:16:54 +0200 Subject: [PATCH] rcommand flush send queue added --- README.md | 1 + src/lorawan.cpp | 47 ++++++++++++++++++ src/lorawan.h | 1 + src/rcommand.cpp | 57 +++------------------- src/senddata.cpp | 124 ++--------------------------------------------- src/senddata.h | 1 + 6 files changed, 61 insertions(+), 170 deletions(-) diff --git a/README.md b/README.md index c4f3fe02..92375698 100644 --- a/README.md +++ b/README.md @@ -277,6 +277,7 @@ Note: all settings are stored in NVRAM and will be reloaded when device starts. 0 = restart device 1 = reset MAC counter to zero 2 = reset device to factory settings + 3 = flush send queues 0x0A set LoRaWAN payload send cycle diff --git a/src/lorawan.cpp b/src/lorawan.cpp index 41c386e9..2337ec1f 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -228,4 +228,51 @@ void lorawan_loop(void *pvParameters) { } } +// helper function to assign LoRa datarates to numeric spreadfactor values +void switch_lora(uint8_t sf, uint8_t tx) { + if (tx > 20) + return; + cfg.txpower = tx; + switch (sf) { + case 7: + LMIC_setDrTxpow(DR_SF7, tx); + cfg.lorasf = sf; + break; + case 8: + LMIC_setDrTxpow(DR_SF8, tx); + cfg.lorasf = sf; + break; + case 9: + LMIC_setDrTxpow(DR_SF9, tx); + cfg.lorasf = sf; + break; + case 10: + LMIC_setDrTxpow(DR_SF10, tx); + cfg.lorasf = sf; + break; + case 11: +#if defined(CFG_eu868) + LMIC_setDrTxpow(DR_SF11, tx); + cfg.lorasf = sf; + break; +#elif defined(CFG_us915) + LMIC_setDrTxpow(DR_SF11CR, tx); + cfg.lorasf = sf; + break; +#endif + case 12: +#if defined(CFG_eu868) + LMIC_setDrTxpow(DR_SF12, tx); + cfg.lorasf = sf; + break; +#elif defined(CFG_us915) + LMIC_setDrTxpow(DR_SF12CR, tx); + cfg.lorasf = sf; + break; +#endif + default: + break; + } +} + #endif // HAS_LORA \ No newline at end of file diff --git a/src/lorawan.h b/src/lorawan.h index b9abb8c1..d88062e7 100644 --- a/src/lorawan.h +++ b/src/lorawan.h @@ -15,5 +15,6 @@ void os_getArtEui(u1_t *buf); void os_getDevEui(u1_t *buf); void showLoraKeys(void); void lorawan_loop(void *pvParameters); +void switch_lora(uint8_t sf, uint8_t tx); #endif \ No newline at end of file diff --git a/src/rcommand.cpp b/src/rcommand.cpp index 7c9d7d40..f925f1da 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -8,55 +8,6 @@ // Local logging tag static const char TAG[] = "main"; -#ifdef HAS_LORA -// helper function to assign LoRa datarates to numeric spreadfactor values -void switch_lora(uint8_t sf, uint8_t tx) { - if (tx > 20) - return; - cfg.txpower = tx; - switch (sf) { - case 7: - LMIC_setDrTxpow(DR_SF7, tx); - cfg.lorasf = sf; - break; - case 8: - LMIC_setDrTxpow(DR_SF8, tx); - cfg.lorasf = sf; - break; - case 9: - LMIC_setDrTxpow(DR_SF9, tx); - cfg.lorasf = sf; - break; - case 10: - LMIC_setDrTxpow(DR_SF10, tx); - cfg.lorasf = sf; - break; - case 11: -#if defined(CFG_eu868) - LMIC_setDrTxpow(DR_SF11, tx); - cfg.lorasf = sf; - break; -#elif defined(CFG_us915) - LMIC_setDrTxpow(DR_SF11CR, tx); - cfg.lorasf = sf; - break; -#endif - case 12: -#if defined(CFG_eu868) - LMIC_setDrTxpow(DR_SF12, tx); - cfg.lorasf = sf; - break; -#elif defined(CFG_us915) - LMIC_setDrTxpow(DR_SF12CR, tx); - cfg.lorasf = sf; - break; -#endif - default: - break; - } -} -#endif // HAS_LORA - // set of functions that can be triggered by remote commands void set_reset(uint8_t val[]) { switch (val[0]) { @@ -78,6 +29,11 @@ void set_reset(uint8_t val[]) { sprintf(display_line6, "Factory reset"); eraseConfig(); break; + case 3: // reset send queues + ESP_LOGI(TAG, "Remote command: flush send queue"); + sprintf(display_line6, "Flush queues"); + flushQueues(); + break; default: ESP_LOGW(TAG, "Remote command: reset called with invalid parameter(s)"); } @@ -304,7 +260,8 @@ void get_status(uint8_t val[]) { uint16_t voltage = 0; #endif payload.reset(); - payload.addStatus(voltage, uptime() / 1000, temperatureRead(), ESP.getFreeHeap()); + payload.addStatus(voltage, uptime() / 1000, temperatureRead(), + ESP.getFreeHeap()); SendData(STATUSPORT); }; diff --git a/src/senddata.cpp b/src/senddata.cpp index bf811fab..d4bced41 100644 --- a/src/senddata.cpp +++ b/src/senddata.cpp @@ -102,127 +102,11 @@ void processSendBuffer() { } // processSendBuffer -/* old version with pointers - -// Basic Config -#include "globals.h" - -// put data to send in RTos Queues used for transmit over channels Lora and SPI -void SendData(uint8_t port, uint8_t data[], uint8_t size) { - - MessageBuffer_t *xMsg = &SendBuffer; - - SendBuffer.MessageSize = size; - SendBuffer.MessagePort = PAYLOAD_ENCODER <= 2 - ? port - : (PAYLOAD_ENCODER == 4 ? LPP2PORT : LPP1PORT); - memcpy(SendBuffer.Message, data, size); - - // enqueue message in LoRa send queue +void flushQueues() { #ifdef HAS_LORA - if (uxQueueSpacesAvailable(LoraSendQueue)) { - xQueueSend(LoraSendQueue, (void *)&xMsg, (TickType_t)0); - ESP_LOGI(TAG, "%d bytes enqueued to send on LoRa", size); - }; + xQueueReset(LoraSendQueue); #endif - -// enqueue message in SPI send queue #ifdef HAS_SPI - if (uxQueueSpacesAvailable(SPISendQueue)) { - xQueueSend(SPISendQueue, (void *)&xMsg, (TickType_t)0); - ESP_LOGI(TAG, "%d bytes enqueued to send on SPI", size); - }; + xQueueReset(SPISendQueue); #endif - - // clear counter if not in cumulative counter mode - if ((port == COUNTERPORT) && (cfg.countermode != 1)) { - reset_counters(); // clear macs container and reset all counters - reset_salt(); // get new salt for salting hashes - ESP_LOGI(TAG, "Counter cleared"); - } - - ESP_LOGI(TAG, "%d Bytes left", ESP.getFreeHeap()); - -} // senddata - -// cyclic called function to prepare payload to send -void sendPayload() { - if (SendCycleTimerIRQ) { - portENTER_CRITICAL(&timerMux); - SendCycleTimerIRQ = 0; - portEXIT_CRITICAL(&timerMux); - - // append counter data to payload - payload.reset(); - payload.addCount(macs_wifi, cfg.blescan ? macs_ble : 0); - // append GPS data, if present - -#ifdef HAS_GPS - // show NMEA data in debug mode, useful for debugging GPS on board - // connection - ESP_LOGD(TAG, "GPS NMEA data: passed %d / failed: %d / with fix: %d", - gps.passedChecksum(), gps.failedChecksum(), - gps.sentencesWithFix()); - // log GPS position if we have a fix and gps data mode is enabled - if ((cfg.gpsmode) && (gps.location.isValid())) { - gps_read(); - payload.addGPS(gps_status); - ESP_LOGD(TAG, "lat=%.6f | lon=%.6f | %u Sats | HDOP=%.1f | Altitude=%um", - gps_status.latitude / (float)1e6, - gps_status.longitude / (float)1e6, gps_status.satellites, - gps_status.hdop / (float)100, gps_status.altitude); - } else { - ESP_LOGD(TAG, "No valid GPS position or GPS data mode disabled"); - } -#endif - SendData(COUNTERPORT, payload.getBuffer(), payload.getSize()); - } -} // sendpayload() - -// interrupt handler used for payload send cycle timer -void IRAM_ATTR SendCycleIRQ() { - portENTER_CRITICAL(&timerMux); - SendCycleTimerIRQ++; - portEXIT_CRITICAL(&timerMux); -} - -// cyclic called function to eat data from RTos send queues and transmit it -void processSendBuffer() { - - MessageBuffer_t *xMsg; - -#ifdef HAS_LORA - // Check if there is a pending TX/RX job running - if ((LMIC.opmode & (OP_JOINING | OP_REJOIN | OP_TXDATA | OP_POLL)) != 0) { - // LoRa Busy -> don't eat data from queue, since it cannot be sent - } else { - if (uxQueueMessagesWaiting(LoraSendQueue)) // check if msg are waiting on -queue if (xQueueReceive(LoraSendQueue, &xMsg, (TickType_t)10)) { - // xMsg now holds the struct MessageBuffer from queue - LMIC_setTxData2(xMsg->MessagePort, xMsg->Message, xMsg->MessageSize, - (cfg.countermode & 0x02)); - ESP_LOGI(TAG, "%d bytes sent to LORA", xMsg->MessageSize); - sprintf(display_line7, "PACKET QUEUED"); - } - } -#endif - -#ifdef HAS_SPI - if (uxQueueMessagesWaiting(SPISendQueue)) // check if msg are waiting on queue - if (xQueueReceive(SPISendQueue, &xMsg, (TickType_t)10)) { - - // to come here: send data over SPI - // use these pointers to the payload: - // xMsg->MessagePort - // xMsg->MessageSize - // xMsg->Message - - ESP_LOGI(TAG, "%d bytes sent to SPI", xMsg->MessageSize); - } -#endif - - ESP_LOGI(TAG, "%d Bytes left", ESP.getFreeHeap()); - -} // processSendBuffer - -*/ \ No newline at end of file +} \ No newline at end of file diff --git a/src/senddata.h b/src/senddata.h index 07fc477d..e709cedc 100644 --- a/src/senddata.h +++ b/src/senddata.h @@ -12,5 +12,6 @@ void SendData(uint8_t port); void sendPayload(void); void SendCycleIRQ(void); void processSendBuffer(void); +void flushQueues(); #endif // _SENDDATA_H_ \ No newline at end of file