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