No changes
Fork of nRF51822 by
source/nRF5xServiceDiscovery.h@561:613dbbdeed27, 2016-01-11 (annotated)
- Committer:
- vcoubard
- Date:
- Mon Jan 11 10:19:15 2016 +0000
- Revision:
- 561:613dbbdeed27
- Parent:
- 549:3f782c64d014
- Child:
- 563:9c4b96f7be8d
Synchronized with git rev 7bf81e7e
Author: Andres Amaya Garcia
Improve shutdown to clear BLE API and not just SD
Improve the shutdown functionality, such that a call to ble.shutdown() from
the user application clears the API and nRF5x state and NOT only the
SoftDevice. To achieve this the following changes are introduced:
* Add a protected member cleanup() to nRF5xGap, nRF5xGattClient,
nRF5xGattServer, nRF5xSecurityManager and nRF5xServiceDiscovery.
* Modify the shutdown() implementation in nRF5xn such that it also calls the
static member shutdown() exposed by the BLE API in Gap.h, SecurityManager.h,
GattClient.h and GattServer.h.
* Modify nRF5xGattClient, nRF5xGattServer and nRF5xSecurityManager
classes so that they dynamically create their respective objects only if
needed. Previously the GattClient, GattServer and SecurityManager objects were
declared as static, which means that they were always present even though they
were not always needed. This increases memory consumption unnecessarily.
Furthermore, pointers to the object instances are stored in static members of
the classes as specified by the BLE API base classes. This ensures that
calls to shutdown do not require calls to getInstance() functions that would
otherwise result in undesired memory allocations.
* nRF5xGap object is always needed, so this remains allocated statically. But
the reference in Gap is pointed to this object.
The shutdown procedure is as follows:
1. The user calls ble.shutdown() which executes the code in nRF5xn::shutdown()
1. The SoftDevice is shutdown
1. The static members of Gap.h, SecurityManager.h, GattClient.h and
GattServer.h are called to clean up their own state.
If at any point an error occur during the last step, BLE_ERROR_INVALID_STATE is
returned.
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 | 549:3f782c64d014 | 44 | characteristicIndex(0), |
vcoubard | 544:9e3d053ad4ec | 45 | numCharacteristics(0), |
vcoubard | 544:9e3d053ad4ec | 46 | state(INACTIVE), |
vcoubard | 544:9e3d053ad4ec | 47 | services(), |
vcoubard | 544:9e3d053ad4ec | 48 | characteristics(), |
vcoubard | 544:9e3d053ad4ec | 49 | serviceUUIDDiscoveryQueue(this), |
vcoubard | 544:9e3d053ad4ec | 50 | charUUIDDiscoveryQueue(this), |
vcoubard | 544:9e3d053ad4ec | 51 | onTerminationCallback(NULL) { |
vcoubard | 544:9e3d053ad4ec | 52 | /* empty */ |
vcoubard | 544:9e3d053ad4ec | 53 | } |
vcoubard | 544:9e3d053ad4ec | 54 | |
vcoubard | 544:9e3d053ad4ec | 55 | virtual ble_error_t launch(Gap::Handle_t connectionHandle, |
vcoubard | 544:9e3d053ad4ec | 56 | ServiceDiscovery::ServiceCallback_t sc, |
vcoubard | 544:9e3d053ad4ec | 57 | ServiceDiscovery::CharacteristicCallback_t cc, |
vcoubard | 544:9e3d053ad4ec | 58 | const UUID &matchingServiceUUIDIn, |
vcoubard | 544:9e3d053ad4ec | 59 | const UUID &matchingCharacteristicUUIDIn) |
vcoubard | 544:9e3d053ad4ec | 60 | { |
vcoubard | 544:9e3d053ad4ec | 61 | if (isActive()) { |
vcoubard | 544:9e3d053ad4ec | 62 | return BLE_ERROR_INVALID_STATE; |
vcoubard | 544:9e3d053ad4ec | 63 | } |
vcoubard | 544:9e3d053ad4ec | 64 | |
vcoubard | 544:9e3d053ad4ec | 65 | serviceCallback = sc; |
vcoubard | 544:9e3d053ad4ec | 66 | characteristicCallback = cc; |
vcoubard | 544:9e3d053ad4ec | 67 | matchingServiceUUID = matchingServiceUUIDIn; |
vcoubard | 544:9e3d053ad4ec | 68 | matchingCharacteristicUUID = matchingCharacteristicUUIDIn; |
vcoubard | 544:9e3d053ad4ec | 69 | |
vcoubard | 544:9e3d053ad4ec | 70 | serviceDiscoveryStarted(connectionHandle); |
vcoubard | 544:9e3d053ad4ec | 71 | |
vcoubard | 544:9e3d053ad4ec | 72 | uint32_t rc; |
vcoubard | 544:9e3d053ad4ec | 73 | if ((rc = sd_ble_gattc_primary_services_discover(connectionHandle, SRV_DISC_START_HANDLE, NULL)) != NRF_SUCCESS) { |
vcoubard | 544:9e3d053ad4ec | 74 | terminate(); |
vcoubard | 544:9e3d053ad4ec | 75 | switch (rc) { |
vcoubard | 544:9e3d053ad4ec | 76 | case NRF_ERROR_INVALID_PARAM: |
vcoubard | 544:9e3d053ad4ec | 77 | case BLE_ERROR_INVALID_CONN_HANDLE: |
vcoubard | 544:9e3d053ad4ec | 78 | return BLE_ERROR_INVALID_PARAM; |
vcoubard | 544:9e3d053ad4ec | 79 | case NRF_ERROR_BUSY: |
vcoubard | 544:9e3d053ad4ec | 80 | return BLE_STACK_BUSY; |
vcoubard | 544:9e3d053ad4ec | 81 | default: |
vcoubard | 544:9e3d053ad4ec | 82 | case NRF_ERROR_INVALID_STATE: |
vcoubard | 544:9e3d053ad4ec | 83 | return BLE_ERROR_INVALID_STATE; |
vcoubard | 544:9e3d053ad4ec | 84 | } |
vcoubard | 544:9e3d053ad4ec | 85 | } |
vcoubard | 544:9e3d053ad4ec | 86 | |
vcoubard | 544:9e3d053ad4ec | 87 | return BLE_ERROR_NONE; |
vcoubard | 544:9e3d053ad4ec | 88 | } |
vcoubard | 544:9e3d053ad4ec | 89 | |
vcoubard | 544:9e3d053ad4ec | 90 | virtual bool isActive(void) const { |
vcoubard | 544:9e3d053ad4ec | 91 | return state != INACTIVE; |
vcoubard | 544:9e3d053ad4ec | 92 | } |
vcoubard | 544:9e3d053ad4ec | 93 | |
vcoubard | 544:9e3d053ad4ec | 94 | virtual void terminate(void) { |
vcoubard | 544:9e3d053ad4ec | 95 | terminateServiceDiscovery(); |
vcoubard | 544:9e3d053ad4ec | 96 | } |
vcoubard | 544:9e3d053ad4ec | 97 | |
vcoubard | 544:9e3d053ad4ec | 98 | virtual void onTermination(ServiceDiscovery::TerminationCallback_t callback) { |
vcoubard | 544:9e3d053ad4ec | 99 | onTerminationCallback = callback; |
vcoubard | 544:9e3d053ad4ec | 100 | } |
vcoubard | 544:9e3d053ad4ec | 101 | |
vcoubard | 561:613dbbdeed27 | 102 | /** |
vcoubard | 561:613dbbdeed27 | 103 | * @brief Clear nRF5xServiceDiscovery's state. |
vcoubard | 561:613dbbdeed27 | 104 | * |
vcoubard | 561:613dbbdeed27 | 105 | * @return |
vcoubard | 561:613dbbdeed27 | 106 | * BLE_ERROR_NONE if successful. |
vcoubard | 561:613dbbdeed27 | 107 | */ |
vcoubard | 561:613dbbdeed27 | 108 | virtual ble_error_t cleanup(void) { |
vcoubard | 561:613dbbdeed27 | 109 | /* Clear all state that is from the parent, including private members */ |
vcoubard | 561:613dbbdeed27 | 110 | if (ServiceDiscovery::cleanup() != BLE_ERROR_NONE) { |
vcoubard | 561:613dbbdeed27 | 111 | return BLE_ERROR_INVALID_STATE; |
vcoubard | 561:613dbbdeed27 | 112 | } |
vcoubard | 561:613dbbdeed27 | 113 | |
vcoubard | 561:613dbbdeed27 | 114 | /* Clear derived class members */ |
vcoubard | 561:613dbbdeed27 | 115 | serviceIndex = 0; |
vcoubard | 561:613dbbdeed27 | 116 | numServices = 0; |
vcoubard | 561:613dbbdeed27 | 117 | characteristicIndex = 0; |
vcoubard | 561:613dbbdeed27 | 118 | numCharacteristics = 0; |
vcoubard | 561:613dbbdeed27 | 119 | |
vcoubard | 561:613dbbdeed27 | 120 | state = INACTIVE; |
vcoubard | 561:613dbbdeed27 | 121 | |
vcoubard | 561:613dbbdeed27 | 122 | serviceUUIDDiscoveryQueue.reset(); |
vcoubard | 561:613dbbdeed27 | 123 | charUUIDDiscoveryQueue.reset(); |
vcoubard | 561:613dbbdeed27 | 124 | |
vcoubard | 561:613dbbdeed27 | 125 | onTerminationCallback = NULL; |
vcoubard | 561:613dbbdeed27 | 126 | |
vcoubard | 561:613dbbdeed27 | 127 | return BLE_ERROR_NONE; |
vcoubard | 561:613dbbdeed27 | 128 | } |
vcoubard | 561:613dbbdeed27 | 129 | |
vcoubard | 544:9e3d053ad4ec | 130 | private: |
vcoubard | 544:9e3d053ad4ec | 131 | ble_error_t launchCharacteristicDiscovery(Gap::Handle_t connectionHandle, Gap::Handle_t startHandle, Gap::Handle_t endHandle); |
vcoubard | 544:9e3d053ad4ec | 132 | |
vcoubard | 544:9e3d053ad4ec | 133 | private: |
vcoubard | 544:9e3d053ad4ec | 134 | void setupDiscoveredServices(const ble_gattc_evt_prim_srvc_disc_rsp_t *response); |
vcoubard | 544:9e3d053ad4ec | 135 | void setupDiscoveredCharacteristics(const ble_gattc_evt_char_disc_rsp_t *response); |
vcoubard | 544:9e3d053ad4ec | 136 | |
vcoubard | 544:9e3d053ad4ec | 137 | void triggerServiceUUIDDiscovery(void); |
vcoubard | 544:9e3d053ad4ec | 138 | void processDiscoverUUIDResponse(const ble_gattc_evt_char_val_by_uuid_read_rsp_t *response); |
vcoubard | 544:9e3d053ad4ec | 139 | void removeFirstServiceNeedingUUIDDiscovery(void); |
vcoubard | 544:9e3d053ad4ec | 140 | |
vcoubard | 544:9e3d053ad4ec | 141 | void terminateServiceDiscovery(void) { |
vcoubard | 544:9e3d053ad4ec | 142 | bool wasActive = isActive(); |
vcoubard | 544:9e3d053ad4ec | 143 | state = INACTIVE; |
vcoubard | 544:9e3d053ad4ec | 144 | |
vcoubard | 544:9e3d053ad4ec | 145 | if (wasActive && onTerminationCallback) { |
vcoubard | 544:9e3d053ad4ec | 146 | onTerminationCallback(connHandle); |
vcoubard | 544:9e3d053ad4ec | 147 | } |
vcoubard | 544:9e3d053ad4ec | 148 | } |
vcoubard | 544:9e3d053ad4ec | 149 | |
vcoubard | 549:3f782c64d014 | 150 | void terminateCharacteristicDiscovery(void) { |
vcoubard | 544:9e3d053ad4ec | 151 | if (state == CHARACTERISTIC_DISCOVERY_ACTIVE) { |
vcoubard | 544:9e3d053ad4ec | 152 | state = SERVICE_DISCOVERY_ACTIVE; |
vcoubard | 544:9e3d053ad4ec | 153 | } |
vcoubard | 544:9e3d053ad4ec | 154 | serviceIndex++; /* Progress service index to keep discovery alive. */ |
vcoubard | 544:9e3d053ad4ec | 155 | } |
vcoubard | 544:9e3d053ad4ec | 156 | |
vcoubard | 544:9e3d053ad4ec | 157 | private: |
vcoubard | 544:9e3d053ad4ec | 158 | void resetDiscoveredServices(void) { |
vcoubard | 544:9e3d053ad4ec | 159 | numServices = 0; |
vcoubard | 544:9e3d053ad4ec | 160 | serviceIndex = 0; |
vcoubard | 544:9e3d053ad4ec | 161 | } |
vcoubard | 544:9e3d053ad4ec | 162 | |
vcoubard | 544:9e3d053ad4ec | 163 | void resetDiscoveredCharacteristics(void) { |
vcoubard | 544:9e3d053ad4ec | 164 | numCharacteristics = 0; |
vcoubard | 549:3f782c64d014 | 165 | characteristicIndex = 0; |
vcoubard | 544:9e3d053ad4ec | 166 | } |
vcoubard | 544:9e3d053ad4ec | 167 | |
vcoubard | 544:9e3d053ad4ec | 168 | private: |
vcoubard | 544:9e3d053ad4ec | 169 | void serviceDiscoveryStarted(Gap::Handle_t connectionHandle) { |
vcoubard | 544:9e3d053ad4ec | 170 | connHandle = connectionHandle; |
vcoubard | 544:9e3d053ad4ec | 171 | resetDiscoveredServices(); |
vcoubard | 544:9e3d053ad4ec | 172 | state = SERVICE_DISCOVERY_ACTIVE; |
vcoubard | 544:9e3d053ad4ec | 173 | } |
vcoubard | 544:9e3d053ad4ec | 174 | |
vcoubard | 544:9e3d053ad4ec | 175 | private: |
vcoubard | 544:9e3d053ad4ec | 176 | void characteristicDiscoveryStarted(Gap::Handle_t connectionHandle) { |
vcoubard | 544:9e3d053ad4ec | 177 | connHandle = connectionHandle; |
vcoubard | 544:9e3d053ad4ec | 178 | resetDiscoveredCharacteristics(); |
vcoubard | 544:9e3d053ad4ec | 179 | state = CHARACTERISTIC_DISCOVERY_ACTIVE; |
vcoubard | 544:9e3d053ad4ec | 180 | } |
vcoubard | 544:9e3d053ad4ec | 181 | |
vcoubard | 544:9e3d053ad4ec | 182 | private: |
vcoubard | 544:9e3d053ad4ec | 183 | /** |
vcoubard | 544:9e3d053ad4ec | 184 | * A datatype to contain service-indices for which long UUIDs need to be |
vcoubard | 544:9e3d053ad4ec | 185 | * discovered using read_val_by_uuid(). |
vcoubard | 544:9e3d053ad4ec | 186 | */ |
vcoubard | 544:9e3d053ad4ec | 187 | class ServiceUUIDDiscoveryQueue { |
vcoubard | 544:9e3d053ad4ec | 188 | public: |
vcoubard | 544:9e3d053ad4ec | 189 | ServiceUUIDDiscoveryQueue(nRF5xServiceDiscovery *parent) : |
vcoubard | 544:9e3d053ad4ec | 190 | numIndices(0), |
vcoubard | 544:9e3d053ad4ec | 191 | serviceIndices(), |
vcoubard | 544:9e3d053ad4ec | 192 | parentDiscoveryObject(parent) { |
vcoubard | 544:9e3d053ad4ec | 193 | /* empty */ |
vcoubard | 544:9e3d053ad4ec | 194 | } |
vcoubard | 544:9e3d053ad4ec | 195 | |
vcoubard | 544:9e3d053ad4ec | 196 | public: |
vcoubard | 544:9e3d053ad4ec | 197 | void reset(void) { |
vcoubard | 544:9e3d053ad4ec | 198 | numIndices = 0; |
vcoubard | 544:9e3d053ad4ec | 199 | for (unsigned i = 0; i < BLE_DB_DISCOVERY_MAX_SRV; i++) { |
vcoubard | 544:9e3d053ad4ec | 200 | serviceIndices[i] = INVALID_INDEX; |
vcoubard | 544:9e3d053ad4ec | 201 | } |
vcoubard | 544:9e3d053ad4ec | 202 | } |
vcoubard | 544:9e3d053ad4ec | 203 | void enqueue(int serviceIndex) { |
vcoubard | 544:9e3d053ad4ec | 204 | serviceIndices[numIndices++] = serviceIndex; |
vcoubard | 544:9e3d053ad4ec | 205 | } |
vcoubard | 544:9e3d053ad4ec | 206 | int dequeue(void) { |
vcoubard | 544:9e3d053ad4ec | 207 | if (numIndices == 0) { |
vcoubard | 544:9e3d053ad4ec | 208 | return INVALID_INDEX; |
vcoubard | 544:9e3d053ad4ec | 209 | } |
vcoubard | 544:9e3d053ad4ec | 210 | |
vcoubard | 544:9e3d053ad4ec | 211 | unsigned valueToReturn = serviceIndices[0]; |
vcoubard | 544:9e3d053ad4ec | 212 | numIndices--; |
vcoubard | 544:9e3d053ad4ec | 213 | for (unsigned i = 0; i < numIndices; i++) { |
vcoubard | 544:9e3d053ad4ec | 214 | serviceIndices[i] = serviceIndices[i + 1]; |
vcoubard | 544:9e3d053ad4ec | 215 | } |
vcoubard | 544:9e3d053ad4ec | 216 | |
vcoubard | 544:9e3d053ad4ec | 217 | return valueToReturn; |
vcoubard | 544:9e3d053ad4ec | 218 | } |
vcoubard | 544:9e3d053ad4ec | 219 | unsigned getFirst(void) const { |
vcoubard | 544:9e3d053ad4ec | 220 | return serviceIndices[0]; |
vcoubard | 544:9e3d053ad4ec | 221 | } |
vcoubard | 544:9e3d053ad4ec | 222 | size_t getCount(void) const { |
vcoubard | 544:9e3d053ad4ec | 223 | return numIndices; |
vcoubard | 544:9e3d053ad4ec | 224 | } |
vcoubard | 544:9e3d053ad4ec | 225 | |
vcoubard | 544:9e3d053ad4ec | 226 | /** |
vcoubard | 544:9e3d053ad4ec | 227 | * Trigger UUID discovery for the first of the enqueued ServiceIndices. |
vcoubard | 544:9e3d053ad4ec | 228 | */ |
vcoubard | 544:9e3d053ad4ec | 229 | void triggerFirst(void); |
vcoubard | 544:9e3d053ad4ec | 230 | |
vcoubard | 544:9e3d053ad4ec | 231 | private: |
vcoubard | 544:9e3d053ad4ec | 232 | static const int INVALID_INDEX = -1; |
vcoubard | 544:9e3d053ad4ec | 233 | |
vcoubard | 544:9e3d053ad4ec | 234 | private: |
vcoubard | 544:9e3d053ad4ec | 235 | size_t numIndices; |
vcoubard | 544:9e3d053ad4ec | 236 | int serviceIndices[BLE_DB_DISCOVERY_MAX_SRV]; |
vcoubard | 544:9e3d053ad4ec | 237 | |
vcoubard | 544:9e3d053ad4ec | 238 | nRF5xServiceDiscovery *parentDiscoveryObject; |
vcoubard | 544:9e3d053ad4ec | 239 | }; |
vcoubard | 544:9e3d053ad4ec | 240 | friend class ServiceUUIDDiscoveryQueue; |
vcoubard | 544:9e3d053ad4ec | 241 | |
vcoubard | 544:9e3d053ad4ec | 242 | /** |
vcoubard | 544:9e3d053ad4ec | 243 | * A datatype to contain characteristic-indices for which long UUIDs need to |
vcoubard | 544:9e3d053ad4ec | 244 | * be discovered using read_val_by_uuid(). |
vcoubard | 544:9e3d053ad4ec | 245 | */ |
vcoubard | 544:9e3d053ad4ec | 246 | class CharUUIDDiscoveryQueue { |
vcoubard | 544:9e3d053ad4ec | 247 | public: |
vcoubard | 544:9e3d053ad4ec | 248 | CharUUIDDiscoveryQueue(nRF5xServiceDiscovery *parent) : |
vcoubard | 544:9e3d053ad4ec | 249 | numIndices(0), |
vcoubard | 544:9e3d053ad4ec | 250 | charIndices(), |
vcoubard | 544:9e3d053ad4ec | 251 | parentDiscoveryObject(parent) { |
vcoubard | 544:9e3d053ad4ec | 252 | /* empty */ |
vcoubard | 544:9e3d053ad4ec | 253 | } |
vcoubard | 544:9e3d053ad4ec | 254 | |
vcoubard | 544:9e3d053ad4ec | 255 | public: |
vcoubard | 544:9e3d053ad4ec | 256 | void reset(void) { |
vcoubard | 544:9e3d053ad4ec | 257 | numIndices = 0; |
vcoubard | 544:9e3d053ad4ec | 258 | for (unsigned i = 0; i < BLE_DB_DISCOVERY_MAX_SRV; i++) { |
vcoubard | 544:9e3d053ad4ec | 259 | charIndices[i] = INVALID_INDEX; |
vcoubard | 544:9e3d053ad4ec | 260 | } |
vcoubard | 544:9e3d053ad4ec | 261 | } |
vcoubard | 544:9e3d053ad4ec | 262 | void enqueue(int serviceIndex) { |
vcoubard | 544:9e3d053ad4ec | 263 | charIndices[numIndices++] = serviceIndex; |
vcoubard | 544:9e3d053ad4ec | 264 | } |
vcoubard | 544:9e3d053ad4ec | 265 | int dequeue(void) { |
vcoubard | 544:9e3d053ad4ec | 266 | if (numIndices == 0) { |
vcoubard | 544:9e3d053ad4ec | 267 | return INVALID_INDEX; |
vcoubard | 544:9e3d053ad4ec | 268 | } |
vcoubard | 544:9e3d053ad4ec | 269 | |
vcoubard | 544:9e3d053ad4ec | 270 | unsigned valueToReturn = charIndices[0]; |
vcoubard | 544:9e3d053ad4ec | 271 | numIndices--; |
vcoubard | 544:9e3d053ad4ec | 272 | for (unsigned i = 0; i < numIndices; i++) { |
vcoubard | 544:9e3d053ad4ec | 273 | charIndices[i] = charIndices[i + 1]; |
vcoubard | 544:9e3d053ad4ec | 274 | } |
vcoubard | 544:9e3d053ad4ec | 275 | |
vcoubard | 544:9e3d053ad4ec | 276 | return valueToReturn; |
vcoubard | 544:9e3d053ad4ec | 277 | } |
vcoubard | 544:9e3d053ad4ec | 278 | unsigned getFirst(void) const { |
vcoubard | 544:9e3d053ad4ec | 279 | return charIndices[0]; |
vcoubard | 544:9e3d053ad4ec | 280 | } |
vcoubard | 544:9e3d053ad4ec | 281 | size_t getCount(void) const { |
vcoubard | 544:9e3d053ad4ec | 282 | return numIndices; |
vcoubard | 544:9e3d053ad4ec | 283 | } |
vcoubard | 544:9e3d053ad4ec | 284 | |
vcoubard | 544:9e3d053ad4ec | 285 | /** |
vcoubard | 544:9e3d053ad4ec | 286 | * Trigger UUID discovery for the first of the enqueued charIndices. |
vcoubard | 544:9e3d053ad4ec | 287 | */ |
vcoubard | 544:9e3d053ad4ec | 288 | void triggerFirst(void); |
vcoubard | 544:9e3d053ad4ec | 289 | |
vcoubard | 544:9e3d053ad4ec | 290 | private: |
vcoubard | 544:9e3d053ad4ec | 291 | static const int INVALID_INDEX = -1; |
vcoubard | 544:9e3d053ad4ec | 292 | |
vcoubard | 544:9e3d053ad4ec | 293 | private: |
vcoubard | 544:9e3d053ad4ec | 294 | size_t numIndices; |
vcoubard | 544:9e3d053ad4ec | 295 | int charIndices[BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV]; |
vcoubard | 544:9e3d053ad4ec | 296 | |
vcoubard | 544:9e3d053ad4ec | 297 | nRF5xServiceDiscovery *parentDiscoveryObject; |
vcoubard | 544:9e3d053ad4ec | 298 | }; |
vcoubard | 544:9e3d053ad4ec | 299 | friend class CharUUIDDiscoveryQueue; |
vcoubard | 544:9e3d053ad4ec | 300 | |
vcoubard | 544:9e3d053ad4ec | 301 | private: |
vcoubard | 544:9e3d053ad4ec | 302 | friend void bleGattcEventHandler(const ble_evt_t *p_ble_evt); |
vcoubard | 544:9e3d053ad4ec | 303 | void progressCharacteristicDiscovery(void); |
vcoubard | 544:9e3d053ad4ec | 304 | void progressServiceDiscovery(void); |
vcoubard | 544:9e3d053ad4ec | 305 | |
vcoubard | 544:9e3d053ad4ec | 306 | private: |
vcoubard | 544:9e3d053ad4ec | 307 | nRF5xGattClient *gattc; |
vcoubard | 544:9e3d053ad4ec | 308 | |
vcoubard | 544:9e3d053ad4ec | 309 | private: |
vcoubard | 544:9e3d053ad4ec | 310 | uint8_t serviceIndex; /**< Index of the current service being discovered. This is intended for internal use during service discovery.*/ |
vcoubard | 544:9e3d053ad4ec | 311 | uint8_t numServices; /**< Number of services at the peers GATT database.*/ |
vcoubard | 549:3f782c64d014 | 312 | uint8_t characteristicIndex; /**< Index of the current characteristic being discovered. This is intended for internal use during service discovery.*/ |
vcoubard | 544:9e3d053ad4ec | 313 | uint8_t numCharacteristics; /**< Number of characteristics within the service.*/ |
vcoubard | 544:9e3d053ad4ec | 314 | |
vcoubard | 544:9e3d053ad4ec | 315 | enum State_t { |
vcoubard | 544:9e3d053ad4ec | 316 | INACTIVE, |
vcoubard | 544:9e3d053ad4ec | 317 | SERVICE_DISCOVERY_ACTIVE, |
vcoubard | 544:9e3d053ad4ec | 318 | CHARACTERISTIC_DISCOVERY_ACTIVE, |
vcoubard | 544:9e3d053ad4ec | 319 | DISCOVER_SERVICE_UUIDS, |
vcoubard | 544:9e3d053ad4ec | 320 | DISCOVER_CHARACTERISTIC_UUIDS, |
vcoubard | 544:9e3d053ad4ec | 321 | } state; |
vcoubard | 544:9e3d053ad4ec | 322 | |
vcoubard | 544:9e3d053ad4ec | 323 | DiscoveredService services[BLE_DB_DISCOVERY_MAX_SRV]; /**< Information related to the current service being discovered. |
vcoubard | 544:9e3d053ad4ec | 324 | * This is intended for internal use during service discovery. */ |
vcoubard | 544:9e3d053ad4ec | 325 | nRF5xDiscoveredCharacteristic characteristics[BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV]; |
vcoubard | 544:9e3d053ad4ec | 326 | |
vcoubard | 544:9e3d053ad4ec | 327 | ServiceUUIDDiscoveryQueue serviceUUIDDiscoveryQueue; |
vcoubard | 544:9e3d053ad4ec | 328 | CharUUIDDiscoveryQueue charUUIDDiscoveryQueue; |
vcoubard | 544:9e3d053ad4ec | 329 | |
vcoubard | 544:9e3d053ad4ec | 330 | TerminationCallback_t onTerminationCallback; |
vcoubard | 544:9e3d053ad4ec | 331 | }; |
vcoubard | 544:9e3d053ad4ec | 332 | |
rgrover1 | 388:db85a09c27ef | 333 | #endif /*__NRF_SERVICE_DISCOVERY_H__*/ |