BLE: Remove old client implementation.

pull/5739/head
Vincent Coubard 2017-12-20 16:27:12 +00:00
parent 865bd94fbd
commit 3fdb749a33
10 changed files with 0 additions and 1792 deletions

View File

@ -1,147 +0,0 @@
/* 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 "nRF5xServiceDiscovery.h"
#include "nRF5xCharacteristicDescriptorDiscoverer.h"
#include "nRF5xGattClient.h"
#include "nRF5xn.h"
#if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110)
void bleGattcEventHandler(const ble_evt_t *p_ble_evt)
{
nRF5xn &ble = nRF5xn::Instance(BLE::DEFAULT_INSTANCE);
nRF5xGattClient &gattClient = (nRF5xGattClient &) ble.getGattClient();
nRF5xServiceDiscovery &sdSingleton = gattClient.discovery();
nRF5xCharacteristicDescriptorDiscoverer &characteristicDescriptorDiscoverer =
gattClient.characteristicDescriptorDiscoverer();
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(BLE_ERROR_NONE);
break;
}
break;
case BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP:
if (sdSingleton.isActive()) {
sdSingleton.processDiscoverUUIDResponse(&p_ble_evt->evt.gattc_evt);
}
break;
case BLE_GATTC_EVT_READ_RSP: {
GattReadCallbackParams response = {
/* .connHandle = */ p_ble_evt->evt.gattc_evt.conn_handle,
/* .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,
};
gattClient.processReadResponse(&response);
}
break;
case BLE_GATTC_EVT_WRITE_RSP: {
GattWriteCallbackParams response = {
/* .connHandle = */ p_ble_evt->evt.gattc_evt.conn_handle,
/* .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,
};
gattClient.processWriteResponse(&response);
}
break;
case BLE_GATTC_EVT_HVX: {
GattHVXCallbackParams params = {
/* connHandle = */ p_ble_evt->evt.gattc_evt.conn_handle,
/* handle = */ p_ble_evt->evt.gattc_evt.params.hvx.handle,
/* type = */ static_cast<HVXType_t>(p_ble_evt->evt.gattc_evt.params.hvx.type),
/* len = */ p_ble_evt->evt.gattc_evt.params.hvx.len,
/* data = */ p_ble_evt->evt.gattc_evt.params.hvx.data
};
gattClient.processHVXEvent(&params);
}
break;
case BLE_GATTC_EVT_DESC_DISC_RSP: {
uint16_t conn_handle = p_ble_evt->evt.gattc_evt.conn_handle;
uint16_t status = p_ble_evt->evt.gattc_evt.gatt_status;
const ble_gattc_evt_desc_disc_rsp_t& discovered_descriptors = p_ble_evt->evt.gattc_evt.params.desc_disc_rsp;
switch(status) {
case BLE_GATT_STATUS_SUCCESS:
characteristicDescriptorDiscoverer.process(
conn_handle,
discovered_descriptors
);
break;
case BLE_GATT_STATUS_ATTERR_ATTRIBUTE_NOT_FOUND:
// end of discovery
characteristicDescriptorDiscoverer.terminate(conn_handle, BLE_ERROR_NONE);
break;
default:
characteristicDescriptorDiscoverer.terminate(conn_handle, BLE_ERROR_UNSPECIFIED);
break;
}
} break;
case BLE_GATTC_EVT_ATTR_INFO_DISC_RSP : {
uint16_t conn_handle = p_ble_evt->evt.gattc_evt.conn_handle;
uint16_t status = p_ble_evt->evt.gattc_evt.gatt_status;
const ble_gattc_evt_attr_info_disc_rsp_t& infos = p_ble_evt->evt.gattc_evt.params.attr_info_disc_rsp;
switch(status) {
case BLE_GATT_STATUS_SUCCESS:
characteristicDescriptorDiscoverer.processAttributeInformation(
conn_handle,
infos
);
break;
default:
characteristicDescriptorDiscoverer.terminate(conn_handle, BLE_ERROR_UNSPECIFIED);
break;
}
} break;
}
sdSingleton.progressCharacteristicDiscovery();
sdSingleton.progressServiceDiscovery();
}
#endif

View File

@ -1,22 +0,0 @@
/* 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

@ -1,325 +0,0 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2015 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 "nRF5xCharacteristicDescriptorDiscoverer.h"
#include "headers/nrf_ble_err.h"
#include "ble/DiscoveredCharacteristicDescriptor.h"
nRF5xCharacteristicDescriptorDiscoverer::nRF5xCharacteristicDescriptorDiscoverer() :
discoveryRunning() {
// nothing to do
}
nRF5xCharacteristicDescriptorDiscoverer::~nRF5xCharacteristicDescriptorDiscoverer() {
// nothing to do
}
ble_error_t nRF5xCharacteristicDescriptorDiscoverer::launch(
const DiscoveredCharacteristic& characteristic,
const CharacteristicDescriptorDiscovery::DiscoveryCallback_t& discoveryCallback,
const CharacteristicDescriptorDiscovery::TerminationCallback_t& terminationCallback
) {
Gap::Handle_t connHandle = characteristic.getConnectionHandle();
// it is ok to deduce that the start handle for descriptors is after
// the characteristic declaration and the characteristic value declaration
// see BLUETOOTH SPECIFICATION Version 4.2 [Vol 3, Part G] (3.3)
Gap::Handle_t descriptorStartHandle = characteristic.getDeclHandle() + 2;
Gap::Handle_t descriptorEndHandle = characteristic.getLastHandle();
// check if there is any descriptor to discover
if (descriptorEndHandle < descriptorStartHandle) {
CharacteristicDescriptorDiscovery::TerminationCallbackParams_t termParams = {
characteristic,
BLE_ERROR_NONE
};
terminationCallback.call(&termParams);
return BLE_ERROR_NONE;
}
// check if we can run this discovery
if (isConnectionInUse(connHandle)) {
return BLE_STACK_BUSY;
}
// get a new discovery slot, if none are available, just return
Discovery* discovery = getAvailableDiscoverySlot();
if(discovery == NULL) {
return BLE_STACK_BUSY;
}
// try to launch the discovery
ble_error_t err = gattc_descriptors_discover(connHandle, descriptorStartHandle, descriptorEndHandle);
if(!err) {
// commit the new discovery to its slot
*discovery = Discovery(characteristic, discoveryCallback, terminationCallback);
}
return err;
}
bool nRF5xCharacteristicDescriptorDiscoverer::isActive(const DiscoveredCharacteristic& characteristic) const {
for(size_t i = 0; i < MAXIMUM_CONCURRENT_CONNECTIONS_COUNT; ++i) {
if(discoveryRunning[i].getCharacteristic() == characteristic) {
return true;
}
}
return false;
}
void nRF5xCharacteristicDescriptorDiscoverer::requestTerminate(const DiscoveredCharacteristic& characteristic) {
Discovery* discovery = findRunningDiscovery(characteristic);
if(discovery) {
// call terminate anyway
terminate(discovery, BLE_ERROR_NONE);
}
}
void nRF5xCharacteristicDescriptorDiscoverer::process(uint16_t connectionHandle, const ble_gattc_evt_desc_disc_rsp_t& descriptors) {
Discovery* discovery = findRunningDiscovery(connectionHandle);
// the discovery has been removed
if(!discovery) {
return;
}
for (uint16_t i = 0; i < descriptors.count; ++i) {
const ble_gattc_desc_t& desc = descriptors.descs[i];
const ble_uuid_t& uuid = desc.uuid;
if (uuid.type == BLE_UUID_TYPE_BLE) {
discovery->process(
desc.handle, UUID(uuid.uuid)
);
} else {
// discover attribute infos of the descriptor
ble_error_t err = gattc_attr_info_discover(connectionHandle, desc.handle, desc.handle);
if (err) {
terminate(discovery, err);
}
return;
}
}
// prepare the next discovery request (if needed)
uint16_t startHandle = descriptors.descs[descriptors.count - 1].handle + 1;
uint16_t endHandle = discovery->getCharacteristic().getLastHandle();
if(startHandle > endHandle) {
terminate(discovery, BLE_ERROR_NONE);
return;
}
ble_error_t err = gattc_descriptors_discover(connectionHandle, startHandle, endHandle);
if(err) {
terminate(discovery, err);
return;
}
}
void nRF5xCharacteristicDescriptorDiscoverer::processAttributeInformation(
uint16_t connectionHandle, const ble_gattc_evt_attr_info_disc_rsp_t& infos) {
Discovery* discovery = findRunningDiscovery(connectionHandle);
// the discovery has been removed
if(!discovery) {
return;
}
#if (NRF_SD_BLE_API_VERSION <= 2)
// for all UUIDS found, process the discovery
for (uint16_t i = 0; i < infos.count; ++i) {
bool use_16bits_uuids = infos.format == BLE_GATTC_ATTR_INFO_FORMAT_16BIT;
const ble_gattc_attr_info_t& attr_info = infos.attr_info[i];
UUID uuid = use_16bits_uuids ? UUID(attr_info.info.uuid16.uuid) : UUID(attr_info.info.uuid128.uuid128, UUID::LSB);
discovery->process(attr_info.handle, uuid);
}
// prepare the next round of descriptors discovery
uint16_t startHandle = infos.attr_info[infos.count - 1].handle + 1;
#else
uint16_t startHandle;
// for all UUIDS found, process the discovery
if (infos.format == BLE_GATTC_ATTR_INFO_FORMAT_16BIT) {
for (uint16_t i = 0; i < infos.count; ++i) {
UUID uuid = UUID(infos.info.attr_info16[i].uuid.uuid);
discovery->process(infos.info.attr_info16[i].handle, uuid);
}
// prepare the next round of descriptors discovery
startHandle = infos.info.attr_info16[infos.count - 1].handle + 1;
} else {
for (uint16_t i = 0; i < infos.count; ++i) {
UUID uuid = UUID(infos.info.attr_info128[i].uuid.uuid128, UUID::LSB);
discovery->process(infos.info.attr_info128[i].handle, uuid);
}
// prepare the next round of descriptors discovery
startHandle = infos.info.attr_info128[infos.count - 1].handle + 1;
}
#endif
uint16_t endHandle = discovery->getCharacteristic().getLastHandle();
if(startHandle > endHandle) {
terminate(discovery, BLE_ERROR_NONE);
return;
}
ble_error_t err = gattc_descriptors_discover(connectionHandle, startHandle, endHandle);
if(err) {
terminate(discovery, err);
return;
}
}
void nRF5xCharacteristicDescriptorDiscoverer::terminate(uint16_t handle, ble_error_t err) {
Discovery* discovery = findRunningDiscovery(handle);
// the discovery has already been terminated
if(!discovery) {
return;
}
terminate(discovery, err);
}
void nRF5xCharacteristicDescriptorDiscoverer::terminate(Discovery* discovery, ble_error_t err) {
// temporary copy, user code can try to launch a new discovery in the onTerminate
// callback. So, this discovery should not appear in such case.
Discovery tmp = *discovery;
*discovery = Discovery();
tmp.terminate(err);
}
nRF5xCharacteristicDescriptorDiscoverer::Discovery*
nRF5xCharacteristicDescriptorDiscoverer::findRunningDiscovery(const DiscoveredCharacteristic& characteristic) {
for(size_t i = 0; i < MAXIMUM_CONCURRENT_CONNECTIONS_COUNT; ++i) {
if((discoveryRunning[i].getCharacteristic() == characteristic) &&
(discoveryRunning[i].isEmpty() == false)) {
return &discoveryRunning[i];
}
}
return NULL;
}
nRF5xCharacteristicDescriptorDiscoverer::Discovery*
nRF5xCharacteristicDescriptorDiscoverer::findRunningDiscovery(uint16_t handle) {
for(size_t i = 0; i < MAXIMUM_CONCURRENT_CONNECTIONS_COUNT; ++i) {
if((discoveryRunning[i].getCharacteristic().getConnectionHandle() == handle) &&
(discoveryRunning[i].isEmpty() == false)) {
return &discoveryRunning[i];
}
}
return NULL;
}
nRF5xCharacteristicDescriptorDiscoverer::Discovery*
nRF5xCharacteristicDescriptorDiscoverer::getAvailableDiscoverySlot() {
for(size_t i = 0; i < MAXIMUM_CONCURRENT_CONNECTIONS_COUNT; ++i) {
if(discoveryRunning[i].isEmpty()) {
return &discoveryRunning[i];
}
}
return NULL;
}
bool nRF5xCharacteristicDescriptorDiscoverer::isConnectionInUse(uint16_t connHandle) {
return findRunningDiscovery(connHandle) != NULL;
}
ble_error_t nRF5xCharacteristicDescriptorDiscoverer::gattc_descriptors_discover(
uint16_t connection_handle, uint16_t start_handle, uint16_t end_handle) {
ble_gattc_handle_range_t discoveryRange = {
start_handle,
end_handle
};
uint32_t err = sd_ble_gattc_descriptors_discover(connection_handle, &discoveryRange);
switch(err) {
case NRF_SUCCESS:
return BLE_ERROR_NONE;
case BLE_ERROR_INVALID_CONN_HANDLE:
return BLE_ERROR_INVALID_PARAM;
case NRF_ERROR_INVALID_ADDR:
return BLE_ERROR_PARAM_OUT_OF_RANGE;
case NRF_ERROR_BUSY:
return BLE_STACK_BUSY;
default:
return BLE_ERROR_UNSPECIFIED;
}
}
ble_error_t nRF5xCharacteristicDescriptorDiscoverer::gattc_attr_info_discover(
uint16_t connection_handle, uint16_t start_handle, uint16_t end_handle) {
ble_gattc_handle_range_t handle_range = { start_handle, end_handle };
uint32_t err = sd_ble_gattc_attr_info_discover(connection_handle, &handle_range);
switch(err) {
case NRF_SUCCESS:
return BLE_ERROR_NONE;
case BLE_ERROR_INVALID_CONN_HANDLE:
return BLE_ERROR_INVALID_PARAM;
case NRF_ERROR_INVALID_ADDR:
return BLE_ERROR_PARAM_OUT_OF_RANGE;
case NRF_ERROR_BUSY:
return BLE_STACK_BUSY;
case NRF_ERROR_INVALID_STATE:
return BLE_ERROR_INVALID_STATE;
default:
return BLE_ERROR_UNSPECIFIED;
}
}
// implementation of nRF5xCharacteristicDescriptorDiscoverer::Discovery
nRF5xCharacteristicDescriptorDiscoverer::Discovery::Discovery() :
characteristic(), onDiscovery(), onTerminate() {
}
nRF5xCharacteristicDescriptorDiscoverer::Discovery::Discovery(
const DiscoveredCharacteristic& c, const DiscoveryCallback_t& dCb, const TerminationCallback_t& tCb) :
characteristic(c), onDiscovery(dCb), onTerminate(tCb) {
}
void nRF5xCharacteristicDescriptorDiscoverer::Discovery::process(
GattAttribute::Handle_t handle, const UUID& uuid) {
CharacteristicDescriptorDiscovery::DiscoveryCallbackParams_t params = {
characteristic,
DiscoveredCharacteristicDescriptor(
characteristic.getGattClient(),
characteristic.getConnectionHandle(),
handle,
uuid
)
};
onDiscovery.call(&params);
}
void nRF5xCharacteristicDescriptorDiscoverer::Discovery::terminate(ble_error_t err) {
CharacteristicDescriptorDiscovery::TerminationCallbackParams_t params = {
characteristic,
err
};
onTerminate.call(&params);
}
bool nRF5xCharacteristicDescriptorDiscoverer::Discovery::isEmpty() const {
return *this == Discovery();
}
const DiscoveredCharacteristic& nRF5xCharacteristicDescriptorDiscoverer::Discovery::getCharacteristic() const {
return characteristic;
}

View File

@ -1,227 +0,0 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2015 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_CHARACTERISTIC_DESCRIPTOR_DISCOVERY_H__
#define __NRF_CHARACTERISTIC_DESCRIPTOR_DISCOVERY_H__
#include "ble/Gap.h"
#include "ble/DiscoveredCharacteristic.h"
#include "ble/CharacteristicDescriptorDiscovery.h"
#include "ble/GattClient.h"
#include "headers/nrf_ble_gattc.h"
/**
* @brief Manage the discovery of Characteristic descriptors
* @details is a bridge between BLE API and Nordic stack regarding Characteristic
* Descriptor discovery. The BLE API can launch, monitor and ask for termination
* of a discovery. The Nordic stack will provide new descriptors and indicate when
* the discovery is done.
*/
class nRF5xCharacteristicDescriptorDiscoverer
{
typedef CharacteristicDescriptorDiscovery::DiscoveryCallback_t DiscoveryCallback_t;
typedef CharacteristicDescriptorDiscovery::TerminationCallback_t TerminationCallback_t;
public:
/**
* @brief Construct a new characteristic descriptor discoverer.
*/
nRF5xCharacteristicDescriptorDiscoverer();
/**
* @brief Destroy a characteristic descriptor discoverer.
*/
~nRF5xCharacteristicDescriptorDiscoverer();
/**
* Launch a new characteristic descriptor discovery for a given DiscoveredCharacteristic.
* @param characteristic The characteristic owning the descriptors to discover.
* @param discoveryCallback The callback called when a descriptor is discovered.
* @param terminationCallback The callback called when the discovery process end.
* @return BLE_ERROR_NONE if characteristic descriptor discovery is launched successfully;
* else an appropriate error.
* @note: this will be called by BLE API side.
*/
ble_error_t launch(
const DiscoveredCharacteristic& characteristic,
const DiscoveryCallback_t& discoveryCallback,
const TerminationCallback_t& terminationCallback
);
/**
* @brief indicate if a characteristic descriptor discovery is active for a
* given DiscoveredCharacteristic.
* @param characteristic The characteristic for whom the descriptor might be
* currently discovered.
* @return true if descriptors of characteristic are discovered, false otherwise.
* @note: this will be called by BLE API side.
*/
bool isActive(const DiscoveredCharacteristic& characteristic) const;
/**
* @brief request the termination of characteristic descriptor discovery
* for a give DiscoveredCharacteristic
* @param characteristic The characteristic for whom the descriptor discovery
* should be stopped.
* @note: this will be called by BLE API side.
*/
void requestTerminate(const DiscoveredCharacteristic& characteristic);
/**
* @brief process descriptors discovered from the Nordic stack.
* @param connectionHandle The connection handle upon which descriptors has been
* discovered.
* @param descriptors Discovered descriptors.
* @note This will be called by the Nordic stack.
*/
void process(uint16_t connectionHandle, const ble_gattc_evt_desc_disc_rsp_t& descriptors);
/**
* @brief Called by the Nordic stack when the discovery is over.
* @param The connection handle upon which the discovery process is done.
* @param err An error if the termination is due to an error.
*/
void terminate(uint16_t connectionHandle, ble_error_t err);
/**
* @brief process attribute informations from the Nordic stack.
* @param connectionHandle The connection handle upon which
* attribute informations has been fetch.
* @param infos Informations around attribute, in that case the
* 128bit UUID of a descriptor.
* @note This will be called by the Nordic stack.
*/
void processAttributeInformation(uint16_t handle, const ble_gattc_evt_attr_info_disc_rsp_t& infos);
private:
// protection against copy construction and assignment
nRF5xCharacteristicDescriptorDiscoverer(const nRF5xCharacteristicDescriptorDiscoverer&);
nRF5xCharacteristicDescriptorDiscoverer& operator=(const nRF5xCharacteristicDescriptorDiscoverer&);
/**
* @brief Discovery process, it store the DiscoveredCharacteristic, the
* discovery callback and the termination callback.
*/
class Discovery {
public:
/**
* @brief Construct an empty discovery, such can be considerate as a not running discovery.
* @note #isEmpty function will return true
*/
Discovery();
/**
* @brief Construct a valid discovery process.
*
* @param c the characteristic from whom descriptors will be discovered.
* @param dCb The discovery callback called each time a descriptor is discovered.
* @param tCb The termination callback called when the discovery terminate.
*
* @note #isEmpty function will return false
*/
Discovery(const DiscoveredCharacteristic& c, const DiscoveryCallback_t& dCb, const TerminationCallback_t& tCb);
/**
* @brief Process the discovery of a descriptor.
*
* @param handle The attribute handle of the descriptor found
* @param uuid The UUID of the descriptor found.
*/
void process(GattAttribute::Handle_t handle, const UUID& uuid);
/**
* @brief Terminate the discovery process.
*
* @param err Error associate with the termination
* @note after this call #isEmpty function will return true.
*/
void terminate(ble_error_t err);
/**
* @brief check if the discovery process is empty or not. Empty discovery are
* not running.
*
* @detail Discovery are empty after:
* - a default construction
* - a copy construction form a default constructed
* - an assignment from a default constructed Discovery
* @return true if the Discovery is empty and false otherwise.
*/
bool isEmpty() const;
/**
* @brief return the characteristic from whom descriptors are discovered.
* @return the characteristic from whom descriptors are discovered.
*/
const DiscoveredCharacteristic& getCharacteristic() const;
/**
* @brief equal to operator, test if two discovery process are equal
*
* @param lhs left hand side of the expression
* @param rhs right hand side of the expression
* @return true if lhs == rhs
*/
friend bool operator==(const Discovery& lhs, const Discovery& rhs) {
return lhs.characteristic == rhs.characteristic &&
lhs.onDiscovery == rhs.onDiscovery &&
lhs.onTerminate == rhs.onTerminate;
}
/**
* @brief not equal to operator, test if two discovery process are not equal
*
* @param lhs left hand side of the expression
* @param rhs right hand side of the expression
* @return true if lhs != rhs
*/
friend bool operator!=(const Discovery& lhs, const Discovery& rhs) {
return !(lhs == rhs);
}
private:
DiscoveredCharacteristic characteristic;
DiscoveryCallback_t onDiscovery;
TerminationCallback_t onTerminate;
};
// find a running discovery process
Discovery* findRunningDiscovery(const DiscoveredCharacteristic& characteristic);
Discovery* findRunningDiscovery(uint16_t handle);
// Called to terminate a discovery is over.
void terminate(Discovery* discovery, ble_error_t err);
// get one slot for a discovery process
Discovery* getAvailableDiscoverySlot();
// indicate if a connection is already running a discovery
bool isConnectionInUse(uint16_t connHandle);
// low level start of a discovery
static ble_error_t gattc_descriptors_discover(uint16_t connection_handle, uint16_t start_handle, uint16_t end_handle);
// discovery of 128bits UUIDS
static ble_error_t gattc_attr_info_discover(uint16_t connection_handle, uint16_t start_handle, uint16_t end_handle);
// count of concurrent connections which can run a descriptor discovery process
static const size_t MAXIMUM_CONCURRENT_CONNECTIONS_COUNT = 3;
// array of running discoveries
Discovery discoveryRunning[MAXIMUM_CONCURRENT_CONNECTIONS_COUNT];
};
#endif /*__NRF_CHARACTERISTIC_DESCRIPTOR_DISCOVERY_H__*/

View File

@ -1,63 +0,0 @@
/* 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 "nRF5xDiscoveredCharacteristic.h"
#include "nRF5xGattClient.h"
#include "headers/nrf_ble_gatt.h"
void
nRF5xDiscoveredCharacteristic::setup(nRF5xGattClient *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
nRF5xDiscoveredCharacteristic::setup(nRF5xGattClient *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

@ -1,45 +0,0 @@
/* 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 "headers/nrf_ble_gatt.h"
class nRF5xGattClient; /* forward declaration */
class nRF5xDiscoveredCharacteristic : public DiscoveredCharacteristic {
public:
void setup(nRF5xGattClient *gattcIn,
Gap::Handle_t connectionHandleIn,
ble_gatt_char_props_t propsIn,
GattAttribute::Handle_t declHandleIn,
GattAttribute::Handle_t valueHandleIn);
void setup(nRF5xGattClient *gattcIn,
Gap::Handle_t connectionHandleIn,
UUID::ShortUUIDBytes_t uuidIn,
ble_gatt_char_props_t propsIn,
GattAttribute::Handle_t declHandleIn,
GattAttribute::Handle_t valueHandleIn);
void setLastHandle(GattAttribute::Handle_t last) {
lastHandle = last;
}
};
#endif /* __NRF_DISCOVERED_CHARACTERISTIC_H__ */

View File

@ -1,50 +0,0 @@
/* 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 "nRF5xGattClient.h"
#if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110)
ble_error_t
nRF5xGattClient::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);
}
ble_error_t nRF5xGattClient::discoverCharacteristicDescriptors(
const DiscoveredCharacteristic& characteristic,
const CharacteristicDescriptorDiscovery::DiscoveryCallback_t& discoveryCallback,
const CharacteristicDescriptorDiscovery::TerminationCallback_t& terminationCallback)
{
return _characteristicDescriptorDiscoverer.launch(
characteristic,
discoveryCallback,
terminationCallback
);
}
bool nRF5xGattClient::isCharacteristicDescriptorsDiscoveryActive(const DiscoveredCharacteristic& characteristic) const {
return _characteristicDescriptorDiscoverer.isActive(characteristic);
}
void nRF5xGattClient::terminateCharacteristicDescriptorsDiscovery(const DiscoveredCharacteristic& characteristic) {
return _characteristicDescriptorDiscoverer.requestTerminate(characteristic);
}
#endif

View File

@ -1,218 +0,0 @@
/* 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 "nRF5xServiceDiscovery.h"
#include "nRF5xCharacteristicDescriptorDiscoverer.h"
class nRF5xGattClient : public GattClient
{
public:
/**
* When using S110, all Gatt client features will return
* BLE_ERROR_NOT_IMPLEMENTED
*/
#if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110)
/**
* 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();
}
/**
* @brief Implementation of GattClient::discoverCharacteristicDescriptors
* @see GattClient::discoverCharacteristicDescriptors
*/
virtual ble_error_t discoverCharacteristicDescriptors(
const DiscoveredCharacteristic& characteristic,
const CharacteristicDescriptorDiscovery::DiscoveryCallback_t& discoveryCallback,
const CharacteristicDescriptorDiscovery::TerminationCallback_t& terminationCallback
);
/**
* @brief Implementation of GattClient::isCharacteristicDiscoveryActive
* @see GattClient::isCharacteristicDiscoveryActive
*/
virtual bool isCharacteristicDescriptorsDiscoveryActive(const DiscoveredCharacteristic& characteristic) const;
/**
* @brief Implementation of GattClient::terminateCharacteristicDiscovery
* @see GattClient::terminateCharacteristicDiscovery
*/
virtual void terminateCharacteristicDescriptorsDiscovery(const DiscoveredCharacteristic& characteristic);
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.flags = 0; /* this is inconsequential */
writeParams.handle = attributeHandle;
writeParams.offset = 0;
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_PACKETS:
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;
}
}
/**
* @brief Clear nRF5xGattClient's state.
*
* @return
* BLE_ERROR_NONE if successful.
*/
virtual ble_error_t reset(void) {
/* Clear all state that is from the parent, including private members */
if (GattClient::reset() != BLE_ERROR_NONE) {
return BLE_ERROR_INVALID_STATE;
}
/* Clear derived class members */
_discovery.reset();
return BLE_ERROR_NONE;
}
public:
/*
* Allow instantiation from nRF5xn when required.
*/
friend class nRF5xn;
nRF5xGattClient() : _discovery(this) {
/* empty */
}
nRF5xServiceDiscovery& discovery() {
return _discovery;
}
nRF5xCharacteristicDescriptorDiscoverer& characteristicDescriptorDiscoverer() {
return _characteristicDescriptorDiscoverer;
}
private:
nRF5xGattClient(const nRF5xGattClient &);
const nRF5xGattClient& operator=(const nRF5xGattClient &);
private:
nRF5xServiceDiscovery _discovery;
nRF5xCharacteristicDescriptorDiscoverer _characteristicDescriptorDiscoverer;
#endif // if !S110
};
#endif // ifndef __NRF51822_GATT_CLIENT_H__

