one massive commit to integrate with GattClient.

DiscoveredCharacteristic still needs to setup gattc pointers.
This commit is contained in:
Rohit Grover 2015-06-04 14:44:44 +01:00
parent 093cc129bb
commit 5a4e62f169
7 changed files with 447 additions and 610 deletions

View file

@ -14,338 +14,13 @@
* limitations under the License.
*/
#include <stdio.h>
#include "blecommon.h"
#include "UUID.h"
#include "Gap.h"
#include "nrf_error.h"
#include "btle_discovery.h"
#include "ble_err.h"
static NordicServiceDiscovery sdSingleton;
DiscoveredCharacteristic::ReadCallback_t DiscoveredCharacteristic::onDataReadCallback = NULL;
ble_error_t
ServiceDiscovery::launch(Gap::Handle_t connectionHandle,
ServiceCallback_t sc,
CharacteristicCallback_t cc,
const UUID &matchingServiceUUIDIn,
const UUID &matchingCharacteristicUUIDIn)
{
if (isActive()) {
return BLE_ERROR_INVALID_STATE;
}
sdSingleton.serviceCallback = sc;
sdSingleton.characteristicCallback = cc;
sdSingleton.matchingServiceUUID = matchingServiceUUIDIn;
sdSingleton.matchingCharacteristicUUID = matchingCharacteristicUUIDIn;
sdSingleton.serviceDiscoveryStarted(connectionHandle);
uint32_t rc;
if ((rc = sd_ble_gattc_primary_services_discover(connectionHandle, NordicServiceDiscovery::SRV_DISC_START_HANDLE, NULL)) != NRF_SUCCESS) {
sdSingleton.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;
}
void
ServiceDiscovery::terminate(void)
{
sdSingleton.terminateServiceDiscovery();
}
bool
ServiceDiscovery::isActive(void)
{
return sdSingleton.isActive();
}
void ServiceDiscovery::onTermination(TerminationCallback_t callback) {
sdSingleton.setOnTermination(callback);
}
ble_error_t
NordicServiceDiscovery::launchCharacteristicDiscovery(Gap::Handle_t connectionHandle,
Gap::Handle_t startHandle,
Gap::Handle_t endHandle)
{
sdSingleton.characteristicDiscoveryStarted(connectionHandle);
ble_gattc_handle_range_t handleRange = {
.start_handle = startHandle,
.end_handle = endHandle
};
uint32_t rc;
if ((rc = sd_ble_gattc_characteristics_discover(connectionHandle, &handleRange)) != NRF_SUCCESS) {
sdSingleton.terminateCharacteristicDiscovery();
switch (rc) {
case BLE_ERROR_INVALID_CONN_HANDLE:
case NRF_ERROR_INVALID_ADDR:
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;
}
void
NordicServiceDiscovery::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
NordicServiceDiscovery::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 = {
.start_handle = parentDiscoveryObject->characteristics[charIndex].getDeclHandle(),
.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
NordicServiceDiscovery::processDiscoverUUIDResponse(const ble_gattc_evt_char_val_by_uuid_read_rsp_t *response)
{
if (state == DISCOVER_SERVICE_UUIDS) {
if ((response->count == 1) && (response->value_len == UUID::LENGTH_OF_LONG_UUID)) {
UUID::LongUUIDBytes_t uuid;
/* Switch longUUID bytes to MSB */
for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) {
uuid[i] = response->handle_value[0].p_value[UUID::LENGTH_OF_LONG_UUID - 1 - i];
}
unsigned serviceIndex = serviceUUIDDiscoveryQueue.dequeue();
services[serviceIndex].setupLongUUID(uuid);
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;
/* Switch longUUID bytes to MSB */
for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) {
uuid[i] = response->handle_value[0].p_value[3 + UUID::LENGTH_OF_LONG_UUID - 1 - i];
}
unsigned charIndex = charUUIDDiscoveryQueue.dequeue();
characteristics[charIndex].setupLongUUID(uuid);
charUUIDDiscoveryQueue.triggerFirst();
} else {
charUUIDDiscoveryQueue.dequeue();
}
}
}
void
NordicServiceDiscovery::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 serviceIndex = 0; serviceIndex < numServices; serviceIndex++) {
if (response->services[serviceIndex].uuid.type == BLE_UUID_TYPE_UNKNOWN) {
serviceUUIDDiscoveryQueue.enqueue(serviceIndex);
services[serviceIndex].setup(response->services[serviceIndex].handle_range.start_handle,
response->services[serviceIndex].handle_range.end_handle);
} else {
services[serviceIndex].setup(response->services[serviceIndex].uuid.uuid,
response->services[serviceIndex].handle_range.start_handle,
response->services[serviceIndex].handle_range.end_handle);
}
}
/* Trigger discovery of service UUID if necessary. */
if (serviceUUIDDiscoveryQueue.getCount()) {
serviceUUIDDiscoveryQueue.triggerFirst();
}
}
void
NordicServiceDiscovery::setupDiscoveredCharacteristics(const ble_gattc_evt_char_disc_rsp_t *response)
{
characteristicIndex = 0;
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(NULL, /* gattc FIX THIS */
connHandle,
response->chars[charIndex].char_props,
response->chars[charIndex].handle_decl,
response->chars[charIndex].handle_value);
} else {
characteristics[charIndex].setup(NULL, /* gattc FIX THIS */
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
NordicServiceDiscovery::progressCharacteristicDiscovery(void)
{
/* Iterate through the previously discovered characteristics cached in characteristics[]. */
while ((state == CHARACTERISTIC_DISCOVERY_ACTIVE) && (characteristicIndex < numCharacteristics)) {
if ((matchingCharacteristicUUID == UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)) ||
((matchingCharacteristicUUID == characteristics[characteristicIndex].getShortUUID()) &&
(matchingServiceUUID != UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)))) {
if (characteristicCallback) {
characteristicCallback(&characteristics[characteristicIndex]);
}
}
characteristicIndex++;
}
/* Relaunch discovery of new characteristics beyond the last entry cached in characteristics[]. */
if (state == CHARACTERISTIC_DISCOVERY_ACTIVE) {
/* Determine the ending handle of the last cached characteristic. */
Gap::Handle_t startHandle = characteristics[characteristicIndex - 1].getValueHandle() + 1;
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 = {
.start_handle = startHandle,
.end_handle = endHandle
};
if (sd_ble_gattc_characteristics_discover(connHandle, &handleRange) != NRF_SUCCESS) {
terminateCharacteristicDiscovery();
}
} else {
terminateCharacteristicDiscovery();
}
}
}
void
NordicServiceDiscovery::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().getShortUUID())) {
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 {
if (sd_ble_gattc_primary_services_discover(connHandle, endHandle, NULL) != NRF_SUCCESS) {
terminateServiceDiscovery();
}
}
}
}
#include "nRFServiceDiscovery.h"
#include "nRF51GattClient.h"
void bleGattcEventHandler(const ble_evt_t *p_ble_evt)
{
nRFServiceDiscovery &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) {
@ -395,3 +70,4 @@ void bleGattcEventHandler(const ble_evt_t *p_ble_evt)
sdSingleton.progressCharacteristicDiscovery();
sdSingleton.progressServiceDiscovery();
}

