From a45d7be922313b9cb6d66d8c1a83925352b7eb2e Mon Sep 17 00:00:00 2001 From: geeksville Date: Sun, 23 Feb 2020 13:54:40 -0800 Subject: [PATCH] still leaking in the BT stack but better --- lib/BluetoothOTA/src/BluetoothUtil.cpp | 25 ++++++++++++++++++++----- lib/BluetoothOTA/src/SimpleAllocator.h | 2 +- src/MeshBluetoothService.cpp | 6 ++++++ src/main.ino | 2 ++ 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/lib/BluetoothOTA/src/BluetoothUtil.cpp b/lib/BluetoothOTA/src/BluetoothUtil.cpp index e2833e436..6a16ad9db 100644 --- a/lib/BluetoothOTA/src/BluetoothUtil.cpp +++ b/lib/BluetoothOTA/src/BluetoothUtil.cpp @@ -189,12 +189,27 @@ class MySecurity : public BLESecurityCallbacks } }; +BLEServer *pServer; + +BLEService *pDevInfo, *pUpdate; + void deinitBLE() { + assert(pServer); + + // First shutdown bluetooth + BLEDevice::deinit(false); + + // do not delete this - it is dynamically allocated, but only once - statically in BLEDevice + // delete pServer->getAdvertising(); + + delete pUpdate; + delete pDevInfo; + delete pServer; + batteryLevelC = NULL; // Don't let anyone generate bogus notifies destroyUpdateService(); - - BLEDevice::deinit(false); + btPool.reset(); } @@ -210,16 +225,16 @@ BLEServer *initBLE(std::string deviceName, std::string hwVendor, std::string swV BLEDevice::setSecurityCallbacks(&mySecurity); // Create the BLE Server - BLEServer *pServer = BLEDevice::createServer(); + pServer = BLEDevice::createServer(); static MyServerCallbacks myCallbacks; pServer->setCallbacks(&myCallbacks); - BLEService *pDevInfo = createDeviceInfomationService(pServer, hwVendor, swVersion, hwVersion); + pDevInfo = createDeviceInfomationService(pServer, hwVendor, swVersion, hwVersion); // We now let users create the battery service only if they really want (not all devices have a battery) // BLEService *pBattery = createBatteryService(pServer); - BLEService *pUpdate = createUpdateService(pServer); // We need to advertise this so our android ble scan operation can see it + pUpdate = createUpdateService(pServer); // We need to advertise this so our android ble scan operation can see it // It seems only one service can be advertised - so for now don't advertise our updater // pServer->getAdvertising()->addServiceUUID(pUpdate->getUUID()); diff --git a/lib/BluetoothOTA/src/SimpleAllocator.h b/lib/BluetoothOTA/src/SimpleAllocator.h index 115cd2fa7..acaab3409 100644 --- a/lib/BluetoothOTA/src/SimpleAllocator.h +++ b/lib/BluetoothOTA/src/SimpleAllocator.h @@ -1,7 +1,7 @@ #pragma once #include -#define POOL_SIZE 32768 +#define POOL_SIZE 16384 /** * An allocator (and placement new operator) that allocates storage from a fixed sized buffer. diff --git a/src/MeshBluetoothService.cpp b/src/MeshBluetoothService.cpp index c1806a715..e221ebc38 100644 --- a/src/MeshBluetoothService.cpp +++ b/src/MeshBluetoothService.cpp @@ -236,6 +236,8 @@ void bluetoothNotifyFromNum(uint32_t newValue) } } +BLEService *meshService; + /* MeshBluetoothService UUID 6ba1b218-15a8-461f-9fa8-5dcae273eafd @@ -317,9 +319,13 @@ BLEService *createMeshBluetoothService(BLEServer *server) DEBUG_MSG("*** Mesh service:\n"); service->dump(); + meshService = service; return service; } void destroyMeshBluetoothService() { + assert(meshService); + delete meshService; + meshFromNumCharacteristic = NULL; } \ No newline at end of file diff --git a/src/main.ino b/src/main.ino index f0d5c580d..7b801aecf 100644 --- a/src/main.ino +++ b/src/main.ino @@ -284,6 +284,7 @@ void setBluetoothEnable(bool on) bluetoothOn = on; if (on) { + Serial.printf("Pre BT: %u heap size", ESP.getFreeHeap()); initBluetooth(); } else @@ -291,6 +292,7 @@ void setBluetoothEnable(bool on) // We have to totally teardown our bluetooth objects to prevent leaks destroyMeshBluetoothService(); deinitBLE(); + Serial.printf("Shutdown BT: %u heap size", ESP.getFreeHeap()); } } }