View File

@ -1,329 +0,0 @@
/* 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 "nRF5xServiceDiscovery.h"
ble_error_t
nRF5xServiceDiscovery::launchCharacteristicDiscovery(Gap::Handle_t connectionHandle,
Gap::Handle_t startHandle,
Gap::Handle_t endHandle)
{
characteristicDiscoveryStarted(connectionHandle);
ble_gattc_handle_range_t handleRange = {
(uint16_t) startHandle,
(uint16_t) endHandle
};
uint32_t rc = sd_ble_gattc_characteristics_discover(connectionHandle, &handleRange);
ble_error_t err = BLE_ERROR_NONE;
switch (rc) {
case NRF_SUCCESS:
err = BLE_ERROR_NONE;
break;
case BLE_ERROR_INVALID_CONN_HANDLE:
case NRF_ERROR_INVALID_ADDR:
err = BLE_ERROR_INVALID_PARAM;
break;
case NRF_ERROR_BUSY:
err = BLE_STACK_BUSY;
break;
case NRF_ERROR_INVALID_STATE:
err = BLE_ERROR_INVALID_STATE;
break;
default:
err = BLE_ERROR_UNSPECIFIED;
break;
}
if (err) {
terminateCharacteristicDiscovery(err);
}
return err;
}
void
nRF5xServiceDiscovery::setupDiscoveredServices(const ble_gattc_evt_prim_srvc_disc_rsp_t *response)
{
serviceIndex = 0;
numServices = response->count;
/* Account for the limitation on the number of discovered services we can handle at a time. */
if (numServices > BLE_DB_DISCOVERY_MAX_SRV) {
numServices = BLE_DB_DISCOVERY_MAX_SRV;
}
serviceUUIDDiscoveryQueue.reset();
for (unsigned i = 0; i < numServices; ++i) {
if (response->services[i].uuid.type == BLE_UUID_TYPE_UNKNOWN) {
serviceUUIDDiscoveryQueue.enqueue(i);
services[i].setup(response->services[i].handle_range.start_handle,
response->services[i].handle_range.end_handle);
} else {
services[i].setup(response->services[i].uuid.uuid,
response->services[i].handle_range.start_handle,
response->services[i].handle_range.end_handle);
}
}
/* Trigger discovery of service UUID if necessary. */
if (serviceUUIDDiscoveryQueue.getCount()) {
serviceUUIDDiscoveryQueue.triggerFirst();
}
}
void
nRF5xServiceDiscovery::setupDiscoveredCharacteristics(const ble_gattc_evt_char_disc_rsp_t *response)
{
numCharacteristics = response->count;
/* Account for the limitation on the number of discovered characteristics we can handle at a time. */
if (numCharacteristics > BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV) {
numCharacteristics = BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV;
}
charUUIDDiscoveryQueue.reset();
for (unsigned charIndex = 0; charIndex < numCharacteristics; charIndex++) {
if (response->chars[charIndex].uuid.type == BLE_UUID_TYPE_UNKNOWN) {
charUUIDDiscoveryQueue.enqueue(charIndex);
characteristics[charIndex].setup(gattc,
connHandle,
response->chars[charIndex].char_props,
response->chars[charIndex].handle_decl,
response->chars[charIndex].handle_value);
} else {
characteristics[charIndex].setup(gattc,
connHandle,
response->chars[charIndex].uuid.uuid,
response->chars[charIndex].char_props,
response->chars[charIndex].handle_decl,
response->chars[charIndex].handle_value);
}
}
/* Trigger discovery of char UUID if necessary. */
if (charUUIDDiscoveryQueue.getCount()) {
charUUIDDiscoveryQueue.triggerFirst();
}
}
void
nRF5xServiceDiscovery::progressCharacteristicDiscovery(void)
{
if (state != CHARACTERISTIC_DISCOVERY_ACTIVE) {
return;
}
if ((discoveredCharacteristic != nRF5xDiscoveredCharacteristic()) && (numCharacteristics > 0)) {
discoveredCharacteristic.setLastHandle(characteristics[0].getDeclHandle() - 1);
if ((matchingCharacteristicUUID == UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)) ||
((matchingCharacteristicUUID == discoveredCharacteristic.getUUID()) &&
(matchingServiceUUID != UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)))) {
if (characteristicCallback) {
characteristicCallback(&discoveredCharacteristic);
}
}
}
for (uint8_t i = 0; i < numCharacteristics; ++i) {
if (state != CHARACTERISTIC_DISCOVERY_ACTIVE) {
return;
}
if (i == (numCharacteristics - 1)) {
discoveredCharacteristic = characteristics[i];
break;
} else {
characteristics[i].setLastHandle(characteristics[i + 1].getDeclHandle() - 1);
}
if ((matchingCharacteristicUUID == UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)) ||
((matchingCharacteristicUUID == characteristics[i].getUUID()) &&
(matchingServiceUUID != UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)))) {
if (characteristicCallback) {
characteristicCallback(&characteristics[i]);
}
}
}
if (state != CHARACTERISTIC_DISCOVERY_ACTIVE) {
return;
}
Gap::Handle_t startHandle = (numCharacteristics > 0) ? characteristics[numCharacteristics - 1].getValueHandle() + 1 : SRV_DISC_END_HANDLE;
Gap::Handle_t endHandle = services[serviceIndex].getEndHandle();
resetDiscoveredCharacteristics(); /* Note: resetDiscoveredCharacteristics() must come after fetching start and end Handles. */
if (startHandle < endHandle) {
ble_gattc_handle_range_t handleRange = {
(uint16_t) startHandle,
(uint16_t) endHandle
};
if (sd_ble_gattc_characteristics_discover(connHandle, &handleRange) != NRF_SUCCESS) {
terminateCharacteristicDiscovery(BLE_ERROR_UNSPECIFIED);
}
} else {
terminateCharacteristicDiscovery(BLE_ERROR_NONE);
}
}
void
nRF5xServiceDiscovery::progressServiceDiscovery(void)
{
/* Iterate through the previously discovered services cached in services[]. */
while ((state == SERVICE_DISCOVERY_ACTIVE) && (serviceIndex < numServices)) {
if ((matchingServiceUUID == UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)) ||
(matchingServiceUUID == services[serviceIndex].getUUID())) {
if (serviceCallback && (matchingCharacteristicUUID == UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN))) {
serviceCallback(&services[serviceIndex]);
}
if ((state == SERVICE_DISCOVERY_ACTIVE) && characteristicCallback) {
launchCharacteristicDiscovery(connHandle, services[serviceIndex].getStartHandle(), services[serviceIndex].getEndHandle());
} else {
serviceIndex++;
}
} else {
serviceIndex++;
}
}
/* Relaunch discovery of new services beyond the last entry cached in services[]. */
if ((state == SERVICE_DISCOVERY_ACTIVE) && (numServices > 0) && (serviceIndex > 0)) {
/* Determine the ending handle of the last cached service. */
Gap::Handle_t endHandle = services[serviceIndex - 1].getEndHandle();
resetDiscoveredServices(); /* Note: resetDiscoveredServices() must come after fetching endHandle. */
if (endHandle == SRV_DISC_END_HANDLE) {
terminateServiceDiscovery();
} else {
// the next service is located after the last handle discovered
// Launch a new discovery from [endHandle + 1 : 0xFFFF]
if (sd_ble_gattc_primary_services_discover(connHandle, endHandle + 1, NULL) != NRF_SUCCESS) {
terminateServiceDiscovery();
}
}
}
}
void
nRF5xServiceDiscovery::ServiceUUIDDiscoveryQueue::triggerFirst(void)
{
while (numIndices) { /* loop until a call to char_value_by_uuid_read() succeeds or we run out of pending indices. */
parentDiscoveryObject->state = DISCOVER_SERVICE_UUIDS;
unsigned serviceIndex = getFirst();
ble_uuid_t uuid = {
.uuid = BLE_UUID_SERVICE_PRIMARY,
.type = BLE_UUID_TYPE_BLE,
};
ble_gattc_handle_range_t handleRange = {
.start_handle = parentDiscoveryObject->services[serviceIndex].getStartHandle(),
.end_handle = parentDiscoveryObject->services[serviceIndex].getEndHandle(),
};
if (sd_ble_gattc_char_value_by_uuid_read(parentDiscoveryObject->connHandle, &uuid, &handleRange) == NRF_SUCCESS) {
return;
}
/* Skip this service if we fail to launch a read for its service-declaration
* attribute. Its UUID will remain INVALID, and it may not match any filters. */
dequeue();
}
/* Switch back to service discovery upon exhausting the service-indices pending UUID discovery. */
if (parentDiscoveryObject->state == DISCOVER_SERVICE_UUIDS) {
parentDiscoveryObject->state = SERVICE_DISCOVERY_ACTIVE;
}
}
void
nRF5xServiceDiscovery::CharUUIDDiscoveryQueue::triggerFirst(void)
{
while (numIndices) { /* loop until a call to char_value_by_uuid_read() succeeds or we run out of pending indices. */
parentDiscoveryObject->state = DISCOVER_CHARACTERISTIC_UUIDS;
unsigned charIndex = getFirst();
ble_uuid_t uuid = {
.uuid = BLE_UUID_CHARACTERISTIC,
.type = BLE_UUID_TYPE_BLE,
};
ble_gattc_handle_range_t handleRange = { };
handleRange.start_handle = parentDiscoveryObject->characteristics[charIndex].getDeclHandle();
handleRange.end_handle = parentDiscoveryObject->characteristics[charIndex].getDeclHandle() + 1;
if (sd_ble_gattc_char_value_by_uuid_read(parentDiscoveryObject->connHandle, &uuid, &handleRange) == NRF_SUCCESS) {
return;
}
/* Skip this service if we fail to launch a read for its service-declaration
* attribute. Its UUID will remain INVALID, and it may not match any filters. */
dequeue();
}
/* Switch back to service discovery upon exhausting the service-indices pending UUID discovery. */
if (parentDiscoveryObject->state == DISCOVER_CHARACTERISTIC_UUIDS) {
parentDiscoveryObject->state = CHARACTERISTIC_DISCOVERY_ACTIVE;
}
}
void
nRF5xServiceDiscovery::processDiscoverUUIDResponse(const ble_gattc_evt_t *p_gattc_evt)
{
const ble_gattc_evt_char_val_by_uuid_read_rsp_t * response = &p_gattc_evt->params.char_val_by_uuid_read_rsp;
if (state == DISCOVER_SERVICE_UUIDS) {
if ((response->count == 1) && (response->value_len == UUID::LENGTH_OF_LONG_UUID)) {
UUID::LongUUIDBytes_t uuid;
#if (NRF_SD_BLE_API_VERSION >= 3)
ble_gattc_handle_value_t iter;
memset(&iter, 0, sizeof(ble_gattc_handle_value_t));
(void) sd_ble_gattc_evt_char_val_by_uuid_read_rsp_iter(const_cast<ble_gattc_evt_t*>(p_gattc_evt), &iter);
memcpy(uuid, iter.p_value, UUID::LENGTH_OF_LONG_UUID);
#else
memcpy(uuid, &(response->handle_value[0].p_value[0]), UUID::LENGTH_OF_LONG_UUID);
#endif
unsigned serviceIndex = serviceUUIDDiscoveryQueue.dequeue();
services[serviceIndex].setupLongUUID(uuid, UUID::LSB);
serviceUUIDDiscoveryQueue.triggerFirst();
} else {
serviceUUIDDiscoveryQueue.dequeue();
}
} else if (state == DISCOVER_CHARACTERISTIC_UUIDS) {
if ((response->count == 1) && (response->value_len == UUID::LENGTH_OF_LONG_UUID + 1 /* props */ + 2 /* value handle */)) {
UUID::LongUUIDBytes_t uuid;
#if (NRF_SD_BLE_API_VERSION >= 3)
ble_gattc_handle_value_t iter;
memset(&iter, 0, sizeof(ble_gattc_handle_value_t));
(void) sd_ble_gattc_evt_char_val_by_uuid_read_rsp_iter(const_cast<ble_gattc_evt_t*>(p_gattc_evt), &iter);
memcpy(uuid, &(iter.p_value[3]), UUID::LENGTH_OF_LONG_UUID);
#else
memcpy(uuid, &(response->handle_value[0].p_value[3]), UUID::LENGTH_OF_LONG_UUID);
#endif
unsigned charIndex = charUUIDDiscoveryQueue.dequeue();
characteristics[charIndex].setupLongUUID(uuid, UUID::LSB);
charUUIDDiscoveryQueue.triggerFirst();
} else {
charUUIDDiscoveryQueue.dequeue();
}
}
}

