ota_backend.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright (c) 2019 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief OTA backend interface
  9. */
  10. #ifndef __OTA_BACKEND_H__
  11. #define __OTA_BACKEND_H__
  12. /** type of backend*/
  13. #define OTA_BACKEND_TYPE_UNKNOWN (0)
  14. #define OTA_BACKEND_TYPE_CARD (1)
  15. #define OTA_BACKEND_TYPE_BLUETOOTH (2)
  16. #define OTA_BACKEND_TYPE_TEMP_PART (3)
  17. #define OTA_BACKEND_IOCTL_REPORT_IMAGE_VALID (0x10001)
  18. #define OTA_BACKEND_IOCTL_REPORT_PROCESS (0x10002)
  19. #define OTA_BACKEND_IOCTL_GET_UNIT_SIZE (0x10003)
  20. #define OTA_BACKEND_IOCTL_GET_MAX_SIZE (0x10004)
  21. #define OTA_BACKEND_IOCTL_GET_CONNECT_TYPE (0x10005)
  22. #define OTA_BACKEND_IOCTL_EXECUTE_EXIT (0x10006)
  23. #define OTA_BACKEND_UPGRADE_STATE (1)
  24. #define OTA_BACKEND_UPGRADE_PROGRESS (2)
  25. #define OTA_BACKEND_UPGRADE_EXIT (3)
  26. #define OTA_ERASE_ALIGN_SIZE 4096
  27. #define OTA_DATA_BUFFER_SIZE OTA_ERASE_ALIGN_SIZE
  28. struct ota_backend;
  29. typedef void (*ota_backend_notify_cb_t)(struct ota_backend *backend, int cmd, int state);
  30. /**
  31. * @brief Logger backend API.
  32. */
  33. struct ota_backend_api
  34. {
  35. /** ota backend init operation Function pointer*/
  36. struct ota_backend * (*init)(ota_backend_notify_cb_t cb, void *init_param);
  37. /** ota backend exit operation Function pointer*/
  38. void (*exit)(struct ota_backend *backend);
  39. /** ota backend open operation Function pointer*/
  40. int (*open)(struct ota_backend *backend);
  41. /** ota backend read operation Function pointer*/
  42. int (*read)(struct ota_backend *backend, int offset, unsigned char *buf, int size);
  43. /** ota backend ioctl operation Function pointer*/
  44. int (*ioctl)(struct ota_backend *backend, int cmd, unsigned int param);
  45. /** ota backend close operation Function pointer*/
  46. int (*close)(struct ota_backend *backend);
  47. /** ota backend read prepare operation Function pointer*/
  48. int (*read_prepare)(struct ota_backend *backend, int offset, unsigned char *buf, int size);
  49. /** ota backend read complete operation Function pointer*/
  50. int (*read_complete)(struct ota_backend *backend, int offset, unsigned char *buf, int size);
  51. };
  52. /** structure of backend*/
  53. struct ota_backend {
  54. /** backend api */
  55. struct ota_backend_api *api;
  56. /** backend type */
  57. int type;
  58. ota_backend_notify_cb_t cb;
  59. };
  60. #define LZMA_MAGIC 0x414d5a4c
  61. typedef struct lzma_head {
  62. uint32_t ih_magic;
  63. uint32_t ih_hdr_size; /* Size of image header (bytes). */
  64. uint32_t ih_img_size; /* Size of image body (bytes). */
  65. uint32_t ih_org_size; /* Size of origin data (bytes) */
  66. }lzma_head_t;
  67. /**
  68. * @brief get backend type
  69. *
  70. * This routine provides get backend type
  71. *
  72. * @param backend backend of ota
  73. *
  74. * @return backend type
  75. */
  76. static inline int ota_backend_get_type(struct ota_backend *backend)
  77. {
  78. __ASSERT_NO_MSG(backend);
  79. return backend->type;
  80. }
  81. /**
  82. * @brief backend ioctl calls by libota to tell user about ota state or progress.
  83. *
  84. * @param backend backend of ota
  85. *
  86. * @param cmd user define comand
  87. *
  88. * @param param comand value
  89. *
  90. * @return 0: success.
  91. *
  92. * @return other: fail.
  93. */
  94. static inline int ota_backend_ioctl(struct ota_backend *backend, int cmd,
  95. unsigned int param)
  96. {
  97. __ASSERT_NO_MSG(backend);
  98. if (backend->api->ioctl) {
  99. return backend->api->ioctl(backend, cmd, param);
  100. }
  101. return 0;
  102. }
  103. /**
  104. * @brief read data by backend,calls by libota.
  105. *
  106. * @param backend pointer to backend
  107. *
  108. * @param offs starting offset to read
  109. *
  110. * @param buf out put data buffer pointer
  111. *
  112. * @param size bytes user want to read
  113. *
  114. * @return 0: read success.
  115. *
  116. * @return other: read fail.
  117. */
  118. static inline int ota_backend_read(struct ota_backend *backend, int offset,
  119. unsigned char *buf, int size)
  120. {
  121. __ASSERT_NO_MSG(backend);
  122. return backend->api->read(backend, offset, buf, size);
  123. }
  124. /**
  125. * @brief backend open.
  126. *
  127. * @param backend pointer to backend
  128. *
  129. * @return 0: open success.
  130. *
  131. * @return other: open fail.
  132. */
  133. /**
  134. * @brief prepare to read data by backend,calls by libota.
  135. *
  136. * @param backend pointer to backend
  137. *
  138. * @param offs starting offset to read
  139. *
  140. * @param buf out put data buffer pointer
  141. *
  142. * @param size bytes user want to read
  143. *
  144. * @return 0: read success.
  145. *
  146. * @return other: read prepare fail.
  147. */
  148. static inline int ota_backend_read_prepare(struct ota_backend *backend, int offset, unsigned char *buf, int size)
  149. {
  150. __ASSERT_NO_MSG(backend);
  151. return backend->api->read_prepare(backend, offset, buf, size);
  152. }
  153. /**
  154. * @brief read data complete by backend,calls by libota.
  155. *
  156. * @param backend pointer to backend
  157. *
  158. * @param offs starting offset to read
  159. *
  160. * @param buf out put data buffer pointer
  161. *
  162. * @param size bytes user want to read
  163. *
  164. * @return 0: read complete.
  165. *
  166. * @return other: read fail.
  167. */
  168. static inline int ota_backend_read_complete(struct ota_backend *backend, int offset, unsigned char *buf, int size)
  169. {
  170. __ASSERT_NO_MSG(backend);
  171. return backend->api->read_complete(backend, offset, buf, size);
  172. }
  173. static inline int ota_backend_open(struct ota_backend *backend)
  174. {
  175. __ASSERT_NO_MSG(backend);
  176. return backend->api->open(backend);
  177. }
  178. /**
  179. * @brief backend close.
  180. *
  181. * @param backend pointer to backend
  182. *
  183. * @return 0: close success.
  184. *
  185. * @return other: close fail.
  186. */
  187. static inline int ota_backend_close(struct ota_backend *backend)
  188. {
  189. __ASSERT_NO_MSG(backend);
  190. return backend->api->close(backend);
  191. }
  192. /**
  193. * @brief backend exit.
  194. *
  195. * This routine free backend
  196. *
  197. * @param backend pointer to backend
  198. */
  199. static inline void ota_backend_exit(struct ota_backend *backend)
  200. {
  201. __ASSERT_NO_MSG(backend);
  202. return backend->api->exit(backend);
  203. }
  204. /**
  205. * @brief backend init.
  206. *
  207. * @param backend pointer to backend
  208. */
  209. static inline int ota_backend_init(struct ota_backend *backend, int type,
  210. struct ota_backend_api *api,
  211. ota_backend_notify_cb_t cb)
  212. {
  213. __ASSERT_NO_MSG(backend);
  214. backend->type = type;
  215. backend->api = api;
  216. backend->cb = cb;
  217. return 0;
  218. }
  219. #endif /* __OTA_BACKEND_H__ */