Release 0.4.0

=============

This is a major release which introduces the GATT Client functionality. It
aligns with release 0.4.0 of BLE_API.

Enhancements
~~~~~~~~~~~~

* Introduce GattClient. This includes functionality for service-discovery,
  connections, and attribute-reads and writes. You'll find a demo program for
  LEDBlinker on the mbed.org Bluetooth team page to use the new APIs. Some of
  the GATT client functionality hasn't been implemented yet, but the APIs have
  been added.

* We've added an implementation for the abstract base class for
  SecurityManager. All security related APIs have been moved into that.

* There has been a major cleanup of APIs under BLE. APIs have now been
  categorized as belonging to Gap, GattServer, GattClient, or SecurityManager.
  There are accessors to get references for Gap, GattServer, GattClient, and
  SecurityManager. A former call to ble.setAddress(...) is now expected to be
  achieved with ble.gap().setAddress(...).

* We've cleaned up our APIs, and this has resulted in dropping some APIs like
  BLE::reset().

* We've also dropped GattServer::initializeGattDatabase(). THis was added at
  some point to support controllers where a commit point was needed to
  indicate when the application had finished constructing the GATT database.
  This API would get called internally before Gap::startAdvertising(). We now
  expect the underlying port to do the equivalent of initializeGattDatabase()
  implicitly upon Gap::startAdvertising().

* We've added a version of Gap::disconnect() which takes a connection handle.
  The previous API (which did not take a connection handle) has been
  deprecated; it will still work for situations where there's only a single
  active connection. We hold on to that API to allow existing code to migrate
  to the new API.

Bugfixes
~~~~~~~~

* None.
master
Rohit Grover 2015-06-19 15:48:04 +01:00
commit 2716309ce1
111 changed files with 29478 additions and 27109 deletions

View File

@ -30,12 +30,15 @@
#include "softdevice_handler.h"
#include "pstorage.h"
#include "GapEvents.h"
#include "ble/GapEvents.h"
#include "nRF51Gap.h"
#include "nRF51GattServer.h"
#include "nRF51SecurityManager.h"
#include "device_manager.h"
#include "ble_hci.h"
#include "btle_discovery.h"
extern "C" void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name);
void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name);
@ -104,6 +107,8 @@ static void btle_handler(ble_evt_t *p_ble_evt)
dm_ble_evt_handler(p_ble_evt);
bleGattcEventHandler(p_ble_evt);
/* Custom event handler */
switch (p_ble_evt->header.evt_id) {
case BLE_GAP_EVT_CONNECTED: {
@ -113,6 +118,7 @@ static void btle_handler(ble_evt_t *p_ble_evt)
const ble_gap_addr_t *peer = &p_ble_evt->evt.gap_evt.params.connected.peer_addr;
const ble_gap_addr_t *own = &p_ble_evt->evt.gap_evt.params.connected.own_addr;
nRF51Gap::getInstance().processConnectionEvent(handle,
static_cast<Gap::Role_t>(p_ble_evt->evt.gap_evt.params.connected.role),
static_cast<Gap::AddressType_t>(peer->addr_type), peer->addr,
static_cast<Gap::AddressType_t>(own->addr_type), own->addr,
params);
@ -147,13 +153,11 @@ static void btle_handler(ble_evt_t *p_ble_evt)
}
case BLE_GAP_EVT_PASSKEY_DISPLAY:
nRF51Gap::getInstance().processPasskeyDisplayEvent(p_ble_evt->evt.gap_evt.conn_handle, p_ble_evt->evt.gap_evt.params.passkey_display.passkey);
nRF51SecurityManager::getInstance().processPasskeyDisplayEvent(p_ble_evt->evt.gap_evt.conn_handle, p_ble_evt->evt.gap_evt.params.passkey_display.passkey);
break;
case BLE_GAP_EVT_TIMEOUT:
if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISING) {
nRF51Gap::getInstance().processEvent(GapEvents::GAP_EVENT_TIMEOUT);
}
nRF51Gap::getInstance().processTimeoutEvent(static_cast<Gap::TimeoutSource_t>(p_ble_evt->evt.gap_evt.params.timeout.src));
break;
case BLE_GATTC_EVT_TIMEOUT:
@ -168,7 +172,7 @@ static void btle_handler(ble_evt_t *p_ble_evt)
nRF51Gap::getInstance().processAdvertisementReport(advReport->peer_addr.addr,
advReport->rssi,
advReport->scan_rsp,
static_cast<Gap::AdvertisementType_t>(advReport->type),
static_cast<GapAdvertisingParams::AdvertisingType_t>(advReport->type),
advReport->dlen,
advReport->data);
break;

86
btle/btle_discovery.cpp Normal file
View File

@ -0,0 +1,86 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 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 "nRF51ServiceDiscovery.h"
#include "nRF51GattClient.h"
void bleGattcEventHandler(const ble_evt_t *p_ble_evt)
{
nRF51ServiceDiscovery &sdSingleton = nRF51GattClient::getInstance().discovery;
switch (p_ble_evt->header.evt_id) {
case BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP:
switch (p_ble_evt->evt.gattc_evt.gatt_status) {
case BLE_GATT_STATUS_SUCCESS:
sdSingleton.setupDiscoveredServices(&p_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp);
break;
case BLE_GATT_STATUS_ATTERR_ATTRIBUTE_NOT_FOUND:
default:
sdSingleton.terminate();
break;
}
break;
case BLE_GATTC_EVT_CHAR_DISC_RSP:
switch (p_ble_evt->evt.gattc_evt.gatt_status) {
case BLE_GATT_STATUS_SUCCESS:
sdSingleton.setupDiscoveredCharacteristics(&p_ble_evt->evt.gattc_evt.params.char_disc_rsp);
break;
case BLE_GATT_STATUS_ATTERR_ATTRIBUTE_NOT_FOUND:
default:
sdSingleton.terminateCharacteristicDiscovery();
break;
}
break;
case BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP:
if (sdSingleton.isActive()) {
sdSingleton.processDiscoverUUIDResponse(&p_ble_evt->evt.gattc_evt.params.char_val_by_uuid_read_rsp);
}
break;
case BLE_GATTC_EVT_READ_RSP:
if (DiscoveredCharacteristic::onDataReadCallback != NULL) {
GattReadCallbackParams response = {
.handle = p_ble_evt->evt.gattc_evt.params.read_rsp.handle,
.offset = p_ble_evt->evt.gattc_evt.params.read_rsp.offset,
.len = p_ble_evt->evt.gattc_evt.params.read_rsp.len,
.data = p_ble_evt->evt.gattc_evt.params.read_rsp.data,
};
DiscoveredCharacteristic::onDataReadCallback(&response);
}
break;
case BLE_GATTC_EVT_WRITE_RSP:
if (DiscoveredCharacteristic::onDataWriteCallback != NULL) {
GattWriteCallbackParams response = {
.handle = p_ble_evt->evt.gattc_evt.params.write_rsp.handle,
.writeOp = (GattWriteCallbackParams::WriteOp_t)(p_ble_evt->evt.gattc_evt.params.write_rsp.write_op),
.offset = p_ble_evt->evt.gattc_evt.params.write_rsp.offset,
.len = p_ble_evt->evt.gattc_evt.params.write_rsp.len,
.data = p_ble_evt->evt.gattc_evt.params.write_rsp.data,
};
DiscoveredCharacteristic::onDataWriteCallback(&response);
}
break;
}
sdSingleton.progressCharacteristicDiscovery();
sdSingleton.progressServiceDiscovery();
}

