diff --git a/src/esp32/main-esp32.cpp b/src/esp32/main-esp32.cpp index 5936e8c8e..d74bda988 100644 --- a/src/esp32/main-esp32.cpp +++ b/src/esp32/main-esp32.cpp @@ -1,4 +1,3 @@ -#include "MeshBluetoothService.h" #include "PowerFSM.h" #include "configuration.h" #include "esp_task_wdt.h" @@ -10,30 +9,6 @@ #include #include -bool bluetoothOn; - -// Enable/disable bluetooth. -void setBluetoothEnable(bool on) -{ - if (on != bluetoothOn) { - DEBUG_MSG("Setting bluetooth enable=%d\n", on); - - bluetoothOn = on; - if (on) { - Serial.printf("Pre BT: %u heap size\n", ESP.getFreeHeap()); - // ESP_ERROR_CHECK( heap_trace_start(HEAP_TRACE_LEAKS) ); - reinitBluetooth(); - } else { - // We have to totally teardown our bluetooth objects to prevent leaks - stopMeshBluetoothService(); // Must do before shutting down bluetooth - deinitBLE(); - 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(); - } - } -} void getMacAddr(uint8_t *dmac) { diff --git a/src/nimble/BluetoothUtil.cpp b/src/nimble/BluetoothUtil.cpp index 81eb2bfad..558ffb500 100644 --- a/src/nimble/BluetoothUtil.cpp +++ b/src/nimble/BluetoothUtil.cpp @@ -1,6 +1,7 @@ #include "BluetoothUtil.h" #include "BluetoothSoftwareUpdate.h" #include "configuration.h" +#include "esp_bt.h" #include #include #include @@ -179,7 +180,10 @@ void deinitBLE() ret = ble_gatts_reset(); // Teardown the service tables, so the next restart assigns the same handle numbers assert(ret == ESP_OK); - +#if 0 + auto ret = esp_bt_controller_disable(); + assert(ret == ESP_OK); +#endif DEBUG_MSG("Done shutting down bluetooth\n"); } @@ -495,11 +499,9 @@ void gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg) // This routine is called multiple times, once each time we come back from sleep void reinitBluetooth() { - DEBUG_MSG("Starting bluetooth\n"); - esp_log_level_set("BTDM_INIT", ESP_LOG_VERBOSE); - auto isFirstTime = !bluetoothPhoneAPI; + DEBUG_MSG("Starting bluetooth\n"); if (isFirstTime) { bluetoothPhoneAPI = new BluetoothPhoneAPI(); bluetoothPhoneAPI->init(); @@ -530,8 +532,8 @@ void reinitBluetooth() ble_svc_gap_init(); ble_svc_gatt_init(); - res = ble_gatts_count_cfg( - gatt_svr_svcs); // assigns handles? see docstring for note about clearing the handle list before calling SLEEP SUPPORT + res = ble_gatts_count_cfg(gatt_svr_svcs); // assigns handles? see docstring for note about clearing the handle list + // before calling SLEEP SUPPORT assert(res == 0); res = ble_gatts_add_svcs(gatt_svr_svcs); @@ -546,3 +548,26 @@ void reinitBluetooth() nimble_port_freertos_init(ble_host_task); } + +bool bluetoothOn; + +// Enable/disable bluetooth. +void setBluetoothEnable(bool on) +{ + if (on != bluetoothOn) { + DEBUG_MSG("Setting bluetooth enable=%d\n", on); + + bluetoothOn = on; + if (on) { + Serial.printf("Pre BT: %u heap size\n", ESP.getFreeHeap()); + // ESP_ERROR_CHECK( heap_trace_start(HEAP_TRACE_LEAKS) ); + reinitBluetooth(); + } else { + // We have to totally teardown our bluetooth objects to prevent leaks + deinitBLE(); + Serial.printf("Shutdown BT: %u heap size\n", ESP.getFreeHeap()); + // ESP_ERROR_CHECK( heap_trace_stop() ); + // heap_trace_dump(); + } + } +}