48 #include "sdk_macros.h"
49 #include "ble_srv_common.h"
54 #include "nrf_dfu_ble_svci_bond_sharing.h"
55 #include "nrf_bootloader_info.h"
56 #include "nrf_svci_async_function.h"
57 #include "nrf_pwr_mgmt.h"
58 #include "peer_manager.h"
59 #include "gatts_cache_manager.h"
61 #if NRF_MODULE_ENABLED(BLE_DFU)
63 #define MAX_CTRL_POINT_RESP_PARAM_LEN 3
65 #define BLE_DFU_SERVICE_UUID 0xFE59
67 static ble_dfu_buttonless_t m_dfu;
74 static void dummy_evt_handler(ble_dfu_buttonless_evt_type_t evt)
76 NRF_LOG_DEBUG(
"Dummy event handler received event 0x%x", evt);
84 static void on_ctrlpt_write(ble_gatts_evt_write_t
const * p_evt_write)
89 ble_gatts_rw_authorize_reply_params_t write_authorize_reply;
90 memset(&write_authorize_reply, 0,
sizeof(write_authorize_reply));
92 write_authorize_reply.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE;
94 if (m_dfu.is_ctrlpt_indication_enabled)
96 write_authorize_reply.params.write.update = 1;
97 write_authorize_reply.params.write.gatt_status = BLE_GATT_STATUS_SUCCESS;
101 write_authorize_reply.params.write.gatt_status = BLE_GATT_STATUS_ATTERR_CPS_CCCD_CONFIG_ERROR;
106 err_code = sd_ble_gatts_rw_authorize_reply(m_dfu.conn_handle, &write_authorize_reply);
107 }
while (err_code == NRF_ERROR_BUSY);
110 if (write_authorize_reply.params.write.gatt_status != BLE_GATT_STATUS_SUCCESS)
116 ble_dfu_buttonless_on_ctrl_pt_write(p_evt_write);
126 static void on_rw_authorize_req(ble_evt_t
const * p_ble_evt)
128 if (p_ble_evt->evt.gatts_evt.conn_handle != m_dfu.conn_handle)
133 const ble_gatts_evt_rw_authorize_request_t * p_auth_req =
134 &p_ble_evt->evt.gatts_evt.params.authorize_request;
137 (p_auth_req->type == BLE_GATTS_AUTHORIZE_TYPE_WRITE) &&
138 (p_auth_req->request.write.handle == m_dfu.control_point_char.value_handle) &&
139 (p_auth_req->request.write.op != BLE_GATTS_OP_PREP_WRITE_REQ) &&
140 (p_auth_req->request.write.op != BLE_GATTS_OP_EXEC_WRITE_REQ_NOW) &&
141 (p_auth_req->request.write.op != BLE_GATTS_OP_EXEC_WRITE_REQ_CANCEL)
144 on_ctrlpt_write(&p_auth_req->request.write);
153 static void on_connect(ble_evt_t
const * p_ble_evt)
155 m_dfu.conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
163 static void on_disconnect(ble_evt_t
const * p_ble_evt)
165 if (m_dfu.conn_handle != p_ble_evt->evt.gap_evt.conn_handle)
170 m_dfu.conn_handle = BLE_CONN_HANDLE_INVALID;
178 static void on_write(ble_evt_t
const * p_ble_evt)
180 const ble_gatts_evt_write_t * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
182 if (p_evt_write->handle != m_dfu.control_point_char.cccd_handle)
187 if (p_evt_write->len == BLE_CCCD_VALUE_LEN)
190 m_dfu.is_ctrlpt_indication_enabled = ble_srv_is_indication_enabled(p_evt_write->data);
192 NRF_LOG_INFO(
"Received indication state %d",
193 m_dfu.is_ctrlpt_indication_enabled);
204 static void on_hvc(ble_evt_t
const * p_ble_evt)
207 ble_gatts_evt_hvc_t
const * p_hvc = &p_ble_evt->evt.gatts_evt.params.hvc;
209 if (p_hvc->handle == m_dfu.control_point_char.value_handle)
212 if (m_dfu.is_waiting_for_reset)
214 err_code = ble_dfu_buttonless_bootloader_start_prepare();
215 if (err_code != NRF_SUCCESS)
217 m_dfu.evt_handler(BLE_DFU_EVT_BOOTLOADER_ENTER_FAILED);
224 void ble_dfu_buttonless_on_ble_evt(ble_evt_t
const * p_ble_evt,
void * p_context)
226 VERIFY_PARAM_NOT_NULL_VOID(p_ble_evt);
228 switch (p_ble_evt->header.evt_id)
230 case BLE_GAP_EVT_CONNECTED:
231 on_connect(p_ble_evt);
234 case BLE_GAP_EVT_DISCONNECTED:
235 on_disconnect(p_ble_evt);
238 case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST:
239 on_rw_authorize_req(p_ble_evt);
242 case BLE_GATTS_EVT_WRITE:
246 case BLE_GATTS_EVT_HVC:
257 uint32_t ble_dfu_buttonless_resp_send(ble_dfu_buttonless_op_code_t op_code, ble_dfu_buttonless_rsp_code_t rsp_code)
261 const uint16_t len = 3;
263 uint8_t hvx_data[MAX_CTRL_POINT_RESP_PARAM_LEN];
264 ble_gatts_hvx_params_t hvx_params;
266 memset(&hvx_params, 0,
sizeof(hvx_params));
269 hvx_data[0] = DFU_OP_RESPONSE_CODE;
270 hvx_data[1] = (uint8_t)op_code;
271 hvx_data[2] = (uint8_t)rsp_code;
273 hvx_params.handle = m_dfu.control_point_char.value_handle;
274 hvx_params.type = BLE_GATT_HVX_INDICATION;
275 hvx_params.offset = 0;
276 hvx_params.p_len = &hvx_len;
277 hvx_params.p_data = hvx_data;
280 err_code = sd_ble_gatts_hvx(m_dfu.conn_handle, &hvx_params);
281 if ((err_code == NRF_SUCCESS) && (hvx_len != len))
283 err_code = NRF_ERROR_DATA_SIZE;
290 uint32_t ble_dfu_buttonless_bootloader_start_finalize(
void)
294 NRF_LOG_DEBUG(
"In ble_dfu_buttonless_bootloader_start_finalize\r\n");
296 err_code = sd_power_gpregret_clr(0, 0xffffffff);
297 VERIFY_SUCCESS(err_code);
299 err_code = sd_power_gpregret_set(0, BOOTLOADER_DFU_START);
300 VERIFY_SUCCESS(err_code);
303 m_dfu.evt_handler(BLE_DFU_EVT_BOOTLOADER_ENTER);
306 nrf_pwr_mgmt_shutdown(NRF_PWR_MGMT_SHUTDOWN_GOTO_DFU);
312 uint32_t ble_dfu_buttonless_init(
const ble_dfu_buttonless_init_t * p_dfu_init)
315 ble_uuid_t service_uuid;
316 ble_uuid128_t nordic_base_uuid = BLE_NORDIC_VENDOR_BASE_UUID;
318 VERIFY_PARAM_NOT_NULL(p_dfu_init);
321 m_dfu.conn_handle = BLE_CONN_HANDLE_INVALID;
322 m_dfu.evt_handler = p_dfu_init->evt_handler;
323 m_dfu.is_waiting_for_reset =
false;
324 m_dfu.is_ctrlpt_indication_enabled =
false;
326 if (m_dfu.evt_handler == NULL)
328 m_dfu.evt_handler = dummy_evt_handler;
331 err_code = ble_dfu_buttonless_backend_init(&m_dfu);
332 VERIFY_SUCCESS(err_code);
334 BLE_UUID_BLE_ASSIGN(service_uuid, BLE_DFU_SERVICE_UUID);
337 err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
339 &(m_dfu.service_handle));
341 VERIFY_SUCCESS(err_code);
344 err_code = sd_ble_uuid_vs_add(&nordic_base_uuid, &m_dfu.uuid_type);
345 VERIFY_SUCCESS(err_code);
348 err_code = ble_dfu_buttonless_char_add(&m_dfu);
349 VERIFY_SUCCESS(err_code);
#define BLE_DFU_BLE_OBSERVER_PRIO