AndroidのBLEラジコンプロポアプリ「BLEPropo」と接続し、RCサーボとDCモータを制御するプログラムです。 BLE Nanoで動作を確認しています。 BLEPropo → https://github.com/lipoyang/BLEPropo
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 "app_timer.h " 00021 #include "btle.h" 00022 00023 #include "ble_stack_handler_types.h " 00024 #include "ble_radio_notification.h " 00025 #include "ble_flash.h " 00026 #if NEED_BOND_MANAGER 00027 #include "ble_bondmngr.h" 00028 #endif 00029 #include "ble_conn_params.h " 00030 00031 #include "btle_gap.h" 00032 #include "btle_advertising.h" 00033 #include "custom/custom_helper.h" 00034 00035 #include "softdevice_handler.h " 00036 #include "pstorage.h " 00037 00038 #include "GapEvents.h" 00039 #include "nRF51Gap.h" 00040 #include "nRF51GattServer.h" 00041 00042 #include "ble_hci.h" 00043 00044 #if NEED_BOND_MANAGER /* disabled by default */ 00045 static void service_error_callback(uint32_t nrf_error); 00046 #endif 00047 extern "C" void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name); 00048 void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name); 00049 00050 #if NEED_BOND_MANAGER /* disabled by default */ 00051 static error_t bond_manager_init(void); 00052 #endif 00053 00054 static void btle_handler(ble_evt_t *p_ble_evt); 00055 00056 static void sys_evt_dispatch(uint32_t sys_evt) 00057 { 00058 pstorage_sys_event_handler(sys_evt); 00059 } 00060 00061 error_t btle_init(void) 00062 { 00063 const bool useScheduler = false; 00064 #ifdef TARGET_HRM1017 00065 SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_RC_250_PPM_4000MS_CALIBRATION, useScheduler); 00066 #else 00067 SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, useScheduler); 00068 #endif 00069 00070 // Enable BLE stack 00071 /** 00072 * Using this call, the application can select whether to include the 00073 * Service Changed characteristic in the GATT Server. The default in all 00074 * previous releases has been to include the Service Changed characteristic, 00075 * but this affects how GATT clients behave. Specifically, it requires 00076 * clients to subscribe to this attribute and not to cache attribute handles 00077 * between connections unless the devices are bonded. If the application 00078 * does not need to change the structure of the GATT server attributes at 00079 * runtime this adds unnecessary complexity to the interaction with peer 00080 * clients. If the SoftDevice is enabled with the Service Changed 00081 * Characteristics turned off, then clients are allowed to cache attribute 00082 * handles making applications simpler on both sides. 00083 */ 00084 static const bool IS_SRVC_CHANGED_CHARACT_PRESENT = true; 00085 ble_enable_params_t enableParams = { 00086 .gatts_enable_params = { 00087 .service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT 00088 } 00089 }; 00090 if (sd_ble_enable(&enableParams) != NRF_SUCCESS) { 00091 return ERROR_INVALID_PARAM; 00092 } 00093 00094 ble_gap_addr_t addr; 00095 if (sd_ble_gap_address_get(&addr) != NRF_SUCCESS) { 00096 return ERROR_INVALID_PARAM; 00097 } 00098 if (sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &addr) != NRF_SUCCESS) { 00099 return ERROR_INVALID_PARAM; 00100 } 00101 00102 ASSERT_STATUS( softdevice_ble_evt_handler_set(btle_handler)); 00103 ASSERT_STATUS( softdevice_sys_evt_handler_set(sys_evt_dispatch)); 00104 00105 #if NEED_BOND_MANAGER /* disabled by default */ 00106 bond_manager_init(); 00107 #endif 00108 btle_gap_init(); 00109 00110 return ERROR_NONE; 00111 } 00112 00113 static void btle_handler(ble_evt_t *p_ble_evt) 00114 { 00115 /* Library service handlers */ 00116 #if NEED_BOND_MANAGER /* disabled by default */ 00117 ble_bondmngr_on_ble_evt(p_ble_evt); 00118 #endif 00119 #if SDK_CONN_PARAMS_MODULE_ENABLE 00120 ble_conn_params_on_ble_evt(p_ble_evt); 00121 #endif 00122 00123 /* Custom event handler */ 00124 switch (p_ble_evt->header.evt_id) { 00125 case BLE_GAP_EVT_CONNECTED: { 00126 Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle; 00127 nRF51Gap::getInstance().setConnectionHandle(handle); 00128 const Gap::ConnectionParams_t *params = reinterpret_cast<Gap::ConnectionParams_t *>(&(p_ble_evt->evt.gap_evt.params.connected.conn_params)); 00129 const ble_gap_addr_t *peer = &p_ble_evt->evt.gap_evt.params.connected.peer_addr; 00130 nRF51Gap::getInstance().processConnectionEvent(handle, static_cast<Gap::addr_type_t>(peer->addr_type), peer->addr, params); 00131 break; 00132 } 00133 00134 case BLE_GAP_EVT_DISCONNECTED: { 00135 Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle; 00136 // Since we are not in a connection and have not started advertising, 00137 // store bonds 00138 nRF51Gap::getInstance().setConnectionHandle (BLE_CONN_HANDLE_INVALID); 00139 #if NEED_BOND_MANAGER /* disabled by default */ 00140 ASSERT_STATUS_RET_VOID ( ble_bondmngr_bonded_centrals_store()); 00141 #endif 00142 00143 Gap::DisconnectionReason_t reason; 00144 switch (p_ble_evt->evt.gap_evt.params.disconnected.reason) { 00145 case BLE_HCI_LOCAL_HOST_TERMINATED_CONNECTION: 00146 reason = Gap::LOCAL_HOST_TERMINATED_CONNECTION; 00147 break; 00148 case BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION: 00149 reason = Gap::REMOTE_USER_TERMINATED_CONNECTION; 00150 break; 00151 case BLE_HCI_CONN_INTERVAL_UNACCEPTABLE: 00152 reason = Gap::CONN_INTERVAL_UNACCEPTABLE; 00153 break; 00154 default: 00155 /* Please refer to the underlying transport library for an 00156 * interpretion of this reason's value. */ 00157 reason = static_cast<Gap::DisconnectionReason_t>(p_ble_evt->evt.gap_evt.params.disconnected.reason); 00158 break; 00159 } 00160 nRF51Gap::getInstance().processDisconnectionEvent(handle, reason); 00161 break; 00162 } 00163 00164 case BLE_GAP_EVT_SEC_PARAMS_REQUEST: { 00165 ble_gap_sec_params_t sec_params = {0}; 00166 00167 sec_params.timeout = 30; /*< Timeout for Pairing Request or 00168 * Security Request (in seconds). */ 00169 sec_params.bond = 1; /**< Perform bonding. */ 00170 sec_params.mitm = CFG_BLE_SEC_PARAM_MITM; 00171 sec_params.io_caps = CFG_BLE_SEC_PARAM_IO_CAPABILITIES; 00172 sec_params.oob = CFG_BLE_SEC_PARAM_OOB; 00173 sec_params.min_key_size = CFG_BLE_SEC_PARAM_MIN_KEY_SIZE; 00174 sec_params.max_key_size = CFG_BLE_SEC_PARAM_MAX_KEY_SIZE; 00175 00176 ASSERT_STATUS_RET_VOID(sd_ble_gap_sec_params_reply(nRF51Gap::getInstance().getConnectionHandle(), 00177 BLE_GAP_SEC_STATUS_SUCCESS, &sec_params)); 00178 } 00179 break; 00180 00181 case BLE_GAP_EVT_TIMEOUT: 00182 if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT) { 00183 nRF51Gap::getInstance().processEvent(GapEvents::GAP_EVENT_TIMEOUT); 00184 } 00185 break; 00186 00187 case BLE_GATTC_EVT_TIMEOUT: 00188 case BLE_GATTS_EVT_TIMEOUT: 00189 // Disconnect on GATT Server and Client timeout events. 00190 // ASSERT_STATUS_RET_VOID (sd_ble_gap_disconnect(m_conn_handle, 00191 // BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION)); 00192 break; 00193 00194 default: 00195 break; 00196 } 00197 00198 nRF51GattServer::getInstance().hwCallback(p_ble_evt); 00199 } 00200 00201 #if NEED_BOND_MANAGER /* disabled by default */ 00202 /**************************************************************************/ 00203 /*! 00204 @brief Initialises the bond manager 00205 00206 @note Bond data will be cleared on reset if the bond delete 00207 button is pressed during initialisation (the button is 00208 defined as CFG_BLE_BOND_DELETE_BUTTON_NUM). 00209 00210 @returns 00211 */ 00212 /**************************************************************************/ 00213 static error_t bond_manager_init(void) 00214 { 00215 ble_bondmngr_init_t bond_para = {0}; 00216 00217 ASSERT_STATUS ( pstorage_init()); 00218 00219 bond_para.flash_page_num_bond = CFG_BLE_BOND_FLASH_PAGE_BOND; 00220 bond_para.flash_page_num_sys_attr = CFG_BLE_BOND_FLASH_PAGE_SYS_ATTR; 00221 //bond_para.bonds_delete = boardButtonCheck(CFG_BLE_BOND_DELETE_BUTTON_NUM) ; 00222 bond_para.evt_handler = NULL; 00223 bond_para.error_handler = service_error_callback; 00224 00225 ASSERT_STATUS( ble_bondmngr_init( &bond_para )); 00226 00227 /* Init radio active/inactive notification to flash (to only perform flashing when the radio is inactive) */ 00228 // ASSERT_STATUS( ble_radio_notification_init(NRF_APP_PRIORITY_HIGH, 00229 // NRF_RADIO_NOTIFICATION_DISTANCE_4560US, 00230 // ble_flash_on_radio_active_evt) ); 00231 00232 return ERROR_NONE; 00233 } 00234 #endif // #if NEED_BOND_MANAGER 00235 00236 #if NEED_BOND_MANAGER /* disabled by default */ 00237 /**************************************************************************/ 00238 /*! 00239 @brief 00240 @param[in] nrf_error 00241 @returns 00242 */ 00243 /**************************************************************************/ 00244 static void service_error_callback(uint32_t nrf_error) 00245 { 00246 ASSERT_STATUS_RET_VOID( nrf_error ); 00247 } 00248 #endif // #if NEED_BOND_MANAGER 00249 00250 /**************************************************************************/ 00251 /*! 00252 @brief Callback when an error occurs inside the SoftDevice 00253 00254 @param[in] line_num 00255 @param[in] p-file_name 00256 00257 @returns 00258 */ 00259 /**************************************************************************/ 00260 void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name) 00261 { 00262 ASSERT(false, (void) 0); 00263 } 00264 00265 /**************************************************************************/ 00266 /*! 00267 @brief Handler for general errors above the SoftDevice layer. 00268 Typically we can' recover from this so we do a reset. 00269 00270 @param[in] error_code 00271 @param[in] line_num 00272 @param[in] p-file_name 00273 00274 @returns 00275 */ 00276 /**************************************************************************/ 00277 void app_error_handler(uint32_t error_code, 00278 uint32_t line_num, 00279 const uint8_t *p_file_name) 00280 { 00281 ASSERT_STATUS_RET_VOID( error_code ); 00282 NVIC_SystemReset(); 00283 }
Generated on Tue Jul 12 2022 16:00:45 by
1.7.2
Bizan Nishimura