Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of Elearnning by
btle.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "common/common.h" 00018 #include "nordic_common.h" 00019 00020 #include "btle.h" 00021 00022 #include "ble_stack_handler_types.h " 00023 #include "ble_flash.h " 00024 #include "ble_conn_params.h " 00025 00026 #include "btle_gap.h" 00027 #include "btle_advertising.h" 00028 #include "custom/custom_helper.h" 00029 00030 #include "softdevice_handler.h " 00031 #include "pstorage.h " 00032 00033 #include "ble/GapEvents.h" 00034 #include "nRF5xGap.h" 00035 #include "nRF5xGattServer.h" 00036 #include "nRF5xSecurityManager.h" 00037 00038 #include "device_manager.h " 00039 00040 #include "ble_hci.h" 00041 #include "btle_discovery.h" 00042 00043 extern "C" void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name); 00044 void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name); 00045 00046 static void btle_handler(ble_evt_t *p_ble_evt); 00047 00048 static void sys_evt_dispatch(uint32_t sys_evt) 00049 { 00050 pstorage_sys_event_handler(sys_evt); 00051 } 00052 00053 /** 00054 * This function is called in interrupt context to handle BLE events; i.e. pull 00055 * system and user events out of the pending events-queue of the BLE stack. The 00056 * BLE stack signals the availability of events by the triggering the SWI2 00057 * interrupt, which forwards the handling to this function. 00058 * 00059 * The event processing loop is implemented in intern_softdevice_events_execute(). 00060 * 00061 * In mbed OS, a callback for intern_softdevice_events_execute() is posted 00062 * to the scheduler, which then executes in thread mode. In mbed-classic, 00063 * event processing happens right-away in interrupt context (which is more 00064 * risk-prone). In either case, the logic of event processing is identical. 00065 */ 00066 static uint32_t eventHandler() 00067 { 00068 #ifdef YOTTA_CFG_MBED_OS 00069 minar::Scheduler::postCallback(intern_softdevice_events_execute); 00070 #else 00071 intern_softdevice_events_execute(); 00072 #endif 00073 00074 return NRF_SUCCESS; 00075 } 00076 00077 error_t btle_init(void) 00078 { 00079 nrf_clock_lfclksrc_t clockSource; 00080 if (NRF_CLOCK->LFCLKSRC & (CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos)) { 00081 clockSource = NRF_CLOCK_LFCLKSRC_XTAL_20_PPM; 00082 } else { 00083 clockSource = NRF_CLOCK_LFCLKSRC_RC_250_PPM_4000MS_CALIBRATION; 00084 } 00085 SOFTDEVICE_HANDLER_INIT(clockSource, eventHandler); 00086 00087 // Enable BLE stack 00088 /** 00089 * Using this call, the application can select whether to include the 00090 * Service Changed characteristic in the GATT Server. The default in all 00091 * previous releases has been to include the Service Changed characteristic, 00092 * but this affects how GATT clients behave. Specifically, it requires 00093 * clients to subscribe to this attribute and not to cache attribute handles 00094 * between connections unless the devices are bonded. If the application 00095 * does not need to change the structure of the GATT server attributes at 00096 * runtime this adds unnecessary complexity to the interaction with peer 00097 * clients. If the SoftDevice is enabled with the Service Changed 00098 * Characteristics turned off, then clients are allowed to cache attribute 00099 * handles making applications simpler on both sides. 00100 */ 00101 static const bool IS_SRVC_CHANGED_CHARACT_PRESENT = true; 00102 ble_enable_params_t enableParams = { 00103 .gatts_enable_params = { 00104 .service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT 00105 } 00106 }; 00107 if (sd_ble_enable(&enableParams) != NRF_SUCCESS) { 00108 return ERROR_INVALID_PARAM; 00109 } 00110 00111 ble_gap_addr_t addr; 00112 if (sd_ble_gap_address_get(&addr) != NRF_SUCCESS) { 00113 return ERROR_INVALID_PARAM; 00114 } 00115 if (sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &addr) != NRF_SUCCESS) { 00116 return ERROR_INVALID_PARAM; 00117 } 00118 00119 ASSERT_STATUS( softdevice_ble_evt_handler_set(btle_handler)); 00120 ASSERT_STATUS( softdevice_sys_evt_handler_set(sys_evt_dispatch)); 00121 00122 btle_gap_init(); 00123 00124 return ERROR_NONE; 00125 } 00126 00127 static void btle_handler(ble_evt_t *p_ble_evt) 00128 { 00129 /* Library service handlers */ 00130 #if SDK_CONN_PARAMS_MODULE_ENABLE 00131 ble_conn_params_on_ble_evt(p_ble_evt); 00132 #endif 00133 00134 dm_ble_evt_handler(p_ble_evt); 00135 00136 #if !defined(MCU_NRF51_16K_S110) && !defined(MCU_NRF51_32K_S110) 00137 bleGattcEventHandler(p_ble_evt); 00138 #endif 00139 00140 /* Custom event handler */ 00141 switch (p_ble_evt->header.evt_id) { 00142 case BLE_GAP_EVT_CONNECTED: { 00143 Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle; 00144 #if defined(MCU_NRF51_16K_S110) || defined(MCU_NRF51_32K_S110) 00145 /* Only peripheral role is supported by S110 */ 00146 Gap::Role_t role = Gap::PERIPHERAL; 00147 #else 00148 Gap::Role_t role = static_cast<Gap::Role_t>(p_ble_evt->evt.gap_evt.params.connected.role); 00149 #endif 00150 nRF5xGap::getInstance().setConnectionHandle(handle); 00151 const Gap::ConnectionParams_t *params = reinterpret_cast<Gap::ConnectionParams_t *>(&(p_ble_evt->evt.gap_evt.params.connected.conn_params)); 00152 const ble_gap_addr_t *peer = &p_ble_evt->evt.gap_evt.params.connected.peer_addr; 00153 const ble_gap_addr_t *own = &p_ble_evt->evt.gap_evt.params.connected.own_addr; 00154 nRF5xGap::getInstance().processConnectionEvent(handle, 00155 role, 00156 static_cast<Gap::AddressType_t>(peer->addr_type), peer->addr, 00157 static_cast<Gap::AddressType_t>(own->addr_type), own->addr, 00158 params); 00159 break; 00160 } 00161 00162 case BLE_GAP_EVT_DISCONNECTED: { 00163 Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle; 00164 // Since we are not in a connection and have not started advertising, 00165 // store bonds 00166 nRF5xGap::getInstance().setConnectionHandle (BLE_CONN_HANDLE_INVALID); 00167 00168 Gap::DisconnectionReason_t reason; 00169 switch (p_ble_evt->evt.gap_evt.params.disconnected.reason) { 00170 case BLE_HCI_LOCAL_HOST_TERMINATED_CONNECTION: 00171 reason = Gap::LOCAL_HOST_TERMINATED_CONNECTION; 00172 break; 00173 case BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION: 00174 reason = Gap::REMOTE_USER_TERMINATED_CONNECTION; 00175 break; 00176 case BLE_HCI_CONN_INTERVAL_UNACCEPTABLE: 00177 reason = Gap::CONN_INTERVAL_UNACCEPTABLE; 00178 break; 00179 default: 00180 /* Please refer to the underlying transport library for an 00181 * interpretion of this reason's value. */ 00182 reason = static_cast<Gap::DisconnectionReason_t>(p_ble_evt->evt.gap_evt.params.disconnected.reason); 00183 break; 00184 } 00185 nRF5xGap::getInstance().processDisconnectionEvent(handle, reason); 00186 break; 00187 } 00188 00189 case BLE_GAP_EVT_PASSKEY_DISPLAY: 00190 nRF5xSecurityManager::getInstance().processPasskeyDisplayEvent(p_ble_evt->evt.gap_evt.conn_handle, p_ble_evt->evt.gap_evt.params.passkey_display.passkey); 00191 break; 00192 00193 case BLE_GAP_EVT_TIMEOUT: 00194 nRF5xGap::getInstance().processTimeoutEvent(static_cast<Gap::TimeoutSource_t>(p_ble_evt->evt.gap_evt.params.timeout.src)); 00195 break; 00196 00197 case BLE_GATTC_EVT_TIMEOUT: 00198 case BLE_GATTS_EVT_TIMEOUT: 00199 // Disconnect on GATT Server and Client timeout events. 00200 // ASSERT_STATUS_RET_VOID (sd_ble_gap_disconnect(m_conn_handle, 00201 // BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION)); 00202 break; 00203 00204 case BLE_GAP_EVT_ADV_REPORT: { 00205 const ble_gap_evt_adv_report_t *advReport = &p_ble_evt->evt.gap_evt.params.adv_report; 00206 nRF5xGap::getInstance().processAdvertisementReport(advReport->peer_addr.addr, 00207 advReport->rssi, 00208 advReport->scan_rsp, 00209 static_cast<GapAdvertisingParams::AdvertisingType_t>(advReport->type), 00210 advReport->dlen, 00211 advReport->data); 00212 break; 00213 } 00214 00215 default: 00216 break; 00217 } 00218 00219 nRF5xGattServer::getInstance().hwCallback(p_ble_evt); 00220 } 00221 00222 /*! @brief Callback when an error occurs inside the SoftDevice */ 00223 void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name) 00224 { 00225 ASSERT(false, (void) 0); 00226 } 00227 00228 /*! 00229 @brief Handler for general errors above the SoftDevice layer. 00230 Typically we can' recover from this so we do a reset. 00231 */ 00232 void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name) 00233 { 00234 ASSERT_STATUS_RET_VOID( error_code ); 00235 NVIC_SystemReset(); 00236 }
Generated on Tue Jul 12 2022 15:08:28 by
1.7.2
