nordic

Fork of nRF51822 by Nordic Semiconductor

Committer:
vcoubard
Date:
Mon Jan 11 23:40:33 2016 +0000
Revision:
610:e640f9176975
Parent:
591:3bdd5346ded1
Synchronized with git rev 29d53c29
Author: Vincent Coubard
Remove dependency to mbed-error

Who changed what in which revision?

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