22
btle/btle_discovery.h Normal file
View File

@ -0,0 +1,22 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 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 _BTLE_DISCOVERY_H_
#define _BTLE_DISCOVERY_H_
void bleGattcEventHandler(const ble_evt_t *p_ble_evt);
#endif /*_BTLE_DISCOVERY_H_*/

View File

@ -16,7 +16,10 @@
#include "btle.h"
#include "pstorage.h"
#include "nRF51Gap.h"
#include "nRF51SecurityManager.h"
#include "device_manager.h"
#include "btle_security.h"
@ -24,7 +27,10 @@ static dm_application_instance_t applicationInstance;
static ret_code_t dm_handler(dm_handle_t const *p_handle, dm_event_t const *p_event, ret_code_t event_result);
ble_error_t
btle_initializeSecurity(bool enableBonding, bool requireMITM, Gap::SecurityIOCapabilities_t iocaps, const Gap::Passkey_t passkey)
btle_initializeSecurity(bool enableBonding,
bool requireMITM,
SecurityManager::SecurityIOCapabilities_t iocaps,
const SecurityManager::Passkey_t passkey)
{
/* guard against multiple initializations */
static bool initialized = false;
@ -114,7 +120,7 @@ btle_purgeAllBondingState(void)
}
ble_error_t
btle_getLinkSecurity(Gap::Handle_t connectionHandle, Gap::LinkSecurityStatus_t *securityStatusP)
btle_getLinkSecurity(Gap::Handle_t connectionHandle, SecurityManager::LinkSecurityStatus_t *securityStatusP)
{
ret_code_t rc;
dm_handle_t dmHandle = {
@ -148,51 +154,52 @@ dm_handler(dm_handle_t const *p_handle, dm_event_t const *p_event, ret_code_t ev
switch (p_event->event_id) {
case DM_EVT_SECURITY_SETUP: /* started */ {
const ble_gap_sec_params_t *peerParams = &p_event->event_param.p_gap_param->params.sec_params_request.peer_params;
nRF51Gap::getInstance().processSecuritySetupInitiatedEvent(p_event->event_param.p_gap_param->conn_handle,
peerParams->bond,
peerParams->mitm,
(Gap::SecurityIOCapabilities_t)peerParams->io_caps);
nRF51SecurityManager::getInstance().processSecuritySetupInitiatedEvent(p_event->event_param.p_gap_param->conn_handle,
peerParams->bond,
peerParams->mitm,
(SecurityManager::SecurityIOCapabilities_t)peerParams->io_caps);
break;
}
case DM_EVT_SECURITY_SETUP_COMPLETE:
nRF51Gap::getInstance().processSecuritySetupCompletedEvent(p_event->event_param.p_gap_param->conn_handle,
(Gap::SecurityCompletionStatus_t)(p_event->event_param.p_gap_param->params.auth_status.auth_status));
nRF51SecurityManager::getInstance().
processSecuritySetupCompletedEvent(p_event->event_param.p_gap_param->conn_handle,
(SecurityManager::SecurityCompletionStatus_t)(p_event->event_param.p_gap_param->params.auth_status.auth_status));
break;
case DM_EVT_LINK_SECURED: {
unsigned securityMode = p_event->event_param.p_gap_param->params.conn_sec_update.conn_sec.sec_mode.sm;
unsigned level = p_event->event_param.p_gap_param->params.conn_sec_update.conn_sec.sec_mode.lv;
Gap::SecurityMode_t resolvedSecurityMode = Gap::SECURITY_MODE_NO_ACCESS;
SecurityManager::SecurityMode_t resolvedSecurityMode = SecurityManager::SECURITY_MODE_NO_ACCESS;
switch (securityMode) {
case 1:
switch (level) {
case 1:
resolvedSecurityMode = Gap::SECURITY_MODE_ENCRYPTION_OPEN_LINK;
resolvedSecurityMode = SecurityManager::SECURITY_MODE_ENCRYPTION_OPEN_LINK;
break;
case 2:
resolvedSecurityMode = Gap::SECURITY_MODE_ENCRYPTION_NO_MITM;
resolvedSecurityMode = SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM;
break;
case 3:
resolvedSecurityMode = Gap::SECURITY_MODE_ENCRYPTION_WITH_MITM;
resolvedSecurityMode = SecurityManager::SECURITY_MODE_ENCRYPTION_WITH_MITM;
break;
}
break;
case 2:
switch (level) {
case 1:
resolvedSecurityMode = Gap::SECURITY_MODE_SIGNED_NO_MITM;
resolvedSecurityMode = SecurityManager::SECURITY_MODE_SIGNED_NO_MITM;
break;
case 2:
resolvedSecurityMode = Gap::SECURITY_MODE_SIGNED_WITH_MITM;
resolvedSecurityMode = SecurityManager::SECURITY_MODE_SIGNED_WITH_MITM;
break;
}
break;
}
nRF51Gap::getInstance().processLinkSecuredEvent(p_event->event_param.p_gap_param->conn_handle, resolvedSecurityMode);
nRF51SecurityManager::getInstance().processLinkSecuredEvent(p_event->event_param.p_gap_param->conn_handle, resolvedSecurityMode);
break;
}
case DM_EVT_DEVICE_CONTEXT_STORED:
nRF51Gap::getInstance().processSecurityContextStoredEvent(p_event->event_param.p_gap_param->conn_handle);
nRF51SecurityManager::getInstance().processSecurityContextStoredEvent(p_event->event_param.p_gap_param->conn_handle);
break;
default:
break;

View File

@ -17,7 +17,8 @@
#ifndef _BTLE_SECURITY_H_
#define _BTLE_SECURITY_H_
#include "Gap.h"
#include "ble/Gap.h"
#include "ble/SecurityManager.h"
/**
* Enable Nordic's Device Manager, which brings in functionality from the
@ -34,10 +35,10 @@
*
* @return BLE_ERROR_NONE on success.
*/
ble_error_t btle_initializeSecurity(bool enableBonding = true,
bool requireMITM = true,
Gap::SecurityIOCapabilities_t iocaps = Gap::IO_CAPS_NONE,
const Gap::Passkey_t passkey = NULL);
ble_error_t btle_initializeSecurity(bool enableBonding = true,
bool requireMITM = true,
SecurityManager::SecurityIOCapabilities_t iocaps = SecurityManager::IO_CAPS_NONE,
const SecurityManager::Passkey_t passkey = NULL);
/**
* Get the security status of a link.
@ -49,7 +50,7 @@ ble_error_t btle_initializeSecurity(bool enableBonding
*
* @return BLE_SUCCESS Or appropriate error code indicating reason for failure.
*/
ble_error_t btle_getLinkSecurity(Gap::Handle_t connectionHandle, Gap::LinkSecurityStatus_t *securityStatusP);
ble_error_t btle_getLinkSecurity(Gap::Handle_t connectionHandle, SecurityManager::LinkSecurityStatus_t *securityStatusP);
/**
* Function for deleting all peer device context and all related bonding

View File

@ -200,7 +200,7 @@ error_t custom_decode_uuid_base(uint8_t const *const p_uuid_base,
error_t custom_add_in_characteristic(uint16_t service_handle,
ble_uuid_t *p_uuid,
uint8_t properties,
Gap::SecurityMode_t requiredSecurity,
SecurityManager::SecurityMode_t requiredSecurity,
uint8_t *p_data,
uint16_t min_length,
uint16_t max_length,
@ -246,19 +246,19 @@ error_t custom_add_in_characteristic(uint16_t service_handle,
if (char_props.read || char_props.notify || char_props.indicate) {
switch (requiredSecurity) {
case Gap::SECURITY_MODE_ENCRYPTION_OPEN_LINK :
case SecurityManager::SECURITY_MODE_ENCRYPTION_OPEN_LINK :
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
break;
case Gap::SECURITY_MODE_ENCRYPTION_NO_MITM :
case SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM :
BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&attr_md.read_perm);
break;
case Gap::SECURITY_MODE_ENCRYPTION_WITH_MITM :
case SecurityManager::SECURITY_MODE_ENCRYPTION_WITH_MITM :
BLE_GAP_CONN_SEC_MODE_SET_ENC_WITH_MITM(&attr_md.read_perm);
break;
case Gap::SECURITY_MODE_SIGNED_NO_MITM :
case SecurityManager::SECURITY_MODE_SIGNED_NO_MITM :
BLE_GAP_CONN_SEC_MODE_SET_SIGNED_NO_MITM(&attr_md.read_perm);
break;
case Gap::SECURITY_MODE_SIGNED_WITH_MITM :
case SecurityManager::SECURITY_MODE_SIGNED_WITH_MITM :
BLE_GAP_CONN_SEC_MODE_SET_SIGNED_WITH_MITM(&attr_md.read_perm);
break;
default:
@ -268,19 +268,19 @@ error_t custom_add_in_characteristic(uint16_t service_handle,
if (char_props.write || char_props.write_wo_resp) {
switch (requiredSecurity) {
case Gap::SECURITY_MODE_ENCRYPTION_OPEN_LINK :
case SecurityManager::SECURITY_MODE_ENCRYPTION_OPEN_LINK :
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);
break;
case Gap::SECURITY_MODE_ENCRYPTION_NO_MITM :
case SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM :
BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&attr_md.write_perm);
break;
case Gap::SECURITY_MODE_ENCRYPTION_WITH_MITM :
case SecurityManager::SECURITY_MODE_ENCRYPTION_WITH_MITM :
BLE_GAP_CONN_SEC_MODE_SET_ENC_WITH_MITM(&attr_md.write_perm);
break;
case Gap::SECURITY_MODE_SIGNED_NO_MITM :
case SecurityManager::SECURITY_MODE_SIGNED_NO_MITM :
BLE_GAP_CONN_SEC_MODE_SET_SIGNED_NO_MITM(&attr_md.write_perm);
break;
case Gap::SECURITY_MODE_SIGNED_WITH_MITM :
case SecurityManager::SECURITY_MODE_SIGNED_WITH_MITM :
BLE_GAP_CONN_SEC_MODE_SET_SIGNED_WITH_MITM(&attr_md.write_perm);
break;
default:

View File

@ -19,8 +19,8 @@
#include "common/common.h"
#include "ble.h"
#include "UUID.h"
#include "GattCharacteristic.h"
#include "ble/UUID.h"
#include "ble/GattCharacteristic.h"
#ifdef __cplusplus
extern "C" {
@ -34,7 +34,7 @@ ble_uuid_t custom_convert_to_nordic_uuid(const UUID &uuid);
error_t custom_add_in_characteristic(uint16_t service_handle,
ble_uuid_t *p_uuid,
uint8_t properties,
Gap::SecurityMode_t requiredSecurity,
SecurityManager::SecurityMode_t requiredSecurity,
uint8_t *p_data,
uint16_t min_length,
uint16_t max_length,

View File

@ -80,11 +80,9 @@ static inline void debugger_breakpoint(void)
// Assert Helper
//--------------------------------------------------------------------+
//#ifndef _TEST_
// #define ASSERT_MESSAGE(format, ...)\
// _PRINTF("Assert at %s: %s: %d: " format "\n", __BASE_FILE__, __PRETTY_FUNCTION__, __LINE__, __VA_ARGS__)
// #define ASSERT_MESSAGE(format, ...) _PRINTF("Assert at %s: %s: %d: " format "\n", __BASE_FILE__, __PRETTY_FUNCTION__, __LINE__, __VA_ARGS__)
//#else
// #define ASSERT_MESSAGE(format, ...)\
// _PRINTF("%d:note: Assert " format "\n", __LINE__, __VA_ARGS__)
// #define ASSERT_MESSAGE(format, ...) _PRINTF("%d:note: Assert " format "\n", __LINE__, __VA_ARGS__)
//#endif
#if CFG_DEBUG == 3

View File

@ -24,7 +24,7 @@
#include "softdevice_handler.h"
/**
* The singleton which represents the nRF51822 transport for the BLEDevice.
* The singleton which represents the nRF51822 transport for the BLE.
*/
static nRF51822n deviceInstance;
@ -32,8 +32,8 @@ static nRF51822n deviceInstance;
* BLE-API requires an implementation of the following function in order to
* obtain its transport handle.
*/
BLEDeviceInstanceBase *
createBLEDeviceInstance(void)
BLEInstanceBase *
createBLEInstance(void)
{
return (&deviceInstance);
}
@ -71,40 +71,11 @@ const char *nRF51822n::getVersion(void)
return versionString;
}
/* (Valid values are -40, -20, -16, -12, -8, -4, 0, 4) */
ble_error_t nRF51822n::setTxPower(int8_t txPower)
{
unsigned rc;
if ((rc = sd_ble_gap_tx_power_set(txPower)) != NRF_SUCCESS) {
switch (rc) {
case NRF_ERROR_BUSY:
return BLE_STACK_BUSY;
case NRF_ERROR_INVALID_PARAM:
default:
return BLE_ERROR_PARAM_OUT_OF_RANGE;
}
}
return BLE_ERROR_NONE;
}
void nRF51822n::getPermittedTxPowerValues(const int8_t **valueArrayPP, size_t *countP)
{
static const int8_t permittedTxValues[] = {
-40, -30, -20, -16, -12, -8, -4, 0, 4
};
*valueArrayPP = permittedTxValues;
*countP = sizeof(permittedTxValues) / sizeof(int8_t);
}
ble_error_t nRF51822n::init(void)
{
/* ToDo: Clear memory contents, reset the SD, etc. */
btle_init();
reset();
return BLE_ERROR_NONE;
}
@ -113,16 +84,6 @@ ble_error_t nRF51822n::shutdown(void)
return (softdevice_handler_sd_disable() == NRF_SUCCESS) ? BLE_ERROR_NONE : BLE_STACK_BUSY;
}
ble_error_t nRF51822n::reset(void)
{
nrf_delay_us(500000);
/* Wait for the radio to come back up */
nrf_delay_us(1000000);
return BLE_ERROR_NONE;
}
void
nRF51822n::waitForEvent(void)
{

View File

@ -18,39 +18,44 @@
#define __NRF51822_H__
#include "mbed.h"
#include "blecommon.h"
#include "BLEDevice.h"
#include "ble/blecommon.h"
#include "ble/BLE.h"
#include "nRF51Gap.h"
#include "nRF51GattServer.h"
#include "nRF51GattClient.h"
#include "nRF51SecurityManager.h"
#include "btle.h"
#include "btle_security.h"
class nRF51822n : public BLEDeviceInstanceBase
class nRF51822n : public BLEInstanceBase
{
public:
nRF51822n(void);
virtual ~nRF51822n(void);
virtual ble_error_t init(void);
virtual ble_error_t shutdown(void);
virtual const char *getVersion(void);
virtual Gap &getGap() {
virtual Gap &getGap() {
return nRF51Gap::getInstance();
};
virtual const Gap &getGap() const {
return nRF51Gap::getInstance();
};
virtual GattServer &getGattServer() {
return nRF51GattServer::getInstance();
};
virtual ble_error_t setTxPower(int8_t txPower);
virtual void getPermittedTxPowerValues(const int8_t **valueArrayPP, size_t *countP);
virtual ble_error_t init(void);
virtual ble_error_t shutdown(void);
virtual ble_error_t reset(void);
virtual ble_error_t initializeSecurity(bool enableBonding = true,
bool requireMITM = true,
Gap::SecurityIOCapabilities_t iocaps = Gap::IO_CAPS_NONE,
const Gap::Passkey_t passkey = NULL) {
return btle_initializeSecurity(enableBonding, requireMITM, iocaps, passkey);
virtual const GattServer &getGattServer() const {
return nRF51GattServer::getInstance();
};
virtual GattClient &getGattClient() {
return nRF51GattClient::getInstance();
}
virtual const SecurityManager &getSecurityManager() const {
return nRF51SecurityManager::getInstance();
}
virtual SecurityManager &getSecurityManager() {
return nRF51SecurityManager::getInstance();
}
virtual void waitForEvent(void);
};

View File

@ -0,0 +1,63 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 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 "nRF51DiscoveredCharacteristic.h"
#include "nRF51GattClient.h"
#include "ble_gatt.h"
void
nRF51DiscoveredCharacteristic::setup(nRF51GattClient *gattcIn,
Gap::Handle_t connectionHandleIn,
ble_gatt_char_props_t propsIn,
GattAttribute::Handle_t declHandleIn,
GattAttribute::Handle_t valueHandleIn)
{
gattc = gattcIn;
connHandle = connectionHandleIn;
declHandle = declHandleIn;
valueHandle = valueHandleIn;
props._broadcast = propsIn.broadcast;
props._read = propsIn.read;
props._writeWoResp = propsIn.write_wo_resp;
props._write = propsIn.write;
props._notify = propsIn.notify;
props._indicate = propsIn.indicate;
props._authSignedWrite = propsIn.auth_signed_wr;
}
void
nRF51DiscoveredCharacteristic::setup(nRF51GattClient *gattcIn,
Gap::Handle_t connectionHandleIn,
UUID::ShortUUIDBytes_t uuidIn,
ble_gatt_char_props_t propsIn,
GattAttribute::Handle_t declHandleIn,
GattAttribute::Handle_t valueHandleIn)
{
gattc = gattcIn;
connHandle = connectionHandleIn;
uuid = uuidIn;
declHandle = declHandleIn;
valueHandle = valueHandleIn;
props._broadcast = propsIn.broadcast;
props._read = propsIn.read;
props._writeWoResp = propsIn.write_wo_resp;
props._write = propsIn.write;
props._notify = propsIn.notify;
props._indicate = propsIn.indicate;
props._authSignedWrite = propsIn.auth_signed_wr;
}

View File

@ -0,0 +1,41 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 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 __NRF_DISCOVERED_CHARACTERISTIC_H__
#define __NRF_DISCOVERED_CHARACTERISTIC_H__
#include "ble/DiscoveredCharacteristic.h"
#include "ble_gatt.h"
class nRF51GattClient; /* forward declaration */
class nRF51DiscoveredCharacteristic : public DiscoveredCharacteristic {
public:
void setup(nRF51GattClient *gattcIn,
Gap::Handle_t connectionHandleIn,
ble_gatt_char_props_t propsIn,
GattAttribute::Handle_t declHandleIn,
GattAttribute::Handle_t valueHandleIn);
void setup(nRF51GattClient *gattcIn,
Gap::Handle_t connectionHandleIn,
UUID::ShortUUIDBytes_t uuidIn,
ble_gatt_char_props_t propsIn,
GattAttribute::Handle_t declHandleIn,
GattAttribute::Handle_t valueHandleIn);
};
#endif /* __NRF_DISCOVERED_CHARACTERISTIC_H__ */

View File

@ -206,23 +206,66 @@ ble_error_t nRF51Gap::stopAdvertising(void)
return BLE_ERROR_NONE;
}
/**************************************************************************/
/*!
@brief Disconnects if we are connected to a central device
ble_error_t nRF51Gap::connect(const Address_t peerAddr,
Gap::AddressType_t peerAddrType,
const ConnectionParams_t *connectionParams,
const GapScanningParams *scanParamsIn)
{
ble_gap_addr_t addr;
addr.addr_type = peerAddrType;
memcpy(addr.addr, peerAddr, Gap::ADDR_LEN);
@returns ble_error_t
ble_gap_conn_params_t connParams;
if (connectionParams != NULL) {
connParams.min_conn_interval = connectionParams->minConnectionInterval;
connParams.max_conn_interval = connectionParams->maxConnectionInterval;
connParams.slave_latency = connectionParams->slaveLatency;
connParams.conn_sup_timeout = connectionParams->connectionSupervisionTimeout;
} else {
connParams.min_conn_interval = 50;
connParams.max_conn_interval = 100;
connParams.slave_latency = 0;
connParams.conn_sup_timeout = 600;
}
@retval BLE_ERROR_NONE
Everything executed properly
ble_gap_scan_params_t scanParams;
scanParams.active = 0; /**< If 1, perform active scanning (scan requests). */
scanParams.selective = 0; /**< If 1, ignore unknown devices (non whitelisted). */
scanParams.p_whitelist = NULL; /**< Pointer to whitelist, NULL if none is given. */
if (scanParamsIn != NULL) {
scanParams.interval = scanParamsIn->getInterval(); /**< Scan interval between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
scanParams.window = scanParamsIn->getWindow(); /**< Scan window between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
scanParams.timeout = scanParamsIn->getTimeout(); /**< Scan timeout between 0x0001 and 0xFFFF in seconds, 0x0000 disables timeout. */
} else {
scanParams.interval = 500; /**< Scan interval between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
scanParams.window = 200; /**< Scan window between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
scanParams.timeout = 0; /**< Scan timeout between 0x0001 and 0xFFFF in seconds, 0x0000 disables timeout. */
}
@section EXAMPLE
uint32_t rc = sd_ble_gap_connect(&addr, &scanParams, &connParams);
if (rc == NRF_SUCCESS) {
return BLE_ERROR_NONE;
}
switch (rc) {
case NRF_ERROR_INVALID_ADDR:
return BLE_ERROR_INVALID_PARAM;
case NRF_ERROR_INVALID_PARAM:
return BLE_ERROR_INVALID_PARAM;
case NRF_ERROR_INVALID_STATE:
return BLE_ERROR_INVALID_STATE;
case BLE_ERROR_GAP_INVALID_BLE_ADDR:
return BLE_ERROR_INVALID_PARAM;
case NRF_ERROR_NO_MEM:
return BLE_ERROR_NO_MEM;
case NRF_ERROR_BUSY:
return BLE_STACK_BUSY;
default:
case BLE_ERROR_GAP_WHITELIST_IN_USE:
return BLE_ERROR_UNSPECIFIED;
}
}
@code
@endcode
*/
/**************************************************************************/
ble_error_t nRF51Gap::disconnect(DisconnectionReason_t reason)
ble_error_t nRF51Gap::disconnect(Handle_t connectionHandle, DisconnectionReason_t reason)
{
state.advertising = 0;
state.connected = 0;
@ -235,14 +278,29 @@ ble_error_t nRF51Gap::disconnect(DisconnectionReason_t reason)
case CONN_INTERVAL_UNACCEPTABLE:
code = BLE_HCI_CONN_INTERVAL_UNACCEPTABLE;
break;
default:
break;
}
/* Disconnect if we are connected to a central device */
ASSERT_INT(ERROR_NONE, sd_ble_gap_disconnect(m_connectionHandle, code), BLE_ERROR_PARAM_OUT_OF_RANGE);
ASSERT_INT(ERROR_NONE, sd_ble_gap_disconnect(connectionHandle, code), BLE_ERROR_PARAM_OUT_OF_RANGE);
return BLE_ERROR_NONE;
}
/*!
@brief Disconnects if we are connected to a central device
@returns ble_error_t
@retval BLE_ERROR_NONE
Everything executed properly
*/
ble_error_t nRF51Gap::disconnect(DisconnectionReason_t reason)
{
return disconnect(m_connectionHandle, reason);
}
ble_error_t nRF51Gap::getPreferredConnectionParams(ConnectionParams_t *params)
{
ASSERT_INT(NRF_SUCCESS,
@ -309,7 +367,7 @@ uint16_t nRF51Gap::getConnectionHandle(void)
@endcode
*/
/**************************************************************************/
ble_error_t nRF51Gap::setAddress(AddressType_t type, const address_t address)
ble_error_t nRF51Gap::setAddress(AddressType_t type, const Address_t address)
{
if (type > ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE) {
return BLE_ERROR_PARAM_OUT_OF_RANGE;
@ -324,7 +382,7 @@ ble_error_t nRF51Gap::setAddress(AddressType_t type, const address_t address)
return BLE_ERROR_NONE;
}
ble_error_t nRF51Gap::getAddress(AddressType_t *typeP, address_t address)
ble_error_t nRF51Gap::getAddress(AddressType_t *typeP, Address_t address)
{
ble_gap_addr_t dev_addr;
if (sd_ble_gap_address_get(&dev_addr) != NRF_SUCCESS) {
@ -361,7 +419,7 @@ ble_error_t nRF51Gap::getDeviceName(uint8_t *deviceName, unsigned *lengthP)
}
}
ble_error_t nRF51Gap::setAppearance(uint16_t appearance)
ble_error_t nRF51Gap::setAppearance(GapAdvertisingData::Appearance appearance)
{
if (sd_ble_gap_appearance_set(appearance) == NRF_SUCCESS) {
return BLE_ERROR_NONE;
@ -370,11 +428,38 @@ ble_error_t nRF51Gap::setAppearance(uint16_t appearance)
}
}
ble_error_t nRF51Gap::getAppearance(uint16_t *appearanceP)
ble_error_t nRF51Gap::getAppearance(GapAdvertisingData::Appearance *appearanceP)
{
if (sd_ble_gap_appearance_get(appearanceP)) {
if (sd_ble_gap_appearance_get(reinterpret_cast<uint16_t *>(appearanceP))) {
return BLE_ERROR_NONE;
} else {
return BLE_ERROR_PARAM_OUT_OF_RANGE;
}
}
/* (Valid values are -40, -20, -16, -12, -8, -4, 0, 4) */
ble_error_t nRF51Gap::setTxPower(int8_t txPower)
{
unsigned rc;
if ((rc = sd_ble_gap_tx_power_set(txPower)) != NRF_SUCCESS) {
switch (rc) {
case NRF_ERROR_BUSY:
return BLE_STACK_BUSY;
case NRF_ERROR_INVALID_PARAM:
default:
return BLE_ERROR_PARAM_OUT_OF_RANGE;
}
}
return BLE_ERROR_NONE;
}
void nRF51Gap::getPermittedTxPowerValues(const int8_t **valueArrayPP, size_t *countP)
{
static const int8_t permittedTxValues[] = {
-40, -30, -20, -16, -12, -8, -4, 0, 4
};
*valueArrayPP = permittedTxValues;
*countP = sizeof(permittedTxValues) / sizeof(int8_t);
}

View File

@ -18,12 +18,12 @@
#define __NRF51822_GAP_H__
#include "mbed.h"
#include "blecommon.h"
#include "ble/blecommon.h"
#include "ble.h"
#include "GapAdvertisingParams.h"
#include "GapAdvertisingData.h"
#include "Gap.h"
#include "GapScanningParams.h"
#include "ble/GapAdvertisingParams.h"
#include "ble/GapAdvertisingData.h"
#include "ble/Gap.h"
#include "ble/GapScanningParams.h"
#include "nrf_soc.h"
#include "ble_radio_notification.h"
@ -41,8 +41,8 @@ public:
static nRF51Gap &getInstance();
/* Functions that must be implemented from Gap */
virtual ble_error_t setAddress(AddressType_t type, const address_t address);
virtual ble_error_t getAddress(AddressType_t *typeP, address_t address);
virtual ble_error_t setAddress(AddressType_t type, const Address_t address);
virtual ble_error_t getAddress(AddressType_t *typeP, Address_t address);
virtual ble_error_t setAdvertisingData(const GapAdvertisingData &, const GapAdvertisingData &);
virtual uint16_t getMinAdvertisingInterval(void) const {return ADVERTISEMENT_DURATION_UNITS_TO_MS(BLE_GAP_ADV_INTERVAL_MIN);}
@ -51,17 +51,17 @@ public:
virtual ble_error_t startAdvertising(const GapAdvertisingParams &);
virtual ble_error_t stopAdvertising(void);
virtual ble_error_t connect(const Address_t, Gap::AddressType_t peerAddrType, const ConnectionParams_t *connectionParams, const GapScanningParams *scanParams);
virtual ble_error_t disconnect(Handle_t connectionHandle, DisconnectionReason_t reason);
virtual ble_error_t disconnect(DisconnectionReason_t reason);
virtual ble_error_t purgeAllBondingState(void) {return btle_purgeAllBondingState();}
virtual ble_error_t getLinkSecurity(Handle_t connectionHandle, LinkSecurityStatus_t *securityStatusP) {
return btle_getLinkSecurity(connectionHandle, securityStatusP);
}
virtual ble_error_t setDeviceName(const uint8_t *deviceName);
virtual ble_error_t getDeviceName(uint8_t *deviceName, unsigned *lengthP);
virtual ble_error_t setAppearance(uint16_t appearance);
virtual ble_error_t getAppearance(uint16_t *appearanceP);
virtual ble_error_t setAppearance(GapAdvertisingData::Appearance appearance);
virtual ble_error_t getAppearance(GapAdvertisingData::Appearance *appearanceP);
virtual ble_error_t setTxPower(int8_t txPower);
virtual void getPermittedTxPowerValues(const int8_t **valueArrayPP, size_t *countP);
void setConnectionHandle(uint16_t con_handle);
uint16_t getConnectionHandle(void);
@ -70,9 +70,9 @@ public:
virtual ble_error_t setPreferredConnectionParams(const ConnectionParams_t *params);
virtual ble_error_t updateConnectionParams(Handle_t handle, const ConnectionParams_t *params);
virtual void setOnRadioNotification(RadioNotificationEventCallback_t callback) {
Gap::setOnRadioNotification(callback);
ble_radio_notification_init(NRF_APP_PRIORITY_HIGH, NRF_RADIO_NOTIFICATION_DISTANCE_800US, onRadioNotification);
virtual void onRadioNotification(RadioNotificationEventCallback_t callback) {
Gap::onRadioNotification(callback);
ble_radio_notification_init(NRF_APP_PRIORITY_HIGH, NRF_RADIO_NOTIFICATION_DISTANCE_800US, radioNotificationCallback);
}
virtual ble_error_t startRadioScan(const GapScanningParams &scanningParams) {

34
nRF51GattClient.cpp Normal file
View File

@ -0,0 +1,34 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 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 "nRF51GattClient.h"
nRF51GattClient nRFGattClientSingleton;
nRF51GattClient &
nRF51GattClient::getInstance(void) {
return nRFGattClientSingleton;
}
ble_error_t
nRF51GattClient::launchServiceDiscovery(Gap::Handle_t connectionHandle,
ServiceDiscovery::ServiceCallback_t sc,
ServiceDiscovery::CharacteristicCallback_t cc,
const UUID &matchingServiceUUIDIn,
const UUID &matchingCharacteristicUUIDIn)
{
return discovery.launch(connectionHandle, sc, cc, matchingServiceUUIDIn, matchingCharacteristicUUIDIn);
}

157
nRF51GattClient.h Normal file
View File

@ -0,0 +1,157 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 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 __NRF51822_GATT_CLIENT_H__
#define __NRF51822_GATT_CLIENT_H__
#include "ble/GattClient.h"
#include "nRF51ServiceDiscovery.h"
class nRF51GattClient : public GattClient
{
public:
static nRF51GattClient &getInstance();
/**
* Launch service discovery. Once launched, service discovery will remain
* active with callbacks being issued back into the application for matching
* services/characteristics. isActive() can be used to determine status; and
* a termination callback (if setup) will be invoked at the end. Service
* discovery can be terminated prematurely if needed using terminate().
*
* @param connectionHandle
* Handle for the connection with the peer.
* @param sc
* This is the application callback for matching service. Taken as
* NULL by default. Note: service discovery may still be active
* when this callback is issued; calling asynchronous BLE-stack
* APIs from within this application callback might cause the
* stack to abort service discovery. If this becomes an issue, it
* may be better to make local copy of the discoveredService and
* wait for service discovery to terminate before operating on the
* service.
* @param cc
* This is the application callback for matching characteristic.
* Taken as NULL by default. Note: service discovery may still be
* active when this callback is issued; calling asynchronous
* BLE-stack APIs from within this application callback might cause
* the stack to abort service discovery. If this becomes an issue,
* it may be better to make local copy of the discoveredCharacteristic
* and wait for service discovery to terminate before operating on the
* characteristic.
* @param matchingServiceUUID
* UUID based filter for specifying a service in which the application is
* interested. By default it is set as the wildcard UUID_UNKNOWN,
* in which case it matches all services. If characteristic-UUID
* filter (below) is set to the wildcard value, then a service
* callback will be invoked for the matching service (or for every
* service if the service filter is a wildcard).
* @param matchingCharacteristicUUIDIn
* UUID based filter for specifying characteristic in which the application
* is interested. By default it is set as the wildcard UUID_UKNOWN
* to match against any characteristic. If both service-UUID
* filter and characteristic-UUID filter are used with non- wildcard
* values, then only a single characteristic callback is
* invoked for the matching characteristic.
*
* @Note Using wildcard values for both service-UUID and characteristic-
* UUID will result in complete service discovery--callbacks being
* called for every service and characteristic.
*
* @return
* BLE_ERROR_NONE if service discovery is launched successfully; else an appropriate error.
*/
virtual ble_error_t launchServiceDiscovery(Gap::Handle_t connectionHandle,
ServiceDiscovery::ServiceCallback_t sc = NULL,
ServiceDiscovery::CharacteristicCallback_t cc = NULL,
const UUID &matchingServiceUUID = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN),
const UUID &matchingCharacteristicUUIDIn = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN));
virtual void onServiceDiscoveryTermination(ServiceDiscovery::TerminationCallback_t callback) {
discovery.onTermination(callback);
}
/**
* Is service-discovery currently active?
*/
virtual bool isServiceDiscoveryActive(void) const {
return discovery.isActive();
}
/**
* Terminate an ongoing service-discovery. This should result in an
* invocation of the TerminationCallback if service-discovery is active.
*/
virtual void terminateServiceDiscovery(void) {
discovery.terminate();
}
virtual ble_error_t read(Gap::Handle_t connHandle, GattAttribute::Handle_t attributeHandle, uint16_t offset) const {
uint32_t rc = sd_ble_gattc_read(connHandle, attributeHandle, offset);
if (rc == NRF_SUCCESS) {
return BLE_ERROR_NONE;
}
switch (rc) {
case NRF_ERROR_BUSY:
return BLE_STACK_BUSY;
case BLE_ERROR_INVALID_CONN_HANDLE:
case NRF_ERROR_INVALID_STATE:
case NRF_ERROR_INVALID_ADDR:
default:
return BLE_ERROR_INVALID_STATE;
}
}
virtual ble_error_t write(GattClient::WriteOp_t cmd, Gap::Handle_t connHandle, GattAttribute::Handle_t attributeHandle, size_t length, const uint8_t *value) const {
ble_gattc_write_params_t writeParams = { };
writeParams.write_op = cmd;
writeParams.handle = attributeHandle;
writeParams.len = length;
writeParams.p_value = const_cast<uint8_t *>(value);
uint32_t rc = sd_ble_gattc_write(connHandle, &writeParams);
if (rc == NRF_SUCCESS) {
return BLE_ERROR_NONE;
}
switch (rc) {
case NRF_ERROR_BUSY:
return BLE_STACK_BUSY;
case BLE_ERROR_NO_TX_BUFFERS:
return BLE_ERROR_NO_MEM;
case BLE_ERROR_INVALID_CONN_HANDLE:
case NRF_ERROR_INVALID_STATE:
case NRF_ERROR_INVALID_ADDR:
default:
return BLE_ERROR_INVALID_STATE;
}
}
public:
nRF51GattClient() : discovery(this) {
/* empty */
}
friend void bleGattcEventHandler(const ble_evt_t *p_ble_evt);
private:
nRF51GattClient(const nRF51GattClient &);
const nRF51GattClient& operator=(const nRF51GattClient &);
private:
nRF51ServiceDiscovery discovery;
};
#endif // ifndef __NRF51822_GATT_CLIENT_H__

View File

@ -159,12 +159,12 @@ ble_error_t nRF51GattServer::addService(GattService &service)
Everything executed properly
*/
/**************************************************************************/
ble_error_t nRF51GattServer::readValue(GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP)
ble_error_t nRF51GattServer::read(GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP)
{
return readValue(BLE_CONN_HANDLE_INVALID, attributeHandle, buffer, lengthP);
return read(BLE_CONN_HANDLE_INVALID, attributeHandle, buffer, lengthP);
}
ble_error_t nRF51GattServer::readValue(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP)
ble_error_t nRF51GattServer::read(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP)
{
ble_gatts_value_t value = {
.len = *lengthP,
@ -199,12 +199,12 @@ ble_error_t nRF51GattServer::readValue(Gap::Handle_t connectionHandle, GattAttri
Everything executed properly
*/
/**************************************************************************/
ble_error_t nRF51GattServer::updateValue(GattAttribute::Handle_t attributeHandle, const uint8_t buffer[], uint16_t len, bool localOnly)
ble_error_t nRF51GattServer::write(GattAttribute::Handle_t attributeHandle, const uint8_t buffer[], uint16_t len, bool localOnly)
{
return updateValue(BLE_CONN_HANDLE_INVALID, attributeHandle, buffer, len, localOnly);
return write(BLE_CONN_HANDLE_INVALID, attributeHandle, buffer, len, localOnly);
}
ble_error_t nRF51GattServer::updateValue(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, const uint8_t buffer[], uint16_t len, bool localOnly)
ble_error_t nRF51GattServer::write(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, const uint8_t buffer[], uint16_t len, bool localOnly)
{
uint16_t gapConnectionHandle = nRF51Gap::getInstance().getConnectionHandle();
ble_error_t returnValue = BLE_ERROR_NONE;
@ -336,22 +336,22 @@ void nRF51GattServer::hwCallback(ble_evt_t *p_ble_evt)
if (nrfCharacteristicHandles[i].value_handle == handle_value) {
switch (eventType) {
case GattServerEvents::GATT_EVENT_DATA_WRITTEN: {
GattCharacteristicWriteCBParams cbParams = {
.charHandle = i,
.op = static_cast<GattCharacteristicWriteCBParams::Type>(gattsEventP->params.write.op),
.offset = gattsEventP->params.write.offset,
.len = gattsEventP->params.write.len,
.data = gattsEventP->params.write.data
GattWriteCallbackParams cbParams = {
.handle = i,
.writeOp = static_cast<GattWriteCallbackParams::WriteOp_t>(gattsEventP->params.write.op),
.offset = gattsEventP->params.write.offset,
.len = gattsEventP->params.write.len,
.data = gattsEventP->params.write.data
};
handleDataWrittenEvent(&cbParams);
break;
}
case GattServerEvents::GATT_EVENT_WRITE_AUTHORIZATION_REQ: {
GattCharacteristicWriteAuthCBParams cbParams = {
.charHandle = i,
.offset = gattsEventP->params.authorize_request.request.write.offset,
.len = gattsEventP->params.authorize_request.request.write.len,
.data = gattsEventP->params.authorize_request.request.write.data,
GattWriteAuthCallbackParams cbParams = {
.handle = i,
.offset = gattsEventP->params.authorize_request.request.write.offset,
.len = gattsEventP->params.authorize_request.request.write.len,
.data = gattsEventP->params.authorize_request.request.write.data,
};
ble_gatts_rw_authorize_reply_params_t reply = {
.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE,
@ -371,23 +371,23 @@ void nRF51GattServer::hwCallback(ble_evt_t *p_ble_evt)
* have done if write-authorization had not been enabled.
*/
if (reply.params.write.gatt_status == BLE_GATT_STATUS_SUCCESS) {
GattCharacteristicWriteCBParams cbParams = {
.charHandle = i,
.op = static_cast<GattCharacteristicWriteCBParams::Type>(gattsEventP->params.authorize_request.request.write.op),
.offset = gattsEventP->params.authorize_request.request.write.offset,
.len = gattsEventP->params.authorize_request.request.write.len,
.data = gattsEventP->params.authorize_request.request.write.data,
GattWriteCallbackParams cbParams = {
.handle = i,
.writeOp = static_cast<GattWriteCallbackParams::WriteOp_t>(gattsEventP->params.authorize_request.request.write.op),
.offset = gattsEventP->params.authorize_request.request.write.offset,
.len = gattsEventP->params.authorize_request.request.write.len,
.data = gattsEventP->params.authorize_request.request.write.data,
};
handleDataWrittenEvent(&cbParams);
}
break;
}
case GattServerEvents::GATT_EVENT_READ_AUTHORIZATION_REQ: {
GattCharacteristicReadAuthCBParams cbParams = {
.charHandle = i,
.offset = gattsEventP->params.authorize_request.request.read.offset,
.len = 0,
.data = NULL
GattReadAuthCallbackParams cbParams = {
.handle = i,
.offset = gattsEventP->params.authorize_request.request.read.offset,
.len = 0,
.data = NULL
};
ble_gatts_rw_authorize_reply_params_t reply = {
@ -419,10 +419,3 @@ void nRF51GattServer::hwCallback(ble_evt_t *p_ble_evt)
}
}
}
ble_error_t
nRF51GattServer::initializeGATTDatabase(void)
{
/* Empty. Services are populated in the GattDatabase through addService(). */
return BLE_ERROR_NONE;
}

View File

@ -19,10 +19,10 @@
#include <stddef.h>
#include "blecommon.h"
#include "ble/blecommon.h"
#include "ble.h" /* nordic ble */
#include "Gap.h"
#include "GattServer.h"
#include "ble/Gap.h"
#include "ble/GattServer.h"
class nRF51GattServer : public GattServer
{
@ -31,11 +31,10 @@ public:
/* Functions that must be implemented from GattServer */
virtual ble_error_t addService(GattService &);
virtual ble_error_t readValue(GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP);
virtual ble_error_t readValue(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP);
virtual ble_error_t updateValue(GattAttribute::Handle_t, const uint8_t[], uint16_t, bool localOnly = false);
virtual ble_error_t updateValue(Gap::Handle_t connectionHandle, GattAttribute::Handle_t, const uint8_t[], uint16_t, bool localOnly = false);
virtual ble_error_t initializeGATTDatabase(void);
virtual 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);
virtual 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);
/* nRF51 Functions */
void eventCallback(void);

22
nRF51SecurityManager.cpp Normal file
View File

@ -0,0 +1,22 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 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 "nRF51SecurityManager.h"
nRF51SecurityManager &nRF51SecurityManager::getInstance(void) {
static nRF51SecurityManager m_instance;
return m_instance;
}

56
nRF51SecurityManager.h Normal file
View File

@ -0,0 +1,56 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 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 __NRF51822_SECURITY_MANAGER_H__
#define __NRF51822_SECURITY_MANAGER_H__