aa
Dependents: Peripheral_1_serial_copy Peripheral_1_serial 151006_1st_Scenario_normal
Fork of nRF51822 by
btle/btle_discovery.cpp@296:8d25fe68d731, 2015-06-19 (annotated)
- Committer:
- rgrover1
- Date:
- Fri Jun 19 15:55:28 2015 +0100
- Revision:
- 296:8d25fe68d731
- Parent:
- 295:5566c7b83bb5
- Child:
- 297:6c432ef3892a
Synchronized with git rev 2d83d6b8
Author: Rohit Grover
setup longUUIDs for services.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 243:a966506d1e5b | 1 | /* mbed Microcontroller Library |
rgrover1 | 243:a966506d1e5b | 2 | * Copyright (c) 2006-2013 ARM Limited |
rgrover1 | 243:a966506d1e5b | 3 | * |
rgrover1 | 243:a966506d1e5b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 243:a966506d1e5b | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 243:a966506d1e5b | 6 | * You may obtain a copy of the License at |
rgrover1 | 243:a966506d1e5b | 7 | * |
rgrover1 | 243:a966506d1e5b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 243:a966506d1e5b | 9 | * |
rgrover1 | 243:a966506d1e5b | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 243:a966506d1e5b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 243:a966506d1e5b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 243:a966506d1e5b | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 243:a966506d1e5b | 14 | * limitations under the License. |
rgrover1 | 243:a966506d1e5b | 15 | */ |
rgrover1 | 243:a966506d1e5b | 16 | |
rgrover1 | 287:faa0dc7ae513 | 17 | #include <stdio.h> |
rgrover1 | 287:faa0dc7ae513 | 18 | |
rgrover1 | 239:693a1f145b5a | 19 | #include "blecommon.h" |
rgrover1 | 239:693a1f145b5a | 20 | #include "UUID.h" |
rgrover1 | 239:693a1f145b5a | 21 | #include "Gap.h" |
rgrover1 | 239:693a1f145b5a | 22 | #include "nrf_error.h" |
rgrover1 | 239:693a1f145b5a | 23 | #include "btle_discovery.h" |
rgrover1 | 239:693a1f145b5a | 24 | #include "ble_err.h" |
rgrover1 | 243:a966506d1e5b | 25 | |
rgrover1 | 278:26e3f0ef1e14 | 26 | static NordicServiceDiscovery sdSingleton; |
rgrover1 | 239:693a1f145b5a | 27 | |
rgrover1 | 239:693a1f145b5a | 28 | ble_error_t |
rgrover1 | 275:fd709cd8b1b2 | 29 | ServiceDiscovery::launch(Gap::Handle_t connectionHandle, |
rgrover1 | 275:fd709cd8b1b2 | 30 | ServiceCallback_t sc, |
rgrover1 | 275:fd709cd8b1b2 | 31 | CharacteristicCallback_t cc, |
rgrover1 | 275:fd709cd8b1b2 | 32 | const UUID &matchingServiceUUIDIn, |
rgrover1 | 275:fd709cd8b1b2 | 33 | const UUID &matchingCharacteristicUUIDIn) |
rgrover1 | 239:693a1f145b5a | 34 | { |
rgrover1 | 281:2280bb1a9a9e | 35 | if (isActive()) { |
rgrover1 | 281:2280bb1a9a9e | 36 | return BLE_ERROR_INVALID_STATE; |
rgrover1 | 281:2280bb1a9a9e | 37 | } |
rgrover1 | 281:2280bb1a9a9e | 38 | |
rgrover1 | 278:26e3f0ef1e14 | 39 | sdSingleton.serviceCallback = sc; |
rgrover1 | 278:26e3f0ef1e14 | 40 | sdSingleton.characteristicCallback = cc; |
rgrover1 | 278:26e3f0ef1e14 | 41 | sdSingleton.matchingServiceUUID = matchingServiceUUIDIn; |
rgrover1 | 278:26e3f0ef1e14 | 42 | sdSingleton.matchingCharacteristicUUID = matchingCharacteristicUUIDIn; |
rgrover1 | 273:8ada9e8111a3 | 43 | |
rgrover1 | 278:26e3f0ef1e14 | 44 | sdSingleton.serviceDiscoveryStarted(connectionHandle); |
rgrover1 | 239:693a1f145b5a | 45 | |
rgrover1 | 239:693a1f145b5a | 46 | uint32_t rc; |
rgrover1 | 248:71ef03789dd3 | 47 | if ((rc = sd_ble_gattc_primary_services_discover(connectionHandle, NordicServiceDiscovery::SRV_DISC_START_HANDLE, NULL)) != NRF_SUCCESS) { |
rgrover1 | 278:26e3f0ef1e14 | 48 | sdSingleton.terminate(); |
rgrover1 | 239:693a1f145b5a | 49 | switch (rc) { |
rgrover1 | 239:693a1f145b5a | 50 | case NRF_ERROR_INVALID_PARAM: |
rgrover1 | 239:693a1f145b5a | 51 | case BLE_ERROR_INVALID_CONN_HANDLE: |
rgrover1 | 239:693a1f145b5a | 52 | return BLE_ERROR_INVALID_PARAM; |
rgrover1 | 239:693a1f145b5a | 53 | case NRF_ERROR_BUSY: |
rgrover1 | 239:693a1f145b5a | 54 | return BLE_STACK_BUSY; |
rgrover1 | 239:693a1f145b5a | 55 | default: |
rgrover1 | 239:693a1f145b5a | 56 | case NRF_ERROR_INVALID_STATE: |
rgrover1 | 239:693a1f145b5a | 57 | return BLE_ERROR_INVALID_STATE; |
rgrover1 | 239:693a1f145b5a | 58 | } |
rgrover1 | 239:693a1f145b5a | 59 | } |
rgrover1 | 239:693a1f145b5a | 60 | |
rgrover1 | 239:693a1f145b5a | 61 | return BLE_ERROR_NONE; |
rgrover1 | 239:693a1f145b5a | 62 | } |
rgrover1 | 239:693a1f145b5a | 63 | |
rgrover1 | 248:71ef03789dd3 | 64 | void |
rgrover1 | 248:71ef03789dd3 | 65 | ServiceDiscovery::terminate(void) |
rgrover1 | 248:71ef03789dd3 | 66 | { |
rgrover1 | 278:26e3f0ef1e14 | 67 | sdSingleton.terminateServiceDiscovery(); |
rgrover1 | 248:71ef03789dd3 | 68 | } |
rgrover1 | 248:71ef03789dd3 | 69 | |
rgrover1 | 279:6e5e79f1fb61 | 70 | bool |
rgrover1 | 279:6e5e79f1fb61 | 71 | ServiceDiscovery::isActive(void) |
rgrover1 | 279:6e5e79f1fb61 | 72 | { |
rgrover1 | 279:6e5e79f1fb61 | 73 | return sdSingleton.isActive(); |
rgrover1 | 279:6e5e79f1fb61 | 74 | } |
rgrover1 | 279:6e5e79f1fb61 | 75 | |
rgrover1 | 280:cbaa4cb83548 | 76 | void ServiceDiscovery::onTermination(TerminationCallback_t callback) { |
rgrover1 | 280:cbaa4cb83548 | 77 | sdSingleton.setOnTermination(callback); |
rgrover1 | 280:cbaa4cb83548 | 78 | } |
rgrover1 | 280:cbaa4cb83548 | 79 | |
rgrover1 | 243:a966506d1e5b | 80 | ble_error_t |
rgrover1 | 252:4b3f294415f5 | 81 | NordicServiceDiscovery::launchCharacteristicDiscovery(Gap::Handle_t connectionHandle, |
rgrover1 | 252:4b3f294415f5 | 82 | Gap::Handle_t startHandle, |
rgrover1 | 252:4b3f294415f5 | 83 | Gap::Handle_t endHandle) |
rgrover1 | 252:4b3f294415f5 | 84 | { |
rgrover1 | 278:26e3f0ef1e14 | 85 | sdSingleton.characteristicDiscoveryStarted(connectionHandle); |
rgrover1 | 239:693a1f145b5a | 86 | |
rgrover1 | 239:693a1f145b5a | 87 | ble_gattc_handle_range_t handleRange = { |
rgrover1 | 239:693a1f145b5a | 88 | .start_handle = startHandle, |
rgrover1 | 239:693a1f145b5a | 89 | .end_handle = endHandle |
rgrover1 | 239:693a1f145b5a | 90 | }; |
rgrover1 | 239:693a1f145b5a | 91 | uint32_t rc; |
rgrover1 | 239:693a1f145b5a | 92 | if ((rc = sd_ble_gattc_characteristics_discover(connectionHandle, &handleRange)) != NRF_SUCCESS) { |
rgrover1 | 278:26e3f0ef1e14 | 93 | sdSingleton.terminateCharacteristicDiscovery(); |
rgrover1 | 239:693a1f145b5a | 94 | switch (rc) { |
rgrover1 | 239:693a1f145b5a | 95 | case BLE_ERROR_INVALID_CONN_HANDLE: |
rgrover1 | 239:693a1f145b5a | 96 | case NRF_ERROR_INVALID_ADDR: |
rgrover1 | 239:693a1f145b5a | 97 | return BLE_ERROR_INVALID_PARAM; |
rgrover1 | 239:693a1f145b5a | 98 | case NRF_ERROR_BUSY: |
rgrover1 | 239:693a1f145b5a | 99 | return BLE_STACK_BUSY; |
rgrover1 | 239:693a1f145b5a | 100 | default: |
rgrover1 | 239:693a1f145b5a | 101 | case NRF_ERROR_INVALID_STATE: |
rgrover1 | 239:693a1f145b5a | 102 | return BLE_ERROR_INVALID_STATE; |
rgrover1 | 239:693a1f145b5a | 103 | } |
rgrover1 | 239:693a1f145b5a | 104 | } |
rgrover1 | 239:693a1f145b5a | 105 | |
rgrover1 | 239:693a1f145b5a | 106 | return BLE_ERROR_NONE; |
rgrover1 | 239:693a1f145b5a | 107 | } |
rgrover1 | 239:693a1f145b5a | 108 | |
rgrover1 | 239:693a1f145b5a | 109 | void |
rgrover1 | 292:01b717803437 | 110 | NordicServiceDiscovery::ServiceIndicesNeedingUUIDDiscovery::triggerFirst(void) |
rgrover1 | 290:83c994bf62d0 | 111 | { |
rgrover1 | 293:cd86acfe41be | 112 | while (numIndices) { /* loop until a call to char_value_by_uuid_read() succeeds or we run out of pending indices. */ |
rgrover1 | 292:01b717803437 | 113 | parentContainer->state = DISCOVER_SERVICE_UUIDS; |
rgrover1 | 290:83c994bf62d0 | 114 | |
rgrover1 | 295:5566c7b83bb5 | 115 | unsigned serviceIndex = getFirst(); |
rgrover1 | 290:83c994bf62d0 | 116 | ble_uuid_t uuid = { |
rgrover1 | 290:83c994bf62d0 | 117 | .uuid = BLE_UUID_SERVICE_PRIMARY, |
rgrover1 | 290:83c994bf62d0 | 118 | .type = BLE_UUID_TYPE_BLE, |
rgrover1 | 290:83c994bf62d0 | 119 | }; |
rgrover1 | 290:83c994bf62d0 | 120 | ble_gattc_handle_range_t handleRange = { |
rgrover1 | 292:01b717803437 | 121 | .start_handle = parentContainer->services[serviceIndex].getStartHandle(), |
rgrover1 | 292:01b717803437 | 122 | .end_handle = parentContainer->services[serviceIndex].getEndHandle(), |
rgrover1 | 290:83c994bf62d0 | 123 | }; |
rgrover1 | 293:cd86acfe41be | 124 | if (sd_ble_gattc_char_value_by_uuid_read(parentContainer->connHandle, &uuid, &handleRange) == NRF_SUCCESS) { |
rgrover1 | 293:cd86acfe41be | 125 | return; |
rgrover1 | 290:83c994bf62d0 | 126 | } |
rgrover1 | 290:83c994bf62d0 | 127 | |
rgrover1 | 293:cd86acfe41be | 128 | /* Skip this service if we fail to launch a read for its service-declaration |
rgrover1 | 293:cd86acfe41be | 129 | * attribute. Its UUID will remain INVALID, and it may not match any filters. */ |
rgrover1 | 293:cd86acfe41be | 130 | removeFirst(); |
rgrover1 | 290:83c994bf62d0 | 131 | } |
rgrover1 | 290:83c994bf62d0 | 132 | |
rgrover1 | 293:cd86acfe41be | 133 | /* Switch back to service discovery upon exhausting the service-indices pending UUID discovery. */ |
rgrover1 | 292:01b717803437 | 134 | if (parentContainer->state == DISCOVER_SERVICE_UUIDS) { |
rgrover1 | 292:01b717803437 | 135 | parentContainer->state = SERVICE_DISCOVERY_ACTIVE; |
rgrover1 | 290:83c994bf62d0 | 136 | } |
rgrover1 | 290:83c994bf62d0 | 137 | } |
rgrover1 | 290:83c994bf62d0 | 138 | |
rgrover1 | 290:83c994bf62d0 | 139 | void |
rgrover1 | 290:83c994bf62d0 | 140 | NordicServiceDiscovery::processDiscoverUUIDResponse(const ble_gattc_evt_char_val_by_uuid_read_rsp_t *response) |
rgrover1 | 290:83c994bf62d0 | 141 | { |
rgrover1 | 290:83c994bf62d0 | 142 | printf("BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP: count %u, len %u\r\n", response->count, response->value_len); |
rgrover1 | 290:83c994bf62d0 | 143 | for (unsigned i = 0; i < response->value_len; i++) { |
rgrover1 | 290:83c994bf62d0 | 144 | printf("%02x ", response->handle_value[0].p_value[i]); |
rgrover1 | 290:83c994bf62d0 | 145 | } |
rgrover1 | 290:83c994bf62d0 | 146 | printf("\r\n"); |
rgrover1 | 290:83c994bf62d0 | 147 | |
rgrover1 | 296:8d25fe68d731 | 148 | if ((response->count == 1) && (response->value_len == UUID::LENGTH_OF_LONG_UUID)) { |
rgrover1 | 296:8d25fe68d731 | 149 | UUID::LongUUIDBytes_t uuid; |
rgrover1 | 296:8d25fe68d731 | 150 | /* Switch longUUID bytes to MSB */ |
rgrover1 | 296:8d25fe68d731 | 151 | for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) { |
rgrover1 | 296:8d25fe68d731 | 152 | uuid[i] = response->handle_value[0].p_value[UUID::LENGTH_OF_LONG_UUID - 1 - i]; |
rgrover1 | 296:8d25fe68d731 | 153 | } |
rgrover1 | 296:8d25fe68d731 | 154 | |
rgrover1 | 296:8d25fe68d731 | 155 | unsigned serviceIndex = serviceIndicesNeedingUUIDDiscovery.getFirst(); |
rgrover1 | 296:8d25fe68d731 | 156 | services[serviceIndex].setupLongUUID(uuid); |
rgrover1 | 296:8d25fe68d731 | 157 | } |
rgrover1 | 296:8d25fe68d731 | 158 | |
rgrover1 | 292:01b717803437 | 159 | serviceIndicesNeedingUUIDDiscovery.removeFirst(); |
rgrover1 | 292:01b717803437 | 160 | serviceIndicesNeedingUUIDDiscovery.triggerFirst(); |
rgrover1 | 290:83c994bf62d0 | 161 | } |
rgrover1 | 290:83c994bf62d0 | 162 | |
rgrover1 | 290:83c994bf62d0 | 163 | void |
rgrover1 | 240:75b69581d1dd | 164 | NordicServiceDiscovery::setupDiscoveredServices(const ble_gattc_evt_prim_srvc_disc_rsp_t *response) |
rgrover1 | 239:693a1f145b5a | 165 | { |
rgrover1 | 242:73fc02cc20b1 | 166 | serviceIndex = 0; |
rgrover1 | 242:73fc02cc20b1 | 167 | numServices = response->count; |
rgrover1 | 239:693a1f145b5a | 168 | |
rgrover1 | 239:693a1f145b5a | 169 | /* Account for the limitation on the number of discovered services we can handle at a time. */ |
rgrover1 | 242:73fc02cc20b1 | 170 | if (numServices > BLE_DB_DISCOVERY_MAX_SRV) { |
rgrover1 | 242:73fc02cc20b1 | 171 | numServices = BLE_DB_DISCOVERY_MAX_SRV; |
rgrover1 | 239:693a1f145b5a | 172 | } |
rgrover1 | 239:693a1f145b5a | 173 | |
rgrover1 | 292:01b717803437 | 174 | serviceIndicesNeedingUUIDDiscovery.reset(); |
rgrover1 | 242:73fc02cc20b1 | 175 | for (unsigned serviceIndex = 0; serviceIndex < numServices; serviceIndex++) { |
rgrover1 | 283:c4c2aeab86c3 | 176 | if (response->services[serviceIndex].uuid.type == BLE_UUID_TYPE_UNKNOWN) { |
rgrover1 | 292:01b717803437 | 177 | serviceIndicesNeedingUUIDDiscovery.append(serviceIndex); |
rgrover1 | 294:a2219bad9d4a | 178 | services[serviceIndex].setup(response->services[serviceIndex].handle_range.start_handle, |
rgrover1 | 294:a2219bad9d4a | 179 | response->services[serviceIndex].handle_range.end_handle); |
rgrover1 | 294:a2219bad9d4a | 180 | } else { |
rgrover1 | 294:a2219bad9d4a | 181 | services[serviceIndex].setup(response->services[serviceIndex].uuid.uuid, |
rgrover1 | 294:a2219bad9d4a | 182 | response->services[serviceIndex].handle_range.start_handle, |
rgrover1 | 294:a2219bad9d4a | 183 | response->services[serviceIndex].handle_range.end_handle); |
rgrover1 | 283:c4c2aeab86c3 | 184 | } |
rgrover1 | 239:693a1f145b5a | 185 | } |
rgrover1 | 290:83c994bf62d0 | 186 | |
rgrover1 | 290:83c994bf62d0 | 187 | /* Trigger discovery of service UUID if necessary. */ |
rgrover1 | 292:01b717803437 | 188 | if (serviceIndicesNeedingUUIDDiscovery.getCount()) { |
rgrover1 | 292:01b717803437 | 189 | serviceIndicesNeedingUUIDDiscovery.triggerFirst(); |
rgrover1 | 290:83c994bf62d0 | 190 | } |
rgrover1 | 239:693a1f145b5a | 191 | } |
rgrover1 | 239:693a1f145b5a | 192 | |
rgrover1 | 239:693a1f145b5a | 193 | void |
rgrover1 | 240:75b69581d1dd | 194 | NordicServiceDiscovery::setupDiscoveredCharacteristics(const ble_gattc_evt_char_disc_rsp_t *response) |
rgrover1 | 239:693a1f145b5a | 195 | { |
rgrover1 | 242:73fc02cc20b1 | 196 | characteristicIndex = 0; |
rgrover1 | 242:73fc02cc20b1 | 197 | numCharacteristics = response->count; |
rgrover1 | 239:693a1f145b5a | 198 | |
rgrover1 | 239:693a1f145b5a | 199 | /* Account for the limitation on the number of discovered characteristics we can handle at a time. */ |
rgrover1 | 242:73fc02cc20b1 | 200 | if (numCharacteristics > BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV) { |
rgrover1 | 242:73fc02cc20b1 | 201 | numCharacteristics = BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV; |
rgrover1 | 239:693a1f145b5a | 202 | } |
rgrover1 | 239:693a1f145b5a | 203 | |
rgrover1 | 242:73fc02cc20b1 | 204 | for (unsigned charIndex = 0; charIndex < numCharacteristics; charIndex++) { |
rgrover1 | 239:693a1f145b5a | 205 | characteristics[charIndex].setup(response->chars[charIndex].uuid.uuid, |
rgrover1 | 239:693a1f145b5a | 206 | *(const uint8_t *)(&response->chars[charIndex].char_props), |
rgrover1 | 239:693a1f145b5a | 207 | response->chars[charIndex].handle_decl, |
rgrover1 | 239:693a1f145b5a | 208 | response->chars[charIndex].handle_value); |
rgrover1 | 239:693a1f145b5a | 209 | } |
rgrover1 | 243:a966506d1e5b | 210 | } |
rgrover1 | 243:a966506d1e5b | 211 | |
rgrover1 | 251:d4e0cf5e8751 | 212 | void |
rgrover1 | 251:d4e0cf5e8751 | 213 | NordicServiceDiscovery::progressCharacteristicDiscovery(void) |
rgrover1 | 251:d4e0cf5e8751 | 214 | { |
rgrover1 | 276:ae6e5129b395 | 215 | /* Iterate through the previously discovered characteristics cached in characteristics[]. */ |
rgrover1 | 286:042b01e59cf8 | 216 | while ((state == CHARACTERISTIC_DISCOVERY_ACTIVE) && (characteristicIndex < numCharacteristics)) { |
rgrover1 | 285:55ac765bcc8d | 217 | if ((matchingCharacteristicUUID == UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)) || |
rgrover1 | 282:6633922380bf | 218 | (matchingCharacteristicUUID == characteristics[characteristicIndex].getShortUUID())) { |
rgrover1 | 282:6633922380bf | 219 | if (characteristicCallback) { |
rgrover1 | 282:6633922380bf | 220 | characteristicCallback(characteristics[characteristicIndex]); |
rgrover1 | 282:6633922380bf | 221 | } |
rgrover1 | 255:d228656b0cd1 | 222 | } |
rgrover1 | 251:d4e0cf5e8751 | 223 | |
rgrover1 | 251:d4e0cf5e8751 | 224 | characteristicIndex++; |
rgrover1 | 251:d4e0cf5e8751 | 225 | } |
rgrover1 | 251:d4e0cf5e8751 | 226 | |
rgrover1 | 276:ae6e5129b395 | 227 | /* Relaunch discovery of new characteristics beyond the last entry cached in characteristics[]. */ |
rgrover1 | 286:042b01e59cf8 | 228 | if (state == CHARACTERISTIC_DISCOVERY_ACTIVE) { |
rgrover1 | 276:ae6e5129b395 | 229 | /* Determine the ending handle of the last cached characteristic. */ |
rgrover1 | 264:5533ac0adf4b | 230 | Gap::Handle_t startHandle = characteristics[characteristicIndex - 1].getValueHandle() + 1; |
rgrover1 | 264:5533ac0adf4b | 231 | Gap::Handle_t endHandle = services[serviceIndex].getEndHandle(); |
rgrover1 | 274:f82e0ecba699 | 232 | resetDiscoveredCharacteristics(); /* Note: resetDiscoveredCharacteristics() must come after fetching start and end Handles. */ |
rgrover1 | 251:d4e0cf5e8751 | 233 | |
rgrover1 | 251:d4e0cf5e8751 | 234 | if (startHandle < endHandle) { |
rgrover1 | 251:d4e0cf5e8751 | 235 | ble_gattc_handle_range_t handleRange = { |
rgrover1 | 251:d4e0cf5e8751 | 236 | .start_handle = startHandle, |
rgrover1 | 251:d4e0cf5e8751 | 237 | .end_handle = endHandle |
rgrover1 | 251:d4e0cf5e8751 | 238 | }; |
rgrover1 | 258:62d6bf03e6a4 | 239 | if (sd_ble_gattc_characteristics_discover(connHandle, &handleRange) != NRF_SUCCESS) { |
rgrover1 | 258:62d6bf03e6a4 | 240 | terminateCharacteristicDiscovery(); |
rgrover1 | 258:62d6bf03e6a4 | 241 | } |
rgrover1 | 251:d4e0cf5e8751 | 242 | } else { |
rgrover1 | 277:91705df8495e | 243 | terminateCharacteristicDiscovery(); |
rgrover1 | 251:d4e0cf5e8751 | 244 | } |
rgrover1 | 251:d4e0cf5e8751 | 245 | } |
rgrover1 | 251:d4e0cf5e8751 | 246 | } |
rgrover1 | 251:d4e0cf5e8751 | 247 | |
rgrover1 | 251:d4e0cf5e8751 | 248 | void |
rgrover1 | 251:d4e0cf5e8751 | 249 | NordicServiceDiscovery::progressServiceDiscovery(void) |
rgrover1 | 251:d4e0cf5e8751 | 250 | { |
rgrover1 | 276:ae6e5129b395 | 251 | /* Iterate through the previously discovered services cached in services[]. */ |
rgrover1 | 286:042b01e59cf8 | 252 | while ((state == SERVICE_DISCOVERY_ACTIVE) && (serviceIndex < numServices)) { |
rgrover1 | 285:55ac765bcc8d | 253 | if ((matchingServiceUUID == UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)) || |
rgrover1 | 277:91705df8495e | 254 | (matchingServiceUUID == services[serviceIndex].getShortUUID())) { |
rgrover1 | 277:91705df8495e | 255 | if (serviceCallback) { |
rgrover1 | 277:91705df8495e | 256 | serviceCallback(services[serviceIndex]); |
rgrover1 | 277:91705df8495e | 257 | } |
rgrover1 | 251:d4e0cf5e8751 | 258 | |
rgrover1 | 286:042b01e59cf8 | 259 | if ((state == SERVICE_DISCOVERY_ACTIVE) && characteristicCallback) { |
rgrover1 | 277:91705df8495e | 260 | launchCharacteristicDiscovery(connHandle, services[serviceIndex].getStartHandle(), services[serviceIndex].getEndHandle()); |
rgrover1 | 277:91705df8495e | 261 | } else { |
rgrover1 | 277:91705df8495e | 262 | serviceIndex++; |
rgrover1 | 277:91705df8495e | 263 | } |
rgrover1 | 251:d4e0cf5e8751 | 264 | } else { |
rgrover1 | 277:91705df8495e | 265 | serviceIndex++; |
rgrover1 | 251:d4e0cf5e8751 | 266 | } |
rgrover1 | 251:d4e0cf5e8751 | 267 | } |
rgrover1 | 276:ae6e5129b395 | 268 | |
rgrover1 | 276:ae6e5129b395 | 269 | /* Relaunch discovery of new services beyond the last entry cached in services[]. */ |
rgrover1 | 286:042b01e59cf8 | 270 | if ((state == SERVICE_DISCOVERY_ACTIVE) && (numServices > 0) && (serviceIndex > 0)) { |
rgrover1 | 276:ae6e5129b395 | 271 | /* Determine the ending handle of the last cached service. */ |
rgrover1 | 264:5533ac0adf4b | 272 | Gap::Handle_t endHandle = services[serviceIndex - 1].getEndHandle(); |
rgrover1 | 274:f82e0ecba699 | 273 | resetDiscoveredServices(); /* Note: resetDiscoveredServices() must come after fetching endHandle. */ |
rgrover1 | 251:d4e0cf5e8751 | 274 | |
rgrover1 | 284:29fb6af6671d | 275 | if (endHandle == SRV_DISC_END_HANDLE) { |
rgrover1 | 258:62d6bf03e6a4 | 276 | terminateServiceDiscovery(); |
rgrover1 | 284:29fb6af6671d | 277 | } else { |
rgrover1 | 284:29fb6af6671d | 278 | if (sd_ble_gattc_primary_services_discover(connHandle, endHandle, NULL) != NRF_SUCCESS) { |
rgrover1 | 284:29fb6af6671d | 279 | terminateServiceDiscovery(); |
rgrover1 | 284:29fb6af6671d | 280 | } |
rgrover1 | 258:62d6bf03e6a4 | 281 | } |
rgrover1 | 251:d4e0cf5e8751 | 282 | } |
rgrover1 | 251:d4e0cf5e8751 | 283 | } |
rgrover1 | 251:d4e0cf5e8751 | 284 | |
rgrover1 | 243:a966506d1e5b | 285 | void bleGattcEventHandler(const ble_evt_t *p_ble_evt) |
rgrover1 | 243:a966506d1e5b | 286 | { |
rgrover1 | 243:a966506d1e5b | 287 | switch (p_ble_evt->header.evt_id) { |
rgrover1 | 243:a966506d1e5b | 288 | case BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP: |
rgrover1 | 243:a966506d1e5b | 289 | switch (p_ble_evt->evt.gattc_evt.gatt_status) { |
rgrover1 | 243:a966506d1e5b | 290 | case BLE_GATT_STATUS_SUCCESS: |
rgrover1 | 278:26e3f0ef1e14 | 291 | sdSingleton.setupDiscoveredServices(&p_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp); |
rgrover1 | 243:a966506d1e5b | 292 | break; |
rgrover1 | 243:a966506d1e5b | 293 | |
rgrover1 | 243:a966506d1e5b | 294 | case BLE_GATT_STATUS_ATTERR_ATTRIBUTE_NOT_FOUND: |
rgrover1 | 243:a966506d1e5b | 295 | default: |
rgrover1 | 278:26e3f0ef1e14 | 296 | sdSingleton.terminate(); |
rgrover1 | 243:a966506d1e5b | 297 | break; |
rgrover1 | 243:a966506d1e5b | 298 | } |
rgrover1 | 243:a966506d1e5b | 299 | break; |
rgrover1 | 243:a966506d1e5b | 300 | |
rgrover1 | 243:a966506d1e5b | 301 | case BLE_GATTC_EVT_CHAR_DISC_RSP: |
rgrover1 | 243:a966506d1e5b | 302 | switch (p_ble_evt->evt.gattc_evt.gatt_status) { |
rgrover1 | 243:a966506d1e5b | 303 | case BLE_GATT_STATUS_SUCCESS: |
rgrover1 | 278:26e3f0ef1e14 | 304 | sdSingleton.setupDiscoveredCharacteristics(&p_ble_evt->evt.gattc_evt.params.char_disc_rsp); |
rgrover1 | 243:a966506d1e5b | 305 | break; |
rgrover1 | 243:a966506d1e5b | 306 | |
rgrover1 | 243:a966506d1e5b | 307 | case BLE_GATT_STATUS_ATTERR_ATTRIBUTE_NOT_FOUND: |
rgrover1 | 243:a966506d1e5b | 308 | default: |
rgrover1 | 278:26e3f0ef1e14 | 309 | sdSingleton.terminateCharacteristicDiscovery(); |
rgrover1 | 243:a966506d1e5b | 310 | break; |
rgrover1 | 243:a966506d1e5b | 311 | } |
rgrover1 | 243:a966506d1e5b | 312 | break; |
rgrover1 | 290:83c994bf62d0 | 313 | case BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP: { |
rgrover1 | 290:83c994bf62d0 | 314 | if (sdSingleton.isActive()) { |
rgrover1 | 290:83c994bf62d0 | 315 | sdSingleton.processDiscoverUUIDResponse(&p_ble_evt->evt.gattc_evt.params.char_val_by_uuid_read_rsp); |
rgrover1 | 290:83c994bf62d0 | 316 | } |
rgrover1 | 290:83c994bf62d0 | 317 | |
rgrover1 | 290:83c994bf62d0 | 318 | break; |
rgrover1 | 290:83c994bf62d0 | 319 | } |
rgrover1 | 243:a966506d1e5b | 320 | } |
rgrover1 | 243:a966506d1e5b | 321 | |
rgrover1 | 278:26e3f0ef1e14 | 322 | sdSingleton.progressCharacteristicDiscovery(); |
rgrover1 | 278:26e3f0ef1e14 | 323 | sdSingleton.progressServiceDiscovery(); |
rgrover1 | 239:693a1f145b5a | 324 | } |