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.
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 #include "common/common.h " 00017 00018 #include "app_timer.h " 00019 #include "btle.h" 00020 00021 #include "ble_stack_handler_types.h " 00022 #include "ble_radio_notification.h " 00023 #include "ble_flash.h " 00024 #include "ble_bondmngr.h " 00025 #include "ble_conn_params.h " 00026 00027 #include "btle_gap.h" 00028 #include "btle_advertising.h" 00029 #include "custom/custom_helper.h" 00030 00031 #include "nordic_common.h" 00032 #include "softdevice_handler.h " 00033 #include "pstorage.h " 00034 00035 #include "hw/GapEvents.h" 00036 #include "hw/nRF51822n/nRF51Gap.h" 00037 #include "hw/nRF51822n/nRF51GattServer.h" 00038 00039 static void service_error_callback(uint32_t nrf_error); 00040 void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name); 00041 void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name); 00042 00043 static error_t bond_manager_init(void); 00044 00045 static void btle_handler(ble_evt_t * p_ble_evt); 00046 00047 /**************************************************************************/ 00048 /*! 00049 00050 */ 00051 /**************************************************************************/ 00052 static void sys_evt_dispatch(uint32_t sys_evt) 00053 { 00054 pstorage_sys_event_handler(sys_evt); 00055 } 00056 00057 /**************************************************************************/ 00058 /*! 00059 @brief Initialises BTLE and the underlying HW/SoftDevice 00060 00061 @returns 00062 */ 00063 /**************************************************************************/ 00064 error_t btle_init(void) 00065 { 00066 APP_TIMER_INIT(0, 8, 5, false); 00067 SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, false); 00068 00069 ASSERT_STATUS( softdevice_ble_evt_handler_set(btle_handler) ); 00070 ASSERT_STATUS( softdevice_sys_evt_handler_set(sys_evt_dispatch) ); 00071 00072 bond_manager_init(); 00073 btle_gap_init(); 00074 00075 return ERROR_NONE; 00076 } 00077 00078 /**************************************************************************/ 00079 /*! 00080 @brief 00081 00082 @param[in] p_ble_evt 00083 00084 @returns 00085 */ 00086 /**************************************************************************/ 00087 static void btle_handler(ble_evt_t * p_ble_evt) 00088 { 00089 /* Library service handlers */ 00090 ble_bondmngr_on_ble_evt(p_ble_evt); 00091 ble_conn_params_on_ble_evt(p_ble_evt); 00092 00093 /* Custom event handler */ 00094 switch (p_ble_evt->header.evt_id) 00095 { 00096 case BLE_GAP_EVT_CONNECTED: 00097 nRF51GattServer::getInstance().m_connectionHandle = p_ble_evt->evt.gap_evt.conn_handle; 00098 nRF51Gap::getInstance().handleEvent(GapEvents::GAP_EVENT_CONNECTED); 00099 break; 00100 00101 case BLE_GAP_EVT_DISCONNECTED: 00102 // Since we are not in a connection and have not started advertising, store bonds 00103 nRF51GattServer::getInstance().m_connectionHandle = BLE_CONN_HANDLE_INVALID; 00104 ASSERT_STATUS_RET_VOID ( ble_bondmngr_bonded_centrals_store() ); 00105 nRF51Gap::getInstance().handleEvent(GapEvents::GAP_EVENT_DISCONNECTED); 00106 break; 00107 00108 case BLE_GAP_EVT_SEC_PARAMS_REQUEST: 00109 { 00110 ble_gap_sec_params_t sec_params = { 0 }; 00111 00112 sec_params.timeout = 30 ; /**< Timeout for Pairing Request or Security Request (in seconds). */ 00113 sec_params.bond = 1 ; /**< Perform bonding. */ 00114 sec_params.mitm = CFG_BLE_SEC_PARAM_MITM ; 00115 sec_params.io_caps = CFG_BLE_SEC_PARAM_IO_CAPABILITIES ; 00116 sec_params.oob = CFG_BLE_SEC_PARAM_OOB ; 00117 sec_params.min_key_size = CFG_BLE_SEC_PARAM_MIN_KEY_SIZE ; 00118 sec_params.max_key_size = CFG_BLE_SEC_PARAM_MAX_KEY_SIZE ; 00119 00120 ASSERT_STATUS_RET_VOID ( sd_ble_gap_sec_params_reply(nRF51GattServer::getInstance().m_connectionHandle, BLE_GAP_SEC_STATUS_SUCCESS, &sec_params) ); 00121 } 00122 break; 00123 00124 case BLE_GAP_EVT_TIMEOUT: 00125 if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT) 00126 { 00127 nRF51Gap::getInstance().handleEvent(GapEvents::GAP_EVENT_TIMEOUT); 00128 } 00129 break; 00130 00131 case BLE_GATTC_EVT_TIMEOUT: 00132 case BLE_GATTS_EVT_TIMEOUT: 00133 // Disconnect on GATT Server and Client timeout events. 00134 // ASSERT_STATUS_RET_VOID (sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION)); 00135 break; 00136 00137 default: 00138 break; 00139 } 00140 00141 nRF51GattServer::getInstance().hwCallback(p_ble_evt); 00142 } 00143 00144 /**************************************************************************/ 00145 /*! 00146 @brief Initialises the bond manager 00147 00148 @note Bond data will be cleared on reset if the bond delete 00149 button is pressed during initialisation (the button is 00150 defined as CFG_BLE_BOND_DELETE_BUTTON_NUM). 00151 00152 @returns 00153 */ 00154 /**************************************************************************/ 00155 static error_t bond_manager_init(void) 00156 { 00157 ble_bondmngr_init_t bond_para = { 0 }; 00158 00159 ASSERT_STATUS ( pstorage_init() ); 00160 00161 bond_para.flash_page_num_bond = CFG_BLE_BOND_FLASH_PAGE_BOND ; 00162 bond_para.flash_page_num_sys_attr = CFG_BLE_BOND_FLASH_PAGE_SYS_ATTR ; 00163 //bond_para.bonds_delete = boardButtonCheck(CFG_BLE_BOND_DELETE_BUTTON_NUM) ; 00164 bond_para.evt_handler = NULL ; 00165 bond_para.error_handler = service_error_callback ; 00166 00167 ASSERT_STATUS( ble_bondmngr_init( &bond_para ) ); 00168 00169 /* Init radio active/inactive notification to flash (to only perform flashing when the radio is inactive) */ 00170 // ASSERT_STATUS( ble_radio_notification_init(NRF_APP_PRIORITY_HIGH, 00171 // NRF_RADIO_NOTIFICATION_DISTANCE_4560US, 00172 // ble_flash_on_radio_active_evt) ); 00173 00174 return ERROR_NONE; 00175 } 00176 00177 /**************************************************************************/ 00178 /*! 00179 @brief 00180 @param[in] nrf_error 00181 @returns 00182 */ 00183 /**************************************************************************/ 00184 static void service_error_callback(uint32_t nrf_error) 00185 { 00186 ASSERT_STATUS_RET_VOID( nrf_error ); 00187 } 00188 00189 /**************************************************************************/ 00190 /*! 00191 @brief Callback when an error occurs inside the SoftDevice 00192 00193 @param[in] line_num 00194 @param[in] p-file_name 00195 00196 @returns 00197 */ 00198 /**************************************************************************/ 00199 void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name) 00200 { 00201 ASSERT(false, (void) 0); 00202 } 00203 00204 /**************************************************************************/ 00205 /*! 00206 @brief Handler for general errors above the SoftDevice layer. 00207 Typically we can' recover from this so we do a reset. 00208 00209 @param[in] error_code 00210 @param[in] line_num 00211 @param[in] p-file_name 00212 00213 @returns 00214 */ 00215 /**************************************************************************/ 00216 void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name) 00217 { 00218 ASSERT_STATUS_RET_VOID( error_code ); 00219 NVIC_SystemReset(); 00220 }
Generated on Tue Jul 12 2022 12:14:18 by
