|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2006-2013 ARM Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "common/common.h" |
| 18 | +#include "nordic_common.h" |
| 19 | + |
| 20 | +#include "btle.h" |
| 21 | +#include "btle_clock.h" |
| 22 | + |
| 23 | +#include "ble_flash.h" |
| 24 | +#include "ble_conn_params.h" |
| 25 | + |
| 26 | +#include "btle_gap.h" |
| 27 | +//#include "btle_advertising.h" |
| 28 | +#include "custom/custom_helper.h" |
| 29 | + |
| 30 | +#include "ble/GapEvents.h" |
| 31 | +#include "nRF5xn.h" |
| 32 | + |
| 33 | +#ifdef S110 |
| 34 | + #define IS_LEGACY_DEVICE_MANAGER_ENABLED 1 |
| 35 | +#elif defined(S130) || defined(S132) |
| 36 | + #define IS_LEGACY_DEVICE_MANAGER_ENABLED 0 |
| 37 | +#endif |
| 38 | + |
| 39 | +extern "C" { |
| 40 | +#if (IS_LEGACY_DEVICE_MANAGER_ENABLED) |
| 41 | + #include "pstorage.h" |
| 42 | + #include "device_manager.h" |
| 43 | +#else |
| 44 | + #include "fstorage.h" |
| 45 | + #include "fds.h" |
| 46 | + #include "peer_manager.h" |
| 47 | + #include "ble_conn_state.h" |
| 48 | +#endif |
| 49 | + |
| 50 | +#include "softdevice_handler.h" |
| 51 | +#include "ble_stack_handler_types.h" |
| 52 | +} |
| 53 | + |
| 54 | +#include "ble_hci.h" |
| 55 | +#include "btle_discovery.h" |
| 56 | + |
| 57 | +#include "nRF5xGattClient.h" |
| 58 | +#include "nRF5xServiceDiscovery.h" |
| 59 | +#include "nRF5xCharacteristicDescriptorDiscoverer.h" |
| 60 | + |
| 61 | + |
| 62 | +bool isEventsSignaled = false; |
| 63 | + |
| 64 | +extern "C" void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name); |
| 65 | +void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name); |
| 66 | +extern "C" void SD_EVT_IRQHandler(void); // export the softdevice event handler for registration by nvic-set-vector. |
| 67 | + |
| 68 | + |
| 69 | +static void btle_handler(ble_evt_t *p_ble_evt); |
| 70 | + |
| 71 | +#if 0 |
| 72 | +#define CENTRAL_LINK_COUNT (YOTTA_CFG_NORDIC_BLE_CENTRAL_LINKS) /**<number of central links used by the application. When changing this number remember to adjust the RAM settings */ |
| 73 | + /** If value for YOTTA_CFG_NORDIC_BLE_PERIPHERAL_LINKS was used, ram settings are adjusted by the yotta target module. */ |
| 74 | +#define PERIPHERAL_LINK_COUNT (YOTTA_CFG_NORDIC_BLE_PERIPHERAL_LINKS) /**<number of peripheral links used by the application. When changing this number remember to adjust the RAM settings*/ |
| 75 | + /** If value for YOTTA_CFG_NORDIC_BLE_CENTRAL_LINKS was used, ram settings are adjusted by the yotta target module. */ |
| 76 | +#define GATTS_ATTR_TAB_SIZE (YOTTA_CFG_NORDIC_BLE_GATTS_ATTR_TAB_SIZE) /**< GATTS attribite table size. */ |
| 77 | + /** If value for YOTTA_CFG_NORDIC_BLE_GATTS_ATTR_TAB_SIZE was used, ram settings are adjusted by the yotta target module. */ |
| 78 | +#else |
| 79 | +#define CENTRAL_LINK_COUNT 3 /**<number of central links used by the application. When changing this number remember to adjust the RAM settings */ |
| 80 | + /** If value for YOTTA_CFG_NORDIC_BLE_PERIPHERAL_LINKS was used, ram settings are adjusted by the yotta target module. */ |
| 81 | +#define PERIPHERAL_LINK_COUNT 1 /**<number of peripheral links used by the application. When changing this number remember to adjust the RAM settings*/ |
| 82 | + /** If value for YOTTA_CFG_NORDIC_BLE_CENTRAL_LINKS was used, ram settings are adjusted by the yotta target module. */ |
| 83 | +#define GATTS_ATTR_TAB_SIZE 0x600 /**< GATTS attribite table size. */ |
| 84 | + /** If value for YOTTA_CFG_NORDIC_BLE_GATTS_ATTR_TAB_SIZE was used, ram settings are adjusted by the yotta target module. */ |
| 85 | + |
| 86 | +#endif |
| 87 | + |
| 88 | +static void sys_evt_dispatch(uint32_t sys_evt) |
| 89 | +{ |
| 90 | +#if (IS_LEGACY_DEVICE_MANAGER_ENABLED) |
| 91 | + pstorage_sys_event_handler(sys_evt); |
| 92 | +#else |
| 93 | + // Forward Softdevice events to the fstorage module |
| 94 | + fs_sys_event_handler(sys_evt); |
| 95 | +#endif |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * This function is called in interrupt context to handle BLE events; i.e. pull |
| 100 | + * system and user events out of the pending events-queue of the BLE stack. The |
| 101 | + * BLE stack signals the availability of events by the triggering the SWI2 |
| 102 | + * interrupt, which forwards the handling to this function. |
| 103 | + * |
| 104 | + * The event processing loop is implemented in intern_softdevice_events_execute(). |
| 105 | + * |
| 106 | + * This function will signal to the user code by calling signalEventsToProcess |
| 107 | + * that their is events to process and BLE::processEvents should be called. |
| 108 | + */ |
| 109 | +static uint32_t signalEvent() |
| 110 | +{ |
| 111 | + if(isEventsSignaled == false) { |
| 112 | + isEventsSignaled = true; |
| 113 | + nRF5xn::Instance(BLE::DEFAULT_INSTANCE).signalEventsToProcess(BLE::DEFAULT_INSTANCE); |
| 114 | + } |
| 115 | + return NRF_SUCCESS; |
| 116 | +} |
| 117 | + |
| 118 | + |
| 119 | +error_t btle_init(void) |
| 120 | +{ |
| 121 | + nrf_clock_lf_cfg_t clockConfiguration; |
| 122 | + |
| 123 | + // register softdevice handler vector |
| 124 | + NVIC_SetVector(SD_EVT_IRQn, (uint32_t) SD_EVT_IRQHandler); |
| 125 | + |
| 126 | + // Configure the LF clock according to values provided by btle_clock.h. |
| 127 | + // It is input from the chain of the yotta configuration system. |
| 128 | + clockConfiguration.source = LFCLK_CONF_SOURCE; |
| 129 | + clockConfiguration.xtal_accuracy = LFCLK_CONF_ACCURACY; |
| 130 | + clockConfiguration.rc_ctiv = LFCLK_CONF_RC_CTIV; |
| 131 | + clockConfiguration.rc_temp_ctiv = LFCLK_CONF_RC_TEMP_CTIV; |
| 132 | + |
| 133 | + SOFTDEVICE_HANDLER_INIT(&clockConfiguration, signalEvent); |
| 134 | + |
| 135 | + // Enable BLE stack |
| 136 | + /** |
| 137 | + * Using this call, the application can select whether to include the |
| 138 | + * Service Changed characteristic in the GATT Server. The default in all |
| 139 | + * previous releases has been to include the Service Changed characteristic, |
| 140 | + * but this affects how GATT clients behave. Specifically, it requires |
| 141 | + * clients to subscribe to this attribute and not to cache attribute handles |
| 142 | + * between connections unless the devices are bonded. If the application |
| 143 | + * does not need to change the structure of the GATT server attributes at |
| 144 | + * runtime this adds unnecessary complexity to the interaction with peer |
| 145 | + * clients. If the SoftDevice is enabled with the Service Changed |
| 146 | + * Characteristics turned off, then clients are allowed to cache attribute |
| 147 | + * handles making applications simpler on both sides. |
| 148 | + */ |
| 149 | + static const bool IS_SRVC_CHANGED_CHARACT_PRESENT = true; |
| 150 | + |
| 151 | + ble_enable_params_t ble_enable_params; |
| 152 | + uint32_t err_code = softdevice_enable_get_default_config(CENTRAL_LINK_COUNT, |
| 153 | + PERIPHERAL_LINK_COUNT, |
| 154 | + &ble_enable_params); |
| 155 | + |
| 156 | + ble_enable_params.gatts_enable_params.attr_tab_size = GATTS_ATTR_TAB_SIZE; |
| 157 | + ble_enable_params.gatts_enable_params.service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT; |
| 158 | + ble_enable_params.common_enable_params.vs_uuid_count = UUID_TABLE_MAX_ENTRIES; |
| 159 | + |
| 160 | + if(err_code != NRF_SUCCESS) { |
| 161 | + return ERROR_INVALID_PARAM; |
| 162 | + } |
| 163 | + |
| 164 | + if (softdevice_enable(&ble_enable_params) != NRF_SUCCESS) { |
| 165 | + return ERROR_INVALID_PARAM; |
| 166 | + } |
| 167 | + |
| 168 | + // Peer Manger must been initialised prior any other call to its API (this file and btle_security_pm.cpp) |
| 169 | + pm_init(); |
| 170 | + |
| 171 | +#if (NRF_SD_BLE_API_VERSION <= 2) |
| 172 | + ble_gap_addr_t addr; |
| 173 | + if (sd_ble_gap_address_get(&addr) != NRF_SUCCESS) { |
| 174 | + return ERROR_INVALID_PARAM; |
| 175 | + } |
| 176 | + if (sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &addr) != NRF_SUCCESS) { |
| 177 | + return ERROR_INVALID_PARAM; |
| 178 | + } |
| 179 | +#else |
| 180 | + ble_gap_privacy_params_t privacy_params = {0}; |
| 181 | + privacy_params.privacy_mode = BLE_GAP_PRIVACY_MODE_OFF; |
| 182 | + pm_privacy_set(&privacy_params); |
| 183 | +#endif |
| 184 | + |
| 185 | + ASSERT_STATUS( softdevice_ble_evt_handler_set(btle_handler)); |
| 186 | + ASSERT_STATUS( softdevice_sys_evt_handler_set(sys_evt_dispatch)); |
| 187 | + |
| 188 | + return btle_gap_init(); |
| 189 | +} |
| 190 | + |
| 191 | +static void btle_handler(ble_evt_t *p_ble_evt) |
| 192 | +{ |
| 193 | + /* Library service handlers */ |
| 194 | +#if SDK_CONN_PARAMS_MODULE_ENABLE |
| 195 | + ble_conn_params_on_ble_evt(p_ble_evt); |
| 196 | +#endif |
| 197 | + |
| 198 | +#if (IS_LEGACY_DEVICE_MANAGER_ENABLED) |
| 199 | + dm_ble_evt_handler(p_ble_evt); |
| 200 | +#else |
| 201 | + // Forward BLE events to the Connection State module. |
| 202 | + // This must be called before any event handler that uses this module. |
| 203 | + ble_conn_state_on_ble_evt(p_ble_evt); |
| 204 | + |
| 205 | + // Forward BLE events to the Peer Manager |
| 206 | + pm_on_ble_evt(p_ble_evt); |
| 207 | +#endif |
| 208 | + |
| 209 | +#if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110) |
| 210 | + bleGattcEventHandler(p_ble_evt); |
| 211 | +#endif |
| 212 | + |
| 213 | + nRF5xn &ble = nRF5xn::Instance(BLE::DEFAULT_INSTANCE); |
| 214 | + nRF5xGap &gap = (nRF5xGap &) ble.getGap(); |
| 215 | + nRF5xGattServer &gattServer = (nRF5xGattServer &) ble.getGattServer(); |
| 216 | + nRF5xSecurityManager &securityManager = (nRF5xSecurityManager &) ble.getSecurityManager(); |
| 217 | + |
| 218 | + /* Custom event handler */ |
| 219 | + switch (p_ble_evt->header.evt_id) { |
| 220 | + case BLE_GAP_EVT_CONNECTED: { |
| 221 | + Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle; |
| 222 | +#if defined(TARGET_MCU_NRF51_16K_S110) || defined(TARGET_MCU_NRF51_32K_S110) |
| 223 | + /* Only peripheral role is supported by S110 */ |
| 224 | + Gap::Role_t role = Gap::PERIPHERAL; |
| 225 | +#else |
| 226 | + Gap::Role_t role = static_cast<Gap::Role_t>(p_ble_evt->evt.gap_evt.params.connected.role); |
| 227 | +#endif |
| 228 | + gap.setConnectionHandle(handle); |
| 229 | + const Gap::ConnectionParams_t *params = reinterpret_cast<Gap::ConnectionParams_t *>(&(p_ble_evt->evt.gap_evt.params.connected.conn_params)); |
| 230 | + |
| 231 | + const ble_gap_addr_t *peer = &p_ble_evt->evt.gap_evt.params.connected.peer_addr; |
| 232 | +#if (NRF_SD_BLE_API_VERSION <= 2) |
| 233 | + const ble_gap_addr_t *own = &p_ble_evt->evt.gap_evt.params.connected.own_addr; |
| 234 | + |
| 235 | + gap.processConnectionEvent(handle, |
| 236 | + role, |
| 237 | + static_cast<BLEProtocol::AddressType_t>(peer->addr_type), peer->addr, |
| 238 | + static_cast<BLEProtocol::AddressType_t>(own->addr_type), own->addr, |
| 239 | + params); |
| 240 | +#else |
| 241 | + Gap::AddressType_t addr_type; |
| 242 | + Gap::Address_t own_address; |
| 243 | + gap.getAddress(&addr_type, own_address); |
| 244 | + |
| 245 | + gap.processConnectionEvent(handle, |
| 246 | + role, |
| 247 | + static_cast<BLEProtocol::AddressType_t>(peer->addr_type), peer->addr, |
| 248 | + addr_type, own_address, |
| 249 | + params); |
| 250 | +#endif |
| 251 | + break; |
| 252 | + } |
| 253 | + |
| 254 | + case BLE_GAP_EVT_DISCONNECTED: { |
| 255 | + Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle; |
| 256 | + // Since we are not in a connection and have not started advertising, |
| 257 | + // store bonds |
| 258 | + gap.setConnectionHandle (BLE_CONN_HANDLE_INVALID); |
| 259 | + |
| 260 | + Gap::DisconnectionReason_t reason; |
| 261 | + switch (p_ble_evt->evt.gap_evt.params.disconnected.reason) { |
| 262 | + case BLE_HCI_LOCAL_HOST_TERMINATED_CONNECTION: |
| 263 | + reason = Gap::LOCAL_HOST_TERMINATED_CONNECTION; |
| 264 | + break; |
| 265 | + case BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION: |
| 266 | + reason = Gap::REMOTE_USER_TERMINATED_CONNECTION; |
| 267 | + break; |
| 268 | + case BLE_HCI_CONN_INTERVAL_UNACCEPTABLE: |
| 269 | + reason = Gap::CONN_INTERVAL_UNACCEPTABLE; |
| 270 | + break; |
| 271 | + default: |
| 272 | + /* Please refer to the underlying transport library for an |
| 273 | + * interpretion of this reason's value. */ |
| 274 | + reason = static_cast<Gap::DisconnectionReason_t>(p_ble_evt->evt.gap_evt.params.disconnected.reason); |
| 275 | + break; |
| 276 | + } |
| 277 | + |
| 278 | +#if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110) |
| 279 | + // Close all pending discoveries for this connection |
| 280 | + nRF5xGattClient& gattClient = ble.getGattClient(); |
| 281 | + gattClient.characteristicDescriptorDiscoverer().terminate(handle, BLE_ERROR_INVALID_STATE); |
| 282 | + gattClient.discovery().terminate(handle); |
| 283 | +#endif |
| 284 | + |
| 285 | + gap.processDisconnectionEvent(handle, reason); |
| 286 | + break; |
| 287 | + } |
| 288 | + |
| 289 | + case BLE_GAP_EVT_PASSKEY_DISPLAY: |
| 290 | + securityManager.processPasskeyDisplayEvent(p_ble_evt->evt.gap_evt.conn_handle, p_ble_evt->evt.gap_evt.params.passkey_display.passkey); |
| 291 | + break; |
| 292 | + |
| 293 | + case BLE_GAP_EVT_TIMEOUT: |
| 294 | + gap.processTimeoutEvent(static_cast<Gap::TimeoutSource_t>(p_ble_evt->evt.gap_evt.params.timeout.src)); |
| 295 | + break; |
| 296 | + |
| 297 | + case BLE_GATTC_EVT_TIMEOUT: |
| 298 | + case BLE_GATTS_EVT_TIMEOUT: |
| 299 | + // Disconnect on GATT Server and Client timeout events. |
| 300 | + // ASSERT_STATUS_RET_VOID (sd_ble_gap_disconnect(m_conn_handle, |
| 301 | + // BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION)); |
| 302 | + break; |
| 303 | + |
| 304 | + case BLE_GAP_EVT_ADV_REPORT: { |
| 305 | + const ble_gap_evt_adv_report_t *advReport = &p_ble_evt->evt.gap_evt.params.adv_report; |
| 306 | + gap.processAdvertisementReport(advReport->peer_addr.addr, |
| 307 | + advReport->rssi, |
| 308 | + advReport->scan_rsp, |
| 309 | + static_cast<GapAdvertisingParams::AdvertisingType_t>(advReport->type), |
| 310 | + advReport->dlen, |
| 311 | + advReport->data); |
| 312 | + break; |
| 313 | + } |
| 314 | + |
| 315 | + default: |
| 316 | + break; |
| 317 | + } |
| 318 | + |
| 319 | + gattServer.hwCallback(p_ble_evt); |
| 320 | +} |
| 321 | + |
| 322 | +/*! @brief Callback when an error occurs inside the SoftDevice */ |
| 323 | +void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name) |
| 324 | +{ |
| 325 | + ASSERT_TRUE(false, (void) 0); |
| 326 | +} |
| 327 | + |
| 328 | +/*! |
| 329 | + @brief Handler for general errors above the SoftDevice layer. |
| 330 | + Typically we can' recover from this so we do a reset. |
| 331 | +*/ |
| 332 | +void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name) |
| 333 | +{ |
| 334 | + ASSERT_STATUS_RET_VOID( error_code ); |
| 335 | + NVIC_SystemReset(); |
| 336 | +} |
0 commit comments