diff --git a/include/globals.h b/include/globals.h index a753d5fb..5ae11780 100644 --- a/include/globals.h +++ b/include/globals.h @@ -41,6 +41,9 @@ #define BLE_MODE (0x40) #define SCREEN_MODE (0x80) +// length of display buffer for lmic event messages +#define LMIC_EVENTMSG_LEN 17 + // I2C bus access control #define I2C_MUTEX_LOCK() \ (xSemaphoreTake(I2Caccess, pdMS_TO_TICKS(10)) == pdTRUE) @@ -104,9 +107,9 @@ extern std::set, Mallocator> macs; extern std::array::iterator it; extern std::array beacons; -extern configData_t cfg; // current device configuration -extern char lmic_event_msg[]; // display buffer -extern uint8_t volatile channel; // wifi channel rotation counter +extern configData_t cfg; // current device configuration +extern char lmic_event_msg[LMIC_EVENTMSG_LEN]; // display buffer +extern uint8_t volatile channel; // wifi channel rotation counter extern uint16_t volatile macs_total, macs_wifi, macs_ble, batt_voltage; // display values extern bool volatile TimePulseTick; // 1sec pps flag set by GPS or RTC diff --git a/include/lorawan.h b/include/lorawan.h index 6d1543d9..4341b397 100644 --- a/include/lorawan.h +++ b/include/lorawan.h @@ -20,7 +20,6 @@ #include #endif -extern QueueHandle_t LoraSendQueue; extern TaskHandle_t lmicTask, lorasendTask; // table of LORAWAN MAC commands diff --git a/src/configmanager.cpp b/src/configmanager.cpp index 41e196e6..0fce294f 100644 --- a/src/configmanager.cpp +++ b/src/configmanager.cpp @@ -179,7 +179,7 @@ void saveConfig() { // set and save cfg.version void migrateVersion() { - sprintf(cfg.version, "%s", PROGVERSION); + snprintf(cfg.version, 10, "%s", PROGVERSION); ESP_LOGI(TAG, "version set to %s", cfg.version); saveConfig(); } diff --git a/src/display.cpp b/src/display.cpp index bf3521d3..364fc9ba 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -15,13 +15,15 @@ Display-Mask (128 x 64 pixel): 4|WIFI:abcde BLTH:abcde SMALL 5|RLIM:abcd Mem:abcdKB SMALL 6|27.Feb 2019 20:27:00* SMALL -7|yyyyyyyyyyyyyyyy SFab SMALL +7|yyyyyyyyyyyyy xx SFab SMALL * = char {L|G|R|?} indicates time source, inverse = clock controller is active, pulsed = pps input signal is active -y = LMIC event message; ab = payload queue length +y = LMIC event message +xx = payload sendqueue length +ab = LMIC spread factor FONT_SMALL: 6x8px = 21 chars / line FONT_NORMAL: 8x8px = 16 chars / line @@ -108,7 +110,7 @@ void init_display(uint8_t verbose) { uint8_t buf[8]; char deveui[17]; os_getDevEui((u1_t *)buf); - sprintf(deveui, "%016llX", *((uint64_t *)&buf)); + snprintf(deveui, 17, "%016llX", *((uint64_t *)&buf)); // display DEVEUI as QR code on the left oledSetContrast(30); @@ -175,7 +177,6 @@ void refreshTheDisplay(bool nextPage) { void draw_page(time_t t, uint8_t page) { char timeState; - uint8_t msgWaiting; #if (HAS_GPS) static bool wasnofix = true; #endif @@ -248,19 +249,11 @@ void draw_page(time_t t, uint8_t page) { // line 7: LORA network status #if (HAS_LORA) - // LMiC event display, display inverse if sendqueue not empty - msgWaiting = uxQueueMessagesWaiting(LoraSendQueue); - if (msgWaiting) - dp_printf(0, 7, FONT_SMALL, 1, "%-16s", lmic_event_msg); - else - dp_printf(0, 7, FONT_SMALL, 0, "%-16s", lmic_event_msg); + // LMiC event display + dp_printf(0, 7, FONT_SMALL, 0, "%-16s", lmic_event_msg); // LORA datarate, display inverse if ADR disabled - if (cfg.adrmode) - dp_printf(100, 7, FONT_SMALL, 0, "%-4s", - getSfName(updr2rps(LMIC.datarate))); - else - dp_printf(100, 7, FONT_SMALL, 1, "%-4s", - getSfName(updr2rps(LMIC.datarate))); + dp_printf(104, 7, FONT_SMALL, !cfg.adrmode, "%-4s", + getSfName(updr2rps(LMIC.datarate))); #endif // HAS_LORA break; // page0 diff --git a/src/lorawan.cpp b/src/lorawan.cpp index 31d324a2..aad3c74d 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -346,8 +346,14 @@ void lora_enqueuedata(MessageBuffer_t *message) { ret = xQueueSendToBack(LoraSendQueue, (void *)message, (TickType_t)0); break; } - if (ret != pdTRUE) + if (ret != pdTRUE) { + snprintf(lmic_event_msg + 14, LMIC_EVENTMSG_LEN - 14, "<>"); ESP_LOGW(TAG, "LORA sendqueue is full"); + } else { + // add Lora send queue length to display + snprintf(lmic_event_msg + 14, LMIC_EVENTMSG_LEN - 14, "%2u", + uxQueueMessagesWaiting(LoraSendQueue)); + } } void lora_queuereset(void) { xQueueReset(LoraSendQueue); } @@ -438,12 +444,15 @@ void myEventCallback(void *pUserData, ev_t ev) { // using message descriptors from LMIC library static const char *const evNames[] = {LMIC_EVENT_NAME_TABLE__INIT}; + // get current length of lora send queue + uint8_t const msgWaiting = uxQueueMessagesWaiting(LoraSendQueue); // get current event message if (ev < sizeof(evNames) / sizeof(evNames[0])) - sprintf(lmic_event_msg, "%s", evNames[ev] + 3); // +3 to strip "EV_" + snprintf(lmic_event_msg, LMIC_EVENTMSG_LEN, "%-16s", + evNames[ev] + 3); // +3 to strip "EV_" else - sprintf(lmic_event_msg, "LMIC event %d", ev); + snprintf(lmic_event_msg, LMIC_EVENTMSG_LEN, "LMIC event %-04u ", ev); // process current event message switch (ev) { @@ -459,12 +468,16 @@ void myEventCallback(void *pUserData, ev_t ev) { case EV_JOIN_TXCOMPLETE: // replace descriptor from library with more descriptive term - lmic_event_msg = "JOIN_WAIT"; + snprintf(lmic_event_msg, LMIC_EVENTMSG_LEN, "%-16s", "JOIN_WAIT"); break; } // print event ESP_LOGD(TAG, "%s", lmic_event_msg); + + // add Lora send queue length to display + if (msgWaiting) + snprintf(lmic_event_msg + 14, LMIC_EVENTMSG_LEN - 14, "%2u", msgWaiting); } // receive message handler diff --git a/src/macsniff.cpp b/src/macsniff.cpp index 0b00e7c1..38a1a991 100644 --- a/src/macsniff.cpp +++ b/src/macsniff.cpp @@ -30,7 +30,7 @@ void printKey(const char *name, const uint8_t *key, uint8_t len, bool lsb) { char keystring[len + 1] = "", keybyte[3]; for (uint8_t i = 0; i < len; i++) { p = lsb ? key + len - i - 1 : key + i; - sprintf(keybyte, "%02X", *p); + snprintf(keybyte, 3, "%02X", *p); strncat(keystring, keybyte, 2); } ESP_LOGI(TAG, "%s: %s", name, keystring); diff --git a/src/main.cpp b/src/main.cpp index 79b692ef..59be9996 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -76,9 +76,9 @@ triggers pps 1 sec impulse // Basic Config #include "main.h" -configData_t cfg; // struct holds current device configuration -char lmic_event_msg[16]; // display buffer for LMIC event message -uint8_t volatile channel = 0; // channel rotation counter +configData_t cfg; // struct holds current device configuration +char lmic_event_msg[LMIC_EVENTMSG_LEN]; // display buffer for LMIC event message +uint8_t volatile channel = 0; // channel rotation counter uint16_t volatile macs_total = 0, macs_wifi = 0, macs_ble = 0, batt_voltage = 0; // globals for display