ghz 2000 / nRF51822_BLE_UART_HRM1017

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 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,
00042                               uint32_t       line_num,
00043                               const uint8_t *p_file_name);
00044 
00045 static error_t bond_manager_init(void);
00046 
00047 static void btle_handler(ble_evt_t *p_ble_evt);
00048 
00049 /**************************************************************************/
00050 /*!
00051 
00052 */
00053 /**************************************************************************/
00054 static void sys_evt_dispatch(uint32_t sys_evt)
00055 {
00056     pstorage_sys_event_handler(sys_evt);
00057 }
00058 
00059 /**************************************************************************/
00060 /*!
00061     @brief      Initialises BTLE and the underlying HW/SoftDevice
00062 
00063     @returns
00064 */
00065 /**************************************************************************/
00066 error_t btle_init(void)
00067 {
00068     APP_TIMER_INIT(0, 8, 5, false);
00069 //  SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, false);
00070   SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_RC_250_PPM_4000MS_CALIBRATION, false);
00071 
00072     ASSERT_STATUS( softdevice_ble_evt_handler_set(btle_handler));
00073     ASSERT_STATUS( softdevice_sys_evt_handler_set(sys_evt_dispatch));
00074 
00075     bond_manager_init();
00076     btle_gap_init();
00077 
00078     return ERROR_NONE;
00079 }
00080 
00081 /**************************************************************************/
00082 /*!
00083     @brief
00084 
00085     @param[in]  p_ble_evt
00086 
00087     @returns
00088 */
00089 /**************************************************************************/
00090 static void btle_handler(ble_evt_t *p_ble_evt)
00091 {
00092     /* Library service handlers */
00093     ble_bondmngr_on_ble_evt(p_ble_evt);
00094     ble_conn_params_on_ble_evt(p_ble_evt);
00095 
00096     /* Custom event handler */
00097     switch (p_ble_evt->header.evt_id) {
00098         case BLE_GAP_EVT_CONNECTED:
00099             nRF51Gap::getInstance().setConnectionHandle(
00100                 p_ble_evt->evt.gap_evt.conn_handle );
00101             nRF51Gap::getInstance().handleEvent(GapEvents::GAP_EVENT_CONNECTED);
00102             break;
00103 
00104         case BLE_GAP_EVT_DISCONNECTED:
00105             // Since we are not in a connection and have not started advertising,
00106             // store bonds
00107             nRF51Gap::getInstance().setConnectionHandle (BLE_CONN_HANDLE_INVALID);
00108             ASSERT_STATUS_RET_VOID ( ble_bondmngr_bonded_centrals_store());
00109             nRF51Gap::getInstance().handleEvent(GapEvents::GAP_EVENT_DISCONNECTED);
00110             break;
00111 
00112         case BLE_GAP_EVT_SEC_PARAMS_REQUEST: {
00113             ble_gap_sec_params_t sec_params = {0};
00114 
00115             sec_params.timeout      = 30; /*< Timeout for Pairing Request or
00116                                    * Security Request (in seconds). */
00117             sec_params.bond         = 1;  /**< Perform bonding. */
00118             sec_params.mitm         = CFG_BLE_SEC_PARAM_MITM;
00119             sec_params.io_caps      = CFG_BLE_SEC_PARAM_IO_CAPABILITIES;
00120             sec_params.oob          = CFG_BLE_SEC_PARAM_OOB;
00121             sec_params.min_key_size = CFG_BLE_SEC_PARAM_MIN_KEY_SIZE;
00122             sec_params.max_key_size = CFG_BLE_SEC_PARAM_MAX_KEY_SIZE;
00123 
00124             ASSERT_STATUS_RET_VOID(
00125                 sd_ble_gap_sec_params_reply(nRF51Gap::getInstance().
00126                                             getConnectionHandle(),
00127                                             BLE_GAP_SEC_STATUS_SUCCESS,
00128                                             &sec_params));
00129         }
00130         break;
00131 
00132         case BLE_GAP_EVT_TIMEOUT:
00133             if (p_ble_evt->evt.gap_evt.params.timeout.src ==
00134                     BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT) {
00135                 nRF51Gap::getInstance().handleEvent(GapEvents::GAP_EVENT_TIMEOUT);
00136             }
00137             break;
00138 
00139         case BLE_GATTC_EVT_TIMEOUT:
00140         case BLE_GATTS_EVT_TIMEOUT:
00141             // Disconnect on GATT Server and Client timeout events.
00142             // ASSERT_STATUS_RET_VOID (sd_ble_gap_disconnect(m_conn_handle,
00143             // BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION));
00144             break;
00145 
00146         default:
00147             break;
00148     }
00149 
00150     nRF51GattServer::getInstance().hwCallback(p_ble_evt);
00151 }
00152 
00153 /**************************************************************************/
00154 /*!
00155     @brief      Initialises the bond manager
00156 
00157     @note       Bond data will be cleared on reset if the bond delete
00158                 button is pressed during initialisation (the button is
00159                 defined as CFG_BLE_BOND_DELETE_BUTTON_NUM).
00160 
00161     @returns
00162 */
00163 /**************************************************************************/
00164 static error_t bond_manager_init(void)
00165 {
00166     ble_bondmngr_init_t bond_para = {0};
00167 
00168     ASSERT_STATUS ( pstorage_init());
00169 
00170     bond_para.flash_page_num_bond     = CFG_BLE_BOND_FLASH_PAGE_BOND;
00171     bond_para.flash_page_num_sys_attr = CFG_BLE_BOND_FLASH_PAGE_SYS_ATTR;
00172     //bond_para.bonds_delete            = boardButtonCheck(CFG_BLE_BOND_DELETE_BUTTON_NUM) ;
00173     bond_para.evt_handler   = NULL;
00174     bond_para.error_handler = service_error_callback;
00175 
00176     ASSERT_STATUS( ble_bondmngr_init( &bond_para ));
00177 
00178     /* Init radio active/inactive notification to flash (to only perform flashing when the radio is inactive) */
00179     //  ASSERT_STATUS( ble_radio_notification_init(NRF_APP_PRIORITY_HIGH,
00180     //                                             NRF_RADIO_NOTIFICATION_DISTANCE_4560US,
00181     //                                             ble_flash_on_radio_active_evt) );
00182 
00183     return ERROR_NONE;
00184 }
00185 
00186 /**************************************************************************/
00187 /*!
00188     @brief
00189     @param[in]  nrf_error
00190     @returns
00191 */
00192 /**************************************************************************/
00193 static void service_error_callback(uint32_t nrf_error)
00194 {
00195     ASSERT_STATUS_RET_VOID( nrf_error );
00196 }
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 }