test

Dependencies:   nrf51-sdk

Dependents:   microbit-dal

Fork of nRF51822 by Lancaster University

Committer:
rgrover1
Date:
Fri Jun 19 15:55:29 2015 +0100
Revision:
308:482f16f3f7f5
Parent:
307:3889582936c9
Child:
309:ef9e806a7b03
Synchronized with git rev 4899e235
Author: Rohit Grover
introduce nRFDiscoveredCharacteristic.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 239:693a1f145b5a 1 /* mbed Microcontroller Library
rgrover1 239:693a1f145b5a 2 * Copyright (c) 2006-2013 ARM Limited
rgrover1 239:693a1f145b5a 3 *
rgrover1 239:693a1f145b5a 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 239:693a1f145b5a 5 * you may not use this file except in compliance with the License.
rgrover1 239:693a1f145b5a 6 * You may obtain a copy of the License at
rgrover1 239:693a1f145b5a 7 *
rgrover1 239:693a1f145b5a 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 239:693a1f145b5a 9 *
rgrover1 239:693a1f145b5a 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 239:693a1f145b5a 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 239:693a1f145b5a 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 239:693a1f145b5a 13 * See the License for the specific language governing permissions and
rgrover1 239:693a1f145b5a 14 * limitations under the License.
rgrover1 239:693a1f145b5a 15 */
rgrover1 239:693a1f145b5a 16
rgrover1 239:693a1f145b5a 17 #ifndef _BTLE_DISCOVERY_H_
rgrover1 239:693a1f145b5a 18 #define _BTLE_DISCOVERY_H_
rgrover1 239:693a1f145b5a 19
rgrover1 247:df37e7bb3f71 20 #include "ble.h"
rgrover1 263:d5da37e9a93b 21 #include "ServiceDiscovery.h"
rgrover1 247:df37e7bb3f71 22
rgrover1 247:df37e7bb3f71 23 void bleGattcEventHandler(const ble_evt_t *p_ble_evt);
rgrover1 239:693a1f145b5a 24
rgrover1 308:482f16f3f7f5 25 class nRFDiscoveredCharacteristic : public DiscoveredCharacteristic {
rgrover1 308:482f16f3f7f5 26 public:
rgrover1 308:482f16f3f7f5 27 /**
rgrover1 308:482f16f3f7f5 28 * Initiate (or continue) a read for the value attribute, optionally at a
rgrover1 308:482f16f3f7f5 29 * given offset. If the Characteristic or Descriptor to be read is longer
rgrover1 308:482f16f3f7f5 30 * than ATT_MTU - 1, this function must be called multiple times with
rgrover1 308:482f16f3f7f5 31 * appropriate offset to read the complete value.
rgrover1 308:482f16f3f7f5 32 */
rgrover1 308:482f16f3f7f5 33 virtual void read(uint16_t offset = 0) {
rgrover1 308:482f16f3f7f5 34 /**@brief Initiate or continue a GATT Read (Long) Characteristic or Descriptor procedure.
rgrover1 308:482f16f3f7f5 35 *
rgrover1 308:482f16f3f7f5 36 * @details This function initiates or resumes a GATT Read (Long) Characteristic or Descriptor procedure. If the Characteristic or Descriptor
rgrover1 308:482f16f3f7f5 37 * to be read is longer than ATT_MTU - 1, this function must be called multiple times with appropriate offset to read the
rgrover1 308:482f16f3f7f5 38 * complete value.
rgrover1 308:482f16f3f7f5 39 *
rgrover1 308:482f16f3f7f5 40 * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on.
rgrover1 308:482f16f3f7f5 41 * @param[in] handle The handle of the attribute to be read.
rgrover1 308:482f16f3f7f5 42 * @param[in] offset Offset into the attribute value to be read.
rgrover1 308:482f16f3f7f5 43 *
rgrover1 308:482f16f3f7f5 44 * @retval ::NRF_SUCCESS Successfully started or resumed the Read (Long) procedure.
rgrover1 308:482f16f3f7f5 45 * @retval ::BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle.
rgrover1 308:482f16f3f7f5 46 * @retval ::NRF_ERROR_INVALID_STATE Invalid Connection State.
rgrover1 308:482f16f3f7f5 47 * @retval ::NRF_ERROR_INVALID_ADDR Invalid pointer supplied.
rgrover1 308:482f16f3f7f5 48 * @retval ::NRF_ERROR_BUSY Client procedure already in progress.
rgrover1 308:482f16f3f7f5 49 */
rgrover1 308:482f16f3f7f5 50 // sd_ble_gattc_read()
rgrover1 308:482f16f3f7f5 51 // SVCALL(SD_BLE_GATTC_READ, uint32_t, sd_ble_gattc_read(uint16_t conn_handle, uint16_t handle, uint16_t offset));
rgrover1 308:482f16f3f7f5 52 }
rgrover1 308:482f16f3f7f5 53 };
rgrover1 308:482f16f3f7f5 54
rgrover1 250:db3c51656388 55 class NordicServiceDiscovery : public ServiceDiscovery
rgrover1 250:db3c51656388 56 {
rgrover1 248:71ef03789dd3 57 public:
rgrover1 260:ea2798f615e5 58 static const uint16_t SRV_DISC_START_HANDLE = 0x0001; /**< The start handle value used during service discovery. */
rgrover1 284:29fb6af6671d 59 static const uint16_t SRV_DISC_END_HANDLE = 0xFFFF; /**< The end handle value used during service discovery. */
rgrover1 260:ea2798f615e5 60
rgrover1 289:b33301b019e6 61 public:
rgrover1 248:71ef03789dd3 62 static const unsigned BLE_DB_DISCOVERY_MAX_SRV = 4; /**< Maximum number of services we can retain information for after a single discovery. */
rgrover1 248:71ef03789dd3 63 static const unsigned BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV = 4; /**< Maximum number of characteristics per service we can retain information for. */
rgrover1 260:ea2798f615e5 64
rgrover1 260:ea2798f615e5 65 public:
rgrover1 288:1d85c4a4d397 66 NordicServiceDiscovery() : serviceIndex(0),
rgrover1 288:1d85c4a4d397 67 numServices(0),
rgrover1 288:1d85c4a4d397 68 characteristicIndex(0),
rgrover1 288:1d85c4a4d397 69 numCharacteristics(0),
rgrover1 288:1d85c4a4d397 70 state(INACTIVE),
rgrover1 288:1d85c4a4d397 71 services(),
rgrover1 288:1d85c4a4d397 72 characteristics(),
rgrover1 305:293634834813 73 serviceUUIDDiscoveryQueue(this),
rgrover1 306:76e2e7349319 74 charUUIDDiscoveryQueue(this),
rgrover1 288:1d85c4a4d397 75 onTerminationCallback(NULL) {
rgrover1 288:1d85c4a4d397 76 /* empty */
rgrover1 288:1d85c4a4d397 77 }
rgrover1 288:1d85c4a4d397 78
rgrover1 288:1d85c4a4d397 79 public:
rgrover1 260:ea2798f615e5 80 ble_error_t launchCharacteristicDiscovery(Gap::Handle_t connectionHandle, Gap::Handle_t startHandle, Gap::Handle_t endHandle);
rgrover1 239:693a1f145b5a 81
rgrover1 239:693a1f145b5a 82 public:
rgrover1 248:71ef03789dd3 83 void setupDiscoveredServices(const ble_gattc_evt_prim_srvc_disc_rsp_t *response);
rgrover1 248:71ef03789dd3 84 void setupDiscoveredCharacteristics(const ble_gattc_evt_char_disc_rsp_t *response);
rgrover1 248:71ef03789dd3 85
rgrover1 290:83c994bf62d0 86 void triggerServiceUUIDDiscovery(void);
rgrover1 290:83c994bf62d0 87 void processDiscoverUUIDResponse(const ble_gattc_evt_char_val_by_uuid_read_rsp_t *response);
rgrover1 290:83c994bf62d0 88 void removeFirstServiceNeedingUUIDDiscovery(void);
rgrover1 290:83c994bf62d0 89
rgrover1 248:71ef03789dd3 90 void terminateServiceDiscovery(void) {
rgrover1 280:cbaa4cb83548 91 bool wasActive = isActive();
rgrover1 286:042b01e59cf8 92 state = INACTIVE;
rgrover1 280:cbaa4cb83548 93
rgrover1 280:cbaa4cb83548 94 if (wasActive && onTerminationCallback) {
rgrover1 280:cbaa4cb83548 95 onTerminationCallback(connHandle);
rgrover1 280:cbaa4cb83548 96 }
rgrover1 239:693a1f145b5a 97 }
rgrover1 239:693a1f145b5a 98
rgrover1 245:3abc61d38db3 99 void terminateCharacteristicDiscovery(void) {
rgrover1 286:042b01e59cf8 100 if (state == CHARACTERISTIC_DISCOVERY_ACTIVE) {
rgrover1 286:042b01e59cf8 101 state = SERVICE_DISCOVERY_ACTIVE;
rgrover1 286:042b01e59cf8 102 }
rgrover1 245:3abc61d38db3 103 serviceIndex++; /* Progress service index to keep discovery alive. */
rgrover1 245:3abc61d38db3 104 }
rgrover1 245:3abc61d38db3 105
rgrover1 279:6e5e79f1fb61 106 bool isActive(void) const {
rgrover1 286:042b01e59cf8 107 return state != INACTIVE;
rgrover1 279:6e5e79f1fb61 108 }
rgrover1 279:6e5e79f1fb61 109
rgrover1 280:cbaa4cb83548 110 void setOnTermination(TerminationCallback_t callback) {
rgrover1 280:cbaa4cb83548 111 onTerminationCallback = callback;
rgrover1 280:cbaa4cb83548 112 }
rgrover1 280:cbaa4cb83548 113
rgrover1 260:ea2798f615e5 114 private:
rgrover1 239:693a1f145b5a 115 void resetDiscoveredServices(void) {
rgrover1 242:73fc02cc20b1 116 numServices = 0;
rgrover1 242:73fc02cc20b1 117 serviceIndex = 0;
rgrover1 239:693a1f145b5a 118 memset(services, 0, sizeof(DiscoveredService) * BLE_DB_DISCOVERY_MAX_SRV);
rgrover1 239:693a1f145b5a 119 }
rgrover1 239:693a1f145b5a 120
rgrover1 245:3abc61d38db3 121 void resetDiscoveredCharacteristics(void) {
rgrover1 245:3abc61d38db3 122 numCharacteristics = 0;
rgrover1 245:3abc61d38db3 123 characteristicIndex = 0;
rgrover1 245:3abc61d38db3 124 memset(characteristics, 0, sizeof(DiscoveredCharacteristic) * BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV);
rgrover1 245:3abc61d38db3 125 }
rgrover1 245:3abc61d38db3 126
rgrover1 245:3abc61d38db3 127 public:
rgrover1 243:a966506d1e5b 128 void serviceDiscoveryStarted(Gap::Handle_t connectionHandle) {
rgrover1 291:d7590ed48009 129 connHandle = connectionHandle;
rgrover1 243:a966506d1e5b 130 resetDiscoveredServices();
rgrover1 286:042b01e59cf8 131 state = SERVICE_DISCOVERY_ACTIVE;
rgrover1 243:a966506d1e5b 132 }
rgrover1 243:a966506d1e5b 133
rgrover1 260:ea2798f615e5 134 private:
rgrover1 245:3abc61d38db3 135 void characteristicDiscoveryStarted(Gap::Handle_t connectionHandle) {
rgrover1 291:d7590ed48009 136 connHandle = connectionHandle;
rgrover1 245:3abc61d38db3 137 resetDiscoveredCharacteristics();
rgrover1 286:042b01e59cf8 138 state = CHARACTERISTIC_DISCOVERY_ACTIVE;
rgrover1 245:3abc61d38db3 139 }
rgrover1 245:3abc61d38db3 140
rgrover1 251:d4e0cf5e8751 141 private:
rgrover1 293:cd86acfe41be 142 /**
rgrover1 293:cd86acfe41be 143 * A datatype to contain service-indices for which long UUIDs need to be
rgrover1 293:cd86acfe41be 144 * discovered using read_val_by_uuid().
rgrover1 293:cd86acfe41be 145 */
rgrover1 305:293634834813 146 class ServiceUUIDDiscoveryQueue {
rgrover1 292:01b717803437 147 public:
rgrover1 305:293634834813 148 ServiceUUIDDiscoveryQueue(NordicServiceDiscovery *parent) :
rgrover1 292:01b717803437 149 numIndices(0),
rgrover1 292:01b717803437 150 serviceIndices(),
rgrover1 301:59e7404a4ea3 151 parentDiscoveryObject(parent) {
rgrover1 292:01b717803437 152 /* empty */
rgrover1 292:01b717803437 153 }
rgrover1 292:01b717803437 154
rgrover1 292:01b717803437 155 public:
rgrover1 292:01b717803437 156 void reset(void) {
rgrover1 292:01b717803437 157 numIndices = 0;
rgrover1 304:8677dcdb218d 158 for (unsigned i = 0; i < BLE_DB_DISCOVERY_MAX_SRV; i++) {
rgrover1 304:8677dcdb218d 159 serviceIndices[i] = INVALID_INDEX;
rgrover1 292:01b717803437 160 }
rgrover1 292:01b717803437 161 }
rgrover1 299:19064275c0e0 162 void enqueue(int serviceIndex) {
rgrover1 292:01b717803437 163 serviceIndices[numIndices++] = serviceIndex;
rgrover1 292:01b717803437 164 }
rgrover1 299:19064275c0e0 165 int dequeue(void) {
rgrover1 299:19064275c0e0 166 if (numIndices == 0) {
rgrover1 304:8677dcdb218d 167 return INVALID_INDEX;
rgrover1 299:19064275c0e0 168 }
rgrover1 299:19064275c0e0 169
rgrover1 299:19064275c0e0 170 unsigned valueToReturn = serviceIndices[0];
rgrover1 292:01b717803437 171 numIndices--;
rgrover1 299:19064275c0e0 172 for (unsigned i = 0; i < numIndices; i++) {
rgrover1 299:19064275c0e0 173 serviceIndices[i] = serviceIndices[i + 1];
rgrover1 292:01b717803437 174 }
rgrover1 299:19064275c0e0 175
rgrover1 299:19064275c0e0 176 return valueToReturn;
rgrover1 292:01b717803437 177 }
rgrover1 302:507e99065379 178 unsigned getFirst(void) const {
rgrover1 302:507e99065379 179 return serviceIndices[0];
rgrover1 302:507e99065379 180 }
rgrover1 292:01b717803437 181 size_t getCount(void) const {
rgrover1 292:01b717803437 182 return numIndices;
rgrover1 292:01b717803437 183 }
rgrover1 292:01b717803437 184
rgrover1 303:697d18859583 185 /**
rgrover1 303:697d18859583 186 * Trigger UUID discovery for the first of the enqueued ServiceIndices.
rgrover1 303:697d18859583 187 */
rgrover1 292:01b717803437 188 void triggerFirst(void);
rgrover1 292:01b717803437 189
rgrover1 292:01b717803437 190 private:
rgrover1 304:8677dcdb218d 191 static const int INVALID_INDEX = -1;
rgrover1 292:01b717803437 192
rgrover1 292:01b717803437 193 private:
rgrover1 292:01b717803437 194 size_t numIndices;
rgrover1 292:01b717803437 195 int serviceIndices[BLE_DB_DISCOVERY_MAX_SRV];
rgrover1 292:01b717803437 196
rgrover1 301:59e7404a4ea3 197 NordicServiceDiscovery *parentDiscoveryObject;
rgrover1 292:01b717803437 198 };
rgrover1 305:293634834813 199 friend class ServiceUUIDDiscoveryQueue;
rgrover1 292:01b717803437 200
rgrover1 306:76e2e7349319 201 /**
rgrover1 306:76e2e7349319 202 * A datatype to contain characteristic-indices for which long UUIDs need to
rgrover1 306:76e2e7349319 203 * be discovered using read_val_by_uuid().
rgrover1 306:76e2e7349319 204 */
rgrover1 306:76e2e7349319 205 class CharUUIDDiscoveryQueue {
rgrover1 306:76e2e7349319 206 public:
rgrover1 306:76e2e7349319 207 CharUUIDDiscoveryQueue(NordicServiceDiscovery *parent) :
rgrover1 306:76e2e7349319 208 numIndices(0),
rgrover1 306:76e2e7349319 209 charIndices(),
rgrover1 306:76e2e7349319 210 parentDiscoveryObject(parent) {
rgrover1 306:76e2e7349319 211 /* empty */
rgrover1 306:76e2e7349319 212 }
rgrover1 306:76e2e7349319 213
rgrover1 306:76e2e7349319 214 public:
rgrover1 306:76e2e7349319 215 void reset(void) {
rgrover1 306:76e2e7349319 216 numIndices = 0;
rgrover1 306:76e2e7349319 217 for (unsigned i = 0; i < BLE_DB_DISCOVERY_MAX_SRV; i++) {
rgrover1 306:76e2e7349319 218 charIndices[i] = INVALID_INDEX;
rgrover1 306:76e2e7349319 219 }
rgrover1 306:76e2e7349319 220 }
rgrover1 306:76e2e7349319 221 void enqueue(int serviceIndex) {
rgrover1 306:76e2e7349319 222 charIndices[numIndices++] = serviceIndex;
rgrover1 306:76e2e7349319 223 }
rgrover1 306:76e2e7349319 224 int dequeue(void) {
rgrover1 306:76e2e7349319 225 if (numIndices == 0) {
rgrover1 306:76e2e7349319 226 return INVALID_INDEX;
rgrover1 306:76e2e7349319 227 }
rgrover1 306:76e2e7349319 228
rgrover1 306:76e2e7349319 229 unsigned valueToReturn = charIndices[0];
rgrover1 306:76e2e7349319 230 numIndices--;
rgrover1 306:76e2e7349319 231 for (unsigned i = 0; i < numIndices; i++) {
rgrover1 306:76e2e7349319 232 charIndices[i] = charIndices[i + 1];
rgrover1 306:76e2e7349319 233 }
rgrover1 306:76e2e7349319 234
rgrover1 306:76e2e7349319 235 return valueToReturn;
rgrover1 306:76e2e7349319 236 }
rgrover1 306:76e2e7349319 237 unsigned getFirst(void) const {
rgrover1 306:76e2e7349319 238 return charIndices[0];
rgrover1 306:76e2e7349319 239 }
rgrover1 306:76e2e7349319 240 size_t getCount(void) const {
rgrover1 306:76e2e7349319 241 return numIndices;
rgrover1 306:76e2e7349319 242 }
rgrover1 306:76e2e7349319 243
rgrover1 306:76e2e7349319 244 /**
rgrover1 306:76e2e7349319 245 * Trigger UUID discovery for the first of the enqueued charIndices.
rgrover1 306:76e2e7349319 246 */
rgrover1 306:76e2e7349319 247 void triggerFirst(void);
rgrover1 306:76e2e7349319 248
rgrover1 306:76e2e7349319 249 private:
rgrover1 306:76e2e7349319 250 static const int INVALID_INDEX = -1;
rgrover1 306:76e2e7349319 251
rgrover1 306:76e2e7349319 252 private:
rgrover1 306:76e2e7349319 253 size_t numIndices;
rgrover1 306:76e2e7349319 254 int charIndices[BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV];
rgrover1 306:76e2e7349319 255
rgrover1 306:76e2e7349319 256 NordicServiceDiscovery *parentDiscoveryObject;
rgrover1 306:76e2e7349319 257 };
rgrover1 306:76e2e7349319 258 friend class CharUUIDDiscoveryQueue;
rgrover1 306:76e2e7349319 259
rgrover1 292:01b717803437 260 private:
rgrover1 251:d4e0cf5e8751 261 friend void bleGattcEventHandler(const ble_evt_t *p_ble_evt);
rgrover1 251:d4e0cf5e8751 262 void progressCharacteristicDiscovery(void);
rgrover1 251:d4e0cf5e8751 263 void progressServiceDiscovery(void);
rgrover1 239:693a1f145b5a 264
rgrover1 248:71ef03789dd3 265 private:
rgrover1 248:71ef03789dd3 266 uint8_t serviceIndex; /**< Index of the current service being discovered. This is intended for internal use during service discovery.*/
rgrover1 248:71ef03789dd3 267 uint8_t numServices; /**< Number of services at the peers GATT database.*/
rgrover1 248:71ef03789dd3 268 uint8_t characteristicIndex; /**< Index of the current characteristic being discovered. This is intended for internal use during service discovery.*/
rgrover1 248:71ef03789dd3 269 uint8_t numCharacteristics; /**< Number of characteristics within the service.*/
rgrover1 248:71ef03789dd3 270
rgrover1 286:042b01e59cf8 271 enum State_t {
rgrover1 286:042b01e59cf8 272 INACTIVE,
rgrover1 286:042b01e59cf8 273 SERVICE_DISCOVERY_ACTIVE,
rgrover1 286:042b01e59cf8 274 CHARACTERISTIC_DISCOVERY_ACTIVE,
rgrover1 290:83c994bf62d0 275 DISCOVER_SERVICE_UUIDS,
rgrover1 306:76e2e7349319 276 DISCOVER_CHARACTERISTIC_UUIDS,
rgrover1 286:042b01e59cf8 277 } state;
rgrover1 248:71ef03789dd3 278
rgrover1 308:482f16f3f7f5 279 DiscoveredService services[BLE_DB_DISCOVERY_MAX_SRV]; /**< Information related to the current service being discovered.
rgrover1 308:482f16f3f7f5 280 * This is intended for internal use during service discovery. */
rgrover1 308:482f16f3f7f5 281 nRFDiscoveredCharacteristic characteristics[BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV];
rgrover1 280:cbaa4cb83548 282
rgrover1 308:482f16f3f7f5 283 ServiceUUIDDiscoveryQueue serviceUUIDDiscoveryQueue;
rgrover1 308:482f16f3f7f5 284 CharUUIDDiscoveryQueue charUUIDDiscoveryQueue;
rgrover1 292:01b717803437 285
rgrover1 308:482f16f3f7f5 286 TerminationCallback_t onTerminationCallback;
rgrover1 240:75b69581d1dd 287 };
rgrover1 240:75b69581d1dd 288
rgrover1 239:693a1f145b5a 289 #endif /*_BTLE_DISCOVERY_H_*/