ble_dfu.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. /* Attention!
  41. * To maintain compliance with Nordic Semiconductor ASA's Bluetooth profile
  42. * qualification listings, this section of source code must not be modified.
  43. */
  44. #include "ble_dfu.h"
  45. #include <string.h>
  46. #include "ble_hci.h"
  47. #include "sdk_macros.h"
  48. #include "ble_srv_common.h"
  49. #include "nrf_nvic.h"
  50. #include "nrf_sdm.h"
  51. #include "nrf_soc.h"
  52. #include "nrf_log.h"
  53. #include "nrf_dfu_ble_svci_bond_sharing.h"
  54. #include "nrf_bootloader_info.h"
  55. #include "nrf_svci_async_function.h"
  56. #include "nrf_pwr_mgmt.h"
  57. #include "peer_manager.h"
  58. #include "gatts_cache_manager.h"
  59. #include "peer_id.h"
  60. #define MAX_CTRL_POINT_RESP_PARAM_LEN 3 /**< Max length of the responses. */
  61. #define BLE_DFU_SERVICE_UUID 0xFE59 /**< The 16-bit UUID of the Secure DFU Service. */
  62. static ble_dfu_buttonless_t m_dfu; /**< Structure holding information about the Buttonless Secure DFU Service. */
  63. NRF_SDH_BLE_OBSERVER(m_dfus_obs, BLE_DFU_BLE_OBSERVER_PRIO, ble_dfu_buttonless_on_ble_evt, &m_dfu);
  64. /**@brief Function that is called if no event handler is provided.
  65. */
  66. static void dummy_evt_handler(ble_dfu_buttonless_evt_type_t evt)
  67. {
  68. NRF_LOG_DEBUG("Dummy event handler received event 0x%x", evt);
  69. }
  70. /**@brief Function for handling write events to the Buttonless Secure DFU Service Service Control Point characteristic.
  71. *
  72. * @param[in] p_evt_write Write event received from the BLE stack.
  73. */
  74. static void on_ctrlpt_write(ble_gatts_evt_write_t const * p_evt_write)
  75. {
  76. uint32_t err_code;
  77. ble_gatts_rw_authorize_reply_params_t write_authorize_reply;
  78. memset(&write_authorize_reply, 0, sizeof(write_authorize_reply));
  79. write_authorize_reply.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE;
  80. uint8_t cccd_val[2];
  81. ble_gatts_value_t value = {.p_value = cccd_val, .len = 2, .offset = 0};
  82. err_code = sd_ble_gatts_value_get(m_dfu.conn_handle, m_dfu.control_point_char.cccd_handle, &value);
  83. if (err_code == NRF_SUCCESS && ble_srv_is_indication_enabled(cccd_val))
  84. {
  85. write_authorize_reply.params.write.update = 1;
  86. write_authorize_reply.params.write.gatt_status = BLE_GATT_STATUS_SUCCESS;
  87. }
  88. else
  89. {
  90. write_authorize_reply.params.write.gatt_status = BLE_GATT_STATUS_ATTERR_CPS_CCCD_CONFIG_ERROR;
  91. }
  92. // Authorize the write request
  93. do {
  94. err_code = sd_ble_gatts_rw_authorize_reply(m_dfu.conn_handle, &write_authorize_reply);
  95. } while (err_code == NRF_ERROR_BUSY);
  96. if (write_authorize_reply.params.write.gatt_status != BLE_GATT_STATUS_SUCCESS)
  97. {
  98. return;
  99. }
  100. // Forward the write event to the Buttonless DFU module.
  101. ble_dfu_buttonless_on_ctrl_pt_write(p_evt_write);
  102. }
  103. /**@brief Write authorization request event handler.
  104. *
  105. * @details The write authorization request event handler is called when writing to the control point.
  106. *
  107. * @param[in] p_ble_evt Event received from the BLE stack.
  108. */
  109. static void on_rw_authorize_req(ble_evt_t const * p_ble_evt)
  110. {
  111. if (p_ble_evt->evt.gatts_evt.conn_handle != m_dfu.conn_handle)
  112. {
  113. return;
  114. }
  115. const ble_gatts_evt_rw_authorize_request_t * p_auth_req =
  116. &p_ble_evt->evt.gatts_evt.params.authorize_request;
  117. if (
  118. (p_auth_req->type == BLE_GATTS_AUTHORIZE_TYPE_WRITE) &&
  119. (p_auth_req->request.write.handle == m_dfu.control_point_char.value_handle) &&
  120. (p_auth_req->request.write.op != BLE_GATTS_OP_PREP_WRITE_REQ) &&
  121. (p_auth_req->request.write.op != BLE_GATTS_OP_EXEC_WRITE_REQ_NOW) &&
  122. (p_auth_req->request.write.op != BLE_GATTS_OP_EXEC_WRITE_REQ_CANCEL)
  123. )
  124. {
  125. on_ctrlpt_write(&p_auth_req->request.write);
  126. }
  127. }
  128. /**@brief Connect event handler.
  129. *
  130. * @param[in] p_ble_evt Event received from the BLE stack.
  131. */
  132. static void on_connect(ble_evt_t const * p_ble_evt)
  133. {
  134. m_dfu.conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
  135. }
  136. /**@brief Disconnect event handler.
  137. *
  138. * @param[in] p_ble_evt Event received from the BLE stack.
  139. */
  140. static void on_disconnect(ble_evt_t const * p_ble_evt)
  141. {
  142. if (m_dfu.conn_handle != p_ble_evt->evt.gap_evt.conn_handle)
  143. {
  144. return;
  145. }
  146. m_dfu.conn_handle = BLE_CONN_HANDLE_INVALID;
  147. }
  148. /**@brief Function for handling the HVC events.
  149. *
  150. * @details Handles HVC events from the BLE stack.
  151. *
  152. * @param[in] p_ble_evt Event received from the BLE stack.
  153. */
  154. static void on_hvc(ble_evt_t const * p_ble_evt)
  155. {
  156. uint32_t err_code;
  157. ble_gatts_evt_hvc_t const * p_hvc = &p_ble_evt->evt.gatts_evt.params.hvc;
  158. if (p_hvc->handle == m_dfu.control_point_char.value_handle)
  159. {
  160. // Enter bootloader if we were waiting for reset after hvc indication confimation.
  161. if (m_dfu.is_waiting_for_reset)
  162. {
  163. err_code = ble_dfu_buttonless_bootloader_start_prepare();
  164. if (err_code != NRF_SUCCESS)
  165. {
  166. m_dfu.evt_handler(BLE_DFU_EVT_BOOTLOADER_ENTER_FAILED);
  167. }
  168. }
  169. }
  170. }
  171. void ble_dfu_buttonless_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
  172. {
  173. VERIFY_PARAM_NOT_NULL_VOID(p_ble_evt);
  174. switch (p_ble_evt->header.evt_id)
  175. {
  176. case BLE_GAP_EVT_CONNECTED:
  177. on_connect(p_ble_evt);
  178. break;
  179. case BLE_GAP_EVT_DISCONNECTED:
  180. on_disconnect(p_ble_evt);
  181. break;
  182. case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST:
  183. on_rw_authorize_req(p_ble_evt);
  184. break;
  185. case BLE_GATTS_EVT_HVC:
  186. on_hvc(p_ble_evt);
  187. break;
  188. default:
  189. // no implementation
  190. break;
  191. }
  192. }
  193. uint32_t ble_dfu_buttonless_resp_send(ble_dfu_buttonless_op_code_t op_code, ble_dfu_buttonless_rsp_code_t rsp_code)
  194. {
  195. // Send indication
  196. uint32_t err_code;
  197. const uint16_t len = MAX_CTRL_POINT_RESP_PARAM_LEN;
  198. uint16_t hvx_len;
  199. uint8_t hvx_data[MAX_CTRL_POINT_RESP_PARAM_LEN];
  200. ble_gatts_hvx_params_t hvx_params;
  201. memset(&hvx_params, 0, sizeof(hvx_params));
  202. hvx_len = len;
  203. hvx_data[0] = DFU_OP_RESPONSE_CODE;
  204. hvx_data[1] = (uint8_t)op_code;
  205. hvx_data[2] = (uint8_t)rsp_code;
  206. hvx_params.handle = m_dfu.control_point_char.value_handle;
  207. hvx_params.type = BLE_GATT_HVX_INDICATION;
  208. hvx_params.offset = 0;
  209. hvx_params.p_len = &hvx_len;
  210. hvx_params.p_data = hvx_data;
  211. err_code = sd_ble_gatts_hvx(m_dfu.conn_handle, &hvx_params);
  212. if ((err_code == NRF_SUCCESS) && (hvx_len != len))
  213. {
  214. err_code = NRF_ERROR_DATA_SIZE;
  215. }
  216. return err_code;
  217. }
  218. uint32_t ble_dfu_buttonless_bootloader_start_finalize(void)
  219. {
  220. uint32_t err_code;
  221. NRF_LOG_DEBUG("In ble_dfu_buttonless_bootloader_start_finalize\r\n");
  222. err_code = sd_power_gpregret_clr(0, 0xffffffff);
  223. VERIFY_SUCCESS(err_code);
  224. err_code = sd_power_gpregret_set(0, BOOTLOADER_DFU_START);
  225. VERIFY_SUCCESS(err_code);
  226. // Indicate that the Secure DFU bootloader will be entered
  227. m_dfu.evt_handler(BLE_DFU_EVT_BOOTLOADER_ENTER);
  228. // Signal that DFU mode is to be enter to the power management module
  229. nrf_pwr_mgmt_shutdown(NRF_PWR_MGMT_SHUTDOWN_GOTO_DFU);
  230. return NRF_SUCCESS;
  231. }
  232. uint32_t ble_dfu_buttonless_init(const ble_dfu_buttonless_init_t * p_dfu_init)
  233. {
  234. uint32_t err_code;
  235. ble_uuid_t service_uuid;
  236. ble_uuid128_t nordic_base_uuid = BLE_NORDIC_VENDOR_BASE_UUID;
  237. VERIFY_PARAM_NOT_NULL(p_dfu_init);
  238. // Initialize the service structure.
  239. m_dfu.conn_handle = BLE_CONN_HANDLE_INVALID;
  240. m_dfu.evt_handler = p_dfu_init->evt_handler;
  241. m_dfu.is_waiting_for_reset = false;
  242. if (m_dfu.evt_handler == NULL)
  243. {
  244. m_dfu.evt_handler = dummy_evt_handler;
  245. }
  246. err_code = ble_dfu_buttonless_backend_init(&m_dfu);
  247. VERIFY_SUCCESS(err_code);
  248. BLE_UUID_BLE_ASSIGN(service_uuid, BLE_DFU_SERVICE_UUID);
  249. // Add the DFU service declaration.
  250. err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
  251. &service_uuid,
  252. &(m_dfu.service_handle));
  253. VERIFY_SUCCESS(err_code);
  254. // Add vendor specific base UUID to use with the Buttonless DFU characteristic.
  255. err_code = sd_ble_uuid_vs_add(&nordic_base_uuid, &m_dfu.uuid_type);
  256. VERIFY_SUCCESS(err_code);
  257. // Add the Buttonless DFU Characteristic (with bonds/without bonds).
  258. err_code = ble_dfu_buttonless_char_add(&m_dfu);
  259. VERIFY_SUCCESS(err_code);
  260. return NRF_SUCCESS;
  261. }