Disconnect BLE before hard reset
If BLE is connected when performing a hard reset, attempt to disconnect first to allow the remote peer to handle the disconnect gracefully without haing to wait for a supervision timeout.
This commit is contained in:
parent
2532d2f0b5
commit
51ea69201e
1 changed files with 19 additions and 0 deletions
|
@ -18,12 +18,31 @@ void panic(int statusCode)
|
|||
uBit.panic(statusCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback that does a hard reset when a BLE GAP disconnect occurs.
|
||||
*/
|
||||
void bleDisconnectionResetCallback(Gap::Handle_t, Gap::DisconnectionReason_t)
|
||||
{
|
||||
NVIC_SystemReset();
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform a hard reset of the micro:bit.
|
||||
* If BLE connected, then try to signal a disconnect first
|
||||
*/
|
||||
void
|
||||
microbit_reset()
|
||||
{
|
||||
if(uBit.ble && uBit.ble->getGapState().connected) {
|
||||
uBit.ble->onDisconnection(bleDisconnectionResetCallback);
|
||||
uBit.ble->gap().disconnect(Gap::REMOTE_USER_TERMINATED_CONNECTION);
|
||||
// We should be reset by the disconnection callback, so we wait to
|
||||
// allow that to happen. If it doesn't happen, then we fall through to the
|
||||
// hard rest here. (For example there is a race condition where
|
||||
// the remote device disconnects between us testing the connection
|
||||
// state and re-setting the disconnection callback).
|
||||
uBit.sleep(1000);
|
||||
}
|
||||
NVIC_SystemReset();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue