No changes

Fork of nRF51822 by Nordic Semiconductor

Committer:
rgrover1
Date:
Tue Jul 21 13:23:44 2015 +0100
Revision:
388:db85a09c27ef
Child:
544:9e3d053ad4ec
Synchronized with git rev 0eb58d86
Author: Rohit Grover
rename nRF51... to nRF5x...
This prepares us to support nRF52.

Who changed what in which revision?

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