ble_dfu.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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_RESPONSE_CODE = 0x20 /**< Response code. */
  108. } ble_dfu_buttonless_op_code_t;
  109. /**@brief Type holding memory used by Secure DFU Buttonless Service.
  110. */
  111. typedef struct
  112. {
  113. uint8_t uuid_type; /**< UUID type for DFU UUID. */
  114. uint16_t service_handle; /**< Service Handle of DFU (as provided by the SoftDevice). */
  115. uint16_t conn_handle; /**< Connection handle for the current peer. */
  116. ble_gatts_char_handles_t control_point_char; /**< Handles related to the DFU Control Point characteristic. */
  117. 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. */
  118. 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. */
  119. bool is_waiting_for_reset; /**< Flag indicating that the device will enter bootloader. */
  120. bool is_waiting_for_svci; /**< Flag indicating that the device is waiting for async SVCI operation */
  121. } ble_dfu_buttonless_t;
  122. /**@brief Type used to initialize the Secure DFU Buttonless Service.
  123. */
  124. typedef struct
  125. {
  126. ble_dfu_buttonless_evt_handler_t evt_handler; /**< Bootloader event handler. */
  127. } ble_dfu_buttonless_init_t;
  128. /**@brief Function for initializing the Device Firmware Update module.
  129. *
  130. * @param[in] p_dfu_init Structure containing the values of characteristics needed by the
  131. * service.
  132. * @retval NRF_SUCCESS on successful initialization of the service.
  133. */
  134. uint32_t ble_dfu_buttonless_init(const ble_dfu_buttonless_init_t * p_dfu_init);
  135. /**@brief Function for initializing the async SVCI interface.
  136. *
  137. * @warning Ensure that no interrupts are triggered when calling this functions as
  138. * interrupts and exceptions are forwarded to the bootloader for the period
  139. * of the call and may be lost.
  140. *
  141. * @details This configures the async interface for calling to the
  142. * bootloader through SVCI interface.
  143. *
  144. * @retval NRF_SUCCESS on success, otherwise an error code.
  145. */
  146. uint32_t ble_dfu_buttonless_async_svci_init(void);
  147. /**@brief Function to initialize the backend Secure DFU Buttonless service which is either
  148. * supports bonds or not.
  149. *
  150. * @note Do not call this function directly. It is called internally by @ref ble_dfu_buttonless_init.
  151. *
  152. * @param[in] p_dfu Nordic DFU Service structure.
  153. *
  154. * @return NRF_SUCCESS On sucessfully initializing, otherwise an error code.
  155. */
  156. uint32_t ble_dfu_buttonless_backend_init(ble_dfu_buttonless_t * p_dfu);
  157. /**@brief Function for adding the buttonless characteristic.
  158. *
  159. * @note This will be implemented differently on bonded/unbonded Buttonless DFU service.
  160. *
  161. * @param[in] p_dfu Nordic DFU Service structure.
  162. *
  163. * @return NRF_SUCCESS on success, otherwise an error code.
  164. */
  165. uint32_t ble_dfu_buttonless_char_add(ble_dfu_buttonless_t * p_dfu);
  166. /**@brief Function for sending a response back to the client.
  167. *
  168. * @param[in] op_code Operation code to send the response for.
  169. * @param[in] rsp_code Response code for the operation.
  170. *
  171. * @retval NRF_SUCCESS on success, otherwise an error code.
  172. */
  173. uint32_t ble_dfu_buttonless_resp_send(ble_dfu_buttonless_op_code_t op_code, ble_dfu_buttonless_rsp_code_t rsp_code);
  174. /**@brief Function for handling the application's BLE stack events.
  175. *
  176. * @details Handles all events from the BLE stack of interest to the DFU buttonless service.
  177. *
  178. * @param[in] p_ble_evt Event received from the BLE stack.
  179. * @param[in] p_context BLE context structure.
  180. */
  181. void ble_dfu_buttonless_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context);
  182. /**@brief Function for handling control point write requests.
  183. *
  184. * @details Handles write requests to the control point in
  185. * DFU with bonds or without bonds.
  186. *
  187. * @param[in] p_evt_write GATTS write event.
  188. */
  189. void ble_dfu_buttonless_on_ctrl_pt_write(ble_gatts_evt_write_t const * p_evt_write);
  190. /**@brief Function for preparing to enter the bootloader.
  191. *
  192. * @warning This function is called directly. (It is called internally).
  193. *
  194. * @retval Any error code from calling @ref sd_ble_gap_disconnect.
  195. */
  196. uint32_t ble_dfu_buttonless_bootloader_start_prepare(void);
  197. /**@brief Function for finalizing entering the bootloader.
  198. *
  199. * @warning This function is not to be called. (It is called internally).
  200. *
  201. * @retval NRF_SUCCESS Finalize was started correctly.
  202. */
  203. uint32_t ble_dfu_buttonless_bootloader_start_finalize(void);
  204. #ifdef __cplusplus
  205. }
  206. #endif
  207. #endif // BLE_DIS_H__
  208. /** @} */