fdsf
Fork of nRF51822 by
btle/btle_discovery.cpp@277:91705df8495e, 2015-06-19 (annotated)
- Committer:
- rgrover1
- Date:
- Fri Jun 19 15:55:26 2015 +0100
- Revision:
- 277:91705df8495e
- Parent:
- 276:ae6e5129b395
- Child:
- 278:26e3f0ef1e14
Synchronized with git rev 661c9c6d
Author: Rohit Grover
SDiscovery: handle UUID matches properly during progression of 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 | 239:693a1f145b5a | 17 | #include "blecommon.h" |
rgrover1 | 239:693a1f145b5a | 18 | #include "UUID.h" |
rgrover1 | 239:693a1f145b5a | 19 | #include "Gap.h" |
rgrover1 | 239:693a1f145b5a | 20 | #include "nrf_error.h" |
rgrover1 | 239:693a1f145b5a | 21 | #include "btle_discovery.h" |
rgrover1 | 239:693a1f145b5a | 22 | #include "ble_err.h" |
rgrover1 | 243:a966506d1e5b | 23 | |
rgrover1 | 243:a966506d1e5b | 24 | static NordicServiceDiscovery discoverySingleton; |
rgrover1 | 239:693a1f145b5a | 25 | |
rgrover1 | 239:693a1f145b5a | 26 | ble_error_t |
rgrover1 | 275:fd709cd8b1b2 | 27 | ServiceDiscovery::launch(Gap::Handle_t connectionHandle, |
rgrover1 | 275:fd709cd8b1b2 | 28 | ServiceCallback_t sc, |
rgrover1 | 275:fd709cd8b1b2 | 29 | CharacteristicCallback_t cc, |
rgrover1 | 275:fd709cd8b1b2 | 30 | const UUID &matchingServiceUUIDIn, |
rgrover1 | 275:fd709cd8b1b2 | 31 | const UUID &matchingCharacteristicUUIDIn) |
rgrover1 | 239:693a1f145b5a | 32 | { |
rgrover1 | 273:8ada9e8111a3 | 33 | discoverySingleton.serviceCallback = sc; |
rgrover1 | 273:8ada9e8111a3 | 34 | discoverySingleton.characteristicCallback = cc; |
rgrover1 | 273:8ada9e8111a3 | 35 | discoverySingleton.matchingServiceUUID = matchingServiceUUIDIn; |
rgrover1 | 273:8ada9e8111a3 | 36 | discoverySingleton.matchingCharacteristicUUID = matchingCharacteristicUUIDIn; |
rgrover1 | 273:8ada9e8111a3 | 37 | |
rgrover1 | 243:a966506d1e5b | 38 | discoverySingleton.serviceDiscoveryStarted(connectionHandle); |
rgrover1 | 239:693a1f145b5a | 39 | |
rgrover1 | 239:693a1f145b5a | 40 | uint32_t rc; |
rgrover1 | 248:71ef03789dd3 | 41 | if ((rc = sd_ble_gattc_primary_services_discover(connectionHandle, NordicServiceDiscovery::SRV_DISC_START_HANDLE, NULL)) != NRF_SUCCESS) { |
rgrover1 | 248:71ef03789dd3 | 42 | discoverySingleton.terminate(); |
rgrover1 | 239:693a1f145b5a | 43 | switch (rc) { |
rgrover1 | 239:693a1f145b5a | 44 | case NRF_ERROR_INVALID_PARAM: |
rgrover1 | 239:693a1f145b5a | 45 | case BLE_ERROR_INVALID_CONN_HANDLE: |
rgrover1 | 239:693a1f145b5a | 46 | return BLE_ERROR_INVALID_PARAM; |
rgrover1 | 239:693a1f145b5a | 47 | case NRF_ERROR_BUSY: |
rgrover1 | 239:693a1f145b5a | 48 | return BLE_STACK_BUSY; |
rgrover1 | 239:693a1f145b5a | 49 | default: |
rgrover1 | 239:693a1f145b5a | 50 | case NRF_ERROR_INVALID_STATE: |
rgrover1 | 239:693a1f145b5a | 51 | return BLE_ERROR_INVALID_STATE; |
rgrover1 | 239:693a1f145b5a | 52 | } |
rgrover1 | 239:693a1f145b5a | 53 | } |
rgrover1 | 239:693a1f145b5a | 54 | |
rgrover1 | 239:693a1f145b5a | 55 | return BLE_ERROR_NONE; |
rgrover1 | 239:693a1f145b5a | 56 | } |
rgrover1 | 239:693a1f145b5a | 57 | |
rgrover1 | 248:71ef03789dd3 | 58 | void |
rgrover1 | 248:71ef03789dd3 | 59 | ServiceDiscovery::terminate(void) |
rgrover1 | 248:71ef03789dd3 | 60 | { |
rgrover1 | 248:71ef03789dd3 | 61 | discoverySingleton.terminateServiceDiscovery(); |
rgrover1 | 248:71ef03789dd3 | 62 | } |
rgrover1 | 248:71ef03789dd3 | 63 | |
rgrover1 | 243:a966506d1e5b | 64 | ble_error_t |
rgrover1 | 252:4b3f294415f5 | 65 | NordicServiceDiscovery::launchCharacteristicDiscovery(Gap::Handle_t connectionHandle, |
rgrover1 | 252:4b3f294415f5 | 66 | Gap::Handle_t startHandle, |
rgrover1 | 252:4b3f294415f5 | 67 | Gap::Handle_t endHandle) |
rgrover1 | 252:4b3f294415f5 | 68 | { |
rgrover1 | 243:a966506d1e5b | 69 | discoverySingleton.characteristicDiscoveryStarted(connectionHandle); |
rgrover1 | 239:693a1f145b5a | 70 | |
rgrover1 | 239:693a1f145b5a | 71 | ble_gattc_handle_range_t handleRange = { |
rgrover1 | 239:693a1f145b5a | 72 | .start_handle = startHandle, |
rgrover1 | 239:693a1f145b5a | 73 | .end_handle = endHandle |
rgrover1 | 239:693a1f145b5a | 74 | }; |
rgrover1 | 239:693a1f145b5a | 75 | uint32_t rc; |
rgrover1 | 239:693a1f145b5a | 76 | if ((rc = sd_ble_gattc_characteristics_discover(connectionHandle, &handleRange)) != NRF_SUCCESS) { |
rgrover1 | 243:a966506d1e5b | 77 | discoverySingleton.terminateCharacteristicDiscovery(); |
rgrover1 | 239:693a1f145b5a | 78 | switch (rc) { |
rgrover1 | 239:693a1f145b5a | 79 | case BLE_ERROR_INVALID_CONN_HANDLE: |
rgrover1 | 239:693a1f145b5a | 80 | case NRF_ERROR_INVALID_ADDR: |
rgrover1 | 239:693a1f145b5a | 81 | return BLE_ERROR_INVALID_PARAM; |
rgrover1 | 239:693a1f145b5a | 82 | case NRF_ERROR_BUSY: |
rgrover1 | 239:693a1f145b5a | 83 | return BLE_STACK_BUSY; |
rgrover1 | 239:693a1f145b5a | 84 | default: |
rgrover1 | 239:693a1f145b5a | 85 | case NRF_ERROR_INVALID_STATE: |
rgrover1 | 239:693a1f145b5a | 86 | return BLE_ERROR_INVALID_STATE; |
rgrover1 | 239:693a1f145b5a | 87 | } |
rgrover1 | 239:693a1f145b5a | 88 | } |
rgrover1 | 239:693a1f145b5a | 89 | |
rgrover1 | 239:693a1f145b5a | 90 | return BLE_ERROR_NONE; |
rgrover1 | 239:693a1f145b5a | 91 | } |
rgrover1 | 239:693a1f145b5a | 92 | |
rgrover1 | 239:693a1f145b5a | 93 | void |
rgrover1 | 240:75b69581d1dd | 94 | NordicServiceDiscovery::setupDiscoveredServices(const ble_gattc_evt_prim_srvc_disc_rsp_t *response) |
rgrover1 | 239:693a1f145b5a | 95 | { |
rgrover1 | 242:73fc02cc20b1 | 96 | serviceIndex = 0; |
rgrover1 | 242:73fc02cc20b1 | 97 | numServices = response->count; |
rgrover1 | 239:693a1f145b5a | 98 | |
rgrover1 | 239:693a1f145b5a | 99 | /* Account for the limitation on the number of discovered services we can handle at a time. */ |
rgrover1 | 242:73fc02cc20b1 | 100 | if (numServices > BLE_DB_DISCOVERY_MAX_SRV) { |
rgrover1 | 242:73fc02cc20b1 | 101 | numServices = BLE_DB_DISCOVERY_MAX_SRV; |
rgrover1 | 239:693a1f145b5a | 102 | } |
rgrover1 | 239:693a1f145b5a | 103 | |
rgrover1 | 242:73fc02cc20b1 | 104 | for (unsigned serviceIndex = 0; serviceIndex < numServices; serviceIndex++) { |
rgrover1 | 239:693a1f145b5a | 105 | services[serviceIndex].setup(response->services[serviceIndex].uuid.uuid, |
rgrover1 | 239:693a1f145b5a | 106 | response->services[serviceIndex].handle_range.start_handle, |
rgrover1 | 239:693a1f145b5a | 107 | response->services[serviceIndex].handle_range.end_handle); |
rgrover1 | 239:693a1f145b5a | 108 | } |
rgrover1 | 239:693a1f145b5a | 109 | } |
rgrover1 | 239:693a1f145b5a | 110 | |
rgrover1 | 239:693a1f145b5a | 111 | void |
rgrover1 | 240:75b69581d1dd | 112 | NordicServiceDiscovery::setupDiscoveredCharacteristics(const ble_gattc_evt_char_disc_rsp_t *response) |
rgrover1 | 239:693a1f145b5a | 113 | { |
rgrover1 | 242:73fc02cc20b1 | 114 | characteristicIndex = 0; |
rgrover1 | 242:73fc02cc20b1 | 115 | numCharacteristics = response->count; |
rgrover1 | 239:693a1f145b5a | 116 | |
rgrover1 | 239:693a1f145b5a | 117 | /* Account for the limitation on the number of discovered characteristics we can handle at a time. */ |
rgrover1 | 242:73fc02cc20b1 | 118 | if (numCharacteristics > BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV) { |
rgrover1 | 242:73fc02cc20b1 | 119 | numCharacteristics = BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV; |
rgrover1 | 239:693a1f145b5a | 120 | } |
rgrover1 | 239:693a1f145b5a | 121 | |
rgrover1 | 242:73fc02cc20b1 | 122 | for (unsigned charIndex = 0; charIndex < numCharacteristics; charIndex++) { |
rgrover1 | 239:693a1f145b5a | 123 | characteristics[charIndex].setup(response->chars[charIndex].uuid.uuid, |
rgrover1 | 239:693a1f145b5a | 124 | *(const uint8_t *)(&response->chars[charIndex].char_props), |
rgrover1 | 239:693a1f145b5a | 125 | response->chars[charIndex].handle_decl, |
rgrover1 | 239:693a1f145b5a | 126 | response->chars[charIndex].handle_value); |
rgrover1 | 239:693a1f145b5a | 127 | } |
rgrover1 | 243:a966506d1e5b | 128 | } |
rgrover1 | 243:a966506d1e5b | 129 | |
rgrover1 | 251:d4e0cf5e8751 | 130 | void |
rgrover1 | 251:d4e0cf5e8751 | 131 | NordicServiceDiscovery::progressCharacteristicDiscovery(void) |
rgrover1 | 251:d4e0cf5e8751 | 132 | { |
rgrover1 | 276:ae6e5129b395 | 133 | /* Iterate through the previously discovered characteristics cached in characteristics[]. */ |
rgrover1 | 251:d4e0cf5e8751 | 134 | while (cDiscoveryActive && (characteristicIndex < numCharacteristics)) { |
rgrover1 | 255:d228656b0cd1 | 135 | if (characteristicCallback) { |
rgrover1 | 255:d228656b0cd1 | 136 | characteristicCallback(characteristics[characteristicIndex]); |
rgrover1 | 255:d228656b0cd1 | 137 | } |
rgrover1 | 251:d4e0cf5e8751 | 138 | |
rgrover1 | 251:d4e0cf5e8751 | 139 | characteristicIndex++; |
rgrover1 | 251:d4e0cf5e8751 | 140 | } |
rgrover1 | 251:d4e0cf5e8751 | 141 | |
rgrover1 | 276:ae6e5129b395 | 142 | /* Relaunch discovery of new characteristics beyond the last entry cached in characteristics[]. */ |
rgrover1 | 251:d4e0cf5e8751 | 143 | if (cDiscoveryActive) { |
rgrover1 | 276:ae6e5129b395 | 144 | /* Determine the ending handle of the last cached characteristic. */ |
rgrover1 | 264:5533ac0adf4b | 145 | Gap::Handle_t startHandle = characteristics[characteristicIndex - 1].getValueHandle() + 1; |
rgrover1 | 264:5533ac0adf4b | 146 | Gap::Handle_t endHandle = services[serviceIndex].getEndHandle(); |
rgrover1 | 274:f82e0ecba699 | 147 | resetDiscoveredCharacteristics(); /* Note: resetDiscoveredCharacteristics() must come after fetching start and end Handles. */ |
rgrover1 | 251:d4e0cf5e8751 | 148 | |
rgrover1 | 251:d4e0cf5e8751 | 149 | if (startHandle < endHandle) { |
rgrover1 | 251:d4e0cf5e8751 | 150 | ble_gattc_handle_range_t handleRange = { |
rgrover1 | 251:d4e0cf5e8751 | 151 | .start_handle = startHandle, |
rgrover1 | 251:d4e0cf5e8751 | 152 | .end_handle = endHandle |
rgrover1 | 251:d4e0cf5e8751 | 153 | }; |
rgrover1 | 258:62d6bf03e6a4 | 154 | if (sd_ble_gattc_characteristics_discover(connHandle, &handleRange) != NRF_SUCCESS) { |
rgrover1 | 258:62d6bf03e6a4 | 155 | terminateCharacteristicDiscovery(); |
rgrover1 | 258:62d6bf03e6a4 | 156 | } |
rgrover1 | 251:d4e0cf5e8751 | 157 | } else { |
rgrover1 | 277:91705df8495e | 158 | terminateCharacteristicDiscovery(); |
rgrover1 | 251:d4e0cf5e8751 | 159 | } |
rgrover1 | 251:d4e0cf5e8751 | 160 | } |
rgrover1 | 251:d4e0cf5e8751 | 161 | } |
rgrover1 | 251:d4e0cf5e8751 | 162 | |
rgrover1 | 251:d4e0cf5e8751 | 163 | void |
rgrover1 | 251:d4e0cf5e8751 | 164 | NordicServiceDiscovery::progressServiceDiscovery(void) |
rgrover1 | 251:d4e0cf5e8751 | 165 | { |
rgrover1 | 276:ae6e5129b395 | 166 | /* Iterate through the previously discovered services cached in services[]. */ |
rgrover1 | 251:d4e0cf5e8751 | 167 | while (sDiscoveryActive && (serviceIndex < numServices)) { |
rgrover1 | 277:91705df8495e | 168 | if ((matchingServiceUUID == ShortUUIDBytes_t(BLE_UUID_UNKNOWN)) || |
rgrover1 | 277:91705df8495e | 169 | (matchingServiceUUID == services[serviceIndex].getShortUUID())) { |
rgrover1 | 277:91705df8495e | 170 | if (serviceCallback) { |
rgrover1 | 277:91705df8495e | 171 | serviceCallback(services[serviceIndex]); |
rgrover1 | 277:91705df8495e | 172 | } |
rgrover1 | 251:d4e0cf5e8751 | 173 | |
rgrover1 | 277:91705df8495e | 174 | if (sDiscoveryActive && characteristicCallback) { |
rgrover1 | 277:91705df8495e | 175 | launchCharacteristicDiscovery(connHandle, services[serviceIndex].getStartHandle(), services[serviceIndex].getEndHandle()); |
rgrover1 | 277:91705df8495e | 176 | } else { |
rgrover1 | 277:91705df8495e | 177 | serviceIndex++; |
rgrover1 | 277:91705df8495e | 178 | } |
rgrover1 | 251:d4e0cf5e8751 | 179 | } else { |
rgrover1 | 277:91705df8495e | 180 | serviceIndex++; |
rgrover1 | 251:d4e0cf5e8751 | 181 | } |
rgrover1 | 251:d4e0cf5e8751 | 182 | } |
rgrover1 | 276:ae6e5129b395 | 183 | |
rgrover1 | 276:ae6e5129b395 | 184 | /* Relaunch discovery of new services beyond the last entry cached in services[]. */ |
rgrover1 | 251:d4e0cf5e8751 | 185 | if (sDiscoveryActive && (numServices > 0) && (serviceIndex > 0)) { |
rgrover1 | 276:ae6e5129b395 | 186 | /* Determine the ending handle of the last cached service. */ |
rgrover1 | 264:5533ac0adf4b | 187 | Gap::Handle_t endHandle = services[serviceIndex - 1].getEndHandle(); |
rgrover1 | 274:f82e0ecba699 | 188 | resetDiscoveredServices(); /* Note: resetDiscoveredServices() must come after fetching endHandle. */ |
rgrover1 | 251:d4e0cf5e8751 | 189 | |
rgrover1 | 258:62d6bf03e6a4 | 190 | if (sd_ble_gattc_primary_services_discover(connHandle, endHandle, NULL) != NRF_SUCCESS) { |
rgrover1 | 258:62d6bf03e6a4 | 191 | terminateServiceDiscovery(); |
rgrover1 | 258:62d6bf03e6a4 | 192 | } |
rgrover1 | 251:d4e0cf5e8751 | 193 | } |
rgrover1 | 251:d4e0cf5e8751 | 194 | } |
rgrover1 | 251:d4e0cf5e8751 | 195 | |
rgrover1 | 243:a966506d1e5b | 196 | void bleGattcEventHandler(const ble_evt_t *p_ble_evt) |
rgrover1 | 243:a966506d1e5b | 197 | { |
rgrover1 | 243:a966506d1e5b | 198 | switch (p_ble_evt->header.evt_id) { |
rgrover1 | 243:a966506d1e5b | 199 | case BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP: |
rgrover1 | 243:a966506d1e5b | 200 | switch (p_ble_evt->evt.gattc_evt.gatt_status) { |
rgrover1 | 243:a966506d1e5b | 201 | case BLE_GATT_STATUS_SUCCESS: |
rgrover1 | 243:a966506d1e5b | 202 | discoverySingleton.setupDiscoveredServices(&p_ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp); |
rgrover1 | 243:a966506d1e5b | 203 | break; |
rgrover1 | 243:a966506d1e5b | 204 | |
rgrover1 | 243:a966506d1e5b | 205 | case BLE_GATT_STATUS_ATTERR_ATTRIBUTE_NOT_FOUND: |
rgrover1 | 243:a966506d1e5b | 206 | default: |
rgrover1 | 243:a966506d1e5b | 207 | discoverySingleton.terminate(); |
rgrover1 | 243:a966506d1e5b | 208 | break; |
rgrover1 | 243:a966506d1e5b | 209 | } |
rgrover1 | 243:a966506d1e5b | 210 | break; |
rgrover1 | 243:a966506d1e5b | 211 | |
rgrover1 | 243:a966506d1e5b | 212 | case BLE_GATTC_EVT_CHAR_DISC_RSP: |
rgrover1 | 243:a966506d1e5b | 213 | switch (p_ble_evt->evt.gattc_evt.gatt_status) { |
rgrover1 | 243:a966506d1e5b | 214 | case BLE_GATT_STATUS_SUCCESS: |
rgrover1 | 243:a966506d1e5b | 215 | discoverySingleton.setupDiscoveredCharacteristics(&p_ble_evt->evt.gattc_evt.params.char_disc_rsp); |
rgrover1 | 243:a966506d1e5b | 216 | break; |
rgrover1 | 243:a966506d1e5b | 217 | |
rgrover1 | 243:a966506d1e5b | 218 | case BLE_GATT_STATUS_ATTERR_ATTRIBUTE_NOT_FOUND: |
rgrover1 | 243:a966506d1e5b | 219 | default: |
rgrover1 | 243:a966506d1e5b | 220 | discoverySingleton.terminateCharacteristicDiscovery(); |
rgrover1 | 243:a966506d1e5b | 221 | break; |
rgrover1 | 243:a966506d1e5b | 222 | } |
rgrover1 | 243:a966506d1e5b | 223 | break; |
rgrover1 | 243:a966506d1e5b | 224 | } |
rgrover1 | 243:a966506d1e5b | 225 | |
rgrover1 | 243:a966506d1e5b | 226 | discoverySingleton.progressCharacteristicDiscovery(); |
rgrover1 | 243:a966506d1e5b | 227 | discoverySingleton.progressServiceDiscovery(); |
rgrover1 | 239:693a1f145b5a | 228 | } |