Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of nRF51822 by
source/nRF5xCharacteristicDescriptorDiscoverer.cpp@544:9e3d053ad4ec, 2016-01-11 (annotated)
- Committer:
- vcoubard
- Date:
- Mon Jan 11 10:19:04 2016 +0000
- Revision:
- 544:9e3d053ad4ec
- Parent:
- 543:53215259c0d2
- Child:
- 545:d834e6591aee
Synchronized with git rev dc2dfb0a
Author: Vincent Coubard
Add status parameter in terminateCharacteristicDiscovery function.
Fix terminate discovery (the replacement of the discovery was done after
the call to terminate).
When searching for a running discovery, dismiss results where the
characteristic is equal to the default characteristic value
Add Discovery::operator!=
Add support of DiscoveredCharacteristic last handle in the characteristic
discovery process
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vcoubard | 542:1bf9c597f44f | 1 | #include "nRF5xCharacteristicDescriptorDiscoverer.h" |
vcoubard | 542:1bf9c597f44f | 2 | #include "ble_err.h" |
vcoubard | 542:1bf9c597f44f | 3 | #include "mbed-drivers/mbed_error.h" |
vcoubard | 542:1bf9c597f44f | 4 | #include "ble/DiscoveredCharacteristicDescriptor.h" |
vcoubard | 542:1bf9c597f44f | 5 | |
vcoubard | 542:1bf9c597f44f | 6 | namespace { |
vcoubard | 542:1bf9c597f44f | 7 | void emptyDiscoveryCallback(const CharacteristicDescriptorDiscovery::DiscoveryCallbackParams_t*) { } |
vcoubard | 542:1bf9c597f44f | 8 | void emptyTerminationCallback(const CharacteristicDescriptorDiscovery::TerminationCallbackParams_t*) { } |
vcoubard | 542:1bf9c597f44f | 9 | } |
vcoubard | 542:1bf9c597f44f | 10 | |
vcoubard | 542:1bf9c597f44f | 11 | nRF5xCharacteristicDescriptorDiscoverer::nRF5xCharacteristicDescriptorDiscoverer(size_t concurrentConnectionsCount) : |
vcoubard | 542:1bf9c597f44f | 12 | maximumConcurrentConnectionsCount(concurrentConnectionsCount), |
vcoubard | 542:1bf9c597f44f | 13 | discoveryRunning(new Discovery[concurrentConnectionsCount]) { |
vcoubard | 542:1bf9c597f44f | 14 | |
vcoubard | 542:1bf9c597f44f | 15 | } |
vcoubard | 542:1bf9c597f44f | 16 | |
vcoubard | 542:1bf9c597f44f | 17 | nRF5xCharacteristicDescriptorDiscoverer::~nRF5xCharacteristicDescriptorDiscoverer() { |
vcoubard | 542:1bf9c597f44f | 18 | delete [] discoveryRunning; |
vcoubard | 542:1bf9c597f44f | 19 | } |
vcoubard | 542:1bf9c597f44f | 20 | |
vcoubard | 542:1bf9c597f44f | 21 | ble_error_t nRF5xCharacteristicDescriptorDiscoverer::launch( |
vcoubard | 542:1bf9c597f44f | 22 | const DiscoveredCharacteristic& characteristic, |
vcoubard | 542:1bf9c597f44f | 23 | const CharacteristicDescriptorDiscovery::DiscoveryCallback_t& discoveryCallback, |
vcoubard | 542:1bf9c597f44f | 24 | const CharacteristicDescriptorDiscovery::TerminationCallback_t& terminationCallback |
vcoubard | 542:1bf9c597f44f | 25 | ) { |
vcoubard | 542:1bf9c597f44f | 26 | Gap::Handle_t connHandle = characteristic.getConnectionHandle(); |
vcoubard | 542:1bf9c597f44f | 27 | // it is ok to deduce that the start handle for descriptors is after |
vcoubard | 542:1bf9c597f44f | 28 | // the characteristic declaration and the characteristic value declaration |
vcoubard | 542:1bf9c597f44f | 29 | // see BLUETOOTH SPECIFICATION Version 4.2 [Vol 3, Part G] (3.3) |
vcoubard | 542:1bf9c597f44f | 30 | Gap::Handle_t descriptorStartHandle = characteristic.getDeclHandle() + 2; |
vcoubard | 542:1bf9c597f44f | 31 | Gap::Handle_t descriptorEndHandle = characteristic.getLastHandle(); |
vcoubard | 542:1bf9c597f44f | 32 | |
vcoubard | 542:1bf9c597f44f | 33 | // check if their is any descriptor to discover |
vcoubard | 542:1bf9c597f44f | 34 | if (descriptorEndHandle < descriptorStartHandle) { |
vcoubard | 542:1bf9c597f44f | 35 | CharacteristicDescriptorDiscovery::TerminationCallbackParams_t termParams = { |
vcoubard | 543:53215259c0d2 | 36 | characteristic, |
vcoubard | 543:53215259c0d2 | 37 | BLE_ERROR_NONE |
vcoubard | 542:1bf9c597f44f | 38 | }; |
vcoubard | 542:1bf9c597f44f | 39 | terminationCallback.call(&termParams); |
vcoubard | 542:1bf9c597f44f | 40 | return BLE_ERROR_NONE; |
vcoubard | 542:1bf9c597f44f | 41 | } |
vcoubard | 542:1bf9c597f44f | 42 | |
vcoubard | 542:1bf9c597f44f | 43 | // check if we can run this discovery |
vcoubard | 542:1bf9c597f44f | 44 | if (isConnectionInUse(connHandle)) { |
vcoubard | 542:1bf9c597f44f | 45 | return BLE_STACK_BUSY; |
vcoubard | 542:1bf9c597f44f | 46 | } |
vcoubard | 542:1bf9c597f44f | 47 | |
vcoubard | 542:1bf9c597f44f | 48 | // get a new discovery slot, if none are available, just return |
vcoubard | 542:1bf9c597f44f | 49 | Discovery* discovery = getAvailableDiscoverySlot(); |
vcoubard | 542:1bf9c597f44f | 50 | if(discovery == NULL) { |
vcoubard | 542:1bf9c597f44f | 51 | return BLE_STACK_BUSY; |
vcoubard | 542:1bf9c597f44f | 52 | } |
vcoubard | 542:1bf9c597f44f | 53 | |
vcoubard | 542:1bf9c597f44f | 54 | // try to launch the discovery |
vcoubard | 543:53215259c0d2 | 55 | ble_error_t err = gattc_descriptors_discover(connHandle, descriptorStartHandle, descriptorEndHandle); |
vcoubard | 543:53215259c0d2 | 56 | if(!err) { |
vcoubard | 543:53215259c0d2 | 57 | // commit the new discovery to its slot |
vcoubard | 543:53215259c0d2 | 58 | *discovery = Discovery(characteristic, discoveryCallback, terminationCallback); |
vcoubard | 543:53215259c0d2 | 59 | } |
vcoubard | 542:1bf9c597f44f | 60 | |
vcoubard | 543:53215259c0d2 | 61 | return err; |
vcoubard | 542:1bf9c597f44f | 62 | } |
vcoubard | 542:1bf9c597f44f | 63 | |
vcoubard | 542:1bf9c597f44f | 64 | bool nRF5xCharacteristicDescriptorDiscoverer::isActive(const DiscoveredCharacteristic& characteristic) const { |
vcoubard | 542:1bf9c597f44f | 65 | return findRunningDiscovery(characteristic) != NULL; |
vcoubard | 542:1bf9c597f44f | 66 | } |
vcoubard | 542:1bf9c597f44f | 67 | |
vcoubard | 542:1bf9c597f44f | 68 | void nRF5xCharacteristicDescriptorDiscoverer::requestTerminate(const DiscoveredCharacteristic& characteristic) { |
vcoubard | 542:1bf9c597f44f | 69 | Discovery* discovery = findRunningDiscovery(characteristic); |
vcoubard | 542:1bf9c597f44f | 70 | if(discovery) { |
vcoubard | 542:1bf9c597f44f | 71 | discovery->onDiscovery = emptyDiscoveryCallback; |
vcoubard | 542:1bf9c597f44f | 72 | // call terminate anyway |
vcoubard | 543:53215259c0d2 | 73 | discovery->terminate(BLE_ERROR_NONE); |
vcoubard | 542:1bf9c597f44f | 74 | discovery->onTerminate = emptyTerminationCallback; |
vcoubard | 542:1bf9c597f44f | 75 | } |
vcoubard | 542:1bf9c597f44f | 76 | } |
vcoubard | 542:1bf9c597f44f | 77 | |
vcoubard | 543:53215259c0d2 | 78 | void nRF5xCharacteristicDescriptorDiscoverer::process(uint16_t connectionHandle, const ble_gattc_evt_desc_disc_rsp_t& descriptors) { |
vcoubard | 543:53215259c0d2 | 79 | Discovery* discovery = findRunningDiscovery(connectionHandle); |
vcoubard | 542:1bf9c597f44f | 80 | if(!discovery) { |
vcoubard | 542:1bf9c597f44f | 81 | error("logic error in nRF5xCharacteristicDescriptorDiscoverer::process !!!"); |
vcoubard | 542:1bf9c597f44f | 82 | } |
vcoubard | 542:1bf9c597f44f | 83 | |
vcoubard | 542:1bf9c597f44f | 84 | for (uint16_t i = 0; i < descriptors.count; ++i) { |
vcoubard | 542:1bf9c597f44f | 85 | discovery->process( |
vcoubard | 542:1bf9c597f44f | 86 | descriptors.descs[i].handle, UUID(descriptors.descs[i].uuid.uuid) |
vcoubard | 542:1bf9c597f44f | 87 | ); |
vcoubard | 542:1bf9c597f44f | 88 | } |
vcoubard | 543:53215259c0d2 | 89 | |
vcoubard | 543:53215259c0d2 | 90 | // prepare the next discovery request (if needed) |
vcoubard | 543:53215259c0d2 | 91 | uint16_t startHandle = descriptors.descs[descriptors.count - 1].handle + 1; |
vcoubard | 543:53215259c0d2 | 92 | uint16_t endHandle = discovery->characteristic.getLastHandle(); |
vcoubard | 543:53215259c0d2 | 93 | |
vcoubard | 543:53215259c0d2 | 94 | if(startHandle > endHandle || |
vcoubard | 543:53215259c0d2 | 95 | (discovery->onDiscovery == emptyDiscoveryCallback && discovery->onTerminate == emptyTerminationCallback)) { |
vcoubard | 543:53215259c0d2 | 96 | terminate(connectionHandle, BLE_ERROR_NONE); |
vcoubard | 543:53215259c0d2 | 97 | return; |
vcoubard | 543:53215259c0d2 | 98 | } |
vcoubard | 543:53215259c0d2 | 99 | |
vcoubard | 543:53215259c0d2 | 100 | ble_error_t err = gattc_descriptors_discover(connectionHandle, startHandle, endHandle); |
vcoubard | 543:53215259c0d2 | 101 | if(err) { |
vcoubard | 543:53215259c0d2 | 102 | terminate(connectionHandle, err); |
vcoubard | 543:53215259c0d2 | 103 | return; |
vcoubard | 543:53215259c0d2 | 104 | } |
vcoubard | 542:1bf9c597f44f | 105 | } |
vcoubard | 542:1bf9c597f44f | 106 | |
vcoubard | 543:53215259c0d2 | 107 | void nRF5xCharacteristicDescriptorDiscoverer::terminate(uint16_t handle, ble_error_t err) { |
vcoubard | 542:1bf9c597f44f | 108 | Discovery* discovery = findRunningDiscovery(handle); |
vcoubard | 542:1bf9c597f44f | 109 | if(!discovery) { |
vcoubard | 542:1bf9c597f44f | 110 | error("logic error in nRF5xCharacteristicDescriptorDiscoverer::process !!!"); |
vcoubard | 542:1bf9c597f44f | 111 | } |
vcoubard | 544:9e3d053ad4ec | 112 | |
vcoubard | 544:9e3d053ad4ec | 113 | Discovery tmp = *discovery; |
vcoubard | 544:9e3d053ad4ec | 114 | *discovery = Discovery(); |
vcoubard | 544:9e3d053ad4ec | 115 | tmp.terminate(err); |
vcoubard | 542:1bf9c597f44f | 116 | } |
vcoubard | 542:1bf9c597f44f | 117 | |
vcoubard | 542:1bf9c597f44f | 118 | nRF5xCharacteristicDescriptorDiscoverer::Discovery* |
vcoubard | 542:1bf9c597f44f | 119 | nRF5xCharacteristicDescriptorDiscoverer::findRunningDiscovery(const DiscoveredCharacteristic& characteristic) { |
vcoubard | 542:1bf9c597f44f | 120 | for(size_t i = 0; i < maximumConcurrentConnectionsCount; ++i) { |
vcoubard | 542:1bf9c597f44f | 121 | if(discoveryRunning[i].characteristic == characteristic) { |
vcoubard | 542:1bf9c597f44f | 122 | return &discoveryRunning[i]; |
vcoubard | 542:1bf9c597f44f | 123 | } |
vcoubard | 542:1bf9c597f44f | 124 | } |
vcoubard | 542:1bf9c597f44f | 125 | return NULL; |
vcoubard | 542:1bf9c597f44f | 126 | } |
vcoubard | 542:1bf9c597f44f | 127 | |
vcoubard | 542:1bf9c597f44f | 128 | nRF5xCharacteristicDescriptorDiscoverer::Discovery* |
vcoubard | 542:1bf9c597f44f | 129 | nRF5xCharacteristicDescriptorDiscoverer::findRunningDiscovery(const DiscoveredCharacteristic& characteristic) const { |
vcoubard | 542:1bf9c597f44f | 130 | for(size_t i = 0; i < maximumConcurrentConnectionsCount; ++i) { |
vcoubard | 542:1bf9c597f44f | 131 | if(discoveryRunning[i].characteristic == characteristic) { |
vcoubard | 542:1bf9c597f44f | 132 | return &discoveryRunning[i]; |
vcoubard | 542:1bf9c597f44f | 133 | } |
vcoubard | 542:1bf9c597f44f | 134 | } |
vcoubard | 542:1bf9c597f44f | 135 | return NULL; |
vcoubard | 542:1bf9c597f44f | 136 | } |
vcoubard | 542:1bf9c597f44f | 137 | |
vcoubard | 542:1bf9c597f44f | 138 | nRF5xCharacteristicDescriptorDiscoverer::Discovery* |
vcoubard | 542:1bf9c597f44f | 139 | nRF5xCharacteristicDescriptorDiscoverer::findRunningDiscovery(uint16_t handle) { |
vcoubard | 544:9e3d053ad4ec | 140 | for(size_t i = 0; i < maximumConcurrentConnectionsCount; ++i) { |
vcoubard | 544:9e3d053ad4ec | 141 | if(discoveryRunning[i].characteristic.getConnectionHandle() == handle && |
vcoubard | 544:9e3d053ad4ec | 142 | discoveryRunning[i] != Discovery()) { |
vcoubard | 542:1bf9c597f44f | 143 | return &discoveryRunning[i]; |
vcoubard | 542:1bf9c597f44f | 144 | } |
vcoubard | 542:1bf9c597f44f | 145 | } |
vcoubard | 542:1bf9c597f44f | 146 | return NULL; |
vcoubard | 542:1bf9c597f44f | 147 | } |
vcoubard | 542:1bf9c597f44f | 148 | |
vcoubard | 542:1bf9c597f44f | 149 | void nRF5xCharacteristicDescriptorDiscoverer::removeDiscovery(Discovery* discovery) { |
vcoubard | 542:1bf9c597f44f | 150 | for(size_t i = 0; i < maximumConcurrentConnectionsCount; ++i) { |
vcoubard | 542:1bf9c597f44f | 151 | if(&discoveryRunning[i] == discovery) { |
vcoubard | 542:1bf9c597f44f | 152 | discoveryRunning[i] = Discovery(); |
vcoubard | 542:1bf9c597f44f | 153 | } |
vcoubard | 542:1bf9c597f44f | 154 | } |
vcoubard | 542:1bf9c597f44f | 155 | } |
vcoubard | 542:1bf9c597f44f | 156 | |
vcoubard | 542:1bf9c597f44f | 157 | nRF5xCharacteristicDescriptorDiscoverer::Discovery* |
vcoubard | 542:1bf9c597f44f | 158 | nRF5xCharacteristicDescriptorDiscoverer::getAvailableDiscoverySlot() { |
vcoubard | 542:1bf9c597f44f | 159 | for(size_t i = 0; i < maximumConcurrentConnectionsCount; ++i) { |
vcoubard | 542:1bf9c597f44f | 160 | if(discoveryRunning[i] == Discovery()) { |
vcoubard | 542:1bf9c597f44f | 161 | return &discoveryRunning[i]; |
vcoubard | 542:1bf9c597f44f | 162 | } |
vcoubard | 542:1bf9c597f44f | 163 | } |
vcoubard | 542:1bf9c597f44f | 164 | return NULL; |
vcoubard | 542:1bf9c597f44f | 165 | } |
vcoubard | 542:1bf9c597f44f | 166 | |
vcoubard | 542:1bf9c597f44f | 167 | bool nRF5xCharacteristicDescriptorDiscoverer::isConnectionInUse(uint16_t connHandle) { |
vcoubard | 542:1bf9c597f44f | 168 | return findRunningDiscovery(connHandle) != NULL; |
vcoubard | 543:53215259c0d2 | 169 | } |
vcoubard | 543:53215259c0d2 | 170 | |
vcoubard | 543:53215259c0d2 | 171 | ble_error_t nRF5xCharacteristicDescriptorDiscoverer::gattc_descriptors_discover( |
vcoubard | 543:53215259c0d2 | 172 | uint16_t connection_handle, uint16_t start_handle, uint16_t end_handle) { |
vcoubard | 543:53215259c0d2 | 173 | |
vcoubard | 543:53215259c0d2 | 174 | ble_gattc_handle_range_t discoveryRange = { |
vcoubard | 543:53215259c0d2 | 175 | start_handle, |
vcoubard | 543:53215259c0d2 | 176 | end_handle |
vcoubard | 543:53215259c0d2 | 177 | }; |
vcoubard | 543:53215259c0d2 | 178 | uint32_t err = sd_ble_gattc_descriptors_discover(connection_handle, &discoveryRange); |
vcoubard | 543:53215259c0d2 | 179 | |
vcoubard | 543:53215259c0d2 | 180 | switch(err) { |
vcoubard | 543:53215259c0d2 | 181 | case NRF_SUCCESS: |
vcoubard | 543:53215259c0d2 | 182 | return BLE_ERROR_NONE; |
vcoubard | 543:53215259c0d2 | 183 | case BLE_ERROR_INVALID_CONN_HANDLE: |
vcoubard | 543:53215259c0d2 | 184 | return BLE_ERROR_INVALID_PARAM; |
vcoubard | 543:53215259c0d2 | 185 | case NRF_ERROR_INVALID_ADDR: |
vcoubard | 543:53215259c0d2 | 186 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
vcoubard | 543:53215259c0d2 | 187 | case NRF_ERROR_BUSY: |
vcoubard | 543:53215259c0d2 | 188 | return BLE_STACK_BUSY; |
vcoubard | 543:53215259c0d2 | 189 | default: |
vcoubard | 543:53215259c0d2 | 190 | return BLE_ERROR_UNSPECIFIED; |
vcoubard | 543:53215259c0d2 | 191 | } |
vcoubard | 542:1bf9c597f44f | 192 | } |