Patched version of nrf51822 FOTA compatible driver, with GPTIO disabled, as it clashed with the mbed definitions...

Fork of nRF51822 by Nordic Semiconductor

Committer:
rgrover1
Date:
Thu Apr 30 08:34:37 2015 +0100
Revision:
112:737b08b3b995
Parent:
108:27213b9fd4f9
Child:
118:f9e5e2935c5c
Synchronized with git rev 4ae92f89
Author: Rohit Grover
porting to v8.0 of the SDK

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