Release 0.4.1

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

This is a minor release.

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

* Hide duplicate symbol definitions in ble_types.h. These have already been
  brought into ble/blecommon.h.

* Replace DiscoveredCharacteristic::setupOnDataRead() with
  GattClient::onDataRead().

* Implement new API GattServer::areUpdatesEnabled() to determine notification
  status for a characteristic.

Bugfixes
~~~~~~~~

* fix for #20: GattServer::addService() incorrectly initialized value
  attribute-handles. These were getting confused with characteristicIndex.
  Event handlers also return attribute handles correctly now; they were also
  previously returning characteristic indices.
master
Rohit Grover 2015-07-02 08:38:51 +01:00
commit 928a5e634d
4 changed files with 190 additions and 117 deletions

View File

@ -54,20 +54,18 @@ void bleGattcEventHandler(const ble_evt_t *p_ble_evt)
}
break;
case BLE_GATTC_EVT_READ_RSP:
if (DiscoveredCharacteristic::onDataReadCallback != NULL) {
case BLE_GATTC_EVT_READ_RSP: {
GattReadCallbackParams response = {
.handle = p_ble_evt->evt.gattc_evt.params.read_rsp.handle,
.offset = p_ble_evt->evt.gattc_evt.params.read_rsp.offset,
.len = p_ble_evt->evt.gattc_evt.params.read_rsp.len,
.data = p_ble_evt->evt.gattc_evt.params.read_rsp.data,
};
DiscoveredCharacteristic::onDataReadCallback(&response);
nRF51GattClient::getInstance().processReadResponse(&response);
}
break;
case BLE_GATTC_EVT_WRITE_RSP:
if (DiscoveredCharacteristic::onDataWriteCallback != NULL) {
case BLE_GATTC_EVT_WRITE_RSP: {
GattWriteCallbackParams response = {
.handle = p_ble_evt->evt.gattc_evt.params.write_rsp.handle,
.writeOp = (GattWriteCallbackParams::WriteOp_t)(p_ble_evt->evt.gattc_evt.params.write_rsp.write_op),
@ -75,7 +73,7 @@ void bleGattcEventHandler(const ble_evt_t *p_ble_evt)
.len = p_ble_evt->evt.gattc_evt.params.write_rsp.len,
.data = p_ble_evt->evt.gattc_evt.params.write_rsp.data,
};
DiscoveredCharacteristic::onDataWriteCallback(&response);
nRF51GattClient::getInstance().processWriteResponse(&response);
}
break;
}

View File

@ -103,10 +103,9 @@ ble_error_t nRF51GattServer::addService(GattService &service)
BLE_ERROR_PARAM_OUT_OF_RANGE );
/* Update the characteristic handle */
uint16_t charHandle = characteristicCount;
p_characteristics[characteristicCount++] = p_char;
p_char->getValueAttribute().setHandle(charHandle);
p_characteristics[characteristicCount] = p_char;
p_char->getValueAttribute().setHandle(nrfCharacteristicHandles[characteristicCount].value_handle);
characteristicCount++;
/* Add optional descriptors if any */
/* ToDo: Make sure we don't overflow the array */
@ -128,9 +127,8 @@ ble_error_t nRF51GattServer::addService(GattService &service)
&nrfDescriptorHandles[descriptorCount]),
BLE_ERROR_PARAM_OUT_OF_RANGE);
uint16_t descHandle = descriptorCount;
p_descriptors[descriptorCount++] = p_desc;
p_desc->setHandle(descHandle);
p_desc->setHandle(nrfDescriptorHandles[descriptorCount]);
}
}
@ -173,7 +171,7 @@ ble_error_t nRF51GattServer::read(Gap::Handle_t connectionHandle, GattAttribute:
};
ASSERT( ERROR_NONE ==
sd_ble_gatts_value_get(connectionHandle, nrfCharacteristicHandles[attributeHandle].value_handle, &value),
sd_ble_gatts_value_get(connectionHandle, attributeHandle, &value),
BLE_ERROR_PARAM_OUT_OF_RANGE);
*lengthP = value.len;
@ -218,19 +216,21 @@ ble_error_t nRF51GattServer::write(Gap::Handle_t connectionHandle, GattAttribute
if (localOnly) {
/* Only update locally regardless of notify/indicate */
ASSERT_INT( ERROR_NONE,
sd_ble_gatts_value_set(connectionHandle, nrfCharacteristicHandles[attributeHandle].value_handle, &value),
sd_ble_gatts_value_set(connectionHandle, attributeHandle, &value),
BLE_ERROR_PARAM_OUT_OF_RANGE );
return BLE_ERROR_NONE;
}
if ((p_characteristics[attributeHandle]->getProperties() & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)) &&
int characteristicIndex = resolveValueHandleToCharIndex(attributeHandle);
if ((characteristicIndex != -1) &&
(p_characteristics[characteristicIndex]->getProperties() & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)) &&
(gapConnectionHandle != connectionHandle)) {
/* HVX update for the characteristic value */
ble_gatts_hvx_params_t hvx_params;
hvx_params.handle = nrfCharacteristicHandles[attributeHandle].value_handle;
hvx_params.handle = attributeHandle;
hvx_params.type =
(p_characteristics[attributeHandle]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) ? BLE_GATT_HVX_NOTIFICATION : BLE_GATT_HVX_INDICATION;
(p_characteristics[characteristicIndex]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) ? BLE_GATT_HVX_NOTIFICATION : BLE_GATT_HVX_INDICATION;
hvx_params.offset = 0;
hvx_params.p_data = const_cast<uint8_t *>(buffer);
hvx_params.p_len = &len;
@ -240,7 +240,7 @@ ble_error_t nRF51GattServer::write(Gap::Handle_t connectionHandle, GattAttribute
/* ERROR_INVALID_STATE, ERROR_BUSY, ERROR_GATTS_SYS_ATTR_MISSING and ERROR_NO_TX_BUFFERS the ATT table has been updated. */
if ((error != ERROR_NONE) && (error != ERROR_INVALID_STATE) && (error != ERROR_BLE_NO_TX_BUFFERS) && (error != ERROR_BUSY) && (error != ERROR_BLEGATTS_SYS_ATTR_MISSING)) {
ASSERT_INT( ERROR_NONE,
sd_ble_gatts_value_set(connectionHandle, nrfCharacteristicHandles[attributeHandle].value_handle, &value),
sd_ble_gatts_value_set(connectionHandle, attributeHandle, &value),
BLE_ERROR_PARAM_OUT_OF_RANGE );
}
@ -252,13 +252,46 @@ ble_error_t nRF51GattServer::write(Gap::Handle_t connectionHandle, GattAttribute
}
} else {
ASSERT_INT( ERROR_NONE,
sd_ble_gatts_value_set(connectionHandle, nrfCharacteristicHandles[attributeHandle].value_handle, &value),
sd_ble_gatts_value_set(connectionHandle, attributeHandle, &value),
BLE_ERROR_PARAM_OUT_OF_RANGE );
}
return returnValue;
}
ble_error_t nRF51GattServer::areUpdatesEnabled(const GattCharacteristic &characteristic, bool *enabledP)
{
/* Forward the call with the default connection handle. */
return areUpdatesEnabled(nRF51Gap::getInstance().getConnectionHandle(), characteristic, enabledP);
}
ble_error_t nRF51GattServer::areUpdatesEnabled(Gap::Handle_t connectionHandle, const GattCharacteristic &characteristic, bool *enabledP)
{
int characteristicIndex = resolveValueHandleToCharIndex(characteristic.getValueHandle());
if (characteristicIndex == -1) {
return BLE_ERROR_INVALID_PARAM;
}
/* Read the cccd value from the GATT server. */
GattAttribute::Handle_t cccdHandle = nrfCharacteristicHandles[characteristicIndex].cccd_handle;
uint16_t cccdValue;
uint16_t length = sizeof(cccdValue);
ble_error_t rc = read(connectionHandle, cccdHandle, reinterpret_cast<uint8_t *>(&cccdValue), &length);
if (rc != BLE_ERROR_NONE) {
return rc;
}
if (length != sizeof(cccdValue)) {
return BLE_ERROR_INVALID_STATE;
}
/* Check for NOTFICATION or INDICATION in CCCD. */
if ((cccdValue & BLE_GATT_HVX_NOTIFICATION) || (cccdValue & BLE_GATT_HVX_INDICATION)) {
*enabledP = true;
}
return BLE_ERROR_NONE;
}
/**************************************************************************/
/*!
@brief Callback handler for events getting pushed up from the SD
@ -271,30 +304,32 @@ void nRF51GattServer::hwCallback(ble_evt_t *p_ble_evt)
const ble_gatts_evt_t *gattsEventP = &p_ble_evt->evt.gatts_evt;
switch (p_ble_evt->header.evt_id) {
case BLE_GATTS_EVT_WRITE:
/* There are 2 use case here: Values being updated & CCCD (indicate/notify) enabled */
case BLE_GATTS_EVT_WRITE: {
/* There are 2 use case here: Values being updated & CCCD (indicate/notify) enabled */
/* 1.) Handle CCCD changes */
handle_value = gattsEventP->params.write.handle;
int characteristicIndex = resolveCCCDHandleToCharIndex(handle_value);
if ((characteristicIndex != -1) &&
(p_characteristics[characteristicIndex]->getProperties() &
(GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY))) {
/* 1.) Handle CCCD changes */
handle_value = gattsEventP->params.write.handle;
for (uint8_t i = 0; i < characteristicCount; i++) {
if ((p_characteristics[i]->getProperties() & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)) &&
(nrfCharacteristicHandles[i].cccd_handle == handle_value)) {
uint16_t cccd_value = (gattsEventP->params.write.data[1] << 8) | gattsEventP->params.write.data[0]; /* Little Endian but M0 may be mis-aligned */
if (((p_characteristics[i]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE) && (cccd_value & BLE_GATT_HVX_INDICATION)) ||
((p_characteristics[i]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) && (cccd_value & BLE_GATT_HVX_NOTIFICATION))) {
if (((p_characteristics[characteristicIndex]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE) && (cccd_value & BLE_GATT_HVX_INDICATION)) ||
((p_characteristics[characteristicIndex]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) && (cccd_value & BLE_GATT_HVX_NOTIFICATION))) {
eventType = GattServerEvents::GATT_EVENT_UPDATES_ENABLED;
} else {
eventType = GattServerEvents::GATT_EVENT_UPDATES_DISABLED;
}
handleEvent(eventType, handle_value);
handleEvent(eventType, p_characteristics[characteristicIndex]->getValueHandle());
return;
}
}
/* 2.) Changes to the characteristic value will be handled with other events below */
eventType = GattServerEvents::GATT_EVENT_DATA_WRITTEN;
/* 2.) Changes to the characteristic value will be handled with other events below */
eventType = GattServerEvents::GATT_EVENT_DATA_WRITTEN;
}
break;
case BLE_GATTS_EVT_HVC:
@ -331,91 +366,92 @@ void nRF51GattServer::hwCallback(ble_evt_t *p_ble_evt)
return;
}
int characteristicIndex = resolveValueHandleToCharIndex(handle_value);
if (characteristicIndex == -1) {
return;
}
/* Find index (charHandle) in the pool */
for (uint8_t i = 0; i < characteristicCount; i++) {
if (nrfCharacteristicHandles[i].value_handle == handle_value) {
switch (eventType) {
case GattServerEvents::GATT_EVENT_DATA_WRITTEN: {
GattWriteCallbackParams cbParams = {
.handle = i,
.writeOp = static_cast<GattWriteCallbackParams::WriteOp_t>(gattsEventP->params.write.op),
.offset = gattsEventP->params.write.offset,
.len = gattsEventP->params.write.len,
.data = gattsEventP->params.write.data
};
handleDataWrittenEvent(&cbParams);
break;
}
case GattServerEvents::GATT_EVENT_WRITE_AUTHORIZATION_REQ: {
GattWriteAuthCallbackParams cbParams = {
.handle = i,
.offset = gattsEventP->params.authorize_request.request.write.offset,
.len = gattsEventP->params.authorize_request.request.write.len,
.data = gattsEventP->params.authorize_request.request.write.data,
};
ble_gatts_rw_authorize_reply_params_t reply = {
.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE,
.params = {
.write = {
.gatt_status = p_characteristics[i]->authorizeWrite(&cbParams)
}
}
};
sd_ble_gatts_rw_authorize_reply(gattsEventP->conn_handle, &reply);
/*
* If write-authorization is enabled for a characteristic,
* AUTHORIZATION_REQ event (if replied with true) is *not*
* followed by another DATA_WRITTEN event; so we still need
* to invoke handleDataWritten(), much the same as we would
* have done if write-authorization had not been enabled.
*/
if (reply.params.write.gatt_status == BLE_GATT_STATUS_SUCCESS) {
GattWriteCallbackParams cbParams = {
.handle = i,
.writeOp = static_cast<GattWriteCallbackParams::WriteOp_t>(gattsEventP->params.authorize_request.request.write.op),
.offset = gattsEventP->params.authorize_request.request.write.offset,
.len = gattsEventP->params.authorize_request.request.write.len,
.data = gattsEventP->params.authorize_request.request.write.data,
};
handleDataWrittenEvent(&cbParams);
}
break;
}
case GattServerEvents::GATT_EVENT_READ_AUTHORIZATION_REQ: {
GattReadAuthCallbackParams cbParams = {
.handle = i,
.offset = gattsEventP->params.authorize_request.request.read.offset,
.len = 0,
.data = NULL
};
ble_gatts_rw_authorize_reply_params_t reply = {
.type = BLE_GATTS_AUTHORIZE_TYPE_READ,
.params = {
.read = {
.gatt_status = p_characteristics[i]->authorizeRead(&cbParams)
}
}
};
if (cbParams.authorizationReply == BLE_GATT_STATUS_SUCCESS) {
if (cbParams.data != NULL) {
reply.params.read.update = 1;
reply.params.read.offset = cbParams.offset;
reply.params.read.len = cbParams.len;
reply.params.read.p_data = cbParams.data;
}
}
sd_ble_gatts_rw_authorize_reply(gattsEventP->conn_handle, &reply);
break;
}
default:
handleEvent(eventType, handle_value);
break;
}
switch (eventType) {
case GattServerEvents::GATT_EVENT_DATA_WRITTEN: {
GattWriteCallbackParams cbParams = {
.handle = handle_value,
.writeOp = static_cast<GattWriteCallbackParams::WriteOp_t>(gattsEventP->params.write.op),
.offset = gattsEventP->params.write.offset,
.len = gattsEventP->params.write.len,
.data = gattsEventP->params.write.data
};
handleDataWrittenEvent(&cbParams);
break;
}
case GattServerEvents::GATT_EVENT_WRITE_AUTHORIZATION_REQ: {
GattWriteAuthCallbackParams cbParams = {
.handle = handle_value,
.offset = gattsEventP->params.authorize_request.request.write.offset,
.len = gattsEventP->params.authorize_request.request.write.len,
.data = gattsEventP->params.authorize_request.request.write.data,
};
ble_gatts_rw_authorize_reply_params_t reply = {
.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE,
.params = {
.write = {
.gatt_status = p_characteristics[characteristicIndex]->authorizeWrite(&cbParams)
}
}
};
sd_ble_gatts_rw_authorize_reply(gattsEventP->conn_handle, &reply);
/*
* If write-authorization is enabled for a characteristic,
* AUTHORIZATION_REQ event (if replied with true) is *not*
* followed by another DATA_WRITTEN event; so we still need
* to invoke handleDataWritten(), much the same as we would
* have done if write-authorization had not been enabled.
*/
if (reply.params.write.gatt_status == BLE_GATT_STATUS_SUCCESS) {
GattWriteCallbackParams cbParams = {
.handle = handle_value,
.writeOp = static_cast<GattWriteCallbackParams::WriteOp_t>(gattsEventP->params.authorize_request.request.write.op),
.offset = gattsEventP->params.authorize_request.request.write.offset,
.len = gattsEventP->params.authorize_request.request.write.len,
.data = gattsEventP->params.authorize_request.request.write.data,
};
handleDataWrittenEvent(&cbParams);
}
break;
}
case GattServerEvents::GATT_EVENT_READ_AUTHORIZATION_REQ: {
GattReadAuthCallbackParams cbParams = {
.handle = handle_value,
.offset = gattsEventP->params.authorize_request.request.read.offset,
.len = 0,
.data = NULL
};
ble_gatts_rw_authorize_reply_params_t reply = {
.type = BLE_GATTS_AUTHORIZE_TYPE_READ,
.params = {
.read = {
.gatt_status = p_characteristics[characteristicIndex]->authorizeRead(&cbParams)
}
}
};
if (cbParams.authorizationReply == BLE_GATT_STATUS_SUCCESS) {
if (cbParams.data != NULL) {
reply.params.read.update = 1;
reply.params.read.offset = cbParams.offset;
reply.params.read.len = cbParams.len;
reply.params.read.p_data = cbParams.data;
}
}
sd_ble_gatts_rw_authorize_reply(gattsEventP->conn_handle, &reply);
break;
}
default:
handleEvent(eventType, handle_value);
break;
}
}

View File

@ -35,6 +35,8 @@ public:
virtual ble_error_t read(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP);
virtual ble_error_t write(GattAttribute::Handle_t, const uint8_t[], uint16_t, bool localOnly = false);
virtual ble_error_t write(Gap::Handle_t connectionHandle, GattAttribute::Handle_t, const uint8_t[], uint16_t, bool localOnly = false);
virtual ble_error_t areUpdatesEnabled(const GattCharacteristic &characteristic, bool *enabledP);
virtual ble_error_t areUpdatesEnabled(Gap::Handle_t connectionHandle, const GattCharacteristic &characteristic, bool *enabledP);
/* nRF51 Functions */
void eventCallback(void);
@ -44,6 +46,40 @@ private:
const static unsigned BLE_TOTAL_CHARACTERISTICS = 20;
const static unsigned BLE_TOTAL_DESCRIPTORS = 8;
private:
/**
* resolve a value attribute to its owning characteristic.
* @param valueHandle the value handle to be resolved.
* @return characteristic index if a resolution is found, else -1.
*/
int resolveValueHandleToCharIndex(GattAttribute::Handle_t valueHandle) const {
unsigned charIndex;
for (charIndex = 0; charIndex < characteristicCount; charIndex++) {
if (nrfCharacteristicHandles[charIndex].value_handle == valueHandle) {
return charIndex;
}
}
return -1;
}
/**
* resolve a CCCD attribute handle to its owning characteristic.
* @param cccdHandle the CCCD handle to be resolved.
* @return characteristic index if a resolution is found, else -1.
*/
int resolveCCCDHandleToCharIndex(GattAttribute::Handle_t cccdHandle) const {
unsigned charIndex;
for (charIndex = 0; charIndex < characteristicCount; charIndex++) {
if (nrfCharacteristicHandles[charIndex].cccd_handle == cccdHandle) {
return charIndex;
}
}
return -1;
}
private:
GattCharacteristic *p_characteristics[BLE_TOTAL_CHARACTERISTICS];
ble_gatts_char_handles_t nrfCharacteristicHandles[BLE_TOTAL_CHARACTERISTICS];
GattAttribute *p_descriptors[BLE_TOTAL_DESCRIPTORS];

View File

@ -54,6 +54,7 @@
/** @} */
#if 0 /* The following have been duplicated in blecommon.h */
/** @defgroup BLE_UUID_VALUES Assigned Values for BLE UUIDs
* @{ */
/* Generic UUIDs, applicable to all services */
@ -79,6 +80,7 @@
#define BLE_UUID_GAP_CHARACTERISTIC_RECONN_ADDR 0x2A03 /**< Reconnection Address Characteristic. */
#define BLE_UUID_GAP_CHARACTERISTIC_PPCP 0x2A04 /**< Peripheral Preferred Connection Parameters Characteristic. */
/** @} */
#endif /* The following have been duplicated in blecommon.h */
/** @defgroup BLE_UUID_TYPES Types of UUID
@ -88,7 +90,7 @@
#define BLE_UUID_TYPE_VENDOR_BEGIN 0x02 /**< Vendor UUID types start at this index (128-bit). */
/** @} */
#if 0 /* The following have been duplicated in blecommon.h */
/** @defgroup BLE_APPEARANCES Bluetooth Appearance values
* @note Retrieved from http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.gap.appearance.xml
* @{ */
@ -142,6 +144,7 @@
#define BLE_APPEARANCE_OUTDOOR_SPORTS_ACT_LOC_POD 5187 /**< Location Pod (Outdoor Sports Activity subtype). */
#define BLE_APPEARANCE_OUTDOOR_SPORTS_ACT_LOC_AND_NAV_POD 5188 /**< Location and Navigation Pod (Outdoor Sports Activity subtype). */
/** @} */
#endif /* The following have been duplicated in blecommon.h */
/** @brief Set .type and .uuid fields of ble_uuid_struct to specified uuid value. */
#define BLE_UUID_BLE_ASSIGN(instance, value) do {\