changed low freq. clock source to IRC

Dependents:   BLE_ANCS_SDAPI_IRC

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