NRF52 bluetooth cleanup and fix (#3328)

* NRF52 bluetooth cleanup. Fixes BLE not returning after serial PhoneAPI connection

* Use new var name in esp32 arch
pull/3337/head
Ben Meadors 2024-03-03 13:56:55 -06:00 zatwierdzone przez GitHub
rodzic e5bf07d4fb
commit 72050530f1
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: B5690EEEBB952194
9 zmienionych plików z 39 dodań i 27 usunięć

Wyświetl plik

@ -16,5 +16,5 @@ class NimbleBluetooth : BluetoothApi
void startAdvertising();
};
void setBluetoothEnable(bool on);
void clearNVS();
void setBluetoothEnable(bool enable);
void clearNVS();

Wyświetl plik

@ -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

Wyświetl plik

@ -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()
@ -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)
{

Wyświetl plik

@ -8,6 +8,7 @@ class NRF52Bluetooth : BluetoothApi
public:
void setup();
void shutdown();
void resumeAdverising();
void clearBonds();
bool isConnected();
int getRssi();
@ -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);
};
};

Wyświetl plik

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

Wyświetl plik

@ -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
}

Wyświetl plik

@ -2,7 +2,7 @@
#include <pico/unique_id.h>
#include <stdio.h>
void setBluetoothEnable(bool on)
void setBluetoothEnable(bool enable)
{
// not needed
}

Wyświetl plik

@ -3,7 +3,7 @@
#include <stm32wle5xx.h>
#include <stm32wlxx_hal.h>
void setBluetoothEnable(bool on) {}
void setBluetoothEnable(bool enable) {}
void playStartMelody() {}
@ -33,4 +33,4 @@ int _gettimeofday(struct timeval *tv, void *tzvp)
{
return -1;
}
}
}

Wyświetl plik

@ -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);