test

Dependencies:   nrf51-sdk

Dependents:   microbit-dal

Fork of nRF51822 by Lancaster University

Committer:
JKsoft_main
Date:
Tue Apr 17 12:10:00 2018 +0000
Revision:
624:c91bc279cbae
Parent:
616:a8f9b022d8fd
????

Who changed what in which revision?

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