From 772f2a15fffc0c528f71bc7bebaa4aa035d22b75 Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Tue, 9 Mar 2021 16:45:40 +0800 Subject: [PATCH] check more error codes --- proto | 2 +- src/RedirectablePrint.cpp | 77 +++++++++++---------- src/RedirectablePrint.h | 2 + src/concurrency/BinarySemaphoreFreeRTOS.cpp | 2 + src/mesh/MemoryPool.h | 11 ++- 5 files changed, 54 insertions(+), 40 deletions(-) diff --git a/proto b/proto index 7c025b9a4..e56f2770c 160000 --- a/proto +++ b/proto @@ -1 +1 @@ -Subproject commit 7c025b9a4d54bb410ec17ee653122861b413f177 +Subproject commit e56f2770c33216ba94f289e2fb7f0b2dfd33aca2 diff --git a/src/RedirectablePrint.cpp b/src/RedirectablePrint.cpp index c3743b973..4c460d77f 100644 --- a/src/RedirectablePrint.cpp +++ b/src/RedirectablePrint.cpp @@ -1,9 +1,9 @@ #include "RedirectablePrint.h" +#include "concurrency/OSThread.h" #include "configuration.h" #include #include #include -#include "concurrency/OSThread.h" /** * A printer that doesn't go anywhere @@ -38,7 +38,7 @@ size_t RedirectablePrint::vprintf(const char *format, va_list arg) va_end(arg); return 0; }; - if (len >= (int) printBufLen) { + if (len >= (int)printBufLen) { delete[] printBuf; printBufLen *= 2; printBuf = new char[printBufLen]; @@ -55,47 +55,52 @@ size_t RedirectablePrint::vprintf(const char *format, va_list arg) 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; + if (!inDebugPrint) { + inDebugPrint = true; - // 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 + va_list arg; + va_start(arg, format); - r += printf("%02d:%02d:%02d %u ", hour, min, sec, millis() / 1000); - } else - r += printf("??:??:?? %u ", millis() / 1000); + // Cope with 0 len format strings, but look for new line terminator + bool hasNewline = *format && format[strlen(format) - 1] == '\n'; - auto thread = concurrency::OSThread::currentThread; - if(thread) { - print("["); - // printf("%p ", thread); - // assert(thread->ThreadName.length()); - print(thread->ThreadName); - print("] "); + // 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 %u ", hour, min, sec, millis() / 1000); + } else + r += printf("??:??:?? %u ", millis() / 1000); + + auto thread = concurrency::OSThread::currentThread; + if (thread) { + print("["); + // printf("%p ", thread); + // assert(thread->ThreadName.length()); + print(thread->ThreadName); + print("] "); + } } + + r += vprintf(format, arg); + va_end(arg); + + isContinuationMessage = !hasNewline; + inDebugPrint = false; } - 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 ca208612c..1eb60199b 100644 --- a/src/RedirectablePrint.h +++ b/src/RedirectablePrint.h @@ -19,6 +19,8 @@ class RedirectablePrint : public Print /// Used to allow multiple logDebug messages to appear on a single log line bool isContinuationMessage = false; + volatile bool inDebugPrint = false; + public: RedirectablePrint(Print *_dest) : dest(_dest) {} diff --git a/src/concurrency/BinarySemaphoreFreeRTOS.cpp b/src/concurrency/BinarySemaphoreFreeRTOS.cpp index 983ee0955..3d8097455 100644 --- a/src/concurrency/BinarySemaphoreFreeRTOS.cpp +++ b/src/concurrency/BinarySemaphoreFreeRTOS.cpp @@ -1,5 +1,6 @@ #include "concurrency/BinarySemaphoreFreeRTOS.h" #include "configuration.h" +#include #ifdef HAS_FREE_RTOS @@ -9,6 +10,7 @@ namespace concurrency BinarySemaphoreFreeRTOS::BinarySemaphoreFreeRTOS() { semaphore = xSemaphoreCreateBinary(); + assert(semaphore); } BinarySemaphoreFreeRTOS::~BinarySemaphoreFreeRTOS() diff --git a/src/mesh/MemoryPool.h b/src/mesh/MemoryPool.h index dc6305d17..8023aa6bc 100644 --- a/src/mesh/MemoryPool.h +++ b/src/mesh/MemoryPool.h @@ -101,25 +101,30 @@ template class MemoryPool : public Allocator /// Return a buffer for use by others virtual void release(T *p) { - assert(dead.enqueue(p, 0)); assert(p >= buf && (size_t)(p - buf) < maxElements); // sanity check to make sure a programmer didn't free something that didn't come from this pool + assert(dead.enqueue(p, 0)); } #ifdef HAS_FREE_RTOS /// Return a buffer from an ISR, if higherPriWoken is set to true you have some work to do ;-) void releaseFromISR(T *p, BaseType_t *higherPriWoken) { - assert(dead.enqueueFromISR(p, higherPriWoken)); assert(p >= buf && (size_t)(p - buf) < maxElements); // sanity check to make sure a programmer didn't free something that didn't come from this pool + assert(dead.enqueueFromISR(p, higherPriWoken)); } #endif protected: /// Return a queable object which has been prefilled with zeros - allow timeout to wait for available buffers (you /// probably don't want this version). - virtual T *alloc(TickType_t maxWait) { return dead.dequeuePtr(maxWait); } + virtual T *alloc(TickType_t maxWait) + { + T *p = dead.dequeuePtr(maxWait); + assert(p); + return p; + } };