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