config changes

Fork of nRF51822 by Nordic Semiconductor

Committer:
RobTT
Date:
Thu Mar 09 10:49:23 2017 +0000
Revision:
639:be81711532c9
Parent:
638:c90ae1400bf2
Internal test version 1

Who changed what in which revision?

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