Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve shutdown to clear BLE API and not just SD #87

Merged
merged 6 commits into from
Dec 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions source/btle/btle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
#include "custom/custom_helper.h"

#include "ble/GapEvents.h"
#include "nRF5xGap.h"
#include "nRF5xGattServer.h"
#include "nRF5xSecurityManager.h"
#include "nRF5xn.h"

extern "C" {
#include "pstorage.h"
Expand Down Expand Up @@ -138,6 +136,11 @@ static void btle_handler(ble_evt_t *p_ble_evt)
bleGattcEventHandler(p_ble_evt);
#endif

nRF5xn &ble = nRF5xn::Instance(BLE::DEFAULT_INSTANCE);
nRF5xGap &gap = (nRF5xGap &) ble.getGap();
nRF5xGattServer &gattServer = (nRF5xGattServer &) ble.getGattServer();
nRF5xSecurityManager &securityManager = (nRF5xSecurityManager &) ble.getSecurityManager();

/* Custom event handler */
switch (p_ble_evt->header.evt_id) {
case BLE_GAP_EVT_CONNECTED: {
Expand All @@ -148,11 +151,11 @@ static void btle_handler(ble_evt_t *p_ble_evt)
#else
Gap::Role_t role = static_cast<Gap::Role_t>(p_ble_evt->evt.gap_evt.params.connected.role);
#endif
nRF5xGap::getInstance().setConnectionHandle(handle);
gap.setConnectionHandle(handle);
const Gap::ConnectionParams_t *params = reinterpret_cast<Gap::ConnectionParams_t *>(&(p_ble_evt->evt.gap_evt.params.connected.conn_params));
const ble_gap_addr_t *peer = &p_ble_evt->evt.gap_evt.params.connected.peer_addr;
const ble_gap_addr_t *own = &p_ble_evt->evt.gap_evt.params.connected.own_addr;
nRF5xGap::getInstance().processConnectionEvent(handle,
gap.processConnectionEvent(handle,
role,
static_cast<Gap::AddressType_t>(peer->addr_type), peer->addr,
static_cast<Gap::AddressType_t>(own->addr_type), own->addr,
Expand All @@ -164,7 +167,7 @@ static void btle_handler(ble_evt_t *p_ble_evt)
Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle;
// Since we are not in a connection and have not started advertising,
// store bonds
nRF5xGap::getInstance().setConnectionHandle (BLE_CONN_HANDLE_INVALID);
gap.setConnectionHandle (BLE_CONN_HANDLE_INVALID);

Gap::DisconnectionReason_t reason;
switch (p_ble_evt->evt.gap_evt.params.disconnected.reason) {
Expand All @@ -183,16 +186,16 @@ static void btle_handler(ble_evt_t *p_ble_evt)
reason = static_cast<Gap::DisconnectionReason_t>(p_ble_evt->evt.gap_evt.params.disconnected.reason);
break;
}
nRF5xGap::getInstance().processDisconnectionEvent(handle, reason);
gap.processDisconnectionEvent(handle, reason);
break;
}

case BLE_GAP_EVT_PASSKEY_DISPLAY:
nRF5xSecurityManager::getInstance().processPasskeyDisplayEvent(p_ble_evt->evt.gap_evt.conn_handle, p_ble_evt->evt.gap_evt.params.passkey_display.passkey);
securityManager.processPasskeyDisplayEvent(p_ble_evt->evt.gap_evt.conn_handle, p_ble_evt->evt.gap_evt.params.passkey_display.passkey);
break;

case BLE_GAP_EVT_TIMEOUT:
nRF5xGap::getInstance().processTimeoutEvent(static_cast<Gap::TimeoutSource_t>(p_ble_evt->evt.gap_evt.params.timeout.src));
gap.processTimeoutEvent(static_cast<Gap::TimeoutSource_t>(p_ble_evt->evt.gap_evt.params.timeout.src));
break;

case BLE_GATTC_EVT_TIMEOUT:
Expand All @@ -204,20 +207,20 @@ static void btle_handler(ble_evt_t *p_ble_evt)

case BLE_GAP_EVT_ADV_REPORT: {
const ble_gap_evt_adv_report_t *advReport = &p_ble_evt->evt.gap_evt.params.adv_report;
nRF5xGap::getInstance().processAdvertisementReport(advReport->peer_addr.addr,
advReport->rssi,
advReport->scan_rsp,
static_cast<GapAdvertisingParams::AdvertisingType_t>(advReport->type),
advReport->dlen,
advReport->data);
gap.processAdvertisementReport(advReport->peer_addr.addr,
advReport->rssi,
advReport->scan_rsp,
static_cast<GapAdvertisingParams::AdvertisingType_t>(advReport->type),
advReport->dlen,
advReport->data);
break;
}

default:
break;
}

nRF5xGattServer::getInstance().hwCallback(p_ble_evt);
gattServer.hwCallback(p_ble_evt);
}

/*! @brief Callback when an error occurs inside the SoftDevice */
Expand Down
14 changes: 8 additions & 6 deletions source/btle/btle_discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
* limitations under the License.
*/

#include "nRF5xServiceDiscovery.h"
#include "nRF5xGattClient.h"
#include "nRF5xn.h"

#if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110)
void bleGattcEventHandler(const ble_evt_t *p_ble_evt)
{
nRF5xServiceDiscovery &sdSingleton = nRF5xGattClient::getInstance().discovery;
nRF5xn &ble = nRF5xn::Instance(BLE::DEFAULT_INSTANCE);
nRF5xGap &gap = (nRF5xGap &) ble.getGap();
nRF5xGattClient &gattClient = (nRF5xGattClient &) ble.getGattClient();
nRF5xServiceDiscovery &sdSingleton = gattClient.discovery;

switch (p_ble_evt->header.evt_id) {
case BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP:
Expand Down Expand Up @@ -63,7 +65,7 @@ void bleGattcEventHandler(const ble_evt_t *p_ble_evt)
.len = p_ble_evt->evt.gattc_evt.params.read_rsp.len,
.data = p_ble_evt->evt.gattc_evt.params.read_rsp.data,
};
nRF5xGattClient::getInstance().processReadResponse(&response);
gattClient.processReadResponse(&response);
}
break;

Expand All @@ -76,7 +78,7 @@ void bleGattcEventHandler(const ble_evt_t *p_ble_evt)
.len = p_ble_evt->evt.gattc_evt.params.write_rsp.len,
.data = p_ble_evt->evt.gattc_evt.params.write_rsp.data,
};
nRF5xGattClient::getInstance().processWriteResponse(&response);
gattClient.processWriteResponse(&response);
}
break;

