@ -5,9 +5,9 @@
# include <stdint.h>
# include <string.h>
# include "aproject_config/config.h"
# include "ble_bas.h"
# include "znordic.h"
# include "aproject_config/config.h"
# if BLE_DFU_ENABLED
# include "nrf_bootloader_info.h"
@ -31,7 +31,9 @@ NRF_BLE_GATT_DEF(m_gatt); /**< GATT module instance. */
/*******************************************************************************
* 连 接 参 数 配 置 *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# define FIRST_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(5000) /**< Time from initiating event (connect or start of notification) to first time sd_ble_gap_conn_param_update is called (5 seconds). */
# define FIRST_CONN_PARAMS_UPDATE_DELAY \
APP_TIMER_TICKS ( 5000 ) /**< Time from initiating event (connect or start of notification) to first time sd_ble_gap_conn_param_update is called (5 seconds). \
*/
# define NEXT_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(30000) /**< Time between each call to sd_ble_gap_conn_param_update after the first call (30 seconds). */
# define MAX_CONN_PARAMS_UPDATE_COUNT 3 /**< Number of attempts before giving up the connection parameter negotiation. */
@ -42,29 +44,26 @@ NRF_BLE_GATT_DEF(m_gatt); /**< GATT module instance. */
static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID ; /**< 当前连接句柄 */
static uint16_t m_mtu_size = BLE_GATT_ATT_MTU_DEFAULT - 3 ;
static zble_event_listener_t m_event_listener ;
static uint32_t m_last_disconnected_tp ;
static void ble_evt_handler ( ble_evt_t const * p_ble_evt , void * p_context )
{
static void ble_evt_handler ( ble_evt_t const * p_ble_evt , void * p_context ) {
uint32_t err_code ;
static zble_event_t zevent ;
switch ( p_ble_evt - > header . evt_id )
{
switch ( p_ble_evt - > header . evt_id ) {
case BLE_GAP_EVT_CONNECTED :
m_conn_handle = p_ble_evt - > evt . gap_evt . conn_handle ;
zevent . eventType = kzble_event_connected ;
if ( m_event_listener )
m_event_listener ( & zevent ) ;
if ( m_event_listener ) m_event_listener ( & zevent ) ;
break ;
case BLE_GAP_EVT_DISCONNECTED :
m_conn_handle = BLE_CONN_HANDLE_INVALID ;
zevent . eventType = kzble_event_disconnected ;
if ( m_event_listener )
m_event_listener ( & zevent ) ;
m_last_disconnected_tp = znordic_getpower_on_ms ( ) ;
if ( m_event_listener ) m_event_listener ( & zevent ) ;
break ;
case BLE_GAP_EVT_PHY_UPDATE_REQUEST :
{
case BLE_GAP_EVT_PHY_UPDATE_REQUEST : {
NRF_LOG_DEBUG ( " PHY update request. " ) ;
ble_gap_phys_t const phys = {
. rx_phys = BLE_GAP_PHY_AUTO ,
@ -72,8 +71,7 @@ static void ble_evt_handler(ble_evt_t const *p_ble_evt, void *p_context)
} ;
err_code = sd_ble_gap_phy_update ( p_ble_evt - > evt . gap_evt . conn_handle , & phys ) ;
ZERROR_CHECK ( err_code ) ;
}
break ;
} break ;
case BLE_GAP_EVT_SEC_PARAMS_REQUEST :
/ / Pairing not supported
@ -104,32 +102,25 @@ static void ble_evt_handler(ble_evt_t const *p_ble_evt, void *p_context)
break ;
}
}
static void gatt_evt_handler ( nrf_ble_gatt_t * p_gatt , nrf_ble_gatt_evt_t const * p_evt )
{
static void gatt_evt_handler ( nrf_ble_gatt_t * p_gatt , nrf_ble_gatt_evt_t const * p_evt ) {
/*******************************************************************************
* MTU SIZE 更 新 *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
if ( ( m_conn_handle = = p_evt - > conn_handle ) & & ( p_evt - > evt_id = = NRF_BLE_GATT_EVT_ATT_MTU_UPDATED ) )
{
if ( ( m_conn_handle = = p_evt - > conn_handle ) & & ( p_evt - > evt_id = = NRF_BLE_GATT_EVT_ATT_MTU_UPDATED ) ) {
m_mtu_size = p_evt - > params . att_mtu_effective - OPCODE_LENGTH - HANDLE_LENGTH ;
NRF_LOG_DEBUG ( " ATT MTU exchange completed. central %d peripheral %d " , p_gatt - > att_mtu_desired_central , p_gatt - > att_mtu_desired_periph ) ;
}
}
static void on_conn_params_evt ( ble_conn_params_evt_t * p_evt )
{
static void on_conn_params_evt ( ble_conn_params_evt_t * p_evt ) {
uint32_t err_code ;
if ( p_evt - > evt_type = = BLE_CONN_PARAMS_EVT_FAILED )
{
if ( p_evt - > evt_type = = BLE_CONN_PARAMS_EVT_FAILED ) {
err_code = sd_ble_gap_disconnect ( m_conn_handle , BLE_HCI_CONN_INTERVAL_UNACCEPTABLE ) ;
ZERROR_CHECK ( err_code ) ;
}
}
static void conn_params_error_handler ( uint32_t nrf_error ) { APP_ERROR_HANDLER ( nrf_error ) ; }
static void on_adv_evt ( ble_adv_evt_t ble_adv_evt )
{
switch ( ble_adv_evt )
{
static void on_adv_evt ( ble_adv_evt_t ble_adv_evt ) {
switch ( ble_adv_evt ) {
case BLE_ADV_EVT_FAST :
ZLOGI ( " Fast advertising " ) ;
break ;
@ -145,14 +136,12 @@ static void on_adv_evt(ble_adv_evt_t ble_adv_evt)
}
}
void zble_module_start_adv ( )
{
void zble_module_start_adv ( ) {
sd_ble_gap_adv_stop ( m_advertising . adv_handle ) ;
uint32_t err_code = ble_advertising_start ( & m_advertising , BLE_ADV_MODE_FAST ) ;
ZERROR_CHECK ( err_code ) ;
}
void zble_module_stop_adv ( )
{
void zble_module_stop_adv ( ) {
sd_ble_gap_adv_stop ( m_advertising . adv_handle ) ;
/ / ZERROR_CHECK ( err_code ) ;
}
@ -175,10 +164,8 @@ void zble_module_reglistener(zble_event_listener_t event_listener) { m_event_lis
*
* @ retval True if shutdown is allowed by this power manager handler , otherwise false .
*/
static bool app_shutdown_handler ( nrf_pwr_mgmt_evt_t event )
{
switch ( event )
{
static bool app_shutdown_handler ( nrf_pwr_mgmt_evt_t event ) {
switch ( event ) {
case NRF_PWR_MGMT_EVT_PREPARE_DFU :
NRF_LOG_INFO ( " Power management wants to reset to DFU mode. " ) ;
/ / YOUR_JOB : Get ready to reset into DFU mode
@ -221,10 +208,8 @@ static bool app_shutdown_handler(nrf_pwr_mgmt_evt_t event)
*/
NRF_PWR_MGMT_HANDLER_REGISTER ( app_shutdown_handler , 0 ) ;
static void buttonless_dfu_sdh_state_observer ( nrf_sdh_state_evt_t state , void * p_context )
{
if ( state = = NRF_SDH_EVT_STATE_DISABLED )
{
static void buttonless_dfu_sdh_state_observer ( nrf_sdh_state_evt_t state , void * p_context ) {
if ( state = = NRF_SDH_EVT_STATE_DISABLED ) {
/ / Softdevice was disabled before going into reset . Inform bootloader to skip CRC on next boot .
nrf_power_gpregret2_set ( BOOTLOADER_DFU_SKIP_CRC ) ;
@ -238,8 +223,7 @@ NRF_SDH_STATE_OBSERVER(m_buttonless_dfu_state_obs, 0) = {
. handler = buttonless_dfu_sdh_state_observer ,
} ;
static void dfu_advertising_config_get ( ble_adv_modes_config_t * p_config )
{
static void dfu_advertising_config_get ( ble_adv_modes_config_t * p_config ) {
memset ( p_config , 0 , sizeof ( ble_adv_modes_config_t ) ) ;
p_config - > ble_adv_fast_enabled = true ;
@ -247,17 +231,13 @@ static void dfu_advertising_config_get(ble_adv_modes_config_t *p_config)
p_config - > ble_adv_fast_timeout = 3000 ; / / 30 s
}
static void disconnect ( uint16_t conn_handle , void * p_context )
{
static void disconnect ( uint16_t conn_handle , void * p_context ) {
UNUSED_PARAMETER ( p_context ) ;
ret_code_t err_code = sd_ble_gap_disconnect ( conn_handle , BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION ) ;
if ( err_code ! = NRF_SUCCESS )
{
if ( err_code ! = NRF_SUCCESS ) {
NRF_LOG_WARNING ( " Failed to disconnect connection. Connection handle: %d Error: %d " , conn_handle , err_code ) ;
}
else
{
} else {
NRF_LOG_DEBUG ( " Disconnected connection handle %d " , conn_handle ) ;
}
}
@ -267,12 +247,9 @@ static void disconnect(uint16_t conn_handle, void *p_context)
*
* @ param [ in ] event Event from the Buttonless Secure DFU service .
*/
static void ble_dfu_evt_handler ( ble_dfu_buttonless_evt_type_t event )
{
switch ( event )
{
case BLE_DFU_EVT_BOOTLOADER_ENTER_PREPARE :
{
static void ble_dfu_evt_handler ( ble_dfu_buttonless_evt_type_t event ) {
switch ( event ) {
case BLE_DFU_EVT_BOOTLOADER_ENTER_PREPARE : {
NRF_LOG_INFO ( " Device is preparing to enter bootloader mode. " ) ;
/ / Prevent device from advertising on disconnect .
@ -320,22 +297,18 @@ static void ble_dfu_evt_handler(ble_dfu_buttonless_evt_type_t event)
/ / static ble_uuid_t m_adv_uuids [ ] = { { BLE_UUID_DEVICE_INFORMATION_SERVICE , BLE_UUID_TYPE_BLE } } ;
# define COMPANY_IDENTIFIER_DATA_LEGTH 2 /**< Total length of information advertised by the Beacon. */
static uint8_t p_company_identifier_data [ COMPANY_IDENTIFIER_DATA_LEGTH ] =
{
static uint8_t p_company_identifier_data [ COMPANY_IDENTIFIER_DATA_LEGTH ] = {
( FIRMWARE_VERSION > > 8 ) & 0xFF ,
FIRMWARE_VERSION & 0xFF ,
} ;
void zble_module_init ( zble_module_cfg_t * cfg )
{
void zble_module_init ( zble_module_cfg_t * cfg ) {
/**
* @ brief
* 初 始 化 蓝 牙 协 议 栈 , 并 注 册 蓝 牙 事 件 处 理 函 数 , 固 定 代 码 , 勿 修 ? ?
*/
{
NRF_SDH_BLE_OBSERVER ( m_ble_observer , 3 , ble_evt_handler , NULL ) ;
}
{ NRF_SDH_BLE_OBSERVER ( m_ble_observer , 3 , ble_evt_handler , NULL ) ; }
/*******************************************************************************
* GAP初始 ? ? *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
@ -401,7 +374,6 @@ void zble_module_init(zble_module_cfg_t *cfg)
init . srdata . uuids_complete . uuid_cnt = 0 ;
init . srdata . uuids_complete . p_uuids = NULL ;
init . config . ble_adv_fast_enabled = true ;
init . config . ble_adv_fast_interval = 64 ; / /*0.625ms
init . config . ble_adv_fast_timeout = 100 ; / /*10ms
@ -427,8 +399,7 @@ void zble_module_init(zble_module_cfg_t *cfg)
dfus_init . evt_handler = ble_dfu_evt_handler ;
ret_code_t err_code = ble_dfu_buttonless_init ( & dfus_init ) ;
ZERROR_CHECK ( err_code ) ;
if ( cfg - > on_service_init )
cfg - > on_service_init ( ) ;
if ( cfg - > on_service_init ) cfg - > on_service_init ( ) ;
}
# endif
@ -463,3 +434,10 @@ bool zble_module_disconnect(){
ble_conn_state_for_each_connected ( disconnect , NULL ) ;
return true ;
}
uint32_t zble_module_has_disconnected_ms ( ) {
if ( m_conn_handle ! = BLE_CONN_HANDLE_INVALID ) {
return 0 ;
}
return znordic_haspassed_ms ( m_last_disconnected_tp ) ;
}