View file

@ -17,242 +17,6 @@
#ifndef _BTLE_DISCOVERY_H_
#define _BTLE_DISCOVERY_H_
#include "ble.h"
#include "ServiceDiscovery.h"
#include "nRFDiscoveredCharacteristic.h"
void bleGattcEventHandler(const ble_evt_t *p_ble_evt);
class NordicServiceDiscovery : 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:
NordicServiceDiscovery() : serviceIndex(0),
numServices(0),
characteristicIndex(0),
numCharacteristics(0),
state(INACTIVE),
services(),
characteristics(),
serviceUUIDDiscoveryQueue(this),
charUUIDDiscoveryQueue(this),
onTerminationCallback(NULL) {
/* empty */
}
public:
ble_error_t launchCharacteristicDiscovery(Gap::Handle_t connectionHandle, Gap::Handle_t startHandle, Gap::Handle_t endHandle);
public:
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_char_val_by_uuid_read_rsp_t *response);
void removeFirstServiceNeedingUUIDDiscovery(void);
void terminateServiceDiscovery(void) {
bool wasActive = isActive();
state = INACTIVE;
if (wasActive && onTerminationCallback) {
onTerminationCallback(connHandle);
}
}
void terminateCharacteristicDiscovery(void) {
if (state == CHARACTERISTIC_DISCOVERY_ACTIVE) {
state = SERVICE_DISCOVERY_ACTIVE;
}
serviceIndex++; /* Progress service index to keep discovery alive. */
}
bool isActive(void) const {
return state != INACTIVE;
}
void setOnTermination(TerminationCallback_t callback) {
onTerminationCallback = callback;
}
private:
void resetDiscoveredServices(void) {
numServices = 0;
serviceIndex = 0;
}
void resetDiscoveredCharacteristics(void) {
numCharacteristics = 0;
characteristicIndex = 0;
}
public:
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(NordicServiceDiscovery *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];
NordicServiceDiscovery *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(NordicServiceDiscovery *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];
NordicServiceDiscovery *parentDiscoveryObject;
};
friend class CharUUIDDiscoveryQueue;
private:
friend void bleGattcEventHandler(const ble_evt_t *p_ble_evt);
void progressCharacteristicDiscovery(void);
void progressServiceDiscovery(void);
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 characteristicIndex; /**< Index of the current characteristic being discovered. This is intended for internal use during service discovery.*/
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. */
nRFDiscoveredCharacteristic characteristics[BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV];
ServiceUUIDDiscoveryQueue serviceUUIDDiscoveryQueue;
CharUUIDDiscoveryQueue charUUIDDiscoveryQueue;
TerminationCallback_t onTerminationCallback;
};
#endif /*_BTLE_DISCOVERY_H_*/