View File

@ -1,366 +0,0 @@
/* 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_SERVICE_DISCOVERY_H__
#define __NRF_SERVICE_DISCOVERY_H__
#include "ble/ServiceDiscovery.h"
#include "ble/DiscoveredService.h"
#include "nRF5xDiscoveredCharacteristic.h"
#include "headers/nrf_ble.h"
#include "headers/nrf_ble_gattc.h"
class nRF5xGattClient; /* forward declaration */
class nRF5xServiceDiscovery : public ServiceDiscovery
{
public:
static const uint16_t SRV_DISC_START_HANDLE = 0x0001; /**< The start handle value used during service discovery. */
static const uint16_t SRV_DISC_END_HANDLE = 0xFFFF; /**< The end handle value used during service discovery. */
public:
static const unsigned BLE_DB_DISCOVERY_MAX_SRV = 4; /**< Maximum number of services we can retain information for after a single discovery. */
static const unsigned BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV = 4; /**< Maximum number of characteristics per service we can retain information for. */
public:
nRF5xServiceDiscovery(nRF5xGattClient *gattcIn) :
gattc(gattcIn),
serviceIndex(0),
numServices(0),
numCharacteristics(0),
state(INACTIVE),
services(),
characteristics(),
serviceUUIDDiscoveryQueue(this),
charUUIDDiscoveryQueue(this),
onTerminationCallback(NULL) {
/* empty */
}
virtual ble_error_t launch(Gap::Handle_t connectionHandle,
ServiceDiscovery::ServiceCallback_t sc,
ServiceDiscovery::CharacteristicCallback_t cc,
const UUID &matchingServiceUUIDIn,
const UUID &matchingCharacteristicUUIDIn)
{
if (isActive()) {
return BLE_ERROR_INVALID_STATE;
}
serviceCallback = sc;
characteristicCallback = cc;
matchingServiceUUID = matchingServiceUUIDIn;
matchingCharacteristicUUID = matchingCharacteristicUUIDIn;
serviceDiscoveryStarted(connectionHandle);
uint32_t rc;
if ((rc = sd_ble_gattc_primary_services_discover(connectionHandle, SRV_DISC_START_HANDLE, NULL)) != NRF_SUCCESS) {
terminate();
switch (rc) {
case NRF_ERROR_INVALID_PARAM:
case BLE_ERROR_INVALID_CONN_HANDLE:
return BLE_ERROR_INVALID_PARAM;
case NRF_ERROR_BUSY:
return BLE_STACK_BUSY;
default:
case NRF_ERROR_INVALID_STATE:
return BLE_ERROR_INVALID_STATE;
}
}
return BLE_ERROR_NONE;
}
virtual bool isActive(void) const {
return state != INACTIVE;
}
virtual void terminate(void) {
terminateServiceDiscovery();
}
void terminate(Gap::Handle_t connectionHandle) {
if(connHandle == connectionHandle) {
terminate();
}
}
virtual void onTermination(ServiceDiscovery::TerminationCallback_t callback) {
onTerminationCallback = callback;
}
/**
* @brief Clear nRF5xServiceDiscovery's state.
*
* @return
* BLE_ERROR_NONE if successful.
*/
virtual ble_error_t reset(void) {
/* Clear all state that is from the parent, including private members */
if (ServiceDiscovery::reset() != BLE_ERROR_NONE) {
return BLE_ERROR_INVALID_STATE;
}
/* Clear derived class members */
serviceIndex = 0;
numServices = 0;
numCharacteristics = 0;
state = INACTIVE;
serviceUUIDDiscoveryQueue.reset();
charUUIDDiscoveryQueue.reset();
onTerminationCallback = NULL;
return BLE_ERROR_NONE;
}
private:
ble_error_t launchCharacteristicDiscovery(Gap::Handle_t connectionHandle, Gap::Handle_t startHandle, Gap::Handle_t endHandle);
private:
void setupDiscoveredServices(const ble_gattc_evt_prim_srvc_disc_rsp_t *response);
void setupDiscoveredCharacteristics(const ble_gattc_evt_char_disc_rsp_t *response);
void triggerServiceUUIDDiscovery(void);
void processDiscoverUUIDResponse(const ble_gattc_evt_t *p_gattc_evt);
void removeFirstServiceNeedingUUIDDiscovery(void);
void terminateServiceDiscovery(void) {
discoveredCharacteristic = nRF5xDiscoveredCharacteristic();
bool wasActive = isActive();
state = INACTIVE;
if (wasActive && onTerminationCallback) {
onTerminationCallback(connHandle);
}
}
void terminateCharacteristicDiscovery(ble_error_t err) {
if (state == CHARACTERISTIC_DISCOVERY_ACTIVE) {
if(discoveredCharacteristic != nRF5xDiscoveredCharacteristic()) {
if(err == BLE_ERROR_NONE) {
// fullfill the last characteristic
discoveredCharacteristic.setLastHandle(services[serviceIndex].getEndHandle());
if ((matchingCharacteristicUUID == UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)) ||
((matchingCharacteristicUUID == discoveredCharacteristic.getUUID()) &&
(matchingServiceUUID != UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)))) {
if (characteristicCallback) {
characteristicCallback(&discoveredCharacteristic);
}
}
}
discoveredCharacteristic = nRF5xDiscoveredCharacteristic();
}
state = SERVICE_DISCOVERY_ACTIVE;
}
serviceIndex++; /* Progress service index to keep discovery alive. */
}
private:
void resetDiscoveredServices(void) {
numServices = 0;
serviceIndex = 0;
}
void resetDiscoveredCharacteristics(void) {
numCharacteristics = 0;
}
private:
void serviceDiscoveryStarted(Gap::Handle_t connectionHandle) {
connHandle = connectionHandle;
resetDiscoveredServices();
state = SERVICE_DISCOVERY_ACTIVE;
}
private:
void characteristicDiscoveryStarted(Gap::Handle_t connectionHandle) {
connHandle = connectionHandle;
resetDiscoveredCharacteristics();
state = CHARACTERISTIC_DISCOVERY_ACTIVE;
}
private:
/**
* A datatype to contain service-indices for which long UUIDs need to be
* discovered using read_val_by_uuid().
*/
class ServiceUUIDDiscoveryQueue {
public:
ServiceUUIDDiscoveryQueue(nRF5xServiceDiscovery *parent) :
numIndices(0),
serviceIndices(),
parentDiscoveryObject(parent) {
/* empty */
}
public:
void reset(void) {
numIndices = 0;
for (unsigned i = 0; i < BLE_DB_DISCOVERY_MAX_SRV; i++) {
serviceIndices[i] = INVALID_INDEX;
}
}
void enqueue(int serviceIndex) {
serviceIndices[numIndices++] = serviceIndex;
}
int dequeue(void) {
if (numIndices == 0) {
return INVALID_INDEX;
}
unsigned valueToReturn = serviceIndices[0];
numIndices--;
for (unsigned i = 0; i < numIndices; i++) {
serviceIndices[i] = serviceIndices[i + 1];
}
return valueToReturn;
}
unsigned getFirst(void) const {
return serviceIndices[0];
}
size_t getCount(void) const {
return numIndices;
}
/**
* Trigger UUID discovery for the first of the enqueued ServiceIndices.
*/
void triggerFirst(void);
private:
static const int INVALID_INDEX = -1;
private:
size_t numIndices;
int serviceIndices[BLE_DB_DISCOVERY_MAX_SRV];
nRF5xServiceDiscovery *parentDiscoveryObject;
};
friend class ServiceUUIDDiscoveryQueue;
/**
* A datatype to contain characteristic-indices for which long UUIDs need to
* be discovered using read_val_by_uuid().
*/
class CharUUIDDiscoveryQueue {
public:
CharUUIDDiscoveryQueue(nRF5xServiceDiscovery *parent) :
numIndices(0),
charIndices(),
parentDiscoveryObject(parent) {
/* empty */
}
public:
void reset(void) {
numIndices = 0;
for (unsigned i = 0; i < BLE_DB_DISCOVERY_MAX_SRV; i++) {
charIndices[i] = INVALID_INDEX;
}
}
void enqueue(int serviceIndex) {
charIndices[numIndices++] = serviceIndex;
}
int dequeue(void) {
if (numIndices == 0) {
return INVALID_INDEX;
}
unsigned valueToReturn = charIndices[0];
numIndices--;
for (unsigned i = 0; i < numIndices; i++) {
charIndices[i] = charIndices[i + 1];
}
return valueToReturn;
}
unsigned getFirst(void) const {
return charIndices[0];
}
size_t getCount(void) const {
return numIndices;
}
/**
* Trigger UUID discovery for the first of the enqueued charIndices.
*/
void triggerFirst(void);
private:
static const int INVALID_INDEX = -1;
private:
size_t numIndices;
int charIndices[BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV];
nRF5xServiceDiscovery *parentDiscoveryObject;
};
friend class CharUUIDDiscoveryQueue;
private:
friend void bleGattcEventHandler(const ble_evt_t *p_ble_evt);
void progressCharacteristicDiscovery(void);
void progressServiceDiscovery(void);
private:
nRF5xGattClient *gattc;
private:
uint8_t serviceIndex; /**< Index of the current service being discovered. This is intended for internal use during service discovery.*/
uint8_t numServices; /**< Number of services at the peers GATT database.*/
uint8_t numCharacteristics; /**< Number of characteristics within the service.*/
enum State_t {
INACTIVE,
SERVICE_DISCOVERY_ACTIVE,
CHARACTERISTIC_DISCOVERY_ACTIVE,
DISCOVER_SERVICE_UUIDS,
DISCOVER_CHARACTERISTIC_UUIDS,
} state;
DiscoveredService services[BLE_DB_DISCOVERY_MAX_SRV]; /**< Information related to the current service being discovered.
* This is intended for internal use during service discovery. */
nRF5xDiscoveredCharacteristic characteristics[BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV];
ServiceUUIDDiscoveryQueue serviceUUIDDiscoveryQueue;
CharUUIDDiscoveryQueue charUUIDDiscoveryQueue;
TerminationCallback_t onTerminationCallback;
/*
* The currently discovered characteristic. Discovery of a characteristic
* is a two phase process.
* First, declaration handle is fetched, it provide the UUID, the value handle and
* the properties of a characteristic.
* Second, the next declaration handle is fetched, with its declaration handle, it is
* possible to compute the last handle of the discovered characteristic and fill the
* missing part of the object.
* If there is no remaining characteristic to discover, the last handle of the
* discovered characteristic will be set to the last handle of its enclosing service.
*/
nRF5xDiscoveredCharacteristic discoveredCharacteristic;
};
#endif /*__NRF_SERVICE_DISCOVERY_H__*/