From 419dd44ff600ce066732be803daa90cf7a41ba4f Mon Sep 17 00:00:00 2001 From: geeksville Date: Sun, 23 Feb 2020 18:09:40 -0800 Subject: [PATCH] BT sleep leak now down to about 200 bytes --- lib/BluetoothOTA/src/BluetoothUtil.cpp | 9 ++++++++- lib/BluetoothOTA/src/SimpleAllocator.cpp | 10 ++++++++-- src/CustomRF95.cpp | 3 ++- src/CustomRF95.h | 2 ++ src/MeshRadio.cpp | 17 +++++++++++++---- src/main.ino | 11 ++++++++--- 6 files changed, 41 insertions(+), 11 deletions(-) diff --git a/lib/BluetoothOTA/src/BluetoothUtil.cpp b/lib/BluetoothOTA/src/BluetoothUtil.cpp index f10abe1a1..b33c6ff93 100644 --- a/lib/BluetoothOTA/src/BluetoothUtil.cpp +++ b/lib/BluetoothOTA/src/BluetoothUtil.cpp @@ -224,6 +224,14 @@ void deinitBLE() { assert(pServer); + pServer->getAdvertising()->stop(); + + destroyUpdateService(); + + pUpdate->stop(); + pDevInfo->stop(); + pUpdate->stop(); // we delete them below + // First shutdown bluetooth BLEDevice::deinit(false); @@ -235,7 +243,6 @@ void deinitBLE() delete pServer; batteryLevelC = NULL; // Don't let anyone generate bogus notifies - destroyUpdateService(); for (int i = 0; i < numChars; i++) delete chars[i]; diff --git a/lib/BluetoothOTA/src/SimpleAllocator.cpp b/lib/BluetoothOTA/src/SimpleAllocator.cpp index bc21cf12d..4855e8edf 100644 --- a/lib/BluetoothOTA/src/SimpleAllocator.cpp +++ b/lib/BluetoothOTA/src/SimpleAllocator.cpp @@ -1,7 +1,6 @@ #include "SimpleAllocator.h" #include "assert.h" -SimpleAllocator *activeAllocator; SimpleAllocator::SimpleAllocator() { reset(); } @@ -22,6 +21,11 @@ void *operator new(size_t size, SimpleAllocator &p) return p.alloc(size); } +#if 0 +// This was a dumb idea, turn off for now + +SimpleAllocator *activeAllocator; + AllocatorScope::AllocatorScope(SimpleAllocator &a) { assert(!activeAllocator); @@ -51,4 +55,6 @@ void operator delete(void *ptr) throw() Serial.println("Warning: leaking an active allocator object"); // We don't properly handle this yet else free(ptr); -} \ No newline at end of file +} + +#endif \ No newline at end of file diff --git a/src/CustomRF95.cpp b/src/CustomRF95.cpp index e684a1f67..6cdcf1e2b 100644 --- a/src/CustomRF95.cpp +++ b/src/CustomRF95.cpp @@ -20,7 +20,8 @@ CustomRF95::CustomRF95(MemoryPool &_pool, PointerQueue & bool CustomRF95::canSleep() { - return (_mode == RHModeIdle || _mode == RHModeRx) && txQueue.isEmpty(); // FIXME - also check if we have started receiving + // We allow initializing mode, because sometimes while testing we don't ever call init() to turn on the hardware + return (_mode == RHModeInitialising || _mode == RHModeIdle || _mode == RHModeRx) && txQueue.isEmpty(); // FIXME - also check if we have started receiving } bool CustomRF95::sleep() diff --git a/src/CustomRF95.h b/src/CustomRF95.h index 8261d1c6a..1768fbf75 100644 --- a/src/CustomRF95.h +++ b/src/CustomRF95.h @@ -15,6 +15,8 @@ */ class CustomRF95 : public RH_RF95 { + friend class MeshRadio; // for debugging we let that class touch pool + MemoryPool &pool; PointerQueue &rxDest; PointerQueue txQueue; diff --git a/src/MeshRadio.cpp b/src/MeshRadio.cpp index 459ba323c..3c1d79fa9 100644 --- a/src/MeshRadio.cpp +++ b/src/MeshRadio.cpp @@ -25,6 +25,9 @@ The band is from 902 to 928 MHz. It mentions channel number and its respective c Channel zero starts at 903.08 MHz center frequency. */ +/// Sometimes while debugging it is useful to set this false, to disable rf95 accesses +bool useHardware = true; + MeshRadio::MeshRadio(MemoryPool &_pool, PointerQueue &_rxDest) : rf95(_pool, _rxDest), manager(rf95) @@ -45,6 +48,9 @@ MeshRadio::MeshRadio(MemoryPool &_pool, PointerQueue &_r bool MeshRadio::init() { + if (!useHardware) + return true; + DEBUG_MSG("Starting meshradio init...\n"); #ifdef RESET_GPIO @@ -109,14 +115,17 @@ void MeshRadio::reloadConfig() rf95.setModeRx(); } - ErrorCode MeshRadio::send(MeshPacket *p) { - return rf95.send(p); + if (useHardware) + return rf95.send(p); + else + { + rf95.pool.release(p); + return ERRNO_OK; + } } - - void MeshRadio::loop() { // Currently does nothing, since we do it all in ISRs now diff --git a/src/main.ino b/src/main.ino index 7b801aecf..e2e661574 100644 --- a/src/main.ino +++ b/src/main.ino @@ -201,6 +201,8 @@ const char *getDeviceName() return name; } + + void setup() { // Debug @@ -284,15 +286,18 @@ void setBluetoothEnable(bool on) bluetoothOn = on; if (on) { - Serial.printf("Pre BT: %u heap size", ESP.getFreeHeap()); + Serial.printf("Pre BT: %u heap size\n", ESP.getFreeHeap()); + //ESP_ERROR_CHECK( heap_trace_start(HEAP_TRACE_LEAKS) ); initBluetooth(); } else { // We have to totally teardown our bluetooth objects to prevent leaks - destroyMeshBluetoothService(); deinitBLE(); - Serial.printf("Shutdown BT: %u heap size", ESP.getFreeHeap()); + destroyMeshBluetoothService(); // must do after deinit, because it frees our service + Serial.printf("Shutdown BT: %u heap size\n", ESP.getFreeHeap()); + //ESP_ERROR_CHECK( heap_trace_stop() ); + //heap_trace_dump(); } } }