Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NRF52 bluetooth cleanup and fix #3328

Merged
merged 4 commits into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Loading