Skip to content

Commit

Permalink
NRF52 bluetooth cleanup and fix (#3328)
Browse files Browse the repository at this point in the history
* NRF52 bluetooth cleanup. Fixes BLE not returning after serial PhoneAPI connection

* Use new var name in esp32 arch
  • Loading branch information
thebentern authored Mar 3, 2024
1 parent e5bf07d commit 7205053
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/nimble/NimbleBluetooth.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ class NimbleBluetooth : BluetoothApi
void startAdvertising();
};

void setBluetoothEnable(bool on);
void clearNVS();
void setBluetoothEnable(bool enable);
void clearNVS();
8 changes: 4 additions & 4 deletions src/platform/esp32/main-esp32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@

#if !defined(CONFIG_IDF_TARGET_ESP32S2)

void setBluetoothEnable(bool on)
void setBluetoothEnable(bool enable)
{
if (!isWifiAvailable() && config.bluetooth.enabled == true) {
if (!nimbleBluetooth) {
nimbleBluetooth = new NimbleBluetooth();
}
if (on && !nimbleBluetooth->isActive()) {
if (enable && !nimbleBluetooth->isActive()) {
nimbleBluetooth->setup();
} else if (!on) {
} else if (!enable) {
nimbleBluetooth->shutdown();
}
}
}
#else
void setBluetoothEnable(bool on) {}
void setBluetoothEnable(bool enable) {}
void updateBatteryLevel(uint8_t level) {}
#endif

Expand Down
12 changes: 11 additions & 1 deletion src/platform/nrf52/NRF52Bluetooth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,10 @@ void NRF52Bluetooth::shutdown()
{
// Shutdown bluetooth for minimum power draw
LOG_INFO("Disable NRF52 bluetooth\n");
if (connectionHandle != 0) {
Bluefruit.disconnect(connectionHandle);
}
Bluefruit.Advertising.stop();
Bluefruit.setTxPower(0); // Minimum power
}

bool NRF52Bluetooth::isConnected()
Expand Down Expand Up @@ -289,6 +291,14 @@ void NRF52Bluetooth::setup()
}
}

void NRF52Bluetooth::resumeAdverising()
{
Bluefruit.Advertising.restartOnDisconnect(true);
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
Bluefruit.Advertising.start(0);
}

/// Given a level between 0-100, update the BLE attribute
void updateBatteryLevel(uint8_t level)
{
Expand Down
3 changes: 2 additions & 1 deletion src/platform/nrf52/NRF52Bluetooth.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class NRF52Bluetooth : BluetoothApi
public:
void setup();
void shutdown();
void resumeAdverising();
void clearBonds();
bool isConnected();
int getRssi();
Expand All @@ -17,4 +18,4 @@ class NRF52Bluetooth : BluetoothApi
void convertToUint8(uint8_t target[4], uint32_t source);
static bool onPairingPasskey(uint16_t conn_handle, uint8_t const passkey[6], bool match_request);
static void onPairingCompleted(uint16_t conn_handle, uint8_t auth_status);
};
};
31 changes: 16 additions & 15 deletions src/platform/nrf52/main-nrf52.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,28 +63,29 @@ static void initBrownout()
// We don't bother with setting up brownout if soft device is disabled - because during production we always use softdevice
}

static bool bleOn = false;
static const bool useSoftDevice = true; // Set to false for easier debugging

void setBluetoothEnable(bool on)
void setBluetoothEnable(bool enable)
{
if (on != bleOn && config.bluetooth.enabled == true) {
if (on) {
if (enable && config.bluetooth.enabled) {
if (!useSoftDevice) {
LOG_INFO("DISABLING NRF52 BLUETOOTH WHILE DEBUGGING\n");
} else {
if (!nrf52Bluetooth) {
if (!useSoftDevice)
LOG_INFO("DISABLING NRF52 BLUETOOTH WHILE DEBUGGING\n");
else {
nrf52Bluetooth = new NRF52Bluetooth();
nrf52Bluetooth->setup();

// We delay brownout init until after BLE because BLE starts soft device
initBrownout();
}
LOG_DEBUG("Initializing NRF52 Bluetooth\n");
nrf52Bluetooth = new NRF52Bluetooth();
nrf52Bluetooth->setup();

// We delay brownout init until after BLE because BLE starts soft device
initBrownout();
} else {
nrf52Bluetooth->resumeAdverising();
}
} else if (nrf52Bluetooth) {
}
} else {
if (nrf52Bluetooth) {
nrf52Bluetooth->shutdown();
}
bleOn = on;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/platform/portduino/PortduinoGlue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ std::map<configNames, std::string> settingsStrings;
char *configPath = nullptr;

// FIXME - move setBluetoothEnable into a HALPlatform class
void setBluetoothEnable(bool on)
void setBluetoothEnable(bool enable)
{
// not needed
}
Expand Down
2 changes: 1 addition & 1 deletion src/platform/rp2040/main-rp2040.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <pico/unique_id.h>
#include <stdio.h>

void setBluetoothEnable(bool on)
void setBluetoothEnable(bool enable)
{
// not needed
}
Expand Down
4 changes: 2 additions & 2 deletions src/platform/stm32wl/main-stm32wl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <stm32wle5xx.h>
#include <stm32wlxx_hal.h>

void setBluetoothEnable(bool on) {}
void setBluetoothEnable(bool enable) {}

void playStartMelody() {}

Expand Down Expand Up @@ -33,4 +33,4 @@ int _gettimeofday(struct timeval *tv, void *tzvp)
{
return -1;
}
}
}
2 changes: 1 addition & 1 deletion src/target_specific.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
// Functions that are unique to particular target types (esp32, bare, nrf52 etc...)

// Enable/disable bluetooth.
void setBluetoothEnable(bool on);
void setBluetoothEnable(bool enable);

void getMacAddr(uint8_t *dmac);

0 comments on commit 7205053

Please sign in to comment.