ble_dfu_unbonded.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. #include "app_flash.h"
  50. #include "system.h"
  51. #if (!NRF_DFU_BLE_BUTTONLESS_SUPPORTS_BONDS)
  52. #define NRF_DFU_ADV_NAME_MAX_LENGTH (20)
  53. void ble_dfu_buttonless_on_sys_evt(uint32_t, void * );
  54. uint32_t nrf_dfu_svci_vector_table_set(void);
  55. uint32_t nrf_dfu_svci_vector_table_unset(void);
  56. /**@brief Define functions for async interface to set new advertisement name for DFU mode. */
  57. NRF_SVCI_ASYNC_FUNC_DEFINE(NRF_DFU_SVCI_SET_ADV_NAME, nrf_dfu_set_adv_name, nrf_dfu_adv_name_t);
  58. // Register SoC observer for the Buttonless Secure DFU service
  59. NRF_SDH_SOC_OBSERVER(m_dfu_buttonless_soc_obs, BLE_DFU_SOC_OBSERVER_PRIO, ble_dfu_buttonless_on_sys_evt, NULL);
  60. ble_dfu_buttonless_t * mp_dfu = NULL;
  61. static nrf_dfu_adv_name_t m_adv_name;
  62. /**@brief Function for setting an advertisement name.
  63. *
  64. * @param[in] adv_name The new advertisement name.
  65. *
  66. * @retval NRF_SUCCESS Advertisement name was successfully set.
  67. * @retval DFU_RSP_BUSY Advertisement name was not set because of an ongoing operation.
  68. * @retval Any other errors from the SVCI interface call.
  69. */
  70. static uint32_t set_adv_name(nrf_dfu_adv_name_t * p_adv_name)
  71. {
  72. uint32_t err_code;
  73. if (mp_dfu->is_waiting_for_svci)
  74. {
  75. return DFU_RSP_BUSY;
  76. }
  77. err_code = nrf_dfu_set_adv_name(p_adv_name);
  78. if (err_code == NRF_SUCCESS)
  79. {
  80. // The request was accepted.
  81. mp_dfu->is_waiting_for_svci = true;
  82. }
  83. else if (err_code == NRF_ERROR_FORBIDDEN)
  84. {
  85. NRF_LOG_ERROR("The bootloader has write protected its settings page. This prohibits setting the advertising name. "\
  86. "The bootloader must be compiled with NRF_BL_SETTINGS_PAGE_PROTECT=0 to allow setting the advertising name.");
  87. }
  88. return err_code;
  89. }
  90. /**@brief Function for entering the bootloader.
  91. */
  92. static uint32_t enter_bootloader(ble_dfu_buttonless_op_code_t op_code)
  93. {
  94. uint32_t err_code;
  95. if (mp_dfu->is_waiting_for_svci)
  96. {
  97. // We have an ongoing async operation. Entering bootloader mode is not possible at this time.
  98. err_code = ble_dfu_buttonless_resp_send(op_code, DFU_RSP_BUSY);
  99. if (err_code != NRF_SUCCESS)
  100. {
  101. mp_dfu->evt_handler(BLE_DFU_EVT_RESPONSE_SEND_ERROR);
  102. }
  103. return NRF_SUCCESS;
  104. }
  105. // Set the flag indicating that we expect DFU mode.
  106. // This will be handled on acknowledgement of the characteristic indication.
  107. mp_dfu->is_waiting_for_reset = true;
  108. err_code = ble_dfu_buttonless_resp_send(op_code, DFU_RSP_SUCCESS);
  109. if (err_code != NRF_SUCCESS)
  110. {
  111. mp_dfu->is_waiting_for_reset = false;
  112. }
  113. return err_code;
  114. }
  115. uint32_t ble_dfu_buttonless_backend_init(ble_dfu_buttonless_t * p_dfu)
  116. {
  117. VERIFY_PARAM_NOT_NULL(p_dfu);
  118. mp_dfu = p_dfu;
  119. return NRF_SUCCESS;
  120. }
  121. uint32_t ble_dfu_buttonless_async_svci_init(void)
  122. {
  123. uint32_t ret_val;
  124. ret_val = nrf_dfu_svci_vector_table_set();
  125. VERIFY_SUCCESS(ret_val);
  126. ret_val = nrf_dfu_set_adv_name_init();
  127. VERIFY_SUCCESS(ret_val);
  128. ret_val = nrf_dfu_svci_vector_table_unset();
  129. return ret_val;
  130. }
  131. void ble_dfu_buttonless_on_sys_evt(uint32_t sys_evt, void * p_context)
  132. {
  133. uint32_t err_code;
  134. if (!nrf_dfu_set_adv_name_is_initialized())
  135. {
  136. return;
  137. }
  138. err_code = nrf_dfu_set_adv_name_on_sys_evt(sys_evt);
  139. if (err_code == NRF_ERROR_INVALID_STATE)
  140. {
  141. // The system event is not from an operation started by buttonless DFU.
  142. // No action is taken, and nothing is reported.
  143. }
  144. else if (err_code == NRF_SUCCESS)
  145. {
  146. // The async operation is finished.
  147. // Set the flag indicating that we are waiting for indication response
  148. // to activate the reset.
  149. mp_dfu->is_waiting_for_svci = false;
  150. // Report back the positive response
  151. err_code = ble_dfu_buttonless_resp_send(DFU_OP_SET_ADV_NAME, DFU_RSP_SUCCESS);
  152. if (err_code != NRF_SUCCESS)
  153. {
  154. mp_dfu->evt_handler(BLE_DFU_EVT_RESPONSE_SEND_ERROR);
  155. }
  156. }
  157. else
  158. {
  159. // Invalid error code reported back.
  160. mp_dfu->is_waiting_for_svci = false;
  161. err_code = ble_dfu_buttonless_resp_send(DFU_OP_SET_ADV_NAME, DFU_RSP_BUSY);
  162. if (err_code != NRF_SUCCESS)
  163. {
  164. mp_dfu->evt_handler(BLE_DFU_EVT_RESPONSE_SEND_ERROR);
  165. }
  166. // Report the failure to enter DFU mode
  167. mp_dfu->evt_handler(BLE_DFU_EVT_BOOTLOADER_ENTER_FAILED);
  168. }
  169. }
  170. uint32_t ble_dfu_buttonless_char_add(ble_dfu_buttonless_t * p_dfu)
  171. {
  172. ble_add_char_params_t add_char_params;
  173. memset(&add_char_params, 0, sizeof(add_char_params));
  174. add_char_params.uuid = BLE_DFU_BUTTONLESS_CHAR_UUID;
  175. add_char_params.uuid_type = p_dfu->uuid_type;
  176. add_char_params.char_props.indicate = 1;
  177. add_char_params.char_props.write = 1;
  178. add_char_params.is_defered_write = true;
  179. add_char_params.is_var_len = true;
  180. add_char_params.max_len = BLE_GATT_ATT_MTU_DEFAULT;
  181. add_char_params.cccd_write_access = SEC_OPEN;
  182. add_char_params.write_access = SEC_OPEN;
  183. add_char_params.read_access = SEC_OPEN;
  184. return characteristic_add(p_dfu->service_handle, &add_char_params, &p_dfu->control_point_char);
  185. }
  186. static volatile uint8_t OTA_CLEAR_flash_flag =0;
  187. static void bsp_ble_dfu_process(void);
  188. void ble_dfu_buttonless_on_ctrl_pt_write(ble_gatts_evt_write_t const * p_evt_write)
  189. {
  190. uint32_t err_code;
  191. ble_dfu_buttonless_rsp_code_t rsp_code = DFU_RSP_OPERATION_FAILED;
  192. uint8_t length = p_evt_write->len;
  193. // Start executing the control point write operation
  194. /*lint -e415 -e416 -save "Out of bounds access"*/
  195. // SEGGER_RTT_printf(0,"1111 dfu_button length:%d\r\n",length);
  196. // SEGGER_RTT_printf(0,"2222 dfu_button:"); for(int i=0;i<length;i++){SEGGER_RTT_printf(0,"%02X ",p_evt_write->data[i]);} SEGGER_RTT_printf(0,"\r\n");
  197. switch (p_evt_write->data[0])
  198. {
  199. case DFU_OP_ENTER_BOOTLOADER:
  200. if(1 == length){
  201. err_code = enter_bootloader(DFU_OP_ENTER_BOOTLOADER);
  202. if (err_code == NRF_SUCCESS)
  203. {
  204. rsp_code = DFU_RSP_SUCCESS;
  205. }
  206. else if (err_code == NRF_ERROR_BUSY)
  207. {
  208. rsp_code = DFU_RSP_BUSY;
  209. }
  210. }
  211. else{
  212. OTA_CLEAR_flash_flag =1;
  213. rsp_code = DFU_RSP_SUCCESS;
  214. Process_SetHoldOn(bsp_ble_dfu_process,1);
  215. }
  216. break;
  217. // case DFU_OP_ClEARFlash_ENTER_BOOTLOADER:
  218. // err_code = enter_bootloader(DFU_OP_ClEARFlash_ENTER_BOOTLOADER);
  219. // if (err_code == NRF_SUCCESS)
  220. // {
  221. // rsp_code = DFU_RSP_SUCCESS;
  222. // }
  223. // else if (err_code == NRF_ERROR_BUSY)
  224. // {
  225. // rsp_code = DFU_RSP_BUSY;
  226. // }
  227. // break;
  228. case DFU_OP_SET_ADV_NAME:
  229. if( (p_evt_write->data[1] > NRF_DFU_ADV_NAME_MAX_LENGTH)
  230. || (p_evt_write->data[1] == 0))
  231. {
  232. // New advertisement name too short or too long.
  233. rsp_code = DFU_RSP_ADV_NAME_INVALID;
  234. }
  235. else
  236. {
  237. memcpy(m_adv_name.name, &p_evt_write->data[2], p_evt_write->data[1]);
  238. m_adv_name.len = p_evt_write->data[1];
  239. err_code = set_adv_name(&m_adv_name);
  240. if (err_code == NRF_SUCCESS)
  241. {
  242. rsp_code = DFU_RSP_SUCCESS;
  243. }
  244. }
  245. break;
  246. default:
  247. rsp_code = DFU_RSP_OP_CODE_NOT_SUPPORTED;
  248. break;
  249. }
  250. /*lint -restore*/
  251. // Report back in case of error
  252. if (rsp_code != DFU_RSP_SUCCESS )
  253. {
  254. // SEGGER_RTT_printf(0,"ble_dfu_buttonless_resp_send:");
  255. err_code = ble_dfu_buttonless_resp_send((ble_dfu_buttonless_op_code_t)p_evt_write->data[0], rsp_code);
  256. if (err_code != NRF_SUCCESS)
  257. {
  258. mp_dfu->evt_handler(BLE_DFU_EVT_RESPONSE_SEND_ERROR);
  259. }
  260. // Report the error to the main application
  261. mp_dfu->evt_handler(BLE_DFU_EVT_BOOTLOADER_ENTER_FAILED);
  262. }
  263. }
  264. uint32_t ble_dfu_buttonless_bootloader_start_prepare(void)
  265. {
  266. uint32_t err_code;
  267. // Indicate to main app that DFU mode is starting.
  268. mp_dfu->evt_handler(BLE_DFU_EVT_BOOTLOADER_ENTER_PREPARE);
  269. err_code = ble_dfu_buttonless_bootloader_start_finalize();
  270. return err_code;
  271. }
  272. static void bsp_ble_dfu_process(){
  273. uint32_t err_code;
  274. ble_dfu_buttonless_rsp_code_t rsp_code = DFU_RSP_OPERATION_FAILED;
  275. if(1 == OTA_CLEAR_flash_flag){OTA_CLEAR_flash_flag=0;
  276. Process_SetHoldOn(bsp_ble_dfu_process,0);
  277. // SEGGER_RTT_printf(0,"OTA_CLEAR_flash_flag 1\r\n");
  278. Flash_DeleteAllStep();
  279. memset(&mFlash,0,sizeof(mFlash));
  280. memset(&mBackup,0,sizeof(mBackup));
  281. mFlash.head = FLASH_HEAD;
  282. mFlash.mClient.LastsotfVersion = SOFTWARE_VERSION;
  283. mFlash.mClient.updateFlag = 0xA5;
  284. if(Flash_SaveBackup() != ZONE_OP_SUCCESS){
  285. DEBUG_LOG("Flash_SaveBackup Fail\r\n");
  286. Except_TxError(EXCEPT_Power,"pwr save backup fail");
  287. }
  288. if(Flash_SaveInfomation() != ZONE_OP_SUCCESS){
  289. DEBUG_LOG("Flash_SaveInfomation Fail\r\n");
  290. Except_TxError(EXCEPT_Power,"pwr save information fail");
  291. }
  292. // SEGGER_RTT_printf(0,"DFU_OP_ENTER_BOOTLOADER 2\r\n");
  293. err_code = enter_bootloader(DFU_OP_ENTER_BOOTLOADER);
  294. if (err_code == NRF_SUCCESS)
  295. {
  296. rsp_code = DFU_RSP_SUCCESS;
  297. }
  298. else if (err_code == NRF_ERROR_BUSY)
  299. {
  300. rsp_code = DFU_RSP_BUSY;
  301. }
  302. if (rsp_code != DFU_RSP_SUCCESS)
  303. {
  304. err_code = ble_dfu_buttonless_resp_send(DFU_OP_ENTER_BOOTLOADER, rsp_code);
  305. if (err_code != NRF_SUCCESS)
  306. {
  307. mp_dfu->evt_handler(BLE_DFU_EVT_RESPONSE_SEND_ERROR);
  308. }
  309. // Report the error to the main application
  310. mp_dfu->evt_handler(BLE_DFU_EVT_BOOTLOADER_ENTER_FAILED);
  311. }
  312. }
  313. }
  314. void bsp_ble_dfu_process_Initialize(void)
  315. {
  316. Process_Start(100,"bsp_ble_dfu_process",bsp_ble_dfu_process);
  317. }
  318. #endif // NRF_DFU_BOTTONLESS_SUPPORT_BOND