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