44 #include "nrf_dfu_ble_svci_bond_sharing.h"
45 #include "nordic_common.h"
46 #include "nrf_error.h"
49 #include "peer_manager.h"
50 #include "gatts_cache_manager.h"
52 #include "nrf_sdh_soc.h"
53 #include "nrf_strerror.h"
54 #if NRF_MODULE_ENABLED(BLE_DFU)
55 #if (NRF_DFU_BLE_BUTTONLESS_SUPPORTS_BONDS)
58 void ble_dfu_buttonless_on_sys_evt(uint32_t,
void * );
59 uint32_t nrf_dfu_svci_vector_table_set(
void);
60 uint32_t nrf_dfu_svci_vector_table_unset(
void);
63 NRF_SVCI_ASYNC_FUNC_DEFINE(NRF_DFU_SVCI_SET_PEER_DATA, nrf_dfu_set_peer_data, nrf_dfu_peer_data_t);
68 ble_dfu_buttonless_t * mp_dfu;
69 static nrf_dfu_peer_data_t m_peer_data;
76 static void pm_evt_handler(pm_evt_t
const * p_evt)
86 if (!mp_dfu->is_waiting_for_reset)
93 case PM_EVT_PEER_DATA_UPDATE_SUCCEEDED:
94 if (p_evt->params.peer_data_update_succeeded.data_id == PM_PEER_DATA_ID_SERVICE_CHANGED_PENDING)
96 mp_dfu->peers_count--;
97 NRF_LOG_DEBUG(
"Updating Service Changed indication for peers, %d left", mp_dfu->peers_count);
98 if (mp_dfu->peers_count == 0)
100 NRF_LOG_DEBUG(
"Finished updating Service Changed indication for peers");
102 ret = ble_dfu_buttonless_bootloader_start_finalize();
103 if (ret != NRF_SUCCESS)
105 mp_dfu->evt_handler(BLE_DFU_EVT_BOOTLOADER_ENTER_FAILED);
111 case PM_EVT_PEER_DATA_UPDATE_FAILED:
113 ret = ble_dfu_buttonless_bootloader_start_finalize();
114 if (ret != NRF_SUCCESS)
116 mp_dfu->evt_handler(BLE_DFU_EVT_BOOTLOADER_ENTER_FAILED);
126 static uint32_t retrieve_peer_data(
void)
129 pm_peer_data_bonding_t bonding_data = {0};
130 pm_peer_id_t peer_id;
132 ret = pm_peer_id_get(mp_dfu->conn_handle, &peer_id);
135 if (peer_id == PM_PEER_ID_INVALID)
137 return NRF_ERROR_FORBIDDEN;
140 ret = pm_peer_data_bonding_load(peer_id, &bonding_data);
143 memcpy(&m_peer_data.ble_id, &bonding_data.peer_ble_id,
sizeof(ble_gap_id_key_t));
144 memcpy(&m_peer_data.enc_key, &bonding_data.own_ltk,
sizeof(ble_gap_enc_key_t));
146 uint16_t len = SYSTEM_SERVICE_ATT_SIZE;
147 ret = sd_ble_gatts_sys_attr_get(mp_dfu->conn_handle,
148 m_peer_data.sys_serv_attr,
150 BLE_GATTS_SYS_ATTR_FLAG_SYS_SRVCS);
152 NRF_LOG_DEBUG(
"system attribute table len: %d", len);
162 static uint32_t enter_bootloader(
void)
166 NRF_LOG_INFO(
"Writing peer data to the bootloader...");
168 if (mp_dfu->is_waiting_for_svci)
170 return ble_dfu_buttonless_resp_send(DFU_OP_ENTER_BOOTLOADER, DFU_RSP_BUSY);
174 ret = retrieve_peer_data();
177 ret = nrf_dfu_set_peer_data(&m_peer_data);
178 if (ret == NRF_SUCCESS)
181 mp_dfu->is_waiting_for_svci =
true;
183 else if (ret == NRF_ERROR_FORBIDDEN)
185 NRF_LOG_ERROR(
"The bootloader has write protected its settings page. This prohibits setting the peer data. "\
186 "The bootloader must be compiled with NRF_BL_SETTINGS_PAGE_PROTECT=0 to allow setting the peer data.");
193 uint32_t ble_dfu_buttonless_backend_init(ble_dfu_buttonless_t * p_dfu)
195 VERIFY_PARAM_NOT_NULL(p_dfu);
201 return pm_register(&pm_evt_handler);
205 uint32_t ble_dfu_buttonless_async_svci_init(
void)
210 ret = nrf_dfu_svci_vector_table_set();
211 NRF_LOG_DEBUG(
"nrf_dfu_svci_vector_table_set() -> %s",
212 (ret == NRF_SUCCESS) ?
"success" : nrf_strerror_get(ret));
216 ret = nrf_dfu_set_peer_data_init();
217 NRF_LOG_DEBUG(
"nrf_dfu_set_peer_data_init() -> %s",
218 (ret == NRF_SUCCESS) ?
"success" : nrf_strerror_get(ret));
222 ret = nrf_dfu_svci_vector_table_unset();
223 NRF_LOG_DEBUG(
"nrf_dfu_svci_vector_table_unset() -> %s",
224 (ret == NRF_SUCCESS) ?
"success" : nrf_strerror_get(ret));
230 void ble_dfu_buttonless_on_sys_evt(uint32_t sys_evt,
void * p_context)
234 if (!nrf_dfu_set_peer_data_is_initialized())
239 ret = nrf_dfu_set_peer_data_on_sys_evt(sys_evt);
240 if (ret == NRF_ERROR_INVALID_STATE)
245 else if (ret == NRF_SUCCESS)
250 mp_dfu->is_waiting_for_reset =
true;
251 mp_dfu->is_waiting_for_svci =
false;
254 ret = ble_dfu_buttonless_resp_send(DFU_OP_ENTER_BOOTLOADER, DFU_RSP_SUCCESS);
255 if (ret != NRF_SUCCESS)
257 mp_dfu->evt_handler(BLE_DFU_EVT_RESPONSE_SEND_ERROR);
258 mp_dfu->is_waiting_for_reset =
false;
264 mp_dfu->is_waiting_for_reset =
false;
265 mp_dfu->is_waiting_for_svci =
false;
266 ret = ble_dfu_buttonless_resp_send(DFU_OP_ENTER_BOOTLOADER, DFU_RSP_BUSY);
269 if (ret != NRF_SUCCESS)
271 mp_dfu->evt_handler(BLE_DFU_EVT_RESPONSE_SEND_ERROR);
275 mp_dfu->evt_handler(BLE_DFU_EVT_BOOTLOADER_ENTER_FAILED);
280 uint32_t ble_dfu_buttonless_char_add(ble_dfu_buttonless_t * p_dfu)
282 ble_add_char_params_t add_char_params;
284 memset(&add_char_params, 0,
sizeof(add_char_params));
285 add_char_params.uuid = BLE_DFU_BUTTONLESS_BONDED_CHAR_UUID;
286 add_char_params.uuid_type = p_dfu->uuid_type;
287 add_char_params.char_props.indicate = 1;
288 add_char_params.char_props.write = 1;
289 add_char_params.is_defered_write =
true;
290 add_char_params.is_var_len =
true;
291 add_char_params.max_len = BLE_GATT_ATT_MTU_DEFAULT;
293 add_char_params.cccd_write_access = SEC_JUST_WORKS;
294 add_char_params.write_access = SEC_JUST_WORKS;
295 add_char_params.read_access = SEC_OPEN;
297 return characteristic_add(p_dfu->service_handle, &add_char_params, &p_dfu->control_point_char);
301 void ble_dfu_buttonless_on_ctrl_pt_write(ble_gatts_evt_write_t
const * p_evt_write)
304 ble_dfu_buttonless_rsp_code_t rsp_code = DFU_RSP_OPERATION_FAILED;
307 switch (p_evt_write->data[0])
309 case DFU_OP_ENTER_BOOTLOADER:
310 ret = enter_bootloader();
311 if (ret == NRF_SUCCESS)
313 rsp_code = DFU_RSP_SUCCESS;
315 else if (ret == NRF_ERROR_BUSY)
317 rsp_code = DFU_RSP_BUSY;
319 else if (ret == NRF_ERROR_FORBIDDEN)
321 rsp_code = DFU_RSP_NOT_BONDED;
326 rsp_code = DFU_RSP_OP_CODE_NOT_SUPPORTED;
331 if (rsp_code != DFU_RSP_SUCCESS)
333 ret = ble_dfu_buttonless_resp_send((ble_dfu_buttonless_op_code_t)p_evt_write->data[0],
336 if (ret != NRF_SUCCESS)
338 mp_dfu->evt_handler(BLE_DFU_EVT_RESPONSE_SEND_ERROR);
342 mp_dfu->evt_handler(BLE_DFU_EVT_BOOTLOADER_ENTER_FAILED);
347 uint32_t ble_dfu_buttonless_bootloader_start_prepare(
void)
349 NRF_LOG_DEBUG(
"In ble_dfu_buttonless_bootloader_start_prepare");
354 mp_dfu->evt_handler(BLE_DFU_EVT_BOOTLOADER_ENTER_PREPARE);
357 mp_dfu->peers_count = peer_id_n_ids();
361 gscm_local_database_has_changed();
#define BLE_DFU_SOC_OBSERVER_PRIO