Improve shutdown to clear BLE API and not just SD

Improve the shutdown functionality, such that a call to ble.shutdown() from
the user application clears the API and nRF5x state and NOT only the
SoftDevice. To achieve this the following changes are introduced:

* Add a protected member cleanup() to nRF5xGap, nRF5xGattClient,
nRF5xGattServer, nRF5xSecurityManager and nRF5xServiceDiscovery.
* Modify the shutdown() implementation in nRF5xn such that it also calls the
static member shutdown() exposed by the BLE API in Gap.h, SecurityManager.h,
GattClient.h and GattServer.h.
* Modify nRF5xGattClient, nRF5xGattServer and nRF5xSecurityManager
classes so that they dynamically create their respective objects only if
needed. Previously the GattClient, GattServer and SecurityManager objects were
declared as static, which means that they were always present even though they
were not always needed. This increases memory consumption unnecessarily.
Furthermore, pointers to the object instances are stored in static members of
the classes as specified by the BLE API base classes. This ensures that
calls to shutdown do not require calls to getInstance() functions that would
otherwise result in undesired memory allocations.
* nRF5xGap object is always needed, so this remains allocated statically. But
the reference in Gap is pointed to this object.

The shutdown procedure is as follows:

1. The user calls ble.shutdown() which executes the code in nRF5xn::shutdown()
1. The SoftDevice is shutdown
1. The static members of Gap.h, SecurityManager.h, GattClient.h and
GattServer.h are called to clean up their own state.

If at any point an error occur during the last step, BLE_ERROR_INVALID_STATE is
returned.
This commit is contained in:
Andres Amaya Garcia 2015-12-11 17:37:58 +00:00
parent 0583503b05
commit 7bf81e7edb
10 changed files with 160 additions and 11 deletions

View file

@ -23,6 +23,9 @@
nRF5xGap &nRF5xGap::getInstance() {
static nRF5xGap m_instance;
if (gapInstance == NULL) {
gapInstance = &m_instance;
}
return m_instance;
}
@ -336,6 +339,29 @@ ble_error_t nRF5xGap::updateConnectionParams(Handle_t handle, const ConnectionPa
}
}
/**************************************************************************/
/*!
@brief Clear nRF5xGap's state.
@returns ble_error_t
@retval BLE_ERROR_NONE
Everything executed properly
*/
/**************************************************************************/
ble_error_t nRF5xGap::cleanup(void)
{
/* Clear all state that is from the parent, including private members */
if (Gap::cleanup() != BLE_ERROR_NONE) {
return BLE_ERROR_INVALID_STATE;
}
/* Clear derived class members */
m_connectionHandle = BLE_CONN_HANDLE_INVALID;
return BLE_ERROR_NONE;
}
/**************************************************************************/
/*!
@brief Sets the 16-bit connection handle
@ -356,7 +382,7 @@ uint16_t nRF5xGap::getConnectionHandle(void)
return m_connectionHandle;
}
/**************************************************************************/
/**************5************************************************************/
/*!
@brief Sets the BLE device address

View file

@ -112,6 +112,9 @@ public:
}
#endif
protected:
virtual ble_error_t cleanup(void);
private:
bool radioNotificationCallbackParam; /* parameter to be passed into the Timeout-generated radio notification callback. */
Timeout radioNotificationTimeout;

View file

