fdsf
Fork of nRF51822 by
source/nRF5xCharacteristicDescriptorDiscoverer.cpp@569:9e72aa06ec32, 2016-01-11 (annotated)
- Committer:
- vcoubard
- Date:
- Mon Jan 11 10:19:20 2016 +0000
- Revision:
- 569:9e72aa06ec32
- Parent:
- 568:13b23a4b1f58
Synchronized with git rev 15592642
Author: Vincent Coubard
Move Implementation of nRF5xCharacteristicDescriptorDiscoverer::Discovery
to cpp file.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vcoubard | 563:9c4b96f7be8d | 1 | /* mbed Microcontroller Library |
vcoubard | 563:9c4b96f7be8d | 2 | * Copyright (c) 2006-2015 ARM Limited |
vcoubard | 563:9c4b96f7be8d | 3 | * |
vcoubard | 563:9c4b96f7be8d | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
vcoubard | 563:9c4b96f7be8d | 5 | * you may not use this file except in compliance with the License. |
vcoubard | 563:9c4b96f7be8d | 6 | * You may obtain a copy of the License at |
vcoubard | 563:9c4b96f7be8d | 7 | * |
vcoubard | 563:9c4b96f7be8d | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
vcoubard | 563:9c4b96f7be8d | 9 | * |
vcoubard | 563:9c4b96f7be8d | 10 | * Unless required by applicable law or agreed to in writing, software |
vcoubard | 563:9c4b96f7be8d | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
vcoubard | 563:9c4b96f7be8d | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
vcoubard | 563:9c4b96f7be8d | 13 | * See the License for the specific language governing permissions and |
vcoubard | 563:9c4b96f7be8d | 14 | * limitations under the License. |
vcoubard | 563:9c4b96f7be8d | 15 | */ |
vcoubard | 563:9c4b96f7be8d | 16 | #include "nRF5xCharacteristicDescriptorDiscoverer.h" |
vcoubard | 563:9c4b96f7be8d | 17 | #include "ble_err.h" |
vcoubard | 563:9c4b96f7be8d | 18 | #include "mbed-drivers/mbed_error.h" |
vcoubard | 563:9c4b96f7be8d | 19 | #include "ble/DiscoveredCharacteristicDescriptor.h" |
vcoubard | 563:9c4b96f7be8d | 20 | |
vcoubard | 568:13b23a4b1f58 | 21 | nRF5xCharacteristicDescriptorDiscoverer::nRF5xCharacteristicDescriptorDiscoverer() : |
vcoubard | 568:13b23a4b1f58 | 22 | discoveryRunning() { |
vcoubard | 568:13b23a4b1f58 | 23 | // nothing to do |
vcoubard | 563:9c4b96f7be8d | 24 | } |
vcoubard | 563:9c4b96f7be8d | 25 | |
vcoubard | 568:13b23a4b1f58 | 26 | nRF5xCharacteristicDescriptorDiscoverer::~nRF5xCharacteristicDescriptorDiscoverer() { |
vcoubard | 568:13b23a4b1f58 | 27 | // nothing to do |
vcoubard | 563:9c4b96f7be8d | 28 | } |
vcoubard | 563:9c4b96f7be8d | 29 | |
vcoubard | 563:9c4b96f7be8d | 30 | ble_error_t nRF5xCharacteristicDescriptorDiscoverer::launch( |
vcoubard | 568:13b23a4b1f58 | 31 | const DiscoveredCharacteristic& characteristic, |
vcoubard | 563:9c4b96f7be8d | 32 | const CharacteristicDescriptorDiscovery::DiscoveryCallback_t& discoveryCallback, |
vcoubard | 563:9c4b96f7be8d | 33 | const CharacteristicDescriptorDiscovery::TerminationCallback_t& terminationCallback |
vcoubard | 563:9c4b96f7be8d | 34 | ) { |
vcoubard | 563:9c4b96f7be8d | 35 | Gap::Handle_t connHandle = characteristic.getConnectionHandle(); |
vcoubard | 568:13b23a4b1f58 | 36 | // it is ok to deduce that the start handle for descriptors is after |
vcoubard | 563:9c4b96f7be8d | 37 | // the characteristic declaration and the characteristic value declaration |
vcoubard | 563:9c4b96f7be8d | 38 | // see BLUETOOTH SPECIFICATION Version 4.2 [Vol 3, Part G] (3.3) |
vcoubard | 563:9c4b96f7be8d | 39 | Gap::Handle_t descriptorStartHandle = characteristic.getDeclHandle() + 2; |
vcoubard | 563:9c4b96f7be8d | 40 | Gap::Handle_t descriptorEndHandle = characteristic.getLastHandle(); |
vcoubard | 563:9c4b96f7be8d | 41 | |
vcoubard | 568:13b23a4b1f58 | 42 | // check if there is any descriptor to discover |
vcoubard | 568:13b23a4b1f58 | 43 | if (descriptorEndHandle < descriptorStartHandle) { |
vcoubard | 568:13b23a4b1f58 | 44 | CharacteristicDescriptorDiscovery::TerminationCallbackParams_t termParams = { |
vcoubard | 563:9c4b96f7be8d | 45 | characteristic, |
vcoubard | 563:9c4b96f7be8d | 46 | BLE_ERROR_NONE |
vcoubard | 563:9c4b96f7be8d | 47 | }; |
vcoubard | 563:9c4b96f7be8d | 48 | terminationCallback.call(&termParams); |
vcoubard | 563:9c4b96f7be8d | 49 | return BLE_ERROR_NONE; |
vcoubard | 563:9c4b96f7be8d | 50 | } |
vcoubard | 563:9c4b96f7be8d | 51 | |
vcoubard | 563:9c4b96f7be8d | 52 | // check if we can run this discovery |
vcoubard | 568:13b23a4b1f58 | 53 | if (isConnectionInUse(connHandle)) { |
vcoubard | 563:9c4b96f7be8d | 54 | return BLE_STACK_BUSY; |
vcoubard | 563:9c4b96f7be8d | 55 | } |
vcoubard | 563:9c4b96f7be8d | 56 | |
vcoubard | 563:9c4b96f7be8d | 57 | // get a new discovery slot, if none are available, just return |
vcoubard | 563:9c4b96f7be8d | 58 | Discovery* discovery = getAvailableDiscoverySlot(); |
vcoubard | 563:9c4b96f7be8d | 59 | if(discovery == NULL) { |
vcoubard | 568:13b23a4b1f58 | 60 | return BLE_STACK_BUSY; |
vcoubard | 563:9c4b96f7be8d | 61 | } |
vcoubard | 563:9c4b96f7be8d | 62 | |
vcoubard | 568:13b23a4b1f58 | 63 | // try to launch the discovery |
vcoubard | 563:9c4b96f7be8d | 64 | ble_error_t err = gattc_descriptors_discover(connHandle, descriptorStartHandle, descriptorEndHandle); |
vcoubard | 568:13b23a4b1f58 | 65 | if(!err) { |
vcoubard | 563:9c4b96f7be8d | 66 | // commit the new discovery to its slot |
vcoubard | 563:9c4b96f7be8d | 67 | *discovery = Discovery(characteristic, discoveryCallback, terminationCallback); |
vcoubard | 563:9c4b96f7be8d | 68 | } |
vcoubard | 563:9c4b96f7be8d | 69 | |
vcoubard | 563:9c4b96f7be8d | 70 | return err; |
vcoubard | 563:9c4b96f7be8d | 71 | } |
vcoubard | 563:9c4b96f7be8d | 72 | |
vcoubard | 563:9c4b96f7be8d | 73 | bool nRF5xCharacteristicDescriptorDiscoverer::isActive(const DiscoveredCharacteristic& characteristic) const { |
vcoubard | 568:13b23a4b1f58 | 74 | for(size_t i = 0; i < MAXIMUM_CONCURRENT_CONNECTIONS_COUNT; ++i) { |
vcoubard | 569:9e72aa06ec32 | 75 | if(discoveryRunning[i].getCharacteristic() == characteristic) { |
vcoubard | 568:13b23a4b1f58 | 76 | return true; |
vcoubard | 568:13b23a4b1f58 | 77 | } |
vcoubard | 568:13b23a4b1f58 | 78 | } |
vcoubard | 568:13b23a4b1f58 | 79 | return false; |
vcoubard | 563:9c4b96f7be8d | 80 | } |
vcoubard | 563:9c4b96f7be8d | 81 | |
vcoubard | 563:9c4b96f7be8d | 82 | void nRF5xCharacteristicDescriptorDiscoverer::requestTerminate(const DiscoveredCharacteristic& characteristic) { |
vcoubard | 563:9c4b96f7be8d | 83 | Discovery* discovery = findRunningDiscovery(characteristic); |
vcoubard | 568:13b23a4b1f58 | 84 | if(discovery) { |
vcoubard | 563:9c4b96f7be8d | 85 | // call terminate anyway |
vcoubard | 568:13b23a4b1f58 | 86 | terminate(discovery, BLE_ERROR_NONE); |
vcoubard | 563:9c4b96f7be8d | 87 | } |
vcoubard | 563:9c4b96f7be8d | 88 | } |
vcoubard | 563:9c4b96f7be8d | 89 | |
vcoubard | 563:9c4b96f7be8d | 90 | void nRF5xCharacteristicDescriptorDiscoverer::process(uint16_t connectionHandle, const ble_gattc_evt_desc_disc_rsp_t& descriptors) { |
vcoubard | 563:9c4b96f7be8d | 91 | Discovery* discovery = findRunningDiscovery(connectionHandle); |
vcoubard | 568:13b23a4b1f58 | 92 | // the discovery has been removed |
vcoubard | 568:13b23a4b1f58 | 93 | if(!discovery) { |
vcoubard | 568:13b23a4b1f58 | 94 | return; |
vcoubard | 563:9c4b96f7be8d | 95 | } |
vcoubard | 563:9c4b96f7be8d | 96 | |
vcoubard | 563:9c4b96f7be8d | 97 | for (uint16_t i = 0; i < descriptors.count; ++i) { |
vcoubard | 563:9c4b96f7be8d | 98 | discovery->process( |
vcoubard | 563:9c4b96f7be8d | 99 | descriptors.descs[i].handle, UUID(descriptors.descs[i].uuid.uuid) |
vcoubard | 563:9c4b96f7be8d | 100 | ); |
vcoubard | 563:9c4b96f7be8d | 101 | } |
vcoubard | 563:9c4b96f7be8d | 102 | |
vcoubard | 563:9c4b96f7be8d | 103 | // prepare the next discovery request (if needed) |
vcoubard | 563:9c4b96f7be8d | 104 | uint16_t startHandle = descriptors.descs[descriptors.count - 1].handle + 1; |
vcoubard | 569:9e72aa06ec32 | 105 | uint16_t endHandle = discovery->getCharacteristic().getLastHandle(); |
vcoubard | 563:9c4b96f7be8d | 106 | |
vcoubard | 568:13b23a4b1f58 | 107 | if(startHandle > endHandle) { |
vcoubard | 568:13b23a4b1f58 | 108 | terminate(discovery, BLE_ERROR_NONE); |
vcoubard | 563:9c4b96f7be8d | 109 | return; |
vcoubard | 563:9c4b96f7be8d | 110 | } |
vcoubard | 563:9c4b96f7be8d | 111 | |
vcoubard | 563:9c4b96f7be8d | 112 | ble_error_t err = gattc_descriptors_discover(connectionHandle, startHandle, endHandle); |
vcoubard | 568:13b23a4b1f58 | 113 | if(err) { |
vcoubard | 568:13b23a4b1f58 | 114 | terminate(discovery, err); |
vcoubard | 563:9c4b96f7be8d | 115 | return; |
vcoubard | 563:9c4b96f7be8d | 116 | } |
vcoubard | 563:9c4b96f7be8d | 117 | } |
vcoubard | 563:9c4b96f7be8d | 118 | |
vcoubard | 563:9c4b96f7be8d | 119 | void nRF5xCharacteristicDescriptorDiscoverer::terminate(uint16_t handle, ble_error_t err) { |
vcoubard | 563:9c4b96f7be8d | 120 | Discovery* discovery = findRunningDiscovery(handle); |
vcoubard | 568:13b23a4b1f58 | 121 | // the discovery has already been terminated |
vcoubard | 568:13b23a4b1f58 | 122 | if(!discovery) { |
vcoubard | 568:13b23a4b1f58 | 123 | return; |
vcoubard | 563:9c4b96f7be8d | 124 | } |
vcoubard | 563:9c4b96f7be8d | 125 | |
vcoubard | 568:13b23a4b1f58 | 126 | terminate(discovery, err); |
vcoubard | 568:13b23a4b1f58 | 127 | } |
vcoubard | 568:13b23a4b1f58 | 128 | |
vcoubard | 568:13b23a4b1f58 | 129 | void nRF5xCharacteristicDescriptorDiscoverer::terminate(Discovery* discovery, ble_error_t err) { |
vcoubard | 568:13b23a4b1f58 | 130 | // temporary copy, user code can try to launch a new discovery in the onTerminate |
vcoubard | 568:13b23a4b1f58 | 131 | // callback. So, this discovery should not appear in such case. |
vcoubard | 563:9c4b96f7be8d | 132 | Discovery tmp = *discovery; |
vcoubard | 563:9c4b96f7be8d | 133 | *discovery = Discovery(); |
vcoubard | 563:9c4b96f7be8d | 134 | tmp.terminate(err); |
vcoubard | 563:9c4b96f7be8d | 135 | } |
vcoubard | 563:9c4b96f7be8d | 136 | |
vcoubard | 568:13b23a4b1f58 | 137 | nRF5xCharacteristicDescriptorDiscoverer::Discovery* |
vcoubard | 563:9c4b96f7be8d | 138 | nRF5xCharacteristicDescriptorDiscoverer::findRunningDiscovery(const DiscoveredCharacteristic& characteristic) { |
vcoubard | 568:13b23a4b1f58 | 139 | for(size_t i = 0; i < MAXIMUM_CONCURRENT_CONNECTIONS_COUNT; ++i) { |
vcoubard | 569:9e72aa06ec32 | 140 | if((discoveryRunning[i].getCharacteristic() == characteristic) && |
vcoubard | 569:9e72aa06ec32 | 141 | (discoveryRunning[i].isEmpty() == false)) { |
vcoubard | 563:9c4b96f7be8d | 142 | return &discoveryRunning[i]; |
vcoubard | 563:9c4b96f7be8d | 143 | } |
vcoubard | 563:9c4b96f7be8d | 144 | } |
vcoubard | 563:9c4b96f7be8d | 145 | return NULL; |
vcoubard | 563:9c4b96f7be8d | 146 | } |
vcoubard | 563:9c4b96f7be8d | 147 | |
vcoubard | 568:13b23a4b1f58 | 148 | nRF5xCharacteristicDescriptorDiscoverer::Discovery* |
vcoubard | 568:13b23a4b1f58 | 149 | nRF5xCharacteristicDescriptorDiscoverer::findRunningDiscovery(uint16_t handle) { |
vcoubard | 568:13b23a4b1f58 | 150 | for(size_t i = 0; i < MAXIMUM_CONCURRENT_CONNECTIONS_COUNT; ++i) { |
vcoubard | 569:9e72aa06ec32 | 151 | if((discoveryRunning[i].getCharacteristic().getConnectionHandle() == handle) && |
vcoubard | 569:9e72aa06ec32 | 152 | (discoveryRunning[i].isEmpty() == false)) { |
vcoubard | 563:9c4b96f7be8d | 153 | return &discoveryRunning[i]; |
vcoubard | 563:9c4b96f7be8d | 154 | } |
vcoubard | 563:9c4b96f7be8d | 155 | } |
vcoubard | 563:9c4b96f7be8d | 156 | return NULL; |
vcoubard | 563:9c4b96f7be8d | 157 | } |
vcoubard | 563:9c4b96f7be8d | 158 | |
vcoubard | 568:13b23a4b1f58 | 159 | nRF5xCharacteristicDescriptorDiscoverer::Discovery* |
vcoubard | 568:13b23a4b1f58 | 160 | nRF5xCharacteristicDescriptorDiscoverer::getAvailableDiscoverySlot() { |
vcoubard | 568:13b23a4b1f58 | 161 | for(size_t i = 0; i < MAXIMUM_CONCURRENT_CONNECTIONS_COUNT; ++i) { |
vcoubard | 568:13b23a4b1f58 | 162 | if(discoveryRunning[i].isEmpty()) { |
vcoubard | 563:9c4b96f7be8d | 163 | return &discoveryRunning[i]; |
vcoubard | 563:9c4b96f7be8d | 164 | } |
vcoubard | 563:9c4b96f7be8d | 165 | } |
vcoubard | 563:9c4b96f7be8d | 166 | return NULL; |
vcoubard | 563:9c4b96f7be8d | 167 | } |
vcoubard | 563:9c4b96f7be8d | 168 | |
vcoubard | 563:9c4b96f7be8d | 169 | bool nRF5xCharacteristicDescriptorDiscoverer::isConnectionInUse(uint16_t connHandle) { |
vcoubard | 563:9c4b96f7be8d | 170 | return findRunningDiscovery(connHandle) != NULL; |
vcoubard | 563:9c4b96f7be8d | 171 | } |
vcoubard | 563:9c4b96f7be8d | 172 | |
vcoubard | 563:9c4b96f7be8d | 173 | ble_error_t nRF5xCharacteristicDescriptorDiscoverer::gattc_descriptors_discover( |
vcoubard | 568:13b23a4b1f58 | 174 | uint16_t connection_handle, uint16_t start_handle, uint16_t end_handle) { |
vcoubard | 563:9c4b96f7be8d | 175 | |
vcoubard | 563:9c4b96f7be8d | 176 | ble_gattc_handle_range_t discoveryRange = { |
vcoubard | 563:9c4b96f7be8d | 177 | start_handle, |
vcoubard | 563:9c4b96f7be8d | 178 | end_handle |
vcoubard | 563:9c4b96f7be8d | 179 | }; |
vcoubard | 563:9c4b96f7be8d | 180 | uint32_t err = sd_ble_gattc_descriptors_discover(connection_handle, &discoveryRange); |
vcoubard | 563:9c4b96f7be8d | 181 | |
vcoubard | 568:13b23a4b1f58 | 182 | switch(err) { |
vcoubard | 563:9c4b96f7be8d | 183 | case NRF_SUCCESS: |
vcoubard | 568:13b23a4b1f58 | 184 | return BLE_ERROR_NONE; |
vcoubard | 563:9c4b96f7be8d | 185 | case BLE_ERROR_INVALID_CONN_HANDLE: |
vcoubard | 563:9c4b96f7be8d | 186 | return BLE_ERROR_INVALID_PARAM; |
vcoubard | 563:9c4b96f7be8d | 187 | case NRF_ERROR_INVALID_ADDR: |
vcoubard | 563:9c4b96f7be8d | 188 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
vcoubard | 563:9c4b96f7be8d | 189 | case NRF_ERROR_BUSY: |
vcoubard | 563:9c4b96f7be8d | 190 | return BLE_STACK_BUSY; |
vcoubard | 563:9c4b96f7be8d | 191 | default: |
vcoubard | 563:9c4b96f7be8d | 192 | return BLE_ERROR_UNSPECIFIED; |
vcoubard | 563:9c4b96f7be8d | 193 | } |
vcoubard | 569:9e72aa06ec32 | 194 | } |
vcoubard | 569:9e72aa06ec32 | 195 | |
vcoubard | 569:9e72aa06ec32 | 196 | // implementation of nRF5xCharacteristicDescriptorDiscoverer::Discovery |
vcoubard | 569:9e72aa06ec32 | 197 | |
vcoubard | 569:9e72aa06ec32 | 198 | nRF5xCharacteristicDescriptorDiscoverer::Discovery::Discovery() : |
vcoubard | 569:9e72aa06ec32 | 199 | characteristic(), onDiscovery(), onTerminate() { |
vcoubard | 569:9e72aa06ec32 | 200 | } |
vcoubard | 569:9e72aa06ec32 | 201 | |
vcoubard | 569:9e72aa06ec32 | 202 | nRF5xCharacteristicDescriptorDiscoverer::Discovery::Discovery( |
vcoubard | 569:9e72aa06ec32 | 203 | const DiscoveredCharacteristic& c, const DiscoveryCallback_t& dCb, const TerminationCallback_t& tCb) : |
vcoubard | 569:9e72aa06ec32 | 204 | characteristic(c), onDiscovery(dCb), onTerminate(tCb) { |
vcoubard | 569:9e72aa06ec32 | 205 | } |
vcoubard | 569:9e72aa06ec32 | 206 | |
vcoubard | 569:9e72aa06ec32 | 207 | void nRF5xCharacteristicDescriptorDiscoverer::Discovery::process( |
vcoubard | 569:9e72aa06ec32 | 208 | GattAttribute::Handle_t handle, const UUID& uuid) { |
vcoubard | 569:9e72aa06ec32 | 209 | CharacteristicDescriptorDiscovery::DiscoveryCallbackParams_t params = { |
vcoubard | 569:9e72aa06ec32 | 210 | characteristic, |
vcoubard | 569:9e72aa06ec32 | 211 | DiscoveredCharacteristicDescriptor( |
vcoubard | 569:9e72aa06ec32 | 212 | characteristic.getGattClient(), |
vcoubard | 569:9e72aa06ec32 | 213 | characteristic.getConnectionHandle(), |
vcoubard | 569:9e72aa06ec32 | 214 | handle, |
vcoubard | 569:9e72aa06ec32 | 215 | uuid |
vcoubard | 569:9e72aa06ec32 | 216 | ) |
vcoubard | 569:9e72aa06ec32 | 217 | }; |
vcoubard | 569:9e72aa06ec32 | 218 | onDiscovery.call(¶ms); |
vcoubard | 569:9e72aa06ec32 | 219 | } |
vcoubard | 569:9e72aa06ec32 | 220 | |
vcoubard | 569:9e72aa06ec32 | 221 | void nRF5xCharacteristicDescriptorDiscoverer::Discovery::terminate(ble_error_t err) { |
vcoubard | 569:9e72aa06ec32 | 222 | CharacteristicDescriptorDiscovery::TerminationCallbackParams_t params = { |
vcoubard | 569:9e72aa06ec32 | 223 | characteristic, |
vcoubard | 569:9e72aa06ec32 | 224 | err |
vcoubard | 569:9e72aa06ec32 | 225 | }; |
vcoubard | 569:9e72aa06ec32 | 226 | |
vcoubard | 569:9e72aa06ec32 | 227 | onTerminate.call(¶ms); |
vcoubard | 569:9e72aa06ec32 | 228 | } |
vcoubard | 569:9e72aa06ec32 | 229 | |
vcoubard | 569:9e72aa06ec32 | 230 | bool nRF5xCharacteristicDescriptorDiscoverer::Discovery::isEmpty() const { |
vcoubard | 569:9e72aa06ec32 | 231 | return *this == Discovery(); |
vcoubard | 569:9e72aa06ec32 | 232 | } |
vcoubard | 569:9e72aa06ec32 | 233 | |
vcoubard | 569:9e72aa06ec32 | 234 | const DiscoveredCharacteristic& nRF5xCharacteristicDescriptorDiscoverer::Discovery::getCharacteristic() const { |
vcoubard | 569:9e72aa06ec32 | 235 | return characteristic; |
vcoubard | 563:9c4b96f7be8d | 236 | } |