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