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