bugfix for duplicate symbol

Fork of nRF51822 by Nordic Semiconductor

Committer:
rgrover1
Date:
Wed Jan 21 10:01:16 2015 +0000
Revision:
92:8c1553b39b03
Parent:
91:112921e467db
Child:
95:7f0d80ab964b
Synchronized with git rev d24bfdb5
Author: Rohit Grover
When the write-authorization attribute 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().

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
Rohit Grover 22:c6ee8136847e 72 ASSERT ( ERROR_NONE ==
carlescufi 54:e2294c844c83 73 custom_add_in_characteristic(BLE_GATT_HANDLE_INVALID,
Rohit Grover 22:c6ee8136847e 74 &nordicUUID,
Rohit Grover 22:c6ee8136847e 75 p_char->getProperties(),
carlescufi 55:9689ec201907 76 p_char->getValueAttribute().getValuePtr(),
carlescufi 55:9689ec201907 77 p_char->getValueAttribute().getInitialLength(),
carlescufi 55:9689ec201907 78 p_char->getValueAttribute().getMaxLength(),
rgrover1 83:71302acf1804 79 p_char->isReadAuthorizationEnabled(),
rgrover1 82:6c51cbe4bc12 80 p_char->isWriteAuthorizationEnabled(),
Rohit Grover 22:c6ee8136847e 81 &nrfCharacteristicHandles[characteristicCount]),
Rohit Grover 22:c6ee8136847e 82 BLE_ERROR_PARAM_OUT_OF_RANGE );
Rohit Grover 22:c6ee8136847e 83
Rohit Grover 22:c6ee8136847e 84 /* Update the characteristic handle */
Rohit Grover 28:fdc1a88a80c8 85 uint16_t charHandle = characteristicCount;
Rohit Grover 22:c6ee8136847e 86 p_characteristics[characteristicCount++] = p_char;
Rohit Grover 28:fdc1a88a80c8 87
carlescufi 55:9689ec201907 88 p_char->getValueAttribute().setHandle(charHandle);
Rohit Grover 56:a1071b629aa3 89
carlescufi 54:e2294c844c83 90 /* Add optional descriptors if any */
carlescufi 54:e2294c844c83 91 /* ToDo: Make sure we don't overflow the array */
carlescufi 54:e2294c844c83 92 for (uint8_t j = 0; j < p_char->getDescriptorCount(); j++) {
carlescufi 54:e2294c844c83 93 GattAttribute *p_desc = p_char->getDescriptor(j);
carlescufi 54:e2294c844c83 94
carlescufi 54:e2294c844c83 95 nordicUUID = custom_convert_to_nordic_uuid(p_desc->getUUID());
carlescufi 54:e2294c844c83 96
carlescufi 54:e2294c844c83 97 ASSERT ( ERROR_NONE ==
carlescufi 54:e2294c844c83 98 custom_add_in_descriptor(BLE_GATT_HANDLE_INVALID,
carlescufi 54:e2294c844c83 99 &nordicUUID,
carlescufi 54:e2294c844c83 100 p_desc->getValuePtr(),
carlescufi 54:e2294c844c83 101 p_desc->getInitialLength(),
carlescufi 54:e2294c844c83 102 p_desc->getMaxLength(),
carlescufi 54:e2294c844c83 103 &nrfDescriptorHandles[descriptorCount]),
carlescufi 54:e2294c844c83 104 BLE_ERROR_PARAM_OUT_OF_RANGE );
carlescufi 54:e2294c844c83 105
carlescufi 54:e2294c844c83 106 uint16_t descHandle = descriptorCount;
Rohit Grover 56:a1071b629aa3 107 p_descriptors[descriptorCount++] = p_desc;
carlescufi 54:e2294c844c83 108 p_desc->setHandle(descHandle);
Rohit Grover 30:85305292b44f 109 }
Rohit Grover 22:c6ee8136847e 110 }
Rohit Grover 22:c6ee8136847e 111
Rohit Grover 22:c6ee8136847e 112 serviceCount++;
Rohit Grover 22:c6ee8136847e 113
Rohit Grover 22:c6ee8136847e 114 return BLE_ERROR_NONE;
Rohit Grover 22:c6ee8136847e 115 }
Rohit Grover 22:c6ee8136847e 116
Rohit Grover 22:c6ee8136847e 117 /**************************************************************************/
Rohit Grover 22:c6ee8136847e 118 /*!
Rohit Grover 22:c6ee8136847e 119 @brief Reads the value of a characteristic, based on the service
Rohit Grover 22:c6ee8136847e 120 and characteristic index fields
Rohit Grover 22:c6ee8136847e 121
Rohit Grover 22:c6ee8136847e 122 @param[in] charHandle
Rohit Grover 22:c6ee8136847e 123 The handle of the GattCharacteristic to read from
Rohit Grover 22:c6ee8136847e 124 @param[in] buffer
Rohit Grover 22:c6ee8136847e 125 Buffer to hold the the characteristic's value
Rohit Grover 22:c6ee8136847e 126 (raw byte array in LSB format)
Rohit Grover 22:c6ee8136847e 127 @param[in] len
Rohit Grover 22:c6ee8136847e 128 The number of bytes read into the buffer
Rohit Grover 22:c6ee8136847e 129
Rohit Grover 22:c6ee8136847e 130 @returns ble_error_t
Rohit Grover 22:c6ee8136847e 131
Rohit Grover 22:c6ee8136847e 132 @retval BLE_ERROR_NONE
Rohit Grover 22:c6ee8136847e 133 Everything executed properly
Rohit Grover 22:c6ee8136847e 134
Rohit Grover 22:c6ee8136847e 135 @section EXAMPLE
Rohit Grover 22:c6ee8136847e 136
Rohit Grover 22:c6ee8136847e 137 @code
Rohit Grover 22:c6ee8136847e 138
Rohit Grover 22:c6ee8136847e 139 @endcode
Rohit Grover 22:c6ee8136847e 140 */
Rohit Grover 22:c6ee8136847e 141 /**************************************************************************/
Rohit Grover 31:c3ce6ee5d300 142 ble_error_t nRF51GattServer::readValue(uint16_t charHandle, uint8_t buffer[], uint16_t *const lengthP)
Rohit Grover 22:c6ee8136847e 143 {
Rohit Grover 22:c6ee8136847e 144 ASSERT( ERROR_NONE ==
Rohit Grover 31:c3ce6ee5d300 145 sd_ble_gatts_value_get(nrfCharacteristicHandles[charHandle].value_handle, 0, lengthP, buffer),
Rohit Grover 22:c6ee8136847e 146 BLE_ERROR_PARAM_OUT_OF_RANGE);
Rohit Grover 22:c6ee8136847e 147
Rohit Grover 22:c6ee8136847e 148 return BLE_ERROR_NONE;
Rohit Grover 22:c6ee8136847e 149 }
Rohit Grover 22:c6ee8136847e 150
Rohit Grover 22:c6ee8136847e 151 /**************************************************************************/
Rohit Grover 22:c6ee8136847e 152 /*!
Rohit Grover 22:c6ee8136847e 153 @brief Updates the value of a characteristic, based on the service
Rohit Grover 22:c6ee8136847e 154 and characteristic index fields
Rohit Grover 22:c6ee8136847e 155
Rohit Grover 22:c6ee8136847e 156 @param[in] charHandle
Rohit Grover 22:c6ee8136847e 157 The handle of the GattCharacteristic to write to
Rohit Grover 22:c6ee8136847e 158 @param[in] buffer
Rohit Grover 22:c6ee8136847e 159 Data to use when updating the characteristic's value
Rohit Grover 22:c6ee8136847e 160 (raw byte array in LSB format)
Rohit Grover 22:c6ee8136847e 161 @param[in] len
Rohit Grover 22:c6ee8136847e 162 The number of bytes in buffer
Rohit Grover 22:c6ee8136847e 163
Rohit Grover 22:c6ee8136847e 164 @returns ble_error_t
Rohit Grover 22:c6ee8136847e 165
Rohit Grover 22:c6ee8136847e 166 @retval BLE_ERROR_NONE
Rohit Grover 22:c6ee8136847e 167 Everything executed properly
Rohit Grover 22:c6ee8136847e 168
Rohit Grover 22:c6ee8136847e 169 @section EXAMPLE
Rohit Grover 22:c6ee8136847e 170
Rohit Grover 22:c6ee8136847e 171 @code
Rohit Grover 22:c6ee8136847e 172
Rohit Grover 22:c6ee8136847e 173 @endcode
Rohit Grover 22:c6ee8136847e 174 */
Rohit Grover 22:c6ee8136847e 175 /**************************************************************************/
Rohit Grover 22:c6ee8136847e 176 ble_error_t nRF51GattServer::updateValue(uint16_t charHandle, uint8_t buffer[], uint16_t len, bool localOnly)
Rohit Grover 22:c6ee8136847e 177 {
Rohit Grover 22:c6ee8136847e 178 uint16_t gapConnectionHandle = nRF51Gap::getInstance().getConnectionHandle();
rgrover1 88:fdb2b0db620e 179 ble_error_t returnValue = BLE_ERROR_NONE;
Rohit Grover 22:c6ee8136847e 180
Rohit Grover 22:c6ee8136847e 181 if (localOnly) {
Rohit Grover 22:c6ee8136847e 182 /* Only update locally regardless of notify/indicate */
Rohit Grover 22:c6ee8136847e 183 ASSERT_INT( ERROR_NONE,
Rohit Grover 22:c6ee8136847e 184 sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer),
Rohit Grover 22:c6ee8136847e 185 BLE_ERROR_PARAM_OUT_OF_RANGE );
Rohit Grover 38:dc4d0edf9bb9 186 return BLE_ERROR_NONE;
Rohit Grover 22:c6ee8136847e 187 }
Rohit Grover 22:c6ee8136847e 188
Rohit Grover 22:c6ee8136847e 189 if ((p_characteristics[charHandle]->getProperties() &
Rohit Grover 22:c6ee8136847e 190 (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE |
Rohit Grover 22:c6ee8136847e 191 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)) &&
Rohit Grover 22:c6ee8136847e 192 (gapConnectionHandle != BLE_CONN_HANDLE_INVALID)) {
Rohit Grover 22:c6ee8136847e 193 /* HVX update for the characteristic value */
Rohit Grover 22:c6ee8136847e 194 ble_gatts_hvx_params_t hvx_params;
Rohit Grover 22:c6ee8136847e 195
Rohit Grover 22:c6ee8136847e 196 hvx_params.handle = nrfCharacteristicHandles[charHandle].value_handle;
Rohit Grover 22:c6ee8136847e 197 hvx_params.type =
Rohit Grover 22:c6ee8136847e 198 (p_characteristics[charHandle]->getProperties() &
Rohit Grover 22:c6ee8136847e 199 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) ?
Rohit Grover 22:c6ee8136847e 200 BLE_GATT_HVX_NOTIFICATION : BLE_GATT_HVX_INDICATION;
Rohit Grover 22:c6ee8136847e 201 hvx_params.offset = 0;
Rohit Grover 22:c6ee8136847e 202 hvx_params.p_data = buffer;
Rohit Grover 22:c6ee8136847e 203 hvx_params.p_len = &len;
Rohit Grover 22:c6ee8136847e 204
Rohit Grover 22:c6ee8136847e 205 error_t error = (error_t) sd_ble_gatts_hvx(gapConnectionHandle, &hvx_params);
Rohit Grover 22:c6ee8136847e 206
Rohit Grover 22:c6ee8136847e 207 /* ERROR_INVALID_STATE, ERROR_BUSY, ERROR_GATTS_SYS_ATTR_MISSING and
Rohit Grover 22:c6ee8136847e 208 *ERROR_NO_TX_BUFFERS the ATT table has been updated. */
Rohit Grover 22:c6ee8136847e 209 if ((error != ERROR_NONE) && (error != ERROR_INVALID_STATE) &&
Rohit Grover 22:c6ee8136847e 210 (error != ERROR_BLE_NO_TX_BUFFERS) && (error != ERROR_BUSY) &&
Rohit Grover 22:c6ee8136847e 211 (error != ERROR_BLEGATTS_SYS_ATTR_MISSING)) {
Rohit Grover 22:c6ee8136847e 212 ASSERT_INT( ERROR_NONE,
Rohit Grover 22:c6ee8136847e 213 sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer),
Rohit Grover 22:c6ee8136847e 214 BLE_ERROR_PARAM_OUT_OF_RANGE );
Rohit Grover 22:c6ee8136847e 215 }
rgrover1 88:fdb2b0db620e 216
rgrover1 89:c0dbd55614b2 217 /* Notifications consume application buffers. The return value can
rgrover1 88:fdb2b0db620e 218 be used for resending notifications.
rgrover1 88:fdb2b0db620e 219 */
rgrover1 89:c0dbd55614b2 220 if (error != ERROR_NONE) {
rgrover1 88:fdb2b0db620e 221 returnValue = BLE_STACK_BUSY;
rgrover1 88:fdb2b0db620e 222 }
Rohit Grover 22:c6ee8136847e 223 } else {
Rohit Grover 22:c6ee8136847e 224 ASSERT_INT( ERROR_NONE,
Rohit Grover 22:c6ee8136847e 225 sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer),
Rohit Grover 22:c6ee8136847e 226 BLE_ERROR_PARAM_OUT_OF_RANGE );
Rohit Grover 22:c6ee8136847e 227 }
Rohit Grover 22:c6ee8136847e 228
rgrover1 88:fdb2b0db620e 229 return returnValue;
Rohit Grover 22:c6ee8136847e 230 }
Rohit Grover 22:c6ee8136847e 231
Rohit Grover 22:c6ee8136847e 232 /**************************************************************************/
Rohit Grover 22:c6ee8136847e 233 /*!
Rohit Grover 22:c6ee8136847e 234 @brief Callback handler for events getting pushed up from the SD
Rohit Grover 22:c6ee8136847e 235 */
Rohit Grover 22:c6ee8136847e 236 /**************************************************************************/
Rohit Grover 22:c6ee8136847e 237 void nRF51GattServer::hwCallback(ble_evt_t *p_ble_evt)
Rohit Grover 22:c6ee8136847e 238 {
Rohit Grover 56:a1071b629aa3 239 uint16_t handle_value;
Rohit Grover 56:a1071b629aa3 240 GattServerEvents::gattEvent_t eventType;
Rohit Grover 56:a1071b629aa3 241 const ble_gatts_evt_t *gattsEventP = &p_ble_evt->evt.gatts_evt;
Rohit Grover 22:c6ee8136847e 242
Rohit Grover 22:c6ee8136847e 243 switch (p_ble_evt->header.evt_id) {
Rohit Grover 22:c6ee8136847e 244 case BLE_GATTS_EVT_WRITE:
Rohit Grover 22:c6ee8136847e 245 /* There are 2 use case here: Values being updated & CCCD (indicate/notify) enabled */
Rohit Grover 22:c6ee8136847e 246
Rohit Grover 22:c6ee8136847e 247 /* 1.) Handle CCCD changes */
Rohit Grover 56:a1071b629aa3 248 handle_value = gattsEventP->params.write.handle;
Rohit Grover 22:c6ee8136847e 249 for (uint8_t i = 0; i<characteristicCount; i++) {
Rohit Grover 35:7174913c9d67 250 if ((p_characteristics[i]->getProperties() & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)) &&
Rohit Grover 35:7174913c9d67 251 (nrfCharacteristicHandles[i].cccd_handle == handle_value)) {
Rohit Grover 22:c6ee8136847e 252 uint16_t cccd_value =
Rohit Grover 56:a1071b629aa3 253 (gattsEventP->params.write.data[1] << 8) |
Rohit Grover 56:a1071b629aa3 254 gattsEventP->params.write.data[0]; /* Little Endian but M0 may be mis-aligned */
Rohit Grover 22:c6ee8136847e 255
Rohit Grover 35:7174913c9d67 256 if (((p_characteristics[i]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE) && (cccd_value & BLE_GATT_HVX_INDICATION)) ||
Rohit Grover 35:7174913c9d67 257 ((p_characteristics[i]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) && (cccd_value & BLE_GATT_HVX_NOTIFICATION))) {
Rohit Grover 56:a1071b629aa3 258 eventType = GattServerEvents::GATT_EVENT_UPDATES_ENABLED;
Rohit Grover 22:c6ee8136847e 259 } else {
Rohit Grover 56:a1071b629aa3 260 eventType = GattServerEvents::GATT_EVENT_UPDATES_DISABLED;
Rohit Grover 22:c6ee8136847e 261 }
Rohit Grover 22:c6ee8136847e 262
Rohit Grover 56:a1071b629aa3 263 handleEvent(eventType, i);
Rohit Grover 22:c6ee8136847e 264 return;
Rohit Grover 22:c6ee8136847e 265 }
Rohit Grover 22:c6ee8136847e 266 }
Rohit Grover 22:c6ee8136847e 267
Rohit Grover 34:48d24b1d2fe6 268 /* 2.) Changes to the characteristic value will be handled with other events below */
Rohit Grover 56:a1071b629aa3 269 eventType = GattServerEvents::GATT_EVENT_DATA_WRITTEN;
Rohit Grover 22:c6ee8136847e 270 break;
Rohit Grover 22:c6ee8136847e 271
Rohit Grover 22:c6ee8136847e 272 case BLE_GATTS_EVT_HVC:
Rohit Grover 22:c6ee8136847e 273 /* Indication confirmation received */
Rohit Grover 56:a1071b629aa3 274 eventType = GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED;
Rohit Grover 56:a1071b629aa3 275 handle_value = gattsEventP->params.hvc.handle;
Rohit Grover 22:c6ee8136847e 276 break;
Rohit Grover 22:c6ee8136847e 277
Rohit Grover 56:a1071b629aa3 278 case BLE_EVT_TX_COMPLETE: {
Rohit Grover 56:a1071b629aa3 279 handleDataSentEvent(p_ble_evt->evt.common_evt.params.tx_complete.count);
Rohit Grover 56:a1071b629aa3 280 return;
Rohit Grover 56:a1071b629aa3 281 }
Rohit Grover 56:a1071b629aa3 282
Rohit Grover 39:d52a28e1fa80 283 case BLE_GATTS_EVT_SYS_ATTR_MISSING:
Rohit Grover 56:a1071b629aa3 284 sd_ble_gatts_sys_attr_set(gattsEventP->conn_handle, NULL, 0);
Rohit Grover 39:d52a28e1fa80 285 return;
Rohit Grover 39:d52a28e1fa80 286
rgrover1 82:6c51cbe4bc12 287 case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST:
rgrover1 83:71302acf1804 288 switch (gattsEventP->params.authorize_request.type) {
rgrover1 83:71302acf1804 289 case BLE_GATTS_AUTHORIZE_TYPE_READ:
rgrover1 83:71302acf1804 290 eventType = GattServerEvents::GATT_EVENT_READ_AUTHORIZATION_REQ;
rgrover1 83:71302acf1804 291 handle_value = gattsEventP->params.authorize_request.request.read.handle;
rgrover1 83:71302acf1804 292 break;
rgrover1 83:71302acf1804 293 case BLE_GATTS_AUTHORIZE_TYPE_WRITE:
rgrover1 83:71302acf1804 294 eventType = GattServerEvents::GATT_EVENT_WRITE_AUTHORIZATION_REQ;
rgrover1 83:71302acf1804 295 handle_value = gattsEventP->params.authorize_request.request.write.handle;
rgrover1 83:71302acf1804 296 break;
rgrover1 83:71302acf1804 297 default:
rgrover1 83:71302acf1804 298 return;
rgrover1 82:6c51cbe4bc12 299 }
rgrover1 82:6c51cbe4bc12 300 break;
rgrover1 82:6c51cbe4bc12 301
Rohit Grover 22:c6ee8136847e 302 default:
Rohit Grover 22:c6ee8136847e 303 return;
Rohit Grover 22:c6ee8136847e 304 }
Rohit Grover 22:c6ee8136847e 305
Rohit Grover 22:c6ee8136847e 306 /* Find index (charHandle) in the pool */
rgrover1 91:112921e467db 307 for (uint8_t i = 0; i < characteristicCount; i++) {
Rohit Grover 22:c6ee8136847e 308 if (nrfCharacteristicHandles[i].value_handle == handle_value) {
Rohit Grover 56:a1071b629aa3 309 switch (eventType) {
Rohit Grover 56:a1071b629aa3 310 case GattServerEvents::GATT_EVENT_DATA_WRITTEN: {
Rohit Grover 56:a1071b629aa3 311 GattCharacteristicWriteCBParams cbParams = {
Rohit Grover 66:b3680699d9a4 312 .charHandle = i,
Rohit Grover 56:a1071b629aa3 313 .op = static_cast<GattCharacteristicWriteCBParams::Type>(gattsEventP->params.write.op),
Rohit Grover 56:a1071b629aa3 314 .offset = gattsEventP->params.write.offset,
Rohit Grover 56:a1071b629aa3 315 .len = gattsEventP->params.write.len,
Rohit Grover 56:a1071b629aa3 316 .data = gattsEventP->params.write.data
Rohit Grover 56:a1071b629aa3 317 };
Rohit Grover 66:b3680699d9a4 318 handleDataWrittenEvent(&cbParams);
Rohit Grover 56:a1071b629aa3 319 break;
Rohit Grover 56:a1071b629aa3 320 }
rgrover1 82:6c51cbe4bc12 321 case GattServerEvents::GATT_EVENT_WRITE_AUTHORIZATION_REQ: {
rgrover1 82:6c51cbe4bc12 322 GattCharacteristicWriteAuthCBParams cbParams = {
rgrover1 82:6c51cbe4bc12 323 .charHandle = i,
rgrover1 82:6c51cbe4bc12 324 .offset = gattsEventP->params.authorize_request.request.write.offset,
rgrover1 82:6c51cbe4bc12 325 .len = gattsEventP->params.authorize_request.request.write.len,
rgrover1 82:6c51cbe4bc12 326 .data = gattsEventP->params.authorize_request.request.write.data,
rgrover1 82:6c51cbe4bc12 327 };
rgrover1 82:6c51cbe4bc12 328 ble_gatts_rw_authorize_reply_params_t reply = {
rgrover1 82:6c51cbe4bc12 329 .type = BLE_GATTS_AUTHORIZE_TYPE_WRITE,
rgrover1 84:658e5ec772a1 330 .params = {
rgrover1 82:6c51cbe4bc12 331 .write = {
rgrover1 82:6c51cbe4bc12 332 .gatt_status = (p_characteristics[i]->authorizeWrite(&cbParams) ?
rgrover1 82:6c51cbe4bc12 333 BLE_GATT_STATUS_SUCCESS : BLE_GATT_STATUS_ATTERR_WRITE_NOT_PERMITTED)
rgrover1 82:6c51cbe4bc12 334 }
rgrover1 82:6c51cbe4bc12 335 }
rgrover1 82:6c51cbe4bc12 336 };
rgrover1 82:6c51cbe4bc12 337 sd_ble_gatts_rw_authorize_reply(gattsEventP->conn_handle, &reply);
rgrover1 92:8c1553b39b03 338
rgrover1 92:8c1553b39b03 339 /*
rgrover1 92:8c1553b39b03 340 * If write-authorization is enabled for a characteristic,
rgrover1 92:8c1553b39b03 341 * AUTHORIZATION_REQ event (if replied with true) is *not*
rgrover1 92:8c1553b39b03 342 * followed by another DATA_WRITTEN event; so we still need
rgrover1 92:8c1553b39b03 343 * to invoke handleDataWritten(), much the same as we would
rgrover1 92:8c1553b39b03 344 * have done if write-authorization had not been enabled.
rgrover1 92:8c1553b39b03 345 */
rgrover1 92:8c1553b39b03 346 if (reply.params.write.gatt_status == BLE_GATT_STATUS_SUCCESS) {
rgrover1 92:8c1553b39b03 347 GattCharacteristicWriteCBParams cbParams = {
rgrover1 92:8c1553b39b03 348 .charHandle = i,
rgrover1 92:8c1553b39b03 349 .op = static_cast<GattCharacteristicWriteCBParams::Type>(gattsEventP->params.authorize_request.request.write.op),
rgrover1 92:8c1553b39b03 350 .offset = gattsEventP->params.authorize_request.request.write.offset,
rgrover1 92:8c1553b39b03 351 .len = gattsEventP->params.authorize_request.request.write.len,
rgrover1 92:8c1553b39b03 352 .data = gattsEventP->params.authorize_request.request.write.data,
rgrover1 92:8c1553b39b03 353 };
rgrover1 92:8c1553b39b03 354 handleDataWrittenEvent(&cbParams);
rgrover1 92:8c1553b39b03 355 }
rgrover1 82:6c51cbe4bc12 356 break;
rgrover1 82:6c51cbe4bc12 357 }
rgrover1 83:71302acf1804 358 case GattServerEvents::GATT_EVENT_READ_AUTHORIZATION_REQ: {
rgrover1 83:71302acf1804 359 GattCharacteristicReadAuthCBParams cbParams = {
rgrover1 83:71302acf1804 360 .charHandle = i,
rgrover1 83:71302acf1804 361 .offset = gattsEventP->params.authorize_request.request.read.offset,
rgrover1 86:561631ee642d 362 .len = 0,
rgrover1 86:561631ee642d 363 .data = NULL
rgrover1 83:71302acf1804 364 };
rgrover1 86:561631ee642d 365
rgrover1 87:f9516241903b 366 /* Ask for authorization and, potentially, new data. Use updated parameters to construct reply. */
rgrover1 86:561631ee642d 367 p_characteristics[i]->authorizeRead(&cbParams);
rgrover1 86:561631ee642d 368
rgrover1 83:71302acf1804 369 ble_gatts_rw_authorize_reply_params_t reply = {
rgrover1 86:561631ee642d 370 .type = BLE_GATTS_AUTHORIZE_TYPE_READ
rgrover1 86:561631ee642d 371 };
rgrover1 86:561631ee642d 372
rgrover1 86:561631ee642d 373 if (cbParams.authorizationReply == true) {
rgrover1 86:561631ee642d 374 reply.params.read.gatt_status = BLE_GATT_STATUS_SUCCESS;
rgrover1 86:561631ee642d 375
rgrover1 86:561631ee642d 376 if (cbParams.data != NULL) {
rgrover1 87:f9516241903b 377 reply.params.read.update = 1;
rgrover1 87:f9516241903b 378 reply.params.read.offset = cbParams.offset;
rgrover1 87:f9516241903b 379 reply.params.read.len = cbParams.len;
rgrover1 87:f9516241903b 380 reply.params.read.p_data = cbParams.data;
rgrover1 83:71302acf1804 381 }
rgrover1 86:561631ee642d 382 } else {
rgrover1 86:561631ee642d 383 reply.params.read.gatt_status = BLE_GATT_STATUS_ATTERR_READ_NOT_PERMITTED;
rgrover1 86:561631ee642d 384 }
rgrover1 86:561631ee642d 385
rgrover1 83:71302acf1804 386 sd_ble_gatts_rw_authorize_reply(gattsEventP->conn_handle, &reply);
rgrover1 83:71302acf1804 387 break;
rgrover1 83:71302acf1804 388 }
rgrover1 83:71302acf1804 389
Rohit Grover 56:a1071b629aa3 390 default:
Rohit Grover 56:a1071b629aa3 391 handleEvent(eventType, i);
Rohit Grover 56:a1071b629aa3 392 break;
Rohit Grover 56:a1071b629aa3 393 }
Rohit Grover 22:c6ee8136847e 394 }
Rohit Grover 22:c6ee8136847e 395 }
Rohit Grover 22:c6ee8136847e 396 }
rgrover1 78:9a5ba2c5d53c 397
rgrover1 78:9a5ba2c5d53c 398 ble_error_t
rgrover1 78:9a5ba2c5d53c 399 nRF51GattServer::initializeGATTDatabase(void)
rgrover1 78:9a5ba2c5d53c 400 {
rgrover1 78:9a5ba2c5d53c 401 /* Empty. Services are populated in the GattDatabase through addService(). */
rgrover1 78:9a5ba2c5d53c 402 return BLE_ERROR_NONE;
rgrover1 78:9a5ba2c5d53c 403 }