Nordic stack and drivers for the mbed BLE API. Version to work around build bug.

Dependents:   microbit_rubber_ducky microbit_mouse_BLE microbit_mouse_BLE_daybreak_version microbit_presenter

Fork of nRF51822 by Nordic Semiconductor

Committer:
vcoubard
Date:
Mon Jan 11 10:19:31 2016 +0000
Revision:
590:3bdd5346ded1
Child:
609:e640f9176975
Synchronized with git rev 19d1c406
Author: Vincent Coubard
Merge branch 'develop' of https://github.com/ARMmbed/ble-nrf51822 into characteristicDescriptorDiscovery

Who changed what in which revision?

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