None

Dependencies:   nrf51-sdk

Dependents:   microbit-dal

Fork of nRF51822 by Lancaster University

Committer:
vcoubard
Date:
Mon Jan 11 10:19:11 2016 +0000
Revision:
553:2a413611e569
Parent:
552:20b282c26f96
Child:
561:613dbbdeed27
Synchronized with git rev 9620c7a1
Author: Rohit Grover
Release 2.1.2
=============

Minor update around GattCharacteristics having variable length.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vcoubard 541:884f95bf5351 1 /* mbed Microcontroller Library
vcoubard 541:884f95bf5351 2 * Copyright (c) 2006-2013 ARM Limited
vcoubard 541:884f95bf5351 3 *
vcoubard 541:884f95bf5351 4 * Licensed under the Apache License, Version 2.0 (the "License");
vcoubard 541:884f95bf5351 5 * you may not use this file except in compliance with the License.
vcoubard 541:884f95bf5351 6 * You may obtain a copy of the License at
vcoubard 541:884f95bf5351 7 *
vcoubard 541:884f95bf5351 8 * http://www.apache.org/licenses/LICENSE-2.0
vcoubard 541:884f95bf5351 9 *
vcoubard 541:884f95bf5351 10 * Unless required by applicable law or agreed to in writing, software
vcoubard 541:884f95bf5351 11 * distributed under the License is distributed on an "AS IS" BASIS,
vcoubard 541:884f95bf5351 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
vcoubard 541:884f95bf5351 13 * See the License for the specific language governing permissions and
vcoubard 541:884f95bf5351 14 * limitations under the License.
vcoubard 541:884f95bf5351 15 */
vcoubard 541:884f95bf5351 16
vcoubard 541:884f95bf5351 17 #include "nRF5xGattServer.h"
vcoubard 541:884f95bf5351 18 #include "mbed.h"
vcoubard 541:884f95bf5351 19
vcoubard 541:884f95bf5351 20 #include "common/common.h"
vcoubard 541:884f95bf5351 21 #include "btle/custom/custom_helper.h"
vcoubard 541:884f95bf5351 22
vcoubard 541:884f95bf5351 23 #include "nRF5xGap.h"
vcoubard 541:884f95bf5351 24
vcoubard 541:884f95bf5351 25 nRF5xGattServer &nRF5xGattServer::getInstance(void) {
vcoubard 541:884f95bf5351 26 static nRF5xGattServer m_instance;
vcoubard 541:884f95bf5351 27 return m_instance;
vcoubard 541:884f95bf5351 28 }
vcoubard 541:884f95bf5351 29
vcoubard 541:884f95bf5351 30 /**************************************************************************/
vcoubard 541:884f95bf5351 31 /*!
vcoubard 541:884f95bf5351 32 @brief Adds a new service to the GATT table on the peripheral
vcoubard 541:884f95bf5351 33
vcoubard 541:884f95bf5351 34 @returns ble_error_t
vcoubard 541:884f95bf5351 35
vcoubard 541:884f95bf5351 36 @retval BLE_ERROR_NONE
vcoubard 541:884f95bf5351 37 Everything executed properly
vcoubard 541:884f95bf5351 38
vcoubard 541:884f95bf5351 39 @section EXAMPLE
vcoubard 541:884f95bf5351 40
vcoubard 541:884f95bf5351 41 @code
vcoubard 541:884f95bf5351 42
vcoubard 541:884f95bf5351 43 @endcode
vcoubard 541:884f95bf5351 44 */
vcoubard 541:884f95bf5351 45 /**************************************************************************/
vcoubard 541:884f95bf5351 46 ble_error_t nRF5xGattServer::addService(GattService &service)
vcoubard 541:884f95bf5351 47 {
vcoubard 541:884f95bf5351 48 /* ToDo: Make sure this service UUID doesn't already exist (?) */
vcoubard 541:884f95bf5351 49 /* ToDo: Basic validation */
vcoubard 541:884f95bf5351 50
vcoubard 541:884f95bf5351 51 /* Add the service to the nRF51 */
vcoubard 541:884f95bf5351 52 ble_uuid_t nordicUUID;
vcoubard 541:884f95bf5351 53 nordicUUID = custom_convert_to_nordic_uuid(service.getUUID());
vcoubard 541:884f95bf5351 54
vcoubard 541:884f95bf5351 55 uint16_t serviceHandle;
vcoubard 541:884f95bf5351 56 ASSERT( ERROR_NONE ==
vcoubard 541:884f95bf5351 57 sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
vcoubard 541:884f95bf5351 58 &nordicUUID,
vcoubard 541:884f95bf5351 59 &serviceHandle),
vcoubard 541:884f95bf5351 60 BLE_ERROR_PARAM_OUT_OF_RANGE );
vcoubard 541:884f95bf5351 61 service.setHandle(serviceHandle);
vcoubard 541:884f95bf5351 62
vcoubard 541:884f95bf5351 63 /* Add characteristics to the service */
vcoubard 541:884f95bf5351 64 for (uint8_t i = 0; i < service.getCharacteristicCount(); i++) {
vcoubard 541:884f95bf5351 65 if (characteristicCount >= BLE_TOTAL_CHARACTERISTICS) {
vcoubard 541:884f95bf5351 66 return BLE_ERROR_NO_MEM;
vcoubard 541:884f95bf5351 67 }
vcoubard 541:884f95bf5351 68 GattCharacteristic *p_char = service.getCharacteristic(i);
vcoubard 541:884f95bf5351 69
vcoubard 541:884f95bf5351 70 /* Skip any incompletely defined, read-only characteristics. */
vcoubard 541:884f95bf5351 71 if ((p_char->getValueAttribute().getValuePtr() == NULL) &&
vcoubard 552:20b282c26f96 72 (p_char->getValueAttribute().getLength() == 0) &&
vcoubard 541:884f95bf5351 73 (p_char->getProperties() == GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ)) {
vcoubard 541:884f95bf5351 74 continue;
vcoubard 541:884f95bf5351 75 }
vcoubard 541:884f95bf5351 76
vcoubard 541:884f95bf5351 77 nordicUUID = custom_convert_to_nordic_uuid(p_char->getValueAttribute().getUUID());
vcoubard 541:884f95bf5351 78
vcoubard 541:884f95bf5351 79 /* The user-description descriptor is a special case which needs to be
vcoubard 541:884f95bf5351 80 * handled at the time of adding the characteristic. The following block
vcoubard 541:884f95bf5351 81 * is meant to discover its presence. */
vcoubard 541:884f95bf5351 82 const uint8_t *userDescriptionDescriptorValuePtr = NULL;
vcoubard 541:884f95bf5351 83 uint16_t userDescriptionDescriptorValueLen = 0;
vcoubard 541:884f95bf5351 84 for (uint8_t j = 0; j < p_char->getDescriptorCount(); j++) {
vcoubard 541:884f95bf5351 85 GattAttribute *p_desc = p_char->getDescriptor(j);
vcoubard 541:884f95bf5351 86 if (p_desc->getUUID() == BLE_UUID_DESCRIPTOR_CHAR_USER_DESC) {
vcoubard 541:884f95bf5351 87 userDescriptionDescriptorValuePtr = p_desc->getValuePtr();
vcoubard 541:884f95bf5351 88 userDescriptionDescriptorValueLen = p_desc->getLength();
vcoubard 541:884f95bf5351 89 }
vcoubard 541:884f95bf5351 90 }
vcoubard 541:884f95bf5351 91
vcoubard 541:884f95bf5351 92 ASSERT ( ERROR_NONE ==
vcoubard 541:884f95bf5351 93 custom_add_in_characteristic(BLE_GATT_HANDLE_INVALID,
vcoubard 541:884f95bf5351 94 &nordicUUID,
vcoubard 541:884f95bf5351 95 p_char->getProperties(),
vcoubard 541:884f95bf5351 96 p_char->getRequiredSecurity(),
vcoubard 541:884f95bf5351 97 p_char->getValueAttribute().getValuePtr(),
vcoubard 552:20b282c26f96 98 p_char->getValueAttribute().getLength(),
vcoubard 541:884f95bf5351 99 p_char->getValueAttribute().getMaxLength(),
vcoubard 553:2a413611e569 100 p_char->getValueAttribute().hasVariableLength(),
vcoubard 541:884f95bf5351 101 userDescriptionDescriptorValuePtr,
vcoubard 541:884f95bf5351 102 userDescriptionDescriptorValueLen,
vcoubard 541:884f95bf5351 103 p_char->isReadAuthorizationEnabled(),
vcoubard 541:884f95bf5351 104 p_char->isWriteAuthorizationEnabled(),
vcoubard 541:884f95bf5351 105 &nrfCharacteristicHandles[characteristicCount]),
vcoubard 541:884f95bf5351 106 BLE_ERROR_PARAM_OUT_OF_RANGE );
vcoubard 541:884f95bf5351 107
vcoubard 541:884f95bf5351 108 /* Update the characteristic handle */
vcoubard 541:884f95bf5351 109 p_characteristics[characteristicCount] = p_char;
vcoubard 541:884f95bf5351 110 p_char->getValueAttribute().setHandle(nrfCharacteristicHandles[characteristicCount].value_handle);
vcoubard 541:884f95bf5351 111 characteristicCount++;
vcoubard 541:884f95bf5351 112
vcoubard 541:884f95bf5351 113 /* Add optional descriptors if any */
vcoubard 541:884f95bf5351 114 for (uint8_t j = 0; j < p_char->getDescriptorCount(); j++) {
vcoubard 541:884f95bf5351 115 if (descriptorCount >= BLE_TOTAL_DESCRIPTORS) {
vcoubard 541:884f95bf5351 116 return BLE_ERROR_NO_MEM;
vcoubard 541:884f95bf5351 117 }
vcoubard 541:884f95bf5351 118
vcoubard 541:884f95bf5351 119 GattAttribute *p_desc = p_char->getDescriptor(j);
vcoubard 541:884f95bf5351 120 /* skip the user-description-descriptor here; this has already been handled when adding the characteristic (above). */
vcoubard 541:884f95bf5351 121 if (p_desc->getUUID() == BLE_UUID_DESCRIPTOR_CHAR_USER_DESC) {
vcoubard 541:884f95bf5351 122 continue;
vcoubard 541:884f95bf5351 123 }
vcoubard 541:884f95bf5351 124
vcoubard 541:884f95bf5351 125 nordicUUID = custom_convert_to_nordic_uuid(p_desc->getUUID());
vcoubard 541:884f95bf5351 126
vcoubard 541:884f95bf5351 127 ASSERT(ERROR_NONE ==
vcoubard 541:884f95bf5351 128 custom_add_in_descriptor(BLE_GATT_HANDLE_INVALID,
vcoubard 541:884f95bf5351 129 &nordicUUID,
vcoubard 541:884f95bf5351 130 p_desc->getValuePtr(),
vcoubard 552:20b282c26f96 131 p_desc->getLength(),
vcoubard 541:884f95bf5351 132 p_desc->getMaxLength(),
vcoubard 553:2a413611e569 133 p_desc->hasVariableLength(),
vcoubard 541:884f95bf5351 134 &nrfDescriptorHandles[descriptorCount]),
vcoubard 541:884f95bf5351 135 BLE_ERROR_PARAM_OUT_OF_RANGE);
vcoubard 541:884f95bf5351 136
vcoubard 541:884f95bf5351 137 p_descriptors[descriptorCount++] = p_desc;
vcoubard 541:884f95bf5351 138 p_desc->setHandle(nrfDescriptorHandles[descriptorCount]);
vcoubard 541:884f95bf5351 139 }
vcoubard 541:884f95bf5351 140 }
vcoubard 541:884f95bf5351 141
vcoubard 541:884f95bf5351 142 serviceCount++;
vcoubard 541:884f95bf5351 143
vcoubard 541:884f95bf5351 144 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 145 }
vcoubard 541:884f95bf5351 146
vcoubard 541:884f95bf5351 147 /**************************************************************************/
vcoubard 541:884f95bf5351 148 /*!
vcoubard 541:884f95bf5351 149 @brief Reads the value of a characteristic, based on the service
vcoubard 541:884f95bf5351 150 and characteristic index fields
vcoubard 541:884f95bf5351 151
vcoubard 541:884f95bf5351 152 @param[in] attributeHandle
vcoubard 541:884f95bf5351 153 The handle of the GattCharacteristic to read from
vcoubard 541:884f95bf5351 154 @param[in] buffer
vcoubard 541:884f95bf5351 155 Buffer to hold the the characteristic's value
vcoubard 541:884f95bf5351 156 (raw byte array in LSB format)
vcoubard 541:884f95bf5351 157 @param[in/out] len
vcoubard 541:884f95bf5351 158 input: Length in bytes to be read.
vcoubard 541:884f95bf5351 159 output: Total length of attribute value upon successful return.
vcoubard 541:884f95bf5351 160
vcoubard 541:884f95bf5351 161 @returns ble_error_t
vcoubard 541:884f95bf5351 162
vcoubard 541:884f95bf5351 163 @retval BLE_ERROR_NONE
vcoubard 541:884f95bf5351 164 Everything executed properly
vcoubard 541:884f95bf5351 165 */
vcoubard 541:884f95bf5351 166 /**************************************************************************/
vcoubard 541:884f95bf5351 167 ble_error_t nRF5xGattServer::read(GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP)
vcoubard 541:884f95bf5351 168 {
vcoubard 541:884f95bf5351 169 return read(BLE_CONN_HANDLE_INVALID, attributeHandle, buffer, lengthP);
vcoubard 541:884f95bf5351 170 }
vcoubard 541:884f95bf5351 171
vcoubard 541:884f95bf5351 172 ble_error_t nRF5xGattServer::read(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP)
vcoubard 541:884f95bf5351 173 {
vcoubard 541:884f95bf5351 174 ble_gatts_value_t value = {
vcoubard 541:884f95bf5351 175 .len = *lengthP,
vcoubard 541:884f95bf5351 176 .offset = 0,
vcoubard 541:884f95bf5351 177 .p_value = buffer,
vcoubard 541:884f95bf5351 178 };
vcoubard 541:884f95bf5351 179
vcoubard 541:884f95bf5351 180 ASSERT( ERROR_NONE ==
vcoubard 541:884f95bf5351 181 sd_ble_gatts_value_get(connectionHandle, attributeHandle, &value),
vcoubard 541:884f95bf5351 182 BLE_ERROR_PARAM_OUT_OF_RANGE);
vcoubard 541:884f95bf5351 183 *lengthP = value.len;
vcoubard 541:884f95bf5351 184
vcoubard 541:884f95bf5351 185 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 186 }
vcoubard 541:884f95bf5351 187
vcoubard 541:884f95bf5351 188 /**************************************************************************/
vcoubard 541:884f95bf5351 189 /*!
vcoubard 541:884f95bf5351 190 @brief Updates the value of a characteristic, based on the service
vcoubard 541:884f95bf5351 191 and characteristic index fields
vcoubard 541:884f95bf5351 192
vcoubard 541:884f95bf5351 193 @param[in] charHandle
vcoubard 541:884f95bf5351 194 The handle of the GattCharacteristic to write to
vcoubard 541:884f95bf5351 195 @param[in] buffer
vcoubard 541:884f95bf5351 196 Data to use when updating the characteristic's value
vcoubard 541:884f95bf5351 197 (raw byte array in LSB format)
vcoubard 541:884f95bf5351 198 @param[in] len
vcoubard 541:884f95bf5351 199 The number of bytes in buffer
vcoubard 541:884f95bf5351 200
vcoubard 541:884f95bf5351 201 @returns ble_error_t
vcoubard 541:884f95bf5351 202
vcoubard 541:884f95bf5351 203 @retval BLE_ERROR_NONE
vcoubard 541:884f95bf5351 204 Everything executed properly
vcoubard 541:884f95bf5351 205 */
vcoubard 541:884f95bf5351 206 /**************************************************************************/
vcoubard 541:884f95bf5351 207 ble_error_t nRF5xGattServer::write(GattAttribute::Handle_t attributeHandle, const uint8_t buffer[], uint16_t len, bool localOnly)
vcoubard 541:884f95bf5351 208 {
vcoubard 541:884f95bf5351 209 return write(BLE_CONN_HANDLE_INVALID, attributeHandle, buffer, len, localOnly);
vcoubard 541:884f95bf5351 210 }
vcoubard 541:884f95bf5351 211
vcoubard 541:884f95bf5351 212 ble_error_t nRF5xGattServer::write(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, const uint8_t buffer[], uint16_t len, bool localOnly)
vcoubard 541:884f95bf5351 213 {
vcoubard 541:884f95bf5351 214 ble_error_t returnValue = BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 215
vcoubard 541:884f95bf5351 216 ble_gatts_value_t value = {
vcoubard 541:884f95bf5351 217 .len = len,
vcoubard 541:884f95bf5351 218 .offset = 0,
vcoubard 541:884f95bf5351 219 .p_value = const_cast<uint8_t *>(buffer),
vcoubard 541:884f95bf5351 220 };
vcoubard 541:884f95bf5351 221
vcoubard 541:884f95bf5351 222 if (localOnly) {
vcoubard 541:884f95bf5351 223 /* Only update locally regardless of notify/indicate */
vcoubard 541:884f95bf5351 224 ASSERT_INT( ERROR_NONE,
vcoubard 541:884f95bf5351 225 sd_ble_gatts_value_set(connectionHandle, attributeHandle, &value),
vcoubard 541:884f95bf5351 226 BLE_ERROR_PARAM_OUT_OF_RANGE );
vcoubard 541:884f95bf5351 227 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 228 }
vcoubard 541:884f95bf5351 229
vcoubard 541:884f95bf5351 230 int characteristicIndex = resolveValueHandleToCharIndex(attributeHandle);
vcoubard 541:884f95bf5351 231 if ((characteristicIndex != -1) &&
vcoubard 541:884f95bf5351 232 (p_characteristics[characteristicIndex]->getProperties() & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY))) {
vcoubard 541:884f95bf5351 233 /* HVX update for the characteristic value */
vcoubard 541:884f95bf5351 234 ble_gatts_hvx_params_t hvx_params;
vcoubard 541:884f95bf5351 235
vcoubard 541:884f95bf5351 236 hvx_params.handle = attributeHandle;
vcoubard 541:884f95bf5351 237 hvx_params.type =
vcoubard 541:884f95bf5351 238 (p_characteristics[characteristicIndex]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) ? BLE_GATT_HVX_NOTIFICATION : BLE_GATT_HVX_INDICATION;
vcoubard 541:884f95bf5351 239 hvx_params.offset = 0;
vcoubard 541:884f95bf5351 240 hvx_params.p_data = const_cast<uint8_t *>(buffer);
vcoubard 541:884f95bf5351 241 hvx_params.p_len = &len;
vcoubard 541:884f95bf5351 242
vcoubard 541:884f95bf5351 243 if (connectionHandle == BLE_CONN_HANDLE_INVALID) { /* use the default connection handle if the caller hasn't specified a valid connectionHandle. */
vcoubard 541:884f95bf5351 244 connectionHandle = nRF5xGap::getInstance().getConnectionHandle();
vcoubard 541:884f95bf5351 245 }
vcoubard 541:884f95bf5351 246 error_t error = (error_t) sd_ble_gatts_hvx(connectionHandle, &hvx_params);
vcoubard 541:884f95bf5351 247 if (error != ERROR_NONE) {
vcoubard 541:884f95bf5351 248 switch (error) {
vcoubard 541:884f95bf5351 249 case ERROR_BLE_NO_TX_BUFFERS: /* Notifications consume application buffers. The return value can be used for resending notifications. */
vcoubard 541:884f95bf5351 250 case ERROR_BUSY:
vcoubard 541:884f95bf5351 251 returnValue = BLE_STACK_BUSY;
vcoubard 541:884f95bf5351 252 break;
vcoubard 541:884f95bf5351 253
vcoubard 541:884f95bf5351 254 case ERROR_INVALID_STATE:
vcoubard 541:884f95bf5351 255 case ERROR_BLEGATTS_SYS_ATTR_MISSING:
vcoubard 541:884f95bf5351 256 returnValue = BLE_ERROR_INVALID_STATE;
vcoubard 541:884f95bf5351 257 break;
vcoubard 541:884f95bf5351 258
vcoubard 541:884f95bf5351 259 default :
vcoubard 541:884f95bf5351 260 ASSERT_INT( ERROR_NONE,
vcoubard 541:884f95bf5351 261 sd_ble_gatts_value_set(connectionHandle, attributeHandle, &value),
vcoubard 541:884f95bf5351 262 BLE_ERROR_PARAM_OUT_OF_RANGE );
vcoubard 541:884f95bf5351 263
vcoubard 541:884f95bf5351 264 /* Notifications consume application buffers. The return value can
vcoubard 541:884f95bf5351 265 * be used for resending notifications. */
vcoubard 541:884f95bf5351 266 returnValue = BLE_STACK_BUSY;
vcoubard 541:884f95bf5351 267 break;
vcoubard 541:884f95bf5351 268 }
vcoubard 541:884f95bf5351 269 }
vcoubard 541:884f95bf5351 270 } else {
vcoubard 541:884f95bf5351 271 returnValue = BLE_ERROR_INVALID_STATE; // if assert is not used
vcoubard 541:884f95bf5351 272 ASSERT_INT( ERROR_NONE,
vcoubard 541:884f95bf5351 273 sd_ble_gatts_value_set(connectionHandle, attributeHandle, &value),
vcoubard 541:884f95bf5351 274 BLE_ERROR_PARAM_OUT_OF_RANGE );
vcoubard 541:884f95bf5351 275 }
vcoubard 541:884f95bf5351 276
vcoubard 541:884f95bf5351 277 return returnValue;
vcoubard 541:884f95bf5351 278 }
vcoubard 541:884f95bf5351 279
vcoubard 541:884f95bf5351 280 ble_error_t nRF5xGattServer::areUpdatesEnabled(const GattCharacteristic &characteristic, bool *enabledP)
vcoubard 541:884f95bf5351 281 {
vcoubard 541:884f95bf5351 282 /* Forward the call with the default connection handle. */
vcoubard 541:884f95bf5351 283 return areUpdatesEnabled(nRF5xGap::getInstance().getConnectionHandle(), characteristic, enabledP);
vcoubard 541:884f95bf5351 284 }
vcoubard 541:884f95bf5351 285
vcoubard 541:884f95bf5351 286 ble_error_t nRF5xGattServer::areUpdatesEnabled(Gap::Handle_t connectionHandle, const GattCharacteristic &characteristic, bool *enabledP)
vcoubard 541:884f95bf5351 287 {
vcoubard 541:884f95bf5351 288 int characteristicIndex = resolveValueHandleToCharIndex(characteristic.getValueHandle());
vcoubard 541:884f95bf5351 289 if (characteristicIndex == -1) {
vcoubard 541:884f95bf5351 290 return BLE_ERROR_INVALID_PARAM;
vcoubard 541:884f95bf5351 291 }
vcoubard 541:884f95bf5351 292
vcoubard 541:884f95bf5351 293 /* Read the cccd value from the GATT server. */
vcoubard 541:884f95bf5351 294 GattAttribute::Handle_t cccdHandle = nrfCharacteristicHandles[characteristicIndex].cccd_handle;
vcoubard 541:884f95bf5351 295 uint16_t cccdValue;
vcoubard 541:884f95bf5351 296 uint16_t length = sizeof(cccdValue);
vcoubard 541:884f95bf5351 297 ble_error_t rc = read(connectionHandle, cccdHandle, reinterpret_cast<uint8_t *>(&cccdValue), &length);
vcoubard 541:884f95bf5351 298 if (rc != BLE_ERROR_NONE) {
vcoubard 541:884f95bf5351 299 return rc;
vcoubard 541:884f95bf5351 300 }
vcoubard 541:884f95bf5351 301 if (length != sizeof(cccdValue)) {
vcoubard 541:884f95bf5351 302 return BLE_ERROR_INVALID_STATE;
vcoubard 541:884f95bf5351 303 }
vcoubard 541:884f95bf5351 304
vcoubard 541:884f95bf5351 305 /* Check for NOTFICATION or INDICATION in CCCD. */
vcoubard 541:884f95bf5351 306 if ((cccdValue & BLE_GATT_HVX_NOTIFICATION) || (cccdValue & BLE_GATT_HVX_INDICATION)) {
vcoubard 541:884f95bf5351 307 *enabledP = true;
vcoubard 541:884f95bf5351 308 }
vcoubard 541:884f95bf5351 309
vcoubard 541:884f95bf5351 310 return BLE_ERROR_NONE;
vcoubard 541:884f95bf5351 311 }
vcoubard 541:884f95bf5351 312
vcoubard 541:884f95bf5351 313 /**************************************************************************/
vcoubard 541:884f95bf5351 314 /*!
vcoubard 541:884f95bf5351 315 @brief Callback handler for events getting pushed up from the SD
vcoubard 541:884f95bf5351 316 */
vcoubard 541:884f95bf5351 317 /**************************************************************************/
vcoubard 541:884f95bf5351 318 void nRF5xGattServer::hwCallback(ble_evt_t *p_ble_evt)
vcoubard 541:884f95bf5351 319 {
vcoubard 541:884f95bf5351 320 GattAttribute::Handle_t handle_value;
vcoubard 541:884f95bf5351 321 GattServerEvents::gattEvent_t eventType;
vcoubard 541:884f95bf5351 322 const ble_gatts_evt_t *gattsEventP = &p_ble_evt->evt.gatts_evt;
vcoubard 541:884f95bf5351 323
vcoubard 541:884f95bf5351 324 switch (p_ble_evt->header.evt_id) {
vcoubard 541:884f95bf5351 325 case BLE_GATTS_EVT_WRITE: {
vcoubard 541:884f95bf5351 326 /* There are 2 use case here: Values being updated & CCCD (indicate/notify) enabled */
vcoubard 541:884f95bf5351 327
vcoubard 541:884f95bf5351 328 /* 1.) Handle CCCD changes */
vcoubard 541:884f95bf5351 329 handle_value = gattsEventP->params.write.handle;
vcoubard 541:884f95bf5351 330 int characteristicIndex = resolveCCCDHandleToCharIndex(handle_value);
vcoubard 541:884f95bf5351 331 if ((characteristicIndex != -1) &&
vcoubard 541:884f95bf5351 332 (p_characteristics[characteristicIndex]->getProperties() &
vcoubard 541:884f95bf5351 333 (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY))) {
vcoubard 541:884f95bf5351 334
vcoubard 541:884f95bf5351 335 uint16_t cccd_value = (gattsEventP->params.write.data[1] << 8) | gattsEventP->params.write.data[0]; /* Little Endian but M0 may be mis-aligned */
vcoubard 541:884f95bf5351 336
vcoubard 541:884f95bf5351 337 if (((p_characteristics[characteristicIndex]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE) && (cccd_value & BLE_GATT_HVX_INDICATION)) ||
vcoubard 541:884f95bf5351 338 ((p_characteristics[characteristicIndex]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) && (cccd_value & BLE_GATT_HVX_NOTIFICATION))) {
vcoubard 541:884f95bf5351 339 eventType = GattServerEvents::GATT_EVENT_UPDATES_ENABLED;
vcoubard 541:884f95bf5351 340 } else {
vcoubard 541:884f95bf5351 341 eventType = GattServerEvents::GATT_EVENT_UPDATES_DISABLED;
vcoubard 541:884f95bf5351 342 }
vcoubard 541:884f95bf5351 343
vcoubard 541:884f95bf5351 344 handleEvent(eventType, p_characteristics[characteristicIndex]->getValueHandle());
vcoubard 541:884f95bf5351 345 return;
vcoubard 541:884f95bf5351 346 }
vcoubard 541:884f95bf5351 347
vcoubard 541:884f95bf5351 348 /* 2.) Changes to the characteristic value will be handled with other events below */
vcoubard 541:884f95bf5351 349 eventType = GattServerEvents::GATT_EVENT_DATA_WRITTEN;
vcoubard 541:884f95bf5351 350 }
vcoubard 541:884f95bf5351 351 break;
vcoubard 541:884f95bf5351 352
vcoubard 541:884f95bf5351 353 case BLE_GATTS_EVT_HVC:
vcoubard 541:884f95bf5351 354 /* Indication confirmation received */
vcoubard 541:884f95bf5351 355 eventType = GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED;
vcoubard 541:884f95bf5351 356 handle_value = gattsEventP->params.hvc.handle;
vcoubard 541:884f95bf5351 357 break;
vcoubard 541:884f95bf5351 358
vcoubard 541:884f95bf5351 359 case BLE_EVT_TX_COMPLETE: {
vcoubard 541:884f95bf5351 360 handleDataSentEvent(p_ble_evt->evt.common_evt.params.tx_complete.count);
vcoubard 541:884f95bf5351 361 return;
vcoubard 541:884f95bf5351 362 }
vcoubard 541:884f95bf5351 363
vcoubard 541:884f95bf5351 364 case BLE_GATTS_EVT_SYS_ATTR_MISSING:
vcoubard 541:884f95bf5351 365 sd_ble_gatts_sys_attr_set(gattsEventP->conn_handle, NULL, 0, 0);
vcoubard 541:884f95bf5351 366 return;
vcoubard 541:884f95bf5351 367
vcoubard 541:884f95bf5351 368 case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST:
vcoubard 541:884f95bf5351 369 switch (gattsEventP->params.authorize_request.type) {
vcoubard 541:884f95bf5351 370 case BLE_GATTS_AUTHORIZE_TYPE_READ:
vcoubard 541:884f95bf5351 371 eventType = GattServerEvents::GATT_EVENT_READ_AUTHORIZATION_REQ;
vcoubard 541:884f95bf5351 372 handle_value = gattsEventP->params.authorize_request.request.read.handle;
vcoubard 541:884f95bf5351 373 break;
vcoubard 541:884f95bf5351 374 case BLE_GATTS_AUTHORIZE_TYPE_WRITE:
vcoubard 541:884f95bf5351 375 eventType = GattServerEvents::GATT_EVENT_WRITE_AUTHORIZATION_REQ;
vcoubard 541:884f95bf5351 376 handle_value = gattsEventP->params.authorize_request.request.write.handle;
vcoubard 541:884f95bf5351 377 break;
vcoubard 541:884f95bf5351 378 default:
vcoubard 541:884f95bf5351 379 return;
vcoubard 541:884f95bf5351 380 }
vcoubard 541:884f95bf5351 381 break;
vcoubard 541:884f95bf5351 382
vcoubard 541:884f95bf5351 383 default:
vcoubard 541:884f95bf5351 384 return;
vcoubard 541:884f95bf5351 385 }
vcoubard 541:884f95bf5351 386
vcoubard 541:884f95bf5351 387 int characteristicIndex = resolveValueHandleToCharIndex(handle_value);
vcoubard 541:884f95bf5351 388 if (characteristicIndex == -1) {
vcoubard 541:884f95bf5351 389 return;
vcoubard 541:884f95bf5351 390 }
vcoubard 541:884f95bf5351 391
vcoubard 541:884f95bf5351 392 /* Find index (charHandle) in the pool */
vcoubard 541:884f95bf5351 393 switch (eventType) {
vcoubard 541:884f95bf5351 394 case GattServerEvents::GATT_EVENT_DATA_WRITTEN: {
vcoubard 541:884f95bf5351 395 GattWriteCallbackParams cbParams = {
vcoubard 541:884f95bf5351 396 .connHandle = gattsEventP->conn_handle,
vcoubard 541:884f95bf5351 397 .handle = handle_value,
vcoubard 541:884f95bf5351 398 .writeOp = static_cast<GattWriteCallbackParams::WriteOp_t>(gattsEventP->params.write.op),
vcoubard 541:884f95bf5351 399 .offset = gattsEventP->params.write.offset,
vcoubard 541:884f95bf5351 400 .len = gattsEventP->params.write.len,
vcoubard 541:884f95bf5351 401 .data = gattsEventP->params.write.data
vcoubard 541:884f95bf5351 402 };
vcoubard 541:884f95bf5351 403 handleDataWrittenEvent(&cbParams);
vcoubard 541:884f95bf5351 404 break;
vcoubard 541:884f95bf5351 405 }
vcoubard 541:884f95bf5351 406 case GattServerEvents::GATT_EVENT_WRITE_AUTHORIZATION_REQ: {
vcoubard 541:884f95bf5351 407 GattWriteAuthCallbackParams cbParams = {
vcoubard 541:884f95bf5351 408 .connHandle = gattsEventP->conn_handle,
vcoubard 541:884f95bf5351 409 .handle = handle_value,
vcoubard 541:884f95bf5351 410 .offset = gattsEventP->params.authorize_request.request.write.offset,
vcoubard 541:884f95bf5351 411 .len = gattsEventP->params.authorize_request.request.write.len,
vcoubard 541:884f95bf5351 412 .data = gattsEventP->params.authorize_request.request.write.data,
vcoubard 541:884f95bf5351 413 .authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS /* the callback handler must leave this member
vcoubard 541:884f95bf5351 414 * set to AUTH_CALLBACK_REPLY_SUCCESS if the client
vcoubard 541:884f95bf5351 415 * request is to proceed. */
vcoubard 541:884f95bf5351 416 };
vcoubard 541:884f95bf5351 417 ble_gatts_rw_authorize_reply_params_t reply = {
vcoubard 541:884f95bf5351 418 .type = BLE_GATTS_AUTHORIZE_TYPE_WRITE,
vcoubard 541:884f95bf5351 419 .params = {
vcoubard 541:884f95bf5351 420 .write = {
vcoubard 541:884f95bf5351 421 .gatt_status = p_characteristics[characteristicIndex]->authorizeWrite(&cbParams)
vcoubard 541:884f95bf5351 422 }
vcoubard 541:884f95bf5351 423 }
vcoubard 541:884f95bf5351 424 };
vcoubard 541:884f95bf5351 425 sd_ble_gatts_rw_authorize_reply(gattsEventP->conn_handle, &reply);
vcoubard 541:884f95bf5351 426
vcoubard 541:884f95bf5351 427 /*
vcoubard 541:884f95bf5351 428 * If write-authorization is enabled for a characteristic,
vcoubard 541:884f95bf5351 429 * AUTHORIZATION_REQ event (if replied with true) is *not*
vcoubard 541:884f95bf5351 430 * followed by another DATA_WRITTEN event; so we still need
vcoubard 541:884f95bf5351 431 * to invoke handleDataWritten(), much the same as we would
vcoubard 541:884f95bf5351 432 * have done if write-authorization had not been enabled.
vcoubard 541:884f95bf5351 433 */
vcoubard 541:884f95bf5351 434 if (reply.params.write.gatt_status == BLE_GATT_STATUS_SUCCESS) {
vcoubard 541:884f95bf5351 435 GattWriteCallbackParams cbParams = {
vcoubard 541:884f95bf5351 436 .connHandle = gattsEventP->conn_handle,
vcoubard 541:884f95bf5351 437 .handle = handle_value,
vcoubard 541:884f95bf5351 438 .writeOp = static_cast<GattWriteCallbackParams::WriteOp_t>(gattsEventP->params.authorize_request.request.write.op),
vcoubard 541:884f95bf5351 439 .offset = gattsEventP->params.authorize_request.request.write.offset,
vcoubard 541:884f95bf5351 440 .len = gattsEventP->params.authorize_request.request.write.len,
vcoubard 541:884f95bf5351 441 .data = gattsEventP->params.authorize_request.request.write.data,
vcoubard 541:884f95bf5351 442 };
vcoubard 541:884f95bf5351 443 handleDataWrittenEvent(&cbParams);
vcoubard 541:884f95bf5351 444 }
vcoubard 541:884f95bf5351 445 break;
vcoubard 541:884f95bf5351 446 }
vcoubard 541:884f95bf5351 447 case GattServerEvents::GATT_EVENT_READ_AUTHORIZATION_REQ: {
vcoubard 541:884f95bf5351 448 GattReadAuthCallbackParams cbParams = {
vcoubard 541:884f95bf5351 449 .connHandle = gattsEventP->conn_handle,
vcoubard 541:884f95bf5351 450 .handle = handle_value,
vcoubard 541:884f95bf5351 451 .offset = gattsEventP->params.authorize_request.request.read.offset,
vcoubard 541:884f95bf5351 452 .len = 0,
vcoubard 541:884f95bf5351 453 .data = NULL,
vcoubard 541:884f95bf5351 454 .authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS /* the callback handler must leave this member
vcoubard 541:884f95bf5351 455 * set to AUTH_CALLBACK_REPLY_SUCCESS if the client
vcoubard 541:884f95bf5351 456 * request is to proceed. */
vcoubard 541:884f95bf5351 457 };
vcoubard 541:884f95bf5351 458
vcoubard 541:884f95bf5351 459 ble_gatts_rw_authorize_reply_params_t reply = {
vcoubard 541:884f95bf5351 460 .type = BLE_GATTS_AUTHORIZE_TYPE_READ,
vcoubard 541:884f95bf5351 461 .params = {
vcoubard 541:884f95bf5351 462 .read = {
vcoubard 541:884f95bf5351 463 .gatt_status = p_characteristics[characteristicIndex]->authorizeRead(&cbParams)
vcoubard 541:884f95bf5351 464 }
vcoubard 541:884f95bf5351 465 }
vcoubard 541:884f95bf5351 466 };
vcoubard 541:884f95bf5351 467
vcoubard 541:884f95bf5351 468 if (cbParams.authorizationReply == BLE_GATT_STATUS_SUCCESS) {
vcoubard 541:884f95bf5351 469 if (cbParams.data != NULL) {
vcoubard 541:884f95bf5351 470 reply.params.read.update = 1;
vcoubard 541:884f95bf5351 471 reply.params.read.offset = cbParams.offset;
vcoubard 541:884f95bf5351 472 reply.params.read.len = cbParams.len;
vcoubard 541:884f95bf5351 473 reply.params.read.p_data = cbParams.data;
vcoubard 541:884f95bf5351 474 }
vcoubard 541:884f95bf5351 475 }
vcoubard 541:884f95bf5351 476
vcoubard 541:884f95bf5351 477 sd_ble_gatts_rw_authorize_reply(gattsEventP->conn_handle, &reply);
vcoubard 541:884f95bf5351 478 break;
vcoubard 541:884f95bf5351 479 }
vcoubard 541:884f95bf5351 480
vcoubard 541:884f95bf5351 481 default:
vcoubard 541:884f95bf5351 482 handleEvent(eventType, handle_value);
vcoubard 541:884f95bf5351 483 break;
vcoubard 541:884f95bf5351 484 }
rgrover1 388:db85a09c27ef 485 }