long dao / nRF51822

Dependents:   VNG_BLE_Basic

Fork of nRF51822 by Nordic Semiconductor

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 #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 "nRF51Gap.h"
00037 #include "nRF51GattServer.h"
00038 
00039 #if NEED_BOND_MANAGER /* disabled by default */
00040 static void service_error_callback(uint32_t nrf_error);
00041 #endif
00042 void        assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name);
00043 void        app_error_handler(uint32_t       error_code,
00044                               uint32_t       line_num,
00045                               const uint8_t *p_file_name);
00046 
00047 #if NEED_BOND_MANAGER /* disabled by default */
00048 static error_t bond_manager_init(void);
00049 #endif
00050 
00051 static void btle_handler(ble_evt_t *p_ble_evt);
00052 
00053 static void sys_evt_dispatch(uint32_t sys_evt)
00054 {
00055 #if NEED_PSTORAGE /* disabled by default */
00056     pstorage_sys_event_handler(sys_evt);
00057 #endif
00058 }
00059 
00060 error_t btle_init(void)
00061 {
00062     APP_TIMER_INIT(0, 8, 5, false);
00063     SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, false);
00064 
00065     ASSERT_STATUS( softdevice_ble_evt_handler_set(btle_handler));
00066     ASSERT_STATUS( softdevice_sys_evt_handler_set(sys_evt_dispatch));
00067 
00068 #if NEED_BOND_MANAGER /* disabled by default */
00069     bond_manager_init();
00070 #endif
00071     btle_gap_init();
00072 
00073     return ERROR_NONE;
00074 }
00075 
00076 static void btle_handler(ble_evt_t *p_ble_evt)
00077 {
00078     /* Library service handlers */
00079 #if NEED_BOND_MANAGER /* disabled by default */
00080     ble_bondmngr_on_ble_evt(p_ble_evt);
00081 #endif
00082     ble_conn_params_on_ble_evt(p_ble_evt);
00083 
00084     /* Custom event handler */
00085     switch (p_ble_evt->header.evt_id) {
00086         case BLE_GAP_EVT_CONNECTED:
00087             nRF51Gap::getInstance().setConnectionHandle(
00088                 p_ble_evt->evt.gap_evt.conn_handle );
00089             nRF51Gap::getInstance().handleEvent(GapEvents::GAP_EVENT_CONNECTED);
00090             break;
00091 
00092         case BLE_GAP_EVT_DISCONNECTED:
00093             // Since we are not in a connection and have not started advertising,
00094             // store bonds
00095             nRF51Gap::getInstance().setConnectionHandle (BLE_CONN_HANDLE_INVALID);
00096 #if NEED_BOND_MANAGER /* disabled by default */
00097             ASSERT_STATUS_RET_VOID ( ble_bondmngr_bonded_centrals_store());
00098 #endif
00099             nRF51Gap::getInstance().handleEvent(GapEvents::GAP_EVENT_DISCONNECTED);
00100             break;
00101 
00102         case BLE_GAP_EVT_SEC_PARAMS_REQUEST: {
00103             ble_gap_sec_params_t sec_params = {0};
00104 
00105             sec_params.timeout      = 30; /*< Timeout for Pairing Request or
00106                                    * Security Request (in seconds). */
00107             sec_params.bond         = 1;  /**< Perform bonding. */
00108             sec_params.mitm         = CFG_BLE_SEC_PARAM_MITM;
00109             sec_params.io_caps      = CFG_BLE_SEC_PARAM_IO_CAPABILITIES;
00110             sec_params.oob          = CFG_BLE_SEC_PARAM_OOB;
00111             sec_params.min_key_size = CFG_BLE_SEC_PARAM_MIN_KEY_SIZE;
00112             sec_params.max_key_size = CFG_BLE_SEC_PARAM_MAX_KEY_SIZE;
00113 
00114             ASSERT_STATUS_RET_VOID(
00115                 sd_ble_gap_sec_params_reply(nRF51Gap::getInstance().
00116                                             getConnectionHandle(),
00117                                             BLE_GAP_SEC_STATUS_SUCCESS,
00118                                             &sec_params));
00119         }
00120         break;
00121 
00122         case BLE_GAP_EVT_TIMEOUT:
00123             if (p_ble_evt->evt.gap_evt.params.timeout.src ==
00124                     BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT) {
00125                 nRF51Gap::getInstance().handleEvent(GapEvents::GAP_EVENT_TIMEOUT);
00126             }
00127             break;
00128 
00129         case BLE_GATTC_EVT_TIMEOUT:
00130         case BLE_GATTS_EVT_TIMEOUT:
00131             // Disconnect on GATT Server and Client timeout events.
00132             // ASSERT_STATUS_RET_VOID (sd_ble_gap_disconnect(m_conn_handle,
00133             // BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION));
00134             break;
00135 
00136         default:
00137             break;
00138     }
00139 
00140     nRF51GattServer::getInstance().hwCallback(p_ble_evt);
00141 }
00142 
00143 #if NEED_BOND_MANAGER /* disabled by default */
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 #endif // #if NEED_BOND_MANAGER
00177 
00178 #if NEED_BOND_MANAGER /* disabled by default */
00179 /**************************************************************************/
00180 /*!
00181     @brief
00182     @param[in]  nrf_error
00183     @returns
00184 */
00185 /**************************************************************************/
00186 static void service_error_callback(uint32_t nrf_error)
00187 {
00188     ASSERT_STATUS_RET_VOID( nrf_error );
00189 }
00190 #endif // #if NEED_BOND_MANAGER
00191 
00192 /**************************************************************************/
00193 /*!
00194     @brief      Callback when an error occurs inside the SoftDevice
00195 
00196     @param[in]  line_num
00197     @param[in]  p-file_name
00198 
00199     @returns
00200 */
00201 /**************************************************************************/
00202 void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name)
00203 {
00204     ASSERT(false, (void) 0);
00205 }
00206 
00207 /**************************************************************************/
00208 /*!
00209     @brief      Handler for general errors above the SoftDevice layer.
00210                 Typically we can' recover from this so we do a reset.
00211 
00212     @param[in]  error_code
00213     @param[in]  line_num
00214     @param[in]  p-file_name
00215 
00216     @returns
00217 */
00218 /**************************************************************************/
00219 void app_error_handler(uint32_t       error_code,
00220                        uint32_t       line_num,
00221                        const uint8_t *p_file_name)
00222 {
00223     ASSERT_STATUS_RET_VOID( error_code );
00224     NVIC_SystemReset();
00225 }