MicroBit as BLE gamepad
Fork of nRF51822 by
source/nRF5xGattServer.cpp@616:a8f9b022d8fd, 2016-04-06 (annotated)
- Committer:
- LancasterUniversity
- Date:
- Wed Apr 06 22:39:17 2016 +0100
- Revision:
- 616:a8f9b022d8fd
- Parent:
- 615:65ea2acfc6a2
Synchronized with git rev 67d0ac73
Author: Joe Finney
microbit: Update to indicate when system attributes require intialisation
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 | 616:a8f9b022d8fd | 18 | #ifdef YOTTA_CFG_MBED_OS |
LancasterUniversity | 616:a8f9b022d8fd | 19 | #include "mbed-drivers/mbed.h" |
LancasterUniversity | 616:a8f9b022d8fd | 20 | #else |
LancasterUniversity | 616:a8f9b022d8fd | 21 | #include "mbed.h" |
LancasterUniversity | 616:a8f9b022d8fd | 22 | #endif |
LancasterUniversity | 615:65ea2acfc6a2 | 23 | |
LancasterUniversity | 615:65ea2acfc6a2 | 24 | #include "common/common.h" |
LancasterUniversity | 615:65ea2acfc6a2 | 25 | #include "btle/custom/custom_helper.h" |
LancasterUniversity | 615:65ea2acfc6a2 | 26 | |
LancasterUniversity | 616:a8f9b022d8fd | 27 | #include "nRF5xn.h" |
LancasterUniversity | 615:65ea2acfc6a2 | 28 | |
LancasterUniversity | 615:65ea2acfc6a2 | 29 | /**************************************************************************/ |
LancasterUniversity | 615:65ea2acfc6a2 | 30 | /*! |
LancasterUniversity | 615:65ea2acfc6a2 | 31 | @brief Adds a new service to the GATT table on the peripheral |
LancasterUniversity | 615:65ea2acfc6a2 | 32 | |
LancasterUniversity | 615:65ea2acfc6a2 | 33 | @returns ble_error_t |
LancasterUniversity | 615:65ea2acfc6a2 | 34 | |
LancasterUniversity | 615:65ea2acfc6a2 | 35 | @retval BLE_ERROR_NONE |
LancasterUniversity | 615:65ea2acfc6a2 | 36 | Everything executed properly |
LancasterUniversity | 615:65ea2acfc6a2 | 37 | |
LancasterUniversity | 615:65ea2acfc6a2 | 38 | @section EXAMPLE |
LancasterUniversity | 615:65ea2acfc6a2 | 39 | |
LancasterUniversity | 615:65ea2acfc6a2 | 40 | @code |
LancasterUniversity | 615:65ea2acfc6a2 | 41 | |
LancasterUniversity | 615:65ea2acfc6a2 | 42 | @endcode |
LancasterUniversity | 615:65ea2acfc6a2 | 43 | */ |
LancasterUniversity | 615:65ea2acfc6a2 | 44 | /**************************************************************************/ |
LancasterUniversity | 615:65ea2acfc6a2 | 45 | ble_error_t nRF5xGattServer::addService(GattService &service) |
LancasterUniversity | 615:65ea2acfc6a2 | 46 | { |
LancasterUniversity | 615:65ea2acfc6a2 | 47 | /* ToDo: Make sure this service UUID doesn't already exist (?) */ |
LancasterUniversity | 615:65ea2acfc6a2 | 48 | /* ToDo: Basic validation */ |
LancasterUniversity | 615:65ea2acfc6a2 | 49 | |
LancasterUniversity | 615:65ea2acfc6a2 | 50 | /* Add the service to the nRF51 */ |
LancasterUniversity | 615:65ea2acfc6a2 | 51 | ble_uuid_t nordicUUID; |
LancasterUniversity | 615:65ea2acfc6a2 | 52 | nordicUUID = custom_convert_to_nordic_uuid(service.getUUID()); |
LancasterUniversity | 615:65ea2acfc6a2 | 53 | |
LancasterUniversity | 615:65ea2acfc6a2 | 54 | uint16_t serviceHandle; |
LancasterUniversity | 615:65ea2acfc6a2 | 55 | ASSERT( ERROR_NONE == |
LancasterUniversity | 615:65ea2acfc6a2 | 56 | sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, |
LancasterUniversity | 615:65ea2acfc6a2 | 57 | &nordicUUID, |
LancasterUniversity | 615:65ea2acfc6a2 | 58 | &serviceHandle), |
LancasterUniversity | 615:65ea2acfc6a2 | 59 | BLE_ERROR_PARAM_OUT_OF_RANGE ); |
LancasterUniversity | 615:65ea2acfc6a2 | 60 | service.setHandle(serviceHandle); |
LancasterUniversity | 615:65ea2acfc6a2 | 61 | |
LancasterUniversity | 615:65ea2acfc6a2 | 62 | /* Add characteristics to the service */ |
LancasterUniversity | 615:65ea2acfc6a2 | 63 | for (uint8_t i = 0; i < service.getCharacteristicCount(); i++) { |
LancasterUniversity | 615:65ea2acfc6a2 | 64 | if (characteristicCount >= BLE_TOTAL_CHARACTERISTICS) { |
LancasterUniversity | 615:65ea2acfc6a2 | 65 | return BLE_ERROR_NO_MEM; |
LancasterUniversity | 615:65ea2acfc6a2 | 66 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 67 | GattCharacteristic *p_char = service.getCharacteristic(i); |
LancasterUniversity | 615:65ea2acfc6a2 | 68 | |
LancasterUniversity | 615:65ea2acfc6a2 | 69 | /* Skip any incompletely defined, read-only characteristics. */ |
LancasterUniversity | 615:65ea2acfc6a2 | 70 | if ((p_char->getValueAttribute().getValuePtr() == NULL) && |
LancasterUniversity | 616:a8f9b022d8fd | 71 | (p_char->getValueAttribute().getLength() == 0) && |
LancasterUniversity | 615:65ea2acfc6a2 | 72 | (p_char->getProperties() == GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ)) { |
LancasterUniversity | 615:65ea2acfc6a2 | 73 | continue; |
LancasterUniversity | 615:65ea2acfc6a2 | 74 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 75 | |
LancasterUniversity | 615:65ea2acfc6a2 | 76 | nordicUUID = custom_convert_to_nordic_uuid(p_char->getValueAttribute().getUUID()); |
LancasterUniversity | 615:65ea2acfc6a2 | 77 | |
LancasterUniversity | 615:65ea2acfc6a2 | 78 | /* The user-description descriptor is a special case which needs to be |
LancasterUniversity | 615:65ea2acfc6a2 | 79 | * handled at the time of adding the characteristic. The following block |
LancasterUniversity | 615:65ea2acfc6a2 | 80 | * is meant to discover its presence. */ |
LancasterUniversity | 615:65ea2acfc6a2 | 81 | const uint8_t *userDescriptionDescriptorValuePtr = NULL; |
LancasterUniversity | 615:65ea2acfc6a2 | 82 | uint16_t userDescriptionDescriptorValueLen = 0; |
LancasterUniversity | 615:65ea2acfc6a2 | 83 | for (uint8_t j = 0; j < p_char->getDescriptorCount(); j++) { |
LancasterUniversity | 615:65ea2acfc6a2 | 84 | GattAttribute *p_desc = p_char->getDescriptor(j); |
LancasterUniversity | 615:65ea2acfc6a2 | 85 | if (p_desc->getUUID() == BLE_UUID_DESCRIPTOR_CHAR_USER_DESC) { |
LancasterUniversity | 615:65ea2acfc6a2 | 86 | userDescriptionDescriptorValuePtr = p_desc->getValuePtr(); |
LancasterUniversity | 615:65ea2acfc6a2 | 87 | userDescriptionDescriptorValueLen = p_desc->getLength(); |
LancasterUniversity | 615:65ea2acfc6a2 | 88 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 89 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 90 | |
LancasterUniversity | 615:65ea2acfc6a2 | 91 | ASSERT ( ERROR_NONE == |
LancasterUniversity | 615:65ea2acfc6a2 | 92 | custom_add_in_characteristic(BLE_GATT_HANDLE_INVALID, |
LancasterUniversity | 615:65ea2acfc6a2 | 93 | &nordicUUID, |
LancasterUniversity | 615:65ea2acfc6a2 | 94 | p_char->getProperties(), |
LancasterUniversity | 615:65ea2acfc6a2 | 95 | p_char->getRequiredSecurity(), |
LancasterUniversity | 615:65ea2acfc6a2 | 96 | p_char->getValueAttribute().getValuePtr(), |
LancasterUniversity | 616:a8f9b022d8fd | 97 | p_char->getValueAttribute().getLength(), |
LancasterUniversity | 615:65ea2acfc6a2 | 98 | p_char->getValueAttribute().getMaxLength(), |
LancasterUniversity | 616:a8f9b022d8fd | 99 | p_char->getValueAttribute().hasVariableLength(), |
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 | 616:a8f9b022d8fd | 130 | p_desc->getLength(), |
LancasterUniversity | 615:65ea2acfc6a2 | 131 | p_desc->getMaxLength(), |
LancasterUniversity | 616:a8f9b022d8fd | 132 | p_desc->hasVariableLength(), |
LancasterUniversity | 615:65ea2acfc6a2 | 133 | &nrfDescriptorHandles[descriptorCount]), |
LancasterUniversity | 615:65ea2acfc6a2 | 134 | BLE_ERROR_PARAM_OUT_OF_RANGE); |
LancasterUniversity | 615:65ea2acfc6a2 | 135 | |
LancasterUniversity | 615:65ea2acfc6a2 | 136 | p_descriptors[descriptorCount++] = p_desc; |
LancasterUniversity | 615:65ea2acfc6a2 | 137 | p_desc->setHandle(nrfDescriptorHandles[descriptorCount]); |
LancasterUniversity | 615:65ea2acfc6a2 | 138 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 139 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 140 | |
LancasterUniversity | 615:65ea2acfc6a2 | 141 | serviceCount++; |
LancasterUniversity | 615:65ea2acfc6a2 | 142 | |
LancasterUniversity | 615:65ea2acfc6a2 | 143 | return BLE_ERROR_NONE; |
LancasterUniversity | 615:65ea2acfc6a2 | 144 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 145 | |
LancasterUniversity | 615:65ea2acfc6a2 | 146 | /**************************************************************************/ |
LancasterUniversity | 615:65ea2acfc6a2 | 147 | /*! |
LancasterUniversity | 615:65ea2acfc6a2 | 148 | @brief Reads the value of a characteristic, based on the service |
LancasterUniversity | 615:65ea2acfc6a2 | 149 | and characteristic index fields |
LancasterUniversity | 615:65ea2acfc6a2 | 150 | |
LancasterUniversity | 615:65ea2acfc6a2 | 151 | @param[in] attributeHandle |
LancasterUniversity | 615:65ea2acfc6a2 | 152 | The handle of the GattCharacteristic to read from |
LancasterUniversity | 615:65ea2acfc6a2 | 153 | @param[in] buffer |
LancasterUniversity | 615:65ea2acfc6a2 | 154 | Buffer to hold the the characteristic's value |
LancasterUniversity | 615:65ea2acfc6a2 | 155 | (raw byte array in LSB format) |
LancasterUniversity | 615:65ea2acfc6a2 | 156 | @param[in/out] len |
LancasterUniversity | 615:65ea2acfc6a2 | 157 | input: Length in bytes to be read. |
LancasterUniversity | 615:65ea2acfc6a2 | 158 | output: Total length of attribute value upon successful return. |
LancasterUniversity | 615:65ea2acfc6a2 | 159 | |
LancasterUniversity | 615:65ea2acfc6a2 | 160 | @returns ble_error_t |
LancasterUniversity | 615:65ea2acfc6a2 | 161 | |
LancasterUniversity | 615:65ea2acfc6a2 | 162 | @retval BLE_ERROR_NONE |
LancasterUniversity | 615:65ea2acfc6a2 | 163 | Everything executed properly |
LancasterUniversity | 615:65ea2acfc6a2 | 164 | */ |
LancasterUniversity | 615:65ea2acfc6a2 | 165 | /**************************************************************************/ |
LancasterUniversity | 615:65ea2acfc6a2 | 166 | ble_error_t nRF5xGattServer::read(GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP) |
LancasterUniversity | 615:65ea2acfc6a2 | 167 | { |
LancasterUniversity | 615:65ea2acfc6a2 | 168 | return read(BLE_CONN_HANDLE_INVALID, attributeHandle, buffer, lengthP); |
LancasterUniversity | 615:65ea2acfc6a2 | 169 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 170 | |
LancasterUniversity | 615:65ea2acfc6a2 | 171 | ble_error_t nRF5xGattServer::read(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP) |
LancasterUniversity | 615:65ea2acfc6a2 | 172 | { |
LancasterUniversity | 615:65ea2acfc6a2 | 173 | ble_gatts_value_t value = { |
LancasterUniversity | 615:65ea2acfc6a2 | 174 | .len = *lengthP, |
LancasterUniversity | 615:65ea2acfc6a2 | 175 | .offset = 0, |
LancasterUniversity | 615:65ea2acfc6a2 | 176 | .p_value = buffer, |
LancasterUniversity | 615:65ea2acfc6a2 | 177 | }; |
LancasterUniversity | 615:65ea2acfc6a2 | 178 | |
LancasterUniversity | 615:65ea2acfc6a2 | 179 | ASSERT( ERROR_NONE == |
LancasterUniversity | 615:65ea2acfc6a2 | 180 | sd_ble_gatts_value_get(connectionHandle, attributeHandle, &value), |
LancasterUniversity | 615:65ea2acfc6a2 | 181 | BLE_ERROR_PARAM_OUT_OF_RANGE); |
LancasterUniversity | 615:65ea2acfc6a2 | 182 | *lengthP = value.len; |
LancasterUniversity | 615:65ea2acfc6a2 | 183 | |
LancasterUniversity | 615:65ea2acfc6a2 | 184 | return BLE_ERROR_NONE; |
LancasterUniversity | 615:65ea2acfc6a2 | 185 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 186 | |
LancasterUniversity | 615:65ea2acfc6a2 | 187 | /**************************************************************************/ |
LancasterUniversity | 615:65ea2acfc6a2 | 188 | /*! |
LancasterUniversity | 615:65ea2acfc6a2 | 189 | @brief Updates the value of a characteristic, based on the service |
LancasterUniversity | 615:65ea2acfc6a2 | 190 | and characteristic index fields |
LancasterUniversity | 615:65ea2acfc6a2 | 191 | |
LancasterUniversity | 615:65ea2acfc6a2 | 192 | @param[in] charHandle |
LancasterUniversity | 615:65ea2acfc6a2 | 193 | The handle of the GattCharacteristic to write to |
LancasterUniversity | 615:65ea2acfc6a2 | 194 | @param[in] buffer |
LancasterUniversity | 615:65ea2acfc6a2 | 195 | Data to use when updating the characteristic's value |
LancasterUniversity | 615:65ea2acfc6a2 | 196 | (raw byte array in LSB format) |
LancasterUniversity | 615:65ea2acfc6a2 | 197 | @param[in] len |
LancasterUniversity | 615:65ea2acfc6a2 | 198 | The number of bytes in buffer |
LancasterUniversity | 615:65ea2acfc6a2 | 199 | |
LancasterUniversity | 615:65ea2acfc6a2 | 200 | @returns ble_error_t |
LancasterUniversity | 615:65ea2acfc6a2 | 201 | |
LancasterUniversity | 615:65ea2acfc6a2 | 202 | @retval BLE_ERROR_NONE |
LancasterUniversity | 615:65ea2acfc6a2 | 203 | Everything executed properly |
LancasterUniversity | 615:65ea2acfc6a2 | 204 | */ |
LancasterUniversity | 615:65ea2acfc6a2 | 205 | /**************************************************************************/ |
LancasterUniversity | 615:65ea2acfc6a2 | 206 | ble_error_t nRF5xGattServer::write(GattAttribute::Handle_t attributeHandle, const uint8_t buffer[], uint16_t len, bool localOnly) |
LancasterUniversity | 615:65ea2acfc6a2 | 207 | { |
LancasterUniversity | 615:65ea2acfc6a2 | 208 | return write(BLE_CONN_HANDLE_INVALID, attributeHandle, buffer, len, localOnly); |
LancasterUniversity | 615:65ea2acfc6a2 | 209 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 210 | |
LancasterUniversity | 615:65ea2acfc6a2 | 211 | 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 | 212 | { |
LancasterUniversity | 615:65ea2acfc6a2 | 213 | ble_error_t returnValue = BLE_ERROR_NONE; |
LancasterUniversity | 615:65ea2acfc6a2 | 214 | |
LancasterUniversity | 615:65ea2acfc6a2 | 215 | ble_gatts_value_t value = { |
LancasterUniversity | 615:65ea2acfc6a2 | 216 | .len = len, |
LancasterUniversity | 615:65ea2acfc6a2 | 217 | .offset = 0, |
LancasterUniversity | 615:65ea2acfc6a2 | 218 | .p_value = const_cast<uint8_t *>(buffer), |
LancasterUniversity | 615:65ea2acfc6a2 | 219 | }; |
LancasterUniversity | 615:65ea2acfc6a2 | 220 | |
LancasterUniversity | 615:65ea2acfc6a2 | 221 | if (localOnly) { |
LancasterUniversity | 615:65ea2acfc6a2 | 222 | /* Only update locally regardless of notify/indicate */ |
LancasterUniversity | 615:65ea2acfc6a2 | 223 | ASSERT_INT( ERROR_NONE, |
LancasterUniversity | 615:65ea2acfc6a2 | 224 | sd_ble_gatts_value_set(connectionHandle, attributeHandle, &value), |
LancasterUniversity | 615:65ea2acfc6a2 | 225 | BLE_ERROR_PARAM_OUT_OF_RANGE ); |
LancasterUniversity | 615:65ea2acfc6a2 | 226 | return BLE_ERROR_NONE; |
LancasterUniversity | 615:65ea2acfc6a2 | 227 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 228 | |
LancasterUniversity | 615:65ea2acfc6a2 | 229 | int characteristicIndex = resolveValueHandleToCharIndex(attributeHandle); |
LancasterUniversity | 615:65ea2acfc6a2 | 230 | if ((characteristicIndex != -1) && |
LancasterUniversity | 615:65ea2acfc6a2 | 231 | (p_characteristics[characteristicIndex]->getProperties() & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY))) { |
LancasterUniversity | 615:65ea2acfc6a2 | 232 | /* HVX update for the characteristic value */ |
LancasterUniversity | 615:65ea2acfc6a2 | 233 | ble_gatts_hvx_params_t hvx_params; |
LancasterUniversity | 615:65ea2acfc6a2 | 234 | |
LancasterUniversity | 615:65ea2acfc6a2 | 235 | hvx_params.handle = attributeHandle; |
LancasterUniversity | 615:65ea2acfc6a2 | 236 | hvx_params.type = |
LancasterUniversity | 615:65ea2acfc6a2 | 237 | (p_characteristics[characteristicIndex]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) ? BLE_GATT_HVX_NOTIFICATION : BLE_GATT_HVX_INDICATION; |
LancasterUniversity | 615:65ea2acfc6a2 | 238 | hvx_params.offset = 0; |
LancasterUniversity | 615:65ea2acfc6a2 | 239 | hvx_params.p_data = const_cast<uint8_t *>(buffer); |
LancasterUniversity | 615:65ea2acfc6a2 | 240 | hvx_params.p_len = &len; |
LancasterUniversity | 615:65ea2acfc6a2 | 241 | |
LancasterUniversity | 615:65ea2acfc6a2 | 242 | if (connectionHandle == BLE_CONN_HANDLE_INVALID) { /* use the default connection handle if the caller hasn't specified a valid connectionHandle. */ |
LancasterUniversity | 616:a8f9b022d8fd | 243 | nRF5xGap &gap = (nRF5xGap &) nRF5xn::Instance(BLE::DEFAULT_INSTANCE).getGap(); |
LancasterUniversity | 616:a8f9b022d8fd | 244 | connectionHandle = gap.getConnectionHandle(); |
LancasterUniversity | 615:65ea2acfc6a2 | 245 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 246 | error_t error = (error_t) sd_ble_gatts_hvx(connectionHandle, &hvx_params); |
LancasterUniversity | 615:65ea2acfc6a2 | 247 | if (error != ERROR_NONE) { |
LancasterUniversity | 615:65ea2acfc6a2 | 248 | switch (error) { |
LancasterUniversity | 615:65ea2acfc6a2 | 249 | case ERROR_BLE_NO_TX_BUFFERS: /* Notifications consume application buffers. The return value can be used for resending notifications. */ |
LancasterUniversity | 615:65ea2acfc6a2 | 250 | case ERROR_BUSY: |
LancasterUniversity | 615:65ea2acfc6a2 | 251 | returnValue = BLE_STACK_BUSY; |
LancasterUniversity | 615:65ea2acfc6a2 | 252 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 253 | |
LancasterUniversity | 615:65ea2acfc6a2 | 254 | case ERROR_INVALID_STATE: |
LancasterUniversity | 615:65ea2acfc6a2 | 255 | case ERROR_BLEGATTS_SYS_ATTR_MISSING: |
LancasterUniversity | 615:65ea2acfc6a2 | 256 | returnValue = BLE_ERROR_INVALID_STATE; |
LancasterUniversity | 615:65ea2acfc6a2 | 257 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 258 | |
LancasterUniversity | 615:65ea2acfc6a2 | 259 | default : |
LancasterUniversity | 615:65ea2acfc6a2 | 260 | ASSERT_INT( ERROR_NONE, |
LancasterUniversity | 615:65ea2acfc6a2 | 261 | sd_ble_gatts_value_set(connectionHandle, attributeHandle, &value), |
LancasterUniversity | 615:65ea2acfc6a2 | 262 | BLE_ERROR_PARAM_OUT_OF_RANGE ); |
LancasterUniversity | 615:65ea2acfc6a2 | 263 | |
LancasterUniversity | 615:65ea2acfc6a2 | 264 | /* Notifications consume application buffers. The return value can |
LancasterUniversity | 615:65ea2acfc6a2 | 265 | * be used for resending notifications. */ |
LancasterUniversity | 615:65ea2acfc6a2 | 266 | returnValue = BLE_STACK_BUSY; |
LancasterUniversity | 615:65ea2acfc6a2 | 267 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 268 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 269 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 270 | } else { |
LancasterUniversity | 615:65ea2acfc6a2 | 271 | returnValue = BLE_ERROR_INVALID_STATE; // if assert is not used |
LancasterUniversity | 615:65ea2acfc6a2 | 272 | ASSERT_INT( ERROR_NONE, |
LancasterUniversity | 615:65ea2acfc6a2 | 273 | sd_ble_gatts_value_set(connectionHandle, attributeHandle, &value), |
LancasterUniversity | 615:65ea2acfc6a2 | 274 | BLE_ERROR_PARAM_OUT_OF_RANGE ); |
LancasterUniversity | 615:65ea2acfc6a2 | 275 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 276 | |
LancasterUniversity | 615:65ea2acfc6a2 | 277 | return returnValue; |
LancasterUniversity | 615:65ea2acfc6a2 | 278 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 279 | |
LancasterUniversity | 616:a8f9b022d8fd | 280 | /** |
LancasterUniversity | 616:a8f9b022d8fd | 281 | * Perform an explicit BLE notification of a given attribute. |
LancasterUniversity | 616:a8f9b022d8fd | 282 | * |
LancasterUniversity | 616:a8f9b022d8fd | 283 | * @param[in] attributeHandle |
LancasterUniversity | 616:a8f9b022d8fd | 284 | * Handle for the value attribute of the Characteristic. |
LancasterUniversity | 616:a8f9b022d8fd | 285 | * @param[in] value |
LancasterUniversity | 616:a8f9b022d8fd | 286 | * A pointer to a buffer holding the new value |
LancasterUniversity | 616:a8f9b022d8fd | 287 | * @param[in] size |
LancasterUniversity | 616:a8f9b022d8fd | 288 | * Size of the new value (in bytes). |
LancasterUniversity | 616:a8f9b022d8fd | 289 | * |
LancasterUniversity | 616:a8f9b022d8fd | 290 | * @return BLE_ERROR_NONE if we have successfully set the value of the attribute. |
LancasterUniversity | 616:a8f9b022d8fd | 291 | */ |
LancasterUniversity | 616:a8f9b022d8fd | 292 | ble_error_t nRF5xGattServer::notify(GattAttribute::Handle_t attributeHandle, const uint8_t buffer[], uint16_t len) |
LancasterUniversity | 616:a8f9b022d8fd | 293 | { |
LancasterUniversity | 616:a8f9b022d8fd | 294 | uint16_t gapConnectionHandle = ((nRF5xGap &)nRF5xn::Instance(BLE::DEFAULT_INSTANCE).getGap()).getConnectionHandle(); |
LancasterUniversity | 616:a8f9b022d8fd | 295 | ble_gatts_hvx_params_t hvx_params; |
LancasterUniversity | 616:a8f9b022d8fd | 296 | |
LancasterUniversity | 616:a8f9b022d8fd | 297 | hvx_params.handle = attributeHandle; |
LancasterUniversity | 616:a8f9b022d8fd | 298 | hvx_params.type = BLE_GATT_HVX_NOTIFICATION; |
LancasterUniversity | 616:a8f9b022d8fd | 299 | hvx_params.offset = 0; |
LancasterUniversity | 616:a8f9b022d8fd | 300 | hvx_params.p_data = const_cast<uint8_t *>(buffer); |
LancasterUniversity | 616:a8f9b022d8fd | 301 | hvx_params.p_len = &len; |
LancasterUniversity | 616:a8f9b022d8fd | 302 | |
LancasterUniversity | 616:a8f9b022d8fd | 303 | error_t error = (error_t) sd_ble_gatts_hvx(gapConnectionHandle, &hvx_params); |
LancasterUniversity | 616:a8f9b022d8fd | 304 | |
LancasterUniversity | 616:a8f9b022d8fd | 305 | if (error == ERROR_NONE) |
LancasterUniversity | 616:a8f9b022d8fd | 306 | return BLE_ERROR_NONE; |
LancasterUniversity | 616:a8f9b022d8fd | 307 | else |
LancasterUniversity | 616:a8f9b022d8fd | 308 | return BLE_STACK_BUSY; |
LancasterUniversity | 616:a8f9b022d8fd | 309 | } |
LancasterUniversity | 616:a8f9b022d8fd | 310 | |
LancasterUniversity | 616:a8f9b022d8fd | 311 | |
LancasterUniversity | 615:65ea2acfc6a2 | 312 | ble_error_t nRF5xGattServer::areUpdatesEnabled(const GattCharacteristic &characteristic, bool *enabledP) |
LancasterUniversity | 615:65ea2acfc6a2 | 313 | { |
LancasterUniversity | 615:65ea2acfc6a2 | 314 | /* Forward the call with the default connection handle. */ |
LancasterUniversity | 616:a8f9b022d8fd | 315 | nRF5xGap &gap = (nRF5xGap &) nRF5xn::Instance(BLE::DEFAULT_INSTANCE).getGap(); |
LancasterUniversity | 616:a8f9b022d8fd | 316 | return areUpdatesEnabled(gap.getConnectionHandle(), characteristic, enabledP); |
LancasterUniversity | 615:65ea2acfc6a2 | 317 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 318 | |
LancasterUniversity | 615:65ea2acfc6a2 | 319 | ble_error_t nRF5xGattServer::areUpdatesEnabled(Gap::Handle_t connectionHandle, const GattCharacteristic &characteristic, bool *enabledP) |
LancasterUniversity | 615:65ea2acfc6a2 | 320 | { |
LancasterUniversity | 615:65ea2acfc6a2 | 321 | int characteristicIndex = resolveValueHandleToCharIndex(characteristic.getValueHandle()); |
LancasterUniversity | 615:65ea2acfc6a2 | 322 | if (characteristicIndex == -1) { |
LancasterUniversity | 615:65ea2acfc6a2 | 323 | return BLE_ERROR_INVALID_PARAM; |
LancasterUniversity | 615:65ea2acfc6a2 | 324 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 325 | |
LancasterUniversity | 615:65ea2acfc6a2 | 326 | /* Read the cccd value from the GATT server. */ |
LancasterUniversity | 615:65ea2acfc6a2 | 327 | GattAttribute::Handle_t cccdHandle = nrfCharacteristicHandles[characteristicIndex].cccd_handle; |
LancasterUniversity | 615:65ea2acfc6a2 | 328 | uint16_t cccdValue; |
LancasterUniversity | 615:65ea2acfc6a2 | 329 | uint16_t length = sizeof(cccdValue); |
LancasterUniversity | 615:65ea2acfc6a2 | 330 | ble_error_t rc = read(connectionHandle, cccdHandle, reinterpret_cast<uint8_t *>(&cccdValue), &length); |
LancasterUniversity | 615:65ea2acfc6a2 | 331 | if (rc != BLE_ERROR_NONE) { |
LancasterUniversity | 615:65ea2acfc6a2 | 332 | return rc; |
LancasterUniversity | 615:65ea2acfc6a2 | 333 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 334 | if (length != sizeof(cccdValue)) { |
LancasterUniversity | 615:65ea2acfc6a2 | 335 | return BLE_ERROR_INVALID_STATE; |
LancasterUniversity | 615:65ea2acfc6a2 | 336 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 337 | |
LancasterUniversity | 615:65ea2acfc6a2 | 338 | /* Check for NOTFICATION or INDICATION in CCCD. */ |
LancasterUniversity | 615:65ea2acfc6a2 | 339 | if ((cccdValue & BLE_GATT_HVX_NOTIFICATION) || (cccdValue & BLE_GATT_HVX_INDICATION)) { |
LancasterUniversity | 615:65ea2acfc6a2 | 340 | *enabledP = true; |
LancasterUniversity | 615:65ea2acfc6a2 | 341 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 342 | |
LancasterUniversity | 615:65ea2acfc6a2 | 343 | return BLE_ERROR_NONE; |
LancasterUniversity | 615:65ea2acfc6a2 | 344 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 345 | |
LancasterUniversity | 615:65ea2acfc6a2 | 346 | /**************************************************************************/ |
LancasterUniversity | 615:65ea2acfc6a2 | 347 | /*! |
LancasterUniversity | 616:a8f9b022d8fd | 348 | @brief Clear nRF5xGattServer's state. |
LancasterUniversity | 616:a8f9b022d8fd | 349 | |
LancasterUniversity | 616:a8f9b022d8fd | 350 | @returns ble_error_t |
LancasterUniversity | 616:a8f9b022d8fd | 351 | |
LancasterUniversity | 616:a8f9b022d8fd | 352 | @retval BLE_ERROR_NONE |
LancasterUniversity | 616:a8f9b022d8fd | 353 | Everything executed properly |
LancasterUniversity | 616:a8f9b022d8fd | 354 | */ |
LancasterUniversity | 616:a8f9b022d8fd | 355 | /**************************************************************************/ |
LancasterUniversity | 616:a8f9b022d8fd | 356 | ble_error_t nRF5xGattServer::reset(void) |
LancasterUniversity | 616:a8f9b022d8fd | 357 | { |
LancasterUniversity | 616:a8f9b022d8fd | 358 | /* Clear all state that is from the parent, including private members */ |
LancasterUniversity | 616:a8f9b022d8fd | 359 | if (GattServer::reset() != BLE_ERROR_NONE) { |
LancasterUniversity | 616:a8f9b022d8fd | 360 | return BLE_ERROR_INVALID_STATE; |
LancasterUniversity | 616:a8f9b022d8fd | 361 | } |
LancasterUniversity | 616:a8f9b022d8fd | 362 | |
LancasterUniversity | 616:a8f9b022d8fd | 363 | /* Clear derived class members */ |
LancasterUniversity | 616:a8f9b022d8fd | 364 | memset(p_characteristics, 0, sizeof(p_characteristics)); |
LancasterUniversity | 616:a8f9b022d8fd | 365 | memset(p_descriptors, 0, sizeof(p_descriptors)); |
LancasterUniversity | 616:a8f9b022d8fd | 366 | memset(nrfCharacteristicHandles, 0, sizeof(ble_gatts_char_handles_t)); |
LancasterUniversity | 616:a8f9b022d8fd | 367 | memset(nrfDescriptorHandles, 0, sizeof(nrfDescriptorHandles)); |
LancasterUniversity | 616:a8f9b022d8fd | 368 | descriptorCount = 0; |
LancasterUniversity | 616:a8f9b022d8fd | 369 | |
LancasterUniversity | 616:a8f9b022d8fd | 370 | return BLE_ERROR_NONE; |
LancasterUniversity | 616:a8f9b022d8fd | 371 | } |
LancasterUniversity | 616:a8f9b022d8fd | 372 | |
LancasterUniversity | 616:a8f9b022d8fd | 373 | /**************************************************************************/ |
LancasterUniversity | 616:a8f9b022d8fd | 374 | /*! |
LancasterUniversity | 615:65ea2acfc6a2 | 375 | @brief Callback handler for events getting pushed up from the SD |
LancasterUniversity | 615:65ea2acfc6a2 | 376 | */ |
LancasterUniversity | 615:65ea2acfc6a2 | 377 | /**************************************************************************/ |
LancasterUniversity | 615:65ea2acfc6a2 | 378 | void nRF5xGattServer::hwCallback(ble_evt_t *p_ble_evt) |
LancasterUniversity | 615:65ea2acfc6a2 | 379 | { |
LancasterUniversity | 615:65ea2acfc6a2 | 380 | GattAttribute::Handle_t handle_value; |
LancasterUniversity | 615:65ea2acfc6a2 | 381 | GattServerEvents::gattEvent_t eventType; |
LancasterUniversity | 615:65ea2acfc6a2 | 382 | const ble_gatts_evt_t *gattsEventP = &p_ble_evt->evt.gatts_evt; |
LancasterUniversity | 615:65ea2acfc6a2 | 383 | |
LancasterUniversity | 615:65ea2acfc6a2 | 384 | switch (p_ble_evt->header.evt_id) { |
LancasterUniversity | 615:65ea2acfc6a2 | 385 | case BLE_GATTS_EVT_WRITE: { |
LancasterUniversity | 615:65ea2acfc6a2 | 386 | /* There are 2 use case here: Values being updated & CCCD (indicate/notify) enabled */ |
LancasterUniversity | 615:65ea2acfc6a2 | 387 | |
LancasterUniversity | 615:65ea2acfc6a2 | 388 | /* 1.) Handle CCCD changes */ |
LancasterUniversity | 615:65ea2acfc6a2 | 389 | handle_value = gattsEventP->params.write.handle; |
LancasterUniversity | 615:65ea2acfc6a2 | 390 | int characteristicIndex = resolveCCCDHandleToCharIndex(handle_value); |
LancasterUniversity | 615:65ea2acfc6a2 | 391 | if ((characteristicIndex != -1) && |
LancasterUniversity | 615:65ea2acfc6a2 | 392 | (p_characteristics[characteristicIndex]->getProperties() & |
LancasterUniversity | 615:65ea2acfc6a2 | 393 | (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY))) { |
LancasterUniversity | 615:65ea2acfc6a2 | 394 | |
LancasterUniversity | 615:65ea2acfc6a2 | 395 | 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 | 396 | |
LancasterUniversity | 615:65ea2acfc6a2 | 397 | if (((p_characteristics[characteristicIndex]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE) && (cccd_value & BLE_GATT_HVX_INDICATION)) || |
LancasterUniversity | 615:65ea2acfc6a2 | 398 | ((p_characteristics[characteristicIndex]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) && (cccd_value & BLE_GATT_HVX_NOTIFICATION))) { |
LancasterUniversity | 615:65ea2acfc6a2 | 399 | eventType = GattServerEvents::GATT_EVENT_UPDATES_ENABLED; |
LancasterUniversity | 615:65ea2acfc6a2 | 400 | } else { |
LancasterUniversity | 615:65ea2acfc6a2 | 401 | eventType = GattServerEvents::GATT_EVENT_UPDATES_DISABLED; |
LancasterUniversity | 615:65ea2acfc6a2 | 402 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 403 | |
LancasterUniversity | 615:65ea2acfc6a2 | 404 | handleEvent(eventType, p_characteristics[characteristicIndex]->getValueHandle()); |
LancasterUniversity | 615:65ea2acfc6a2 | 405 | return; |
LancasterUniversity | 615:65ea2acfc6a2 | 406 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 407 | |
LancasterUniversity | 615:65ea2acfc6a2 | 408 | /* 2.) Changes to the characteristic value will be handled with other events below */ |
LancasterUniversity | 615:65ea2acfc6a2 | 409 | eventType = GattServerEvents::GATT_EVENT_DATA_WRITTEN; |
LancasterUniversity | 615:65ea2acfc6a2 | 410 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 411 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 412 | |
LancasterUniversity | 615:65ea2acfc6a2 | 413 | case BLE_GATTS_EVT_HVC: |
LancasterUniversity | 615:65ea2acfc6a2 | 414 | /* Indication confirmation received */ |
LancasterUniversity | 615:65ea2acfc6a2 | 415 | eventType = GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED; |
LancasterUniversity | 615:65ea2acfc6a2 | 416 | handle_value = gattsEventP->params.hvc.handle; |
LancasterUniversity | 615:65ea2acfc6a2 | 417 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 418 | |
LancasterUniversity | 615:65ea2acfc6a2 | 419 | case BLE_EVT_TX_COMPLETE: { |
LancasterUniversity | 615:65ea2acfc6a2 | 420 | handleDataSentEvent(p_ble_evt->evt.common_evt.params.tx_complete.count); |
LancasterUniversity | 615:65ea2acfc6a2 | 421 | return; |
LancasterUniversity | 615:65ea2acfc6a2 | 422 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 423 | |
LancasterUniversity | 616:a8f9b022d8fd | 424 | case BLE_GATTS_EVT_SYS_ATTR_MISSING: |
LancasterUniversity | 616:a8f9b022d8fd | 425 | case BLE_GAP_EVT_CONN_SEC_UPDATE: |
LancasterUniversity | 616:a8f9b022d8fd | 426 | { |
LancasterUniversity | 616:a8f9b022d8fd | 427 | GattSysAttrMissingCallbackParams cbParams = {gattsEventP->conn_handle}; |
LancasterUniversity | 616:a8f9b022d8fd | 428 | handleSysAttrMissingEvent(&cbParams); |
LancasterUniversity | 615:65ea2acfc6a2 | 429 | return; |
LancasterUniversity | 616:a8f9b022d8fd | 430 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 431 | |
LancasterUniversity | 615:65ea2acfc6a2 | 432 | case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST: |
LancasterUniversity | 615:65ea2acfc6a2 | 433 | switch (gattsEventP->params.authorize_request.type) { |
LancasterUniversity | 615:65ea2acfc6a2 | 434 | case BLE_GATTS_AUTHORIZE_TYPE_READ: |
LancasterUniversity | 615:65ea2acfc6a2 | 435 | eventType = GattServerEvents::GATT_EVENT_READ_AUTHORIZATION_REQ; |
LancasterUniversity | 615:65ea2acfc6a2 | 436 | handle_value = gattsEventP->params.authorize_request.request.read.handle; |
LancasterUniversity | 615:65ea2acfc6a2 | 437 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 438 | case BLE_GATTS_AUTHORIZE_TYPE_WRITE: |
LancasterUniversity | 615:65ea2acfc6a2 | 439 | eventType = GattServerEvents::GATT_EVENT_WRITE_AUTHORIZATION_REQ; |
LancasterUniversity | 615:65ea2acfc6a2 | 440 | handle_value = gattsEventP->params.authorize_request.request.write.handle; |
LancasterUniversity | 615:65ea2acfc6a2 | 441 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 442 | default: |
LancasterUniversity | 615:65ea2acfc6a2 | 443 | return; |
LancasterUniversity | 615:65ea2acfc6a2 | 444 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 445 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 446 | |
LancasterUniversity | 615:65ea2acfc6a2 | 447 | default: |
LancasterUniversity | 615:65ea2acfc6a2 | 448 | return; |
LancasterUniversity | 615:65ea2acfc6a2 | 449 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 450 | |
LancasterUniversity | 615:65ea2acfc6a2 | 451 | int characteristicIndex = resolveValueHandleToCharIndex(handle_value); |
LancasterUniversity | 615:65ea2acfc6a2 | 452 | if (characteristicIndex == -1) { |
LancasterUniversity | 615:65ea2acfc6a2 | 453 | return; |
LancasterUniversity | 615:65ea2acfc6a2 | 454 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 455 | |
LancasterUniversity | 615:65ea2acfc6a2 | 456 | /* Find index (charHandle) in the pool */ |
LancasterUniversity | 615:65ea2acfc6a2 | 457 | switch (eventType) { |
LancasterUniversity | 615:65ea2acfc6a2 | 458 | case GattServerEvents::GATT_EVENT_DATA_WRITTEN: { |
LancasterUniversity | 615:65ea2acfc6a2 | 459 | GattWriteCallbackParams cbParams = { |
LancasterUniversity | 615:65ea2acfc6a2 | 460 | .connHandle = gattsEventP->conn_handle, |
LancasterUniversity | 615:65ea2acfc6a2 | 461 | .handle = handle_value, |
LancasterUniversity | 615:65ea2acfc6a2 | 462 | .writeOp = static_cast<GattWriteCallbackParams::WriteOp_t>(gattsEventP->params.write.op), |
LancasterUniversity | 615:65ea2acfc6a2 | 463 | .offset = gattsEventP->params.write.offset, |
LancasterUniversity | 615:65ea2acfc6a2 | 464 | .len = gattsEventP->params.write.len, |
LancasterUniversity | 615:65ea2acfc6a2 | 465 | .data = gattsEventP->params.write.data |
LancasterUniversity | 615:65ea2acfc6a2 | 466 | }; |
LancasterUniversity | 615:65ea2acfc6a2 | 467 | handleDataWrittenEvent(&cbParams); |
LancasterUniversity | 615:65ea2acfc6a2 | 468 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 469 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 470 | case GattServerEvents::GATT_EVENT_WRITE_AUTHORIZATION_REQ: { |
LancasterUniversity | 615:65ea2acfc6a2 | 471 | GattWriteAuthCallbackParams cbParams = { |
LancasterUniversity | 615:65ea2acfc6a2 | 472 | .connHandle = gattsEventP->conn_handle, |
LancasterUniversity | 615:65ea2acfc6a2 | 473 | .handle = handle_value, |
LancasterUniversity | 615:65ea2acfc6a2 | 474 | .offset = gattsEventP->params.authorize_request.request.write.offset, |
LancasterUniversity | 615:65ea2acfc6a2 | 475 | .len = gattsEventP->params.authorize_request.request.write.len, |
LancasterUniversity | 615:65ea2acfc6a2 | 476 | .data = gattsEventP->params.authorize_request.request.write.data, |
LancasterUniversity | 615:65ea2acfc6a2 | 477 | .authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS /* the callback handler must leave this member |
LancasterUniversity | 615:65ea2acfc6a2 | 478 | * set to AUTH_CALLBACK_REPLY_SUCCESS if the client |
LancasterUniversity | 615:65ea2acfc6a2 | 479 | * request is to proceed. */ |
LancasterUniversity | 615:65ea2acfc6a2 | 480 | }; |
LancasterUniversity | 615:65ea2acfc6a2 | 481 | ble_gatts_rw_authorize_reply_params_t reply = { |
LancasterUniversity | 615:65ea2acfc6a2 | 482 | .type = BLE_GATTS_AUTHORIZE_TYPE_WRITE, |
LancasterUniversity | 615:65ea2acfc6a2 | 483 | .params = { |
LancasterUniversity | 615:65ea2acfc6a2 | 484 | .write = { |
LancasterUniversity | 615:65ea2acfc6a2 | 485 | .gatt_status = p_characteristics[characteristicIndex]->authorizeWrite(&cbParams) |
LancasterUniversity | 615:65ea2acfc6a2 | 486 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 487 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 488 | }; |
LancasterUniversity | 615:65ea2acfc6a2 | 489 | sd_ble_gatts_rw_authorize_reply(gattsEventP->conn_handle, &reply); |
LancasterUniversity | 615:65ea2acfc6a2 | 490 | |
LancasterUniversity | 615:65ea2acfc6a2 | 491 | /* |
LancasterUniversity | 615:65ea2acfc6a2 | 492 | * If write-authorization is enabled for a characteristic, |
LancasterUniversity | 615:65ea2acfc6a2 | 493 | * AUTHORIZATION_REQ event (if replied with true) is *not* |
LancasterUniversity | 615:65ea2acfc6a2 | 494 | * followed by another DATA_WRITTEN event; so we still need |
LancasterUniversity | 615:65ea2acfc6a2 | 495 | * to invoke handleDataWritten(), much the same as we would |
LancasterUniversity | 615:65ea2acfc6a2 | 496 | * have done if write-authorization had not been enabled. |
LancasterUniversity | 615:65ea2acfc6a2 | 497 | */ |
LancasterUniversity | 615:65ea2acfc6a2 | 498 | if (reply.params.write.gatt_status == BLE_GATT_STATUS_SUCCESS) { |
LancasterUniversity | 615:65ea2acfc6a2 | 499 | GattWriteCallbackParams cbParams = { |
LancasterUniversity | 615:65ea2acfc6a2 | 500 | .connHandle = gattsEventP->conn_handle, |
LancasterUniversity | 615:65ea2acfc6a2 | 501 | .handle = handle_value, |
LancasterUniversity | 615:65ea2acfc6a2 | 502 | .writeOp = static_cast<GattWriteCallbackParams::WriteOp_t>(gattsEventP->params.authorize_request.request.write.op), |
LancasterUniversity | 615:65ea2acfc6a2 | 503 | .offset = gattsEventP->params.authorize_request.request.write.offset, |
LancasterUniversity | 615:65ea2acfc6a2 | 504 | .len = gattsEventP->params.authorize_request.request.write.len, |
LancasterUniversity | 615:65ea2acfc6a2 | 505 | .data = gattsEventP->params.authorize_request.request.write.data, |
LancasterUniversity | 615:65ea2acfc6a2 | 506 | }; |
LancasterUniversity | 615:65ea2acfc6a2 | 507 | handleDataWrittenEvent(&cbParams); |
LancasterUniversity | 615:65ea2acfc6a2 | 508 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 509 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 510 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 511 | case GattServerEvents::GATT_EVENT_READ_AUTHORIZATION_REQ: { |
LancasterUniversity | 615:65ea2acfc6a2 | 512 | GattReadAuthCallbackParams cbParams = { |
LancasterUniversity | 615:65ea2acfc6a2 | 513 | .connHandle = gattsEventP->conn_handle, |
LancasterUniversity | 615:65ea2acfc6a2 | 514 | .handle = handle_value, |
LancasterUniversity | 615:65ea2acfc6a2 | 515 | .offset = gattsEventP->params.authorize_request.request.read.offset, |
LancasterUniversity | 615:65ea2acfc6a2 | 516 | .len = 0, |
LancasterUniversity | 615:65ea2acfc6a2 | 517 | .data = NULL, |
LancasterUniversity | 615:65ea2acfc6a2 | 518 | .authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS /* the callback handler must leave this member |
LancasterUniversity | 615:65ea2acfc6a2 | 519 | * set to AUTH_CALLBACK_REPLY_SUCCESS if the client |
LancasterUniversity | 615:65ea2acfc6a2 | 520 | * request is to proceed. */ |
LancasterUniversity | 615:65ea2acfc6a2 | 521 | }; |
LancasterUniversity | 615:65ea2acfc6a2 | 522 | |
LancasterUniversity | 615:65ea2acfc6a2 | 523 | ble_gatts_rw_authorize_reply_params_t reply = { |
LancasterUniversity | 615:65ea2acfc6a2 | 524 | .type = BLE_GATTS_AUTHORIZE_TYPE_READ, |
LancasterUniversity | 615:65ea2acfc6a2 | 525 | .params = { |
LancasterUniversity | 615:65ea2acfc6a2 | 526 | .read = { |
LancasterUniversity | 615:65ea2acfc6a2 | 527 | .gatt_status = p_characteristics[characteristicIndex]->authorizeRead(&cbParams) |
LancasterUniversity | 615:65ea2acfc6a2 | 528 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 529 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 530 | }; |
LancasterUniversity | 615:65ea2acfc6a2 | 531 | |
LancasterUniversity | 615:65ea2acfc6a2 | 532 | if (cbParams.authorizationReply == BLE_GATT_STATUS_SUCCESS) { |
LancasterUniversity | 615:65ea2acfc6a2 | 533 | if (cbParams.data != NULL) { |
LancasterUniversity | 615:65ea2acfc6a2 | 534 | reply.params.read.update = 1; |
LancasterUniversity | 615:65ea2acfc6a2 | 535 | reply.params.read.offset = cbParams.offset; |
LancasterUniversity | 615:65ea2acfc6a2 | 536 | reply.params.read.len = cbParams.len; |
LancasterUniversity | 615:65ea2acfc6a2 | 537 | reply.params.read.p_data = cbParams.data; |
LancasterUniversity | 615:65ea2acfc6a2 | 538 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 539 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 540 | |
LancasterUniversity | 615:65ea2acfc6a2 | 541 | sd_ble_gatts_rw_authorize_reply(gattsEventP->conn_handle, &reply); |
LancasterUniversity | 615:65ea2acfc6a2 | 542 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 543 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 544 | |
LancasterUniversity | 615:65ea2acfc6a2 | 545 | default: |
LancasterUniversity | 615:65ea2acfc6a2 | 546 | handleEvent(eventType, handle_value); |
LancasterUniversity | 615:65ea2acfc6a2 | 547 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 548 | } |
rgrover1 | 388:db85a09c27ef | 549 | } |