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