BLE - Devirtualization of the NRF52 softdevice port.

pull/9727/head
Vincent Coubard 2019-02-25 19:26:35 +00:00
parent ddbb177e33
commit 9ab284940a
14 changed files with 773 additions and 324 deletions

View File

@ -0,0 +1,89 @@
/* mbed Microcontroller Library
* Copyright (c) 2019 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ble/GattServer.h"
#include "source/GattServer.tpp"
#include "nRF5xGattServer.h"
#include "ble/GattClient.h"
#include "source/GattClient.tpp"
#include "ble/generic/GenericGattClient.h"
#include "source/generic/GenericGattClient.tpp"
#include "nRF5xPalGattClient.h"
#include "nRF5xPalGattClient.tpp"
#include "ble/gap/Gap.h"
#include "ble/Gap.h"
#include "source/gap/Gap.tpp"
#include "source/LegacyGap.tpp"
#include "nRF5xGap.h"
#include "ble/SecurityManager.h"
#include "source/SecurityManager.tpp"
#include "generic/GenericSecurityManager.h"
#include "source/generic/GenericSecurityManager.tpp"
#include "nRF5xPalSecurityManager.h"
#include "nRF5xPalSecurityManager.tpp"
#include "DummySigningEventMonitor.h"
// SECURITY MANAGER
template class ble::generic::GenericSecurityManager<
ble::pal::vendor::nordic::nRF5xSecurityManager,
ble::vendor::nordic::DummySigningEventMonitor
>;
template class ::ble::pal::SecurityManager<
ble::impl::PalSecurityManagerImpl,
ble::impl::GenericSecurityManagerImpl
>;
template class ble::pal::vendor::nordic::nRF5xSecurityManager<
ble::generic::GenericSecurityManager<
ble::pal::vendor::nordic::nRF5xSecurityManager,
ble::vendor::nordic::DummySigningEventMonitor
>
>;
template class ble::interface::SecurityManager<
ble::impl::GenericSecurityManagerImpl
>;
// GATT CLIENT
template class ble::generic::GenericGattClient<
ble::pal::vendor::nordic::nRF5xGattClient,
ble::impl::SigningEventHandler
>;
template class ble::pal::vendor::nordic::nRF5xGattClient<
ble::impl::GenericGattClientImpl
>;
template class ble::interface::GattClient<
ble::impl::GenericGattClientImpl
>;
// GATT SERVER
template class ble::interface::GattServer<nRF5xGattServer>;
// GAP
// Instantiated in nRF5xGap.cpp as a workaround for ARMCC5
//template class ble::interface::LegacyGap<nRF5xGap>;
//template class ble::interface::Gap<nRF5xGap>;

View File

@ -0,0 +1,151 @@
/* mbed Microcontroller Library
* Copyright (c) 2019 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef BLE_IMPLEMENTATION_FORWARD_H_
#define BLE_IMPLEMENTATION_FORWARD_H_
////////////////////////////////////////////////////////////////////////////////
// Forward declarations of the implementation types
//
namespace ble {
namespace interface {
template<class Impl>
class LegacyGap;
template<class Impl>
class Gap;
template<class Impl>
class GattClient;
template<class Impl>
class SecurityManager;
template<class Impl>
class GattServer;
} // namespace interface
namespace generic {
template <
template<class> class TPalSecurityManager,
template<class> class SigningMonitor
>
class GenericSecurityManager;
template<
template<class> class TPalGattClient,
class SigningMonitorEventHandler
>
class GenericGattClient;
} // namespace generic
namespace pal {
template<class Impl, class EventHandler>
class SecurityManager;
template<class Impl>
class SigningMonitorEventHandler;
template<class Impl>
class ConnectionEventMonitorEventHandler;
namespace vendor {
namespace nordic {
template <class EventHandler>
class nRF5xSecurityManager;
template<class EventHandler>
class nRF5xGattClient;
} // nordic
} // vendor
} // pal
namespace vendor {
namespace nordic {
template<class>
class DummySigningEventMonitor;
} // nordic
} // vendor
} // ble
class nRF5xGap;
class nRF5xGattServer;
// implementation assembly
namespace ble {
namespace impl {
// SECURITY MANAGER
typedef generic::GenericSecurityManager<
pal::vendor::nordic::nRF5xSecurityManager,
vendor::nordic::DummySigningEventMonitor
> GenericSecurityManagerImpl;
typedef interface::SecurityManager<GenericSecurityManagerImpl> SecurityManager;
typedef GenericSecurityManagerImpl SigningEventHandler;
typedef GenericSecurityManagerImpl ConnectionEventHandler;
typedef pal::vendor::nordic::nRF5xSecurityManager<
GenericSecurityManagerImpl
> PalSecurityManagerImpl;
typedef ::ble::pal::SecurityManager<
PalSecurityManagerImpl,
GenericSecurityManagerImpl
> PalSecurityManager;
// GAP
typedef interface::LegacyGap<nRF5xGap> LegacyGap;
typedef interface::Gap<nRF5xGap> Gap;
// GATT CLIENT
typedef generic::GenericGattClient<
ble::pal::vendor::nordic::nRF5xGattClient,
SigningEventHandler
> GenericGattClientImpl;
typedef ble::pal::vendor::nordic::nRF5xGattClient<
GenericGattClientImpl
> PalGattClientImpl;
// construct the final GattClient type
typedef interface::GattClient<GenericGattClientImpl> GattClient;
// GATT SERVER
typedef ble::interface::GattServer<nRF5xGattServer> GattServer;
} // impl
} // ble
#endif //BLE_IMPLEMENTATION_FORWARD_H_

View File

@ -0,0 +1,40 @@
/* mbed Microcontroller Library
* Copyright (c) 2019 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef NRF5X_DUMMYSIGNINGEVENTMONITOR_H
#define NRF5X_DUMMYSIGNINGEVENTMONITOR_H
#include "ble/pal/SigningEventMonitor.h"
namespace ble {
namespace vendor {
namespace nordic {
template<class Handler>
struct DummySigningEventMonitor :
public ble::pal::SigningEventMonitor<DummySigningEventMonitor<Handler>, Handler >
{
void set_signing_event_handler_(Handler *signing_event_handler)
{
}
};
} // nordic
} // vendor
} // ble
#endif //NRF5X_DUMMYSIGNINGEVENTMONITOR_H

View File

@ -281,13 +281,14 @@ void btle_handler(const ble_evt_t *p_ble_evt)
#endif #endif
#if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110) #if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110)
nRF5xGattClient::handle_events(p_ble_evt); ble::impl::PalGattClientImpl::handle_events(p_ble_evt);
#endif #endif
nRF5xn &ble = nRF5xn::Instance(BLE::DEFAULT_INSTANCE); nRF5xn &ble = nRF5xn::Instance(BLE::DEFAULT_INSTANCE);
nRF5xGap &gap = (nRF5xGap &) ble.getGap(); nRF5xGap &gap = (nRF5xGap &) ble.getGap();
nRF5xGattServer &gattServer = (nRF5xGattServer &) ble.getGattServer(); nRF5xGattServer &gattServer = (nRF5xGattServer &) ble.getGattServer();
nRF5xSecurityManager &securityManager = nRF5xSecurityManager::get_security_manager(); ble::impl::PalSecurityManagerImpl &securityManager =
ble::impl::PalSecurityManagerImpl::get_security_manager();
/* Custom event handler */ /* Custom event handler */
switch (p_ble_evt->header.evt_id) { switch (p_ble_evt->header.evt_id) {
@ -324,7 +325,7 @@ void btle_handler(const ble_evt_t *p_ble_evt)
#if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110) #if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110)
// Close all pending discoveries for this connection // Close all pending discoveries for this connection
nRF5xGattClient::handle_connection_termination(handle); ble::impl::PalGattClientImpl::handle_connection_termination(handle);
#endif #endif
gap.processDisconnectionEvent(handle, reason); gap.processDisconnectionEvent(handle, reason);
@ -417,7 +418,7 @@ void btle_handler(const ble_evt_t *p_ble_evt)
const ble_gattc_evt_exchange_mtu_rsp_t &update = const ble_gattc_evt_exchange_mtu_rsp_t &update =
p_ble_evt->evt.gattc_evt.params.exchange_mtu_rsp; p_ble_evt->evt.gattc_evt.params.exchange_mtu_rsp;
nRF5xGattClient &gatt_client = (nRF5xGattClient&) ble.getGattClient(); ble::impl::PalGattClientImpl &gatt_client = (ble::impl::PalGattClientImpl&) ble.getGattClient();
if (gatt_client.get_event_handler()) { if (gatt_client.get_event_handler()) {
gatt_client.get_event_handler()->on_att_mtu_change( gatt_client.get_event_handler()->on_att_mtu_change(
connection, connection,

View File

@ -26,8 +26,19 @@
#include "headers/ble_hci.h" #include "headers/ble_hci.h"
#include "ble/pal/ConnectionEventMonitor.h" #include "ble/pal/ConnectionEventMonitor.h"
#include "nRF5xPalSecurityManager.h" #include "nRF5xPalSecurityManager.h"
#include "BleImplementationForward.h"
#include <algorithm> #include <algorithm>
// ARMCC5 is not able to export static variable of explicitly instantiated class
// template if not used immediately. The default privacy configurations are used
// in this file so we instantiate them here.
#include "source/gap/Gap.tpp"
#include "source/LegacyGap.tpp"
template class ble::interface::LegacyGap<nRF5xGap>;
template class ble::interface::Gap<nRF5xGap>;
using ble::pal::vendor::nordic::nRF5xSecurityManager; using ble::pal::vendor::nordic::nRF5xSecurityManager;
using ble::ArrayView; using ble::ArrayView;
using ble::pal::advertising_peer_address_type_t; using ble::pal::advertising_peer_address_type_t;
@ -38,8 +49,8 @@ typedef BLEProtocol::AddressType_t LegacyAddressType_t;
namespace { namespace {
nRF5xSecurityManager& get_sm() { ble::impl::PalSecurityManagerImpl& get_sm() {
return nRF5xSecurityManager::get_security_manager(); return ble::impl::PalSecurityManagerImpl::get_security_manager();
} }
ble_error_t set_private_resolvable_address() { ble_error_t set_private_resolvable_address() {
@ -144,14 +155,13 @@ void radioNotificationStaticCallback(bool param) {
gap.processRadioNotificationEvent(param); gap.processRadioNotificationEvent(param);
} }
nRF5xGap::nRF5xGap() : Gap(), nRF5xGap::nRF5xGap() :
advertisingPolicyMode(Gap::ADV_POLICY_IGNORE_WHITELIST), advertisingPolicyMode(ADV_POLICY_IGNORE_WHITELIST),
scanningPolicyMode(Gap::SCAN_POLICY_IGNORE_WHITELIST), scanningPolicyMode(SCAN_POLICY_IGNORE_WHITELIST),
whitelistAddressesSize(0), whitelistAddressesSize(0),
whitelistAddresses(), whitelistAddresses(),
radioNotificationCallbackParam(false), radioNotificationCallbackParam(false),
radioNotificationTimeout(), radioNotificationTimeout(),
_connection_event_handler(NULL),
_privacy_enabled(false), _privacy_enabled(false),
_peripheral_privacy_configuration(default_peripheral_privacy_configuration), _peripheral_privacy_configuration(default_peripheral_privacy_configuration),
_central_privacy_configuration(default_central_privacy_configuration), _central_privacy_configuration(default_central_privacy_configuration),
@ -199,7 +209,7 @@ nRF5xGap::nRF5xGap() : Gap(),
@endcode @endcode
*/ */
/**************************************************************************/ /**************************************************************************/
ble_error_t nRF5xGap::setAdvertisingData(const GapAdvertisingData &advData, const GapAdvertisingData &scanResponse) ble_error_t nRF5xGap::setAdvertisingData_(const GapAdvertisingData &advData, const GapAdvertisingData &scanResponse)
{ {
/* Make sure we don't exceed the advertising payload length */ /* Make sure we don't exceed the advertising payload length */
if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD) { if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD) {
@ -264,7 +274,7 @@ ble_error_t nRF5xGap::setAdvertisingData(const GapAdvertisingData &advData, cons
@endcode @endcode
*/ */
/**************************************************************************/ /**************************************************************************/
ble_error_t nRF5xGap::startAdvertising(const GapAdvertisingParams &params) ble_error_t nRF5xGap::startAdvertising_(const GapAdvertisingParams &params)
{ {
uint32_t err; uint32_t err;
ble_gap_adv_params_t adv_para = {0}; ble_gap_adv_params_t adv_para = {0};
@ -387,7 +397,7 @@ ble_error_t nRF5xGap::startAdvertising(const GapAdvertisingParams &params)
/* Observer role is not supported by S110, return BLE_ERROR_NOT_IMPLEMENTED */ /* Observer role is not supported by S110, return BLE_ERROR_NOT_IMPLEMENTED */
#if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110) #if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110)
ble_error_t nRF5xGap::startRadioScan(const GapScanningParams &scanningParams) ble_error_t nRF5xGap::startRadioScan_(const GapScanningParams &scanningParams)
{ {
ble_gap_scan_params_t scanParams; ble_gap_scan_params_t scanParams;
@ -447,7 +457,7 @@ ble_error_t nRF5xGap::startRadioScan(const GapScanningParams &scanningParams)
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
} }
ble_error_t nRF5xGap::stopScan(void) { ble_error_t nRF5xGap::stopScan_(void) {
if (sd_ble_gap_scan_stop() == NRF_SUCCESS) { if (sd_ble_gap_scan_stop() == NRF_SUCCESS) {
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
} }
@ -472,7 +482,7 @@ ble_error_t nRF5xGap::stopScan(void) {
@endcode @endcode
*/ */
/**************************************************************************/ /**************************************************************************/
ble_error_t nRF5xGap::stopAdvertising(void) ble_error_t nRF5xGap::stopAdvertising_(void)
{ {
/* Stop Advertising */ /* Stop Advertising */
ASSERT_TRUE(ERROR_NONE == sd_ble_gap_adv_stop(), BLE_ERROR_PARAM_OUT_OF_RANGE); ASSERT_TRUE(ERROR_NONE == sd_ble_gap_adv_stop(), BLE_ERROR_PARAM_OUT_OF_RANGE);
@ -482,7 +492,7 @@ ble_error_t nRF5xGap::stopAdvertising(void)
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
} }
ble_error_t nRF5xGap::connect( ble_error_t nRF5xGap::connect_(
const Address_t peerAddr, const Address_t peerAddr,
peer_address_type_t peerAddrType, peer_address_type_t peerAddrType,
const ConnectionParams_t *connectionParams, const ConnectionParams_t *connectionParams,
@ -534,7 +544,7 @@ ble_error_t nRF5xGap::connect(
} }
ble_error_t nRF5xGap::connect( ble_error_t nRF5xGap::connect_(
const Address_t peerAddr, const Address_t peerAddr,
LegacyAddressType_t peerAddrType, LegacyAddressType_t peerAddrType,
const ConnectionParams_t *connectionParams, const ConnectionParams_t *connectionParams,
@ -553,7 +563,7 @@ ble_error_t nRF5xGap::connect(
ble_gap_addr_t addr; ble_gap_addr_t addr;
ble_gap_addr_t* addr_ptr = &addr; ble_gap_addr_t* addr_ptr = &addr;
addr.addr_type = peerAddrType; addr.addr_type = peerAddrType;
memcpy(addr.addr, peerAddr, Gap::ADDR_LEN); memcpy(addr.addr, peerAddr, ADDR_LEN);
ble_gap_conn_params_t connParams; ble_gap_conn_params_t connParams;
if (connectionParams != NULL) { if (connectionParams != NULL) {
@ -678,7 +688,7 @@ ble_error_t nRF5xGap::connect(
} }
} }
ble_error_t nRF5xGap::readPhy(Handle_t connection) { ble_error_t nRF5xGap::readPhy_(Handle_t connection) {
/* /*
* This function is not implemented as it is not possible with current * This function is not implemented as it is not possible with current
* nordic API to know which phy was used to establish the connection. * nordic API to know which phy was used to establish the connection.
@ -688,7 +698,7 @@ ble_error_t nRF5xGap::readPhy(Handle_t connection) {
return BLE_ERROR_NOT_IMPLEMENTED; return BLE_ERROR_NOT_IMPLEMENTED;
} }
ble_error_t nRF5xGap::setPreferredPhys( ble_error_t nRF5xGap::setPreferredPhys_(
const ble::phy_set_t* txPhys, const ble::phy_set_t* txPhys,
const ble::phy_set_t* rxPhys const ble::phy_set_t* rxPhys
) { ) {
@ -725,7 +735,7 @@ ble_error_t nRF5xGap::setPreferredPhys(
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
} }
ble_error_t nRF5xGap::setPhy( ble_error_t nRF5xGap::setPhy_(
Handle_t connection, Handle_t connection,
const ble::phy_set_t* txPhys, const ble::phy_set_t* txPhys,
const ble::phy_set_t* rxPhys, const ble::phy_set_t* rxPhys,
@ -759,7 +769,7 @@ ble_error_t nRF5xGap::setPhy(
#endif #endif
} }
ble_error_t nRF5xGap::disconnect(Handle_t connectionHandle, DisconnectionReason_t reason) ble_error_t nRF5xGap::disconnect_(Handle_t connectionHandle, DisconnectionReason_t reason)
{ {
uint8_t code = BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION; uint8_t code = BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION;
switch (reason) { switch (reason) {
@ -787,12 +797,12 @@ ble_error_t nRF5xGap::disconnect(Handle_t connectionHandle, DisconnectionReason_
@retval BLE_ERROR_NONE @retval BLE_ERROR_NONE
Everything executed properly Everything executed properly
*/ */
ble_error_t nRF5xGap::disconnect(DisconnectionReason_t reason) ble_error_t nRF5xGap::disconnect_(DisconnectionReason_t reason)
{ {
return disconnect(m_connectionHandle, reason); return disconnect(m_connectionHandle, reason);
} }
ble_error_t nRF5xGap::getPreferredConnectionParams(ConnectionParams_t *params) ble_error_t nRF5xGap::getPreferredConnectionParams_(ConnectionParams_t *params)
{ {
ASSERT_INT(NRF_SUCCESS, ASSERT_INT(NRF_SUCCESS,
sd_ble_gap_ppcp_get(reinterpret_cast<ble_gap_conn_params_t *>(params)), sd_ble_gap_ppcp_get(reinterpret_cast<ble_gap_conn_params_t *>(params)),
@ -801,7 +811,7 @@ ble_error_t nRF5xGap::getPreferredConnectionParams(ConnectionParams_t *params)
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
} }
ble_error_t nRF5xGap::setPreferredConnectionParams(const ConnectionParams_t *params) ble_error_t nRF5xGap::setPreferredConnectionParams_(const ConnectionParams_t *params)
{ {
ASSERT_INT(NRF_SUCCESS, ASSERT_INT(NRF_SUCCESS,
sd_ble_gap_ppcp_set(reinterpret_cast<const ble_gap_conn_params_t *>(params)), sd_ble_gap_ppcp_set(reinterpret_cast<const ble_gap_conn_params_t *>(params)),
@ -810,7 +820,7 @@ ble_error_t nRF5xGap::setPreferredConnectionParams(const ConnectionParams_t *par
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
} }
ble_error_t nRF5xGap::updateConnectionParams(Handle_t handle, const ConnectionParams_t *newParams) ble_error_t nRF5xGap::updateConnectionParams_(Handle_t handle, const ConnectionParams_t *newParams)
{ {
uint32_t rc; uint32_t rc;
@ -832,10 +842,10 @@ ble_error_t nRF5xGap::updateConnectionParams(Handle_t handle, const ConnectionPa
Everything executed properly Everything executed properly
*/ */
/**************************************************************************/ /**************************************************************************/
ble_error_t nRF5xGap::reset(void) ble_error_t nRF5xGap::reset_(void)
{ {
/* Clear all state that is from the parent, including private members */ /* Clear all state that is from the parent, including private members */
if (Gap::reset() != BLE_ERROR_NONE) { if (ble::interface::LegacyGap<nRF5xGap>::reset_() != BLE_ERROR_NONE) {
return BLE_ERROR_INVALID_STATE; return BLE_ERROR_INVALID_STATE;
} }
@ -843,8 +853,8 @@ ble_error_t nRF5xGap::reset(void)
m_connectionHandle = BLE_CONN_HANDLE_INVALID; m_connectionHandle = BLE_CONN_HANDLE_INVALID;
/* Set the whitelist policy filter modes to IGNORE_WHITELIST */ /* Set the whitelist policy filter modes to IGNORE_WHITELIST */
advertisingPolicyMode = Gap::ADV_POLICY_IGNORE_WHITELIST; advertisingPolicyMode = ADV_POLICY_IGNORE_WHITELIST;
scanningPolicyMode = Gap::SCAN_POLICY_IGNORE_WHITELIST; scanningPolicyMode = SCAN_POLICY_IGNORE_WHITELIST;
/* Clear the internal whitelist */ /* Clear the internal whitelist */
whitelistAddressesSize = 0; whitelistAddressesSize = 0;
@ -891,7 +901,7 @@ uint16_t nRF5xGap::getConnectionHandle(void)
@endcode @endcode
*/ */
/**************************************************************************/ /**************************************************************************/
ble_error_t nRF5xGap::setAddress(LegacyAddressType_t type, const Address_t address) ble_error_t nRF5xGap::setAddress_(LegacyAddressType_t type, const Address_t address)
{ {
if (type != LegacyAddressType::PUBLIC && if (type != LegacyAddressType::PUBLIC &&
type != LegacyAddressType::RANDOM_STATIC type != LegacyAddressType::RANDOM_STATIC
@ -933,7 +943,7 @@ ble_error_t nRF5xGap::setAddress(LegacyAddressType_t type, const Address_t addre
} }
} }
ble_error_t nRF5xGap::getAddress(AddressType_t *typeP, Address_t address) ble_error_t nRF5xGap::getAddress_(AddressType_t *typeP, Address_t address)
{ {
// FIXME: check if privacy is enabled ? // FIXME: check if privacy is enabled ?
if (typeP == NULL || address == NULL) { if (typeP == NULL || address == NULL) {
@ -966,7 +976,7 @@ ble_error_t nRF5xGap::getAddress(AddressType_t *typeP, Address_t address)
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
} }
ble_error_t nRF5xGap::setDeviceName(const uint8_t *deviceName) ble_error_t nRF5xGap::setDeviceName_(const uint8_t *deviceName)
{ {
ble_gap_conn_sec_mode_t sec_mode; ble_gap_conn_sec_mode_t sec_mode;
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); // no security is needed BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); // no security is needed
@ -978,7 +988,7 @@ ble_error_t nRF5xGap::setDeviceName(const uint8_t *deviceName)
} }
} }
ble_error_t nRF5xGap::getDeviceName(uint8_t *deviceName, unsigned *lengthP) ble_error_t nRF5xGap::getDeviceName_(uint8_t *deviceName, unsigned *lengthP)
{ {
if (sd_ble_gap_device_name_get(deviceName, (uint16_t *)lengthP) == NRF_SUCCESS) { if (sd_ble_gap_device_name_get(deviceName, (uint16_t *)lengthP) == NRF_SUCCESS) {
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
@ -987,7 +997,7 @@ ble_error_t nRF5xGap::getDeviceName(uint8_t *deviceName, unsigned *lengthP)
} }
} }
ble_error_t nRF5xGap::setAppearance(GapAdvertisingData::Appearance appearance) ble_error_t nRF5xGap::setAppearance_(GapAdvertisingData::Appearance appearance)
{ {
if (sd_ble_gap_appearance_set(appearance) == NRF_SUCCESS) { if (sd_ble_gap_appearance_set(appearance) == NRF_SUCCESS) {
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
@ -996,7 +1006,7 @@ ble_error_t nRF5xGap::setAppearance(GapAdvertisingData::Appearance appearance)
} }
} }
ble_error_t nRF5xGap::getAppearance(GapAdvertisingData::Appearance *appearanceP) ble_error_t nRF5xGap::getAppearance_(GapAdvertisingData::Appearance *appearanceP)
{ {
if ((sd_ble_gap_appearance_get(reinterpret_cast<uint16_t *>(appearanceP)) == NRF_SUCCESS)) { if ((sd_ble_gap_appearance_get(reinterpret_cast<uint16_t *>(appearanceP)) == NRF_SUCCESS)) {
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
@ -1006,7 +1016,7 @@ ble_error_t nRF5xGap::getAppearance(GapAdvertisingData::Appearance *appearanceP)
} }
/* (Valid values are -40, -20, -16, -12, -8, -4, 0, 4) */ /* (Valid values are -40, -20, -16, -12, -8, -4, 0, 4) */
ble_error_t nRF5xGap::setTxPower(int8_t txPower) ble_error_t nRF5xGap::setTxPower_(int8_t txPower)
{ {
unsigned rc; unsigned rc;
if ((rc = sd_ble_gap_tx_power_set(txPower)) != NRF_SUCCESS) { if ((rc = sd_ble_gap_tx_power_set(txPower)) != NRF_SUCCESS) {
@ -1022,7 +1032,7 @@ ble_error_t nRF5xGap::setTxPower(int8_t txPower)
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
} }
void nRF5xGap::getPermittedTxPowerValues(const int8_t **valueArrayPP, size_t *countP) void nRF5xGap::getPermittedTxPowerValues_(const int8_t **valueArrayPP, size_t *countP)
{ {
#if defined(NRF51) #if defined(NRF51)
static const int8_t permittedTxValues[] = { static const int8_t permittedTxValues[] = {
@ -1064,7 +1074,7 @@ void nRF5xGap::getPermittedTxPowerValues(const int8_t **valueArrayPP, size_t *co
@endcode @endcode
*/ */
/**************************************************************************/ /**************************************************************************/
uint8_t nRF5xGap::getMaxWhitelistSize(void) const uint8_t nRF5xGap::getMaxWhitelistSize_(void) const
{ {
return YOTTA_CFG_WHITELIST_MAX_SIZE; return YOTTA_CFG_WHITELIST_MAX_SIZE;
} }
@ -1089,7 +1099,7 @@ uint8_t nRF5xGap::getMaxWhitelistSize(void) const
@endcode @endcode
*/ */
/**************************************************************************/ /**************************************************************************/
ble_error_t nRF5xGap::getWhitelist(Gap::Whitelist_t &whitelistOut) const ble_error_t nRF5xGap::getWhitelist_(Whitelist_t &whitelistOut) const
{ {
uint32_t i; uint32_t i;
for (i = 0; i < whitelistAddressesSize && i < whitelistOut.capacity; ++i) { for (i = 0; i < whitelistAddressesSize && i < whitelistOut.capacity; ++i) {
@ -1133,7 +1143,7 @@ ble_error_t nRF5xGap::getWhitelist(Gap::Whitelist_t &whitelistOut) const
@endcode @endcode
*/ */
/**************************************************************************/ /**************************************************************************/
ble_error_t nRF5xGap::setWhitelist(const Gap::Whitelist_t &whitelistIn) ble_error_t nRF5xGap::setWhitelist_(const Whitelist_t &whitelistIn)
{ {
if (whitelistIn.size > getMaxWhitelistSize()) { if (whitelistIn.size > getMaxWhitelistSize()) {
return BLE_ERROR_PARAM_OUT_OF_RANGE; return BLE_ERROR_PARAM_OUT_OF_RANGE;
@ -1199,7 +1209,7 @@ ble_error_t nRF5xGap::setWhitelist(const Gap::Whitelist_t &whitelistIn)
@endcode @endcode
*/ */
/**************************************************************************/ /**************************************************************************/
ble_error_t nRF5xGap::setAdvertisingPolicyMode(Gap::AdvertisingPolicyMode_t mode) ble_error_t nRF5xGap::setAdvertisingPolicyMode_(AdvertisingPolicyMode_t mode)
{ {
advertisingPolicyMode = mode; advertisingPolicyMode = mode;
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
@ -1225,7 +1235,7 @@ ble_error_t nRF5xGap::setAdvertisingPolicyMode(Gap::AdvertisingPolicyMode_t mode
@endcode @endcode
*/ */
/**************************************************************************/ /**************************************************************************/
ble_error_t nRF5xGap::setScanningPolicyMode(Gap::ScanningPolicyMode_t mode) ble_error_t nRF5xGap::setScanningPolicyMode_(ScanningPolicyMode_t mode)
{ {
scanningPolicyMode = mode; scanningPolicyMode = mode;
@ -1252,7 +1262,7 @@ ble_error_t nRF5xGap::setScanningPolicyMode(Gap::ScanningPolicyMode_t mode)
@endcode @endcode
*/ */
/**************************************************************************/ /**************************************************************************/
ble_error_t nRF5xGap::setInitiatorPolicyMode(Gap::InitiatorPolicyMode_t mode) ble_error_t nRF5xGap::setInitiatorPolicyMode_(InitiatorPolicyMode_t mode)
{ {
return BLE_ERROR_NOT_IMPLEMENTED; return BLE_ERROR_NOT_IMPLEMENTED;
} }
@ -1270,7 +1280,7 @@ ble_error_t nRF5xGap::setInitiatorPolicyMode(Gap::InitiatorPolicyMode_t mode)
@endcode @endcode
*/ */
/**************************************************************************/ /**************************************************************************/
Gap::AdvertisingPolicyMode_t nRF5xGap::getAdvertisingPolicyMode(void) const Gap::AdvertisingPolicyMode_t nRF5xGap::getAdvertisingPolicyMode_(void) const
{ {
return advertisingPolicyMode; return advertisingPolicyMode;
} }
@ -1288,7 +1298,7 @@ Gap::AdvertisingPolicyMode_t nRF5xGap::getAdvertisingPolicyMode(void) const
@endcode @endcode
*/ */
/**************************************************************************/ /**************************************************************************/
Gap::ScanningPolicyMode_t nRF5xGap::getScanningPolicyMode(void) const Gap::ScanningPolicyMode_t nRF5xGap::getScanningPolicyMode_(void) const
{ {
return scanningPolicyMode; return scanningPolicyMode;
} }
@ -1309,12 +1319,12 @@ Gap::ScanningPolicyMode_t nRF5xGap::getScanningPolicyMode(void) const
@endcode @endcode
*/ */
/**************************************************************************/ /**************************************************************************/
Gap::InitiatorPolicyMode_t nRF5xGap::getInitiatorPolicyMode(void) const Gap::InitiatorPolicyMode_t nRF5xGap::getInitiatorPolicyMode_(void) const
{ {
return Gap::INIT_POLICY_IGNORE_WHITELIST; return INIT_POLICY_IGNORE_WHITELIST;
} }
ble_error_t nRF5xGap::enablePrivacy(bool enable_privacy) ble_error_t nRF5xGap::enablePrivacy_(bool enable_privacy)
{ {
if (enable_privacy == _privacy_enabled) { if (enable_privacy == _privacy_enabled) {
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
@ -1349,40 +1359,34 @@ ble_error_t nRF5xGap::enablePrivacy(bool enable_privacy)
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
} }
ble_error_t nRF5xGap::setPeripheralPrivacyConfiguration( ble_error_t nRF5xGap::setPeripheralPrivacyConfiguration_(
const PeripheralPrivacyConfiguration_t *configuration const PeripheralPrivacyConfiguration_t *configuration
) { ) {
_peripheral_privacy_configuration = *configuration; _peripheral_privacy_configuration = *configuration;
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
} }
ble_error_t nRF5xGap::getPeripheralPrivacyConfiguration( ble_error_t nRF5xGap::getPeripheralPrivacyConfiguration_(
PeripheralPrivacyConfiguration_t *configuration PeripheralPrivacyConfiguration_t *configuration
) { ) {
*configuration = _peripheral_privacy_configuration; *configuration = _peripheral_privacy_configuration;
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
} }
ble_error_t nRF5xGap::setCentralPrivacyConfiguration( ble_error_t nRF5xGap::setCentralPrivacyConfiguration_(
const CentralPrivacyConfiguration_t *configuration const CentralPrivacyConfiguration_t *configuration
) { ) {
_central_privacy_configuration = *configuration; _central_privacy_configuration = *configuration;
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
} }
ble_error_t nRF5xGap::getCentralPrivacyConfiguration( ble_error_t nRF5xGap::getCentralPrivacyConfiguration_(
CentralPrivacyConfiguration_t *configuration CentralPrivacyConfiguration_t *configuration
) { ) {
*configuration = _central_privacy_configuration; *configuration = _central_privacy_configuration;
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
} }
void nRF5xGap::set_connection_event_handler(
ConnectionEventMonitor::EventHandler* connection_event_handler
) {
_connection_event_handler = connection_event_handler;
}
void nRF5xGap::processDisconnectionEvent( void nRF5xGap::processDisconnectionEvent(
Handle_t handle, Handle_t handle,
DisconnectionReason_t reason DisconnectionReason_t reason
@ -1433,7 +1437,7 @@ ble_error_t nRF5xGap::update_identities_list(bool resolution_enabled)
return err ? BLE_ERROR_INVALID_STATE : BLE_ERROR_NONE; return err ? BLE_ERROR_INVALID_STATE : BLE_ERROR_NONE;
} }
void nRF5xGap::on_connection(Gap::Handle_t handle, const ble_gap_evt_connected_t& evt) { void nRF5xGap::on_connection(Handle_t handle, const ble_gap_evt_connected_t& evt) {
// set the new connection handle as the _default_ handle in gap // set the new connection handle as the _default_ handle in gap
setConnectionHandle(handle); setConnectionHandle(handle);
@ -1591,7 +1595,7 @@ void nRF5xGap::allocate_connection_role(
connection_role_t& c = _connections_role[i]; connection_role_t& c = _connections_role[i];
if (c.is_allocated == false) { if (c.is_allocated == false) {
c.connection = connection; c.connection = connection;
c.is_peripheral = (role == Gap::PERIPHERAL); c.is_peripheral = (role == PERIPHERAL);
c.is_allocated = true; c.is_allocated = true;
return; return;
} }

View File

@ -17,6 +17,8 @@
#ifndef __NRF5x_GAP_H__ #ifndef __NRF5x_GAP_H__
#define __NRF5x_GAP_H__ #define __NRF5x_GAP_H__
#include "ble/Gap.h"
#ifdef YOTTA_CFG_MBED_OS #ifdef YOTTA_CFG_MBED_OS
#include "mbed-drivers/mbed.h" #include "mbed-drivers/mbed.h"
#else #else
@ -61,18 +63,23 @@ void radioNotificationStaticCallback(bool param);
*/ */
/**************************************************************************/ /**************************************************************************/
class nRF5xGap : public ::Gap, public ble::pal::ConnectionEventMonitor { class nRF5xGap :
public ble::interface::LegacyGap<nRF5xGap>,
public ble::pal::ConnectionEventMonitor<ble::impl::ConnectionEventHandler> {
public: public:
nRF5xGap(); nRF5xGap();
virtual ~nRF5xGap() { } ~nRF5xGap() { }
/* Functions that must be implemented from Gap */ /* Functions that must be implemented from Gap */
virtual ble_error_t setAddress(AddressType_t type, const Address_t address); ble_error_t setAddress_(AddressType_t type, const Address_t address);
virtual ble_error_t getAddress(AddressType_t *typeP, Address_t address); ble_error_t getAddress_(AddressType_t *typeP, Address_t address);
virtual ble_error_t setAdvertisingData(const GapAdvertisingData &, const GapAdvertisingData &); ble_error_t setAdvertisingData_(const GapAdvertisingData &, const GapAdvertisingData &);
virtual uint16_t getMinAdvertisingInterval(void) const {return GapAdvertisingParams::ADVERTISEMENT_DURATION_UNITS_TO_MS(BLE_GAP_ADV_INTERVAL_MIN);} uint16_t getMinAdvertisingInterval_(void) const {
virtual uint16_t getMinNonConnectableAdvertisingInterval(void) const { return GapAdvertisingParams::ADVERTISEMENT_DURATION_UNITS_TO_MS(BLE_GAP_ADV_INTERVAL_MIN);
}
uint16_t getMinNonConnectableAdvertisingInterval_(void) const {
#if (NRF_SD_BLE_API_VERSION >= 5) // In SD v5+, non connectable advertising interval is the same as connectable advertising interval #if (NRF_SD_BLE_API_VERSION >= 5) // In SD v5+, non connectable advertising interval is the same as connectable advertising interval
// The Mbed infrastructure expects 100ms+ - so for now return that // The Mbed infrastructure expects 100ms+ - so for now return that
// return getMinAdvertisingInterval(); // return getMinAdvertisingInterval();
@ -82,62 +89,64 @@ public:
return GapAdvertisingParams::ADVERTISEMENT_DURATION_UNITS_TO_MS(BLE_GAP_ADV_NONCON_INTERVAL_MIN); return GapAdvertisingParams::ADVERTISEMENT_DURATION_UNITS_TO_MS(BLE_GAP_ADV_NONCON_INTERVAL_MIN);
#endif #endif
} }
virtual uint16_t getMaxAdvertisingInterval(void) const {return GapAdvertisingParams::ADVERTISEMENT_DURATION_UNITS_TO_MS(BLE_GAP_ADV_INTERVAL_MAX);} uint16_t getMaxAdvertisingInterval_(void) const {
return GapAdvertisingParams::ADVERTISEMENT_DURATION_UNITS_TO_MS(BLE_GAP_ADV_INTERVAL_MAX);
}
virtual ble_error_t startAdvertising(const GapAdvertisingParams &); ble_error_t startAdvertising_(const GapAdvertisingParams &);
virtual ble_error_t stopAdvertising(void); ble_error_t stopAdvertising_(void);
virtual ble_error_t connect(const Address_t, ble::peer_address_type_t peerAddrType, const ConnectionParams_t *connectionParams, const GapScanningParams *scanParams); ble_error_t connect_(const Address_t, ble::peer_address_type_t peerAddrType, const ConnectionParams_t *connectionParams, const GapScanningParams *scanParams);
virtual ble_error_t connect(const Address_t, BLEProtocol::AddressType_t peerAddrType, const ConnectionParams_t *connectionParams, const GapScanningParams *scanParams); ble_error_t connect_(const Address_t, BLEProtocol::AddressType_t peerAddrType, const ConnectionParams_t *connectionParams, const GapScanningParams *scanParams);
ble_error_t connect(const Address_t, BLEProtocol::AddressType_t peerAddrType, const ConnectionParams_t *connectionParams, const GapScanningParams *scanParams, bool identity); ble_error_t connect(const Address_t, BLEProtocol::AddressType_t peerAddrType, const ConnectionParams_t *connectionParams, const GapScanningParams *scanParams, bool identity);
virtual ble_error_t readPhy(Handle_t connection); ble_error_t readPhy_(Handle_t connection);
virtual ble_error_t setPreferredPhys( ble_error_t setPreferredPhys_(
const ble::phy_set_t* txPhys, const ble::phy_set_t* txPhys,
const ble::phy_set_t* rxPhys const ble::phy_set_t* rxPhys
); );
virtual ble_error_t setPhy( ble_error_t setPhy_(
Handle_t connection, Handle_t connection,
const ble::phy_set_t* txPhys, const ble::phy_set_t* txPhys,
const ble::phy_set_t* rxPhys, const ble::phy_set_t* rxPhys,
CodedSymbolPerBit_t codedSymbol CodedSymbolPerBit_t codedSymbol
); );
virtual ble_error_t disconnect(Handle_t connectionHandle, DisconnectionReason_t reason); ble_error_t disconnect_(Handle_t connectionHandle, DisconnectionReason_t reason);
virtual ble_error_t disconnect(DisconnectionReason_t reason); ble_error_t disconnect_(DisconnectionReason_t reason);
virtual ble_error_t setDeviceName(const uint8_t *deviceName); ble_error_t setDeviceName_(const uint8_t *deviceName);
virtual ble_error_t getDeviceName(uint8_t *deviceName, unsigned *lengthP); ble_error_t getDeviceName_(uint8_t *deviceName, unsigned *lengthP);
virtual ble_error_t setAppearance(GapAdvertisingData::Appearance appearance); ble_error_t setAppearance_(GapAdvertisingData::Appearance appearance);
virtual ble_error_t getAppearance(GapAdvertisingData::Appearance *appearanceP); ble_error_t getAppearance_(GapAdvertisingData::Appearance *appearanceP);
virtual ble_error_t setTxPower(int8_t txPower); ble_error_t setTxPower_(int8_t txPower);
virtual void getPermittedTxPowerValues(const int8_t **valueArrayPP, size_t *countP); void getPermittedTxPowerValues_(const int8_t **valueArrayPP, size_t *countP);
void setConnectionHandle(uint16_t con_handle); void setConnectionHandle(uint16_t con_handle);
uint16_t getConnectionHandle(void); uint16_t getConnectionHandle(void);
virtual ble_error_t getPreferredConnectionParams(ConnectionParams_t *params); ble_error_t getPreferredConnectionParams_(ConnectionParams_t *params);
virtual ble_error_t setPreferredConnectionParams(const ConnectionParams_t *params); ble_error_t setPreferredConnectionParams_(const ConnectionParams_t *params);
virtual ble_error_t updateConnectionParams(Handle_t handle, const ConnectionParams_t *params); ble_error_t updateConnectionParams_(Handle_t handle, const ConnectionParams_t *params);
virtual ble_error_t reset(void); ble_error_t reset_(void);
/* /*
* The following functions are part of the whitelisting experimental API. * The following functions are part of the whitelisting experimental API.
* Therefore, this functionality can change in the near future. * Therefore, this functionality can change in the near future.
*/ */
virtual uint8_t getMaxWhitelistSize(void) const; uint8_t getMaxWhitelistSize_(void) const;
virtual ble_error_t getWhitelist(Gap::Whitelist_t &whitelistOut) const; ble_error_t getWhitelist_(Whitelist_t &whitelistOut) const;
virtual ble_error_t setWhitelist(const Gap::Whitelist_t &whitelistIn); ble_error_t setWhitelist_(const Whitelist_t &whitelistIn);
virtual ble_error_t setAdvertisingPolicyMode(AdvertisingPolicyMode_t mode); ble_error_t setAdvertisingPolicyMode_(AdvertisingPolicyMode_t mode);
virtual ble_error_t setScanningPolicyMode(ScanningPolicyMode_t mode); ble_error_t setScanningPolicyMode_(ScanningPolicyMode_t mode);
virtual ble_error_t setInitiatorPolicyMode(InitiatorPolicyMode_t mode); ble_error_t setInitiatorPolicyMode_(InitiatorPolicyMode_t mode);
virtual Gap::AdvertisingPolicyMode_t getAdvertisingPolicyMode(void) const; AdvertisingPolicyMode_t getAdvertisingPolicyMode_(void) const;
virtual Gap::ScanningPolicyMode_t getScanningPolicyMode(void) const; ScanningPolicyMode_t getScanningPolicyMode_(void) const;
virtual Gap::InitiatorPolicyMode_t getInitiatorPolicyMode(void) const; InitiatorPolicyMode_t getInitiatorPolicyMode_(void) const;
virtual ble_error_t initRadioNotification(void) { ble_error_t initRadioNotification_(void) {
if (ble_radio_notification_init(APP_IRQ_PRIORITY_HIGH /*MID*/, NRF_RADIO_NOTIFICATION_DISTANCE_800US, radioNotificationStaticCallback) == NRF_SUCCESS) { if (ble_radio_notification_init(APP_IRQ_PRIORITY_HIGH /*MID*/, NRF_RADIO_NOTIFICATION_DISTANCE_800US, radioNotificationStaticCallback) == NRF_SUCCESS) {
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
} }
@ -145,27 +154,27 @@ public:
return BLE_ERROR_UNSPECIFIED; return BLE_ERROR_UNSPECIFIED;
} }
virtual ble_error_t enablePrivacy(bool enable); ble_error_t enablePrivacy_(bool enable);
virtual ble_error_t setPeripheralPrivacyConfiguration( ble_error_t setPeripheralPrivacyConfiguration_(
const PeripheralPrivacyConfiguration_t *configuration const PeripheralPrivacyConfiguration_t *configuration
); );
virtual ble_error_t getPeripheralPrivacyConfiguration( ble_error_t getPeripheralPrivacyConfiguration_(
PeripheralPrivacyConfiguration_t *configuration PeripheralPrivacyConfiguration_t *configuration
); );
virtual ble_error_t setCentralPrivacyConfiguration( ble_error_t setCentralPrivacyConfiguration_(
const CentralPrivacyConfiguration_t *configuration const CentralPrivacyConfiguration_t *configuration
); );
virtual ble_error_t getCentralPrivacyConfiguration( ble_error_t getCentralPrivacyConfiguration_(
CentralPrivacyConfiguration_t *configuration CentralPrivacyConfiguration_t *configuration
); );
/* Observer role is not supported by S110, return BLE_ERROR_NOT_IMPLEMENTED */ /* Observer role is not supported by S110, return BLE_ERROR_NOT_IMPLEMENTED */
#if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110) #if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110)
virtual ble_error_t startRadioScan(const GapScanningParams &scanningParams); ble_error_t startRadioScan_(const GapScanningParams &scanningParams);
virtual ble_error_t stopScan(void); ble_error_t stopScan_(void);
#endif #endif
private: private:
@ -174,8 +183,8 @@ private:
*/ */
/* Policy modes set by the user. By default these are set to ignore the whitelist */ /* Policy modes set by the user. By default these are set to ignore the whitelist */
Gap::AdvertisingPolicyMode_t advertisingPolicyMode; AdvertisingPolicyMode_t advertisingPolicyMode;
Gap::ScanningPolicyMode_t scanningPolicyMode; ScanningPolicyMode_t scanningPolicyMode;
/* Internal representation of a whitelist */ /* Internal representation of a whitelist */
uint8_t whitelistAddressesSize; uint8_t whitelistAddressesSize;
@ -263,12 +272,6 @@ private:
} }
friend void radioNotificationStaticCallback(bool param); /* allow invocations of processRadioNotificationEvent() */ friend void radioNotificationStaticCallback(bool param); /* allow invocations of processRadioNotificationEvent() */
public: public:
/** @note Implements ConnectionEventMonitor.
* @copydoc ConnectionEventMonitor::set_connection_event_handler
*/
virtual void set_connection_event_handler(
ConnectionEventMonitor::EventHandler* connection_event_handler
);
/** /**
* @copydoc ::Gap::processDisconnectionEvent * @copydoc ::Gap::processDisconnectionEvent
@ -288,6 +291,15 @@ public:
*/ */
ble_error_t get_role(ble::connection_handle_t connection, Role_t& role); ble_error_t get_role(ble::connection_handle_t connection, Role_t& role);
protected:
// import from Gap
friend ble::interface::Gap<nRF5xGap>;
using ble::interface::Gap<nRF5xGap>::startAdvertising_;
using ble::interface::Gap<nRF5xGap>::stopAdvertising_;
using ble::interface::Gap<nRF5xGap>::connect_;
using ble::interface::Gap<nRF5xGap>::disconnect_;
private: private:
friend void btle_handler(const ble_evt_t *p_ble_evt); friend void btle_handler(const ble_evt_t *p_ble_evt);
friend void btle_handler(const ble_evt_t *p_ble_evt, void *p_context); friend void btle_handler(const ble_evt_t *p_ble_evt, void *p_context);
@ -307,7 +319,6 @@ private:
#endif #endif
uint16_t m_connectionHandle; uint16_t m_connectionHandle;
ConnectionEventMonitor::EventHandler* _connection_event_handler;
bool _privacy_enabled; bool _privacy_enabled;
PeripheralPrivacyConfiguration_t _peripheral_privacy_configuration; PeripheralPrivacyConfiguration_t _peripheral_privacy_configuration;

View File

@ -107,7 +107,7 @@ static ble_error_t set_attribute_value(
@endcode @endcode
*/ */
/**************************************************************************/ /**************************************************************************/
ble_error_t nRF5xGattServer::addService(GattService &service) ble_error_t nRF5xGattServer::addService_(GattService &service)
{ {
/* ToDo: Make sure this service UUID doesn't already exist (?) */ /* ToDo: Make sure this service UUID doesn't already exist (?) */
/* ToDo: Basic validation */ /* ToDo: Basic validation */
@ -258,12 +258,12 @@ ble_error_t nRF5xGattServer::addService(GattService &service)
Everything executed properly Everything executed properly
*/ */
/**************************************************************************/ /**************************************************************************/
ble_error_t nRF5xGattServer::read(GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP) ble_error_t nRF5xGattServer::read_(GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP)
{ {
return read(BLE_CONN_HANDLE_INVALID, attributeHandle, buffer, lengthP); return read(BLE_CONN_HANDLE_INVALID, attributeHandle, buffer, lengthP);
} }
ble_error_t nRF5xGattServer::read(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP) ble_error_t nRF5xGattServer::read_(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP)
{ {
ble_gatts_value_t value = { ble_gatts_value_t value = {
/* .len = */ *lengthP, /* .len = */ *lengthP,
@ -298,12 +298,12 @@ ble_error_t nRF5xGattServer::read(Gap::Handle_t connectionHandle, GattAttribute:
Everything executed properly Everything executed properly
*/ */
/**************************************************************************/ /**************************************************************************/
ble_error_t nRF5xGattServer::write(GattAttribute::Handle_t attributeHandle, const uint8_t buffer[], uint16_t len, bool localOnly) ble_error_t nRF5xGattServer::write_(GattAttribute::Handle_t attributeHandle, const uint8_t buffer[], uint16_t len, bool localOnly)
{ {
return write(BLE_CONN_HANDLE_INVALID, attributeHandle, buffer, len, localOnly); return write(BLE_CONN_HANDLE_INVALID, attributeHandle, buffer, len, localOnly);
} }
ble_error_t nRF5xGattServer::write(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, const uint8_t buffer[], uint16_t len, bool localOnly) ble_error_t nRF5xGattServer::write_(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, const uint8_t buffer[], uint16_t len, bool localOnly)
{ {
ble_error_t returnValue = BLE_ERROR_NONE; ble_error_t returnValue = BLE_ERROR_NONE;
@ -395,14 +395,14 @@ ble_error_t nRF5xGattServer::write(Gap::Handle_t connectionHandle, GattAttribute
return returnValue; return returnValue;
} }
ble_error_t nRF5xGattServer::areUpdatesEnabled(const GattCharacteristic &characteristic, bool *enabledP) ble_error_t nRF5xGattServer::areUpdatesEnabled_(const GattCharacteristic &characteristic, bool *enabledP)
{ {
/* Forward the call with the default connection handle. */ /* Forward the call with the default connection handle. */
nRF5xGap &gap = (nRF5xGap &) nRF5xn::Instance(BLE::DEFAULT_INSTANCE).getGap(); nRF5xGap &gap = (nRF5xGap &) nRF5xn::Instance(BLE::DEFAULT_INSTANCE).getGap();
return areUpdatesEnabled(gap.getConnectionHandle(), characteristic, enabledP); return areUpdatesEnabled_(gap.getConnectionHandle(), characteristic, enabledP);
} }
ble_error_t nRF5xGattServer::areUpdatesEnabled(Gap::Handle_t connectionHandle, const GattCharacteristic &characteristic, bool *enabledP) ble_error_t nRF5xGattServer::areUpdatesEnabled_(Gap::Handle_t connectionHandle, const GattCharacteristic &characteristic, bool *enabledP)
{ {
return areUpdatesEnabled(connectionHandle, characteristic.getValueHandle(), enabledP); return areUpdatesEnabled(connectionHandle, characteristic.getValueHandle(), enabledP);
} }
@ -444,10 +444,10 @@ ble_error_t nRF5xGattServer::areUpdatesEnabled(Gap::Handle_t connectionHandle, G
Everything executed properly Everything executed properly
*/ */
/**************************************************************************/ /**************************************************************************/
ble_error_t nRF5xGattServer::reset(void) ble_error_t nRF5xGattServer::reset_(void)
{ {
/* Clear all state that is from the parent, including private members */ /* Clear all state that is from the parent, including private members */
if (GattServer::reset() != BLE_ERROR_NONE) { if (ble::interface::GattServer<nRF5xGattServer>::reset_() != BLE_ERROR_NONE) {
return BLE_ERROR_INVALID_STATE; return BLE_ERROR_INVALID_STATE;
} }

View File

@ -24,18 +24,18 @@
#include "ble/Gap.h" #include "ble/Gap.h"
#include "ble/GattServer.h" #include "ble/GattServer.h"
class nRF5xGattServer : public GattServer class nRF5xGattServer : public ble::interface::GattServer<nRF5xGattServer>
{ {
public: public:
/* Functions that must be implemented from GattServer */ /* Functions that must be implemented from GattServer */
virtual ble_error_t addService(GattService &); ble_error_t addService_(GattService &);
virtual ble_error_t read(GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP); ble_error_t read_(GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP);
virtual ble_error_t read(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP); ble_error_t read_(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP);
virtual ble_error_t write(GattAttribute::Handle_t, const uint8_t[], uint16_t, bool localOnly = false); ble_error_t write_(GattAttribute::Handle_t, const uint8_t[], uint16_t, bool localOnly = false);
virtual ble_error_t write(Gap::Handle_t connectionHandle, GattAttribute::Handle_t, const uint8_t[], uint16_t, bool localOnly = false); ble_error_t write_(Gap::Handle_t connectionHandle, GattAttribute::Handle_t, const uint8_t[], uint16_t, bool localOnly = false);
virtual ble_error_t areUpdatesEnabled(const GattCharacteristic &characteristic, bool *enabledP); ble_error_t areUpdatesEnabled_(const GattCharacteristic &characteristic, bool *enabledP);
virtual ble_error_t areUpdatesEnabled(Gap::Handle_t connectionHandle, const GattCharacteristic &characteristic, bool *enabledP); ble_error_t areUpdatesEnabled_(Gap::Handle_t connectionHandle, const GattCharacteristic &characteristic, bool *enabledP);
virtual ble_error_t reset(void); ble_error_t reset_(void);
/* nRF51 Functions */ /* nRF51 Functions */
void eventCallback(void); void eventCallback(void);

View File

@ -34,39 +34,40 @@ namespace nordic {
/** /**
* Implementation of pal::GattClient for the Nordic stack. * Implementation of pal::GattClient for the Nordic stack.
*/ */
class nRF5xGattClient : public ble::pal::GattClient { template<class EventHandler>
class nRF5xGattClient : public ble::pal::GattClient<nRF5xGattClient<EventHandler>, EventHandler> {
public: public:
nRF5xGattClient(); nRF5xGattClient();
virtual ~nRF5xGattClient(); ~nRF5xGattClient();
/** /**
* see pal::GattClient::initialize . * see pal::GattClient::initialize .
*/ */
virtual ble_error_t initialize(); ble_error_t initialize_();
/** /**
* see pal::GattClient::terminate . * see pal::GattClient::terminate .
*/ */
virtual ble_error_t terminate(); ble_error_t terminate_();
/** /**
* see pal::GattClient::exchange_mtu . * see pal::GattClient::exchange_mtu .
*/ */
virtual ble_error_t exchange_mtu(connection_handle_t connection); ble_error_t exchange_mtu_(connection_handle_t connection);
/** /**
* see pal::GattClient::get_mtu_size . * see pal::GattClient::get_mtu_size .
*/ */
virtual ble_error_t get_mtu_size( ble_error_t get_mtu_size_(
connection_handle_t connection_handle, uint16_t& mtu_size connection_handle_t connection_handle, uint16_t& mtu_size
); );
/** /**
* see pal::GattClient::discover_primary_service . * see pal::GattClient::discover_primary_service .
*/ */
virtual ble_error_t discover_primary_service( ble_error_t discover_primary_service_(
connection_handle_t connection, connection_handle_t connection,
attribute_handle_t discovery_range_begining attribute_handle_t discovery_range_begining
); );
@ -74,7 +75,7 @@ public:
/** /**
* see pal::GattClient::discover_primary_service_by_service_uuid . * see pal::GattClient::discover_primary_service_by_service_uuid .
*/ */
virtual ble_error_t discover_primary_service_by_service_uuid( ble_error_t discover_primary_service_by_service_uuid_(
connection_handle_t connection_handle, connection_handle_t connection_handle,
attribute_handle_t discovery_range_beginning, attribute_handle_t discovery_range_beginning,
const UUID& uuid const UUID& uuid
@ -83,7 +84,7 @@ public:
/** /**
* see pal::GattClient::find_included_service . * see pal::GattClient::find_included_service .
*/ */
virtual ble_error_t find_included_service( ble_error_t find_included_service_(
connection_handle_t connection_handle, connection_handle_t connection_handle,
attribute_handle_range_t service_range attribute_handle_range_t service_range
); );
@ -91,7 +92,7 @@ public:
/** /**
* see pal::GattClient::discover_characteristics_of_a_service . * see pal::GattClient::discover_characteristics_of_a_service .
*/ */
virtual ble_error_t discover_characteristics_of_a_service( ble_error_t discover_characteristics_of_a_service_(
connection_handle_t connection_handle, connection_handle_t connection_handle,
attribute_handle_range_t discovery_range attribute_handle_range_t discovery_range
); );
@ -99,7 +100,7 @@ public:
/** /**
* see pal::GattClient::discover_characteristics_descriptors . * see pal::GattClient::discover_characteristics_descriptors .
*/ */
virtual ble_error_t discover_characteristics_descriptors( ble_error_t discover_characteristics_descriptors_(
connection_handle_t connection_handle, connection_handle_t connection_handle,
attribute_handle_range_t descriptors_discovery_range attribute_handle_range_t descriptors_discovery_range
); );
@ -107,7 +108,7 @@ public:
/** /**
* see pal::GattClient::read_attribute_value . * see pal::GattClient::read_attribute_value .
*/ */
virtual ble_error_t read_attribute_value( ble_error_t read_attribute_value_(
connection_handle_t connection_handle, connection_handle_t connection_handle,
attribute_handle_t attribute_handle attribute_handle_t attribute_handle
); );
@ -115,7 +116,7 @@ public:
/** /**
* see pal::GattClient::read_using_characteristic_uuid . * see pal::GattClient::read_using_characteristic_uuid .
*/ */
virtual ble_error_t read_using_characteristic_uuid( ble_error_t read_using_characteristic_uuid_(
connection_handle_t connection_handle, connection_handle_t connection_handle,
attribute_handle_range_t read_range, attribute_handle_range_t read_range,
const UUID& uuid const UUID& uuid
@ -124,7 +125,7 @@ public:
/** /**
* see pal::GattClient::read_attribute_blob . * see pal::GattClient::read_attribute_blob .
*/ */
virtual ble_error_t read_attribute_blob( ble_error_t read_attribute_blob_(
connection_handle_t connection, connection_handle_t connection,
attribute_handle_t attribute_handle, attribute_handle_t attribute_handle,
uint16_t offset uint16_t offset
@ -133,7 +134,7 @@ public:
/** /**
* see pal::GattClient::read_multiple_characteristic_values . * see pal::GattClient::read_multiple_characteristic_values .
*/ */
virtual ble_error_t read_multiple_characteristic_values( ble_error_t read_multiple_characteristic_values_(
connection_handle_t connection, connection_handle_t connection,
const ArrayView<const attribute_handle_t>& characteristic_handles const ArrayView<const attribute_handle_t>& characteristic_handles
); );
@ -141,7 +142,7 @@ public:
/** /**
* see pal::GattClient::write_without_response . * see pal::GattClient::write_without_response .
*/ */
virtual ble_error_t write_without_response( ble_error_t write_without_response_(
connection_handle_t connection_handle, connection_handle_t connection_handle,
attribute_handle_t characteristic_value_handle, attribute_handle_t characteristic_value_handle,
const ArrayView<const uint8_t>& value const ArrayView<const uint8_t>& value
@ -150,7 +151,7 @@ public:
/** /**
* see pal::GattClient::signed_write_without_response . * see pal::GattClient::signed_write_without_response .
*/ */
virtual ble_error_t signed_write_without_response( ble_error_t signed_write_without_response_(
connection_handle_t connection_handle, connection_handle_t connection_handle,
attribute_handle_t characteristic_value_handle, attribute_handle_t characteristic_value_handle,
const ArrayView<const uint8_t>& value const ArrayView<const uint8_t>& value
@ -159,7 +160,7 @@ public:
/** /**
* see pal::GattClient::write_attribute . * see pal::GattClient::write_attribute .
*/ */
virtual ble_error_t write_attribute( ble_error_t write_attribute_(
connection_handle_t connection_handle, connection_handle_t connection_handle,
attribute_handle_t attribute_handle, attribute_handle_t attribute_handle,
const ArrayView<const uint8_t>& value const ArrayView<const uint8_t>& value
@ -168,7 +169,7 @@ public:
/** /**
* see pal::GattClient::queue_prepare_write . * see pal::GattClient::queue_prepare_write .
*/ */
virtual ble_error_t queue_prepare_write( ble_error_t queue_prepare_write_(
connection_handle_t connection_handle, connection_handle_t connection_handle,
attribute_handle_t characteristic_value_handle, attribute_handle_t characteristic_value_handle,
const ArrayView<const uint8_t>& value, const ArrayView<const uint8_t>& value,
@ -178,7 +179,7 @@ public:
/** /**
* see pal::GattClient::execute_write_queue . * see pal::GattClient::execute_write_queue .
*/ */
virtual ble_error_t execute_write_queue( ble_error_t execute_write_queue_(
connection_handle_t connection_handle, connection_handle_t connection_handle,
bool execute bool execute
); );

View File

@ -81,7 +81,7 @@ static attribute_handle_range_t to_ble_handle_range(const ble_gattc_handle_range
/** /**
* Convert an error from the softdevice into a ble_error_t * Convert an error from the softdevice into a ble_error_t
*/ */
static ble_error_t convert_sd_error(uint32_t err) static ble_error_t client_convert_sd_error(uint32_t err)
{ {
switch (err) { switch (err) {
case NRF_SUCCESS: case NRF_SUCCESS:
@ -135,7 +135,7 @@ static ble_error_t to_nordic_uuid(const UUID &uuid, ble_uuid_t &nordic_uuid)
nordic_uuid.uuid = bytes_to_u16(uuid.getBaseUUID() + 12); nordic_uuid.uuid = bytes_to_u16(uuid.getBaseUUID() + 12);
} }
return convert_sd_error(err); return client_convert_sd_error(err);
} else { } else {
nordic_uuid.type = BLE_UUID_TYPE_BLE; nordic_uuid.type = BLE_UUID_TYPE_BLE;
nordic_uuid.uuid = uuid.getShortUUID(); nordic_uuid.uuid = uuid.getShortUUID();
@ -161,30 +161,34 @@ static const size_t characteristic_declaration_length = 1 + 2 + 16;
} // end of anonymous namespace } // end of anonymous namespace
nRF5xGattClient::nRF5xGattClient() : template<class EventHandler>
ble::pal::GattClient(), nRF5xGattClient<EventHandler>::nRF5xGattClient() :
_procedures() _procedures()
{ {
} }
nRF5xGattClient::~nRF5xGattClient() template<class EventHandler>
nRF5xGattClient<EventHandler>::~nRF5xGattClient()
{ {
terminate(); this->terminate();
} }
ble_error_t nRF5xGattClient::initialize() template<class EventHandler>
ble_error_t nRF5xGattClient<EventHandler>::initialize_()
{ {
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
} }
ble_error_t nRF5xGattClient::exchange_mtu(connection_handle_t connection) template<class EventHandler>
ble_error_t nRF5xGattClient<EventHandler>::exchange_mtu_(connection_handle_t connection)
{ {
// FIXME: implement when SD 140 5.x.x is present // FIXME: implement when SD 140 5.x.x is present
// (see sd_ble_gatts_exchange_mtu_reply) // (see sd_ble_gatts_exchange_mtu_reply)
return BLE_ERROR_NOT_IMPLEMENTED; return BLE_ERROR_NOT_IMPLEMENTED;
} }
ble_error_t nRF5xGattClient::get_mtu_size( template<class EventHandler>
ble_error_t nRF5xGattClient<EventHandler>::get_mtu_size_(
connection_handle_t connection_handle, uint16_t& mtu_size connection_handle_t connection_handle, uint16_t& mtu_size
) { ) {
#if (NRF_SD_BLE_API_VERSION >= 3) #if (NRF_SD_BLE_API_VERSION >= 3)
@ -197,7 +201,8 @@ ble_error_t nRF5xGattClient::get_mtu_size(
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
} }
ble_error_t nRF5xGattClient::discover_primary_service( template<class EventHandler>
ble_error_t nRF5xGattClient<EventHandler>::discover_primary_service_(
connection_handle_t connection, connection_handle_t connection,
attribute_handle_t discovery_range_begining attribute_handle_t discovery_range_begining
) { ) {
@ -206,7 +211,8 @@ ble_error_t nRF5xGattClient::discover_primary_service(
); );
} }
ble_error_t nRF5xGattClient::discover_primary_service_by_service_uuid( template<class EventHandler>
ble_error_t nRF5xGattClient<EventHandler>::discover_primary_service_by_service_uuid_(
connection_handle_t connection_handle, connection_handle_t connection_handle,
attribute_handle_t discovery_range_beginning, attribute_handle_t discovery_range_beginning,
const UUID& uuid const UUID& uuid
@ -216,7 +222,8 @@ ble_error_t nRF5xGattClient::discover_primary_service_by_service_uuid(
); );
} }
ble_error_t nRF5xGattClient::find_included_service( template<class EventHandler>
ble_error_t nRF5xGattClient<EventHandler>::find_included_service_(
connection_handle_t connection_handle, connection_handle_t connection_handle,
attribute_handle_range_t service_range attribute_handle_range_t service_range
) { ) {
@ -225,7 +232,8 @@ ble_error_t nRF5xGattClient::find_included_service(
); );
} }
ble_error_t nRF5xGattClient::discover_characteristics_of_a_service( template<class EventHandler>
ble_error_t nRF5xGattClient<EventHandler>::discover_characteristics_of_a_service_(
connection_handle_t connection_handle, connection_handle_t connection_handle,
attribute_handle_range_t discovery_range attribute_handle_range_t discovery_range
) { ) {
@ -234,7 +242,8 @@ ble_error_t nRF5xGattClient::discover_characteristics_of_a_service(
); );
} }
ble_error_t nRF5xGattClient::discover_characteristics_descriptors( template<class EventHandler>
ble_error_t nRF5xGattClient<EventHandler>::discover_characteristics_descriptors_(
connection_handle_t connection_handle, connection_handle_t connection_handle,
attribute_handle_range_t descriptors_discovery_range attribute_handle_range_t descriptors_discovery_range
) { ) {
@ -243,7 +252,8 @@ ble_error_t nRF5xGattClient::discover_characteristics_descriptors(
); );
} }
ble_error_t nRF5xGattClient::read_attribute_value( template<class EventHandler>
ble_error_t nRF5xGattClient<EventHandler>::read_attribute_value_(
connection_handle_t connection_handle, connection_handle_t connection_handle,
attribute_handle_t attribute_handle attribute_handle_t attribute_handle
) { ) {
@ -252,7 +262,8 @@ ble_error_t nRF5xGattClient::read_attribute_value(
); );
} }
ble_error_t nRF5xGattClient::read_using_characteristic_uuid( template<class EventHandler>
ble_error_t nRF5xGattClient<EventHandler>::read_using_characteristic_uuid_(
connection_handle_t connection_handle, connection_handle_t connection_handle,
attribute_handle_range_t read_range, attribute_handle_range_t read_range,
const UUID& uuid const UUID& uuid
@ -262,7 +273,8 @@ ble_error_t nRF5xGattClient::read_using_characteristic_uuid(
); );
} }
ble_error_t nRF5xGattClient::read_attribute_blob( template<class EventHandler>
ble_error_t nRF5xGattClient<EventHandler>::read_attribute_blob_(
connection_handle_t connection, connection_handle_t connection,
attribute_handle_t attribute_handle, attribute_handle_t attribute_handle,
uint16_t offset uint16_t offset
@ -272,7 +284,8 @@ ble_error_t nRF5xGattClient::read_attribute_blob(
); );
} }
ble_error_t nRF5xGattClient::read_multiple_characteristic_values( template<class EventHandler>
ble_error_t nRF5xGattClient<EventHandler>::read_multiple_characteristic_values_(
connection_handle_t connection, connection_handle_t connection,
const ArrayView<const attribute_handle_t>& characteristic_handles const ArrayView<const attribute_handle_t>& characteristic_handles
) { ) {
@ -281,7 +294,8 @@ ble_error_t nRF5xGattClient::read_multiple_characteristic_values(
); );
} }
ble_error_t nRF5xGattClient::write_without_response( template<class EventHandler>
ble_error_t nRF5xGattClient<EventHandler>::write_without_response_(
connection_handle_t connection_handle, connection_handle_t connection_handle,
attribute_handle_t characteristic_value_handle, attribute_handle_t characteristic_value_handle,
const ArrayView<const uint8_t>& value const ArrayView<const uint8_t>& value
@ -296,10 +310,11 @@ ble_error_t nRF5xGattClient::write_without_response(
}; };
uint32_t err = sd_ble_gattc_write(connection_handle, &write_params); uint32_t err = sd_ble_gattc_write(connection_handle, &write_params);
return convert_sd_error(err); return client_convert_sd_error(err);
} }
ble_error_t nRF5xGattClient::signed_write_without_response( template<class EventHandler>
ble_error_t nRF5xGattClient<EventHandler>::signed_write_without_response_(
connection_handle_t connection_handle, connection_handle_t connection_handle,
attribute_handle_t characteristic_value_handle, attribute_handle_t characteristic_value_handle,
const ArrayView<const uint8_t>& value const ArrayView<const uint8_t>& value
@ -314,10 +329,11 @@ ble_error_t nRF5xGattClient::signed_write_without_response(
}; };
uint32_t err = sd_ble_gattc_write(connection_handle, &write_params); uint32_t err = sd_ble_gattc_write(connection_handle, &write_params);
return convert_sd_error(err); return client_convert_sd_error(err);
} }
ble_error_t nRF5xGattClient::write_attribute( template<class EventHandler>
ble_error_t nRF5xGattClient<EventHandler>::write_attribute_(
connection_handle_t connection_handle, connection_handle_t connection_handle,
attribute_handle_t attribute_handle, attribute_handle_t attribute_handle,
const ArrayView<const uint8_t>& value const ArrayView<const uint8_t>& value
@ -327,7 +343,8 @@ ble_error_t nRF5xGattClient::write_attribute(
); );
} }
ble_error_t nRF5xGattClient::queue_prepare_write( template<class EventHandler>
ble_error_t nRF5xGattClient<EventHandler>::queue_prepare_write_(
connection_handle_t connection_handle, connection_handle_t connection_handle,
attribute_handle_t characteristic_value_handle, attribute_handle_t characteristic_value_handle,
const ArrayView<const uint8_t>& value, const ArrayView<const uint8_t>& value,
@ -338,7 +355,8 @@ ble_error_t nRF5xGattClient::queue_prepare_write(
); );
} }
ble_error_t nRF5xGattClient::execute_write_queue( template<class EventHandler>
ble_error_t nRF5xGattClient<EventHandler>::execute_write_queue_(
connection_handle_t connection_handle, connection_handle_t connection_handle,
bool execute bool execute
) { ) {
@ -369,7 +387,8 @@ ble_error_t nRF5xGattClient::execute_write_queue(
* @note Commands such as write without response or signed write without response * @note Commands such as write without response or signed write without response
* are not procedures. * are not procedures.
*/ */
struct nRF5xGattClient::GattProcedure { template<class EventHandler>
struct nRF5xGattClient<EventHandler>::GattProcedure {
/** /**
* Initialize the procedure. * Initialize the procedure.
* *
@ -426,7 +445,12 @@ struct nRF5xGattClient::GattProcedure {
* Given that such procedure expects a single event type from the soft device, * Given that such procedure expects a single event type from the soft device,
* error handling can be generalized. * error handling can be generalized.
*/ */
struct nRF5xGattClient::RegularGattProcedure : GattProcedure { template<class EventHandler>
struct nRF5xGattClient<EventHandler>::RegularGattProcedure : GattProcedure {
using GattProcedure::procedure_opcode;
using GattProcedure::connection_handle;
using GattProcedure::terminate;
/** /**
* Construct a RegularGattProcedure. * Construct a RegularGattProcedure.
@ -485,8 +509,12 @@ protected:
* In such case a read request is issued for each service attribute handle * In such case a read request is issued for each service attribute handle
* to extract that information. * to extract that information.
*/ */
struct nRF5xGattClient::DiscoverPrimaryServiceProcedure : GattProcedure { template<class EventHandler>
struct nRF5xGattClient<EventHandler>::DiscoverPrimaryServiceProcedure : GattProcedure {
using GattProcedure::procedure_opcode;
using GattProcedure::connection_handle;
using GattProcedure::terminate;
typedef ArrayView<const ble_gattc_service_t> services_array_t; typedef ArrayView<const ble_gattc_service_t> services_array_t;
DiscoverPrimaryServiceProcedure(connection_handle_t connection) : DiscoverPrimaryServiceProcedure(connection_handle_t connection) :
@ -505,7 +533,7 @@ struct nRF5xGattClient::DiscoverPrimaryServiceProcedure : GattProcedure {
uint32_t err = sd_ble_gattc_primary_services_discover( uint32_t err = sd_ble_gattc_primary_services_discover(
connection_handle, begining, /* p_srvc_uuid */ NULL connection_handle, begining, /* p_srvc_uuid */ NULL
); );
return convert_sd_error(err); return client_convert_sd_error(err);
} }
/** /**
@ -694,8 +722,12 @@ struct nRF5xGattClient::DiscoverPrimaryServiceProcedure : GattProcedure {
* response it is possible to reconstruct it by keeping a copy of the UUID to * response it is possible to reconstruct it by keeping a copy of the UUID to
* find. * find.
*/ */
struct nRF5xGattClient::DiscoverPrimaryServiceByUUIDProcedure : RegularGattProcedure { template<class EventHandler>
struct nRF5xGattClient<EventHandler>::DiscoverPrimaryServiceByUUIDProcedure : RegularGattProcedure {
using GattProcedure::procedure_opcode;
using GattProcedure::connection_handle;
using GattProcedure::terminate;
typedef ArrayView<const ble_gattc_service_t> services_array_t; typedef ArrayView<const ble_gattc_service_t> services_array_t;
DiscoverPrimaryServiceByUUIDProcedure(connection_handle_t connection) : DiscoverPrimaryServiceByUUIDProcedure(connection_handle_t connection) :
@ -721,7 +753,7 @@ struct nRF5xGattClient::DiscoverPrimaryServiceByUUIDProcedure : RegularGattProce
_service_uuid = uuid; _service_uuid = uuid;
} }
return convert_sd_error(err); return client_convert_sd_error(err);
} }
virtual void do_handle(const ble_gattc_evt_t &evt) virtual void do_handle(const ble_gattc_evt_t &evt)
@ -768,8 +800,12 @@ struct nRF5xGattClient::DiscoverPrimaryServiceByUUIDProcedure : RegularGattProce
/** /**
* Procedure that manage Find Included Services transactions. * Procedure that manage Find Included Services transactions.
*/ */
struct nRF5xGattClient::FindIncludedServicesProcedure : RegularGattProcedure { template<class EventHandler>
struct nRF5xGattClient<EventHandler>::FindIncludedServicesProcedure : RegularGattProcedure {
using GattProcedure::procedure_opcode;
using GattProcedure::connection_handle;
using GattProcedure::terminate;
typedef ArrayView<const ble_gattc_service_t> services_array_t; typedef ArrayView<const ble_gattc_service_t> services_array_t;
FindIncludedServicesProcedure(connection_handle_t connection) : FindIncludedServicesProcedure(connection_handle_t connection) :
@ -786,7 +822,7 @@ struct nRF5xGattClient::FindIncludedServicesProcedure : RegularGattProcedure {
connection_handle, &range connection_handle, &range
); );
return convert_sd_error(err); return client_convert_sd_error(err);
} }
virtual void do_handle(const ble_gattc_evt_t &evt) virtual void do_handle(const ble_gattc_evt_t &evt)
@ -843,7 +879,12 @@ struct nRF5xGattClient::FindIncludedServicesProcedure : RegularGattProcedure {
* In such case a read request is issued for each attribute handle of * In such case a read request is issued for each attribute handle of
* characteristics that exposes a long UUID. * characteristics that exposes a long UUID.
*/ */
struct nRF5xGattClient::DiscoverCharacteristicsProcedure : GattProcedure { template<class EventHandler>
struct nRF5xGattClient<EventHandler>::DiscoverCharacteristicsProcedure : GattProcedure {
using GattProcedure::procedure_opcode;
using GattProcedure::connection_handle;
using GattProcedure::terminate;
/** /**
* Data structure returned by the function flatten_response. * Data structure returned by the function flatten_response.
*/ */
@ -872,7 +913,7 @@ struct nRF5xGattClient::DiscoverCharacteristicsProcedure : GattProcedure {
&range &range
); );
return convert_sd_error(err); return client_convert_sd_error(err);
} }
/** /**
@ -1062,7 +1103,13 @@ struct nRF5xGattClient::DiscoverCharacteristicsProcedure : GattProcedure {
/** /**
* Procedure that handle discovery of characteristic descriptors. * Procedure that handle discovery of characteristic descriptors.
*/ */
struct nRF5xGattClient::DiscoverDescriptorsProcedure : RegularGattProcedure { template<class EventHandler>
struct nRF5xGattClient<EventHandler>::DiscoverDescriptorsProcedure : RegularGattProcedure {
using GattProcedure::procedure_opcode;
using GattProcedure::connection_handle;
using GattProcedure::terminate;
DiscoverDescriptorsProcedure(connection_handle_t connection) : DiscoverDescriptorsProcedure(connection_handle_t connection) :
RegularGattProcedure( RegularGattProcedure(
connection, connection,
@ -1079,7 +1126,7 @@ struct nRF5xGattClient::DiscoverDescriptorsProcedure : RegularGattProcedure {
&range &range
); );
return convert_sd_error(err); return client_convert_sd_error(err);
} }
virtual void do_handle(const ble_gattc_evt_t &evt) virtual void do_handle(const ble_gattc_evt_t &evt)
@ -1153,7 +1200,13 @@ struct nRF5xGattClient::DiscoverDescriptorsProcedure : RegularGattProcedure {
/** /**
* Procedure that handle read of attribute handles. * Procedure that handle read of attribute handles.
*/ */
struct nRF5xGattClient::ReadAttributeProcedure : RegularGattProcedure { template<class EventHandler>
struct nRF5xGattClient<EventHandler>::ReadAttributeProcedure : RegularGattProcedure {
using GattProcedure::procedure_opcode;
using GattProcedure::connection_handle;
using GattProcedure::terminate;
ReadAttributeProcedure(connection_handle_t connection) : ReadAttributeProcedure(connection_handle_t connection) :
RegularGattProcedure( RegularGattProcedure(
connection, AttributeOpcode::READ_REQUEST, BLE_GATTC_EVT_READ_RSP connection, AttributeOpcode::READ_REQUEST, BLE_GATTC_EVT_READ_RSP
@ -1162,7 +1215,7 @@ struct nRF5xGattClient::ReadAttributeProcedure : RegularGattProcedure {
ble_error_t start(attribute_handle_t attribute_handle) ble_error_t start(attribute_handle_t attribute_handle)
{ {
uint32_t err = sd_ble_gattc_read(connection_handle, attribute_handle, 0); uint32_t err = sd_ble_gattc_read(connection_handle, attribute_handle, 0);
return convert_sd_error(err); return client_convert_sd_error(err);
} }
virtual void do_handle(const ble_gattc_evt_t &evt) virtual void do_handle(const ble_gattc_evt_t &evt)
@ -1180,7 +1233,13 @@ struct nRF5xGattClient::ReadAttributeProcedure : RegularGattProcedure {
/** /**
* Procedure that handle read of characteristic using characteristic UUID. * Procedure that handle read of characteristic using characteristic UUID.
*/ */
struct nRF5xGattClient::ReadUsingCharacteristicUUIDProcedure : RegularGattProcedure { template<class EventHandler>
struct nRF5xGattClient<EventHandler>::ReadUsingCharacteristicUUIDProcedure : RegularGattProcedure {
using GattProcedure::procedure_opcode;
using GattProcedure::connection_handle;
using GattProcedure::terminate;
ReadUsingCharacteristicUUIDProcedure(connection_handle_t connection) : ReadUsingCharacteristicUUIDProcedure(connection_handle_t connection) :
RegularGattProcedure( RegularGattProcedure(
connection, connection,
@ -1206,7 +1265,7 @@ struct nRF5xGattClient::ReadUsingCharacteristicUUIDProcedure : RegularGattProced
&nordic_uuid, &nordic_uuid,
&range &range
); );
return convert_sd_error(err); return client_convert_sd_error(err);
} }
#if (NRF_SD_BLE_API_VERSION >= 3) #if (NRF_SD_BLE_API_VERSION >= 3)
@ -1267,7 +1326,13 @@ struct nRF5xGattClient::ReadUsingCharacteristicUUIDProcedure : RegularGattProced
/** /**
* Procedure that handles read blob transactions. * Procedure that handles read blob transactions.
*/ */
struct nRF5xGattClient::ReadAttributeBlobProcedure : RegularGattProcedure { template<class EventHandler>
struct nRF5xGattClient<EventHandler>::ReadAttributeBlobProcedure : RegularGattProcedure {
using GattProcedure::procedure_opcode;
using GattProcedure::connection_handle;
using GattProcedure::terminate;
ReadAttributeBlobProcedure(connection_handle_t connection) : ReadAttributeBlobProcedure(connection_handle_t connection) :
RegularGattProcedure( RegularGattProcedure(
connection, AttributeOpcode::READ_BLOB_REQUEST, BLE_GATTC_EVT_READ_RSP connection, AttributeOpcode::READ_BLOB_REQUEST, BLE_GATTC_EVT_READ_RSP
@ -1278,7 +1343,7 @@ struct nRF5xGattClient::ReadAttributeBlobProcedure : RegularGattProcedure {
uint32_t err = sd_ble_gattc_read( uint32_t err = sd_ble_gattc_read(
connection_handle, attribute_handle, offset connection_handle, attribute_handle, offset
); );
return convert_sd_error(err); return client_convert_sd_error(err);
} }
virtual void do_handle(const ble_gattc_evt_t &evt) virtual void do_handle(const ble_gattc_evt_t &evt)
@ -1293,7 +1358,13 @@ struct nRF5xGattClient::ReadAttributeBlobProcedure : RegularGattProcedure {
/** /**
* Procedure that handles Read Multiple Characteristic Values transactions. * Procedure that handles Read Multiple Characteristic Values transactions.
*/ */
struct nRF5xGattClient::ReadMultipleCharacteristicsProcedure : RegularGattProcedure { template<class EventHandler>
struct nRF5xGattClient<EventHandler>::ReadMultipleCharacteristicsProcedure : RegularGattProcedure {
using GattProcedure::procedure_opcode;
using GattProcedure::connection_handle;
using GattProcedure::terminate;
ReadMultipleCharacteristicsProcedure(connection_handle_t connection) : ReadMultipleCharacteristicsProcedure(connection_handle_t connection) :
RegularGattProcedure( RegularGattProcedure(
connection, connection,
@ -1308,7 +1379,7 @@ struct nRF5xGattClient::ReadMultipleCharacteristicsProcedure : RegularGattProced
characteristic_handles.data(), characteristic_handles.data(),
characteristic_handles.size() characteristic_handles.size()
); );
return convert_sd_error(err); return client_convert_sd_error(err);
} }
virtual void do_handle(const ble_gattc_evt_t &evt) virtual void do_handle(const ble_gattc_evt_t &evt)
@ -1323,7 +1394,13 @@ struct nRF5xGattClient::ReadMultipleCharacteristicsProcedure : RegularGattProced
/** /**
* Procedure that handles Write transactions. * Procedure that handles Write transactions.
*/ */
struct nRF5xGattClient::WriteAttributeProcedure : RegularGattProcedure { template<class EventHandler>
struct nRF5xGattClient<EventHandler>::WriteAttributeProcedure : RegularGattProcedure {
using GattProcedure::procedure_opcode;
using GattProcedure::connection_handle;
using GattProcedure::terminate;
WriteAttributeProcedure(connection_handle_t connection) : WriteAttributeProcedure(connection_handle_t connection) :
RegularGattProcedure( RegularGattProcedure(
connection, AttributeOpcode::WRITE_REQUEST, BLE_GATTC_EVT_WRITE_RSP connection, AttributeOpcode::WRITE_REQUEST, BLE_GATTC_EVT_WRITE_RSP
@ -1342,7 +1419,7 @@ struct nRF5xGattClient::WriteAttributeProcedure : RegularGattProcedure {
}; };
uint32_t err = sd_ble_gattc_write(connection_handle, &write_params); uint32_t err = sd_ble_gattc_write(connection_handle, &write_params);
return convert_sd_error(err); return client_convert_sd_error(err);
} }
virtual void do_handle(const ble_gattc_evt_t &evt) virtual void do_handle(const ble_gattc_evt_t &evt)
@ -1354,7 +1431,13 @@ struct nRF5xGattClient::WriteAttributeProcedure : RegularGattProcedure {
/** /**
* Procedure that handles Prepare Write transactions. * Procedure that handles Prepare Write transactions.
*/ */
struct nRF5xGattClient::QueuePrepareWriteProcedure : RegularGattProcedure { template<class EventHandler>
struct nRF5xGattClient<EventHandler>::QueuePrepareWriteProcedure : RegularGattProcedure {
using GattProcedure::procedure_opcode;
using GattProcedure::connection_handle;
using GattProcedure::terminate;
QueuePrepareWriteProcedure(connection_handle_t connection) : QueuePrepareWriteProcedure(connection_handle_t connection) :
RegularGattProcedure( RegularGattProcedure(
connection, connection,
@ -1377,7 +1460,7 @@ struct nRF5xGattClient::QueuePrepareWriteProcedure : RegularGattProcedure {
}; };
uint32_t err = sd_ble_gattc_write(connection_handle, &write_params); uint32_t err = sd_ble_gattc_write(connection_handle, &write_params);
return convert_sd_error(err); return client_convert_sd_error(err);
} }
virtual void do_handle(const ble_gattc_evt_t &evt) virtual void do_handle(const ble_gattc_evt_t &evt)
@ -1400,7 +1483,13 @@ struct nRF5xGattClient::QueuePrepareWriteProcedure : RegularGattProcedure {
/** /**
* Procedure that handles Execute Write transactions. * Procedure that handles Execute Write transactions.
*/ */
struct nRF5xGattClient::ExecuteWriteQueueProcedure : RegularGattProcedure { template<class EventHandler>
struct nRF5xGattClient<EventHandler>::ExecuteWriteQueueProcedure : RegularGattProcedure {
using GattProcedure::procedure_opcode;
using GattProcedure::connection_handle;
using GattProcedure::terminate;
ExecuteWriteQueueProcedure(connection_handle_t connection) : ExecuteWriteQueueProcedure(connection_handle_t connection) :
RegularGattProcedure( RegularGattProcedure(
connection, connection,
@ -1423,7 +1512,7 @@ struct nRF5xGattClient::ExecuteWriteQueueProcedure : RegularGattProcedure {
}; };
uint32_t err = sd_ble_gattc_write(connection_handle, &write_params); uint32_t err = sd_ble_gattc_write(connection_handle, &write_params);
return convert_sd_error(err); return client_convert_sd_error(err);
} }
virtual void do_handle(const ble_gattc_evt_t &evt) virtual void do_handle(const ble_gattc_evt_t &evt)
@ -1439,7 +1528,8 @@ struct nRF5xGattClient::ExecuteWriteQueueProcedure : RegularGattProcedure {
}; };
// NOTE: position after declaration of GattProcedure on purpose. // NOTE: position after declaration of GattProcedure on purpose.
ble_error_t nRF5xGattClient::terminate() template<class EventHandler>
ble_error_t nRF5xGattClient<EventHandler>::terminate_()
{ {
for (size_t i = 0; i < max_procedures_count; ++i) { for (size_t i = 0; i < max_procedures_count; ++i) {
if (_procedures[i]) { if (_procedures[i]) {
@ -1451,8 +1541,9 @@ ble_error_t nRF5xGattClient::terminate()
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
} }
template<class EventHandler>
template<typename ProcType, typename A0> template<typename ProcType, typename A0>
ble_error_t nRF5xGattClient::launch_procedure( ble_error_t nRF5xGattClient<EventHandler>::launch_procedure(
connection_handle_t connection, const A0& a0 connection_handle_t connection, const A0& a0
) { ) {
ProcType* p = new(std::nothrow) ProcType(connection); ProcType* p = new(std::nothrow) ProcType(connection);
@ -1474,8 +1565,9 @@ ble_error_t nRF5xGattClient::launch_procedure(
return err; return err;
} }
template<class EventHandler>
template<typename ProcType, typename A0, typename A1> template<typename ProcType, typename A0, typename A1>
ble_error_t nRF5xGattClient::launch_procedure( ble_error_t nRF5xGattClient<EventHandler>::launch_procedure(
connection_handle_t connection, const A0& a0, const A1& a1 connection_handle_t connection, const A0& a0, const A1& a1
) { ) {
ProcType* p = new(std::nothrow) ProcType(connection); ProcType* p = new(std::nothrow) ProcType(connection);
@ -1497,8 +1589,9 @@ ble_error_t nRF5xGattClient::launch_procedure(
return err; return err;
} }
template<class EventHandler>
template<typename ProcType, typename A0, typename A1, typename A2> template<typename ProcType, typename A0, typename A1, typename A2>
ble_error_t nRF5xGattClient::launch_procedure( ble_error_t nRF5xGattClient<EventHandler>::launch_procedure(
connection_handle_t connection, connection_handle_t connection,
const A0& a0, const A1& a1, const A2& a2 const A0& a0, const A1& a1, const A2& a2
) { ) {
@ -1521,8 +1614,9 @@ ble_error_t nRF5xGattClient::launch_procedure(
return err; return err;
} }
template<class EventHandler>
template<typename ProcType, typename A0, typename A1, typename A2, typename A3> template<typename ProcType, typename A0, typename A1, typename A2, typename A3>
ble_error_t nRF5xGattClient::launch_procedure( ble_error_t nRF5xGattClient<EventHandler>::launch_procedure(
connection_handle_t connection, connection_handle_t connection,
const A0& a0, const A1& a1, const A2& a2, const A3& a3 const A0& a0, const A1& a1, const A2& a2, const A3& a3
) { ) {
@ -1545,7 +1639,8 @@ ble_error_t nRF5xGattClient::launch_procedure(
return err; return err;
} }
nRF5xGattClient::GattProcedure* nRF5xGattClient::get_procedure( template<class EventHandler>
typename nRF5xGattClient<EventHandler>::GattProcedure* nRF5xGattClient<EventHandler>::get_procedure(
connection_handle_t connection connection_handle_t connection
) const { ) const {
for (size_t i = 0; i < max_procedures_count; ++i) { for (size_t i = 0; i < max_procedures_count; ++i) {
@ -1556,7 +1651,8 @@ nRF5xGattClient::GattProcedure* nRF5xGattClient::get_procedure(
return NULL; return NULL;
} }
bool nRF5xGattClient::register_procedure(GattProcedure *p) template<class EventHandler>
bool nRF5xGattClient<EventHandler>::register_procedure(GattProcedure *p)
{ {
if (get_procedure(p->connection_handle)) { if (get_procedure(p->connection_handle)) {
return false; return false;
@ -1572,7 +1668,8 @@ bool nRF5xGattClient::register_procedure(GattProcedure *p)
return false; return false;
} }
bool nRF5xGattClient::remove_procedure(nRF5xGattClient::GattProcedure* p) template<class EventHandler>
bool nRF5xGattClient<EventHandler>::remove_procedure(nRF5xGattClient<EventHandler>::GattProcedure* p)
{ {
for (size_t i = 0; i < max_procedures_count; ++i) { for (size_t i = 0; i < max_procedures_count; ++i) {
if (_procedures[i] == p) { if (_procedures[i] == p) {
@ -1585,13 +1682,15 @@ bool nRF5xGattClient::remove_procedure(nRF5xGattClient::GattProcedure* p)
} }
// singleton of the ARM Cordio client // singleton of the ARM Cordio client
nRF5xGattClient& nRF5xGattClient::get_client() template<class EventHandler>
nRF5xGattClient<EventHandler>& nRF5xGattClient<EventHandler>::get_client()
{ {
static nRF5xGattClient _client; static nRF5xGattClient _client;
return _client; return _client;
} }
void nRF5xGattClient::handle_events(const ble_evt_t *evt) { template<class EventHandler>
void nRF5xGattClient<EventHandler>::handle_events(const ble_evt_t *evt) {
switch (evt->header.evt_id) { switch (evt->header.evt_id) {
case BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP: case BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP:
case BLE_GATTC_EVT_REL_DISC_RSP: case BLE_GATTC_EVT_REL_DISC_RSP:
@ -1613,7 +1712,8 @@ void nRF5xGattClient::handle_events(const ble_evt_t *evt) {
} }
} }
void nRF5xGattClient::handle_procedure_event(const ble_evt_t &evt) template<class EventHandler>
void nRF5xGattClient<EventHandler>::handle_procedure_event(const ble_evt_t &evt)
{ {
GattProcedure* p = get_procedure(evt.evt.gattc_evt.conn_handle); GattProcedure* p = get_procedure(evt.evt.gattc_evt.conn_handle);
if (p) { if (p) {
@ -1621,14 +1721,15 @@ void nRF5xGattClient::handle_procedure_event(const ble_evt_t &evt)
} }
} }
void nRF5xGattClient::handle_hvx_event(const ble_evt_t &evt) template<class EventHandler>
void nRF5xGattClient<EventHandler>::handle_hvx_event(const ble_evt_t &evt)
{ {
connection_handle_t connection = evt.evt.gattc_evt.conn_handle; connection_handle_t connection = evt.evt.gattc_evt.conn_handle;
const ble_gattc_evt_hvx_t &hvx_evt = evt.evt.gattc_evt.params.hvx; const ble_gattc_evt_hvx_t &hvx_evt = evt.evt.gattc_evt.params.hvx;
switch (hvx_evt.type) { switch (hvx_evt.type) {
case BLE_GATT_HVX_NOTIFICATION: case BLE_GATT_HVX_NOTIFICATION:
on_server_event( this->on_server_event(
connection, connection,
AttHandleValueNotification( AttHandleValueNotification(
hvx_evt.handle, hvx_evt.handle,
@ -1639,7 +1740,7 @@ void nRF5xGattClient::handle_hvx_event(const ble_evt_t &evt)
case BLE_GATT_HVX_INDICATION: case BLE_GATT_HVX_INDICATION:
// send confirmation first then process the event // send confirmation first then process the event
sd_ble_gattc_hv_confirm(connection, hvx_evt.handle); sd_ble_gattc_hv_confirm(connection, hvx_evt.handle);
on_server_event( this->on_server_event(
connection, connection,
AttHandleValueIndication( AttHandleValueIndication(
hvx_evt.handle, hvx_evt.handle,
@ -1652,7 +1753,8 @@ void nRF5xGattClient::handle_hvx_event(const ble_evt_t &evt)
} }
} }
void nRF5xGattClient::handle_timeout_event(const ble_evt_t &evt) template<class EventHandler>
void nRF5xGattClient<EventHandler>::handle_timeout_event(const ble_evt_t &evt)
{ {
connection_handle_t connection = evt.evt.gattc_evt.conn_handle; connection_handle_t connection = evt.evt.gattc_evt.conn_handle;
GattProcedure* p = get_procedure(connection); GattProcedure* p = get_procedure(connection);
@ -1660,10 +1762,11 @@ void nRF5xGattClient::handle_timeout_event(const ble_evt_t &evt)
p->abort(); p->abort();
} }
on_transaction_timeout(connection); this->on_transaction_timeout(connection);
} }
void nRF5xGattClient::handle_connection_termination(connection_handle_t connection) template<class EventHandler>
void nRF5xGattClient<EventHandler>::handle_connection_termination(connection_handle_t connection)
{ {
GattProcedure* p = get_client().get_procedure(connection); GattProcedure* p = get_client().get_procedure(connection);
if (p) { if (p) {

View File

@ -28,11 +28,17 @@ namespace pal {
namespace vendor { namespace vendor {
namespace nordic { namespace nordic {
class nRF5xSecurityManager : public ::ble::pal::SecurityManager { template <class EventHandler>
class nRF5xSecurityManager : public ::ble::pal::SecurityManager<nRF5xSecurityManager<EventHandler>, EventHandler> {
public: public:
typedef ::ble::pal::SecurityManager<
nRF5xSecurityManager<EventHandler>,
EventHandler
> Base;
nRF5xSecurityManager(); nRF5xSecurityManager();
virtual ~nRF5xSecurityManager(); ~nRF5xSecurityManager();
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// SM lifecycle management // SM lifecycle management
@ -41,17 +47,17 @@ public:
/** /**
* @see ::ble::pal::SecurityManager::initialize * @see ::ble::pal::SecurityManager::initialize
*/ */
virtual ble_error_t initialize(); ble_error_t initialize_();
/** /**
* @see ::ble::pal::SecurityManager::terminate * @see ::ble::pal::SecurityManager::terminate
*/ */
virtual ble_error_t terminate(); ble_error_t terminate_();
/** /**
* @see ::ble::pal::SecurityManager::reset * @see ::ble::pal::SecurityManager::reset
*/ */
virtual ble_error_t reset() ; ble_error_t reset_() ;
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// Resolving list management // Resolving list management
@ -60,12 +66,12 @@ public:
/** /**
* @see ::ble::pal::SecurityManager::read_resolving_list_capacity * @see ::ble::pal::SecurityManager::read_resolving_list_capacity
*/ */
virtual uint8_t read_resolving_list_capacity(); uint8_t read_resolving_list_capacity_();
/** /**
* @see ::ble::pal::SecurityManager::add_device_to_resolving_list * @see ::ble::pal::SecurityManager::add_device_to_resolving_list
*/ */
virtual ble_error_t add_device_to_resolving_list( ble_error_t add_device_to_resolving_list_(
advertising_peer_address_type_t peer_identity_address_type, advertising_peer_address_type_t peer_identity_address_type,
const address_t &peer_identity_address, const address_t &peer_identity_address,
const irk_t &peer_irk const irk_t &peer_irk
@ -74,7 +80,7 @@ public:
/** /**
* @see ::ble::pal::SecurityManager::remove_device_from_resolving_list * @see ::ble::pal::SecurityManager::remove_device_from_resolving_list
*/ */
virtual ble_error_t remove_device_from_resolving_list( ble_error_t remove_device_from_resolving_list_(
advertising_peer_address_type_t peer_identity_address_type, advertising_peer_address_type_t peer_identity_address_type,
const address_t &peer_identity_address const address_t &peer_identity_address
); );
@ -82,7 +88,7 @@ public:
/** /**
* @see ::ble::pal::SecurityManager::clear_resolving_list * @see ::ble::pal::SecurityManager::clear_resolving_list
*/ */
virtual ble_error_t clear_resolving_list(); ble_error_t clear_resolving_list_();
/** /**
* Return the IRKs present in the resolving list * Return the IRKs present in the resolving list
@ -98,7 +104,7 @@ public:
/** /**
* @see ::ble::pal::SecurityManager::send_pairing_request * @see ::ble::pal::SecurityManager::send_pairing_request
*/ */
virtual ble_error_t send_pairing_request( ble_error_t send_pairing_request_(
connection_handle_t connection, connection_handle_t connection,
bool oob_data_flag, bool oob_data_flag,
AuthenticationMask authentication_requirements, AuthenticationMask authentication_requirements,
@ -109,7 +115,7 @@ public:
/** /**
* @see ::ble::pal::SecurityManager::send_pairing_response * @see ::ble::pal::SecurityManager::send_pairing_response
*/ */
virtual ble_error_t send_pairing_response( ble_error_t send_pairing_response_(
connection_handle_t connection, connection_handle_t connection,
bool oob_data_flag, bool oob_data_flag,
AuthenticationMask authentication_requirements, AuthenticationMask authentication_requirements,
@ -120,7 +126,7 @@ public:
/** /**
* @see ::ble::pal::SecurityManager::cancel_pairing * @see ::ble::pal::SecurityManager::cancel_pairing
*/ */
virtual ble_error_t cancel_pairing( ble_error_t cancel_pairing_(
connection_handle_t connection, pairing_failure_t reason connection_handle_t connection, pairing_failure_t reason
); );
@ -132,14 +138,14 @@ public:
/** /**
* @see ::ble::pal::SecurityManager::get_secure_connections_support * @see ::ble::pal::SecurityManager::get_secure_connections_support
*/ */
virtual ble_error_t get_secure_connections_support( ble_error_t get_secure_connections_support_(
bool &enabled bool &enabled
); );
/** /**
* @see ::ble::pal::SecurityManager::set_io_capability * @see ::ble::pal::SecurityManager::set_io_capability
*/ */
virtual ble_error_t set_io_capability(io_capability_t io_capability); ble_error_t set_io_capability_(io_capability_t io_capability);
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// Security settings // Security settings
@ -148,21 +154,21 @@ public:
/** /**
* @see ::ble::pal::SecurityManager::set_authentication_timeout * @see ::ble::pal::SecurityManager::set_authentication_timeout
*/ */
virtual ble_error_t set_authentication_timeout( ble_error_t set_authentication_timeout_(
connection_handle_t, uint16_t timeout_in_10ms connection_handle_t, uint16_t timeout_in_10ms
); );
/** /**
* @see ::ble::pal::SecurityManager::get_authentication_timeout * @see ::ble::pal::SecurityManager::get_authentication_timeout
*/ */
virtual ble_error_t get_authentication_timeout( ble_error_t get_authentication_timeout_(
connection_handle_t, uint16_t &timeout_in_10ms connection_handle_t, uint16_t &timeout_in_10ms
); );
/** /**
* @see ::ble::pal::SecurityManager::set_encryption_key_requirements * @see ::ble::pal::SecurityManager::set_encryption_key_requirements
*/ */
virtual ble_error_t set_encryption_key_requirements( ble_error_t set_encryption_key_requirements_(
uint8_t min_encryption_key_size, uint8_t min_encryption_key_size,
uint8_t max_encryption_key_size uint8_t max_encryption_key_size
); );
@ -170,7 +176,7 @@ public:
/** /**
* @see ::ble::pal::SecurityManager::slave_security_request * @see ::ble::pal::SecurityManager::slave_security_request
*/ */
virtual ble_error_t slave_security_request( ble_error_t slave_security_request_(
connection_handle_t connection, connection_handle_t connection,
AuthenticationMask authentication AuthenticationMask authentication
); );
@ -182,7 +188,7 @@ public:
/** /**
* @see ::ble::pal::SecurityManager::enable_encryption * @see ::ble::pal::SecurityManager::enable_encryption
*/ */
virtual ble_error_t enable_encryption( ble_error_t enable_encryption_(
connection_handle_t connection, connection_handle_t connection,
const ltk_t &ltk, const ltk_t &ltk,
const rand_t &rand, const rand_t &rand,
@ -193,7 +199,7 @@ public:
/** /**
* @see ::ble::pal::SecurityManager::enable_encryption * @see ::ble::pal::SecurityManager::enable_encryption
*/ */
virtual ble_error_t enable_encryption( ble_error_t enable_encryption_(
connection_handle_t connection, connection_handle_t connection,
const ltk_t &ltk, const ltk_t &ltk,
bool mitm bool mitm
@ -202,7 +208,7 @@ public:
/** /**
* @see ::ble::pal::SecurityManager::encrypt_data * @see ::ble::pal::SecurityManager::encrypt_data
*/ */
virtual ble_error_t encrypt_data( ble_error_t encrypt_data_(
const byte_array_t<16> &key, const byte_array_t<16> &key,
encryption_block_t &data encryption_block_t &data
); );
@ -214,7 +220,7 @@ public:
/** /**
* @see ::ble::pal::SecurityManager::set_private_address_timeout * @see ::ble::pal::SecurityManager::set_private_address_timeout
*/ */
virtual ble_error_t set_private_address_timeout(uint16_t timeout_in_seconds); ble_error_t set_private_address_timeout_(uint16_t timeout_in_seconds);
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// Keys // Keys
@ -223,7 +229,7 @@ public:
/** /**
* @see ::ble::pal::SecurityManager::set_ltk * @see ::ble::pal::SecurityManager::set_ltk
*/ */
virtual ble_error_t set_ltk( ble_error_t set_ltk_(
connection_handle_t connection, connection_handle_t connection,
const ltk_t &ltk, const ltk_t &ltk,
bool mitm, bool mitm,
@ -233,24 +239,24 @@ public:
/** /**
* @see ::ble::pal::SecurityManager::set_ltk_not_found * @see ::ble::pal::SecurityManager::set_ltk_not_found
*/ */
virtual ble_error_t set_ltk_not_found( ble_error_t set_ltk_not_found_(
connection_handle_t connection connection_handle_t connection
); );
/** /**
* @see ::ble::pal::SecurityManager::set_irk * @see ::ble::pal::SecurityManager::set_irk
*/ */
virtual ble_error_t set_irk(const irk_t &irk); ble_error_t set_irk_(const irk_t &irk);
/** /**
* @see ::ble::pal::SecurityManager::set_csrk * @see ::ble::pal::SecurityManager::set_csrk
*/ */
virtual ble_error_t set_csrk(const csrk_t &csrk, sign_count_t sign_counter); ble_error_t set_csrk_(const csrk_t &csrk, sign_count_t sign_counter);
/** /**
* @see ::ble::pal::SecurityManager::set_peer_csrk * @see ::ble::pal::SecurityManager::set_peer_csrk
*/ */
virtual ble_error_t set_peer_csrk( ble_error_t set_peer_csrk_(
connection_handle_t connection, connection_handle_t connection,
const csrk_t &csrk, const csrk_t &csrk,
bool authenticated, bool authenticated,
@ -260,7 +266,7 @@ public:
/** /**
* @see ::ble::pal::SecurityManager::remove_peer_csrk * @see ::ble::pal::SecurityManager::remove_peer_csrk
*/ */
virtual ble_error_t remove_peer_csrk(connection_handle_t connection); ble_error_t remove_peer_csrk_(connection_handle_t connection);
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// Authentication // Authentication
@ -269,7 +275,7 @@ public:
/** /**
* @see ::ble::pal::SecurityManager::get_random_data * @see ::ble::pal::SecurityManager::get_random_data
*/ */
virtual ble_error_t get_random_data(byte_array_t<8> &random_data); ble_error_t get_random_data_(byte_array_t<8> &random_data);
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// MITM // MITM
@ -278,12 +284,12 @@ public:
/** /**
* @see ::ble::pal::SecurityManager::set_display_passkey * @see ::ble::pal::SecurityManager::set_display_passkey
*/ */
virtual ble_error_t set_display_passkey(passkey_num_t passkey); ble_error_t set_display_passkey_(passkey_num_t passkey);
/** /**
* @see ::ble::pal::SecurityManager::passkey_request_reply * @see ::ble::pal::SecurityManager::passkey_request_reply
*/ */
virtual ble_error_t passkey_request_reply( ble_error_t passkey_request_reply_(
connection_handle_t connection, connection_handle_t connection,
passkey_num_t passkey passkey_num_t passkey
); );
@ -291,7 +297,7 @@ public:
/** /**
* @see ::ble::pal::SecurityManager::secure_connections_oob_request_reply * @see ::ble::pal::SecurityManager::secure_connections_oob_request_reply
*/ */
virtual ble_error_t secure_connections_oob_request_reply( ble_error_t secure_connections_oob_request_reply_(
connection_handle_t connection, connection_handle_t connection,
const oob_lesc_value_t &local_random, const oob_lesc_value_t &local_random,
const oob_lesc_value_t &peer_random, const oob_lesc_value_t &peer_random,
@ -301,7 +307,7 @@ public:
/** /**
* @see ::ble::pal::SecurityManager::legacy_pairing_oob_request_reply * @see ::ble::pal::SecurityManager::legacy_pairing_oob_request_reply
*/ */
virtual ble_error_t legacy_pairing_oob_request_reply( ble_error_t legacy_pairing_oob_request_reply_(
connection_handle_t connection, connection_handle_t connection,
const oob_tk_t &oob_data const oob_tk_t &oob_data
); );
@ -309,21 +315,21 @@ public:
/** /**
* @see ::ble::pal::SecurityManager::confirmation_entered * @see ::ble::pal::SecurityManager::confirmation_entered
*/ */
virtual ble_error_t confirmation_entered( ble_error_t confirmation_entered_(
connection_handle_t connection, bool confirmation connection_handle_t connection, bool confirmation
); );
/** /**
* @see ::ble::pal::SecurityManager::send_keypress_notification * @see ::ble::pal::SecurityManager::send_keypress_notification
*/ */
virtual ble_error_t send_keypress_notification( ble_error_t send_keypress_notification_(
connection_handle_t connection, Keypress_t keypress connection_handle_t connection, Keypress_t keypress
); );
/** /**
* @see ::ble::pal::SecurityManager::generate_secure_connections_oob * @see ::ble::pal::SecurityManager::generate_secure_connections_oob
*/ */
virtual ble_error_t generate_secure_connections_oob(); ble_error_t generate_secure_connections_oob_();
// singleton of nordic Security Manager // singleton of nordic Security Manager
static nRF5xSecurityManager& get_security_manager(); static nRF5xSecurityManager& get_security_manager();

View File

@ -61,7 +61,8 @@ enum pairing_role_t {
PAIRING_RESPONDER PAIRING_RESPONDER
}; };
struct nRF5xSecurityManager::pairing_control_block_t { template <class EventHandler>
struct nRF5xSecurityManager<EventHandler>::pairing_control_block_t {
pairing_control_block_t* next; pairing_control_block_t* next;
connection_handle_t connection; connection_handle_t connection;
pairing_role_t role; pairing_role_t role;
@ -83,8 +84,8 @@ struct nRF5xSecurityManager::pairing_control_block_t {
ble_gap_lesc_p256_pk_t peer_pk; ble_gap_lesc_p256_pk_t peer_pk;
}; };
nRF5xSecurityManager::nRF5xSecurityManager() template <class EventHandler>
: ::ble::pal::SecurityManager(), nRF5xSecurityManager<EventHandler>::nRF5xSecurityManager() :
_sign_counter(), _sign_counter(),
_io_capability(io_capability_t::NO_INPUT_NO_OUTPUT), _io_capability(io_capability_t::NO_INPUT_NO_OUTPUT),
_min_encryption_key_size(7), _min_encryption_key_size(7),
@ -95,7 +96,8 @@ nRF5xSecurityManager::nRF5xSecurityManager()
} }
nRF5xSecurityManager::~nRF5xSecurityManager() template <class EventHandler>
nRF5xSecurityManager<EventHandler>::~nRF5xSecurityManager()
{ {
terminate(); terminate();
} }
@ -104,7 +106,8 @@ nRF5xSecurityManager::~nRF5xSecurityManager()
// SM lifecycle management // SM lifecycle management
// //
ble_error_t nRF5xSecurityManager::initialize() template <class EventHandler>
ble_error_t nRF5xSecurityManager<EventHandler>::initialize_()
{ {
#if defined(MBEDTLS_ECDH_C) #if defined(MBEDTLS_ECDH_C)
// Note: we do not use the object on the stack as the CryptoToolbox is quite large // Note: we do not use the object on the stack as the CryptoToolbox is quite large
@ -122,20 +125,22 @@ ble_error_t nRF5xSecurityManager::initialize()
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
} }
ble_error_t nRF5xSecurityManager::terminate() template <class EventHandler>
ble_error_t nRF5xSecurityManager<EventHandler>::terminate_()
{ {
release_all_pairing_cb(); release_all_pairing_cb();
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
} }
ble_error_t nRF5xSecurityManager::reset() template <class EventHandler>
ble_error_t nRF5xSecurityManager<EventHandler>::reset_()
{ {
ble_error_t err = terminate(); ble_error_t err = this->terminate();
if (err) { if (err) {
return err; return err;
} }
return initialize(); return this->initialize();
} }
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
@ -145,12 +150,14 @@ ble_error_t nRF5xSecurityManager::reset()
// FIXME: on nordic, the irk is passed in sd_ble_gap_scan_start where whitelist // FIXME: on nordic, the irk is passed in sd_ble_gap_scan_start where whitelist
// and resolving list are all mixed up. // and resolving list are all mixed up.
uint8_t nRF5xSecurityManager::read_resolving_list_capacity() template <class EventHandler>
uint8_t nRF5xSecurityManager<EventHandler>::read_resolving_list_capacity_()
{ {
return MAX_RESOLVING_LIST_ENTRIES; return MAX_RESOLVING_LIST_ENTRIES;
} }
ble_error_t nRF5xSecurityManager::add_device_to_resolving_list( template <class EventHandler>
ble_error_t nRF5xSecurityManager<EventHandler>::add_device_to_resolving_list_(
advertising_peer_address_type_t peer_identity_address_type, advertising_peer_address_type_t peer_identity_address_type,
const address_t &peer_identity_address, const address_t &peer_identity_address,
const irk_t &peer_irk const irk_t &peer_irk
@ -177,7 +184,8 @@ ble_error_t nRF5xSecurityManager::add_device_to_resolving_list(
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
} }
ble_error_t nRF5xSecurityManager::remove_device_from_resolving_list( template <class EventHandler>
ble_error_t nRF5xSecurityManager<EventHandler>::remove_device_from_resolving_list_(
advertising_peer_address_type_t peer_identity_address_type, advertising_peer_address_type_t peer_identity_address_type,
const address_t &peer_identity_address const address_t &peer_identity_address
) { ) {
@ -207,13 +215,15 @@ ble_error_t nRF5xSecurityManager::remove_device_from_resolving_list(
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
} }
ble_error_t nRF5xSecurityManager::clear_resolving_list() template <class EventHandler>
ble_error_t nRF5xSecurityManager<EventHandler>::clear_resolving_list_()
{ {
resolving_list_entry_count = 0; resolving_list_entry_count = 0;
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
} }
ArrayView<ble_gap_id_key_t> nRF5xSecurityManager::get_resolving_list() { template <class EventHandler>
ArrayView<ble_gap_id_key_t> nRF5xSecurityManager<EventHandler>::get_resolving_list() {
return ArrayView<ble_gap_id_key_t>( return ArrayView<ble_gap_id_key_t>(
resolving_list, resolving_list,
resolving_list_entry_count resolving_list_entry_count
@ -224,8 +234,8 @@ ArrayView<ble_gap_id_key_t> nRF5xSecurityManager::get_resolving_list() {
// Pairing // Pairing
// //
template <class EventHandler>
ble_error_t nRF5xSecurityManager::send_pairing_request( ble_error_t nRF5xSecurityManager<EventHandler>::send_pairing_request_(
connection_handle_t connection, connection_handle_t connection,
bool oob_data_flag, bool oob_data_flag,
AuthenticationMask authentication_requirements, AuthenticationMask authentication_requirements,
@ -266,7 +276,8 @@ ble_error_t nRF5xSecurityManager::send_pairing_request(
return convert_sd_error(err); return convert_sd_error(err);
} }
ble_error_t nRF5xSecurityManager::send_pairing_response( template <class EventHandler>
ble_error_t nRF5xSecurityManager<EventHandler>::send_pairing_response_(
connection_handle_t connection, connection_handle_t connection,
bool oob_data_flag, bool oob_data_flag,
AuthenticationMask authentication_requirements, AuthenticationMask authentication_requirements,
@ -277,7 +288,7 @@ ble_error_t nRF5xSecurityManager::send_pairing_response(
if (!pairing_cb) { if (!pairing_cb) {
// not enough memory; try to reject the pairing request instead of // not enough memory; try to reject the pairing request instead of
// waiting for timeout. // waiting for timeout.
cancel_pairing(connection, pairing_failure_t::UNSPECIFIED_REASON); this->cancel_pairing(connection, pairing_failure_t::UNSPECIFIED_REASON);
return BLE_ERROR_NO_MEM; return BLE_ERROR_NO_MEM;
} }
pairing_cb->role = PAIRING_RESPONDER; pairing_cb->role = PAIRING_RESPONDER;
@ -317,7 +328,8 @@ ble_error_t nRF5xSecurityManager::send_pairing_response(
return convert_sd_error(err); return convert_sd_error(err);
} }
ble_error_t nRF5xSecurityManager::cancel_pairing( template <class EventHandler>
ble_error_t nRF5xSecurityManager<EventHandler>::cancel_pairing_(
connection_handle_t connection, pairing_failure_t reason connection_handle_t connection, pairing_failure_t reason
) { ) {
uint32_t err = 0; uint32_t err = 0;
@ -361,14 +373,16 @@ ble_error_t nRF5xSecurityManager::cancel_pairing(
// Feature support // Feature support
// //
ble_error_t nRF5xSecurityManager::get_secure_connections_support( template <class EventHandler>
ble_error_t nRF5xSecurityManager<EventHandler>::get_secure_connections_support_(
bool &enabled bool &enabled
) { ) {
enabled = true; enabled = true;
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
} }
ble_error_t nRF5xSecurityManager::set_io_capability(io_capability_t io_capability) template <class EventHandler>
ble_error_t nRF5xSecurityManager<EventHandler>::set_io_capability_(io_capability_t io_capability)
{ {
_io_capability = io_capability; _io_capability = io_capability;
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
@ -378,7 +392,8 @@ ble_error_t nRF5xSecurityManager::set_io_capability(io_capability_t io_capabilit
// Security settings // Security settings
// //
ble_error_t nRF5xSecurityManager::set_authentication_timeout( template <class EventHandler>
ble_error_t nRF5xSecurityManager<EventHandler>::set_authentication_timeout_(
connection_handle_t connection, uint16_t timeout_in_10ms connection_handle_t connection, uint16_t timeout_in_10ms
) { ) {
ble_opt_t opt; ble_opt_t opt;
@ -388,7 +403,8 @@ ble_error_t nRF5xSecurityManager::set_authentication_timeout(
return convert_sd_error(err); return convert_sd_error(err);
} }
ble_error_t nRF5xSecurityManager::get_authentication_timeout( template <class EventHandler>
ble_error_t nRF5xSecurityManager<EventHandler>::get_authentication_timeout_(
connection_handle_t connection, uint16_t &timeout_in_10ms connection_handle_t connection, uint16_t &timeout_in_10ms
) { ) {
ble_opt_t opt; ble_opt_t opt;
@ -403,7 +419,8 @@ ble_error_t nRF5xSecurityManager::get_authentication_timeout(
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
} }
ble_error_t nRF5xSecurityManager::set_encryption_key_requirements( template <class EventHandler>
ble_error_t nRF5xSecurityManager<EventHandler>::set_encryption_key_requirements_(
uint8_t min_encryption_key_size, uint8_t min_encryption_key_size,
uint8_t max_encryption_key_size uint8_t max_encryption_key_size
) { ) {
@ -418,7 +435,8 @@ ble_error_t nRF5xSecurityManager::set_encryption_key_requirements(
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
} }
ble_error_t nRF5xSecurityManager::slave_security_request( template <class EventHandler>
ble_error_t nRF5xSecurityManager<EventHandler>::slave_security_request_(
connection_handle_t connection, connection_handle_t connection,
AuthenticationMask authentication AuthenticationMask authentication
) { ) {
@ -444,7 +462,8 @@ ble_error_t nRF5xSecurityManager::slave_security_request(
// Encryption // Encryption
// //
ble_error_t nRF5xSecurityManager::enable_encryption( template <class EventHandler>
ble_error_t nRF5xSecurityManager<EventHandler>::enable_encryption_(
connection_handle_t connection, connection_handle_t connection,
const ltk_t &ltk, const ltk_t &ltk,
const rand_t &rand, const rand_t &rand,
@ -472,7 +491,8 @@ ble_error_t nRF5xSecurityManager::enable_encryption(
return convert_sd_error(err); return convert_sd_error(err);
} }
ble_error_t nRF5xSecurityManager::enable_encryption( template <class EventHandler>
ble_error_t nRF5xSecurityManager<EventHandler>::enable_encryption_(
connection_handle_t connection, connection_handle_t connection,
const ltk_t &ltk, const ltk_t &ltk,
bool mitm bool mitm
@ -494,7 +514,8 @@ ble_error_t nRF5xSecurityManager::enable_encryption(
return convert_sd_error(err); return convert_sd_error(err);
} }
ble_error_t nRF5xSecurityManager::encrypt_data( template <class EventHandler>
ble_error_t nRF5xSecurityManager<EventHandler>::encrypt_data_(
const byte_array_t<16> &key, const byte_array_t<16> &key,
encryption_block_t &data encryption_block_t &data
) { ) {
@ -515,7 +536,8 @@ ble_error_t nRF5xSecurityManager::encrypt_data(
// Privacy // Privacy
// //
ble_error_t nRF5xSecurityManager::set_private_address_timeout( template <class EventHandler>
ble_error_t nRF5xSecurityManager<EventHandler>::set_private_address_timeout_(
uint16_t timeout_in_seconds uint16_t timeout_in_seconds
) { ) {
ble_gap_privacy_params_t privacy_config; ble_gap_privacy_params_t privacy_config;
@ -535,7 +557,8 @@ ble_error_t nRF5xSecurityManager::set_private_address_timeout(
// Keys // Keys
// //
ble_error_t nRF5xSecurityManager::set_ltk( template <class EventHandler>
ble_error_t nRF5xSecurityManager<EventHandler>::set_ltk_(
connection_handle_t connection, connection_handle_t connection,
const ltk_t& ltk, const ltk_t& ltk,
bool mitm, bool mitm,
@ -558,7 +581,8 @@ ble_error_t nRF5xSecurityManager::set_ltk(
return convert_sd_error(err); return convert_sd_error(err);
} }
ble_error_t nRF5xSecurityManager::set_ltk_not_found( template <class EventHandler>
ble_error_t nRF5xSecurityManager<EventHandler>::set_ltk_not_found_(
connection_handle_t connection connection_handle_t connection
) { ) {
uint32_t err = sd_ble_gap_sec_info_reply( uint32_t err = sd_ble_gap_sec_info_reply(
@ -571,7 +595,8 @@ ble_error_t nRF5xSecurityManager::set_ltk_not_found(
return convert_sd_error(err); return convert_sd_error(err);
} }
ble_error_t nRF5xSecurityManager::set_irk(const irk_t& irk) template <class EventHandler>
ble_error_t nRF5xSecurityManager<EventHandler>::set_irk_(const irk_t& irk)
{ {
ble_gap_privacy_params_t privacy_config; ble_gap_privacy_params_t privacy_config;
@ -588,7 +613,8 @@ ble_error_t nRF5xSecurityManager::set_irk(const irk_t& irk)
return convert_sd_error(err); return convert_sd_error(err);
} }
ble_error_t nRF5xSecurityManager::set_csrk( template <class EventHandler>
ble_error_t nRF5xSecurityManager<EventHandler>::set_csrk_(
const csrk_t& csrk, const csrk_t& csrk,
sign_count_t sign_counter sign_count_t sign_counter
) { ) {
@ -597,7 +623,8 @@ ble_error_t nRF5xSecurityManager::set_csrk(
return BLE_ERROR_NONE; return BLE_ERROR_NONE;
} }
ble_error_t nRF5xSecurityManager::set_peer_csrk( template <class EventHandler>
ble_error_t nRF5xSecurityManager<EventHandler>::set_peer_csrk_(
connection_handle_t connection, connection_handle_t connection,
const csrk_t &csrk, const csrk_t &csrk,
bool authenticated, bool authenticated,
@ -606,7 +633,8 @@ ble_error_t nRF5xSecurityManager::set_peer_csrk(
return BLE_ERROR_NOT_IMPLEMENTED; return BLE_ERROR_NOT_IMPLEMENTED;
} }
ble_error_t nRF5xSecurityManager::remove_peer_csrk(connection_handle_t connection) template <class EventHandler>
ble_error_t nRF5xSecurityManager<EventHandler>::remove_peer_csrk_(connection_handle_t connection)
{ {
return BLE_ERROR_NOT_IMPLEMENTED; return BLE_ERROR_NOT_IMPLEMENTED;
} }
@ -614,7 +642,8 @@ ble_error_t nRF5xSecurityManager::remove_peer_csrk(connection_handle_t connectio
// Authentication // Authentication
// //
ble_error_t nRF5xSecurityManager::get_random_data(byte_array_t<8> &random_data) template <class EventHandler>
ble_error_t nRF5xSecurityManager<EventHandler>::get_random_data_(byte_array_t<8> &random_data)
{ {
uint32_t err = sd_rand_application_vector_get( uint32_t err = sd_rand_application_vector_get(
random_data.data(), random_data.size() random_data.data(), random_data.size()
@ -626,7 +655,8 @@ ble_error_t nRF5xSecurityManager::get_random_data(byte_array_t<8> &random_data)
// MITM // MITM
// //
ble_error_t nRF5xSecurityManager::set_display_passkey(passkey_num_t passkey) template <class EventHandler>
ble_error_t nRF5xSecurityManager<EventHandler>::set_display_passkey_(passkey_num_t passkey)
{ {
PasskeyAscii passkey_ascii(passkey); PasskeyAscii passkey_ascii(passkey);
ble_opt_t sd_passkey; ble_opt_t sd_passkey;
@ -635,8 +665,8 @@ ble_error_t nRF5xSecurityManager::set_display_passkey(passkey_num_t passkey)
return convert_sd_error(err); return convert_sd_error(err);
} }
template <class EventHandler>
ble_error_t nRF5xSecurityManager::passkey_request_reply( ble_error_t nRF5xSecurityManager<EventHandler>::passkey_request_reply_(
connection_handle_t connection, const passkey_num_t passkey connection_handle_t connection, const passkey_num_t passkey
) { ) {
pairing_control_block_t* pairing_cb = get_pairing_cb(connection); pairing_control_block_t* pairing_cb = get_pairing_cb(connection);
@ -654,7 +684,8 @@ ble_error_t nRF5xSecurityManager::passkey_request_reply(
return convert_sd_error(err); return convert_sd_error(err);
} }
ble_error_t nRF5xSecurityManager::secure_connections_oob_request_reply( template <class EventHandler>
ble_error_t nRF5xSecurityManager<EventHandler>::secure_connections_oob_request_reply_(
connection_handle_t connection, connection_handle_t connection,
const oob_lesc_value_t &local_random, const oob_lesc_value_t &local_random,
const oob_lesc_value_t &peer_random, const oob_lesc_value_t &peer_random,
@ -696,7 +727,8 @@ ble_error_t nRF5xSecurityManager::secure_connections_oob_request_reply(
return convert_sd_error(err); return convert_sd_error(err);
} }
ble_error_t nRF5xSecurityManager::legacy_pairing_oob_request_reply( template <class EventHandler>
ble_error_t nRF5xSecurityManager<EventHandler>::legacy_pairing_oob_request_reply_(
connection_handle_t connection, connection_handle_t connection,
const oob_tk_t& oob_data const oob_tk_t& oob_data
) { ) {
@ -709,7 +741,8 @@ ble_error_t nRF5xSecurityManager::legacy_pairing_oob_request_reply(
return convert_sd_error(err); return convert_sd_error(err);
} }
ble_error_t nRF5xSecurityManager::confirmation_entered( template <class EventHandler>
ble_error_t nRF5xSecurityManager<EventHandler>::confirmation_entered_(
connection_handle_t connection, bool confirmation connection_handle_t connection, bool confirmation
) { ) {
pairing_control_block_t* pairing_cb = get_pairing_cb(connection); pairing_control_block_t* pairing_cb = get_pairing_cb(connection);
@ -726,7 +759,8 @@ ble_error_t nRF5xSecurityManager::confirmation_entered(
return convert_sd_error(err); return convert_sd_error(err);
} }
ble_error_t nRF5xSecurityManager::send_keypress_notification( template <class EventHandler>
ble_error_t nRF5xSecurityManager<EventHandler>::send_keypress_notification_(
connection_handle_t connection, Keypress_t keypress connection_handle_t connection, Keypress_t keypress
) { ) {
uint32_t err = sd_ble_gap_keypress_notify( uint32_t err = sd_ble_gap_keypress_notify(
@ -736,8 +770,8 @@ ble_error_t nRF5xSecurityManager::send_keypress_notification(
return convert_sd_error(err); return convert_sd_error(err);
} }
template <class EventHandler>
ble_error_t nRF5xSecurityManager::generate_secure_connections_oob() ble_error_t nRF5xSecurityManager<EventHandler>::generate_secure_connections_oob_()
{ {
#if defined(MBEDTLS_ECDH_C) #if defined(MBEDTLS_ECDH_C)
ble_gap_lesc_p256_pk_t own_secret; ble_gap_lesc_p256_pk_t own_secret;
@ -754,7 +788,7 @@ ble_error_t nRF5xSecurityManager::generate_secure_connections_oob()
); );
if (!err) { if (!err) {
get_event_handler()->on_secure_connections_oob_generated( this->get_event_handler()->on_secure_connections_oob_generated(
oob_data.r, oob_data.r,
oob_data.c oob_data.c
); );
@ -765,7 +799,8 @@ ble_error_t nRF5xSecurityManager::generate_secure_connections_oob()
return BLE_ERROR_NOT_IMPLEMENTED; return BLE_ERROR_NOT_IMPLEMENTED;
} }
nRF5xSecurityManager& nRF5xSecurityManager::get_security_manager() template <class EventHandler>
nRF5xSecurityManager<EventHandler>& nRF5xSecurityManager<EventHandler>::get_security_manager()
{ {
static nRF5xSecurityManager _security_manager; static nRF5xSecurityManager _security_manager;
return _security_manager; return _security_manager;
@ -789,10 +824,11 @@ bool is_ediv_rand_valid(const uint16_t ediv, const uint8_t* rand)
return false; return false;
} }
bool nRF5xSecurityManager::sm_handler(const ble_evt_t *evt) template <class EventHandler>
bool nRF5xSecurityManager<EventHandler>::sm_handler(const ble_evt_t *evt)
{ {
nRF5xSecurityManager& self = nRF5xSecurityManager::get_security_manager(); nRF5xSecurityManager& self = nRF5xSecurityManager<EventHandler>::get_security_manager();
SecurityManager::EventHandler* handler = self.get_event_handler(); EventHandler* handler = self.get_event_handler();
if ((evt == NULL) || (handler == NULL)) { if ((evt == NULL) || (handler == NULL)) {
return false; return false;
@ -1145,7 +1181,8 @@ bool nRF5xSecurityManager::sm_handler(const ble_evt_t *evt)
} }
} }
ble_gap_sec_params_t nRF5xSecurityManager::make_security_params( template <class EventHandler>
ble_gap_sec_params_t nRF5xSecurityManager<EventHandler>::make_security_params(
bool oob_data_flag, bool oob_data_flag,
AuthenticationMask authentication_requirements, AuthenticationMask authentication_requirements,
KeyDistribution initiator_dist, KeyDistribution initiator_dist,
@ -1176,7 +1213,8 @@ ble_gap_sec_params_t nRF5xSecurityManager::make_security_params(
return security_params; return security_params;
} }
ble_gap_sec_keyset_t nRF5xSecurityManager::make_keyset( template <class EventHandler>
ble_gap_sec_keyset_t nRF5xSecurityManager<EventHandler>::make_keyset(
pairing_control_block_t& pairing_cb, pairing_control_block_t& pairing_cb,
KeyDistribution initiator_dist, KeyDistribution initiator_dist,
KeyDistribution responder_dist KeyDistribution responder_dist
@ -1212,8 +1250,9 @@ ble_gap_sec_keyset_t nRF5xSecurityManager::make_keyset(
return keyset; return keyset;
} }
nRF5xSecurityManager::pairing_control_block_t* template <class EventHandler>
nRF5xSecurityManager::allocate_pairing_cb(connection_handle_t connection) typename nRF5xSecurityManager<EventHandler>::pairing_control_block_t*
nRF5xSecurityManager<EventHandler>::allocate_pairing_cb(connection_handle_t connection)
{ {
pairing_control_block_t* pairing_cb = pairing_control_block_t* pairing_cb =
new (std::nothrow) pairing_control_block_t(); new (std::nothrow) pairing_control_block_t();
@ -1224,7 +1263,8 @@ nRF5xSecurityManager::allocate_pairing_cb(connection_handle_t connection)
return pairing_cb; return pairing_cb;
} }
void nRF5xSecurityManager::release_pairing_cb(pairing_control_block_t* pairing_cb) template <class EventHandler>
void nRF5xSecurityManager<EventHandler>::release_pairing_cb(pairing_control_block_t* pairing_cb)
{ {
if (pairing_cb == _control_blocks) { if (pairing_cb == _control_blocks) {
_control_blocks = _control_blocks->next; _control_blocks = _control_blocks->next;
@ -1242,8 +1282,9 @@ void nRF5xSecurityManager::release_pairing_cb(pairing_control_block_t* pairing_c
} }
} }
nRF5xSecurityManager::pairing_control_block_t* template <class EventHandler>
nRF5xSecurityManager::get_pairing_cb(connection_handle_t connection) typename nRF5xSecurityManager<EventHandler>::pairing_control_block_t*
nRF5xSecurityManager<EventHandler>::get_pairing_cb(connection_handle_t connection)
{ {
pairing_control_block_t* pcb = _control_blocks; pairing_control_block_t* pcb = _control_blocks;
while (pcb) { while (pcb) {
@ -1256,7 +1297,8 @@ nRF5xSecurityManager::get_pairing_cb(connection_handle_t connection)
return NULL; return NULL;
} }
void nRF5xSecurityManager::release_all_pairing_cb() template <class EventHandler>
void nRF5xSecurityManager<EventHandler>::release_all_pairing_cb()
{ {
while(_control_blocks) { while(_control_blocks) {
release_pairing_cb(_control_blocks); release_pairing_cb(_control_blocks);

View File

@ -32,6 +32,7 @@ extern "C" {
} }
#include "nRF5xPalGattClient.h" #include "nRF5xPalGattClient.h"
#include "DummySigningEventMonitor.h"
/** /**
* The singleton which represents the nRF51822 transport for the BLE. * The singleton which represents the nRF51822 transport for the BLE.
@ -62,7 +63,7 @@ nRF5xn::nRF5xn(void) :
instanceID(BLE::DEFAULT_INSTANCE), instanceID(BLE::DEFAULT_INSTANCE),
gapInstance(), gapInstance(),
gattServerInstance(NULL), gattServerInstance(NULL),
gattClient(&(ble::pal::vendor::nordic::nRF5xGattClient::get_client())) gattClient(&(ble::impl::PalGattClientImpl::get_client()))
{ {
} }
@ -214,13 +215,14 @@ SecurityManager& nRF5xn::getSecurityManager()
const SecurityManager& nRF5xn::getSecurityManager() const const SecurityManager& nRF5xn::getSecurityManager() const
{ {
ble::pal::vendor::nordic::nRF5xSecurityManager &m_pal = ble::impl::PalSecurityManagerImpl &m_pal =
ble::pal::vendor::nordic::nRF5xSecurityManager::get_security_manager(); ble::impl::PalSecurityManagerImpl::get_security_manager();
static struct : ble::pal::SigningEventMonitor {
virtual void set_signing_event_handler(EventHandler *signing_event_handler) { }
} dummy_signing_event_monitor;
static ble::generic::GenericSecurityManager m_instance( static ble::vendor::nordic::DummySigningEventMonitor<
ble::impl::GenericSecurityManagerImpl
> dummy_signing_event_monitor;
static ble::impl::GenericSecurityManagerImpl m_instance(
m_pal, m_pal,
const_cast<nRF5xGap&>(getGap()), const_cast<nRF5xGap&>(getGap()),
dummy_signing_event_monitor dummy_signing_event_monitor

View File

@ -148,8 +148,7 @@ private:
* If NULL, then GattServer has not been initialized. * If NULL, then GattServer has not been initialized.
* The pointer has been declared as 'mutable' so that * The pointer has been declared as 'mutable' so that
* it can be assigned inside a 'const' function. */ * it can be assigned inside a 'const' function. */
ble::generic::GenericGattClient gattClient; ble::impl::GenericGattClientImpl gattClient;
}; };