Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of nRF51822 by
Diff: source/nRF5xn.cpp
- Revision:
- 616:a8f9b022d8fd
- Parent:
- 615:65ea2acfc6a2
--- a/source/nRF5xn.cpp Wed Apr 06 22:38:43 2016 +0100 +++ b/source/nRF5xn.cpp Wed Apr 06 22:39:17 2016 +0100 @@ -14,7 +14,11 @@ * limitations under the License. */ -#include "mbed.h" +#ifdef YOTTA_CFG_MBED_OS + #include "mbed-drivers/mbed.h" +#else + #include "mbed.h" +#endif #include "nRF5xn.h" #include "ble/blecommon.h" #include "nrf_soc.h" @@ -22,12 +26,14 @@ #include "btle/btle.h" #include "nrf_delay.h" +extern "C" { #include "softdevice_handler.h" +} /** * The singleton which represents the nRF51822 transport for the BLE. */ -static nRF5xn deviceInstance; +static nRF5xn *deviceInstance = NULL; /** * BLE-API requires an implementation of the following function in order to @@ -36,10 +42,24 @@ BLEInstanceBase * createBLEInstance(void) { - return (&deviceInstance); + return &nRF5xn::Instance(BLE::DEFAULT_INSTANCE); } -nRF5xn::nRF5xn(void) : initialized(false), instanceID(BLE::DEFAULT_INSTANCE) +nRF5xn& nRF5xn::Instance(BLE::InstanceID_t instanceId) +{ + if (deviceInstance == NULL) + deviceInstance = new nRF5xn(); + + return *deviceInstance; +} + +nRF5xn::nRF5xn(void) : + initialized(false), + instanceID(BLE::DEFAULT_INSTANCE), + gapInstance(), + gattServerInstance(NULL), + gattClientInstance(NULL), + securityManagerInstance(NULL) { } @@ -77,6 +97,20 @@ return versionString; } +/**************************************************************************/ +/*! + @brief Initialize the BLE stack. + + @returns ble_error_t + + @retval BLE_ERROR_NONE if everything executed properly and + BLE_ERROR_ALREADY_INITIALIZED if the stack has already + been initialized (possibly through a call to nRF5xn::init()). + BLE_ERROR_INTERNAL_STACK_FAILURE is returned if initialization + of the internal stack (SoftDevice) failed. + +*/ +/**************************************************************************/ ble_error_t nRF5xn::init(BLE::InstanceID_t instanceID, FunctionPointerWithContext<BLE::InitializationCompleteCallbackContext *> callback) { if (initialized) { @@ -91,7 +125,9 @@ instanceID = instanceID; /* ToDo: Clear memory contents, reset the SD, etc. */ - btle_init(); + if (btle_init() != ERROR_NONE) { + return BLE_ERROR_INTERNAL_STACK_FAILURE; + } initialized = true; BLE::InitializationCompleteCallbackContext context = { @@ -102,16 +138,69 @@ return BLE_ERROR_NONE; } +/**************************************************************************/ +/*! + @brief Purge the BLE stack of GATT and GAP state. + + @returns ble_error_t + + @retval BLE_ERROR_NONE + Everything executed properly + + @note When using S110, GattClient::shutdown() will not be called + since Gatt client features are not supported. +*/ +/**************************************************************************/ ble_error_t nRF5xn::shutdown(void) { if (!initialized) { return BLE_ERROR_INITIALIZATION_INCOMPLETE; } - if(softdevice_handler_sd_disable() != NRF_SUCCESS) { + /* + * Shutdown the SoftDevice first. This is because we need to disable all + * interrupts. Otherwise if we clear the BLE API and glue code first there + * will be many NULL references and no config information which could lead + * to errors if the shutdown process is interrupted. + */ + if (softdevice_handler_sd_disable() != NRF_SUCCESS) { return BLE_STACK_BUSY; } + + /* Shutdown the BLE API and nRF51 glue code */ + ble_error_t error; + + if (gattServerInstance != NULL) { + error = gattServerInstance->reset(); + if (error != BLE_ERROR_NONE) { + return error; + } + } + + if (securityManagerInstance != NULL) { + error = securityManagerInstance->reset(); + if (error != BLE_ERROR_NONE) { + return error; + } + } + + /* S110 does not support BLE client features, nothing to reset. */ +#if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110) + if (gattClientInstance != NULL) { + error = gattClientInstance->reset(); + if (error != BLE_ERROR_NONE) { + return error; + } + } +#endif + + /* Gap instance is always present */ + error = gapInstance.reset(); + if (error != BLE_ERROR_NONE) { + return error; + } + initialized = false; return BLE_ERROR_NONE; }