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