ble_dfu.c 11 KB

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