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