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