Update to MicrobitBLEManager to allow safe storage of BLE bond information

This commit is contained in:
Joe Finney 2017-03-01 17:41:28 +00:00
parent 564e7d700c
commit 4b3520daa6
2 changed files with 42 additions and 7 deletions

View file

@ -74,6 +74,10 @@ DEALINGS IN THE SOFTWARE.
#define MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL 400
#define MICROBIT_BLE_EDDYSTONE_DEFAULT_POWER 0xF0
// MicroBitComponent status flags
#define MICROBIT_BLE_STATUS_STORE_SYSATTR 0x02
#define MICROBIT_BLE_STATUS_DISCONNECT 0x04
extern const int8_t MICROBIT_BLE_POWER_LEVEL[];
struct BLESysAttribute
@ -214,6 +218,14 @@ class MicroBitBLEManager : MicroBitComponent
* Stops any currently running BLE advertisements
*/
void stopAdvertising();
/**
* A member function used to defer writes to flash, in order to prevent a write collision with
* softdevice.
* @param handle The handle offered by soft device during pairing.
* */
void deferredSysAttrWrite(Gap::Handle_t handle);
#if CONFIG_ENABLED(MICROBIT_BLE_EDDYSTONE_URL)
/**

View file

@ -127,10 +127,11 @@ static void bleDisconnectionCallback(const Gap::DisconnectionCallbackParams_t *r
{
MicroBitEvent(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_DISCONNECTED);
storeSystemAttributes(reason->handle);
if (MicroBitBLEManager::manager)
{
MicroBitBLEManager::manager->advertise();
MicroBitBLEManager::manager->deferredSysAttrWrite(reason->handle);
}
}
/**
@ -228,6 +229,7 @@ MicroBitBLEManager::MicroBitBLEManager(MicroBitStorage &_storage) : storage(&_st
manager = this;
this->ble = NULL;
this->pairingStatus = 0;
this->status = MICROBIT_COMPONENT_RUNNING;
}
/**
@ -268,6 +270,17 @@ void MicroBitBLEManager::advertise()
ble->gap().startAdvertising();
}
/**
* A member function used to defer writes to flash, in order to prevent a write collision with
* softdevice.
* @param handle The handle offered by soft device during pairing.
* */
void MicroBitBLEManager::deferredSysAttrWrite(Gap::Handle_t handle)
{
pairingHandle = handle;
this->status |= MICROBIT_BLE_STATUS_STORE_SYSATTR;
}
/**
* Post constructor initialisation method as the BLE stack cannot be brought
* up in a static context.
@ -483,7 +496,7 @@ void MicroBitBLEManager::pairingComplete(bool success)
if (success)
{
this->pairingStatus |= MICROBIT_BLE_PAIR_SUCCESSFUL;
fiber_add_idle_component(this);
this->status |= MICROBIT_BLE_STATUS_DISCONNECT;
}
}
@ -493,12 +506,20 @@ void MicroBitBLEManager::pairingComplete(bool success)
*/
void MicroBitBLEManager::idleTick()
{
if((system_timer_current_time() - pairing_completed_at_time) >= MICROBIT_BLE_DISCONNECT_AFTER_PAIRING_DELAY) {
if (ble)
ble->disconnect(pairingHandle, Gap::REMOTE_DEV_TERMINATION_DUE_TO_POWER_OFF);
fiber_remove_idle_component(this);
if (this->status & MICROBIT_BLE_STATUS_DISCONNECT)
{
if((system_timer_current_time() - pairing_completed_at_time) >= MICROBIT_BLE_DISCONNECT_AFTER_PAIRING_DELAY) {
if (ble)
ble->disconnect(pairingHandle, Gap::REMOTE_DEV_TERMINATION_DUE_TO_POWER_OFF);
this->status &= ~MICROBIT_BLE_STATUS_DISCONNECT;
}
}
if (this->status & MICROBIT_BLE_STATUS_STORE_SYSATTR)
{
storeSystemAttributes(pairingHandle);
this->status &= ~MICROBIT_BLE_STATUS_STORE_SYSATTR;
}
}
@ -658,6 +679,8 @@ void MicroBitBLEManager::pairingMode(MicroBitDisplay &display, MicroBitButton &a
// Display our name, visualised as a histogram in the display to aid identification.
showNameHistogram(display);
fiber_add_idle_component(this);
while (1)
{
if (pairingStatus & MICROBIT_BLE_PAIR_REQUEST)