From 6f5ad99aa20e05789b46e76c4f2538713de24300 Mon Sep 17 00:00:00 2001 From: Joe Finney Date: Fri, 11 Dec 2015 01:16:48 +0000 Subject: [PATCH] BUGFIX: Enabled BLE_COMMON_OPT_RADIO_CPU_MUTEX option This option is now configured when the BLE stack is initialised. This ensures that SoftDevice is never starved of CPU during periods of criticality. This does lock out the CPU for application use, but prevents MIC failures caused by __disable_irq() operations (as found in the mbed Ticker API for example). --- source/ble-services/MicroBitBLEManager.cpp | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/source/ble-services/MicroBitBLEManager.cpp b/source/ble-services/MicroBitBLEManager.cpp index 977f21e..399cf24 100644 --- a/source/ble-services/MicroBitBLEManager.cpp +++ b/source/ble-services/MicroBitBLEManager.cpp @@ -1,5 +1,26 @@ #include "MicroBit.h" + +/* The underlying Nordic libraries that support BLE do not compile cleanly with the stringent GCC settings we employ + * If we're compiling under GCC, then we suppress any warnings generated from this code (but not the rest of the DAL) + * The ARM cc compiler is more tolerant. We don't test __GNUC__ here to detect GCC as ARMCC also typically sets this + * as a compatability option, but does not support the options used... + */ +#if !defined(__arm) +#pragma GCC diagnostic ignored "-Wunused-function" +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-parameter" +#endif + +#include "ble.h" + +/* + * Return to our predefined compiler settings. + */ +#if !defined(__arm) +#pragma GCC diagnostic pop +#endif + #define MICROBIT_BLE_ENABLE_BONDING true #define MICROBIT_BLE_REQUIRE_MITM true @@ -98,6 +119,13 @@ void MicroBitBLEManager::init(ManagedString deviceName, ManagedString serialNumb // automatically restart advertising after a device disconnects. ble->onDisconnection(bleDisconnectionCallback); + // configure the stack to hold on to CPU during critical timing events. + // mbed-classic performs __disabe_irq calls in its timers, which can cause MIC failures + // on secure BLE channels. + ble_common_opt_radio_cpu_mutex_t opt; + opt.enable = 1; + sd_ble_opt_set(BLE_COMMON_OPT_RADIO_CPU_MUTEX, (const ble_opt_t *)&opt); + // Setup our security requirements. ble->securityManager().onPasskeyDisplay(passkeyDisplayCallback); ble->securityManager().onSecuritySetupCompleted(securitySetupCompletedCallback);