smart ball test board code

Dependencies:   nrf51-sdk

Fork of nRF51822 by Nordic Semiconductor

Committer:
vcoubard
Date:
Mon Jan 11 10:19:03 2016 +0000
Revision:
544:53215259c0d2
Parent:
543:1bf9c597f44f
Child:
545:9e3d053ad4ec
Synchronized with git rev cdd5b921
Author: Vincent Coubard
Relaunch discovery operation when gatt event status is
BLE_GATT_STATUS_SUCCESS.
Report error in the Termination callback

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vcoubard 543:1bf9c597f44f 1 #include "nRF5xCharacteristicDescriptorDiscoverer.h"
vcoubard 543:1bf9c597f44f 2 #include "ble_err.h"
vcoubard 543:1bf9c597f44f 3 #include "mbed-drivers/mbed_error.h"
vcoubard 543:1bf9c597f44f 4 #include "ble/DiscoveredCharacteristicDescriptor.h"
vcoubard 543:1bf9c597f44f 5
vcoubard 543:1bf9c597f44f 6
vcoubard 543:1bf9c597f44f 7
vcoubard 543:1bf9c597f44f 8 namespace {
vcoubard 543:1bf9c597f44f 9 void emptyDiscoveryCallback(const CharacteristicDescriptorDiscovery::DiscoveryCallbackParams_t*) { }
vcoubard 543:1bf9c597f44f 10 void emptyTerminationCallback(const CharacteristicDescriptorDiscovery::TerminationCallbackParams_t*) { }
vcoubard 543:1bf9c597f44f 11 }
vcoubard 543:1bf9c597f44f 12
vcoubard 543:1bf9c597f44f 13 nRF5xCharacteristicDescriptorDiscoverer::nRF5xCharacteristicDescriptorDiscoverer(size_t concurrentConnectionsCount) :
vcoubard 543:1bf9c597f44f 14 maximumConcurrentConnectionsCount(concurrentConnectionsCount),
vcoubard 543:1bf9c597f44f 15 discoveryRunning(new Discovery[concurrentConnectionsCount]) {
vcoubard 543:1bf9c597f44f 16
vcoubard 543:1bf9c597f44f 17 }
vcoubard 543:1bf9c597f44f 18
vcoubard 543:1bf9c597f44f 19 nRF5xCharacteristicDescriptorDiscoverer::~nRF5xCharacteristicDescriptorDiscoverer() {
vcoubard 543:1bf9c597f44f 20 delete [] discoveryRunning;
vcoubard 543:1bf9c597f44f 21 }
vcoubard 543:1bf9c597f44f 22
vcoubard 543:1bf9c597f44f 23 ble_error_t nRF5xCharacteristicDescriptorDiscoverer::launch(
vcoubard 543:1bf9c597f44f 24 const DiscoveredCharacteristic& characteristic,
vcoubard 543:1bf9c597f44f 25 const CharacteristicDescriptorDiscovery::DiscoveryCallback_t& discoveryCallback,
vcoubard 543:1bf9c597f44f 26 const CharacteristicDescriptorDiscovery::TerminationCallback_t& terminationCallback
vcoubard 543:1bf9c597f44f 27 ) {
vcoubard 543:1bf9c597f44f 28 Gap::Handle_t connHandle = characteristic.getConnectionHandle();
vcoubard 543:1bf9c597f44f 29 // it is ok to deduce that the start handle for descriptors is after
vcoubard 543:1bf9c597f44f 30 // the characteristic declaration and the characteristic value declaration
vcoubard 543:1bf9c597f44f 31 // see BLUETOOTH SPECIFICATION Version 4.2 [Vol 3, Part G] (3.3)
vcoubard 543:1bf9c597f44f 32 Gap::Handle_t descriptorStartHandle = characteristic.getDeclHandle() + 2;
vcoubard 543:1bf9c597f44f 33 Gap::Handle_t descriptorEndHandle = characteristic.getLastHandle();
vcoubard 543:1bf9c597f44f 34
vcoubard 543:1bf9c597f44f 35 // check if their is any descriptor to discover
vcoubard 543:1bf9c597f44f 36 if (descriptorEndHandle < descriptorStartHandle) {
vcoubard 543:1bf9c597f44f 37 CharacteristicDescriptorDiscovery::TerminationCallbackParams_t termParams = {
vcoubard 544:53215259c0d2 38 characteristic,
vcoubard 544:53215259c0d2 39 BLE_ERROR_NONE
vcoubard 543:1bf9c597f44f 40 };
vcoubard 543:1bf9c597f44f 41 terminationCallback.call(&termParams);
vcoubard 543:1bf9c597f44f 42 return BLE_ERROR_NONE;
vcoubard 543:1bf9c597f44f 43 }
vcoubard 543:1bf9c597f44f 44
vcoubard 543:1bf9c597f44f 45 // check if we can run this discovery
vcoubard 543:1bf9c597f44f 46 if (isConnectionInUse(connHandle)) {
vcoubard 543:1bf9c597f44f 47 return BLE_STACK_BUSY;
vcoubard 543:1bf9c597f44f 48 }
vcoubard 543:1bf9c597f44f 49
vcoubard 543:1bf9c597f44f 50 // get a new discovery slot, if none are available, just return
vcoubard 543:1bf9c597f44f 51 Discovery* discovery = getAvailableDiscoverySlot();
vcoubard 543:1bf9c597f44f 52 if(discovery == NULL) {
vcoubard 543:1bf9c597f44f 53 return BLE_STACK_BUSY;
vcoubard 543:1bf9c597f44f 54 }
vcoubard 543:1bf9c597f44f 55
vcoubard 543:1bf9c597f44f 56 // try to launch the discovery
vcoubard 544:53215259c0d2 57 ble_error_t err = gattc_descriptors_discover(connHandle, descriptorStartHandle, descriptorEndHandle);
vcoubard 544:53215259c0d2 58 if(!err) {
vcoubard 544:53215259c0d2 59 // commit the new discovery to its slot
vcoubard 544:53215259c0d2 60 *discovery = Discovery(characteristic, discoveryCallback, terminationCallback);
vcoubard 544:53215259c0d2 61 }
vcoubard 543:1bf9c597f44f 62
vcoubard 544:53215259c0d2 63 return err;
vcoubard 543:1bf9c597f44f 64 }
vcoubard 543:1bf9c597f44f 65
vcoubard 543:1bf9c597f44f 66 bool nRF5xCharacteristicDescriptorDiscoverer::isActive(const DiscoveredCharacteristic& characteristic) const {
vcoubard 543:1bf9c597f44f 67 return findRunningDiscovery(characteristic) != NULL;
vcoubard 543:1bf9c597f44f 68 }
vcoubard 543:1bf9c597f44f 69
vcoubard 543:1bf9c597f44f 70 void nRF5xCharacteristicDescriptorDiscoverer::requestTerminate(const DiscoveredCharacteristic& characteristic) {
vcoubard 543:1bf9c597f44f 71 Discovery* discovery = findRunningDiscovery(characteristic);
vcoubard 543:1bf9c597f44f 72 if(discovery) {
vcoubard 543:1bf9c597f44f 73 discovery->onDiscovery = emptyDiscoveryCallback;
vcoubard 543:1bf9c597f44f 74 // call terminate anyway
vcoubard 544:53215259c0d2 75 discovery->terminate(BLE_ERROR_NONE);
vcoubard 543:1bf9c597f44f 76 discovery->onTerminate = emptyTerminationCallback;
vcoubard 543:1bf9c597f44f 77 }
vcoubard 543:1bf9c597f44f 78 }
vcoubard 543:1bf9c597f44f 79
vcoubard 544:53215259c0d2 80 void nRF5xCharacteristicDescriptorDiscoverer::process(uint16_t connectionHandle, const ble_gattc_evt_desc_disc_rsp_t& descriptors) {
vcoubard 544:53215259c0d2 81 Discovery* discovery = findRunningDiscovery(connectionHandle);
vcoubard 543:1bf9c597f44f 82 if(!discovery) {
vcoubard 543:1bf9c597f44f 83 error("logic error in nRF5xCharacteristicDescriptorDiscoverer::process !!!");
vcoubard 543:1bf9c597f44f 84 }
vcoubard 543:1bf9c597f44f 85
vcoubard 543:1bf9c597f44f 86 for (uint16_t i = 0; i < descriptors.count; ++i) {
vcoubard 543:1bf9c597f44f 87 discovery->process(
vcoubard 543:1bf9c597f44f 88 descriptors.descs[i].handle, UUID(descriptors.descs[i].uuid.uuid)
vcoubard 543:1bf9c597f44f 89 );
vcoubard 543:1bf9c597f44f 90 }
vcoubard 544:53215259c0d2 91
vcoubard 544:53215259c0d2 92 // prepare the next discovery request (if needed)
vcoubard 544:53215259c0d2 93 uint16_t startHandle = descriptors.descs[descriptors.count - 1].handle + 1;
vcoubard 544:53215259c0d2 94 uint16_t endHandle = discovery->characteristic.getLastHandle();
vcoubard 544:53215259c0d2 95
vcoubard 544:53215259c0d2 96 if(startHandle > endHandle ||
vcoubard 544:53215259c0d2 97 (discovery->onDiscovery == emptyDiscoveryCallback && discovery->onTerminate == emptyTerminationCallback)) {
vcoubard 544:53215259c0d2 98 terminate(connectionHandle, BLE_ERROR_NONE);
vcoubard 544:53215259c0d2 99 return;
vcoubard 544:53215259c0d2 100 }
vcoubard 544:53215259c0d2 101
vcoubard 544:53215259c0d2 102 ble_error_t err = gattc_descriptors_discover(connectionHandle, startHandle, endHandle);
vcoubard 544:53215259c0d2 103 if(err) {
vcoubard 544:53215259c0d2 104 terminate(connectionHandle, err);
vcoubard 544:53215259c0d2 105 return;
vcoubard 544:53215259c0d2 106 }
vcoubard 543:1bf9c597f44f 107 }
vcoubard 543:1bf9c597f44f 108
vcoubard 544:53215259c0d2 109 void nRF5xCharacteristicDescriptorDiscoverer::terminate(uint16_t handle, ble_error_t err) {
vcoubard 543:1bf9c597f44f 110 Discovery* discovery = findRunningDiscovery(handle);
vcoubard 543:1bf9c597f44f 111 if(!discovery) {
vcoubard 543:1bf9c597f44f 112 error("logic error in nRF5xCharacteristicDescriptorDiscoverer::process !!!");
vcoubard 543:1bf9c597f44f 113 }
vcoubard 544:53215259c0d2 114 discovery->terminate(err);
vcoubard 543:1bf9c597f44f 115 removeDiscovery(discovery);
vcoubard 543:1bf9c597f44f 116 }
vcoubard 543:1bf9c597f44f 117
vcoubard 543:1bf9c597f44f 118 nRF5xCharacteristicDescriptorDiscoverer::Discovery*
vcoubard 543:1bf9c597f44f 119 nRF5xCharacteristicDescriptorDiscoverer::findRunningDiscovery(const DiscoveredCharacteristic& characteristic) {
vcoubard 543:1bf9c597f44f 120 for(size_t i = 0; i < maximumConcurrentConnectionsCount; ++i) {
vcoubard 543:1bf9c597f44f 121 if(discoveryRunning[i].characteristic == characteristic) {
vcoubard 543:1bf9c597f44f 122 return &discoveryRunning[i];
vcoubard 543:1bf9c597f44f 123 }
vcoubard 543:1bf9c597f44f 124 }
vcoubard 543:1bf9c597f44f 125 return NULL;
vcoubard 543:1bf9c597f44f 126 }
vcoubard 543:1bf9c597f44f 127
vcoubard 543:1bf9c597f44f 128 nRF5xCharacteristicDescriptorDiscoverer::Discovery*
vcoubard 543:1bf9c597f44f 129 nRF5xCharacteristicDescriptorDiscoverer::findRunningDiscovery(const DiscoveredCharacteristic& characteristic) const {
vcoubard 543:1bf9c597f44f 130 for(size_t i = 0; i < maximumConcurrentConnectionsCount; ++i) {
vcoubard 543:1bf9c597f44f 131 if(discoveryRunning[i].characteristic == characteristic) {
vcoubard 543:1bf9c597f44f 132 return &discoveryRunning[i];
vcoubard 543:1bf9c597f44f 133 }
vcoubard 543:1bf9c597f44f 134 }
vcoubard 543:1bf9c597f44f 135 return NULL;
vcoubard 543:1bf9c597f44f 136 }
vcoubard 543:1bf9c597f44f 137
vcoubard 543:1bf9c597f44f 138 nRF5xCharacteristicDescriptorDiscoverer::Discovery*
vcoubard 543:1bf9c597f44f 139 nRF5xCharacteristicDescriptorDiscoverer::findRunningDiscovery(uint16_t handle) {
vcoubard 543:1bf9c597f44f 140 for(size_t i = 0; i < maximumConcurrentConnectionsCount; ++i) {
vcoubard 543:1bf9c597f44f 141 if(discoveryRunning[i].characteristic.getConnectionHandle() == handle) {
vcoubard 543:1bf9c597f44f 142 return &discoveryRunning[i];
vcoubard 543:1bf9c597f44f 143 }
vcoubard 543:1bf9c597f44f 144 }
vcoubard 543:1bf9c597f44f 145 return NULL;
vcoubard 543:1bf9c597f44f 146 }
vcoubard 543:1bf9c597f44f 147
vcoubard 543:1bf9c597f44f 148 void nRF5xCharacteristicDescriptorDiscoverer::removeDiscovery(Discovery* discovery) {
vcoubard 543:1bf9c597f44f 149 for(size_t i = 0; i < maximumConcurrentConnectionsCount; ++i) {
vcoubard 543:1bf9c597f44f 150 if(&discoveryRunning[i] == discovery) {
vcoubard 543:1bf9c597f44f 151 discoveryRunning[i] = Discovery();
vcoubard 543:1bf9c597f44f 152 }
vcoubard 543:1bf9c597f44f 153 }
vcoubard 543:1bf9c597f44f 154 }
vcoubard 543:1bf9c597f44f 155
vcoubard 543:1bf9c597f44f 156 nRF5xCharacteristicDescriptorDiscoverer::Discovery*
vcoubard 543:1bf9c597f44f 157 nRF5xCharacteristicDescriptorDiscoverer::getAvailableDiscoverySlot() {
vcoubard 543:1bf9c597f44f 158 for(size_t i = 0; i < maximumConcurrentConnectionsCount; ++i) {
vcoubard 543:1bf9c597f44f 159 if(discoveryRunning[i] == Discovery()) {
vcoubard 543:1bf9c597f44f 160 return &discoveryRunning[i];
vcoubard 543:1bf9c597f44f 161 }
vcoubard 543:1bf9c597f44f 162 }
vcoubard 543:1bf9c597f44f 163 return NULL;
vcoubard 543:1bf9c597f44f 164 }
vcoubard 543:1bf9c597f44f 165
vcoubard 543:1bf9c597f44f 166 bool nRF5xCharacteristicDescriptorDiscoverer::isConnectionInUse(uint16_t connHandle) {
vcoubard 543:1bf9c597f44f 167 return findRunningDiscovery(connHandle) != NULL;
vcoubard 544:53215259c0d2 168 }
vcoubard 544:53215259c0d2 169
vcoubard 544:53215259c0d2 170 ble_error_t nRF5xCharacteristicDescriptorDiscoverer::gattc_descriptors_discover(
vcoubard 544:53215259c0d2 171 uint16_t connection_handle, uint16_t start_handle, uint16_t end_handle) {
vcoubard 544:53215259c0d2 172
vcoubard 544:53215259c0d2 173 ble_gattc_handle_range_t discoveryRange = {
vcoubard 544:53215259c0d2 174 start_handle,
vcoubard 544:53215259c0d2 175 end_handle
vcoubard 544:53215259c0d2 176 };
vcoubard 544:53215259c0d2 177 uint32_t err = sd_ble_gattc_descriptors_discover(connection_handle, &discoveryRange);
vcoubard 544:53215259c0d2 178
vcoubard 544:53215259c0d2 179 switch(err) {
vcoubard 544:53215259c0d2 180 case NRF_SUCCESS:
vcoubard 544:53215259c0d2 181 return BLE_ERROR_NONE;
vcoubard 544:53215259c0d2 182 case BLE_ERROR_INVALID_CONN_HANDLE:
vcoubard 544:53215259c0d2 183 return BLE_ERROR_INVALID_PARAM;
vcoubard 544:53215259c0d2 184 case NRF_ERROR_INVALID_ADDR:
vcoubard 544:53215259c0d2 185 return BLE_ERROR_PARAM_OUT_OF_RANGE;
vcoubard 544:53215259c0d2 186 case NRF_ERROR_BUSY:
vcoubard 544:53215259c0d2 187 return BLE_STACK_BUSY;
vcoubard 544:53215259c0d2 188 default:
vcoubard 544:53215259c0d2 189 return BLE_ERROR_UNSPECIFIED;
vcoubard 544:53215259c0d2 190 }
vcoubard 543:1bf9c597f44f 191 }