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