Nordic stack and drivers for the mbed BLE API

Dependents:   BLE_ANCS_SDAPI BLE_temperature BLE_HeartRate writable_gatt ... more

Committer:
Vincent Coubard
Date:
Wed Sep 14 14:39:43 2016 +0100
Revision:
638:c90ae1400bf2
Sync with bdab10dc0f90748b6989c8b577771bb403ca6bd8 from ARMmbed/mbed-os.

Who changed what in which revision?

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