From dda5568e2c688c5f815941b820645789176e080d Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Fri, 25 Dec 2020 11:14:00 +0800 Subject: [PATCH 1/5] update arduino lib & esp bins to fix #584 --- docs/software/esp32-arduino-build-notes.md | 8 +++++++- platformio.ini | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/docs/software/esp32-arduino-build-notes.md b/docs/software/esp32-arduino-build-notes.md index 94b64f621..1b5a5da0b 100644 --- a/docs/software/esp32-arduino-build-notes.md +++ b/docs/software/esp32-arduino-build-notes.md @@ -10,7 +10,7 @@ you'll automatically get our fixed libraries. IDF release/v3.3 46b12a560 IDF release/v3.3 367c3c09c https://docs.espressif.com/projects/esp-idf/en/release-v3.3/get-started/linux-setup.html - kevinh@kevin-server:~/development/meshtastic/esp32-arduino-lib-builder\$ python /home/kevinh/development/meshtastic/esp32-arduino-lib-builder/esp-idf/components/esptool*py/esptool/esptool.py --chip esp32 --port /dev/ttyUSB0 --baud 921600 --before default_reset --after hard_reset write_flash -z --flash_mode dout --flash_freq 40m --flash_size detect 0x1000 /home/kevinh/development/meshtastic/esp32-arduino-lib-builder/build/bootloader/bootloader.bin + kevinh@kevin-server:~/development/meshtastic/esp32-arduino-lib-builder\$ python /home/kevinh/development/meshtastic/ cp -a out/tools/sdk/* components/arduino/tools/sdk cp -ar components/arduino/* ~/.platformio/packages/framework-arduinoespressif32 @@ -21,3 +21,9 @@ you'll automatically get our fixed libraries. cp -ar out/tools/sdk/* ~/.platformio/packages/framework-arduinoespressif32/tools/sdk ``` + +How to flash new bootloader + +``` +esp32-arduino-lib-builder/esp-idf/components/esptool*py/esptool/esptool.py --chip esp32 --port /dev/ttyUSB0 --baud 921600 --before default_reset --after hard_reset write_flash -z --flash_mode dout --flash_freq 40m --flash_size detect 0x1000 /home/kevinh/development/meshtastic/esp32-arduino-lib-builder/build/bootloader/bootloader.bin +``` diff --git a/platformio.ini b/platformio.ini index 327c5d6ca..50d381c8b 100644 --- a/platformio.ini +++ b/platformio.ini @@ -103,7 +103,7 @@ lib_deps = # board_build.ldscript = linker/esp32.extram.bss.ld lib_ignore = segger_rtt platform_packages = - framework-arduinoespressif32@https://github.com/meshtastic/arduino-esp32.git#c29e0bcde1b1b4a939dd7339dea0302d2d589ae7 + framework-arduinoespressif32@https://github.com/meshtastic/arduino-esp32.git#352c8ea7cb73f10433ed139f34251979c470ad56 ; customize the partition table ; http://docs.platformio.org/en/latest/platforms/espressif32.html#partition-tables From c349ad62e73f9ad314e086806802c012ba9e414d Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Fri, 25 Dec 2020 14:53:33 +0800 Subject: [PATCH 2/5] we set randomSeed at boot so I think probably not good to do again cool @mc-hamster? --- src/meshwifi/meshhttp.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/meshwifi/meshhttp.cpp b/src/meshwifi/meshhttp.cpp index a0183f340..401cb86db 100644 --- a/src/meshwifi/meshhttp.cpp +++ b/src/meshwifi/meshhttp.cpp @@ -940,8 +940,6 @@ void handleRoot(HTTPRequest *req, HTTPResponse *res) { res->setHeader("Content-Type", "text/html"); - randomSeed(millis()); - res->setHeader("Set-Cookie", "mt_session=" + httpsserver::intToString(random(1, 9999999)) + "; Expires=Wed, 20 Apr 2049 4:20:00 PST"); From 59577b9d7930ec7262e8aa180e3d6dbaa2b99d3b Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Fri, 25 Dec 2020 15:17:56 +0800 Subject: [PATCH 3/5] add real formatted debug logging with timestamps --- src/RedirectablePrint.cpp | 75 ++++++++++++++++++++++++++++++++++++ src/RedirectablePrint.h | 23 +++++++++++ src/concurrency/OSThread.cpp | 5 +++ src/concurrency/OSThread.h | 4 ++ src/configuration.h | 2 +- 5 files changed, 108 insertions(+), 1 deletion(-) diff --git a/src/RedirectablePrint.cpp b/src/RedirectablePrint.cpp index 0205aa66d..541b55c78 100644 --- a/src/RedirectablePrint.cpp +++ b/src/RedirectablePrint.cpp @@ -1,6 +1,9 @@ #include "RedirectablePrint.h" #include "configuration.h" #include +#include +#include +#include "concurrency/OSThread.h" /** * A printer that doesn't go anywhere @@ -15,10 +18,82 @@ void RedirectablePrint::setDestination(Print *_dest) size_t RedirectablePrint::write(uint8_t c) { + // Always send the characters to our segger JTAG debugger #ifdef SEGGER_STDOUT_CH SEGGER_RTT_PutCharSkip(SEGGER_STDOUT_CH, c); #endif dest->write(c); return 1; // We always claim one was written, rather than trusting what the serial port said (which could be zero) +} + +size_t RedirectablePrint::vprintf(const char *format, va_list arg) +{ + va_list copy; + + va_copy(copy, arg); + int len = vsnprintf(printBuf, printBufLen, format, copy); + va_end(copy); + if (len < 0) { + va_end(arg); + return 0; + }; + if (len >= printBufLen) { + delete[] printBuf; + printBufLen *= 2; + printBuf = new char[printBufLen]; + len = vsnprintf(printBuf, printBufLen, format, arg); + } + + len = Print::write(printBuf, len); + return len; +} + +#define SEC_PER_DAY 86400 +#define SEC_PER_HOUR 3600 +#define SEC_PER_MIN 60 + +size_t RedirectablePrint::logDebug(const char *format, ...) +{ + va_list arg; + va_start(arg, format); + + // Cope with 0 len format strings, but look for new line terminator + bool hasNewline = *format && format[strlen(format) - 1] == '\n'; + + size_t r = 0; + + // If we are the first message on a report, include the header + if (!isContinuationMessage) { + struct timeval tv; + if (!gettimeofday(&tv, NULL)) { + long hms = tv.tv_sec % SEC_PER_DAY; + //hms += tz.tz_dsttime * SEC_PER_HOUR; + //hms -= tz.tz_minuteswest * SEC_PER_MIN; + // mod `hms` to ensure in positive range of [0...SEC_PER_DAY) + hms = (hms + SEC_PER_DAY) % SEC_PER_DAY; + + // Tear apart hms into h:m:s + int hour = hms / SEC_PER_HOUR; + int min = (hms % SEC_PER_HOUR) / SEC_PER_MIN; + int sec = (hms % SEC_PER_HOUR) % SEC_PER_MIN; // or hms % SEC_PER_MIN + + r += printf("%02d:%02d:%02d ", hour, min, sec); + } else + r += printf("??:??:?? "); + + auto thread = concurrency::OSThread::currentThread; + if(thread) { + print("["); + print(thread->ThreadName); + print("] "); + } + } + + r += vprintf(format, arg); + va_end(arg); + + isContinuationMessage = !hasNewline; + + return r; } \ No newline at end of file diff --git a/src/RedirectablePrint.h b/src/RedirectablePrint.h index 2d525118d..ca208612c 100644 --- a/src/RedirectablePrint.h +++ b/src/RedirectablePrint.h @@ -1,6 +1,7 @@ #pragma once #include +#include /** * A Printable that can be switched to squirt its bytes to a different sink. @@ -11,6 +12,13 @@ class RedirectablePrint : public Print { Print *dest; + /// We dynamically grow this scratch buffer if necessary + char *printBuf = new char[64]; + size_t printBufLen = 64; + + /// Used to allow multiple logDebug messages to appear on a single log line + bool isContinuationMessage = false; + public: RedirectablePrint(Print *_dest) : dest(_dest) {} @@ -20,6 +28,21 @@ class RedirectablePrint : public Print void setDestination(Print *dest); virtual size_t write(uint8_t c); + + /** + * Debug logging print message + * + * If the provide format string ends with a newline we assume it is the final print of a single + * log message. Otherwise we assume more prints will come before the log message ends. This + * allows you to call logDebug a few times to build up a single log message line if you wish. + * + * FIXME, eventually add log levels (INFO, WARN, ERROR) and subsystems. Move into + * a different class. + */ + size_t logDebug(const char * format, ...) __attribute__ ((format (printf, 2, 3))); + + /** like printf but va_list based */ + size_t vprintf(const char *format, va_list arg); }; class NoopPrint : public Print diff --git a/src/concurrency/OSThread.cpp b/src/concurrency/OSThread.cpp index 916e68e15..58a0d59ca 100644 --- a/src/concurrency/OSThread.cpp +++ b/src/concurrency/OSThread.cpp @@ -14,6 +14,8 @@ bool OSThread::showRun = false; /// Show debugging info for threads we decide not to run; bool OSThread::showWaiting = false; +const OSThread *OSThread::currentThread; + ThreadController mainController, timerController; InterruptableDelay mainDelay; @@ -68,12 +70,15 @@ bool OSThread::shouldRun(unsigned long time) void OSThread::run() { + currentThread = this; auto newDelay = runOnce(); runned(); if (newDelay >= 0) setInterval(newDelay); + + currentThread = NULL; } } // namespace concurrency diff --git a/src/concurrency/OSThread.h b/src/concurrency/OSThread.h index dc600387e..3be149d5b 100644 --- a/src/concurrency/OSThread.h +++ b/src/concurrency/OSThread.h @@ -42,6 +42,10 @@ class OSThread : public Thread static bool showWaiting; public: + + /// For debug printing only (might be null) + static const OSThread *currentThread; + OSThread(const char *name, uint32_t period = 0, ThreadController *controller = &mainController); virtual ~OSThread(); diff --git a/src/configuration.h b/src/configuration.h index 28eea052b..6fa07ce63 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -428,7 +428,7 @@ along with this program. If not, see . #define DEBUG_MSG(...) SEGGER_RTT_printf(0, __VA_ARGS__) #else #ifdef DEBUG_PORT -#define DEBUG_MSG(...) DEBUG_PORT.printf(__VA_ARGS__) +#define DEBUG_MSG(...) DEBUG_PORT.logDebug(__VA_ARGS__) #else #define DEBUG_MSG(...) #endif From f45451ca74312b81841844b324b35d51f3fd28b4 Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Fri, 25 Dec 2020 15:31:17 +0800 Subject: [PATCH 4/5] missing line term --- src/graphics/Screen.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/graphics/Screen.cpp b/src/graphics/Screen.cpp index 3f98969d1..8cc8ebe29 100644 --- a/src/graphics/Screen.cpp +++ b/src/graphics/Screen.cpp @@ -912,7 +912,7 @@ void Screen::blink() { void Screen::handlePrint(const char *text) { - DEBUG_MSG("Screen: %s", text); + DEBUG_MSG("Screen: %s\n", text); if (!useDisplay || !showingNormalScreen) return; From 3c2aac87f7bd94cd492f13652e8de87295a7487f Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Fri, 25 Dec 2020 15:39:42 +0800 Subject: [PATCH 5/5] better fix for screen messages in log --- src/graphics/Screen.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/graphics/Screen.cpp b/src/graphics/Screen.cpp index 8cc8ebe29..ecaa216f7 100644 --- a/src/graphics/Screen.cpp +++ b/src/graphics/Screen.cpp @@ -912,7 +912,9 @@ void Screen::blink() { void Screen::handlePrint(const char *text) { - DEBUG_MSG("Screen: %s\n", text); + // the string passed into us probably has a newline, but that would confuse the logging system + // so strip it + DEBUG_MSG("Screen: %.*s\n", strlen(text) - 1, text); if (!useDisplay || !showingNormalScreen) return;