View file

@ -16,7 +16,19 @@
#include "nRF51GattClient.h"
nRF51GattClient &nRF51GattClient::getInstance(void) {
static nRF51GattClient m_instance;
return m_instance;
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);
}

View file

@ -20,15 +20,85 @@
#include "GattClient.h"
#include "nRFServiceDiscovery.h"
#include "blecommon.h"
#include "ble_err.h"
#include "ble_gattc.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) {
@ -45,27 +115,17 @@ public:
}
}
#if 0
/* Functions that must be implemented from GattClient */
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);
/* nRF51 Functions */
void eventCallback(void);
void hwCallback(ble_evt_t *p_ble_evt);
private:
#endif
virtual ble_error_t write(GattClient::Command_t cmd, Gap::Handle_t connHandle, size_t length, const uint8_t *value) const {
return BLE_ERROR_NONE;
}
public:
nRF51GattClient() {
nRF51GattClient() { /* need a default constructor because we've added a private copy constructor */
/* empty */
}
friend void bleGattcEventHandler(const ble_evt_t *p_ble_evt);
private:
nRF51GattClient(const nRF51GattClient &);
const nRF51GattClient& operator=(const nRF51GattClient &);

View file

@ -17,6 +17,8 @@
#ifndef __NRF_DISCOVERED_CHARACTERISTIC_H__
#define __NRF_DISCOVERED_CHARACTERISTIC_H__
#include "ble_gatt.h"
class nRFDiscoveredCharacteristic : public DiscoveredCharacteristic {
public:
void setup(GattClient *gattcIn,

284
nRFServiceDiscovery.cpp Normal file
View file

@ -0,0 +1,284 @@
/* 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 "nRFServiceDiscovery.h"
ble_error_t
nRFServiceDiscovery::launchCharacteristicDiscovery(Gap::Handle_t connectionHandle,
Gap::Handle_t startHandle,
Gap::Handle_t endHandle)
{
characteristicDiscoveryStarted(connectionHandle);
ble_gattc_handle_range_t handleRange = {
.start_handle = startHandle,
.end_handle = endHandle
};
uint32_t rc;
if ((rc = sd_ble_gattc_characteristics_discover(connectionHandle, &handleRange)) != NRF_SUCCESS) {
terminateCharacteristicDiscovery();
switch (rc) {
case BLE_ERROR_INVALID_CONN_HANDLE:
case NRF_ERROR_INVALID_ADDR:
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;
}
void
nRFServiceDiscovery::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 serviceIndex = 0; serviceIndex < numServices; serviceIndex++) {
if (response->services[serviceIndex].uuid.type == BLE_UUID_TYPE_UNKNOWN) {
serviceUUIDDiscoveryQueue.enqueue(serviceIndex);
services[serviceIndex].setup(response->services[serviceIndex].handle_range.start_handle,
response->services[serviceIndex].handle_range.end_handle);
} else {
services[serviceIndex].setup(response->services[serviceIndex].uuid.uuid,
response->services[serviceIndex].handle_range.start_handle,
response->services[serviceIndex].handle_range.end_handle);
}
}
/* Trigger discovery of service UUID if necessary. */
if (serviceUUIDDiscoveryQueue.getCount()) {
serviceUUIDDiscoveryQueue.triggerFirst();
}
}
void
nRFServiceDiscovery::setupDiscoveredCharacteristics(const ble_gattc_evt_char_disc_rsp_t *response)
{
characteristicIndex = 0;
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(NULL, /* gattc FIX THIS */
connHandle,
response->chars[charIndex].char_props,
response->chars[charIndex].handle_decl,
response->chars[charIndex].handle_value);
} else {
characteristics[charIndex].setup(NULL, /* gattc FIX THIS */
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
nRFServiceDiscovery::progressCharacteristicDiscovery(void)
{
/* Iterate through the previously discovered characteristics cached in characteristics[]. */
while ((state == CHARACTERISTIC_DISCOVERY_ACTIVE) && (characteristicIndex < numCharacteristics)) {
if ((matchingCharacteristicUUID == UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)) ||
((matchingCharacteristicUUID == characteristics[characteristicIndex].getShortUUID()) &&
(matchingServiceUUID != UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)))) {
if (characteristicCallback) {
characteristicCallback(&characteristics[characteristicIndex]);
}
}
characteristicIndex++;
}
/* Relaunch discovery of new characteristics beyond the last entry cached in characteristics[]. */
if (state == CHARACTERISTIC_DISCOVERY_ACTIVE) {
/* Determine the ending handle of the last cached characteristic. */
Gap::Handle_t startHandle = characteristics[characteristicIndex - 1].getValueHandle() + 1;
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 = {
.start_handle = startHandle,
.end_handle = endHandle
};
if (sd_ble_gattc_characteristics_discover(connHandle, &handleRange) != NRF_SUCCESS) {
terminateCharacteristicDiscovery();
}
} else {
terminateCharacteristicDiscovery();
}
}
}
void
nRFServiceDiscovery::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().getShortUUID())) {
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 {
if (sd_ble_gattc_primary_services_discover(connHandle, endHandle, NULL) != NRF_SUCCESS) {
terminateServiceDiscovery();
}
}
}
}
void
nRFServiceDiscovery::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
nRFServiceDiscovery::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 = {
.start_handle = parentDiscoveryObject->characteristics[charIndex].getDeclHandle(),
.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
nRFServiceDiscovery::processDiscoverUUIDResponse(const ble_gattc_evt_char_val_by_uuid_read_rsp_t *response)
{
if (state == DISCOVER_SERVICE_UUIDS) {
if ((response->count == 1) && (response->value_len == UUID::LENGTH_OF_LONG_UUID)) {
UUID::LongUUIDBytes_t uuid;
/* Switch longUUID bytes to MSB */
for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) {
uuid[i] = response->handle_value[0].p_value[UUID::LENGTH_OF_LONG_UUID - 1 - i];
}
unsigned serviceIndex = serviceUUIDDiscoveryQueue.dequeue();
services[serviceIndex].setupLongUUID(uuid);
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;
/* Switch longUUID bytes to MSB */
for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) {
uuid[i] = response->handle_value[0].p_value[3 + UUID::LENGTH_OF_LONG_UUID - 1 - i];
}
unsigned charIndex = charUUIDDiscoveryQueue.dequeue();
characteristics[charIndex].setupLongUUID(uuid);
charUUIDDiscoveryQueue.triggerFirst();
} else {
charUUIDDiscoveryQueue.dequeue();
}
}
}

