No changes
Fork of nRF51822 by
source/nRF5xServiceDiscovery.cpp@544:9e3d053ad4ec, 2016-01-11 (annotated)
- Committer:
- vcoubard
- Date:
- Mon Jan 11 10:19:04 2016 +0000
- Revision:
- 544:9e3d053ad4ec
- Parent:
- 543:53215259c0d2
- Child:
- 549:3f782c64d014
Synchronized with git rev dc2dfb0a
Author: Vincent Coubard
Add status parameter in terminateCharacteristicDiscovery function.
Fix terminate discovery (the replacement of the discovery was done after
the call to terminate).
When searching for a running discovery, dismiss results where the
characteristic is equal to the default characteristic value
Add Discovery::operator!=
Add support of DiscoveredCharacteristic last handle in the characteristic
discovery process
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vcoubard | 541:884f95bf5351 | 1 | /* mbed Microcontroller Library |
vcoubard | 541:884f95bf5351 | 2 | * Copyright (c) 2006-2013 ARM Limited |
vcoubard | 541:884f95bf5351 | 3 | * |
vcoubard | 541:884f95bf5351 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
vcoubard | 541:884f95bf5351 | 5 | * you may not use this file except in compliance with the License. |
vcoubard | 541:884f95bf5351 | 6 | * You may obtain a copy of the License at |
vcoubard | 541:884f95bf5351 | 7 | * |
vcoubard | 541:884f95bf5351 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
vcoubard | 541:884f95bf5351 | 9 | * |
vcoubard | 541:884f95bf5351 | 10 | * Unless required by applicable law or agreed to in writing, software |
vcoubard | 541:884f95bf5351 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
vcoubard | 541:884f95bf5351 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
vcoubard | 541:884f95bf5351 | 13 | * See the License for the specific language governing permissions and |
vcoubard | 541:884f95bf5351 | 14 | * limitations under the License. |
vcoubard | 541:884f95bf5351 | 15 | */ |
vcoubard | 541:884f95bf5351 | 16 | |
vcoubard | 541:884f95bf5351 | 17 | #include "nRF5xServiceDiscovery.h" |
vcoubard | 541:884f95bf5351 | 18 | |
vcoubard | 541:884f95bf5351 | 19 | ble_error_t |
vcoubard | 541:884f95bf5351 | 20 | nRF5xServiceDiscovery::launchCharacteristicDiscovery(Gap::Handle_t connectionHandle, |
vcoubard | 541:884f95bf5351 | 21 | Gap::Handle_t startHandle, |
vcoubard | 541:884f95bf5351 | 22 | Gap::Handle_t endHandle) |
vcoubard | 541:884f95bf5351 | 23 | { |
vcoubard | 541:884f95bf5351 | 24 | characteristicDiscoveryStarted(connectionHandle); |
vcoubard | 541:884f95bf5351 | 25 | |
vcoubard | 541:884f95bf5351 | 26 | ble_gattc_handle_range_t handleRange = { |
vcoubard | 541:884f95bf5351 | 27 | .start_handle = startHandle, |
vcoubard | 541:884f95bf5351 | 28 | .end_handle = endHandle |
vcoubard | 541:884f95bf5351 | 29 | }; |
vcoubard | 544:9e3d053ad4ec | 30 | uint32_t rc = sd_ble_gattc_characteristics_discover(connectionHandle, &handleRange); |
vcoubard | 544:9e3d053ad4ec | 31 | ble_error_t err = BLE_ERROR_NONE; |
vcoubard | 544:9e3d053ad4ec | 32 | |
vcoubard | 544:9e3d053ad4ec | 33 | switch (rc) { |
vcoubard | 544:9e3d053ad4ec | 34 | case NRF_SUCCESS: |
vcoubard | 544:9e3d053ad4ec | 35 | err = BLE_ERROR_NONE; |
vcoubard | 544:9e3d053ad4ec | 36 | break; |
vcoubard | 544:9e3d053ad4ec | 37 | case BLE_ERROR_INVALID_CONN_HANDLE: |
vcoubard | 544:9e3d053ad4ec | 38 | case NRF_ERROR_INVALID_ADDR: |
vcoubard | 544:9e3d053ad4ec | 39 | err = BLE_ERROR_INVALID_PARAM; |
vcoubard | 544:9e3d053ad4ec | 40 | break; |
vcoubard | 544:9e3d053ad4ec | 41 | case NRF_ERROR_BUSY: |
vcoubard | 544:9e3d053ad4ec | 42 | err = BLE_STACK_BUSY; |
vcoubard | 544:9e3d053ad4ec | 43 | break; |
vcoubard | 544:9e3d053ad4ec | 44 | case NRF_ERROR_INVALID_STATE: |
vcoubard | 544:9e3d053ad4ec | 45 | err = BLE_ERROR_INVALID_STATE; |
vcoubard | 544:9e3d053ad4ec | 46 | break; |
vcoubard | 544:9e3d053ad4ec | 47 | default: |
vcoubard | 544:9e3d053ad4ec | 48 | err = BLE_ERROR_UNSPECIFIED; |
vcoubard | 544:9e3d053ad4ec | 49 | break; |
vcoubard | 541:884f95bf5351 | 50 | } |
vcoubard | 541:884f95bf5351 | 51 | |
vcoubard | 544:9e3d053ad4ec | 52 | if(err) { |
vcoubard | 544:9e3d053ad4ec | 53 | terminateCharacteristicDiscovery(err); |
vcoubard | 544:9e3d053ad4ec | 54 | } |
vcoubard | 544:9e3d053ad4ec | 55 | return err; |
vcoubard | 541:884f95bf5351 | 56 | } |
vcoubard | 541:884f95bf5351 | 57 | |
vcoubard | 541:884f95bf5351 | 58 | void |
vcoubard | 541:884f95bf5351 | 59 | nRF5xServiceDiscovery::setupDiscoveredServices(const ble_gattc_evt_prim_srvc_disc_rsp_t *response) |
vcoubard | 541:884f95bf5351 | 60 | { |
vcoubard | 541:884f95bf5351 | 61 | serviceIndex = 0; |
vcoubard | 541:884f95bf5351 | 62 | numServices = response->count; |
vcoubard | 541:884f95bf5351 | 63 | |
vcoubard | 541:884f95bf5351 | 64 | /* Account for the limitation on the number of discovered services we can handle at a time. */ |
vcoubard | 541:884f95bf5351 | 65 | if (numServices > BLE_DB_DISCOVERY_MAX_SRV) { |
vcoubard | 541:884f95bf5351 | 66 | numServices = BLE_DB_DISCOVERY_MAX_SRV; |
vcoubard | 541:884f95bf5351 | 67 | } |
vcoubard | 541:884f95bf5351 | 68 | |
vcoubard | 541:884f95bf5351 | 69 | serviceUUIDDiscoveryQueue.reset(); |
vcoubard | 541:884f95bf5351 | 70 | for (unsigned serviceIndex = 0; serviceIndex < numServices; serviceIndex++) { |
vcoubard | 541:884f95bf5351 | 71 | if (response->services[serviceIndex].uuid.type == BLE_UUID_TYPE_UNKNOWN) { |
vcoubard | 541:884f95bf5351 | 72 | serviceUUIDDiscoveryQueue.enqueue(serviceIndex); |
vcoubard | 541:884f95bf5351 | 73 | services[serviceIndex].setup(response->services[serviceIndex].handle_range.start_handle, |
vcoubard | 541:884f95bf5351 | 74 | response->services[serviceIndex].handle_range.end_handle); |
vcoubard | 541:884f95bf5351 | 75 | } else { |
vcoubard | 541:884f95bf5351 | 76 | services[serviceIndex].setup(response->services[serviceIndex].uuid.uuid, |
vcoubard | 541:884f95bf5351 | 77 | response->services[serviceIndex].handle_range.start_handle, |
vcoubard | 541:884f95bf5351 | 78 | response->services[serviceIndex].handle_range.end_handle); |
vcoubard | 541:884f95bf5351 | 79 | } |
vcoubard | 541:884f95bf5351 | 80 | } |
vcoubard | 541:884f95bf5351 | 81 | |
vcoubard | 541:884f95bf5351 | 82 | /* Trigger discovery of service UUID if necessary. */ |
vcoubard | 541:884f95bf5351 | 83 | if (serviceUUIDDiscoveryQueue.getCount()) { |
vcoubard | 541:884f95bf5351 | 84 | serviceUUIDDiscoveryQueue.triggerFirst(); |
vcoubard | 541:884f95bf5351 | 85 | } |
vcoubard | 541:884f95bf5351 | 86 | } |
vcoubard | 541:884f95bf5351 | 87 | |
vcoubard | 541:884f95bf5351 | 88 | void |
vcoubard | 541:884f95bf5351 | 89 | nRF5xServiceDiscovery::setupDiscoveredCharacteristics(const ble_gattc_evt_char_disc_rsp_t *response) |
vcoubard | 541:884f95bf5351 | 90 | { |
vcoubard | 541:884f95bf5351 | 91 | numCharacteristics = response->count; |
vcoubard | 541:884f95bf5351 | 92 | |
vcoubard | 541:884f95bf5351 | 93 | /* Account for the limitation on the number of discovered characteristics we can handle at a time. */ |
vcoubard | 541:884f95bf5351 | 94 | if (numCharacteristics > BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV) { |
vcoubard | 541:884f95bf5351 | 95 | numCharacteristics = BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV; |
vcoubard | 541:884f95bf5351 | 96 | } |
vcoubard | 541:884f95bf5351 | 97 | |
vcoubard | 541:884f95bf5351 | 98 | charUUIDDiscoveryQueue.reset(); |
vcoubard | 541:884f95bf5351 | 99 | for (unsigned charIndex = 0; charIndex < numCharacteristics; charIndex++) { |
vcoubard | 541:884f95bf5351 | 100 | if (response->chars[charIndex].uuid.type == BLE_UUID_TYPE_UNKNOWN) { |
vcoubard | 541:884f95bf5351 | 101 | charUUIDDiscoveryQueue.enqueue(charIndex); |
vcoubard | 541:884f95bf5351 | 102 | characteristics[charIndex].setup(gattc, |
vcoubard | 541:884f95bf5351 | 103 | connHandle, |
vcoubard | 541:884f95bf5351 | 104 | response->chars[charIndex].char_props, |
vcoubard | 541:884f95bf5351 | 105 | response->chars[charIndex].handle_decl, |
vcoubard | 541:884f95bf5351 | 106 | response->chars[charIndex].handle_value); |
vcoubard | 541:884f95bf5351 | 107 | } else { |
vcoubard | 541:884f95bf5351 | 108 | characteristics[charIndex].setup(gattc, |
vcoubard | 541:884f95bf5351 | 109 | connHandle, |
vcoubard | 541:884f95bf5351 | 110 | response->chars[charIndex].uuid.uuid, |
vcoubard | 541:884f95bf5351 | 111 | response->chars[charIndex].char_props, |
vcoubard | 541:884f95bf5351 | 112 | response->chars[charIndex].handle_decl, |
vcoubard | 541:884f95bf5351 | 113 | response->chars[charIndex].handle_value); |
vcoubard | 541:884f95bf5351 | 114 | } |
vcoubard | 541:884f95bf5351 | 115 | } |
vcoubard | 541:884f95bf5351 | 116 | |
vcoubard | 541:884f95bf5351 | 117 | /* Trigger discovery of char UUID if necessary. */ |
vcoubard | 541:884f95bf5351 | 118 | if (charUUIDDiscoveryQueue.getCount()) { |
vcoubard | 541:884f95bf5351 | 119 | charUUIDDiscoveryQueue.triggerFirst(); |
vcoubard | 541:884f95bf5351 | 120 | } |
vcoubard | 541:884f95bf5351 | 121 | } |
vcoubard | 541:884f95bf5351 | 122 | |
vcoubard | 541:884f95bf5351 | 123 | void |
vcoubard | 541:884f95bf5351 | 124 | nRF5xServiceDiscovery::progressCharacteristicDiscovery(void) |
vcoubard | 541:884f95bf5351 | 125 | { |
vcoubard | 544:9e3d053ad4ec | 126 | if(state != CHARACTERISTIC_DISCOVERY_ACTIVE) { |
vcoubard | 544:9e3d053ad4ec | 127 | return; |
vcoubard | 544:9e3d053ad4ec | 128 | } |
vcoubard | 544:9e3d053ad4ec | 129 | |
vcoubard | 544:9e3d053ad4ec | 130 | if(remainingCharacteristic != nRF5xDiscoveredCharacteristic() && numCharacteristics > 0) { |
vcoubard | 544:9e3d053ad4ec | 131 | remainingCharacteristic.setLastHandle(characteristics[0].getDeclHandle() - 1); |
vcoubard | 544:9e3d053ad4ec | 132 | |
vcoubard | 544:9e3d053ad4ec | 133 | if ((matchingCharacteristicUUID == UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)) || |
vcoubard | 544:9e3d053ad4ec | 134 | ((matchingCharacteristicUUID == remainingCharacteristic.getUUID()) && |
vcoubard | 544:9e3d053ad4ec | 135 | (matchingServiceUUID != UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)))) { |
vcoubard | 544:9e3d053ad4ec | 136 | if (characteristicCallback) { |
vcoubard | 544:9e3d053ad4ec | 137 | characteristicCallback(&remainingCharacteristic); |
vcoubard | 544:9e3d053ad4ec | 138 | } |
vcoubard | 544:9e3d053ad4ec | 139 | } |
vcoubard | 544:9e3d053ad4ec | 140 | } |
vcoubard | 544:9e3d053ad4ec | 141 | |
vcoubard | 543:53215259c0d2 | 142 | for(uint8_t i = 0; i < numCharacteristics; ++i) { |
vcoubard | 543:53215259c0d2 | 143 | if(state != CHARACTERISTIC_DISCOVERY_ACTIVE) { |
vcoubard | 543:53215259c0d2 | 144 | return; |
vcoubard | 543:53215259c0d2 | 145 | } |
vcoubard | 543:53215259c0d2 | 146 | |
vcoubard | 544:9e3d053ad4ec | 147 | if(i == numCharacteristics - 1) { |
vcoubard | 544:9e3d053ad4ec | 148 | remainingCharacteristic = characteristics[i]; |
vcoubard | 544:9e3d053ad4ec | 149 | break; |
vcoubard | 544:9e3d053ad4ec | 150 | } else { |
vcoubard | 544:9e3d053ad4ec | 151 | characteristics[i].setLastHandle(characteristics[i + 1].getDeclHandle() - 1); |
vcoubard | 544:9e3d053ad4ec | 152 | } |
vcoubard | 544:9e3d053ad4ec | 153 | |
vcoubard | 541:884f95bf5351 | 154 | if ((matchingCharacteristicUUID == UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)) || |
vcoubard | 544:9e3d053ad4ec | 155 | ((matchingCharacteristicUUID == characteristics[i].getUUID()) && |
vcoubard | 541:884f95bf5351 | 156 | (matchingServiceUUID != UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)))) { |
vcoubard | 541:884f95bf5351 | 157 | if (characteristicCallback) { |
vcoubard | 543:53215259c0d2 | 158 | characteristicCallback(&characteristics[i]); |
vcoubard | 541:884f95bf5351 | 159 | } |
vcoubard | 541:884f95bf5351 | 160 | } |
vcoubard | 543:53215259c0d2 | 161 | } |
vcoubard | 541:884f95bf5351 | 162 | |
vcoubard | 543:53215259c0d2 | 163 | if(state != CHARACTERISTIC_DISCOVERY_ACTIVE) { |
vcoubard | 543:53215259c0d2 | 164 | return; |
vcoubard | 541:884f95bf5351 | 165 | } |
vcoubard | 541:884f95bf5351 | 166 | |
vcoubard | 544:9e3d053ad4ec | 167 | |
vcoubard | 544:9e3d053ad4ec | 168 | Gap::Handle_t startHandle = (numCharacteristics > 0) ? characteristics[numCharacteristics - 1].getValueHandle() + 1 : SRV_DISC_END_HANDLE; |
vcoubard | 543:53215259c0d2 | 169 | Gap::Handle_t endHandle = services[serviceIndex].getEndHandle(); |
vcoubard | 543:53215259c0d2 | 170 | resetDiscoveredCharacteristics(); /* Note: resetDiscoveredCharacteristics() must come after fetching start and end Handles. */ |
vcoubard | 541:884f95bf5351 | 171 | |
vcoubard | 543:53215259c0d2 | 172 | if (startHandle < endHandle) { |
vcoubard | 543:53215259c0d2 | 173 | ble_gattc_handle_range_t handleRange = { |
vcoubard | 543:53215259c0d2 | 174 | .start_handle = startHandle, |
vcoubard | 543:53215259c0d2 | 175 | .end_handle = endHandle |
vcoubard | 543:53215259c0d2 | 176 | }; |
vcoubard | 543:53215259c0d2 | 177 | if (sd_ble_gattc_characteristics_discover(connHandle, &handleRange) != NRF_SUCCESS) { |
vcoubard | 544:9e3d053ad4ec | 178 | terminateCharacteristicDiscovery(BLE_ERROR_UNSPECIFIED); |
vcoubard | 541:884f95bf5351 | 179 | } |
vcoubard | 543:53215259c0d2 | 180 | } else { |
vcoubard | 544:9e3d053ad4ec | 181 | terminateCharacteristicDiscovery(BLE_ERROR_NONE); |
vcoubard | 541:884f95bf5351 | 182 | } |
vcoubard | 541:884f95bf5351 | 183 | } |
vcoubard | 541:884f95bf5351 | 184 | |
vcoubard | 541:884f95bf5351 | 185 | void |
vcoubard | 541:884f95bf5351 | 186 | nRF5xServiceDiscovery::progressServiceDiscovery(void) |
vcoubard | 541:884f95bf5351 | 187 | { |
vcoubard | 541:884f95bf5351 | 188 | /* Iterate through the previously discovered services cached in services[]. */ |
vcoubard | 541:884f95bf5351 | 189 | while ((state == SERVICE_DISCOVERY_ACTIVE) && (serviceIndex < numServices)) { |
vcoubard | 541:884f95bf5351 | 190 | if ((matchingServiceUUID == UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)) || |
vcoubard | 541:884f95bf5351 | 191 | (matchingServiceUUID == services[serviceIndex].getUUID())) { |
vcoubard | 541:884f95bf5351 | 192 | |
vcoubard | 541:884f95bf5351 | 193 | if (serviceCallback && (matchingCharacteristicUUID == UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN))) { |
vcoubard | 541:884f95bf5351 | 194 | serviceCallback(&services[serviceIndex]); |
vcoubard | 541:884f95bf5351 | 195 | } |
vcoubard | 541:884f95bf5351 | 196 | |
vcoubard | 541:884f95bf5351 | 197 | if ((state == SERVICE_DISCOVERY_ACTIVE) && characteristicCallback) { |
vcoubard | 541:884f95bf5351 | 198 | launchCharacteristicDiscovery(connHandle, services[serviceIndex].getStartHandle(), services[serviceIndex].getEndHandle()); |
vcoubard | 541:884f95bf5351 | 199 | } else { |
vcoubard | 541:884f95bf5351 | 200 | serviceIndex++; |
vcoubard | 541:884f95bf5351 | 201 | } |
vcoubard | 541:884f95bf5351 | 202 | } else { |
vcoubard | 541:884f95bf5351 | 203 | serviceIndex++; |
vcoubard | 541:884f95bf5351 | 204 | } |
vcoubard | 541:884f95bf5351 | 205 | } |
vcoubard | 541:884f95bf5351 | 206 | |
vcoubard | 541:884f95bf5351 | 207 | /* Relaunch discovery of new services beyond the last entry cached in services[]. */ |
vcoubard | 541:884f95bf5351 | 208 | if ((state == SERVICE_DISCOVERY_ACTIVE) && (numServices > 0) && (serviceIndex > 0)) { |
vcoubard | 541:884f95bf5351 | 209 | /* Determine the ending handle of the last cached service. */ |
vcoubard | 541:884f95bf5351 | 210 | Gap::Handle_t endHandle = services[serviceIndex - 1].getEndHandle(); |
vcoubard | 541:884f95bf5351 | 211 | resetDiscoveredServices(); /* Note: resetDiscoveredServices() must come after fetching endHandle. */ |
vcoubard | 541:884f95bf5351 | 212 | |
vcoubard | 541:884f95bf5351 | 213 | if (endHandle == SRV_DISC_END_HANDLE) { |
vcoubard | 541:884f95bf5351 | 214 | terminateServiceDiscovery(); |
vcoubard | 541:884f95bf5351 | 215 | } else { |
vcoubard | 541:884f95bf5351 | 216 | if (sd_ble_gattc_primary_services_discover(connHandle, endHandle, NULL) != NRF_SUCCESS) { |
vcoubard | 541:884f95bf5351 | 217 | terminateServiceDiscovery(); |
vcoubard | 541:884f95bf5351 | 218 | } |
vcoubard | 541:884f95bf5351 | 219 | } |
vcoubard | 541:884f95bf5351 | 220 | } |
vcoubard | 541:884f95bf5351 | 221 | } |
vcoubard | 541:884f95bf5351 | 222 | |
vcoubard | 541:884f95bf5351 | 223 | void |
vcoubard | 541:884f95bf5351 | 224 | nRF5xServiceDiscovery::ServiceUUIDDiscoveryQueue::triggerFirst(void) |
vcoubard | 541:884f95bf5351 | 225 | { |
vcoubard | 541:884f95bf5351 | 226 | while (numIndices) { /* loop until a call to char_value_by_uuid_read() succeeds or we run out of pending indices. */ |
vcoubard | 541:884f95bf5351 | 227 | parentDiscoveryObject->state = DISCOVER_SERVICE_UUIDS; |
vcoubard | 541:884f95bf5351 | 228 | |
vcoubard | 541:884f95bf5351 | 229 | unsigned serviceIndex = getFirst(); |
vcoubard | 541:884f95bf5351 | 230 | ble_uuid_t uuid = { |
vcoubard | 541:884f95bf5351 | 231 | .uuid = BLE_UUID_SERVICE_PRIMARY, |
vcoubard | 541:884f95bf5351 | 232 | .type = BLE_UUID_TYPE_BLE, |
vcoubard | 541:884f95bf5351 | 233 | }; |
vcoubard | 541:884f95bf5351 | 234 | ble_gattc_handle_range_t handleRange = { |
vcoubard | 541:884f95bf5351 | 235 | .start_handle = parentDiscoveryObject->services[serviceIndex].getStartHandle(), |
vcoubard | 541:884f95bf5351 | 236 | .end_handle = parentDiscoveryObject->services[serviceIndex].getEndHandle(), |
vcoubard | 541:884f95bf5351 | 237 | }; |
vcoubard | 541:884f95bf5351 | 238 | if (sd_ble_gattc_char_value_by_uuid_read(parentDiscoveryObject->connHandle, &uuid, &handleRange) == NRF_SUCCESS) { |
vcoubard | 541:884f95bf5351 | 239 | return; |
vcoubard | 541:884f95bf5351 | 240 | } |
vcoubard | 541:884f95bf5351 | 241 | |
vcoubard | 541:884f95bf5351 | 242 | /* Skip this service if we fail to launch a read for its service-declaration |
vcoubard | 541:884f95bf5351 | 243 | * attribute. Its UUID will remain INVALID, and it may not match any filters. */ |
vcoubard | 541:884f95bf5351 | 244 | dequeue(); |
vcoubard | 541:884f95bf5351 | 245 | } |
vcoubard | 541:884f95bf5351 | 246 | |
vcoubard | 541:884f95bf5351 | 247 | /* Switch back to service discovery upon exhausting the service-indices pending UUID discovery. */ |
vcoubard | 541:884f95bf5351 | 248 | if (parentDiscoveryObject->state == DISCOVER_SERVICE_UUIDS) { |
vcoubard | 541:884f95bf5351 | 249 | parentDiscoveryObject->state = SERVICE_DISCOVERY_ACTIVE; |
vcoubard | 541:884f95bf5351 | 250 | } |
vcoubard | 541:884f95bf5351 | 251 | } |
vcoubard | 541:884f95bf5351 | 252 | |
vcoubard | 541:884f95bf5351 | 253 | void |
vcoubard | 541:884f95bf5351 | 254 | nRF5xServiceDiscovery::CharUUIDDiscoveryQueue::triggerFirst(void) |
vcoubard | 541:884f95bf5351 | 255 | { |
vcoubard | 541:884f95bf5351 | 256 | while (numIndices) { /* loop until a call to char_value_by_uuid_read() succeeds or we run out of pending indices. */ |
vcoubard | 541:884f95bf5351 | 257 | parentDiscoveryObject->state = DISCOVER_CHARACTERISTIC_UUIDS; |
vcoubard | 541:884f95bf5351 | 258 | |
vcoubard | 541:884f95bf5351 | 259 | unsigned charIndex = getFirst(); |
vcoubard | 541:884f95bf5351 | 260 | ble_uuid_t uuid = { |
vcoubard | 541:884f95bf5351 | 261 | .uuid = BLE_UUID_CHARACTERISTIC, |
vcoubard | 541:884f95bf5351 | 262 | .type = BLE_UUID_TYPE_BLE, |
vcoubard | 541:884f95bf5351 | 263 | }; |
vcoubard | 541:884f95bf5351 | 264 | ble_gattc_handle_range_t handleRange = { }; |
vcoubard | 541:884f95bf5351 | 265 | handleRange.start_handle = parentDiscoveryObject->characteristics[charIndex].getDeclHandle(); |
vcoubard | 541:884f95bf5351 | 266 | handleRange.end_handle = parentDiscoveryObject->characteristics[charIndex].getDeclHandle() + 1; |
vcoubard | 541:884f95bf5351 | 267 | if (sd_ble_gattc_char_value_by_uuid_read(parentDiscoveryObject->connHandle, &uuid, &handleRange) == NRF_SUCCESS) { |
vcoubard | 541:884f95bf5351 | 268 | return; |
vcoubard | 541:884f95bf5351 | 269 | } |
vcoubard | 541:884f95bf5351 | 270 | |
vcoubard | 541:884f95bf5351 | 271 | /* Skip this service if we fail to launch a read for its service-declaration |
vcoubard | 541:884f95bf5351 | 272 | * attribute. Its UUID will remain INVALID, and it may not match any filters. */ |
vcoubard | 541:884f95bf5351 | 273 | dequeue(); |
vcoubard | 541:884f95bf5351 | 274 | } |
vcoubard | 541:884f95bf5351 | 275 | |
vcoubard | 541:884f95bf5351 | 276 | /* Switch back to service discovery upon exhausting the service-indices pending UUID discovery. */ |
vcoubard | 541:884f95bf5351 | 277 | if (parentDiscoveryObject->state == DISCOVER_CHARACTERISTIC_UUIDS) { |
vcoubard | 541:884f95bf5351 | 278 | parentDiscoveryObject->state = CHARACTERISTIC_DISCOVERY_ACTIVE; |
vcoubard | 541:884f95bf5351 | 279 | } |
vcoubard | 541:884f95bf5351 | 280 | } |
vcoubard | 541:884f95bf5351 | 281 | |
vcoubard | 541:884f95bf5351 | 282 | void |
vcoubard | 541:884f95bf5351 | 283 | nRF5xServiceDiscovery::processDiscoverUUIDResponse(const ble_gattc_evt_char_val_by_uuid_read_rsp_t *response) |
vcoubard | 541:884f95bf5351 | 284 | { |
vcoubard | 541:884f95bf5351 | 285 | if (state == DISCOVER_SERVICE_UUIDS) { |
vcoubard | 541:884f95bf5351 | 286 | if ((response->count == 1) && (response->value_len == UUID::LENGTH_OF_LONG_UUID)) { |
vcoubard | 541:884f95bf5351 | 287 | UUID::LongUUIDBytes_t uuid; |
vcoubard | 541:884f95bf5351 | 288 | /* Switch longUUID bytes to MSB */ |
vcoubard | 541:884f95bf5351 | 289 | for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) { |
vcoubard | 541:884f95bf5351 | 290 | uuid[i] = response->handle_value[0].p_value[UUID::LENGTH_OF_LONG_UUID - 1 - i]; |
vcoubard | 541:884f95bf5351 | 291 | } |
vcoubard | 541:884f95bf5351 | 292 | |
vcoubard | 541:884f95bf5351 | 293 | unsigned serviceIndex = serviceUUIDDiscoveryQueue.dequeue(); |
vcoubard | 541:884f95bf5351 | 294 | services[serviceIndex].setupLongUUID(uuid); |
vcoubard | 541:884f95bf5351 | 295 | |
vcoubard | 541:884f95bf5351 | 296 | serviceUUIDDiscoveryQueue.triggerFirst(); |
vcoubard | 541:884f95bf5351 | 297 | } else { |
vcoubard | 541:884f95bf5351 | 298 | serviceUUIDDiscoveryQueue.dequeue(); |
vcoubard | 541:884f95bf5351 | 299 | } |
vcoubard | 541:884f95bf5351 | 300 | } else if (state == DISCOVER_CHARACTERISTIC_UUIDS) { |
vcoubard | 541:884f95bf5351 | 301 | if ((response->count == 1) && (response->value_len == UUID::LENGTH_OF_LONG_UUID + 1 /* props */ + 2 /* value handle */)) { |
vcoubard | 541:884f95bf5351 | 302 | UUID::LongUUIDBytes_t uuid; |
vcoubard | 541:884f95bf5351 | 303 | /* Switch longUUID bytes to MSB */ |
vcoubard | 541:884f95bf5351 | 304 | for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) { |
vcoubard | 541:884f95bf5351 | 305 | uuid[i] = response->handle_value[0].p_value[3 + UUID::LENGTH_OF_LONG_UUID - 1 - i]; |
vcoubard | 541:884f95bf5351 | 306 | } |
vcoubard | 541:884f95bf5351 | 307 | |
vcoubard | 541:884f95bf5351 | 308 | unsigned charIndex = charUUIDDiscoveryQueue.dequeue(); |
vcoubard | 541:884f95bf5351 | 309 | characteristics[charIndex].setupLongUUID(uuid); |
vcoubard | 541:884f95bf5351 | 310 | |
vcoubard | 541:884f95bf5351 | 311 | charUUIDDiscoveryQueue.triggerFirst(); |
vcoubard | 541:884f95bf5351 | 312 | } else { |
vcoubard | 541:884f95bf5351 | 313 | charUUIDDiscoveryQueue.dequeue(); |
vcoubard | 541:884f95bf5351 | 314 | } |
vcoubard | 541:884f95bf5351 | 315 | } |
rgrover1 | 393:0f7c5048efb3 | 316 | } |