fdsf
Fork of nRF51822 by
source/btle/btle.cpp@424:28ea27dcba79, 2015-08-13 (annotated)
- Committer:
- rgrover1
- Date:
- Thu Aug 13 13:23:18 2015 +0100
- Revision:
- 424:28ea27dcba79
- Parent:
- 423:9bf22621d592
- Child:
- 426:3edd435c8364
Synchronized with git rev c9a77b82
Author: Rohit Grover
Release 0.4.6
=============
Enhancements
~~~~~~~~~~~~
* Add connection handle to GATT callback parameters. This paves the way for
applications requiring multiple concurrent connections: read/write/HVX
callbacks will be able to distinguish between peripherals by comparing per-
device connection handles.
* nRFGattClient: move the allocation of the singleton to within the
getInstance() method. This saves memory when nRFGattClient isn't
instantiated.
* Disable GattClient features when using S110 SoftDevice. This is controlled
by the pre-processor macros: MCU_NORDIC_16K_S110 or MCU_NORDIC_16K_S110.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rohit Grover |
22:c6ee8136847e | 1 | /* mbed Microcontroller Library |
Rohit Grover |
22:c6ee8136847e | 2 | * Copyright (c) 2006-2013 ARM Limited |
Rohit Grover |
22:c6ee8136847e | 3 | * |
Rohit Grover |
22:c6ee8136847e | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Rohit Grover |
22:c6ee8136847e | 5 | * you may not use this file except in compliance with the License. |
Rohit Grover |
22:c6ee8136847e | 6 | * You may obtain a copy of the License at |
Rohit Grover |
22:c6ee8136847e | 7 | * |
Rohit Grover |
22:c6ee8136847e | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Rohit Grover |
22:c6ee8136847e | 9 | * |
Rohit Grover |
22:c6ee8136847e | 10 | * Unless required by applicable law or agreed to in writing, software |
Rohit Grover |
22:c6ee8136847e | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Rohit Grover |
22:c6ee8136847e | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Rohit Grover |
22:c6ee8136847e | 13 | * See the License for the specific language governing permissions and |
Rohit Grover |
22:c6ee8136847e | 14 | * limitations under the License. |
Rohit Grover |
22:c6ee8136847e | 15 | */ |
Rohit Grover |
52:120bd37b9d0d | 16 | |
Rohit Grover |
22:c6ee8136847e | 17 | #include "common/common.h" |
Rohit Grover |
37:c29c330d942c | 18 | #include "nordic_common.h" |
Rohit Grover |
22:c6ee8136847e | 19 | |
Rohit Grover |
22:c6ee8136847e | 20 | #include "btle.h" |
Rohit Grover |
22:c6ee8136847e | 21 | |
Rohit Grover |
22:c6ee8136847e | 22 | #include "ble_stack_handler_types.h" |
Rohit Grover |
22:c6ee8136847e | 23 | #include "ble_flash.h" |
Rohit Grover |
22:c6ee8136847e | 24 | #include "ble_conn_params.h" |
Rohit Grover |
22:c6ee8136847e | 25 | |
Rohit Grover |
22:c6ee8136847e | 26 | #include "btle_gap.h" |
Rohit Grover |
22:c6ee8136847e | 27 | #include "btle_advertising.h" |
Rohit Grover |
22:c6ee8136847e | 28 | #include "custom/custom_helper.h" |
Rohit Grover |
22:c6ee8136847e | 29 | |
Rohit Grover |
22:c6ee8136847e | 30 | #include "softdevice_handler.h" |
Rohit Grover |
22:c6ee8136847e | 31 | #include "pstorage.h" |
Rohit Grover |
22:c6ee8136847e | 32 | |
rgrover1 | 371:8f7d2137727a | 33 | #include "ble/GapEvents.h" |
rgrover1 | 393:0f7c5048efb3 | 34 | #include "nRF5xGap.h" |
rgrover1 | 393:0f7c5048efb3 | 35 | #include "nRF5xGattServer.h" |
rgrover1 | 393:0f7c5048efb3 | 36 | #include "nRF5xSecurityManager.h" |
rgrover1 | 371:8f7d2137727a | 37 | |
rgrover1 | 124:664d4257a9f8 | 38 | #include "device_manager.h" |
Rohit Grover |
22:c6ee8136847e | 39 | |
Rohit Grover |
56:a1071b629aa3 | 40 | #include "ble_hci.h" |
rgrover1 | 371:8f7d2137727a | 41 | #include "btle_discovery.h" |
Rohit Grover |
56:a1071b629aa3 | 42 | |
Rohit Grover |
56:a1071b629aa3 | 43 | extern "C" void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name); |
Rohit Grover |
56:a1071b629aa3 | 44 | void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name); |
Rohit Grover |
22:c6ee8136847e | 45 | |
Rohit Grover |
22:c6ee8136847e | 46 | static void btle_handler(ble_evt_t *p_ble_evt); |
Rohit Grover |
22:c6ee8136847e | 47 | |
Rohit Grover |
22:c6ee8136847e | 48 | static void sys_evt_dispatch(uint32_t sys_evt) |
Rohit Grover |
22:c6ee8136847e | 49 | { |
Rohit Grover |
22:c6ee8136847e | 50 | pstorage_sys_event_handler(sys_evt); |
Rohit Grover |
22:c6ee8136847e | 51 | } |
Rohit Grover |
22:c6ee8136847e | 52 | |
Rohit Grover |
22:c6ee8136847e | 53 | error_t btle_init(void) |
Rohit Grover |
22:c6ee8136847e | 54 | { |
rgrover1 | 424:28ea27dcba79 | 55 | nrf_clock_lfclksrc_t clockSource; |
rgrover1 | 404:fccbe137c4ac | 56 | if (NRF_CLOCK->LFCLKSRC & (CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos)) { |
rgrover1 | 424:28ea27dcba79 | 57 | clockSource = NRF_CLOCK_LFCLKSRC_XTAL_20_PPM; |
rgrover1 | 404:fccbe137c4ac | 58 | } else { |
rgrover1 | 424:28ea27dcba79 | 59 | clockSource = NRF_CLOCK_LFCLKSRC_RC_250_PPM_4000MS_CALIBRATION; |
rgrover1 | 404:fccbe137c4ac | 60 | } |
rgrover1 | 424:28ea27dcba79 | 61 | SOFTDEVICE_HANDLER_INIT(clockSource, NULL); |
Rohit Grover |
56:a1071b629aa3 | 62 | |
Rohit Grover |
56:a1071b629aa3 | 63 | // Enable BLE stack |
Rohit Grover |
56:a1071b629aa3 | 64 | /** |
Rohit Grover |
56:a1071b629aa3 | 65 | * Using this call, the application can select whether to include the |
Rohit Grover |
56:a1071b629aa3 | 66 | * Service Changed characteristic in the GATT Server. The default in all |
Rohit Grover |
56:a1071b629aa3 | 67 | * previous releases has been to include the Service Changed characteristic, |
Rohit Grover |
56:a1071b629aa3 | 68 | * but this affects how GATT clients behave. Specifically, it requires |
Rohit Grover |
56:a1071b629aa3 | 69 | * clients to subscribe to this attribute and not to cache attribute handles |
Rohit Grover |
56:a1071b629aa3 | 70 | * between connections unless the devices are bonded. If the application |
Rohit Grover |
56:a1071b629aa3 | 71 | * does not need to change the structure of the GATT server attributes at |
Rohit Grover |
56:a1071b629aa3 | 72 | * runtime this adds unnecessary complexity to the interaction with peer |
Rohit Grover |
56:a1071b629aa3 | 73 | * clients. If the SoftDevice is enabled with the Service Changed |
Rohit Grover |
56:a1071b629aa3 | 74 | * Characteristics turned off, then clients are allowed to cache attribute |
Rohit Grover |
56:a1071b629aa3 | 75 | * handles making applications simpler on both sides. |
Rohit Grover |
56:a1071b629aa3 | 76 | */ |
Rohit Grover |
68:936d81c963fe | 77 | static const bool IS_SRVC_CHANGED_CHARACT_PRESENT = true; |
Rohit Grover |
56:a1071b629aa3 | 78 | ble_enable_params_t enableParams = { |
Rohit Grover |
56:a1071b629aa3 | 79 | .gatts_enable_params = { |
Rohit Grover |
56:a1071b629aa3 | 80 | .service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT |
Rohit Grover |
56:a1071b629aa3 | 81 | } |
Rohit Grover |
56:a1071b629aa3 | 82 | }; |
Rohit Grover |
56:a1071b629aa3 | 83 | if (sd_ble_enable(&enableParams) != NRF_SUCCESS) { |
Rohit Grover |
56:a1071b629aa3 | 84 | return ERROR_INVALID_PARAM; |
Rohit Grover |
56:a1071b629aa3 | 85 | } |
Rohit Grover |
56:a1071b629aa3 | 86 | |
Rohit Grover |
56:a1071b629aa3 | 87 | ble_gap_addr_t addr; |
Rohit Grover |
56:a1071b629aa3 | 88 | if (sd_ble_gap_address_get(&addr) != NRF_SUCCESS) { |
Rohit Grover |
56:a1071b629aa3 | 89 | return ERROR_INVALID_PARAM; |
Rohit Grover |
56:a1071b629aa3 | 90 | } |
Rohit Grover |
56:a1071b629aa3 | 91 | if (sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &addr) != NRF_SUCCESS) { |
Rohit Grover |
56:a1071b629aa3 | 92 | return ERROR_INVALID_PARAM; |
Rohit Grover |
56:a1071b629aa3 | 93 | } |
Rohit Grover |
22:c6ee8136847e | 94 | |
Rohit Grover |
22:c6ee8136847e | 95 | ASSERT_STATUS( softdevice_ble_evt_handler_set(btle_handler)); |
Rohit Grover |
22:c6ee8136847e | 96 | ASSERT_STATUS( softdevice_sys_evt_handler_set(sys_evt_dispatch)); |
Rohit Grover |
22:c6ee8136847e | 97 | |
rgrover1 | 127:bd1e1fe607e0 | 98 | btle_gap_init(); |
rgrover1 | 127:bd1e1fe607e0 | 99 | |
rgrover1 | 127:bd1e1fe607e0 | 100 | return ERROR_NONE; |
rgrover1 | 127:bd1e1fe607e0 | 101 | } |
rgrover1 | 127:bd1e1fe607e0 | 102 | |
Rohit Grover |
22:c6ee8136847e | 103 | static void btle_handler(ble_evt_t *p_ble_evt) |
Rohit Grover |
22:c6ee8136847e | 104 | { |
Rohit Grover |
22:c6ee8136847e | 105 | /* Library service handlers */ |
Rohit Grover |
56:a1071b629aa3 | 106 | #if SDK_CONN_PARAMS_MODULE_ENABLE |
Rohit Grover |
22:c6ee8136847e | 107 | ble_conn_params_on_ble_evt(p_ble_evt); |
Rohit Grover |
56:a1071b629aa3 | 108 | #endif |
Rohit Grover |
22:c6ee8136847e | 109 | |
rgrover1 | 125:35e4f65364bc | 110 | dm_ble_evt_handler(p_ble_evt); |
rgrover1 | 125:35e4f65364bc | 111 | |
rgrover1 | 424:28ea27dcba79 | 112 | #if !defined(MCU_NORDIC_16K_S110) && !defined(MCU_NORDIC_32K_S110) |
rgrover1 | 371:8f7d2137727a | 113 | bleGattcEventHandler(p_ble_evt); |
rgrover1 | 424:28ea27dcba79 | 114 | #endif |
rgrover1 | 371:8f7d2137727a | 115 | |
Rohit Grover |
22:c6ee8136847e | 116 | /* Custom event handler */ |
Rohit Grover |
22:c6ee8136847e | 117 | switch (p_ble_evt->header.evt_id) { |
Rohit Grover |
41:6e66cd970659 | 118 | case BLE_GAP_EVT_CONNECTED: { |
Rohit Grover |
41:6e66cd970659 | 119 | Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle; |
rgrover1 | 399:a471c3da4806 | 120 | #if defined(MCU_NORDIC_16K_S110) || defined(MCU_NORDIC_32K_S110) |
rgrover1 | 399:a471c3da4806 | 121 | /* Only peripheral role is supported by S110 */ |
rgrover1 | 399:a471c3da4806 | 122 | Gap::Role_t role = Gap::PERIPHERAL; |
rgrover1 | 399:a471c3da4806 | 123 | #else |
rgrover1 | 399:a471c3da4806 | 124 | Gap::Role_t role = static_cast<Gap::Role_t>(p_ble_evt->evt.gap_evt.params.connected.role); |
rgrover1 | 399:a471c3da4806 | 125 | #endif |
rgrover1 | 393:0f7c5048efb3 | 126 | nRF5xGap::getInstance().setConnectionHandle(handle); |
Rohit Grover |
56:a1071b629aa3 | 127 | const Gap::ConnectionParams_t *params = reinterpret_cast<Gap::ConnectionParams_t *>(&(p_ble_evt->evt.gap_evt.params.connected.conn_params)); |
rgrover1 | 77:9886b2865631 | 128 | const ble_gap_addr_t *peer = &p_ble_evt->evt.gap_evt.params.connected.peer_addr; |
rgrover1 | 112:737b08b3b995 | 129 | const ble_gap_addr_t *own = &p_ble_evt->evt.gap_evt.params.connected.own_addr; |
rgrover1 | 393:0f7c5048efb3 | 130 | nRF5xGap::getInstance().processConnectionEvent(handle, |
rgrover1 | 399:a471c3da4806 | 131 | role, |
rgrover1 | 179:10e4c13360d9 | 132 | static_cast<Gap::AddressType_t>(peer->addr_type), peer->addr, |
rgrover1 | 179:10e4c13360d9 | 133 | static_cast<Gap::AddressType_t>(own->addr_type), own->addr, |
rgrover1 | 112:737b08b3b995 | 134 | params); |
Rohit Grover |
22:c6ee8136847e | 135 | break; |
Rohit Grover |
41:6e66cd970659 | 136 | } |
Rohit Grover |
22:c6ee8136847e | 137 | |
Rohit Grover |
41:6e66cd970659 | 138 | case BLE_GAP_EVT_DISCONNECTED: { |
Rohit Grover |
41:6e66cd970659 | 139 | Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle; |
Rohit Grover |
22:c6ee8136847e | 140 | // Since we are not in a connection and have not started advertising, |
Rohit Grover |
22:c6ee8136847e | 141 | // store bonds |
rgrover1 | 393:0f7c5048efb3 | 142 | nRF5xGap::getInstance().setConnectionHandle (BLE_CONN_HANDLE_INVALID); |
Rohit Grover |
56:a1071b629aa3 | 143 | |
Rohit Grover |
65:98215c4f3a25 | 144 | Gap::DisconnectionReason_t reason; |
Rohit Grover |
65:98215c4f3a25 | 145 | switch (p_ble_evt->evt.gap_evt.params.disconnected.reason) { |
Rohit Grover |
65:98215c4f3a25 | 146 | case BLE_HCI_LOCAL_HOST_TERMINATED_CONNECTION: |
Rohit Grover |
65:98215c4f3a25 | 147 | reason = Gap::LOCAL_HOST_TERMINATED_CONNECTION; |
Rohit Grover |
65:98215c4f3a25 | 148 | break; |
Rohit Grover |
65:98215c4f3a25 | 149 | case BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION: |
Rohit Grover |
65:98215c4f3a25 | 150 | reason = Gap::REMOTE_USER_TERMINATED_CONNECTION; |
Rohit Grover |
65:98215c4f3a25 | 151 | break; |
Rohit Grover |
65:98215c4f3a25 | 152 | case BLE_HCI_CONN_INTERVAL_UNACCEPTABLE: |
Rohit Grover |
65:98215c4f3a25 | 153 | reason = Gap::CONN_INTERVAL_UNACCEPTABLE; |
Rohit Grover |
65:98215c4f3a25 | 154 | break; |
Rohit Grover |
65:98215c4f3a25 | 155 | default: |
Rohit Grover |
65:98215c4f3a25 | 156 | /* Please refer to the underlying transport library for an |
Rohit Grover |
65:98215c4f3a25 | 157 | * interpretion of this reason's value. */ |
Rohit Grover |
65:98215c4f3a25 | 158 | reason = static_cast<Gap::DisconnectionReason_t>(p_ble_evt->evt.gap_evt.params.disconnected.reason); |
Rohit Grover |
65:98215c4f3a25 | 159 | break; |
Rohit Grover |
56:a1071b629aa3 | 160 | } |
rgrover1 | 393:0f7c5048efb3 | 161 | nRF5xGap::getInstance().processDisconnectionEvent(handle, reason); |
Rohit Grover |
22:c6ee8136847e | 162 | break; |
Rohit Grover |
41:6e66cd970659 | 163 | } |
Rohit Grover |
22:c6ee8136847e | 164 | |
rgrover1 | 161:6712855a0107 | 165 | case BLE_GAP_EVT_PASSKEY_DISPLAY: |
rgrover1 | 393:0f7c5048efb3 | 166 | nRF5xSecurityManager::getInstance().processPasskeyDisplayEvent(p_ble_evt->evt.gap_evt.conn_handle, p_ble_evt->evt.gap_evt.params.passkey_display.passkey); |
rgrover1 | 161:6712855a0107 | 167 | break; |
rgrover1 | 161:6712855a0107 | 168 | |
Rohit Grover |
22:c6ee8136847e | 169 | case BLE_GAP_EVT_TIMEOUT: |
rgrover1 | 393:0f7c5048efb3 | 170 | nRF5xGap::getInstance().processTimeoutEvent(static_cast<Gap::TimeoutSource_t>(p_ble_evt->evt.gap_evt.params.timeout.src)); |
Rohit Grover |
22:c6ee8136847e | 171 | break; |
Rohit Grover |
22:c6ee8136847e | 172 | |
Rohit Grover |
22:c6ee8136847e | 173 | case BLE_GATTC_EVT_TIMEOUT: |
Rohit Grover |
22:c6ee8136847e | 174 | case BLE_GATTS_EVT_TIMEOUT: |
Rohit Grover |
22:c6ee8136847e | 175 | // Disconnect on GATT Server and Client timeout events. |
Rohit Grover |
22:c6ee8136847e | 176 | // ASSERT_STATUS_RET_VOID (sd_ble_gap_disconnect(m_conn_handle, |
Rohit Grover |
22:c6ee8136847e | 177 | // BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION)); |
Rohit Grover |
22:c6ee8136847e | 178 | break; |
Rohit Grover |
22:c6ee8136847e | 179 | |
rgrover1 | 165:67fcad70785a | 180 | case BLE_GAP_EVT_ADV_REPORT: { |
rgrover1 | 165:67fcad70785a | 181 | const ble_gap_evt_adv_report_t *advReport = &p_ble_evt->evt.gap_evt.params.adv_report; |
rgrover1 | 393:0f7c5048efb3 | 182 | nRF5xGap::getInstance().processAdvertisementReport(advReport->peer_addr.addr, |
rgrover1 | 167:66ee4f876396 | 183 | advReport->rssi, |
rgrover1 | 167:66ee4f876396 | 184 | advReport->scan_rsp, |
rgrover1 | 371:8f7d2137727a | 185 | static_cast<GapAdvertisingParams::AdvertisingType_t>(advReport->type), |
rgrover1 | 167:66ee4f876396 | 186 | advReport->dlen, |
rgrover1 | 167:66ee4f876396 | 187 | advReport->data); |
rgrover1 | 167:66ee4f876396 | 188 | break; |
rgrover1 | 165:67fcad70785a | 189 | } |
rgrover1 | 165:67fcad70785a | 190 | |
Rohit Grover |
22:c6ee8136847e | 191 | default: |
Rohit Grover |
22:c6ee8136847e | 192 | break; |
Rohit Grover |
22:c6ee8136847e | 193 | } |
Rohit Grover |
22:c6ee8136847e | 194 | |
rgrover1 | 393:0f7c5048efb3 | 195 | nRF5xGattServer::getInstance().hwCallback(p_ble_evt); |
Rohit Grover |
22:c6ee8136847e | 196 | } |
Rohit Grover |
22:c6ee8136847e | 197 | |
rgrover1 | 160:7d04579fdb2a | 198 | /*! @brief Callback when an error occurs inside the SoftDevice */ |
Rohit Grover |
22:c6ee8136847e | 199 | void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name) |
Rohit Grover |
22:c6ee8136847e | 200 | { |
Rohit Grover |
22:c6ee8136847e | 201 | ASSERT(false, (void) 0); |
Rohit Grover |
22:c6ee8136847e | 202 | } |
Rohit Grover |
22:c6ee8136847e | 203 | |
Rohit Grover |
22:c6ee8136847e | 204 | /*! |
Rohit Grover |
22:c6ee8136847e | 205 | @brief Handler for general errors above the SoftDevice layer. |
Rohit Grover |
22:c6ee8136847e | 206 | Typically we can' recover from this so we do a reset. |
Rohit Grover |
22:c6ee8136847e | 207 | */ |
rgrover1 | 160:7d04579fdb2a | 208 | void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name) |
Rohit Grover |
22:c6ee8136847e | 209 | { |
Rohit Grover |
22:c6ee8136847e | 210 | ASSERT_STATUS_RET_VOID( error_code ); |
Rohit Grover |
22:c6ee8136847e | 211 | NVIC_SystemReset(); |
rgrover1 | 77:9886b2865631 | 212 | } |