test

Fork of nRF51822 by Nordic Semiconductor

Committer:
rgrover1
Date:
Fri Jun 19 15:55:30 2015 +0100
Revision:
310:4d7ccb8c2a51
Parent:
309:ef9e806a7b03
Child:
311:5d5abc30e3a8
Synchronized with git rev 543e493d
Author: Rohit Grover
add to the comment header for DiscoveredCharacteristic::read()

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 310:4d7ccb8c2a51 32 *
rgrover1 310:4d7ccb8c2a51 33 * @return BLE_ERROR_NONE if a read has been initiated, else
rgrover1 310:4d7ccb8c2a51 34 * BLE_ERROR_INVALID_STATE if some internal state about the connection is invalid, or
rgrover1 310:4d7ccb8c2a51 35 * BLE_STACK_BUSY if some client procedure already in progress.
rgrover1 308:482f16f3f7f5 36 */
rgrover1 309:ef9e806a7b03 37 virtual ble_error_t read(uint16_t offset = 0) {
rgrover1 308:482f16f3f7f5 38 // sd_ble_gattc_read()
rgrover1 308:482f16f3f7f5 39 // SVCALL(SD_BLE_GATTC_READ, uint32_t, sd_ble_gattc_read(uint16_t conn_handle, uint16_t handle, uint16_t offset));
rgrover1 309:ef9e806a7b03 40 return BLE_ERROR_NONE;
rgrover1 308:482f16f3f7f5 41 }
rgrover1 308:482f16f3f7f5 42 };
rgrover1 308:482f16f3f7f5 43
rgrover1 250:db3c51656388 44 class NordicServiceDiscovery : public ServiceDiscovery
rgrover1 250:db3c51656388 45 {
rgrover1 248:71ef03789dd3 46 public:
rgrover1 260:ea2798f615e5 47 static const uint16_t SRV_DISC_START_HANDLE = 0x0001; /**< The start handle value used during service discovery. */
rgrover1 284:29fb6af6671d 48 static const uint16_t SRV_DISC_END_HANDLE = 0xFFFF; /**< The end handle value used during service discovery. */
rgrover1 260:ea2798f615e5 49
rgrover1 289:b33301b019e6 50 public:
rgrover1 248:71ef03789dd3 51 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 52 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 53
rgrover1 260:ea2798f615e5 54 public:
rgrover1 288:1d85c4a4d397 55 NordicServiceDiscovery() : serviceIndex(0),
rgrover1 288:1d85c4a4d397 56 numServices(0),
rgrover1 288:1d85c4a4d397 57 characteristicIndex(0),
rgrover1 288:1d85c4a4d397 58 numCharacteristics(0),
rgrover1 288:1d85c4a4d397 59 state(INACTIVE),
rgrover1 288:1d85c4a4d397 60 services(),
rgrover1 288:1d85c4a4d397 61 characteristics(),
rgrover1 305:293634834813 62 serviceUUIDDiscoveryQueue(this),
rgrover1 306:76e2e7349319 63 charUUIDDiscoveryQueue(this),
rgrover1 288:1d85c4a4d397 64 onTerminationCallback(NULL) {
rgrover1 288:1d85c4a4d397 65 /* empty */
rgrover1 288:1d85c4a4d397 66 }
rgrover1 288:1d85c4a4d397 67
rgrover1 288:1d85c4a4d397 68 public:
rgrover1 260:ea2798f615e5 69 ble_error_t launchCharacteristicDiscovery(Gap::Handle_t connectionHandle, Gap::Handle_t startHandle, Gap::Handle_t endHandle);
rgrover1 239:693a1f145b5a 70
rgrover1 239:693a1f145b5a 71 public:
rgrover1 248:71ef03789dd3 72 void setupDiscoveredServices(const ble_gattc_evt_prim_srvc_disc_rsp_t *response);
rgrover1 248:71ef03789dd3 73 void setupDiscoveredCharacteristics(const ble_gattc_evt_char_disc_rsp_t *response);
rgrover1 248:71ef03789dd3 74
rgrover1 290:83c994bf62d0 75 void triggerServiceUUIDDiscovery(void);
rgrover1 290:83c994bf62d0 76 void processDiscoverUUIDResponse(const ble_gattc_evt_char_val_by_uuid_read_rsp_t *response);
rgrover1 290:83c994bf62d0 77 void removeFirstServiceNeedingUUIDDiscovery(void);
rgrover1 290:83c994bf62d0 78
rgrover1 248:71ef03789dd3 79 void terminateServiceDiscovery(void) {
rgrover1 280:cbaa4cb83548 80 bool wasActive = isActive();
rgrover1 286:042b01e59cf8 81 state = INACTIVE;
rgrover1 280:cbaa4cb83548 82
rgrover1 280:cbaa4cb83548 83 if (wasActive && onTerminationCallback) {
rgrover1 280:cbaa4cb83548 84 onTerminationCallback(connHandle);
rgrover1 280:cbaa4cb83548 85 }
rgrover1 239:693a1f145b5a 86 }
rgrover1 239:693a1f145b5a 87
rgrover1 245:3abc61d38db3 88 void terminateCharacteristicDiscovery(void) {
rgrover1 286:042b01e59cf8 89 if (state == CHARACTERISTIC_DISCOVERY_ACTIVE) {
rgrover1 286:042b01e59cf8 90 state = SERVICE_DISCOVERY_ACTIVE;
rgrover1 286:042b01e59cf8 91 }
rgrover1 245:3abc61d38db3 92 serviceIndex++; /* Progress service index to keep discovery alive. */
rgrover1 245:3abc61d38db3 93 }
rgrover1 245:3abc61d38db3 94
rgrover1 279:6e5e79f1fb61 95 bool isActive(void) const {
rgrover1 286:042b01e59cf8 96 return state != INACTIVE;
rgrover1 279:6e5e79f1fb61 97 }
rgrover1 279:6e5e79f1fb61 98
rgrover1 280:cbaa4cb83548 99 void setOnTermination(TerminationCallback_t callback) {
rgrover1 280:cbaa4cb83548 100 onTerminationCallback = callback;
rgrover1 280:cbaa4cb83548 101 }
rgrover1 280:cbaa4cb83548 102
rgrover1 260:ea2798f615e5 103 private:
rgrover1 239:693a1f145b5a 104 void resetDiscoveredServices(void) {
rgrover1 242:73fc02cc20b1 105 numServices = 0;
rgrover1 242:73fc02cc20b1 106 serviceIndex = 0;
rgrover1 239:693a1f145b5a 107 memset(services, 0, sizeof(DiscoveredService) * BLE_DB_DISCOVERY_MAX_SRV);
rgrover1 239:693a1f145b5a 108 }
rgrover1 239:693a1f145b5a 109
rgrover1 245:3abc61d38db3 110 void resetDiscoveredCharacteristics(void) {
rgrover1 245:3abc61d38db3 111 numCharacteristics = 0;
rgrover1 245:3abc61d38db3 112 characteristicIndex = 0;
rgrover1 245:3abc61d38db3 113 memset(characteristics, 0, sizeof(DiscoveredCharacteristic) * BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV);
rgrover1 245:3abc61d38db3 114 }
rgrover1 245:3abc61d38db3 115
rgrover1 245:3abc61d38db3 116 public:
rgrover1 243:a966506d1e5b 117 void serviceDiscoveryStarted(Gap::Handle_t connectionHandle) {
rgrover1 291:d7590ed48009 118 connHandle = connectionHandle;
rgrover1 243:a966506d1e5b 119 resetDiscoveredServices();
rgrover1 286:042b01e59cf8 120 state = SERVICE_DISCOVERY_ACTIVE;
rgrover1 243:a966506d1e5b 121 }
rgrover1 243:a966506d1e5b 122
rgrover1 260:ea2798f615e5 123 private:
rgrover1 245:3abc61d38db3 124 void characteristicDiscoveryStarted(Gap::Handle_t connectionHandle) {
rgrover1 291:d7590ed48009 125 connHandle = connectionHandle;
rgrover1 245:3abc61d38db3 126 resetDiscoveredCharacteristics();
rgrover1 286:042b01e59cf8 127 state = CHARACTERISTIC_DISCOVERY_ACTIVE;
rgrover1 245:3abc61d38db3 128 }
rgrover1 245:3abc61d38db3 129
rgrover1 251:d4e0cf5e8751 130 private:
rgrover1 293:cd86acfe41be 131 /**
rgrover1 293:cd86acfe41be 132 * A datatype to contain service-indices for which long UUIDs need to be
rgrover1 293:cd86acfe41be 133 * discovered using read_val_by_uuid().
rgrover1 293:cd86acfe41be 134 */
rgrover1 305:293634834813 135 class ServiceUUIDDiscoveryQueue {
rgrover1 292:01b717803437 136 public:
rgrover1 305:293634834813 137 ServiceUUIDDiscoveryQueue(NordicServiceDiscovery *parent) :
rgrover1 292:01b717803437 138 numIndices(0),
rgrover1 292:01b717803437 139 serviceIndices(),
rgrover1 301:59e7404a4ea3 140 parentDiscoveryObject(parent) {
rgrover1 292:01b717803437 141 /* empty */
rgrover1 292:01b717803437 142 }
rgrover1 292:01b717803437 143
rgrover1 292:01b717803437 144 public:
rgrover1 292:01b717803437 145 void reset(void) {
rgrover1 292:01b717803437 146 numIndices = 0;
rgrover1 304:8677dcdb218d 147 for (unsigned i = 0; i < BLE_DB_DISCOVERY_MAX_SRV; i++) {
rgrover1 304:8677dcdb218d 148 serviceIndices[i] = INVALID_INDEX;
rgrover1 292:01b717803437 149 }
rgrover1 292:01b717803437 150 }
rgrover1 299:19064275c0e0 151 void enqueue(int serviceIndex) {
rgrover1 292:01b717803437 152 serviceIndices[numIndices++] = serviceIndex;
rgrover1 292:01b717803437 153 }
rgrover1 299:19064275c0e0 154 int dequeue(void) {
rgrover1 299:19064275c0e0 155 if (numIndices == 0) {
rgrover1 304:8677dcdb218d 156 return INVALID_INDEX;
rgrover1 299:19064275c0e0 157 }
rgrover1 299:19064275c0e0 158
rgrover1 299:19064275c0e0 159 unsigned valueToReturn = serviceIndices[0];
rgrover1 292:01b717803437 160 numIndices--;
rgrover1 299:19064275c0e0 161 for (unsigned i = 0; i < numIndices; i++) {
rgrover1 299:19064275c0e0 162 serviceIndices[i] = serviceIndices[i + 1];
rgrover1 292:01b717803437 163 }
rgrover1 299:19064275c0e0 164
rgrover1 299:19064275c0e0 165 return valueToReturn;
rgrover1 292:01b717803437 166 }
rgrover1 302:507e99065379 167 unsigned getFirst(void) const {
rgrover1 302:507e99065379 168 return serviceIndices[0];
rgrover1 302:507e99065379 169 }
rgrover1 292:01b717803437 170 size_t getCount(void) const {
rgrover1 292:01b717803437 171 return numIndices;
rgrover1 292:01b717803437 172 }
rgrover1 292:01b717803437 173
rgrover1 303:697d18859583 174 /**
rgrover1 303:697d18859583 175 * Trigger UUID discovery for the first of the enqueued ServiceIndices.
rgrover1 303:697d18859583 176 */
rgrover1 292:01b717803437 177 void triggerFirst(void);
rgrover1 292:01b717803437 178
rgrover1 292:01b717803437 179 private:
rgrover1 304:8677dcdb218d 180 static const int INVALID_INDEX = -1;
rgrover1 292:01b717803437 181
rgrover1 292:01b717803437 182 private:
rgrover1 292:01b717803437 183 size_t numIndices;
rgrover1 292:01b717803437 184 int serviceIndices[BLE_DB_DISCOVERY_MAX_SRV];
rgrover1 292:01b717803437 185
rgrover1 301:59e7404a4ea3 186 NordicServiceDiscovery *parentDiscoveryObject;
rgrover1 292:01b717803437 187 };
rgrover1 305:293634834813 188 friend class ServiceUUIDDiscoveryQueue;
rgrover1 292:01b717803437 189
rgrover1 306:76e2e7349319 190 /**
rgrover1 306:76e2e7349319 191 * A datatype to contain characteristic-indices for which long UUIDs need to
rgrover1 306:76e2e7349319 192 * be discovered using read_val_by_uuid().
rgrover1 306:76e2e7349319 193 */
rgrover1 306:76e2e7349319 194 class CharUUIDDiscoveryQueue {
rgrover1 306:76e2e7349319 195 public:
rgrover1 306:76e2e7349319 196 CharUUIDDiscoveryQueue(NordicServiceDiscovery *parent) :
rgrover1 306:76e2e7349319 197 numIndices(0),
rgrover1 306:76e2e7349319 198 charIndices(),
rgrover1 306:76e2e7349319 199 parentDiscoveryObject(parent) {
rgrover1 306:76e2e7349319 200 /* empty */
rgrover1 306:76e2e7349319 201 }
rgrover1 306:76e2e7349319 202
rgrover1 306:76e2e7349319 203 public:
rgrover1 306:76e2e7349319 204 void reset(void) {
rgrover1 306:76e2e7349319 205 numIndices = 0;
rgrover1 306:76e2e7349319 206 for (unsigned i = 0; i < BLE_DB_DISCOVERY_MAX_SRV; i++) {
rgrover1 306:76e2e7349319 207 charIndices[i] = INVALID_INDEX;
rgrover1 306:76e2e7349319 208 }
rgrover1 306:76e2e7349319 209 }
rgrover1 306:76e2e7349319 210 void enqueue(int serviceIndex) {
rgrover1 306:76e2e7349319 211 charIndices[numIndices++] = serviceIndex;
rgrover1 306:76e2e7349319 212 }
rgrover1 306:76e2e7349319 213 int dequeue(void) {
rgrover1 306:76e2e7349319 214 if (numIndices == 0) {
rgrover1 306:76e2e7349319 215 return INVALID_INDEX;
rgrover1 306:76e2e7349319 216 }
rgrover1 306:76e2e7349319 217
rgrover1 306:76e2e7349319 218 unsigned valueToReturn = charIndices[0];
rgrover1 306:76e2e7349319 219 numIndices--;
rgrover1 306:76e2e7349319 220 for (unsigned i = 0; i < numIndices; i++) {
rgrover1 306:76e2e7349319 221 charIndices[i] = charIndices[i + 1];
rgrover1 306:76e2e7349319 222 }
rgrover1 306:76e2e7349319 223
rgrover1 306:76e2e7349319 224 return valueToReturn;
rgrover1 306:76e2e7349319 225 }
rgrover1 306:76e2e7349319 226 unsigned getFirst(void) const {
rgrover1 306:76e2e7349319 227 return charIndices[0];
rgrover1 306:76e2e7349319 228 }
rgrover1 306:76e2e7349319 229 size_t getCount(void) const {
rgrover1 306:76e2e7349319 230 return numIndices;
rgrover1 306:76e2e7349319 231 }
rgrover1 306:76e2e7349319 232
rgrover1 306:76e2e7349319 233 /**
rgrover1 306:76e2e7349319 234 * Trigger UUID discovery for the first of the enqueued charIndices.
rgrover1 306:76e2e7349319 235 */
rgrover1 306:76e2e7349319 236 void triggerFirst(void);
rgrover1 306:76e2e7349319 237
rgrover1 306:76e2e7349319 238 private:
rgrover1 306:76e2e7349319 239 static const int INVALID_INDEX = -1;
rgrover1 306:76e2e7349319 240
rgrover1 306:76e2e7349319 241 private:
rgrover1 306:76e2e7349319 242 size_t numIndices;
rgrover1 306:76e2e7349319 243 int charIndices[BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV];
rgrover1 306:76e2e7349319 244
rgrover1 306:76e2e7349319 245 NordicServiceDiscovery *parentDiscoveryObject;
rgrover1 306:76e2e7349319 246 };
rgrover1 306:76e2e7349319 247 friend class CharUUIDDiscoveryQueue;
rgrover1 306:76e2e7349319 248
rgrover1 292:01b717803437 249 private:
rgrover1 251:d4e0cf5e8751 250 friend void bleGattcEventHandler(const ble_evt_t *p_ble_evt);
rgrover1 251:d4e0cf5e8751 251 void progressCharacteristicDiscovery(void);
rgrover1 251:d4e0cf5e8751 252 void progressServiceDiscovery(void);
rgrover1 239:693a1f145b5a 253
rgrover1 248:71ef03789dd3 254 private:
rgrover1 248:71ef03789dd3 255 uint8_t serviceIndex; /**< Index of the current service being discovered. This is intended for internal use during service discovery.*/
rgrover1 248:71ef03789dd3 256 uint8_t numServices; /**< Number of services at the peers GATT database.*/
rgrover1 248:71ef03789dd3 257 uint8_t characteristicIndex; /**< Index of the current characteristic being discovered. This is intended for internal use during service discovery.*/
rgrover1 248:71ef03789dd3 258 uint8_t numCharacteristics; /**< Number of characteristics within the service.*/
rgrover1 248:71ef03789dd3 259
rgrover1 286:042b01e59cf8 260 enum State_t {
rgrover1 286:042b01e59cf8 261 INACTIVE,
rgrover1 286:042b01e59cf8 262 SERVICE_DISCOVERY_ACTIVE,
rgrover1 286:042b01e59cf8 263 CHARACTERISTIC_DISCOVERY_ACTIVE,
rgrover1 290:83c994bf62d0 264 DISCOVER_SERVICE_UUIDS,
rgrover1 306:76e2e7349319 265 DISCOVER_CHARACTERISTIC_UUIDS,
rgrover1 286:042b01e59cf8 266 } state;
rgrover1 248:71ef03789dd3 267
rgrover1 308:482f16f3f7f5 268 DiscoveredService services[BLE_DB_DISCOVERY_MAX_SRV]; /**< Information related to the current service being discovered.
rgrover1 308:482f16f3f7f5 269 * This is intended for internal use during service discovery. */
rgrover1 308:482f16f3f7f5 270 nRFDiscoveredCharacteristic characteristics[BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV];
rgrover1 280:cbaa4cb83548 271
rgrover1 308:482f16f3f7f5 272 ServiceUUIDDiscoveryQueue serviceUUIDDiscoveryQueue;
rgrover1 308:482f16f3f7f5 273 CharUUIDDiscoveryQueue charUUIDDiscoveryQueue;
rgrover1 292:01b717803437 274
rgrover1 308:482f16f3f7f5 275 TerminationCallback_t onTerminationCallback;
rgrover1 240:75b69581d1dd 276 };
rgrover1 240:75b69581d1dd 277
rgrover1 239:693a1f145b5a 278 #endif /*_BTLE_DISCOVERY_H_*/