konashi/SBBLEのテスト

Dependencies:   BLE_API mbed

Fork of BLE_LoopbackUART by Bluetooth Low Energy

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers btle.cpp Source File

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 #if NEED_BOND_MANAGER /* disabled by default */
00043 static void service_error_callback(uint32_t nrf_error);
00044 #endif
00045 extern "C" void        assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name);
00046 void        app_error_handler(uint32_t       error_code,
00047                               uint32_t       line_num,
00048                               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 #if NEED_PSTORAGE /* disabled by default */
00059     pstorage_sys_event_handler(sys_evt);
00060 #endif
00061 }
00062 
00063 error_t btle_init(void)
00064 {
00065     APP_TIMER_INIT(0, 8, 5, false);
00066 //    SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, false);
00067     SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_RC_250_PPM_4000MS_CALIBRATION, 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 #if NEED_BOND_MANAGER /* disabled by default */
00073     bond_manager_init();
00074 #endif
00075     btle_gap_init();
00076 
00077     return ERROR_NONE;
00078 }
00079 
00080 static void btle_handler(ble_evt_t *p_ble_evt)
00081 {
00082     /* Library service handlers */
00083 #if NEED_BOND_MANAGER /* disabled by default */
00084     ble_bondmngr_on_ble_evt(p_ble_evt);
00085 #endif
00086     ble_conn_params_on_ble_evt(p_ble_evt);
00087 
00088     /* Custom event handler */
00089     switch (p_ble_evt->header.evt_id) {
00090         case BLE_GAP_EVT_CONNECTED: {
00091             Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle;
00092             nRF51Gap::getInstance().setConnectionHandle(handle);
00093             nRF51Gap::getInstance().processHandleSpecificEvent(GapEvents::GAP_EVENT_CONNECTED, handle);
00094             break;
00095         }
00096 
00097         case BLE_GAP_EVT_DISCONNECTED: {
00098             Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle;
00099             // Since we are not in a connection and have not started advertising,
00100             // store bonds
00101             nRF51Gap::getInstance().setConnectionHandle (BLE_CONN_HANDLE_INVALID);
00102 #if NEED_BOND_MANAGER /* disabled by default */
00103             ASSERT_STATUS_RET_VOID ( ble_bondmngr_bonded_centrals_store());
00104 #endif
00105             nRF51Gap::getInstance().processHandleSpecificEvent(GapEvents::GAP_EVENT_DISCONNECTED, handle);
00106             break;
00107         }
00108 
00109         case BLE_GAP_EVT_SEC_PARAMS_REQUEST: {
00110             ble_gap_sec_params_t sec_params = {0};
00111 
00112             sec_params.timeout      = 30; /*< Timeout for Pairing Request or
00113                                    * Security Request (in seconds). */
00114             sec_params.bond         = 1;  /**< Perform bonding. */
00115             sec_params.mitm         = CFG_BLE_SEC_PARAM_MITM;
00116             sec_params.io_caps      = CFG_BLE_SEC_PARAM_IO_CAPABILITIES;
00117             sec_params.oob          = CFG_BLE_SEC_PARAM_OOB;
00118             sec_params.min_key_size = CFG_BLE_SEC_PARAM_MIN_KEY_SIZE;
00119             sec_params.max_key_size = CFG_BLE_SEC_PARAM_MAX_KEY_SIZE;
00120 
00121             ASSERT_STATUS_RET_VOID(
00122                 sd_ble_gap_sec_params_reply(nRF51Gap::getInstance().
00123                                             getConnectionHandle(),
00124                                             BLE_GAP_SEC_STATUS_SUCCESS,
00125                                             &sec_params));
00126         }
00127         break;
00128 
00129         case BLE_GAP_EVT_TIMEOUT:
00130             if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT) {
00131                 nRF51Gap::getInstance().processEvent(GapEvents::GAP_EVENT_TIMEOUT);
00132             }
00133             break;
00134 
00135         case BLE_GATTC_EVT_TIMEOUT:
00136         case BLE_GATTS_EVT_TIMEOUT:
00137             // Disconnect on GATT Server and Client timeout events.
00138             // ASSERT_STATUS_RET_VOID (sd_ble_gap_disconnect(m_conn_handle,
00139             // BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION));
00140             break;
00141 
00142         default:
00143             break;
00144     }
00145 
00146     nRF51GattServer::getInstance().hwCallback(p_ble_evt);
00147 }
00148 
00149 #if NEED_BOND_MANAGER /* disabled by default */
00150 /**************************************************************************/
00151 /*!
00152     @brief      Initialises the bond manager
00153 
00154     @note       Bond data will be cleared on reset if the bond delete
00155                 button is pressed during initialisation (the button is
00156                 defined as CFG_BLE_BOND_DELETE_BUTTON_NUM).
00157 
00158     @returns
00159 */
00160 /**************************************************************************/
00161 static error_t bond_manager_init(void)
00162 {
00163     ble_bondmngr_init_t bond_para = {0};
00164 
00165     ASSERT_STATUS ( pstorage_init());
00166 
00167     bond_para.flash_page_num_bond     = CFG_BLE_BOND_FLASH_PAGE_BOND;
00168     bond_para.flash_page_num_sys_attr = CFG_BLE_BOND_FLASH_PAGE_SYS_ATTR;
00169     //bond_para.bonds_delete            = boardButtonCheck(CFG_BLE_BOND_DELETE_BUTTON_NUM) ;
00170     bond_para.evt_handler   = NULL;
00171     bond_para.error_handler = service_error_callback;
00172 
00173     ASSERT_STATUS( ble_bondmngr_init( &bond_para ));
00174 
00175     /* Init radio active/inactive notification to flash (to only perform flashing when the radio is inactive) */
00176     //  ASSERT_STATUS( ble_radio_notification_init(NRF_APP_PRIORITY_HIGH,
00177     //                                             NRF_RADIO_NOTIFICATION_DISTANCE_4560US,
00178     //                                             ble_flash_on_radio_active_evt) );
00179 
00180     return ERROR_NONE;
00181 }
00182 #endif // #if NEED_BOND_MANAGER
00183 
00184 #if NEED_BOND_MANAGER /* disabled by default */
00185 /**************************************************************************/
00186 /*!
00187     @brief
00188     @param[in]  nrf_error
00189     @returns
00190 */
00191 /**************************************************************************/
00192 static void service_error_callback(uint32_t nrf_error)
00193 {
00194     ASSERT_STATUS_RET_VOID( nrf_error );
00195 }
00196 #endif // #if NEED_BOND_MANAGER
00197 
00198 /**************************************************************************/
00199 /*!
00200     @brief      Callback when an error occurs inside the SoftDevice
00201 
00202     @param[in]  line_num
00203     @param[in]  p-file_name
00204 
00205     @returns
00206 */
00207 /**************************************************************************/
00208 void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name)
00209 {
00210     ASSERT(false, (void) 0);
00211 }
00212 
00213 /**************************************************************************/
00214 /*!
00215     @brief      Handler for general errors above the SoftDevice layer.
00216                 Typically we can' recover from this so we do a reset.
00217 
00218     @param[in]  error_code
00219     @param[in]  line_num
00220     @param[in]  p-file_name
00221 
00222     @returns
00223 */
00224 /**************************************************************************/
00225 void app_error_handler(uint32_t       error_code,
00226                        uint32_t       line_num,
00227                        const uint8_t *p_file_name)
00228 {
00229     ASSERT_STATUS_RET_VOID( error_code );
00230     NVIC_SystemReset();
00231 }