@ -18,11 +18,10 @@
nRF5xGattClient &
nRF5xGattClient::getInstance(void) {
static nRF5xGattClient* nRFGattClientSingleton = NULL;
if (nRFGattClientSingleton == NULL) {
nRFGattClientSingleton = new nRF5xGattClient();
if (gattClientInstance == NULL) {
gattClientInstance = new nRF5xGattClient();
}
return *nRFGattClientSingleton;
return (nRF5xGattClient &) *gattClientInstance;
}
#if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110)

View file

@ -147,6 +147,25 @@ public:
}
}
protected:
/**
* @brief Clear nRF5xGattClient's state.
*
* @return
* BLE_ERROR_NONE if successful.
*/
virtual ble_error_t cleanup(void) {
/* Clear all state that is from the parent, including private members */
if (GattClient::cleanup() != BLE_ERROR_NONE) {
return BLE_ERROR_INVALID_STATE;
}
/* Clear derived class members */
discovery.cleanup();
return BLE_ERROR_NONE;
}
public:
nRF5xGattClient() : discovery(this) {
/* empty */

View file

@ -23,8 +23,10 @@
#include "nRF5xGap.h"
nRF5xGattServer &nRF5xGattServer::getInstance(void) {
static nRF5xGattServer m_instance;
return m_instance;
if (gattServerInstance == NULL) {
gattServerInstance = new nRF5xGattServer();
}
return (nRF5xGattServer &) *gattServerInstance;
}
/**************************************************************************/
@ -310,6 +312,33 @@ ble_error_t nRF5xGattServer::areUpdatesEnabled(Gap::Handle_t connectionHandle, c
return BLE_ERROR_NONE;
}
/**************************************************************************/
/*!
@brief Clear nRF5xGattServer's state.
@returns ble_error_t
@retval BLE_ERROR_NONE
Everything executed properly
*/
/**************************************************************************/
ble_error_t nRF5xGattServer::cleanup(void)
{
/* Clear all state that is from the parent, including private members */
if (GattServer::cleanup() != BLE_ERROR_NONE) {
return BLE_ERROR_INVALID_STATE;
}
/* Clear derived class members */
memset(p_characteristics, 0, sizeof(p_characteristics));
memset(p_descriptors, 0, sizeof(p_descriptors));
memset(nrfCharacteristicHandles, 0, sizeof(ble_gatts_char_handles_t));
memset(nrfDescriptorHandles, 0, sizeof(nrfDescriptorHandles));
descriptorCount = 0;
return BLE_ERROR_NONE;
}
/**************************************************************************/
/*!
@brief Callback handler for events getting pushed up from the SD

View file

@ -42,6 +42,9 @@ public:
void eventCallback(void);
void hwCallback(ble_evt_t *p_ble_evt);
protected:
virtual ble_error_t cleanup(void);
private:
const static unsigned BLE_TOTAL_CHARACTERISTICS = 20;
const static unsigned BLE_TOTAL_DESCRIPTORS = 8;

View file

@ -17,9 +17,8 @@
#include "nRF5xSecurityManager.h"
nRF5xSecurityManager &nRF5xSecurityManager::getInstance(void) {
static nRF5xSecurityManager* m_instance = NULL;
if (m_instance == NULL) {
m_instance = new nRF5xSecurityManager();
if (securityManagerInstance == NULL) {
securityManagerInstance = new nRF5xSecurityManager();
}
return *m_instance;
return (nRF5xSecurityManager &) *securityManagerInstance;
}

View file

@ -47,6 +47,21 @@ public:
return btle_purgeAllBondingState();
}
/**
* @brief Clear nRF5xSecurityManager's state.
*
* @return
* BLE_ERROR_NONE if successful.
*/
virtual ble_error_t cleanup(void)
{
if (SecurityManager::cleanup() != BLE_ERROR_NONE) {
return BLE_ERROR_INVALID_STATE;
}
return BLE_ERROR_NONE;
}
public:
nRF5xSecurityManager() {
/* empty */

View file

@ -99,6 +99,34 @@ public:
onTerminationCallback = callback;
}
/**
* @brief Clear nRF5xServiceDiscovery's state.
*
* @return
* BLE_ERROR_NONE if successful.
*/
virtual ble_error_t cleanup(void) {
/* Clear all state that is from the parent, including private members */
if (ServiceDiscovery::cleanup() != BLE_ERROR_NONE) {
return BLE_ERROR_INVALID_STATE;
}
/* Clear derived class members */
serviceIndex = 0;
numServices = 0;
characteristicIndex = 0;
numCharacteristics = 0;
state = INACTIVE;
serviceUUIDDiscoveryQueue.reset();
charUUIDDiscoveryQueue.reset();
onTerminationCallback = NULL;
return BLE_ERROR_NONE;
}
private:
ble_error_t launchCharacteristicDiscovery(Gap::Handle_t connectionHandle, Gap::Handle_t startHandle, Gap::Handle_t endHandle);

View file

@ -104,16 +104,44 @@ ble_error_t nRF5xn::init(BLE::InstanceID_t instanceID, FunctionPointerWithContex
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;
}
/* Shutdown the SoftDevice */
if(softdevice_handler_sd_disable() != NRF_SUCCESS) {
return BLE_STACK_BUSY;
}
/* Shutdown the BLE API and nRF51 glue code */
#if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110)
if (GattServer::shutdown() != BLE_ERROR_NONE ||
SecurityManager::shutdown() != BLE_ERROR_NONE ||
GattClient::shutdown() != BLE_ERROR_NONE ||
Gap::shutdown() != BLE_ERROR_NONE) {
#else
if (GattServer::shutdown() != BLE_ERROR_NONE ||
SecurityManager::shutdown() != BLE_ERROR_NONE ||
Gap::shutdown() != BLE_ERROR_NONE) {
#endif
return BLE_ERROR_INVALID_STATE;
}
initialized = false;
return BLE_ERROR_NONE;
}