|
|
@ -7,6 +7,7 @@ |
|
|
|
|
|
|
|
#include "ble_bas.h" |
|
|
|
#include "znordic.h" |
|
|
|
#include "basic/version.h" |
|
|
|
|
|
|
|
#if BLE_DFU_ENABLED |
|
|
|
#include "nrf_bootloader_info.h" |
|
|
@ -44,23 +45,28 @@ 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 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); |
|
|
|
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, |
|
|
@ -68,7 +74,8 @@ 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 |
|
|
@ -99,25 +106,31 @@ 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: |
|
|
|
// err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING); |
|
|
|
// ZERROR_CHECK(err_code); |
|
|
@ -130,11 +143,13 @@ static void on_adv_evt(ble_adv_evt_t ble_adv_evt) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void zble_module_start_adv() { |
|
|
|
void zble_module_start_adv() |
|
|
|
{ |
|
|
|
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() |
|
|
|
{ |
|
|
|
uint32_t err_code = sd_ble_gap_adv_stop(m_advertising.adv_handle); |
|
|
|
ZERROR_CHECK(err_code); |
|
|
|
} |
|
|
@ -157,8 +172,10 @@ 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 |
|
|
@ -201,8 +218,10 @@ 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); |
|
|
|
|
|
|
@ -216,7 +235,8 @@ NRF_SDH_STATE_OBSERVER(m_buttonless_dfu_state_obs, 0) = { |
|
|
|
.handler = buttonless_dfu_sdh_state_observer, |
|
|
|
}; |
|
|
|
|
|
|
|
static void advertising_config_get(ble_adv_modes_config_t* p_config) { |
|
|
|
static void 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; |
|
|
@ -224,13 +244,17 @@ static void advertising_config_get(ble_adv_modes_config_t* p_config) { |
|
|
|
p_config->ble_adv_fast_timeout = APP_ADV_DURATION; |
|
|
|
} |
|
|
|
|
|
|
|
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); |
|
|
|
} |
|
|
|
} |
|
|
@ -240,9 +264,12 @@ 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. |
|
|
@ -289,12 +316,23 @@ 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}}; |
|
|
|
|
|
|
|
void zble_module_init(zble_module_cfg_t* cfg) { |
|
|
|
#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] = |
|
|
|
{ |
|
|
|
(FIRMWARE_VERSION >> 8) & 0xFF, |
|
|
|
FIRMWARE_VERSION & 0xFF, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
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初始?? * |
|
|
|
*******************************************************************************/ |
|
|
@ -340,9 +378,14 @@ void zble_module_init(zble_module_cfg_t* cfg) { |
|
|
|
ble_advertising_init_t init; |
|
|
|
|
|
|
|
memset(&init, 0, sizeof(init)); |
|
|
|
static ble_advdata_manuf_data_t manuf_specific_data; |
|
|
|
manuf_specific_data.company_identifier = APP_COMPANY_IDENTIFIER; |
|
|
|
manuf_specific_data.data.p_data = (uint8_t *)p_company_identifier_data; |
|
|
|
manuf_specific_data.data.size = COMPANY_IDENTIFIER_DATA_LEGTH; |
|
|
|
|
|
|
|
init.advdata.name_type = BLE_ADVDATA_FULL_NAME; |
|
|
|
init.advdata.include_appearance = true; |
|
|
|
init.advdata.p_manuf_specific_data = &manuf_specific_data; |
|
|
|
// init.advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE; |
|
|
|
init.advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE; |
|
|
|
|
|
|
@ -375,7 +418,8 @@ 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 |
|
|
|
|
|
|
|