View file

@ -17,11 +17,11 @@
#ifndef __NRF_SERVICE_DISCOVERY_H__
#define __NRF_SERVICE_DISCOVERY_H__
#include "ble.h"
#include "ServiceDiscovery.h"
#include "nRFDiscoveredCharacteristic.h"
void bleGattcEventHandler(const ble_evt_t *p_ble_evt);
#include "ble.h"
#include "ble_gattc.h"
class nRFServiceDiscovery : public ServiceDiscovery
{
@ -35,22 +35,69 @@ public:
public:
nRFServiceDiscovery() : serviceIndex(0),
numServices(0),
characteristicIndex(0),
numCharacteristics(0),
state(INACTIVE),
services(),
characteristics(),
serviceUUIDDiscoveryQueue(this),
charUUIDDiscoveryQueue(this),
onTerminationCallback(NULL) {
numServices(0),
characteristicIndex(0),
numCharacteristics(0),
state(INACTIVE),
services(),
characteristics(),
serviceUUIDDiscoveryQueue(this),
charUUIDDiscoveryQueue(this),
onTerminationCallback(NULL) {
/* empty */
}
public:
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();
}
virtual void onTermination(ServiceDiscovery::TerminationCallback_t callback) {
onTerminationCallback = callback;
}
private:
ble_error_t launchCharacteristicDiscovery(Gap::Handle_t connectionHandle, Gap::Handle_t startHandle, Gap::Handle_t endHandle);
public:
private:
void setupDiscoveredServices(const ble_gattc_evt_prim_srvc_disc_rsp_t *response);
void setupDiscoveredCharacteristics(const ble_gattc_evt_char_disc_rsp_t *response);
@ -74,14 +121,6 @@ public:
serviceIndex++; /* Progress service index to keep discovery alive. */
}
bool isActive(void) const {
return state != INACTIVE;
}
void setOnTermination(TerminationCallback_t callback) {
onTerminationCallback = callback;
}
private:
void resetDiscoveredServices(void) {
numServices = 0;
@ -93,7 +132,7 @@ private:
characteristicIndex = 0;
}
public:
private:
void serviceDiscoveryStarted(Gap::Handle_t connectionHandle) {
connHandle = connectionHandle;
resetDiscoveredServices();