1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
|
/* 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 "nRF5xGattServer.h"
#ifdef YOTTA_CFG_MBED_OS
#include "mbed-drivers/mbed.h"
#else
#include "mbed.h"
#endif
#include "common/common.h"
#include "btle/custom/custom_helper.h"
#include "nRF5xn.h"
/**************************************************************************/
/*!
@brief Adds a new service to the GATT table on the peripheral
@returns ble_error_t
@retval BLE_ERROR_NONE
Everything executed properly
@section EXAMPLE
@code
@endcode
*/
/**************************************************************************/
ble_error_t nRF5xGattServer::addService(GattService &service)
{
/* ToDo: Make sure this service UUID doesn't already exist (?) */
/* ToDo: Basic validation */
/* Add the service to the nRF51 */
ble_uuid_t nordicUUID;
nordicUUID = custom_convert_to_nordic_uuid(service.getUUID());
uint16_t serviceHandle;
ASSERT( ERROR_NONE ==
sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
&nordicUUID,
&serviceHandle),
BLE_ERROR_PARAM_OUT_OF_RANGE );
service.setHandle(serviceHandle);
/* Add characteristics to the service */
for (uint8_t i = 0; i < service.getCharacteristicCount(); i++) {
if (characteristicCount >= BLE_TOTAL_CHARACTERISTICS) {
return BLE_ERROR_NO_MEM;
}
GattCharacteristic *p_char = service.getCharacteristic(i);
/* Skip any incompletely defined, read-only characteristics. */
if ((p_char->getValueAttribute().getValuePtr() == NULL) &&
(p_char->getValueAttribute().getLength() == 0) &&
(p_char->getProperties() == GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ)) {
continue;
}
nordicUUID = custom_convert_to_nordic_uuid(p_char->getValueAttribute().getUUID());
/* The user-description descriptor is a special case which needs to be
* handled at the time of adding the characteristic. The following block
* is meant to discover its presence. */
const uint8_t *userDescriptionDescriptorValuePtr = NULL;
uint16_t userDescriptionDescriptorValueLen = 0;
for (uint8_t j = 0; j < p_char->getDescriptorCount(); j++) {
GattAttribute *p_desc = p_char->getDescriptor(j);
if (p_desc->getUUID() == BLE_UUID_DESCRIPTOR_CHAR_USER_DESC) {
userDescriptionDescriptorValuePtr = p_desc->getValuePtr();
userDescriptionDescriptorValueLen = p_desc->getLength();
}
}
ASSERT ( ERROR_NONE ==
custom_add_in_characteristic(BLE_GATT_HANDLE_INVALID,
&nordicUUID,
p_char->getProperties(),
p_char->getRequiredSecurity(),
p_char->getValueAttribute().getValuePtr(),
p_char->getValueAttribute().getLength(),
p_char->getValueAttribute().getMaxLength(),
p_char->getValueAttribute().hasVariableLength(),
userDescriptionDescriptorValuePtr,
userDescriptionDescriptorValueLen,
p_char->isReadAuthorizationEnabled(),
p_char->isWriteAuthorizationEnabled(),
&nrfCharacteristicHandles[characteristicCount]),
BLE_ERROR_PARAM_OUT_OF_RANGE );
/* Update the characteristic handle */
p_characteristics[characteristicCount] = p_char;
p_char->getValueAttribute().setHandle(nrfCharacteristicHandles[characteristicCount].value_handle);
characteristicCount++;
/* Add optional descriptors if any */
for (uint8_t j = 0; j < p_char->getDescriptorCount(); j++) {
if (descriptorCount >= BLE_TOTAL_DESCRIPTORS) {
return BLE_ERROR_NO_MEM;
}
GattAttribute *p_desc = p_char->getDescriptor(j);
/* skip the user-description-descriptor here; this has already been handled when adding the characteristic (above). */
if (p_desc->getUUID() == BLE_UUID_DESCRIPTOR_CHAR_USER_DESC) {
continue;
}
nordicUUID = custom_convert_to_nordic_uuid(p_desc->getUUID());
ASSERT(ERROR_NONE ==
custom_add_in_descriptor(BLE_GATT_HANDLE_INVALID,
&nordicUUID,
p_desc->getValuePtr(),
p_desc->getLength(),
p_desc->getMaxLength(),
p_desc->hasVariableLength(),
&nrfDescriptorHandles[descriptorCount]),
BLE_ERROR_PARAM_OUT_OF_RANGE);
p_descriptors[descriptorCount++] = p_desc;
p_desc->setHandle(nrfDescriptorHandles[descriptorCount]);
}
}
serviceCount++;
return BLE_ERROR_NONE;
}
/**************************************************************************/
/*!
@brief Reads the value of a characteristic, based on the service
and characteristic index fields
@param[in] attributeHandle
The handle of the GattCharacteristic to read from
@param[in] buffer
Buffer to hold the the characteristic's value
(raw byte array in LSB format)
@param[in/out] len
input: Length in bytes to be read.
output: Total length of attribute value upon successful return.
@returns ble_error_t
@retval BLE_ERROR_NONE
Everything executed properly
*/
/**************************************************************************/
ble_error_t nRF5xGattServer::read(GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP)
{
return read(BLE_CONN_HANDLE_INVALID, attributeHandle, buffer, lengthP);
}
ble_error_t nRF5xGattServer::read(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP)
{
ble_gatts_value_t value = {
.len = *lengthP,
.offset = 0,
.p_value = buffer,
};
ASSERT( ERROR_NONE ==
sd_ble_gatts_value_get(connectionHandle, attributeHandle, &value),
BLE_ERROR_PARAM_OUT_OF_RANGE);
*lengthP = value.len;
return BLE_ERROR_NONE;
}
/**************************************************************************/
/*!
@brief Updates the value of a characteristic, based on the service
and characteristic index fields
@param[in] charHandle
The handle of the GattCharacteristic to write to
@param[in] buffer
Data to use when updating the characteristic's value
(raw byte array in LSB format)
@param[in] len
The number of bytes in buffer
@returns ble_error_t
@retval BLE_ERROR_NONE
Everything executed properly
*/
/**************************************************************************/
ble_error_t nRF5xGattServer::write(GattAttribute::Handle_t attributeHandle, const uint8_t buffer[], uint16_t len, bool localOnly)
{
return write(BLE_CONN_HANDLE_INVALID, attributeHandle, buffer, len, localOnly);
}
ble_error_t nRF5xGattServer::write(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, const uint8_t buffer[], uint16_t len, bool localOnly)
{
ble_error_t returnValue = BLE_ERROR_NONE;
ble_gatts_value_t value = {
.len = len,
.offset = 0,
.p_value = const_cast<uint8_t *>(buffer),
};
if (localOnly) {
/* Only update locally regardless of notify/indicate */
ASSERT_INT( ERROR_NONE,
sd_ble_gatts_value_set(connectionHandle, attributeHandle, &value),
BLE_ERROR_PARAM_OUT_OF_RANGE );
return BLE_ERROR_NONE;
}
int characteristicIndex = resolveValueHandleToCharIndex(attributeHandle);
if ((characteristicIndex != -1) &&
(p_characteristics[characteristicIndex]->getProperties() & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY))) {
/* HVX update for the characteristic value */
ble_gatts_hvx_params_t hvx_params;
hvx_params.handle = attributeHandle;
hvx_params.type =
(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;
if (connectionHandle == BLE_CONN_HANDLE_INVALID) { /* use the default connection handle if the caller hasn't specified a valid connectionHandle. */
nRF5xGap &gap = (nRF5xGap &) nRF5xn::Instance(BLE::DEFAULT_INSTANCE).getGap();
connectionHandle = gap.getConnectionHandle();
}
error_t error = (error_t) sd_ble_gatts_hvx(connectionHandle, &hvx_params);
if (error != ERROR_NONE) {
switch (error) {
case ERROR_BLE_NO_TX_BUFFERS: /* Notifications consume application buffers. The return value can be used for resending notifications. */
case ERROR_BUSY:
returnValue = BLE_STACK_BUSY;
break;
case ERROR_INVALID_STATE:
case ERROR_BLEGATTS_SYS_ATTR_MISSING:
returnValue = BLE_ERROR_INVALID_STATE;
break;
default :
ASSERT_INT( ERROR_NONE,
sd_ble_gatts_value_set(connectionHandle, attributeHandle, &value),
BLE_ERROR_PARAM_OUT_OF_RANGE );
/* Notifications consume application buffers. The return value can
* be used for resending notifications. */
returnValue = BLE_STACK_BUSY;
break;
}
}
} else {
returnValue = BLE_ERROR_INVALID_STATE; // if assert is not used
ASSERT_INT( ERROR_NONE,
sd_ble_gatts_value_set(connectionHandle, attributeHandle, &value),
BLE_ERROR_PARAM_OUT_OF_RANGE );
}
return returnValue;
}
/**
* Perform an explicit BLE notification of a given attribute.
*
* @param[in] attributeHandle
* Handle for the value attribute of the Characteristic.
* @param[in] value
* A pointer to a buffer holding the new value
* @param[in] size
* Size of the new value (in bytes).
*
* @return BLE_ERROR_NONE if we have successfully set the value of the attribute.
*/
ble_error_t nRF5xGattServer::notify(GattAttribute::Handle_t attributeHandle, const uint8_t buffer[], uint16_t len)
{
uint16_t gapConnectionHandle = ((nRF5xGap &)nRF5xn::Instance(BLE::DEFAULT_INSTANCE).getGap()).getConnectionHandle();
ble_gatts_hvx_params_t hvx_params;
hvx_params.handle = attributeHandle;
hvx_params.type = BLE_GATT_HVX_NOTIFICATION;
hvx_params.offset = 0;
hvx_params.p_data = const_cast<uint8_t *>(buffer);
hvx_params.p_len = &len;
error_t error = (error_t) sd_ble_gatts_hvx(gapConnectionHandle, &hvx_params);
if (error == ERROR_NONE)
return BLE_ERROR_NONE;
else
return BLE_STACK_BUSY;
}
ble_error_t nRF5xGattServer::areUpdatesEnabled(const GattCharacteristic &characteristic, bool *enabledP)
{
/* Forward the call with the default connection handle. */
nRF5xGap &gap = (nRF5xGap &) nRF5xn::Instance(BLE::DEFAULT_INSTANCE).getGap();
return areUpdatesEnabled(gap.getConnectionHandle(), characteristic, enabledP);
}
ble_error_t nRF5xGattServer::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 Clear nRF5xGattServer's state.
@returns ble_error_t
@retval BLE_ERROR_NONE
Everything executed properly
*/
/**************************************************************************/
ble_error_t nRF5xGattServer::reset(void)
{
/* Clear all state that is from the parent, including private members */
if (GattServer::reset() != BLE_ERROR_NONE) {
return BLE_ERROR_INVALID_STATE;
}
/* Clear derived class members */
memset(p_characteristics, 0, sizeof(p_characteristics));
memset(p_descriptors, 0, sizeof(p_descriptors));
memset(nrfCharacteristicHandles, 0, sizeof(ble_gatts_char_handles_t));
memset(nrfDescriptorHandles, 0, sizeof(nrfDescriptorHandles));
descriptorCount = 0;
return BLE_ERROR_NONE;
}
/**************************************************************************/
/*!
@brief Callback handler for events getting pushed up from the SD
*/
/**************************************************************************/
void nRF5xGattServer::hwCallback(ble_evt_t *p_ble_evt)
{
GattAttribute::Handle_t handle_value;
GattServerEvents::gattEvent_t eventType;
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 */
/* 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))) {
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[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, p_characteristics[characteristicIndex]->getValueHandle());
return;
}
/* 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:
/* Indication confirmation received */
eventType = GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED;
handle_value = gattsEventP->params.hvc.handle;
break;
case BLE_EVT_TX_COMPLETE: {
handleDataSentEvent(p_ble_evt->evt.common_evt.params.tx_complete.count);
return;
}
case BLE_GATTS_EVT_SYS_ATTR_MISSING:
case BLE_GAP_EVT_CONN_SEC_UPDATE:
{
GattSysAttrMissingCallbackParams cbParams = {gattsEventP->conn_handle};
handleSysAttrMissingEvent(&cbParams);
return;
}
case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST:
switch (gattsEventP->params.authorize_request.type) {
case BLE_GATTS_AUTHORIZE_TYPE_READ:
eventType = GattServerEvents::GATT_EVENT_READ_AUTHORIZATION_REQ;
handle_value = gattsEventP->params.authorize_request.request.read.handle;
break;
case BLE_GATTS_AUTHORIZE_TYPE_WRITE:
eventType = GattServerEvents::GATT_EVENT_WRITE_AUTHORIZATION_REQ;
handle_value = gattsEventP->params.authorize_request.request.write.handle;
break;
default:
return;
}
break;
default:
return;
}
int characteristicIndex = resolveValueHandleToCharIndex(handle_value);
if (characteristicIndex == -1) {
return;
}
/* Find index (charHandle) in the pool */
switch (eventType) {
case GattServerEvents::GATT_EVENT_DATA_WRITTEN: {
GattWriteCallbackParams cbParams = {
.connHandle = gattsEventP->conn_handle,
.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 = {
.connHandle = gattsEventP->conn_handle,
.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,
.authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS /* the callback handler must leave this member
* set to AUTH_CALLBACK_REPLY_SUCCESS if the client
* request is to proceed. */
};
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 = {
.connHandle = gattsEventP->conn_handle,
.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 = {
.connHandle = gattsEventP->conn_handle,
.handle = handle_value,
.offset = gattsEventP->params.authorize_request.request.read.offset,
.len = 0,
.data = NULL,
.authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS /* the callback handler must leave this member
* set to AUTH_CALLBACK_REPLY_SUCCESS if the client
* request is to proceed. */
};
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;
}
}
|