No changes
Fork of nRF51822 by
source/nRF5xGattServer.cpp@552:20b282c26f96, 2016-01-11 (annotated)
- Committer:
- vcoubard
- Date:
- Mon Jan 11 10:19:10 2016 +0000
- Revision:
- 552:20b282c26f96
- Parent:
- 541:884f95bf5351
- Child:
- 553:2a413611e569
Synchronized with git rev f4f35c8a
Author: Rohit Grover
Release 2.1.1
=============
A minor release to separate the concept of minlen and len in
GattCharacteristic.
Who changed what in which revision?
User | Revision | Line number | New 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 | 541:884f95bf5351 | 100 | userDescriptionDescriptorValuePtr, |
vcoubard | 541:884f95bf5351 | 101 | userDescriptionDescriptorValueLen, |
vcoubard | 541:884f95bf5351 | 102 | p_char->isReadAuthorizationEnabled(), |
vcoubard | 541:884f95bf5351 | 103 | p_char->isWriteAuthorizationEnabled(), |
vcoubard | 541:884f95bf5351 | 104 | &nrfCharacteristicHandles[characteristicCount]), |
vcoubard | 541:884f95bf5351 | 105 | BLE_ERROR_PARAM_OUT_OF_RANGE ); |
vcoubard | 541:884f95bf5351 | 106 | |
vcoubard | 541:884f95bf5351 | 107 | /* Update the characteristic handle */ |
vcoubard | 541:884f95bf5351 | 108 | p_characteristics[characteristicCount] = p_char; |
vcoubard | 541:884f95bf5351 | 109 | p_char->getValueAttribute().setHandle(nrfCharacteristicHandles[characteristicCount].value_handle); |
vcoubard | 541:884f95bf5351 | 110 | characteristicCount++; |
vcoubard | 541:884f95bf5351 | 111 | |
vcoubard | 541:884f95bf5351 | 112 | /* Add optional descriptors if any */ |
vcoubard | 541:884f95bf5351 | 113 | for (uint8_t j = 0; j < p_char->getDescriptorCount(); j++) { |
vcoubard | 541:884f95bf5351 | 114 | if (descriptorCount >= BLE_TOTAL_DESCRIPTORS) { |
vcoubard | 541:884f95bf5351 | 115 | return BLE_ERROR_NO_MEM; |
vcoubard | 541:884f95bf5351 | 116 | } |
vcoubard | 541:884f95bf5351 | 117 | |
vcoubard | 541:884f95bf5351 | 118 | GattAttribute *p_desc = p_char->getDescriptor(j); |
vcoubard | 541:884f95bf5351 | 119 | /* skip the user-description-descriptor here; this has already been handled when adding the characteristic (above). */ |
vcoubard | 541:884f95bf5351 | 120 | if (p_desc->getUUID() == BLE_UUID_DESCRIPTOR_CHAR_USER_DESC) { |
vcoubard | 541:884f95bf5351 | 121 | continue; |
vcoubard | 541:884f95bf5351 | 122 | } |
vcoubard | 541:884f95bf5351 | 123 | |
vcoubard | 541:884f95bf5351 | 124 | nordicUUID = custom_convert_to_nordic_uuid(p_desc->getUUID()); |
vcoubard | 541:884f95bf5351 | 125 | |
vcoubard | 541:884f95bf5351 | 126 | ASSERT(ERROR_NONE == |
vcoubard | 541:884f95bf5351 | 127 | custom_add_in_descriptor(BLE_GATT_HANDLE_INVALID, |
vcoubard | 541:884f95bf5351 | 128 | &nordicUUID, |
vcoubard | 541:884f95bf5351 | 129 | p_desc->getValuePtr(), |
vcoubard | 552:20b282c26f96 | 130 | p_desc->getLength(), |
vcoubard | 541:884f95bf5351 | 131 | p_desc->getMaxLength(), |
vcoubard | 541:884f95bf5351 | 132 | &nrfDescriptorHandles[descriptorCount]), |
vcoubard | 541:884f95bf5351 | 133 | BLE_ERROR_PARAM_OUT_OF_RANGE); |
vcoubard | 541:884f95bf5351 | 134 | |
vcoubard | 541:884f95bf5351 | 135 | p_descriptors[descriptorCount++] = p_desc; |
vcoubard | 541:884f95bf5351 | 136 | p_desc->setHandle(nrfDescriptorHandles[descriptorCount]); |
vcoubard | 541:884f95bf5351 | 137 | } |
vcoubard | 541:884f95bf5351 | 138 | } |
vcoubard | 541:884f95bf5351 | 139 | |
vcoubard | 541:884f95bf5351 | 140 | serviceCount++; |
vcoubard | 541:884f95bf5351 | 141 | |
vcoubard | 541:884f95bf5351 | 142 | return BLE_ERROR_NONE; |
vcoubard | 541:884f95bf5351 | 143 | } |
vcoubard | 541:884f95bf5351 | 144 | |
vcoubard | 541:884f95bf5351 | 145 | /**************************************************************************/ |
vcoubard | 541:884f95bf5351 | 146 | /*! |
vcoubard | 541:884f95bf5351 | 147 | @brief Reads the value of a characteristic, based on the service |
vcoubard | 541:884f95bf5351 | 148 | and characteristic index fields |
vcoubard | 541:884f95bf5351 | 149 | |
vcoubard | 541:884f95bf5351 | 150 | @param[in] attributeHandle |
vcoubard | 541:884f95bf5351 | 151 | The handle of the GattCharacteristic to read from |
vcoubard | 541:884f95bf5351 | 152 | @param[in] buffer |
vcoubard | 541:884f95bf5351 | 153 | Buffer to hold the the characteristic's value |
vcoubard | 541:884f95bf5351 | 154 | (raw byte array in LSB format) |
vcoubard | 541:884f95bf5351 | 155 | @param[in/out] len |
vcoubard | 541:884f95bf5351 | 156 | input: Length in bytes to be read. |
vcoubard | 541:884f95bf5351 | 157 | output: Total length of attribute value upon successful return. |
vcoubard | 541:884f95bf5351 | 158 | |
vcoubard | 541:884f95bf5351 | 159 | @returns ble_error_t |
vcoubard | 541:884f95bf5351 | 160 | |
vcoubard | 541:884f95bf5351 | 161 | @retval BLE_ERROR_NONE |
vcoubard | 541:884f95bf5351 | 162 | Everything executed properly |
vcoubard | 541:884f95bf5351 | 163 | */ |
vcoubard | 541:884f95bf5351 | 164 | /**************************************************************************/ |
vcoubard | 541:884f95bf5351 | 165 | ble_error_t nRF5xGattServer::read(GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP) |
vcoubard | 541:884f95bf5351 | 166 | { |
vcoubard | 541:884f95bf5351 | 167 | return read(BLE_CONN_HANDLE_INVALID, attributeHandle, buffer, lengthP); |
vcoubard | 541:884f95bf5351 | 168 | } |
vcoubard | 541:884f95bf5351 | 169 | |
vcoubard | 541:884f95bf5351 | 170 | ble_error_t nRF5xGattServer::read(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP) |
vcoubard | 541:884f95bf5351 | 171 | { |
vcoubard | 541:884f95bf5351 | 172 | ble_gatts_value_t value = { |
vcoubard | 541:884f95bf5351 | 173 | .len = *lengthP, |
vcoubard | 541:884f95bf5351 | 174 | .offset = 0, |
vcoubard | 541:884f95bf5351 | 175 | .p_value = buffer, |
vcoubard | 541:884f95bf5351 | 176 | }; |
vcoubard | 541:884f95bf5351 | 177 | |
vcoubard | 541:884f95bf5351 | 178 | ASSERT( ERROR_NONE == |
vcoubard | 541:884f95bf5351 | 179 | sd_ble_gatts_value_get(connectionHandle, attributeHandle, &value), |
vcoubard | 541:884f95bf5351 | 180 | BLE_ERROR_PARAM_OUT_OF_RANGE); |
vcoubard | 541:884f95bf5351 | 181 | *lengthP = value.len; |
vcoubard | 541:884f95bf5351 | 182 | |
vcoubard | 541:884f95bf5351 | 183 | return BLE_ERROR_NONE; |
vcoubard | 541:884f95bf5351 | 184 | } |
vcoubard | 541:884f95bf5351 | 185 | |
vcoubard | 541:884f95bf5351 | 186 | /**************************************************************************/ |
vcoubard | 541:884f95bf5351 | 187 | /*! |
vcoubard | 541:884f95bf5351 | 188 | @brief Updates the value of a characteristic, based on the service |
vcoubard | 541:884f95bf5351 | 189 | and characteristic index fields |
vcoubard | 541:884f95bf5351 | 190 | |
vcoubard | 541:884f95bf5351 | 191 | @param[in] charHandle |
vcoubard | 541:884f95bf5351 | 192 | The handle of the GattCharacteristic to write to |
vcoubard | 541:884f95bf5351 | 193 | @param[in] buffer |
vcoubard | 541:884f95bf5351 | 194 | Data to use when updating the characteristic's value |
vcoubard | 541:884f95bf5351 | 195 | (raw byte array in LSB format) |
vcoubard | 541:884f95bf5351 | 196 | @param[in] len |
vcoubard | 541:884f95bf5351 | 197 | The number of bytes in buffer |
vcoubard | 541:884f95bf5351 | 198 | |
vcoubard | 541:884f95bf5351 | 199 | @returns ble_error_t |
vcoubard | 541:884f95bf5351 | 200 | |
vcoubard | 541:884f95bf5351 | 201 | @retval BLE_ERROR_NONE |
vcoubard | 541:884f95bf5351 | 202 | Everything executed properly |
vcoubard | 541:884f95bf5351 | 203 | */ |
vcoubard | 541:884f95bf5351 | 204 | /**************************************************************************/ |
vcoubard | 541:884f95bf5351 | 205 | ble_error_t nRF5xGattServer::write(GattAttribute::Handle_t attributeHandle, const uint8_t buffer[], uint16_t len, bool localOnly) |
vcoubard | 541:884f95bf5351 | 206 | { |
vcoubard | 541:884f95bf5351 | 207 | return write(BLE_CONN_HANDLE_INVALID, attributeHandle, buffer, len, localOnly); |
vcoubard | 541:884f95bf5351 | 208 | } |
vcoubard | 541:884f95bf5351 | 209 | |
vcoubard | 541:884f95bf5351 | 210 | 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 | 211 | { |
vcoubard | 541:884f95bf5351 | 212 | ble_error_t returnValue = BLE_ERROR_NONE; |
vcoubard | 541:884f95bf5351 | 213 | |
vcoubard | 541:884f95bf5351 | 214 | ble_gatts_value_t value = { |
vcoubard | 541:884f95bf5351 | 215 | .len = len, |
vcoubard | 541:884f95bf5351 | 216 | .offset = 0, |
vcoubard | 541:884f95bf5351 | 217 | .p_value = const_cast<uint8_t *>(buffer), |
vcoubard | 541:884f95bf5351 | 218 | }; |
vcoubard | 541:884f95bf5351 | 219 | |
vcoubard | 541:884f95bf5351 | 220 | if (localOnly) { |
vcoubard | 541:884f95bf5351 | 221 | /* Only update locally regardless of notify/indicate */ |
vcoubard | 541:884f95bf5351 | 222 | ASSERT_INT( ERROR_NONE, |
vcoubard | 541:884f95bf5351 | 223 | sd_ble_gatts_value_set(connectionHandle, attributeHandle, &value), |
vcoubard | 541:884f95bf5351 | 224 | BLE_ERROR_PARAM_OUT_OF_RANGE ); |
vcoubard | 541:884f95bf5351 | 225 | return BLE_ERROR_NONE; |
vcoubard | 541:884f95bf5351 | 226 | } |
vcoubard | 541:884f95bf5351 | 227 | |
vcoubard | 541:884f95bf5351 | 228 | int characteristicIndex = resolveValueHandleToCharIndex(attributeHandle); |
vcoubard | 541:884f95bf5351 | 229 | if ((characteristicIndex != -1) && |
vcoubard | 541:884f95bf5351 | 230 | (p_characteristics[characteristicIndex]->getProperties() & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY))) { |
vcoubard | 541:884f95bf5351 | 231 | /* HVX update for the characteristic value */ |
vcoubard | 541:884f95bf5351 | 232 | ble_gatts_hvx_params_t hvx_params; |
vcoubard | 541:884f95bf5351 | 233 | |
vcoubard | 541:884f95bf5351 | 234 | hvx_params.handle = attributeHandle; |
vcoubard | 541:884f95bf5351 | 235 | hvx_params.type = |
vcoubard | 541:884f95bf5351 | 236 | (p_characteristics[characteristicIndex]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) ? BLE_GATT_HVX_NOTIFICATION : BLE_GATT_HVX_INDICATION; |
vcoubard | 541:884f95bf5351 | 237 | hvx_params.offset = 0; |
vcoubard | 541:884f95bf5351 | 238 | hvx_params.p_data = const_cast<uint8_t *>(buffer); |
vcoubard | 541:884f95bf5351 | 239 | hvx_params.p_len = &len; |
vcoubard | 541:884f95bf5351 | 240 | |
vcoubard | 541:884f95bf5351 | 241 | if (connectionHandle == BLE_CONN_HANDLE_INVALID) { /* use the default connection handle if the caller hasn't specified a valid connectionHandle. */ |
vcoubard | 541:884f95bf5351 | 242 | connectionHandle = nRF5xGap::getInstance().getConnectionHandle(); |
vcoubard | 541:884f95bf5351 | 243 | } |
vcoubard | 541:884f95bf5351 | 244 | error_t error = (error_t) sd_ble_gatts_hvx(connectionHandle, &hvx_params); |
vcoubard | 541:884f95bf5351 | 245 | if (error != ERROR_NONE) { |
vcoubard | 541:884f95bf5351 | 246 | switch (error) { |
vcoubard | 541:884f95bf5351 | 247 | case ERROR_BLE_NO_TX_BUFFERS: /* Notifications consume application buffers. The return value can be used for resending notifications. */ |
vcoubard | 541:884f95bf5351 | 248 | case ERROR_BUSY: |
vcoubard | 541:884f95bf5351 | 249 | returnValue = BLE_STACK_BUSY; |
vcoubard | 541:884f95bf5351 | 250 | break; |
vcoubard | 541:884f95bf5351 | 251 | |
vcoubard | 541:884f95bf5351 | 252 | case ERROR_INVALID_STATE: |
vcoubard | 541:884f95bf5351 | 253 | case ERROR_BLEGATTS_SYS_ATTR_MISSING: |
vcoubard | 541:884f95bf5351 | 254 | returnValue = BLE_ERROR_INVALID_STATE; |
vcoubard | 541:884f95bf5351 | 255 | break; |
vcoubard | 541:884f95bf5351 | 256 | |
vcoubard | 541:884f95bf5351 | 257 | default : |
vcoubard | 541:884f95bf5351 | 258 | ASSERT_INT( ERROR_NONE, |
vcoubard | 541:884f95bf5351 | 259 | sd_ble_gatts_value_set(connectionHandle, attributeHandle, &value), |
vcoubard | 541:884f95bf5351 | 260 | BLE_ERROR_PARAM_OUT_OF_RANGE ); |
vcoubard | 541:884f95bf5351 | 261 | |
vcoubard | 541:884f95bf5351 | 262 | /* Notifications consume application buffers. The return value can |
vcoubard | 541:884f95bf5351 | 263 | * be used for resending notifications. */ |
vcoubard | 541:884f95bf5351 | 264 | returnValue = BLE_STACK_BUSY; |
vcoubard | 541:884f95bf5351 | 265 | break; |
vcoubard | 541:884f95bf5351 | 266 | } |
vcoubard | 541:884f95bf5351 | 267 | } |
vcoubard | 541:884f95bf5351 | 268 | } else { |
vcoubard | 541:884f95bf5351 | 269 | returnValue = BLE_ERROR_INVALID_STATE; // if assert is not used |
vcoubard | 541:884f95bf5351 | 270 | ASSERT_INT( ERROR_NONE, |
vcoubard | 541:884f95bf5351 | 271 | sd_ble_gatts_value_set(connectionHandle, attributeHandle, &value), |
vcoubard | 541:884f95bf5351 | 272 | BLE_ERROR_PARAM_OUT_OF_RANGE ); |
vcoubard | 541:884f95bf5351 | 273 | } |
vcoubard | 541:884f95bf5351 | 274 | |
vcoubard | 541:884f95bf5351 | 275 | return returnValue; |
vcoubard | 541:884f95bf5351 | 276 | } |
vcoubard | 541:884f95bf5351 | 277 | |
vcoubard | 541:884f95bf5351 | 278 | ble_error_t nRF5xGattServer::areUpdatesEnabled(const GattCharacteristic &characteristic, bool *enabledP) |
vcoubard | 541:884f95bf5351 | 279 | { |
vcoubard | 541:884f95bf5351 | 280 | /* Forward the call with the default connection handle. */ |
vcoubard | 541:884f95bf5351 | 281 | return areUpdatesEnabled(nRF5xGap::getInstance().getConnectionHandle(), characteristic, enabledP); |
vcoubard | 541:884f95bf5351 | 282 | } |
vcoubard | 541:884f95bf5351 | 283 | |
vcoubard | 541:884f95bf5351 | 284 | ble_error_t nRF5xGattServer::areUpdatesEnabled(Gap::Handle_t connectionHandle, const GattCharacteristic &characteristic, bool *enabledP) |
vcoubard | 541:884f95bf5351 | 285 | { |
vcoubard | 541:884f95bf5351 | 286 | int characteristicIndex = resolveValueHandleToCharIndex(characteristic.getValueHandle()); |
vcoubard | 541:884f95bf5351 | 287 | if (characteristicIndex == -1) { |
vcoubard | 541:884f95bf5351 | 288 | return BLE_ERROR_INVALID_PARAM; |
vcoubard | 541:884f95bf5351 | 289 | } |
vcoubard | 541:884f95bf5351 | 290 | |
vcoubard | 541:884f95bf5351 | 291 | /* Read the cccd value from the GATT server. */ |
vcoubard | 541:884f95bf5351 | 292 | GattAttribute::Handle_t cccdHandle = nrfCharacteristicHandles[characteristicIndex].cccd_handle; |
vcoubard | 541:884f95bf5351 | 293 | uint16_t cccdValue; |
vcoubard | 541:884f95bf5351 | 294 | uint16_t length = sizeof(cccdValue); |
vcoubard | 541:884f95bf5351 | 295 | ble_error_t rc = read(connectionHandle, cccdHandle, reinterpret_cast<uint8_t *>(&cccdValue), &length); |
vcoubard | 541:884f95bf5351 | 296 | if (rc != BLE_ERROR_NONE) { |
vcoubard | 541:884f95bf5351 | 297 | return rc; |
vcoubard | 541:884f95bf5351 | 298 | } |
vcoubard | 541:884f95bf5351 | 299 | if (length != sizeof(cccdValue)) { |
vcoubard | 541:884f95bf5351 | 300 | return BLE_ERROR_INVALID_STATE; |
vcoubard | 541:884f95bf5351 | 301 | } |
vcoubard | 541:884f95bf5351 | 302 | |
vcoubard | 541:884f95bf5351 | 303 | /* Check for NOTFICATION or INDICATION in CCCD. */ |
vcoubard | 541:884f95bf5351 | 304 | if ((cccdValue & BLE_GATT_HVX_NOTIFICATION) || (cccdValue & BLE_GATT_HVX_INDICATION)) { |
vcoubard | 541:884f95bf5351 | 305 | *enabledP = true; |
vcoubard | 541:884f95bf5351 | 306 | } |
vcoubard | 541:884f95bf5351 | 307 | |
vcoubard | 541:884f95bf5351 | 308 | return BLE_ERROR_NONE; |
vcoubard | 541:884f95bf5351 | 309 | } |
vcoubard | 541:884f95bf5351 | 310 | |
vcoubard | 541:884f95bf5351 | 311 | /**************************************************************************/ |
vcoubard | 541:884f95bf5351 | 312 | /*! |
vcoubard | 541:884f95bf5351 | 313 | @brief Callback handler for events getting pushed up from the SD |
vcoubard | 541:884f95bf5351 | 314 | */ |
vcoubard | 541:884f95bf5351 | 315 | /**************************************************************************/ |
vcoubard | 541:884f95bf5351 | 316 | void nRF5xGattServer::hwCallback(ble_evt_t *p_ble_evt) |
vcoubard | 541:884f95bf5351 | 317 | { |
vcoubard | 541:884f95bf5351 | 318 | GattAttribute::Handle_t handle_value; |
vcoubard | 541:884f95bf5351 | 319 | GattServerEvents::gattEvent_t eventType; |
vcoubard | 541:884f95bf5351 | 320 | const ble_gatts_evt_t *gattsEventP = &p_ble_evt->evt.gatts_evt; |
vcoubard | 541:884f95bf5351 | 321 | |
vcoubard | 541:884f95bf5351 | 322 | switch (p_ble_evt->header.evt_id) { |
vcoubard | 541:884f95bf5351 | 323 | case BLE_GATTS_EVT_WRITE: { |
vcoubard | 541:884f95bf5351 | 324 | /* There are 2 use case here: Values being updated & CCCD (indicate/notify) enabled */ |
vcoubard | 541:884f95bf5351 | 325 | |
vcoubard | 541:884f95bf5351 | 326 | /* 1.) Handle CCCD changes */ |
vcoubard | 541:884f95bf5351 | 327 | handle_value = gattsEventP->params.write.handle; |
vcoubard | 541:884f95bf5351 | 328 | int characteristicIndex = resolveCCCDHandleToCharIndex(handle_value); |
vcoubard | 541:884f95bf5351 | 329 | if ((characteristicIndex != -1) && |
vcoubard | 541:884f95bf5351 | 330 | (p_characteristics[characteristicIndex]->getProperties() & |
vcoubard | 541:884f95bf5351 | 331 | (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY))) { |
vcoubard | 541:884f95bf5351 | 332 | |
vcoubard | 541:884f95bf5351 | 333 | 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 | 334 | |
vcoubard | 541:884f95bf5351 | 335 | if (((p_characteristics[characteristicIndex]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE) && (cccd_value & BLE_GATT_HVX_INDICATION)) || |
vcoubard | 541:884f95bf5351 | 336 | ((p_characteristics[characteristicIndex]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) && (cccd_value & BLE_GATT_HVX_NOTIFICATION))) { |
vcoubard | 541:884f95bf5351 | 337 | eventType = GattServerEvents::GATT_EVENT_UPDATES_ENABLED; |
vcoubard | 541:884f95bf5351 | 338 | } else { |
vcoubard | 541:884f95bf5351 | 339 | eventType = GattServerEvents::GATT_EVENT_UPDATES_DISABLED; |
vcoubard | 541:884f95bf5351 | 340 | } |
vcoubard | 541:884f95bf5351 | 341 | |
vcoubard | 541:884f95bf5351 | 342 | handleEvent(eventType, p_characteristics[characteristicIndex]->getValueHandle()); |
vcoubard | 541:884f95bf5351 | 343 | return; |
vcoubard | 541:884f95bf5351 | 344 | } |
vcoubard | 541:884f95bf5351 | 345 | |
vcoubard | 541:884f95bf5351 | 346 | /* 2.) Changes to the characteristic value will be handled with other events below */ |
vcoubard | 541:884f95bf5351 | 347 | eventType = GattServerEvents::GATT_EVENT_DATA_WRITTEN; |
vcoubard | 541:884f95bf5351 | 348 | } |
vcoubard | 541:884f95bf5351 | 349 | break; |
vcoubard | 541:884f95bf5351 | 350 | |
vcoubard | 541:884f95bf5351 | 351 | case BLE_GATTS_EVT_HVC: |
vcoubard | 541:884f95bf5351 | 352 | /* Indication confirmation received */ |
vcoubard | 541:884f95bf5351 | 353 | eventType = GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED; |
vcoubard | 541:884f95bf5351 | 354 | handle_value = gattsEventP->params.hvc.handle; |
vcoubard | 541:884f95bf5351 | 355 | break; |
vcoubard | 541:884f95bf5351 | 356 | |
vcoubard | 541:884f95bf5351 | 357 | case BLE_EVT_TX_COMPLETE: { |
vcoubard | 541:884f95bf5351 | 358 | handleDataSentEvent(p_ble_evt->evt.common_evt.params.tx_complete.count); |
vcoubard | 541:884f95bf5351 | 359 | return; |
vcoubard | 541:884f95bf5351 | 360 | } |
vcoubard | 541:884f95bf5351 | 361 | |
vcoubard | 541:884f95bf5351 | 362 | case BLE_GATTS_EVT_SYS_ATTR_MISSING: |
vcoubard | 541:884f95bf5351 | 363 | sd_ble_gatts_sys_attr_set(gattsEventP->conn_handle, NULL, 0, 0); |
vcoubard | 541:884f95bf5351 | 364 | return; |
vcoubard | 541:884f95bf5351 | 365 | |
vcoubard | 541:884f95bf5351 | 366 | case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST: |
vcoubard | 541:884f95bf5351 | 367 | switch (gattsEventP->params.authorize_request.type) { |
vcoubard | 541:884f95bf5351 | 368 | case BLE_GATTS_AUTHORIZE_TYPE_READ: |
vcoubard | 541:884f95bf5351 | 369 | eventType = GattServerEvents::GATT_EVENT_READ_AUTHORIZATION_REQ; |
vcoubard | 541:884f95bf5351 | 370 | handle_value = gattsEventP->params.authorize_request.request.read.handle; |
vcoubard | 541:884f95bf5351 | 371 | break; |
vcoubard | 541:884f95bf5351 | 372 | case BLE_GATTS_AUTHORIZE_TYPE_WRITE: |
vcoubard | 541:884f95bf5351 | 373 | eventType = GattServerEvents::GATT_EVENT_WRITE_AUTHORIZATION_REQ; |
vcoubard | 541:884f95bf5351 | 374 | handle_value = gattsEventP->params.authorize_request.request.write.handle; |
vcoubard | 541:884f95bf5351 | 375 | break; |
vcoubard | 541:884f95bf5351 | 376 | default: |
vcoubard | 541:884f95bf5351 | 377 | return; |
vcoubard | 541:884f95bf5351 | 378 | } |
vcoubard | 541:884f95bf5351 | 379 | break; |
vcoubard | 541:884f95bf5351 | 380 | |
vcoubard | 541:884f95bf5351 | 381 | default: |
vcoubard | 541:884f95bf5351 | 382 | return; |
vcoubard | 541:884f95bf5351 | 383 | } |
vcoubard | 541:884f95bf5351 | 384 | |
vcoubard | 541:884f95bf5351 | 385 | int characteristicIndex = resolveValueHandleToCharIndex(handle_value); |
vcoubard | 541:884f95bf5351 | 386 | if (characteristicIndex == -1) { |
vcoubard | 541:884f95bf5351 | 387 | return; |
vcoubard | 541:884f95bf5351 | 388 | } |
vcoubard | 541:884f95bf5351 | 389 | |
vcoubard | 541:884f95bf5351 | 390 | /* Find index (charHandle) in the pool */ |
vcoubard | 541:884f95bf5351 | 391 | switch (eventType) { |
vcoubard | 541:884f95bf5351 | 392 | case GattServerEvents::GATT_EVENT_DATA_WRITTEN: { |
vcoubard | 541:884f95bf5351 | 393 | GattWriteCallbackParams cbParams = { |
vcoubard | 541:884f95bf5351 | 394 | .connHandle = gattsEventP->conn_handle, |
vcoubard | 541:884f95bf5351 | 395 | .handle = handle_value, |
vcoubard | 541:884f95bf5351 | 396 | .writeOp = static_cast<GattWriteCallbackParams::WriteOp_t>(gattsEventP->params.write.op), |
vcoubard | 541:884f95bf5351 | 397 | .offset = gattsEventP->params.write.offset, |
vcoubard | 541:884f95bf5351 | 398 | .len = gattsEventP->params.write.len, |
vcoubard | 541:884f95bf5351 | 399 | .data = gattsEventP->params.write.data |
vcoubard | 541:884f95bf5351 | 400 | }; |
vcoubard | 541:884f95bf5351 | 401 | handleDataWrittenEvent(&cbParams); |
vcoubard | 541:884f95bf5351 | 402 | break; |
vcoubard | 541:884f95bf5351 | 403 | } |
vcoubard | 541:884f95bf5351 | 404 | case GattServerEvents::GATT_EVENT_WRITE_AUTHORIZATION_REQ: { |
vcoubard | 541:884f95bf5351 | 405 | GattWriteAuthCallbackParams cbParams = { |
vcoubard | 541:884f95bf5351 | 406 | .connHandle = gattsEventP->conn_handle, |
vcoubard | 541:884f95bf5351 | 407 | .handle = handle_value, |
vcoubard | 541:884f95bf5351 | 408 | .offset = gattsEventP->params.authorize_request.request.write.offset, |
vcoubard | 541:884f95bf5351 | 409 | .len = gattsEventP->params.authorize_request.request.write.len, |
vcoubard | 541:884f95bf5351 | 410 | .data = gattsEventP->params.authorize_request.request.write.data, |
vcoubard | 541:884f95bf5351 | 411 | .authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS /* the callback handler must leave this member |
vcoubard | 541:884f95bf5351 | 412 | * set to AUTH_CALLBACK_REPLY_SUCCESS if the client |
vcoubard | 541:884f95bf5351 | 413 | * request is to proceed. */ |
vcoubard | 541:884f95bf5351 | 414 | }; |
vcoubard | 541:884f95bf5351 | 415 | ble_gatts_rw_authorize_reply_params_t reply = { |
vcoubard | 541:884f95bf5351 | 416 | .type = BLE_GATTS_AUTHORIZE_TYPE_WRITE, |
vcoubard | 541:884f95bf5351 | 417 | .params = { |
vcoubard | 541:884f95bf5351 | 418 | .write = { |
vcoubard | 541:884f95bf5351 | 419 | .gatt_status = p_characteristics[characteristicIndex]->authorizeWrite(&cbParams) |
vcoubard | 541:884f95bf5351 | 420 | } |
vcoubard | 541:884f95bf5351 | 421 | } |
vcoubard | 541:884f95bf5351 | 422 | }; |
vcoubard | 541:884f95bf5351 | 423 | sd_ble_gatts_rw_authorize_reply(gattsEventP->conn_handle, &reply); |
vcoubard | 541:884f95bf5351 | 424 | |
vcoubard | 541:884f95bf5351 | 425 | /* |
vcoubard | 541:884f95bf5351 | 426 | * If write-authorization is enabled for a characteristic, |
vcoubard | 541:884f95bf5351 | 427 | * AUTHORIZATION_REQ event (if replied with true) is *not* |
vcoubard | 541:884f95bf5351 | 428 | * followed by another DATA_WRITTEN event; so we still need |
vcoubard | 541:884f95bf5351 | 429 | * to invoke handleDataWritten(), much the same as we would |
vcoubard | 541:884f95bf5351 | 430 | * have done if write-authorization had not been enabled. |
vcoubard | 541:884f95bf5351 | 431 | */ |
vcoubard | 541:884f95bf5351 | 432 | if (reply.params.write.gatt_status == BLE_GATT_STATUS_SUCCESS) { |
vcoubard | 541:884f95bf5351 | 433 | GattWriteCallbackParams cbParams = { |
vcoubard | 541:884f95bf5351 | 434 | .connHandle = gattsEventP->conn_handle, |
vcoubard | 541:884f95bf5351 | 435 | .handle = handle_value, |
vcoubard | 541:884f95bf5351 | 436 | .writeOp = static_cast<GattWriteCallbackParams::WriteOp_t>(gattsEventP->params.authorize_request.request.write.op), |
vcoubard | 541:884f95bf5351 | 437 | .offset = gattsEventP->params.authorize_request.request.write.offset, |
vcoubard | 541:884f95bf5351 | 438 | .len = gattsEventP->params.authorize_request.request.write.len, |
vcoubard | 541:884f95bf5351 | 439 | .data = gattsEventP->params.authorize_request.request.write.data, |
vcoubard | 541:884f95bf5351 | 440 | }; |
vcoubard | 541:884f95bf5351 | 441 | handleDataWrittenEvent(&cbParams); |
vcoubard | 541:884f95bf5351 | 442 | } |
vcoubard | 541:884f95bf5351 | 443 | break; |
vcoubard | 541:884f95bf5351 | 444 | } |
vcoubard | 541:884f95bf5351 | 445 | case GattServerEvents::GATT_EVENT_READ_AUTHORIZATION_REQ: { |
vcoubard | 541:884f95bf5351 | 446 | GattReadAuthCallbackParams cbParams = { |
vcoubard | 541:884f95bf5351 | 447 | .connHandle = gattsEventP->conn_handle, |
vcoubard | 541:884f95bf5351 | 448 | .handle = handle_value, |
vcoubard | 541:884f95bf5351 | 449 | .offset = gattsEventP->params.authorize_request.request.read.offset, |
vcoubard | 541:884f95bf5351 | 450 | .len = 0, |
vcoubard | 541:884f95bf5351 | 451 | .data = NULL, |
vcoubard | 541:884f95bf5351 | 452 | .authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS /* the callback handler must leave this member |
vcoubard | 541:884f95bf5351 | 453 | * set to AUTH_CALLBACK_REPLY_SUCCESS if the client |
vcoubard | 541:884f95bf5351 | 454 | * request is to proceed. */ |
vcoubard | 541:884f95bf5351 | 455 | }; |
vcoubard | 541:884f95bf5351 | 456 | |
vcoubard | 541:884f95bf5351 | 457 | ble_gatts_rw_authorize_reply_params_t reply = { |
vcoubard | 541:884f95bf5351 | 458 | .type = BLE_GATTS_AUTHORIZE_TYPE_READ, |
vcoubard | 541:884f95bf5351 | 459 | .params = { |
vcoubard | 541:884f95bf5351 | 460 | .read = { |
vcoubard | 541:884f95bf5351 | 461 | .gatt_status = p_characteristics[characteristicIndex]->authorizeRead(&cbParams) |
vcoubard | 541:884f95bf5351 | 462 | } |
vcoubard | 541:884f95bf5351 | 463 | } |
vcoubard | 541:884f95bf5351 | 464 | }; |
vcoubard | 541:884f95bf5351 | 465 | |
vcoubard | 541:884f95bf5351 | 466 | if (cbParams.authorizationReply == BLE_GATT_STATUS_SUCCESS) { |
vcoubard | 541:884f95bf5351 | 467 | if (cbParams.data != NULL) { |
vcoubard | 541:884f95bf5351 | 468 | reply.params.read.update = 1; |
vcoubard | 541:884f95bf5351 | 469 | reply.params.read.offset = cbParams.offset; |
vcoubard | 541:884f95bf5351 | 470 | reply.params.read.len = cbParams.len; |
vcoubard | 541:884f95bf5351 | 471 | reply.params.read.p_data = cbParams.data; |
vcoubard | 541:884f95bf5351 | 472 | } |
vcoubard | 541:884f95bf5351 | 473 | } |
vcoubard | 541:884f95bf5351 | 474 | |
vcoubard | 541:884f95bf5351 | 475 | sd_ble_gatts_rw_authorize_reply(gattsEventP->conn_handle, &reply); |
vcoubard | 541:884f95bf5351 | 476 | break; |
vcoubard | 541:884f95bf5351 | 477 | } |
vcoubard | 541:884f95bf5351 | 478 | |
vcoubard | 541:884f95bf5351 | 479 | default: |
vcoubard | 541:884f95bf5351 | 480 | handleEvent(eventType, handle_value); |
vcoubard | 541:884f95bf5351 | 481 | break; |
vcoubard | 541:884f95bf5351 | 482 | } |
rgrover1 | 388:db85a09c27ef | 483 | } |