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