background_dfu_state.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /**
  2. * Copyright (c) 2017 - 2019, 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 background_dfu_state background_dfu_state.h
  43. * @{
  44. * @ingroup background_dfu
  45. * @brief Background DFU state management.
  46. *
  47. */
  48. #ifndef BACKGROUND_DFU_STATE_H_
  49. #define BACKGROUND_DFU_STATE_H_
  50. #include <stdint.h>
  51. #include <stdbool.h>
  52. #include "background_dfu_block.h"
  53. #include "nrf_dfu_req_handler.h"
  54. /** @brief DFU client state ID.
  55. *
  56. * We reuse DFU object type IDs as DFU process states IDs,
  57. * so that current state can be used as the object type in
  58. * function which expect one.
  59. */
  60. typedef enum
  61. {
  62. BACKGROUND_DFU_DOWNLOAD_INIT_CMD = NRF_DFU_OBJ_TYPE_COMMAND,
  63. BACKGROUND_DFU_DOWNLOAD_FIRMWARE = NRF_DFU_OBJ_TYPE_DATA,
  64. BACKGROUND_DFU_DOWNLOAD_TRIG,
  65. BACKGROUND_DFU_WAIT_FOR_RESET,
  66. BACKGROUND_DFU_IDLE,
  67. BACKGROUND_DFU_ERROR,
  68. } background_dfu_state_t;
  69. /** @brief DFU event definitions. */
  70. typedef enum
  71. {
  72. BACKGROUND_DFU_EVENT_TRANSFER_COMPLETE,
  73. BACKGROUND_DFU_EVENT_TRANSFER_CONTINUE,
  74. BACKGROUND_DFU_EVENT_TRANSFER_ERROR,
  75. BACKGROUND_DFU_EVENT_PROCESSING_ERROR,
  76. } background_dfu_event_t;
  77. /** @brief DFU mode definitions. */
  78. typedef enum
  79. {
  80. BACKGROUND_DFU_MODE_UNICAST,
  81. BACKGROUND_DFU_MODE_MULTICAST
  82. } background_dfu_mode_t;
  83. /** @brief Trigger packet structure. */
  84. typedef PACKED_STRUCT
  85. {
  86. uint8_t flags; /**< Trigger message flags. Bits 7:4 (oldest) - trigger version, bit 3 - DFU mode, bits 2:0 - reserved. */
  87. uint32_t init_length;
  88. uint32_t init_crc;
  89. uint32_t image_length;
  90. uint32_t image_crc;
  91. } background_dfu_trigger_t;
  92. /**@brief Structure with DFU diagnostic information. */
  93. typedef PACKED_STRUCT background_dfu_diagnostic
  94. {
  95. uint32_t build_id; /**< Build identifier, based on compilation time. */
  96. uint8_t state; /**< Current DFU state. */
  97. uint8_t prev_state; /**< Previous DFU state. */
  98. uint16_t init_blocks_requested; /**< Number of requested missing init blocks. */
  99. uint16_t image_blocks_requested; /**< Number of requested missing image blocks. */
  100. uint16_t triggers_received; /**< Number of triggers received. */
  101. uint16_t total_init_blocks_received; /**< Total number of init blocks received, including retransmitted ones. */
  102. uint16_t total_image_blocks_received; /**< Total number of image blocks received, including retransmitted ones. */
  103. } background_dfu_diagnostic_t;
  104. /** @brief DFU client state. */
  105. typedef struct dfu_context
  106. {
  107. background_dfu_state_t dfu_state; /**< Current DFU client state. */
  108. background_dfu_mode_t dfu_mode; /**< Current DFU mode. */
  109. bool reset_suppress; /**< If set then device won't automatically reset after
  110. downloading firmware. */
  111. background_dfu_diagnostic_t dfu_diag; /**< DFU diagnostic information. */
  112. uint32_t init_cmd_size; /**< Current init command size. */
  113. uint32_t init_cmd_crc; /**< Current init command checksum. */
  114. uint32_t firmware_size; /**< Current firmware command size. */
  115. uint32_t firmware_crc; /**< Current firmware command checksum. */
  116. uint32_t max_obj_size; /**< Maximum size of the DFU object. */
  117. uint32_t remaining_size; /**< Remaining size, in bytes, of the resource which
  118. is being downloaded. */
  119. /* TODO Move the block num to the block manager. */
  120. uint32_t block_num; /**< Currently requested block number. */
  121. uint32_t * p_resource_size; /**< Downloaded resource size. */
  122. background_dfu_block_manager_t block_manager; /**< An entity managing block reception and storage. */
  123. uint8_t retry_count; /**< Number of remaining retires. */
  124. } background_dfu_context_t;
  125. /**@brief Check if payload contains valid trigger.
  126. *
  127. * @param[inout] p_dfu_ctx DFU context.
  128. * @param[in] p_payload A pointer to the message payload.
  129. * @param[in[ payload_len Payload length.
  130. *
  131. * @return True if trigger was valid, false otherwise.
  132. */
  133. bool background_dfu_validate_trigger(background_dfu_context_t * p_dfu_ctx,
  134. const uint8_t * p_payload,
  135. uint32_t payload_len);
  136. /**@brief Process a payload with a DFU trigger.
  137. *
  138. * @param[inout] p_dfu_ctx DFU context.
  139. * @param[in] p_payload A pointer to the message payload.
  140. * @param[in[ payload_len Payload length.
  141. *
  142. * @return True if trigger was successfully processed, false otherwise.
  143. */
  144. bool background_dfu_process_trigger(background_dfu_context_t * p_dfu_ctx,
  145. const uint8_t * p_payload,
  146. uint32_t payload_len);
  147. /**@brief Process the block and return CoAP result code corresponding to the result of operation.
  148. *
  149. * @param[inout] p_dfu_ctx DFU context.
  150. * @param[in] p_block A pointer to the block structure.
  151. *
  152. * @return True if init command is valid, false otherwise.
  153. *
  154. * @retval BACKGROUND_DFU_BLOCK_SUCCESS The block was processed correctly.
  155. * @retval BACKGROUND_DFU_BLOCK_IGNORE The block was incorrect for this node, but did not
  156. * indicate that the DFU shall be stopped.
  157. * @retval BACKGROUND_DFU_BLOCK_INVALID The block indicated that the node would not catch-up with the DFU
  158. * transfer or other error occured. Node aborted DFU.
  159. * @retval BACKGROUND_DFU_BLOCK_STORE_ERROR DFU store error.
  160. */
  161. background_dfu_block_result_t background_dfu_process_block(background_dfu_context_t * p_dfu_ctx,
  162. const background_dfu_block_t * p_block);
  163. /**@brief DFU state machine handler.
  164. *
  165. * @param[in] p_dfu_ctx DFU context.
  166. * @param[in] event DFU event.
  167. *
  168. * @return NRF_SUCCESS or error code
  169. */
  170. uint32_t background_dfu_handle_event(background_dfu_context_t * p_dfu_ctx,
  171. background_dfu_event_t event);
  172. /**@brief Reset state machine state.
  173. *
  174. * @param[in] p_dfu_ctx A pointer to DFU client context.
  175. */
  176. void background_dfu_reset_state(background_dfu_context_t * p_dfu_ctx);
  177. /**@brief Initialize state machine state.
  178. *
  179. * @param[in] p_dfu_ctx A pointer to DFU client context.
  180. */
  181. void background_dfu_state_init(background_dfu_context_t * p_dfu_ctx);
  182. /**@brief Convert a DFU event enum value to a string description.
  183. *
  184. * @param[in] event A DFU event.
  185. *
  186. * @return String representing the event.
  187. */
  188. const char * background_dfu_event_to_string(const background_dfu_event_t event);
  189. /**@brief Convert a DFU state enum value to a string description.
  190. *
  191. * @param[in] event A DFU state.
  192. *
  193. * @return String representing the state.
  194. */
  195. const char * background_dfu_state_to_string(const background_dfu_state_t state);
  196. #endif /* BACKGROUND_DFU_STATE_H_ */