Skip to content

Commit

Permalink
Clear bluetooth bonds on multi-press and factory_reset (meshtastic#1176)
Browse files Browse the repository at this point in the history
* Clear bluetooth bonds on multi-press and factory_reset
  • Loading branch information
thebentern authored Feb 2, 2022
1 parent dd31a82 commit b21b7de
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,9 @@ class ButtonThread : public OSThread
{
#ifndef NO_ESP32
clearNVS();
#endif
#ifdef NRF52_SERIES
clearBonds();
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ extern uint32_t shutdownAtMsec;
// This will supress the current delay and instead try to run ASAP.
extern bool runASAP;

void nrf52Setup(), esp32Setup(), nrf52Loop(), esp32Loop();
void nrf52Setup(), esp32Setup(), nrf52Loop(), esp32Loop(), clearBonds();
15 changes: 15 additions & 0 deletions src/mesh/NodeDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
#include <nvs_flash.h>
#endif

#ifdef NRF52_SERIES
#include <bluefruit.h>
#include <utility/bonding.h>
#endif

NodeDB nodeDB;

// we have plenty of ram so statically alloc this tempbuf (for now)
Expand Down Expand Up @@ -90,6 +95,16 @@ bool NodeDB::resetRadioConfig()
#ifndef NO_ESP32
// This will erase what's in NVS including ssl keys, persistant variables and ble pairing
nvs_flash_erase();
#endif
#ifdef NRF52_SERIES
Bluefruit.begin();

DEBUG_MSG("Clearing bluetooth bonds!\n");
bond_print_list(BLE_GAP_ROLE_PERIPH);
bond_print_list(BLE_GAP_ROLE_CENTRAL);

Bluefruit.Periph.clearBonds();
Bluefruit.Central.clearBonds();
#endif
didFactoryReset = true;
}
Expand Down
11 changes: 11 additions & 0 deletions src/nrf52/NRF52Bluetooth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "mesh/PhoneAPI.h"
#include "mesh/mesh-pb-constants.h"
#include <bluefruit.h>
#include <utility/bonding.h>

static BLEService meshBleService = BLEService(BLEUuid(MESH_SERVICE_UUID_16));
static BLECharacteristic fromNum = BLECharacteristic(BLEUuid(FROMNUM_UUID_16));
Expand Down Expand Up @@ -266,3 +267,13 @@ void updateBatteryLevel(uint8_t level)
{
blebas.write(level);
}

void NRF52Bluetooth::clearBonds()
{
DEBUG_MSG("Clearing bluetooth bonds!\n");
bond_print_list(BLE_GAP_ROLE_PERIPH);
bond_print_list(BLE_GAP_ROLE_CENTRAL);

Bluefruit.Periph.clearBonds();
Bluefruit.Central.clearBonds();
}
1 change: 1 addition & 0 deletions src/nrf52/NRF52Bluetooth.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ class NRF52Bluetooth
public:
void setup();
void shutdown();
void clearBonds();
};

10 changes: 9 additions & 1 deletion src/nrf52/main-nrf52.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void setBluetoothEnable(bool on)
else {
nrf52Bluetooth = new NRF52Bluetooth();
nrf52Bluetooth->setup();

// We delay brownout init until after BLE because BLE starts soft device
initBrownout();
}
Expand Down Expand Up @@ -185,4 +185,12 @@ void cpuDeepSleep(uint64_t msecToWake)
delay(5000);
DEBUG_MSG(".");
}
}

void clearBonds() {
if (!nrf52Bluetooth) {
nrf52Bluetooth = new NRF52Bluetooth();
nrf52Bluetooth->setup();
}
nrf52Bluetooth->clearBonds();
}

0 comments on commit b21b7de

Please sign in to comment.