ble_dfu.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /**
  2. * Copyright (c) 2012 - 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. /** @file
  41. *
  42. * @defgroup ble_dfu Buttonless DFU Service
  43. * @{
  44. * @ingroup ble_sdk_srv
  45. * @brief Buttonless DFU Service module.
  46. *
  47. * @details This module implements a proprietary Buttonless Secure DFU Service. The service can
  48. * be configured to support bonds or not. The bond support configuration must correspond to the
  49. * requirement of Secure DFU bootloader.
  50. *
  51. * @note Attention!
  52. * To maintain compliance with Nordic Semiconductor ASA Bluetooth profile
  53. * qualification listings, this section of source code must not be modified.
  54. */
  55. #ifndef BLE_DFU_H__
  56. #define BLE_DFU_H__
  57. #include <stdint.h>
  58. #include "ble_srv_common.h"
  59. #include "nrf_sdh_ble.h"
  60. #ifdef __cplusplus
  61. extern "C" {
  62. #endif
  63. /**@brief SoC observer priority.
  64. * @details Priority of this module's SoC event handler.
  65. */
  66. #define BLE_DFU_SOC_OBSERVER_PRIO 1
  67. #define BLE_DFU_BUTTONLESS_CHAR_UUID (0x0003) /**< Value combined with vendor-specific base to create Unbonded Buttonless characteristic UUID. */
  68. #define BLE_DFU_BUTTONLESS_BONDED_CHAR_UUID (0x0004) /**< Value combined with vendor-specific base to create Bonded Buttonless characteristic UUID. */
  69. /**@brief Nordic vendor-specific base UUID.
  70. */
  71. #define BLE_NORDIC_VENDOR_BASE_UUID \
  72. {{ \
  73. 0x50, 0xEA, 0xDA, 0x30, 0x88, 0x83, 0xB8, 0x9F, \
  74. 0x60, 0x4F, 0x15, 0xF3, 0x00, 0x00, 0xC9, 0x8E \
  75. }}
  76. /**@brief Nordic Buttonless DFU Service event type .
  77. */
  78. typedef enum
  79. {
  80. BLE_DFU_EVT_BOOTLOADER_ENTER_PREPARE, /**< Event indicating that the device is preparing to enter bootloader.*/
  81. BLE_DFU_EVT_BOOTLOADER_ENTER, /**< Event indicating that the bootloader will be entered after return of this event.*/
  82. BLE_DFU_EVT_BOOTLOADER_ENTER_FAILED, /**< Failure to enter bootloader mode.*/
  83. BLE_DFU_EVT_RESPONSE_SEND_ERROR, /**< Failure to send response.*/
  84. } ble_dfu_buttonless_evt_type_t;
  85. /**@brief Nordic Buttonless DFU Service event handler type.
  86. */
  87. typedef void (*ble_dfu_buttonless_evt_handler_t) (ble_dfu_buttonless_evt_type_t p_evt);
  88. /**@brief Enumeration of Bootloader DFU response codes.
  89. */
  90. typedef enum
  91. {
  92. DFU_RSP_INVALID = 0x00, /**< Invalid op code. */
  93. DFU_RSP_SUCCESS = 0x01, /**< Success. */
  94. DFU_RSP_OP_CODE_NOT_SUPPORTED = 0x02, /**< Op code not supported. */
  95. DFU_RSP_OPERATION_FAILED = 0x04, /**< Operation failed. */
  96. DFU_RSP_ADV_NAME_INVALID = 0x05, /**< Requested advertisement name is too short or too long. */
  97. DFU_RSP_BUSY = 0x06, /**< Ongoing async operation. */
  98. DFU_RSP_NOT_BONDED = 0x07, /**< Buttonless unavailable due to device not bonded. */
  99. } ble_dfu_buttonless_rsp_code_t;
  100. /**@brief Enumeration of Bootloader DFU Operation codes.
  101. */
  102. typedef enum
  103. {
  104. DFU_OP_RESERVED = 0x00, /**< Reserved for future use. */
  105. DFU_OP_ENTER_BOOTLOADER = 0x01, /**< Enter bootloader. */
  106. DFU_OP_SET_ADV_NAME = 0x02, /**< Set advertisement name to use in DFU mode. */
  107. // DFU_OP_ClEARFlash_ENTER_BOOTLOADER = 0x03, /**< Clear flash Enter bootloader. */
  108. DFU_OP_RESPONSE_CODE = 0x20 /**< Response code. */
  109. } ble_dfu_buttonless_op_code_t;
  110. /**@brief Type holding memory used by Secure DFU Buttonless Service.
  111. */
  112. typedef struct
  113. {
  114. uint8_t uuid_type; /**< UUID type for DFU UUID. */
  115. uint16_t service_handle; /**< Service Handle of DFU (as provided by the SoftDevice). */
  116. uint16_t conn_handle; /**< Connection handle for the current peer. */
  117. ble_gatts_char_handles_t control_point_char; /**< Handles related to the DFU Control Point characteristic. */
  118. uint32_t peers_count; /**< Counter to see how many persistently stored peers must be updated for Service Changed indication. This value will be counted down when comparing write requests. */
  119. ble_dfu_buttonless_evt_handler_t evt_handler; /**< Event handler that is called upon Buttonless DFU events. See @ref ble_dfu_buttonless_evt_type_t. */
  120. bool is_waiting_for_reset; /**< Flag indicating that the device will enter bootloader. */
  121. bool is_waiting_for_svci; /**< Flag indicating that the device is waiting for async SVCI operation */
  122. } ble_dfu_buttonless_t;
  123. /**@brief Type used to initialize the Secure DFU Buttonless Service.
  124. */
  125. typedef struct
  126. {
  127. ble_dfu_buttonless_evt_handler_t evt_handler; /**< Bootloader event handler. */
  128. } ble_dfu_buttonless_init_t;
  129. /**@brief Function for initializing the Device Firmware Update module.
  130. *
  131. * @param[in] p_dfu_init Structure containing the values of characteristics needed by the
  132. * service.
  133. * @retval NRF_SUCCESS on successful initialization of the service.
  134. */
  135. uint32_t ble_dfu_buttonless_init(const ble_dfu_buttonless_init_t * p_dfu_init);
  136. /**@brief Function for initializing the async SVCI interface.
  137. *
  138. * @warning Ensure that no interrupts are triggered when calling this functions as
  139. * interrupts and exceptions are forwarded to the bootloader for the period
  140. * of the call and may be lost.
  141. *
  142. * @details This configures the async interface for calling to the
  143. * bootloader through SVCI interface.
  144. *
  145. * @retval NRF_SUCCESS on success, otherwise an error code.
  146. */
  147. uint32_t ble_dfu_buttonless_async_svci_init(void);
  148. /**@brief Function to initialize the backend Secure DFU Buttonless service which is either
  149. * supports bonds or not.
  150. *
  151. * @note Do not call this function directly. It is called internally by @ref ble_dfu_buttonless_init.
  152. *
  153. * @param[in] p_dfu Nordic DFU Service structure.
  154. *
  155. * @return NRF_SUCCESS On sucessfully initializing, otherwise an error code.
  156. */
  157. uint32_t ble_dfu_buttonless_backend_init(ble_dfu_buttonless_t * p_dfu);
  158. /**@brief Function for adding the buttonless characteristic.
  159. *
  160. * @note This will be implemented differently on bonded/unbonded Buttonless DFU service.
  161. *
  162. * @param[in] p_dfu Nordic DFU Service structure.
  163. *
  164. * @return NRF_SUCCESS on success, otherwise an error code.
  165. */
  166. uint32_t ble_dfu_buttonless_char_add(ble_dfu_buttonless_t * p_dfu);
  167. /**@brief Function for sending a response back to the client.
  168. *
  169. * @param[in] op_code Operation code to send the response for.
  170. * @param[in] rsp_code Response code for the operation.
  171. *
  172. * @retval NRF_SUCCESS on success, otherwise an error code.
  173. */
  174. uint32_t ble_dfu_buttonless_resp_send(ble_dfu_buttonless_op_code_t op_code, ble_dfu_buttonless_rsp_code_t rsp_code);
  175. /**@brief Function for handling the application's BLE stack events.
  176. *
  177. * @details Handles all events from the BLE stack of interest to the DFU buttonless service.
  178. *
  179. * @param[in] p_ble_evt Event received from the BLE stack.
  180. * @param[in] p_context BLE context structure.
  181. */
  182. void ble_dfu_buttonless_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context);
  183. /**@brief Function for handling control point write requests.
  184. *
  185. * @details Handles write requests to the control point in
  186. * DFU with bonds or without bonds.
  187. *
  188. * @param[in] p_evt_write GATTS write event.
  189. */
  190. void ble_dfu_buttonless_on_ctrl_pt_write(ble_gatts_evt_write_t const * p_evt_write);
  191. /**@brief Function for preparing to enter the bootloader.
  192. *
  193. * @warning This function is called directly. (It is called internally).
  194. *
  195. * @retval Any error code from calling @ref sd_ble_gap_disconnect.
  196. */
  197. uint32_t ble_dfu_buttonless_bootloader_start_prepare(void);
  198. /**@brief Function for finalizing entering the bootloader.
  199. *
  200. * @warning This function is not to be called. (It is called internally).
  201. *
  202. * @retval NRF_SUCCESS Finalize was started correctly.
  203. */
  204. uint32_t ble_dfu_buttonless_bootloader_start_finalize(void);
  205. void bsp_ble_dfu_process_Initialize(void);
  206. #ifdef __cplusplus
  207. }
  208. #endif
  209. #endif // BLE_DIS_H__
  210. /** @} */