|
| 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 | +extern "C" { |
| 34 | +#include "pstorage.h" |
| 35 | +#include "device_manager.h" |
| 36 | +#include "softdevice_handler.h" |
| 37 | +#include "ble_stack_handler_types.h" |
| 38 | +} |
| 39 | + |
| 40 | +#include "nrf_ble_hci.h" |
| 41 | +#include "btle_discovery.h" |
| 42 | + |
| 43 | +#include "nRF5xGattClient.h" |
| 44 | +#include "nRF5xServiceDiscovery.h" |
| 45 | +#include "nRF5xCharacteristicDescriptorDiscoverer.h" |
| 46 | + |
| 47 | +bool isEventsSignaled = false; |
| 48 | + |
| 49 | +extern "C" void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name); |
| 50 | +void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name); |
| 51 | + |
| 52 | +static void btle_handler(ble_evt_t *p_ble_evt); |
| 53 | + |
| 54 | +#if 0 |
| 55 | +#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 */ |
| 56 | + /** If value for YOTTA_CFG_NORDIC_BLE_PERIPHERAL_LINKS was used, ram settings are adjusted by the yotta target module. */ |
| 57 | +#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*/ |
| 58 | + /** If value for YOTTA_CFG_NORDIC_BLE_CENTRAL_LINKS was used, ram settings are adjusted by the yotta target module. */ |
| 59 | +#define GATTS_ATTR_TAB_SIZE (YOTTA_CFG_NORDIC_BLE_GATTS_ATTR_TAB_SIZE) /**< GATTS attribite table size. */ |
| 60 | + /** If value for YOTTA_CFG_NORDIC_BLE_GATTS_ATTR_TAB_SIZE was used, ram settings are adjusted by the yotta target module. */ |
| 61 | +#else |
| 62 | +#define CENTRAL_LINK_COUNT 3 /**<number of central links used by the application. When changing this number remember to adjust the RAM settings */ |
| 63 | + /** If value for YOTTA_CFG_NORDIC_BLE_PERIPHERAL_LINKS was used, ram settings are adjusted by the yotta target module. */ |
| 64 | +#define PERIPHERAL_LINK_COUNT 1 /**<number of peripheral links used by the application. When changing this number remember to adjust the RAM settings*/ |
| 65 | + /** If value for YOTTA_CFG_NORDIC_BLE_CENTRAL_LINKS was used, ram settings are adjusted by the yotta target module. */ |
| 66 | +#define GATTS_ATTR_TAB_SIZE 0x600 /**< GATTS attribite table size. */ |
| 67 | + /** If value for YOTTA_CFG_NORDIC_BLE_GATTS_ATTR_TAB_SIZE was used, ram settings are adjusted by the yotta target module. */ |
| 68 | + |
| 69 | +#endif |
| 70 | + |
| 71 | +static void sys_evt_dispatch(uint32_t sys_evt) |
| 72 | +{ |
| 73 | + pstorage_sys_event_handler(sys_evt); |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * This function is called in interrupt context to handle BLE events; i.e. pull |
| 78 | + * system and user events out of the pending events-queue of the BLE stack. The |
| 79 | + * BLE stack signals the availability of events by the triggering the SWI2 |
| 80 | + * interrupt, which forwards the handling to this function. |
| 81 | + * |
| 82 | + * The event processing loop is implemented in intern_softdevice_events_execute(). |
| 83 | + * |
| 84 | + * This function will signal to the user code by calling signalEventsToProcess |
| 85 | + * that their is events to process and BLE::processEvents should be called. |
| 86 | + */ |
| 87 | +static uint32_t signalEvent() |
| 88 | +{ |
| 89 | + if(isEventsSignaled == false) { |
| 90 | + isEventsSignaled = true; |
| 91 | + nRF5xn::Instance(BLE::DEFAULT_INSTANCE).signalEventsToProcess(BLE::DEFAULT_INSTANCE); |
| 92 | + } |
| 93 | + return NRF_SUCCESS; |
| 94 | +} |
| 95 | + |
| 96 | +error_t btle_init(void) |
| 97 | +{ |
| 98 | + nrf_clock_lf_cfg_t clockConfiguration; |
| 99 | + |
| 100 | + // Configure the LF clock according to values provided by btle_clock.h. |
| 101 | + // It is input from the chain of the yotta configuration system. |
| 102 | + clockConfiguration.source = LFCLK_CONF_SOURCE; |
| 103 | + clockConfiguration.xtal_accuracy = LFCLK_CONF_ACCURACY; |
| 104 | + clockConfiguration.rc_ctiv = LFCLK_CONF_RC_CTIV; |
| 105 | + clockConfiguration.rc_temp_ctiv = LFCLK_CONF_RC_TEMP_CTIV; |
| 106 | + |
| 107 | + SOFTDEVICE_HANDLER_INIT(&clockConfiguration, signalEvent); |
| 108 | + |
| 109 | + // Enable BLE stack |
| 110 | + /** |
| 111 | + * Using this call, the application can select whether to include the |
| 112 | + * Service Changed characteristic in the GATT Server. The default in all |
| 113 | + * previous releases has been to include the Service Changed characteristic, |
| 114 | + * but this affects how GATT clients behave. Specifically, it requires |
| 115 | + * clients to subscribe to this attribute and not to cache attribute handles |
| 116 | + * between connections unless the devices are bonded. If the application |
| 117 | + * does not need to change the structure of the GATT server attributes at |
| 118 | + * runtime this adds unnecessary complexity to the interaction with peer |
| 119 | + * clients. If the SoftDevice is enabled with the Service Changed |
| 120 | + * Characteristics turned off, then clients are allowed to cache attribute |
| 121 | + * handles making applications simpler on both sides. |
| 122 | + */ |
| 123 | + static const bool IS_SRVC_CHANGED_CHARACT_PRESENT = true; |
| 124 | + |
| 125 | + ble_enable_params_t ble_enable_params; |
| 126 | + uint32_t err_code = softdevice_enable_get_default_config(CENTRAL_LINK_COUNT, |
| 127 | + PERIPHERAL_LINK_COUNT, |
| 128 | + &ble_enable_params); |
| 129 | + |
| 130 | + ble_enable_params.gatts_enable_params.attr_tab_size = GATTS_ATTR_TAB_SIZE; |
| 131 | + ble_enable_params.gatts_enable_params.service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT; |
| 132 | + ble_enable_params.common_enable_params.vs_uuid_count = UUID_TABLE_MAX_ENTRIES; |
| 133 | + |
| 134 | + if(err_code != NRF_SUCCESS) { |
| 135 | + return ERROR_INVALID_PARAM; |
| 136 | + } |
| 137 | + |
| 138 | + if (softdevice_enable(&ble_enable_params) != NRF_SUCCESS) { |
| 139 | + return ERROR_INVALID_PARAM; |
| 140 | + } |
| 141 | + |
| 142 | + ble_gap_addr_t addr; |
| 143 | + if (sd_ble_gap_address_get(&addr) != NRF_SUCCESS) { |
| 144 | + return ERROR_INVALID_PARAM; |
| 145 | + } |
| 146 | + if (sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &addr) != NRF_SUCCESS) { |
| 147 | + return ERROR_INVALID_PARAM; |
| 148 | + } |
| 149 | + |
| 150 | + ASSERT_STATUS( softdevice_ble_evt_handler_set(btle_handler)); |
| 151 | + ASSERT_STATUS( softdevice_sys_evt_handler_set(sys_evt_dispatch)); |
| 152 | + |
| 153 | + return btle_gap_init(); |
| 154 | +} |
| 155 | + |
| 156 | +static void btle_handler(ble_evt_t *p_ble_evt) |
| 157 | +{ |
| 158 | + /* Library service handlers */ |
| 159 | +#if SDK_CONN_PARAMS_MODULE_ENABLE |
| 160 | + ble_conn_params_on_ble_evt(p_ble_evt); |
| 161 | +#endif |
| 162 | + |
| 163 | + dm_ble_evt_handler(p_ble_evt); |
| 164 | + |
| 165 | +#if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110) |
| 166 | + bleGattcEventHandler(p_ble_evt); |
| 167 | +#endif |
| 168 | + |
| 169 | + nRF5xn &ble = nRF5xn::Instance(BLE::DEFAULT_INSTANCE); |
| 170 | + nRF5xGap &gap = (nRF5xGap &) ble.getGap(); |
| 171 | + nRF5xGattServer &gattServer = (nRF5xGattServer &) ble.getGattServer(); |
| 172 | + nRF5xSecurityManager &securityManager = (nRF5xSecurityManager &) ble.getSecurityManager(); |
| 173 | + |
| 174 | + /* Custom event handler */ |
| 175 | + switch (p_ble_evt->header.evt_id) { |
| 176 | + case BLE_GAP_EVT_CONNECTED: { |
| 177 | + Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle; |
| 178 | +#if defined(TARGET_MCU_NRF51_16K_S110) || defined(TARGET_MCU_NRF51_32K_S110) |
| 179 | + /* Only peripheral role is supported by S110 */ |
| 180 | + Gap::Role_t role = Gap::PERIPHERAL; |
| 181 | +#else |
| 182 | + Gap::Role_t role = static_cast<Gap::Role_t>(p_ble_evt->evt.gap_evt.params.connected.role); |
| 183 | +#endif |
| 184 | + gap.setConnectionHandle(handle); |
| 185 | + const Gap::ConnectionParams_t *params = reinterpret_cast<Gap::ConnectionParams_t *>(&(p_ble_evt->evt.gap_evt.params.connected.conn_params)); |
| 186 | + const ble_gap_addr_t *peer = &p_ble_evt->evt.gap_evt.params.connected.peer_addr; |
| 187 | + const ble_gap_addr_t *own = &p_ble_evt->evt.gap_evt.params.connected.own_addr; |
| 188 | + gap.processConnectionEvent(handle, |
| 189 | + role, |
| 190 | + static_cast<BLEProtocol::AddressType_t>(peer->addr_type), peer->addr, |
| 191 | + static_cast<BLEProtocol::AddressType_t>(own->addr_type), own->addr, |
| 192 | + params); |
| 193 | + break; |
| 194 | + } |
| 195 | + |
| 196 | + case BLE_GAP_EVT_DISCONNECTED: { |
| 197 | + Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle; |
| 198 | + // Since we are not in a connection and have not started advertising, |
| 199 | + // store bonds |
| 200 | + gap.setConnectionHandle (BLE_CONN_HANDLE_INVALID); |
| 201 | + |
| 202 | + Gap::DisconnectionReason_t reason; |
| 203 | + switch (p_ble_evt->evt.gap_evt.params.disconnected.reason) { |
| 204 | + case BLE_HCI_LOCAL_HOST_TERMINATED_CONNECTION: |
| 205 | + reason = Gap::LOCAL_HOST_TERMINATED_CONNECTION; |
| 206 | + break; |
| 207 | + case BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION: |
| 208 | + reason = Gap::REMOTE_USER_TERMINATED_CONNECTION; |
| 209 | + break; |
| 210 | + case BLE_HCI_CONN_INTERVAL_UNACCEPTABLE: |
| 211 | + reason = Gap::CONN_INTERVAL_UNACCEPTABLE; |
| 212 | + break; |
| 213 | + default: |
| 214 | + /* Please refer to the underlying transport library for an |
| 215 | + * interpretion of this reason's value. */ |
| 216 | + reason = static_cast<Gap::DisconnectionReason_t>(p_ble_evt->evt.gap_evt.params.disconnected.reason); |
| 217 | + break; |
| 218 | + } |
| 219 | + |
| 220 | +#if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110) |
| 221 | + // Close all pending discoveries for this connection |
| 222 | + nRF5xGattClient& gattClient = ble.getGattClient(); |
| 223 | + gattClient.characteristicDescriptorDiscoverer().terminate(handle, BLE_ERROR_INVALID_STATE); |
| 224 | + gattClient.discovery().terminate(handle); |
| 225 | +#endif |
| 226 | + |
| 227 | + gap.processDisconnectionEvent(handle, reason); |
| 228 | + break; |
| 229 | + } |
| 230 | + |
| 231 | + case BLE_GAP_EVT_PASSKEY_DISPLAY: |
| 232 | + securityManager.processPasskeyDisplayEvent(p_ble_evt->evt.gap_evt.conn_handle, p_ble_evt->evt.gap_evt.params.passkey_display.passkey); |
| 233 | + break; |
| 234 | + |
| 235 | + case BLE_GAP_EVT_TIMEOUT: |
| 236 | + gap.processTimeoutEvent(static_cast<Gap::TimeoutSource_t>(p_ble_evt->evt.gap_evt.params.timeout.src)); |
| 237 | + break; |
| 238 | + |
| 239 | + case BLE_GATTC_EVT_TIMEOUT: |
| 240 | + case BLE_GATTS_EVT_TIMEOUT: |
| 241 | + // Disconnect on GATT Server and Client timeout events. |
| 242 | + // ASSERT_STATUS_RET_VOID (sd_ble_gap_disconnect(m_conn_handle, |
| 243 | + // BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION)); |
| 244 | + break; |
| 245 | + |
| 246 | + case BLE_GAP_EVT_ADV_REPORT: { |
| 247 | + const ble_gap_evt_adv_report_t *advReport = &p_ble_evt->evt.gap_evt.params.adv_report; |
| 248 | + gap.processAdvertisementReport(advReport->peer_addr.addr, |
| 249 | + advReport->rssi, |
| 250 | + advReport->scan_rsp, |
| 251 | + static_cast<GapAdvertisingParams::AdvertisingType_t>(advReport->type), |
| 252 | + advReport->dlen, |
| 253 | + advReport->data); |
| 254 | + break; |
| 255 | + } |
| 256 | + |
| 257 | + default: |
| 258 | + break; |
| 259 | + } |
| 260 | + |
| 261 | + gattServer.hwCallback(p_ble_evt); |
| 262 | +} |
| 263 | + |
| 264 | +/*! @brief Callback when an error occurs inside the SoftDevice */ |
| 265 | +void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name) |
| 266 | +{ |
| 267 | + ASSERT(false, (void) 0); |
| 268 | +} |
| 269 | + |
| 270 | +/*! |
| 271 | + @brief Handler for general errors above the SoftDevice layer. |
| 272 | + Typically we can' recover from this so we do a reset. |
| 273 | +*/ |
| 274 | +void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name) |
| 275 | +{ |
| 276 | + ASSERT_STATUS_RET_VOID( error_code ); |
| 277 | + NVIC_SystemReset(); |
| 278 | +} |
0 commit comments