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