ble_dfu_unbonded.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /**
  2. * Copyright (c) 2017 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include <stdint.h>
  41. #include <stdbool.h>
  42. #include <stddef.h>
  43. #include "nrf_dfu_ble_svci_bond_sharing.h"
  44. #include "nordic_common.h"
  45. #include "nrf_error.h"
  46. #include "ble_dfu.h"
  47. #include "nrf_log.h"
  48. #include "nrf_sdh_soc.h"
  49. #if (!NRF_DFU_BLE_BUTTONLESS_SUPPORTS_BONDS)
  50. #define NRF_DFU_ADV_NAME_MAX_LENGTH (20)
  51. void ble_dfu_buttonless_on_sys_evt(uint32_t, void * );
  52. uint32_t nrf_dfu_svci_vector_table_set(void);
  53. uint32_t nrf_dfu_svci_vector_table_unset(void);
  54. /**@brief Define functions for async interface to set new advertisement name for DFU mode. */
  55. NRF_SVCI_ASYNC_FUNC_DEFINE(NRF_DFU_SVCI_SET_ADV_NAME, nrf_dfu_set_adv_name, nrf_dfu_adv_name_t);
  56. // Register SoC observer for the Buttonless Secure DFU service
  57. NRF_SDH_SOC_OBSERVER(m_dfu_buttonless_soc_obs, BLE_DFU_SOC_OBSERVER_PRIO, ble_dfu_buttonless_on_sys_evt, NULL);
  58. ble_dfu_buttonless_t * mp_dfu = NULL;
  59. static nrf_dfu_adv_name_t m_adv_name;
  60. /**@brief Function for setting an advertisement name.
  61. *
  62. * @param[in] adv_name The new advertisement name.
  63. *
  64. * @retval NRF_SUCCESS Advertisement name was successfully set.
  65. * @retval DFU_RSP_BUSY Advertisement name was not set because of an ongoing operation.
  66. * @retval Any other errors from the SVCI interface call.
  67. */
  68. static uint32_t set_adv_name(nrf_dfu_adv_name_t * p_adv_name)
  69. {
  70. uint32_t err_code;
  71. if (mp_dfu->is_waiting_for_svci)
  72. {
  73. return DFU_RSP_BUSY;
  74. }
  75. err_code = nrf_dfu_set_adv_name(p_adv_name);
  76. if (err_code == NRF_SUCCESS)
  77. {
  78. // The request was accepted.
  79. mp_dfu->is_waiting_for_svci = true;
  80. }
  81. else if (err_code == NRF_ERROR_FORBIDDEN)
  82. {
  83. NRF_LOG_ERROR("The bootloader has write protected its settings page. This prohibits setting the advertising name. "\
  84. "The bootloader must be compiled with NRF_BL_SETTINGS_PAGE_PROTECT=0 to allow setting the advertising name.");
  85. }
  86. return err_code;
  87. }
  88. /**@brief Function for entering the bootloader.
  89. */
  90. static uint32_t enter_bootloader()
  91. {
  92. uint32_t err_code;
  93. if (mp_dfu->is_waiting_for_svci)
  94. {
  95. // We have an ongoing async operation. Entering bootloader mode is not possible at this time.
  96. err_code = ble_dfu_buttonless_resp_send(DFU_OP_ENTER_BOOTLOADER, DFU_RSP_BUSY);
  97. if (err_code != NRF_SUCCESS)
  98. {
  99. mp_dfu->evt_handler(BLE_DFU_EVT_RESPONSE_SEND_ERROR);
  100. }
  101. return NRF_SUCCESS;
  102. }
  103. // Set the flag indicating that we expect DFU mode.
  104. // This will be handled on acknowledgement of the characteristic indication.
  105. mp_dfu->is_waiting_for_reset = true;
  106. err_code = ble_dfu_buttonless_resp_send(DFU_OP_ENTER_BOOTLOADER, DFU_RSP_SUCCESS);
  107. if (err_code != NRF_SUCCESS)
  108. {
  109. mp_dfu->is_waiting_for_reset = false;
  110. }
  111. return err_code;
  112. }
  113. uint32_t ble_dfu_buttonless_backend_init(ble_dfu_buttonless_t * p_dfu)
  114. {
  115. VERIFY_PARAM_NOT_NULL(p_dfu);
  116. mp_dfu = p_dfu;
  117. return NRF_SUCCESS;
  118. }
  119. uint32_t ble_dfu_buttonless_async_svci_init(void)
  120. {
  121. uint32_t ret_val;
  122. ret_val = nrf_dfu_svci_vector_table_set();
  123. VERIFY_SUCCESS(ret_val);
  124. ret_val = nrf_dfu_set_adv_name_init();
  125. VERIFY_SUCCESS(ret_val);
  126. ret_val = nrf_dfu_svci_vector_table_unset();
  127. return ret_val;
  128. }
  129. void ble_dfu_buttonless_on_sys_evt(uint32_t sys_evt, void * p_context)
  130. {
  131. uint32_t err_code;
  132. if (!nrf_dfu_set_adv_name_is_initialized())
  133. {
  134. return;
  135. }
  136. err_code = nrf_dfu_set_adv_name_on_sys_evt(sys_evt);
  137. if (err_code == NRF_ERROR_INVALID_STATE)
  138. {
  139. // The system event is not from an operation started by buttonless DFU.
  140. // No action is taken, and nothing is reported.
  141. }
  142. else if (err_code == NRF_SUCCESS)
  143. {
  144. // The async operation is finished.
  145. // Set the flag indicating that we are waiting for indication response
  146. // to activate the reset.
  147. mp_dfu->is_waiting_for_svci = false;
  148. // Report back the positive response
  149. err_code = ble_dfu_buttonless_resp_send(DFU_OP_SET_ADV_NAME, DFU_RSP_SUCCESS);
  150. if (err_code != NRF_SUCCESS)
  151. {
  152. mp_dfu->evt_handler(BLE_DFU_EVT_RESPONSE_SEND_ERROR);
  153. }
  154. }
  155. else
  156. {
  157. // Invalid error code reported back.
  158. mp_dfu->is_waiting_for_svci = false;
  159. err_code = ble_dfu_buttonless_resp_send(DFU_OP_SET_ADV_NAME, DFU_RSP_BUSY);
  160. if (err_code != NRF_SUCCESS)
  161. {
  162. mp_dfu->evt_handler(BLE_DFU_EVT_RESPONSE_SEND_ERROR);
  163. }
  164. // Report the failure to enter DFU mode
  165. mp_dfu->evt_handler(BLE_DFU_EVT_BOOTLOADER_ENTER_FAILED);
  166. }
  167. }
  168. uint32_t ble_dfu_buttonless_char_add(ble_dfu_buttonless_t * p_dfu)
  169. {
  170. ble_add_char_params_t add_char_params;
  171. memset(&add_char_params, 0, sizeof(add_char_params));
  172. add_char_params.uuid = BLE_DFU_BUTTONLESS_CHAR_UUID;
  173. add_char_params.uuid_type = p_dfu->uuid_type;
  174. add_char_params.char_props.indicate = 1;
  175. add_char_params.char_props.write = 1;
  176. add_char_params.is_defered_write = true;
  177. add_char_params.is_var_len = true;
  178. add_char_params.max_len = BLE_GATT_ATT_MTU_DEFAULT;
  179. add_char_params.cccd_write_access = SEC_OPEN;
  180. add_char_params.write_access = SEC_OPEN;
  181. add_char_params.read_access = SEC_OPEN;
  182. return characteristic_add(p_dfu->service_handle, &add_char_params, &p_dfu->control_point_char);
  183. }
  184. void ble_dfu_buttonless_on_ctrl_pt_write(ble_gatts_evt_write_t const * p_evt_write)
  185. {
  186. uint32_t err_code;
  187. ble_dfu_buttonless_rsp_code_t rsp_code = DFU_RSP_OPERATION_FAILED;
  188. // Start executing the control point write operation
  189. /*lint -e415 -e416 -save "Out of bounds access"*/
  190. switch (p_evt_write->data[0])
  191. {
  192. case DFU_OP_ENTER_BOOTLOADER:
  193. err_code = enter_bootloader();
  194. if (err_code == NRF_SUCCESS)
  195. {
  196. rsp_code = DFU_RSP_SUCCESS;
  197. }
  198. else if (err_code == NRF_ERROR_BUSY)
  199. {
  200. rsp_code = DFU_RSP_BUSY;
  201. }
  202. break;
  203. case DFU_OP_SET_ADV_NAME:
  204. if( (p_evt_write->data[1] > NRF_DFU_ADV_NAME_MAX_LENGTH)
  205. || (p_evt_write->data[1] == 0))
  206. {
  207. // New advertisement name too short or too long.
  208. rsp_code = DFU_RSP_ADV_NAME_INVALID;
  209. }
  210. else
  211. {
  212. memcpy(m_adv_name.name, &p_evt_write->data[2], p_evt_write->data[1]);
  213. m_adv_name.len = p_evt_write->data[1];
  214. err_code = set_adv_name(&m_adv_name);
  215. if (err_code == NRF_SUCCESS)
  216. {
  217. rsp_code = DFU_RSP_SUCCESS;
  218. }
  219. }
  220. break;
  221. default:
  222. rsp_code = DFU_RSP_OP_CODE_NOT_SUPPORTED;
  223. break;
  224. }
  225. /*lint -restore*/
  226. // Report back in case of error
  227. if (rsp_code != DFU_RSP_SUCCESS)
  228. {
  229. err_code = ble_dfu_buttonless_resp_send((ble_dfu_buttonless_op_code_t)p_evt_write->data[0], rsp_code);
  230. if (err_code != NRF_SUCCESS)
  231. {
  232. mp_dfu->evt_handler(BLE_DFU_EVT_RESPONSE_SEND_ERROR);
  233. }
  234. // Report the error to the main application
  235. mp_dfu->evt_handler(BLE_DFU_EVT_BOOTLOADER_ENTER_FAILED);
  236. }
  237. }
  238. uint32_t ble_dfu_buttonless_bootloader_start_prepare(void)
  239. {
  240. uint32_t err_code;
  241. // Indicate to main app that DFU mode is starting.
  242. mp_dfu->evt_handler(BLE_DFU_EVT_BOOTLOADER_ENTER_PREPARE);
  243. err_code = ble_dfu_buttonless_bootloader_start_finalize();
  244. return err_code;
  245. }
  246. #endif // NRF_DFU_BOTTONLESS_SUPPORT_BOND