Expand All @@ -88,7 +90,7 @@ void bleGattcEventHandler(const ble_evt_t *p_ble_evt)
params.len = p_ble_evt->evt.gattc_evt.params.hvx.len;
params.data = p_ble_evt->evt.gattc_evt.params.hvx.data;

nRF5xGattClient::getInstance().processHVXEvent(&params);
gattClient.processHVXEvent(&params);
}
break;
}
Expand Down
14 changes: 8 additions & 6 deletions source/btle/btle_security.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

#include "btle.h"

#include "nRF5xGap.h"
#include "nRF5xSecurityManager.h"
#include "nRF5xn.h"

extern "C" {
#include "pstorage.h"
Expand Down Expand Up @@ -204,17 +203,20 @@ btle_setLinkSecurity(Gap::Handle_t connectionHandle, SecurityManager::SecurityMo
ret_code_t
dm_handler(dm_handle_t const *p_handle, dm_event_t const *p_event, ret_code_t event_result)
{
nRF5xn &ble = nRF5xn::Instance(BLE::DEFAULT_INSTANCE);
nRF5xSecurityManager &securityManager = (nRF5xSecurityManager &) ble.getSecurityManager();

switch (p_event->event_id) {
case DM_EVT_SECURITY_SETUP: /* started */ {
const ble_gap_sec_params_t *peerParams = &p_event->event_param.p_gap_param->params.sec_params_request.peer_params;
nRF5xSecurityManager::getInstance().processSecuritySetupInitiatedEvent(p_event->event_param.p_gap_param->conn_handle,
securityManager.processSecuritySetupInitiatedEvent(p_event->event_param.p_gap_param->conn_handle,
peerParams->bond,
peerParams->mitm,
(SecurityManager::SecurityIOCapabilities_t)peerParams->io_caps);
break;
}
case DM_EVT_SECURITY_SETUP_COMPLETE:
nRF5xSecurityManager::getInstance().
securityManager.
processSecuritySetupCompletedEvent(p_event->event_param.p_gap_param->conn_handle,
(SecurityManager::SecurityCompletionStatus_t)(p_event->event_param.p_gap_param->params.auth_status.auth_status));
break;
Expand Down Expand Up @@ -248,11 +250,11 @@ dm_handler(dm_handle_t const *p_handle, dm_event_t const *p_event, ret_code_t ev
break;
}

nRF5xSecurityManager::getInstance().processLinkSecuredEvent(p_event->event_param.p_gap_param->conn_handle, resolvedSecurityMode);
securityManager.processLinkSecuredEvent(p_event->event_param.p_gap_param->conn_handle, resolvedSecurityMode);
break;
}
case DM_EVT_DEVICE_CONTEXT_STORED:
nRF5xSecurityManager::getInstance().processSecurityContextStoredEvent(p_event->event_param.p_gap_param->conn_handle);
securityManager.processSecurityContextStoredEvent(p_event->event_param.p_gap_param->conn_handle);
break;
default:
break;
Expand Down
34 changes: 27 additions & 7 deletions source/nRF5xGap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,17 @@
* limitations under the License.
*/

#include "nRF5xGap.h"
#include "nRF5xn.h"
#include "mbed.h"
#include "ble/BLE.h"

#include "common/common.h"
#include "ble_advdata.h"
#include "ble_hci.h"

nRF5xGap &nRF5xGap::getInstance() {
static nRF5xGap m_instance;
return m_instance;
}

void radioNotificationStaticCallback(bool param) {
nRF5xGap::getInstance().processRadioNotificationEvent(param);
nRF5xGap &gap = (nRF5xGap &) nRF5xn::Instance(BLE::DEFAULT_INSTANCE).getGap();
gap.processRadioNotificationEvent(param);
}

/**************************************************************************/
Expand Down Expand Up @@ -336,6 +333,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::reset(void)
{
/* Clear all state that is from the parent, including private members */
if (Gap::reset() != 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
Expand Down
10 changes: 8 additions & 2 deletions source/nRF5xGap.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ void radioNotificationStaticCallback(bool param);
class nRF5xGap : public Gap
{
public:
static nRF5xGap &getInstance();

/* Functions that must be implemented from Gap */
virtual ble_error_t setAddress(AddressType_t type, const Address_t address);
virtual ble_error_t getAddress(AddressType_t *typeP, Address_t address);
Expand Down Expand Up @@ -76,6 +74,8 @@ class nRF5xGap : public Gap
virtual ble_error_t setPreferredConnectionParams(const ConnectionParams_t *params);
virtual ble_error_t updateConnectionParams(Handle_t handle, const ConnectionParams_t *params);

virtual ble_error_t reset(void);

virtual ble_error_t initRadioNotification(void) {
if (ble_radio_notification_init(NRF_APP_PRIORITY_HIGH, NRF_RADIO_NOTIFICATION_DISTANCE_800US, radioNotificationStaticCallback) == NRF_SUCCESS) {
return BLE_ERROR_NONE;
Expand Down Expand Up @@ -196,6 +196,12 @@ class nRF5xGap : public Gap

private:
uint16_t m_connectionHandle;

/*
* Allow instantiation from nRF5xn when required.
*/
friend class nRF5xn;

nRF5xGap() {
m_connectionHandle = BLE_CONN_HANDLE_INVALID;
}
Expand Down
9 changes: 0 additions & 9 deletions source/nRF5xGattClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@

#include "nRF5xGattClient.h"

nRF5xGattClient &
nRF5xGattClient::getInstance(void) {
static nRF5xGattClient* nRFGattClientSingleton = NULL;
if (nRFGattClientSingleton == NULL) {
nRFGattClientSingleton = new nRF5xGattClient();
}
return *nRFGattClientSingleton;
}

#if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110)
ble_error_t
nRF5xGattClient::launchServiceDiscovery(Gap::Handle_t connectionHandle,
Expand Down
25 changes: 23 additions & 2 deletions source/nRF5xGattClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
class nRF5xGattClient : public GattClient
{
public:
static nRF5xGattClient &getInstance();

/**
* When using S110, all Gatt client features will return
* BLE_ERROR_NOT_IMPLEMENTED
Expand Down Expand Up @@ -147,7 +145,30 @@ class nRF5xGattClient : public GattClient
}
}

/**
* @brief Clear nRF5xGattClient's state.
*
* @return
* BLE_ERROR_NONE if successful.
*/
virtual ble_error_t reset(void) {
/* Clear all state that is from the parent, including private members */
if (GattClient::reset() != BLE_ERROR_NONE) {
return BLE_ERROR_INVALID_STATE;
}

/* Clear derived class members */
discovery.reset();

return BLE_ERROR_NONE;
}

public:
/*
* Allow instantiation from nRF5xn when required.
*/
friend class nRF5xn;

nRF5xGattClient() : discovery(this) {
/* empty */
}
Expand Down
40 changes: 32 additions & 8 deletions source/nRF5xGattServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@
#include "common/common.h"
#include "btle/custom/custom_helper.h"

#include "nRF5xGap.h"

nRF5xGattServer &nRF5xGattServer::getInstance(void) {
static nRF5xGattServer m_instance;
return m_instance;
}
#include "nRF5xn.h"

/**************************************************************************/
/*!
Expand Down Expand Up @@ -241,7 +236,8 @@ ble_error_t nRF5xGattServer::write(Gap::Handle_t connectionHandle, GattAttribute
hvx_params.p_len = &len;

if (connectionHandle == BLE_CONN_HANDLE_INVALID) { /* use the default connection handle if the caller hasn't specified a valid connectionHandle. */
connectionHandle = nRF5xGap::getInstance().getConnectionHandle();
nRF5xGap &gap = (nRF5xGap &) nRF5xn::Instance(BLE::DEFAULT_INSTANCE).getGap();
connectionHandle = gap.getConnectionHandle();
}
error_t error = (error_t) sd_ble_gatts_hvx(connectionHandle, &hvx_params);
if (error != ERROR_NONE) {
Expand Down Expand Up @@ -280,7 +276,8 @@ ble_error_t nRF5xGattServer::write(Gap::Handle_t connectionHandle, GattAttribute
ble_error_t nRF5xGattServer::areUpdatesEnabled(const GattCharacteristic &characteristic, bool *enabledP)
{
/* Forward the call with the default connection handle. */
return areUpdatesEnabled(nRF5xGap::getInstance().getConnectionHandle(), characteristic, enabledP);
nRF5xGap &gap = (nRF5xGap &) nRF5xn::Instance(BLE::DEFAULT_INSTANCE).getGap();
return areUpdatesEnabled(gap.getConnectionHandle(), characteristic, enabledP);
}

ble_error_t nRF5xGattServer::areUpdatesEnabled(Gap::Handle_t connectionHandle, const GattCharacteristic &characteristic, bool *enabledP)
Expand Down Expand Up @@ -310,6 +307,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::reset(void)
{
/* Clear all state that is from the parent, including private members */
if (GattServer::reset() != 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
Expand Down
Loading