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