For scoop

Dependents:   GonioTrainer

Fork of nRF51822 by Nordic Semiconductor

Committer:
rgrover1
Date:
Thu Apr 30 08:34:38 2015 +0100
Revision:
119:3ba3e377b972
Parent:
118:f9e5e2935c5c
Child:
131:b21411170d00
Synchronized with git rev ac865c5e
Author: Rohit Grover
Fixes #9: Handle user-description-descriptor properly.
Also tracked by https://github.com/mbedmicro/BLE_API/issues/38

Who changed what in which revision?

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