background_dfu_block.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**
  2. * Copyright (c) 2017 - 2018, 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_block background_dfu_block.H
  43. * @{
  44. * @ingroup background_dfu
  45. * @brief Background DFU block handling.
  46. *
  47. */
  48. #ifndef BACKGROUND_DFU_BLOCK_H_
  49. #define BACKGROUND_DFU_BLOCK_H_
  50. #include <stdint.h>
  51. #include <stdbool.h>
  52. #include "app_util_platform.h"
  53. #include "sdk_config.h"
  54. /** @brief Macro for calculating the number of blocks that fits in particular size. */
  55. #define BLOCKS_PER_SIZE(SIZE) ((SIZE + DEFAULT_BLOCK_SIZE - 1) / DEFAULT_BLOCK_SIZE)
  56. /** @brief Default block size for background DFU blocks. */
  57. #define DEFAULT_BLOCK_SIZE BACKGROUND_DFU_DEFAULT_BLOCK_SIZE
  58. /** @brief Number of blocks in superblock. */
  59. #define BLOCKS_PER_BUFFER BACKGROUND_DFU_BLOCKS_PER_BUFFER
  60. /** @brief Size of the block buffer. Shall be a multiply of @ref DEFAULT_BLOCK_SIZE. */
  61. #define BLOCK_BUFFER_SIZE (BLOCKS_PER_BUFFER * DEFAULT_BLOCK_SIZE)
  62. /** @brief Size of the bitmap reflecting the state of the blocks in a superblock. */
  63. #define BITMAP_SIZE ((BLOCKS_PER_BUFFER + 7) / 8)
  64. /** @brief Default size of DFU object. Shall be a multiply of @ref DEFAULT_BLOCK_SIZE. */
  65. #define DEFAULT_DFU_OBJECT_SIZE 4096
  66. /** @brief Number of blocks in DFU object. */
  67. #define BLOCKS_PER_DFU_OBJECT (BLOCKS_PER_SIZE(DEFAULT_DFU_OBJECT_SIZE))
  68. /** @brief Value of invalid block number (for example to indicate that no block is being stored). */
  69. #define INVALID_BLOCK_NUMBER (-1)
  70. /** @brief Result of a DFU block operation. */
  71. typedef enum
  72. {
  73. BACKGROUND_DFU_BLOCK_SUCCESS, /**< Block operation completed successfully. */
  74. BACKGROUND_DFU_BLOCK_IGNORE, /**< Block was ignored in current DFU context (i.e. duplicated block). */
  75. BACKGROUND_DFU_BLOCK_INVALID, /**< Block is invalid in current context, indicates that DFU shall be aborted. */
  76. BACKGROUND_DFU_BLOCK_STORE_ERROR /**< Block was not stored due to internal store error. */
  77. } background_dfu_block_result_t;
  78. /**@brief A function that module can register to receive block manager error notifications. */
  79. typedef void (* block_manager_result_notify_t)(background_dfu_block_result_t result,
  80. void * p_context);
  81. /**@brief Block information structure. */
  82. typedef struct
  83. {
  84. uint16_t size; /**< Size of the block in bytes. */
  85. uint32_t number; /**< Block number. */
  86. uint8_t * p_payload; /**< Block payload. */
  87. } background_dfu_block_t;
  88. /**@brief Block manager structure.
  89. *
  90. * Block manager keeps track of received blocks, ensuring that they are written into flash in
  91. * a correct order, and updates the missing blocks bitmap, so that they could be requested from
  92. * the server.
  93. */
  94. typedef struct
  95. {
  96. uint32_t image_size; /**< Size of currently stored image. */
  97. uint32_t image_type; /**< Image type (init command or firmware). */
  98. int32_t last_block_stored; /**< Number of the last block written in the flash. */
  99. int32_t current_block; /**< Last received (or expected) block. */
  100. uint8_t data[BLOCK_BUFFER_SIZE]; /**< Block buffer. */
  101. uint8_t bitmap[BITMAP_SIZE]; /**< A bitmap indicating which blocks have been received. */
  102. block_manager_result_notify_t result_handler; /**< A callback function for error notification. */
  103. void * p_context; /**< A context for result notification.*/
  104. int32_t currently_stored_block; /**< Number of block that is currently being stored. */
  105. } background_dfu_block_manager_t;
  106. /**@brief Bitmap structure used in bitmap requests. */
  107. typedef struct
  108. {
  109. uint16_t size; /**< Size of the bitmap, in bytes.*/
  110. uint16_t offset; /**< Bitmap offset, indicating which block is referenced by first bit in bitmap. */
  111. uint8_t bitmap[BITMAP_SIZE]; /**< Bitmap itself. One in specific bit indicates which block is missing. */
  112. } background_dfu_request_bitmap_t;
  113. /**@brief Initialize block manager.
  114. *
  115. * @param[inout] p_bm A pointer to the block manager.
  116. * @param[in] object_type Type of the image to store.
  117. * @param[in] object_size Size of the image to store.
  118. * @param[in] initial_block Number of the first block to receive. Typically it would be 0, but
  119. * in case DFU restarted in the middle, it may differ.
  120. * @param[in] error_handler A callback for error notification.
  121. * @param[in] p_context A context for error notification.
  122. */
  123. void block_manager_init(background_dfu_block_manager_t * p_bm,
  124. uint32_t object_type,
  125. uint32_t object_size,
  126. int32_t initial_block,
  127. block_manager_result_notify_t result_handler,
  128. void * p_context);
  129. /**@brief Process a single block.
  130. *
  131. * @param[inout] p_bm A pointer to the block manager.
  132. * @param[in] p_block A pointer to the block structure containing information about the block.
  133. *
  134. * @retval BACKGROUND_DFU_BLOCK_SUCCESS Block stored successfully.
  135. * @retval BACKGROUND_DFU_BLOCK_IGNORE Invalid block size or block already stored in flash.
  136. * @retval BACKGROUND_DFU_BLOCK_INVALID Block number indicates that too many blocks were missed.
  137. * @retval BACKGROUND_DFU_BLOCK_STORE_ERROR Block store in flash failed.
  138. */
  139. background_dfu_block_result_t block_manager_block_process(background_dfu_block_manager_t * p_bm,
  140. const background_dfu_block_t * p_block);
  141. /**@brief Check if an image managed by a block manager is complete.
  142. *
  143. * @param[in] p_bm A pointer to the block manager.
  144. *
  145. * @return True if image is complete, false otherwise.
  146. */
  147. bool block_manager_is_image_complete(const background_dfu_block_manager_t * p_bm);
  148. /**@brief Get current block bitmap.
  149. *
  150. * @param[in] p_bm A pointer to the block manager.
  151. * @param[out] p_req_bmp A pointer to the block bitmap structure.
  152. *
  153. * @return True if non-empty bitmap was generated, false otherwise.
  154. */
  155. bool block_manager_request_bitmap_get(const background_dfu_block_manager_t * p_bm,
  156. background_dfu_request_bitmap_t * p_req_bmp);
  157. /**@brief Increment current block, in case no blocks were received and block timeout shot.
  158. *
  159. * @param[in] p_bm A pointer to the block manager.
  160. *
  161. * @return True if block was incremented, false if block manager is already on a last block of the image.
  162. */
  163. bool block_manager_increment_current_block(background_dfu_block_manager_t * p_bm);
  164. /**@brief Get current block number that block manager received/expects.
  165. *
  166. * @param[in] p_bm A pointer to the block manager.
  167. *
  168. * @return Current block number.
  169. */
  170. int32_t block_manager_get_current_block(const background_dfu_block_manager_t * p_bm);
  171. #endif /* BACKGROUND_DFU_BLOCK_H_ */
  172. /** @} */