test

Fork of nRF51822 by Nordic Semiconductor

Committer:
rgrover1
Date:
Fri Jun 19 15:55:30 2015 +0100
Revision:
315:044071756a8d
Parent:
312:d55797e0d632
Child:
316:1ccf6ee70900
Synchronized with git rev 771213d5
Author: Rohit Grover
split out nRFDiscoveredCharacteristic.h from btle_discovery.h

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