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