Official Sheffield ARMBand micro:bit program

Committer:
MrBedfordVan
Date:
Mon Oct 17 12:41:20 2016 +0000
Revision:
0:b9164b348919
Official Sheffield ARMBand Micro:bit program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MrBedfordVan 0:b9164b348919 1 /* mbed Microcontroller Library
MrBedfordVan 0:b9164b348919 2 * Copyright (c) 2006-2015 ARM Limited
MrBedfordVan 0:b9164b348919 3 *
MrBedfordVan 0:b9164b348919 4 * Licensed under the Apache License, Version 2.0 (the "License");
MrBedfordVan 0:b9164b348919 5 * you may not use this file except in compliance with the License.
MrBedfordVan 0:b9164b348919 6 * You may obtain a copy of the License at
MrBedfordVan 0:b9164b348919 7 *
MrBedfordVan 0:b9164b348919 8 * http://www.apache.org/licenses/LICENSE-2.0
MrBedfordVan 0:b9164b348919 9 *
MrBedfordVan 0:b9164b348919 10 * Unless required by applicable law or agreed to in writing, software
MrBedfordVan 0:b9164b348919 11 * distributed under the License is distributed on an "AS IS" BASIS,
MrBedfordVan 0:b9164b348919 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
MrBedfordVan 0:b9164b348919 13 * See the License for the specific language governing permissions and
MrBedfordVan 0:b9164b348919 14 * limitations under the License.
MrBedfordVan 0:b9164b348919 15 */
MrBedfordVan 0:b9164b348919 16
MrBedfordVan 0:b9164b348919 17 #ifndef __NRF_CHARACTERISTIC_DESCRIPTOR_DISCOVERY_H__
MrBedfordVan 0:b9164b348919 18 #define __NRF_CHARACTERISTIC_DESCRIPTOR_DISCOVERY_H__
MrBedfordVan 0:b9164b348919 19
MrBedfordVan 0:b9164b348919 20 #include "ble/Gap.h"
MrBedfordVan 0:b9164b348919 21 #include "ble/DiscoveredCharacteristic.h"
MrBedfordVan 0:b9164b348919 22 #include "ble/CharacteristicDescriptorDiscovery.h"
MrBedfordVan 0:b9164b348919 23 #include "ble/GattClient.h"
MrBedfordVan 0:b9164b348919 24 #include "ble_gattc.h"
MrBedfordVan 0:b9164b348919 25
MrBedfordVan 0:b9164b348919 26 /**
MrBedfordVan 0:b9164b348919 27 * @brief Manage the discovery of Characteristic descriptors
MrBedfordVan 0:b9164b348919 28 * @details is a bridge between BLE API and Nordic stack regarding Characteristic
MrBedfordVan 0:b9164b348919 29 * Descriptor discovery. The BLE API can launch, monitor and ask for termination
MrBedfordVan 0:b9164b348919 30 * of a discovery. The Nordic stack will provide new descriptors and indicate when
MrBedfordVan 0:b9164b348919 31 * the discovery is done.
MrBedfordVan 0:b9164b348919 32 */
MrBedfordVan 0:b9164b348919 33 class nRF5xCharacteristicDescriptorDiscoverer
MrBedfordVan 0:b9164b348919 34 {
MrBedfordVan 0:b9164b348919 35 typedef CharacteristicDescriptorDiscovery::DiscoveryCallback_t DiscoveryCallback_t;
MrBedfordVan 0:b9164b348919 36 typedef CharacteristicDescriptorDiscovery::TerminationCallback_t TerminationCallback_t;
MrBedfordVan 0:b9164b348919 37
MrBedfordVan 0:b9164b348919 38 public:
MrBedfordVan 0:b9164b348919 39 /**
MrBedfordVan 0:b9164b348919 40 * @brief Construct a new characteristic descriptor discoverer.
MrBedfordVan 0:b9164b348919 41 */
MrBedfordVan 0:b9164b348919 42 nRF5xCharacteristicDescriptorDiscoverer();
MrBedfordVan 0:b9164b348919 43
MrBedfordVan 0:b9164b348919 44 /**
MrBedfordVan 0:b9164b348919 45 * @brief Destroy a characteristic descriptor discoverer.
MrBedfordVan 0:b9164b348919 46 */
MrBedfordVan 0:b9164b348919 47 ~nRF5xCharacteristicDescriptorDiscoverer();
MrBedfordVan 0:b9164b348919 48
MrBedfordVan 0:b9164b348919 49 /**
MrBedfordVan 0:b9164b348919 50 * Launch a new characteristic descriptor discovery for a given DiscoveredCharacteristic.
MrBedfordVan 0:b9164b348919 51 * @param characteristic The characteristic owning the descriptors to discover.
MrBedfordVan 0:b9164b348919 52 * @param discoveryCallback The callback called when a descriptor is discovered.
MrBedfordVan 0:b9164b348919 53 * @param terminationCallback The callback called when the discovery process end.
MrBedfordVan 0:b9164b348919 54 * @return BLE_ERROR_NONE if characteristic descriptor discovery is launched successfully;
MrBedfordVan 0:b9164b348919 55 * else an appropriate error.
MrBedfordVan 0:b9164b348919 56 * @note: this will be called by BLE API side.
MrBedfordVan 0:b9164b348919 57 */
MrBedfordVan 0:b9164b348919 58 ble_error_t launch(
MrBedfordVan 0:b9164b348919 59 const DiscoveredCharacteristic& characteristic,
MrBedfordVan 0:b9164b348919 60 const DiscoveryCallback_t& discoveryCallback,
MrBedfordVan 0:b9164b348919 61 const TerminationCallback_t& terminationCallback
MrBedfordVan 0:b9164b348919 62 );
MrBedfordVan 0:b9164b348919 63
MrBedfordVan 0:b9164b348919 64 /**
MrBedfordVan 0:b9164b348919 65 * @brief indicate if a characteristic descriptor discovery is active for a
MrBedfordVan 0:b9164b348919 66 * given DiscoveredCharacteristic.
MrBedfordVan 0:b9164b348919 67 * @param characteristic The characteristic for whom the descriptor might be
MrBedfordVan 0:b9164b348919 68 * currently discovered.
MrBedfordVan 0:b9164b348919 69 * @return true if descriptors of characteristic are discovered, false otherwise.
MrBedfordVan 0:b9164b348919 70 * @note: this will be called by BLE API side.
MrBedfordVan 0:b9164b348919 71 */
MrBedfordVan 0:b9164b348919 72 bool isActive(const DiscoveredCharacteristic& characteristic) const;
MrBedfordVan 0:b9164b348919 73
MrBedfordVan 0:b9164b348919 74 /**
MrBedfordVan 0:b9164b348919 75 * @brief request the termination of characteristic descriptor discovery
MrBedfordVan 0:b9164b348919 76 * for a give DiscoveredCharacteristic
MrBedfordVan 0:b9164b348919 77 * @param characteristic The characteristic for whom the descriptor discovery
MrBedfordVan 0:b9164b348919 78 * should be stopped.
MrBedfordVan 0:b9164b348919 79 * @note: this will be called by BLE API side.
MrBedfordVan 0:b9164b348919 80 */
MrBedfordVan 0:b9164b348919 81 void requestTerminate(const DiscoveredCharacteristic& characteristic);
MrBedfordVan 0:b9164b348919 82
MrBedfordVan 0:b9164b348919 83 /**
MrBedfordVan 0:b9164b348919 84 * @brief process descriptors discovered from the Nordic stack.
MrBedfordVan 0:b9164b348919 85 * @param connectionHandle The connection handle upon which descriptors has been
MrBedfordVan 0:b9164b348919 86 * discovered.
MrBedfordVan 0:b9164b348919 87 * @param descriptors Discovered descriptors.
MrBedfordVan 0:b9164b348919 88 * @note This will be called by the Nordic stack.
MrBedfordVan 0:b9164b348919 89 */
MrBedfordVan 0:b9164b348919 90 void process(uint16_t connectionHandle, const ble_gattc_evt_desc_disc_rsp_t& descriptors);
MrBedfordVan 0:b9164b348919 91
MrBedfordVan 0:b9164b348919 92 /**
MrBedfordVan 0:b9164b348919 93 * @brief Called by the Nordic stack when the discovery is over.
MrBedfordVan 0:b9164b348919 94 * @param The connection handle upon which the discovery process is done.
MrBedfordVan 0:b9164b348919 95 * @param err An error if the termination is due to an error.
MrBedfordVan 0:b9164b348919 96 */
MrBedfordVan 0:b9164b348919 97 void terminate(uint16_t connectionHandle, ble_error_t err);
MrBedfordVan 0:b9164b348919 98
MrBedfordVan 0:b9164b348919 99 private:
MrBedfordVan 0:b9164b348919 100 // protection against copy construction and assignment
MrBedfordVan 0:b9164b348919 101 nRF5xCharacteristicDescriptorDiscoverer(const nRF5xCharacteristicDescriptorDiscoverer&);
MrBedfordVan 0:b9164b348919 102 nRF5xCharacteristicDescriptorDiscoverer& operator=(const nRF5xCharacteristicDescriptorDiscoverer&);
MrBedfordVan 0:b9164b348919 103
MrBedfordVan 0:b9164b348919 104 /**
MrBedfordVan 0:b9164b348919 105 * @brief Discovery process, it store the DiscoveredCharacteristic, the
MrBedfordVan 0:b9164b348919 106 * discovery callback and the termination callback.
MrBedfordVan 0:b9164b348919 107 */
MrBedfordVan 0:b9164b348919 108 class Discovery {
MrBedfordVan 0:b9164b348919 109 public:
MrBedfordVan 0:b9164b348919 110 /**
MrBedfordVan 0:b9164b348919 111 * @brief Construct an empty discovery, such can be considerate as a not running discovery.
MrBedfordVan 0:b9164b348919 112 * @note #isEmpty function will return true
MrBedfordVan 0:b9164b348919 113 */
MrBedfordVan 0:b9164b348919 114 Discovery();
MrBedfordVan 0:b9164b348919 115
MrBedfordVan 0:b9164b348919 116 /**
MrBedfordVan 0:b9164b348919 117 * @brief Construct a valid discovery process.
MrBedfordVan 0:b9164b348919 118 *
MrBedfordVan 0:b9164b348919 119 * @param c the characteristic from whom descriptors will be discovered.
MrBedfordVan 0:b9164b348919 120 * @param dCb The discovery callback called each time a descriptor is discovered.
MrBedfordVan 0:b9164b348919 121 * @param tCb The termination callback called when the discovery terminate.
MrBedfordVan 0:b9164b348919 122 *
MrBedfordVan 0:b9164b348919 123 * @note #isEmpty function will return false
MrBedfordVan 0:b9164b348919 124 */
MrBedfordVan 0:b9164b348919 125 Discovery(const DiscoveredCharacteristic& c, const DiscoveryCallback_t& dCb, const TerminationCallback_t& tCb);
MrBedfordVan 0:b9164b348919 126
MrBedfordVan 0:b9164b348919 127 /**
MrBedfordVan 0:b9164b348919 128 * @brief Process the discovery of a descriptor.
MrBedfordVan 0:b9164b348919 129 *
MrBedfordVan 0:b9164b348919 130 * @param handle The attribute handle of the descriptor found
MrBedfordVan 0:b9164b348919 131 * @param uuid The UUID of the descriptor found.
MrBedfordVan 0:b9164b348919 132 */
MrBedfordVan 0:b9164b348919 133 void process(GattAttribute::Handle_t handle, const UUID& uuid);
MrBedfordVan 0:b9164b348919 134
MrBedfordVan 0:b9164b348919 135 /**
MrBedfordVan 0:b9164b348919 136 * @brief Terminate the discovery process.
MrBedfordVan 0:b9164b348919 137 *
MrBedfordVan 0:b9164b348919 138 * @param err Error associate with the termination
MrBedfordVan 0:b9164b348919 139 * @note after this call #isEmpty function will return true.
MrBedfordVan 0:b9164b348919 140 */
MrBedfordVan 0:b9164b348919 141 void terminate(ble_error_t err);
MrBedfordVan 0:b9164b348919 142
MrBedfordVan 0:b9164b348919 143 /**
MrBedfordVan 0:b9164b348919 144 * @brief check if the discovery process is empty or not. Empty discovery are
MrBedfordVan 0:b9164b348919 145 * not running.
MrBedfordVan 0:b9164b348919 146 *
MrBedfordVan 0:b9164b348919 147 * @detail Discovery are empty after:
MrBedfordVan 0:b9164b348919 148 * - a default construction
MrBedfordVan 0:b9164b348919 149 * - a copy construction form a default constructed
MrBedfordVan 0:b9164b348919 150 * - an assignment from a default constructed Discovery
MrBedfordVan 0:b9164b348919 151 * @return true if the Discovery is empty and false otherwise.
MrBedfordVan 0:b9164b348919 152 */
MrBedfordVan 0:b9164b348919 153 bool isEmpty() const;
MrBedfordVan 0:b9164b348919 154
MrBedfordVan 0:b9164b348919 155 /**
MrBedfordVan 0:b9164b348919 156 * @brief return the characteristic from whom descriptors are discovered.
MrBedfordVan 0:b9164b348919 157 * @return the characteristic from whom descriptors are discovered.
MrBedfordVan 0:b9164b348919 158 */
MrBedfordVan 0:b9164b348919 159 const DiscoveredCharacteristic& getCharacteristic() const;
MrBedfordVan 0:b9164b348919 160
MrBedfordVan 0:b9164b348919 161 /**
MrBedfordVan 0:b9164b348919 162 * @brief equal to operator, test if two discovery process are equal
MrBedfordVan 0:b9164b348919 163 *
MrBedfordVan 0:b9164b348919 164 * @param lhs left hand side of the expression
MrBedfordVan 0:b9164b348919 165 * @param rhs right hand side of the expression
MrBedfordVan 0:b9164b348919 166 * @return true if lhs == rhs
MrBedfordVan 0:b9164b348919 167 */
MrBedfordVan 0:b9164b348919 168 friend bool operator==(const Discovery& lhs, const Discovery& rhs) {
MrBedfordVan 0:b9164b348919 169 return lhs.characteristic == rhs.characteristic &&
MrBedfordVan 0:b9164b348919 170 lhs.onDiscovery == rhs.onDiscovery &&
MrBedfordVan 0:b9164b348919 171 lhs.onTerminate == rhs.onTerminate;
MrBedfordVan 0:b9164b348919 172 }
MrBedfordVan 0:b9164b348919 173
MrBedfordVan 0:b9164b348919 174 /**
MrBedfordVan 0:b9164b348919 175 * @brief not equal to operator, test if two discovery process are not equal
MrBedfordVan 0:b9164b348919 176 *
MrBedfordVan 0:b9164b348919 177 * @param lhs left hand side of the expression
MrBedfordVan 0:b9164b348919 178 * @param rhs right hand side of the expression
MrBedfordVan 0:b9164b348919 179 * @return true if lhs != rhs
MrBedfordVan 0:b9164b348919 180 */
MrBedfordVan 0:b9164b348919 181 friend bool operator!=(const Discovery& lhs, const Discovery& rhs) {
MrBedfordVan 0:b9164b348919 182 return !(lhs == rhs);
MrBedfordVan 0:b9164b348919 183 }
MrBedfordVan 0:b9164b348919 184
MrBedfordVan 0:b9164b348919 185 private:
MrBedfordVan 0:b9164b348919 186 DiscoveredCharacteristic characteristic;
MrBedfordVan 0:b9164b348919 187 DiscoveryCallback_t onDiscovery;
MrBedfordVan 0:b9164b348919 188 TerminationCallback_t onTerminate;
MrBedfordVan 0:b9164b348919 189 };
MrBedfordVan 0:b9164b348919 190
MrBedfordVan 0:b9164b348919 191 // find a running discovery process
MrBedfordVan 0:b9164b348919 192 Discovery* findRunningDiscovery(const DiscoveredCharacteristic& characteristic);
MrBedfordVan 0:b9164b348919 193 Discovery* findRunningDiscovery(uint16_t handle);
MrBedfordVan 0:b9164b348919 194
MrBedfordVan 0:b9164b348919 195 // Called to terminate a discovery is over.
MrBedfordVan 0:b9164b348919 196 void terminate(Discovery* discovery, ble_error_t err);
MrBedfordVan 0:b9164b348919 197
MrBedfordVan 0:b9164b348919 198 // get one slot for a discovery process
MrBedfordVan 0:b9164b348919 199 Discovery* getAvailableDiscoverySlot();
MrBedfordVan 0:b9164b348919 200
MrBedfordVan 0:b9164b348919 201 // indicate if a connection is already running a discovery
MrBedfordVan 0:b9164b348919 202 bool isConnectionInUse(uint16_t connHandle);
MrBedfordVan 0:b9164b348919 203
MrBedfordVan 0:b9164b348919 204 // low level start of a discovery
MrBedfordVan 0:b9164b348919 205 static ble_error_t gattc_descriptors_discover(uint16_t connection_handle, uint16_t start_handle, uint16_t end_handle);
MrBedfordVan 0:b9164b348919 206
MrBedfordVan 0:b9164b348919 207 // count of concurrent connections which can run a descriptor discovery process
MrBedfordVan 0:b9164b348919 208 static const size_t MAXIMUM_CONCURRENT_CONNECTIONS_COUNT = 3;
MrBedfordVan 0:b9164b348919 209
MrBedfordVan 0:b9164b348919 210 // array of running discoveries
MrBedfordVan 0:b9164b348919 211 Discovery discoveryRunning[MAXIMUM_CONCURRENT_CONNECTIONS_COUNT];
MrBedfordVan 0:b9164b348919 212 };
MrBedfordVan 0:b9164b348919 213
MrBedfordVan 0:b9164b348919 214 #endif /*__NRF_CHARACTERISTIC_DESCRIPTOR_DISCOVERY_H__*/