44 #include "nrf_dfu_ble_svci_bond_sharing.h"
45 #include "nordic_common.h"
46 #include "nrf_error.h"
49 #include "nrf_sdh_soc.h"
50 #if NRF_MODULE_ENABLED(BLE_DFU)
52 #if (!NRF_DFU_BLE_BUTTONLESS_SUPPORTS_BONDS)
54 #define NRF_DFU_ADV_NAME_MAX_LENGTH (20)
57 void ble_dfu_buttonless_on_sys_evt(uint32_t,
void * );
58 uint32_t nrf_dfu_svci_vector_table_set(
void);
59 uint32_t nrf_dfu_svci_vector_table_unset(
void);
62 NRF_SVCI_ASYNC_FUNC_DEFINE(NRF_DFU_SVCI_SET_ADV_NAME, nrf_dfu_set_adv_name, nrf_dfu_adv_name_t);
67 ble_dfu_buttonless_t * mp_dfu = NULL;
68 static nrf_dfu_adv_name_t m_adv_name;
79 static uint32_t set_adv_name(nrf_dfu_adv_name_t * p_adv_name)
83 if (mp_dfu->is_waiting_for_svci)
88 err_code = nrf_dfu_set_adv_name(p_adv_name);
89 if (err_code == NRF_SUCCESS)
92 mp_dfu->is_waiting_for_svci =
true;
94 else if (err_code == NRF_ERROR_FORBIDDEN)
96 NRF_LOG_ERROR(
"The bootloader has write protected its settings page. This prohibits setting the advertising name. "\
97 "The bootloader must be compiled with NRF_BL_SETTINGS_PAGE_PROTECT=0 to allow setting the advertising name.");
106 static uint32_t enter_bootloader()
110 if (mp_dfu->is_waiting_for_svci)
113 err_code = ble_dfu_buttonless_resp_send(DFU_OP_ENTER_BOOTLOADER, DFU_RSP_BUSY);
114 if (err_code != NRF_SUCCESS)
116 mp_dfu->evt_handler(BLE_DFU_EVT_RESPONSE_SEND_ERROR);
123 mp_dfu->is_waiting_for_reset =
true;
125 err_code = ble_dfu_buttonless_resp_send(DFU_OP_ENTER_BOOTLOADER, DFU_RSP_SUCCESS);
126 if (err_code != NRF_SUCCESS)
128 mp_dfu->is_waiting_for_reset =
false;
135 uint32_t ble_dfu_buttonless_backend_init(ble_dfu_buttonless_t * p_dfu)
137 VERIFY_PARAM_NOT_NULL(p_dfu);
145 uint32_t ble_dfu_buttonless_async_svci_init(
void)
148 CRITICAL_REGION_ENTER();
149 ret_val = nrf_dfu_svci_vector_table_set();
151 if(NRF_SUCCESS == ret_val)
153 ret_val = nrf_dfu_set_adv_name_init();
155 if(NRF_SUCCESS == ret_val)
157 ret_val = nrf_dfu_svci_vector_table_unset();
160 CRITICAL_REGION_EXIT();
165 void ble_dfu_buttonless_on_sys_evt(uint32_t sys_evt,
void * p_context)
169 if (!nrf_dfu_set_adv_name_is_initialized())
174 err_code = nrf_dfu_set_adv_name_on_sys_evt(sys_evt);
175 if (err_code == NRF_ERROR_INVALID_STATE)
180 else if (err_code == NRF_SUCCESS)
185 mp_dfu->is_waiting_for_svci =
false;
188 err_code = ble_dfu_buttonless_resp_send(DFU_OP_SET_ADV_NAME, DFU_RSP_SUCCESS);
189 if (err_code != NRF_SUCCESS)
191 mp_dfu->evt_handler(BLE_DFU_EVT_RESPONSE_SEND_ERROR);
197 mp_dfu->is_waiting_for_svci =
false;
199 err_code = ble_dfu_buttonless_resp_send(DFU_OP_SET_ADV_NAME, DFU_RSP_BUSY);
200 if (err_code != NRF_SUCCESS)
202 mp_dfu->evt_handler(BLE_DFU_EVT_RESPONSE_SEND_ERROR);
206 mp_dfu->evt_handler(BLE_DFU_EVT_BOOTLOADER_ENTER_FAILED);
211 uint32_t ble_dfu_buttonless_char_add(ble_dfu_buttonless_t * p_dfu)
213 ble_add_char_params_t add_char_params;
215 memset(&add_char_params, 0,
sizeof(add_char_params));
216 add_char_params.uuid = BLE_DFU_BUTTONLESS_CHAR_UUID;
217 add_char_params.uuid_type = p_dfu->uuid_type;
218 add_char_params.char_props.indicate = 1;
219 add_char_params.char_props.write = 1;
220 add_char_params.is_defered_write =
true;
221 add_char_params.is_var_len =
true;
222 add_char_params.max_len = BLE_GATT_ATT_MTU_DEFAULT;
224 add_char_params.cccd_write_access = SEC_OPEN;
225 add_char_params.write_access = SEC_OPEN;
226 add_char_params.read_access = SEC_OPEN;
228 return characteristic_add(p_dfu->service_handle, &add_char_params, &p_dfu->control_point_char);
232 void ble_dfu_buttonless_on_ctrl_pt_write(ble_gatts_evt_write_t
const * p_evt_write)
235 ble_dfu_buttonless_rsp_code_t rsp_code = DFU_RSP_OPERATION_FAILED;
239 switch (p_evt_write->data[0])
241 case DFU_OP_ENTER_BOOTLOADER:
242 err_code = enter_bootloader();
243 if (err_code == NRF_SUCCESS)
245 rsp_code = DFU_RSP_SUCCESS;
247 else if (err_code == NRF_ERROR_BUSY)
249 rsp_code = DFU_RSP_BUSY;
253 case DFU_OP_SET_ADV_NAME:
254 if( (p_evt_write->data[1] > NRF_DFU_ADV_NAME_MAX_LENGTH)
255 || (p_evt_write->data[1] == 0))
258 rsp_code = DFU_RSP_ADV_NAME_INVALID;
262 memcpy(m_adv_name.name, &p_evt_write->data[2], p_evt_write->data[1]);
263 m_adv_name.len = p_evt_write->data[1];
264 err_code = set_adv_name(&m_adv_name);
265 if (err_code == NRF_SUCCESS)
267 rsp_code = DFU_RSP_SUCCESS;
273 rsp_code = DFU_RSP_OP_CODE_NOT_SUPPORTED;
280 if (rsp_code != DFU_RSP_SUCCESS)
282 err_code = ble_dfu_buttonless_resp_send((ble_dfu_buttonless_op_code_t)p_evt_write->data[0], rsp_code);
283 if (err_code != NRF_SUCCESS)
285 mp_dfu->evt_handler(BLE_DFU_EVT_RESPONSE_SEND_ERROR);
289 mp_dfu->evt_handler(BLE_DFU_EVT_BOOTLOADER_ENTER_FAILED);
293 uint32_t ble_dfu_buttonless_bootloader_start_prepare(
void)
298 mp_dfu->evt_handler(BLE_DFU_EVT_BOOTLOADER_ENTER_PREPARE);
300 err_code = ble_dfu_buttonless_bootloader_start_finalize();
#define BLE_DFU_SOC_OBSERVER_PRIO