aa
Dependents: Peripheral_1_serial_copy Peripheral_1_serial 151006_1st_Scenario_normal
Fork of nRF51822 by
btle/btle_discovery.cpp@242:73fc02cc20b1, 2015-06-19 (annotated)
- Committer:
- rgrover1
- Date:
- Fri Jun 19 15:55:21 2015 +0100
- Revision:
- 242:73fc02cc20b1
- Parent:
- 240:75b69581d1dd
- Child:
- 243:a966506d1e5b
Synchronized with git rev a2e83d29
Author: Rohit Grover
variable renames.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 239:693a1f145b5a | 1 | #include "blecommon.h" |
rgrover1 | 239:693a1f145b5a | 2 | #include "UUID.h" |
rgrover1 | 239:693a1f145b5a | 3 | #include "Gap.h" |
rgrover1 | 239:693a1f145b5a | 4 | #include "nrf_error.h" |
rgrover1 | 239:693a1f145b5a | 5 | #include "btle_discovery.h" |
rgrover1 | 239:693a1f145b5a | 6 | #include "ble_err.h" |
rgrover1 | 239:693a1f145b5a | 7 | |
rgrover1 | 239:693a1f145b5a | 8 | ble_error_t |
rgrover1 | 239:693a1f145b5a | 9 | ServiceDiscovery::launch(Gap::Handle_t connectionHandle, ServiceCallback_t sc, CharacteristicCallback_t cc) |
rgrover1 | 239:693a1f145b5a | 10 | { |
rgrover1 | 239:693a1f145b5a | 11 | ServiceDiscovery *singleton = getSingleton(); |
rgrover1 | 239:693a1f145b5a | 12 | singleton->serviceDiscoveryStarted(connectionHandle); |
rgrover1 | 239:693a1f145b5a | 13 | |
rgrover1 | 239:693a1f145b5a | 14 | uint32_t rc; |
rgrover1 | 239:693a1f145b5a | 15 | if ((rc = sd_ble_gattc_primary_services_discover(connectionHandle, SRV_DISC_START_HANDLE, NULL)) != NRF_SUCCESS) { |
rgrover1 | 239:693a1f145b5a | 16 | singleton->terminate(); |
rgrover1 | 239:693a1f145b5a | 17 | switch (rc) { |
rgrover1 | 239:693a1f145b5a | 18 | case NRF_ERROR_INVALID_PARAM: |
rgrover1 | 239:693a1f145b5a | 19 | case BLE_ERROR_INVALID_CONN_HANDLE: |
rgrover1 | 239:693a1f145b5a | 20 | return BLE_ERROR_INVALID_PARAM; |
rgrover1 | 239:693a1f145b5a | 21 | case NRF_ERROR_BUSY: |
rgrover1 | 239:693a1f145b5a | 22 | return BLE_STACK_BUSY; |
rgrover1 | 239:693a1f145b5a | 23 | default: |
rgrover1 | 239:693a1f145b5a | 24 | case NRF_ERROR_INVALID_STATE: |
rgrover1 | 239:693a1f145b5a | 25 | return BLE_ERROR_INVALID_STATE; |
rgrover1 | 239:693a1f145b5a | 26 | } |
rgrover1 | 239:693a1f145b5a | 27 | } |
rgrover1 | 239:693a1f145b5a | 28 | |
rgrover1 | 239:693a1f145b5a | 29 | return BLE_ERROR_NONE; |
rgrover1 | 239:693a1f145b5a | 30 | } |
rgrover1 | 239:693a1f145b5a | 31 | |
rgrover1 | 239:693a1f145b5a | 32 | ble_error_t ServiceDiscovery::launchCharacteristicDiscovery(Gap::Handle_t connectionHandle, Gap::Handle_t startHandle, Gap::Handle_t endHandle) { |
rgrover1 | 239:693a1f145b5a | 33 | ServiceDiscovery *singleton = getSingleton(); |
rgrover1 | 239:693a1f145b5a | 34 | singleton->characteristicDiscoveryStarted(connectionHandle); |
rgrover1 | 239:693a1f145b5a | 35 | |
rgrover1 | 239:693a1f145b5a | 36 | ble_gattc_handle_range_t handleRange = { |
rgrover1 | 239:693a1f145b5a | 37 | .start_handle = startHandle, |
rgrover1 | 239:693a1f145b5a | 38 | .end_handle = endHandle |
rgrover1 | 239:693a1f145b5a | 39 | }; |
rgrover1 | 239:693a1f145b5a | 40 | uint32_t rc; |
rgrover1 | 239:693a1f145b5a | 41 | if ((rc = sd_ble_gattc_characteristics_discover(connectionHandle, &handleRange)) != NRF_SUCCESS) { |
rgrover1 | 239:693a1f145b5a | 42 | singleton->terminateCharacteristicDiscovery(); |
rgrover1 | 239:693a1f145b5a | 43 | switch (rc) { |
rgrover1 | 239:693a1f145b5a | 44 | case BLE_ERROR_INVALID_CONN_HANDLE: |
rgrover1 | 239:693a1f145b5a | 45 | case NRF_ERROR_INVALID_ADDR: |
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 | 239:693a1f145b5a | 58 | void |
rgrover1 | 240:75b69581d1dd | 59 | NordicServiceDiscovery::setupDiscoveredServices(const ble_gattc_evt_prim_srvc_disc_rsp_t *response) |
rgrover1 | 239:693a1f145b5a | 60 | { |
rgrover1 | 242:73fc02cc20b1 | 61 | serviceIndex = 0; |
rgrover1 | 242:73fc02cc20b1 | 62 | numServices = response->count; |
rgrover1 | 239:693a1f145b5a | 63 | |
rgrover1 | 239:693a1f145b5a | 64 | /* Account for the limitation on the number of discovered services we can handle at a time. */ |
rgrover1 | 242:73fc02cc20b1 | 65 | if (numServices > BLE_DB_DISCOVERY_MAX_SRV) { |
rgrover1 | 242:73fc02cc20b1 | 66 | numServices = BLE_DB_DISCOVERY_MAX_SRV; |
rgrover1 | 239:693a1f145b5a | 67 | } |
rgrover1 | 239:693a1f145b5a | 68 | |
rgrover1 | 242:73fc02cc20b1 | 69 | for (unsigned serviceIndex = 0; serviceIndex < numServices; serviceIndex++) { |
rgrover1 | 239:693a1f145b5a | 70 | services[serviceIndex].setup(response->services[serviceIndex].uuid.uuid, |
rgrover1 | 239:693a1f145b5a | 71 | response->services[serviceIndex].handle_range.start_handle, |
rgrover1 | 239:693a1f145b5a | 72 | response->services[serviceIndex].handle_range.end_handle); |
rgrover1 | 239:693a1f145b5a | 73 | } |
rgrover1 | 239:693a1f145b5a | 74 | } |
rgrover1 | 239:693a1f145b5a | 75 | |
rgrover1 | 239:693a1f145b5a | 76 | void |
rgrover1 | 240:75b69581d1dd | 77 | NordicServiceDiscovery::setupDiscoveredCharacteristics(const ble_gattc_evt_char_disc_rsp_t *response) |
rgrover1 | 239:693a1f145b5a | 78 | { |
rgrover1 | 242:73fc02cc20b1 | 79 | characteristicIndex = 0; |
rgrover1 | 242:73fc02cc20b1 | 80 | numCharacteristics = response->count; |
rgrover1 | 239:693a1f145b5a | 81 | |
rgrover1 | 239:693a1f145b5a | 82 | /* Account for the limitation on the number of discovered characteristics we can handle at a time. */ |
rgrover1 | 242:73fc02cc20b1 | 83 | if (numCharacteristics > BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV) { |
rgrover1 | 242:73fc02cc20b1 | 84 | numCharacteristics = BLE_DB_DISCOVERY_MAX_CHAR_PER_SRV; |
rgrover1 | 239:693a1f145b5a | 85 | } |
rgrover1 | 239:693a1f145b5a | 86 | |
rgrover1 | 242:73fc02cc20b1 | 87 | for (unsigned charIndex = 0; charIndex < numCharacteristics; charIndex++) { |
rgrover1 | 239:693a1f145b5a | 88 | characteristics[charIndex].setup(response->chars[charIndex].uuid.uuid, |
rgrover1 | 239:693a1f145b5a | 89 | *(const uint8_t *)(&response->chars[charIndex].char_props), |
rgrover1 | 239:693a1f145b5a | 90 | response->chars[charIndex].handle_decl, |
rgrover1 | 239:693a1f145b5a | 91 | response->chars[charIndex].handle_value); |
rgrover1 | 239:693a1f145b5a | 92 | } |
rgrover1 | 239:693a1f145b5a | 93 | } |