ruuvi.drivers.c  ${PROJECT_VERSION}
Drivers for external sensors and peripherals on embedded systems.
ble_dfu_bonded.c
Go to the documentation of this file.
1 
41 #include <stdint.h>
42 #include <stdbool.h>
43 #include <stddef.h>
44 #include "nrf_dfu_ble_svci_bond_sharing.h"
45 #include "nordic_common.h"
46 #include "nrf_error.h"
47 #include "ble_dfu.h"
48 #include "nrf_log.h"
49 #include "peer_manager.h"
50 #include "gatts_cache_manager.h"
51 #include "peer_id.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)
56 
57 
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);
61 
63 NRF_SVCI_ASYNC_FUNC_DEFINE(NRF_DFU_SVCI_SET_PEER_DATA, nrf_dfu_set_peer_data, nrf_dfu_peer_data_t);
64 
65 // Register SoC observer for the Buttonless Secure DFU service
66 NRF_SDH_SOC_OBSERVER(m_dfu_buttonless_soc_obs, BLE_DFU_SOC_OBSERVER_PRIO, ble_dfu_buttonless_on_sys_evt, NULL);
67 
68 ble_dfu_buttonless_t * mp_dfu;
69 static nrf_dfu_peer_data_t m_peer_data;
70 
71 
76 static void pm_evt_handler(pm_evt_t const * p_evt)
77 {
78  uint32_t ret;
79 
80  if (mp_dfu == NULL)
81  {
82  return;
83  }
84 
85  // Only handle this when we are waiting to reset into DFU mode
86  if (!mp_dfu->is_waiting_for_reset)
87  {
88  return;
89  }
90 
91  switch(p_evt->evt_id)
92  {
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)
95  {
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)
99  {
100  NRF_LOG_DEBUG("Finished updating Service Changed indication for peers");
101  // We have updated Service Changed Indication for all devices.
102  ret = ble_dfu_buttonless_bootloader_start_finalize();
103  if (ret != NRF_SUCCESS)
104  {
105  mp_dfu->evt_handler(BLE_DFU_EVT_BOOTLOADER_ENTER_FAILED);
106  }
107  }
108  }
109  break;
110 
111  case PM_EVT_PEER_DATA_UPDATE_FAILED:
112  // Failure to update data. Service Changed cannot be sent but DFU mode is still possible
113  ret = ble_dfu_buttonless_bootloader_start_finalize();
114  if (ret != NRF_SUCCESS)
115  {
116  mp_dfu->evt_handler(BLE_DFU_EVT_BOOTLOADER_ENTER_FAILED);
117  }
118  break;
119 
120  default:
121  break;
122  }
123 }
124 
125 
126 static uint32_t retrieve_peer_data(void)
127 {
128  ret_code_t ret;
129  pm_peer_data_bonding_t bonding_data = {0};
130  pm_peer_id_t peer_id;
131 
132  ret = pm_peer_id_get(mp_dfu->conn_handle, &peer_id);
133  VERIFY_SUCCESS(ret);
134 
135  if (peer_id == PM_PEER_ID_INVALID)
136  {
137  return NRF_ERROR_FORBIDDEN;
138  }
139 
140  ret = pm_peer_data_bonding_load(peer_id, &bonding_data);
141  VERIFY_SUCCESS(ret);
142 
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));
145 
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,
149  &len,
150  BLE_GATTS_SYS_ATTR_FLAG_SYS_SRVCS);
151 
152  NRF_LOG_DEBUG("system attribute table len: %d", len);
153 
154  return ret;
155 }
156 
157 
162 static uint32_t enter_bootloader(void)
163 {
164  uint32_t ret;
165 
166  NRF_LOG_INFO("Writing peer data to the bootloader...");
167 
168  if (mp_dfu->is_waiting_for_svci)
169  {
170  return ble_dfu_buttonless_resp_send(DFU_OP_ENTER_BOOTLOADER, DFU_RSP_BUSY);
171  }
172 
173  // If retrieve_peer_data returns NRF_ERROR_FORBIDDEN, then the device was not bonded.
174  ret = retrieve_peer_data();
175  VERIFY_SUCCESS(ret);
176 
177  ret = nrf_dfu_set_peer_data(&m_peer_data);
178  if (ret == NRF_SUCCESS)
179  {
180  // The request was accepted. Waiting for sys events to progress.
181  mp_dfu->is_waiting_for_svci = true;
182  }
183  else if (ret == NRF_ERROR_FORBIDDEN)
184  {
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.");
187  }
188 
189  return ret;
190 }
191 
192 
193 uint32_t ble_dfu_buttonless_backend_init(ble_dfu_buttonless_t * p_dfu)
194 {
195  VERIFY_PARAM_NOT_NULL(p_dfu);
196 
197  // Set the memory used by the backend.
198  mp_dfu = p_dfu;
199 
200  // Initialize the Peer manager handler.
201  return pm_register(&pm_evt_handler);
202 }
203 
204 
205 uint32_t ble_dfu_buttonless_async_svci_init(void)
206 {
207  uint32_t ret;
208 
209  // Set the vector table base address to the bootloader.
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));
213  VERIFY_SUCCESS(ret);
214 
215  // Initialize the asynchronous SuperVisor interface to set peer data in Secure DFU bootloader.
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));
219  VERIFY_SUCCESS(ret);
220 
221  // Set the vector table base address back to main application.
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));
225 
226  return ret;
227 }
228 
229 
230 void ble_dfu_buttonless_on_sys_evt(uint32_t sys_evt, void * p_context)
231 {
232  uint32_t ret;
233 
234  if (!nrf_dfu_set_peer_data_is_initialized())
235  {
236  return;
237  }
238 
239  ret = nrf_dfu_set_peer_data_on_sys_evt(sys_evt);
240  if (ret == NRF_ERROR_INVALID_STATE)
241  {
242  // The system event is not from an operation started by buttonless DFU.
243  // No action is taken, and nothing is reported.
244  }
245  else if (ret == NRF_SUCCESS)
246  {
247  // Peer data was successfully forwarded to the Secure DFU bootloader.
248  // Set the flag indicating that we are waiting for indication response
249  // to activate the reset.
250  mp_dfu->is_waiting_for_reset = true;
251  mp_dfu->is_waiting_for_svci = false;
252 
253  // Report back the positive response
254  ret = ble_dfu_buttonless_resp_send(DFU_OP_ENTER_BOOTLOADER, DFU_RSP_SUCCESS);
255  if (ret != NRF_SUCCESS)
256  {
257  mp_dfu->evt_handler(BLE_DFU_EVT_RESPONSE_SEND_ERROR);
258  mp_dfu->is_waiting_for_reset = false;
259  }
260  }
261  else
262  {
263  // Failed to set peer data. Report this.
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);
267 
268  // Report the failure to send the response to the client
269  if (ret != NRF_SUCCESS)
270  {
271  mp_dfu->evt_handler(BLE_DFU_EVT_RESPONSE_SEND_ERROR);
272  }
273 
274  // Report the failure to enter DFU mode
275  mp_dfu->evt_handler(BLE_DFU_EVT_BOOTLOADER_ENTER_FAILED);
276  }
277 }
278 
279 
280 uint32_t ble_dfu_buttonless_char_add(ble_dfu_buttonless_t * p_dfu)
281 {
282  ble_add_char_params_t add_char_params;
283 
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;
292 
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;
296 
297  return characteristic_add(p_dfu->service_handle, &add_char_params, &p_dfu->control_point_char);
298 }
299 
300 
301 void ble_dfu_buttonless_on_ctrl_pt_write(ble_gatts_evt_write_t const * p_evt_write)
302 {
303  uint32_t ret;
304  ble_dfu_buttonless_rsp_code_t rsp_code = DFU_RSP_OPERATION_FAILED;
305 
306  // Start executing the control point write action
307  switch (p_evt_write->data[0])
308  {
309  case DFU_OP_ENTER_BOOTLOADER:
310  ret = enter_bootloader();
311  if (ret == NRF_SUCCESS)
312  {
313  rsp_code = DFU_RSP_SUCCESS;
314  }
315  else if (ret == NRF_ERROR_BUSY)
316  {
317  rsp_code = DFU_RSP_BUSY;
318  }
319  else if (ret == NRF_ERROR_FORBIDDEN)
320  {
321  rsp_code = DFU_RSP_NOT_BONDED;
322  }
323  break;
324 
325  default:
326  rsp_code = DFU_RSP_OP_CODE_NOT_SUPPORTED;
327  break;
328  }
329 
330  // Report back in case of error
331  if (rsp_code != DFU_RSP_SUCCESS)
332  {
333  ret = ble_dfu_buttonless_resp_send((ble_dfu_buttonless_op_code_t)p_evt_write->data[0],
334  rsp_code);
335 
336  if (ret != NRF_SUCCESS)
337  {
338  mp_dfu->evt_handler(BLE_DFU_EVT_RESPONSE_SEND_ERROR);
339  }
340 
341  // Report the error to the main application
342  mp_dfu->evt_handler(BLE_DFU_EVT_BOOTLOADER_ENTER_FAILED);
343  }
344 }
345 
346 
347 uint32_t ble_dfu_buttonless_bootloader_start_prepare(void)
348 {
349  NRF_LOG_DEBUG("In ble_dfu_buttonless_bootloader_start_prepare");
350 
351  // Indicate to main app that DFU mode is starting.
352  // This event can be used to let the device take down any connection to
353  // bonded devices.
354  mp_dfu->evt_handler(BLE_DFU_EVT_BOOTLOADER_ENTER_PREPARE);
355 
356  // Store the number of peers for which Peer Manager is expected to successfully write events.
357  mp_dfu->peers_count = peer_id_n_ids();
358 
359  // Set local database changed to get Service Changed indication for all bonded peers
360  // on next bootup (either because of a successful or aborted DFU).
361  gscm_local_database_has_changed();
362 
363  return NRF_SUCCESS;
364 }
365 
366 #endif // NRF_DFU_BLE_BUTTONLESS_SUPPORTS_BONDS
367 #endif // BLE_DFU_ENABLED
#define BLE_DFU_SOC_OBSERVER_PRIO
Definition: sdk_config.h:12474