lightduer_ota_downloader.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /**
  2. * Copyright (2017) Baidu Inc. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /**
  17. * File: lightduer_ota_downloader.h
  18. * Auth: Zhong Shuai (zhongshuai@baidu.com)
  19. * Desc: OTA Downloader Head File
  20. */
  21. #ifndef BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_DOWNLOADER_H
  22. #define BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_DOWNLOADER_H
  23. #include "lightduer_mutex.h"
  24. #include "lightduer_http_client.h"
  25. #ifndef URL_LEN
  26. #define URL_LEN 301
  27. #endif
  28. typedef enum _duer_downloader_Protocol {
  29. HTTP = 0,
  30. COAP = 1,
  31. LOCAL = 2,
  32. MAX_PROTOCOL_COUNT = 3,
  33. } duer_downloader_protocol;
  34. typedef struct _duer_ota_downloader_s {
  35. duer_downloader_protocol dp;
  36. void *private_data; // Set the private data you want to pass
  37. duer_mutex_t lock;
  38. char url[URL_LEN + 1];
  39. struct _duer_ota_downloader_ops_s *ops;
  40. } duer_ota_downloader_t ;
  41. typedef int (*data_handler)(
  42. void* private_data,
  43. const char *buf,
  44. size_t len);
  45. /*
  46. * We need to consider unblock & timeout situation (TBD)
  47. * connect_server callback function requires you that
  48. * connect to server and receive the data immediately
  49. * I think the abstract is not very good. Refactor (TBD)
  50. */
  51. typedef struct _duer_ota_downloader_ops_s {
  52. int (*init)(duer_ota_downloader_t *downloader);
  53. int (*register_data_notify)(duer_ota_downloader_t *downloader, data_handler handler, void *private_data);
  54. int (*connect_server)(duer_ota_downloader_t *downloader, const char *url);
  55. int (*disconnect_server)(duer_ota_downloader_t *downloader);
  56. int (*destroy)(duer_ota_downloader_t *downloader);
  57. } duer_ota_downloader_ops_t;
  58. /*
  59. * Initialise OTA Downloader Module
  60. *
  61. * @param void:
  62. *
  63. * @return int: Success: DUER_OK
  64. * Failed: Other
  65. */
  66. extern int duer_init_ota_downloader(void);
  67. /*
  68. * Uninitialise OTA Downloader Module
  69. *
  70. * @param void:
  71. *
  72. * @return int: Success: DUER_OK
  73. * Failed: Other
  74. */
  75. extern int duer_uninit_ota_downloader(void);
  76. /*
  77. * Create OTA Downloader
  78. *
  79. * @param void:
  80. *
  81. * @return duer_ota_downloader_t *: Success: duer_ota_downloader_t *
  82. * Failed: NULL
  83. */
  84. extern duer_ota_downloader_t *duer_ota_downloader_create_downloader(void);
  85. /*
  86. * Initialise OTA Downloader
  87. *
  88. * @param downloader: OTA Downloader object
  89. *
  90. * @return int: Success: DUER_OK
  91. * Failed: Other
  92. */
  93. extern int duer_ota_downloader_init_downloader(duer_ota_downloader_t *downloader);
  94. /*
  95. * Register a OTA Downloader to OTA Downloader module
  96. *
  97. * @param downloader: OTA Downloader object
  98. *
  99. * @param dp: Download protocol which the downloader support
  100. *
  101. * @return int: Success: DUER_OK
  102. * Failed: Other
  103. */
  104. extern int duer_ota_downloader_register_downloader(
  105. duer_ota_downloader_t *downloader,
  106. duer_downloader_protocol dp);
  107. /*
  108. * Unregister a OTA Downloader from OTA Downloader module
  109. *
  110. * @param dp: Download protocol
  111. *
  112. * @return int: Success: DUER_OK
  113. * Failed: Other
  114. */
  115. extern int duer_ota_downloader_unregister_downloader(duer_downloader_protocol dp);
  116. /*
  117. * Get a OTA Downloader from OTA Downloader module
  118. *
  119. * @param dp: Download protocol which you want
  120. *
  121. * @return duer_ota_downloader_t *: Success: duer_ota_downloader_t *
  122. * Failed: NULL
  123. */
  124. extern duer_ota_downloader_t *duer_ota_downloader_get_downloader(duer_downloader_protocol dp);
  125. /*
  126. * Destroy a OTA Downloader, free the memory and unregister it from OTA Downloader module
  127. *
  128. * @param downloader: OTA downloader object
  129. *
  130. * @return int: Success: DUER_OK
  131. * Failed: Other
  132. */
  133. extern int duer_ota_downloader_destroy_downloader(duer_ota_downloader_t *downloader);
  134. /*
  135. * Register the downloader operations to a Downloader object
  136. * If you want to support a new downloader protocol, you need to implement duer_ota_downloader_ops
  137. *
  138. * @param downloader: OTA Downloader object
  139. *
  140. * @param downloader_ops: OTA Downloader operations
  141. *
  142. * @return int: Success: DUER_OK
  143. * Failed: Other
  144. */
  145. extern int duer_ota_downloader_register_downloader_ops(
  146. duer_ota_downloader_t *downloader,
  147. duer_ota_downloader_ops_t *downloader_ops);
  148. /*
  149. * Connect to server to get the data
  150. * Note: Once you call it, the OTADownloader will download the data from server and pass it to you
  151. *
  152. * @param downloader: OTA Downloader object
  153. *
  154. * @param url: URL
  155. *
  156. * @return int: Success: DUER_OK
  157. * Failed: Other
  158. */
  159. extern int duer_ota_downloader_connect_server(duer_ota_downloader_t *downloader, const char *url);
  160. /*
  161. * Disconnect server
  162. * Close the socket
  163. *
  164. * @param downloader: OTA Downloader object
  165. *
  166. * @return int: Success: DUER_OK
  167. * Failed: Other
  168. */
  169. extern int duer_ota_downloader_disconnect_server(duer_ota_downloader_t *downloader);
  170. /*
  171. * Register a callback functions "hancler" to receive the data which get from URL
  172. *
  173. * @param downloader: OTA Downloader object
  174. *
  175. * @param handler: Callback function to receive the data which get form URL
  176. *
  177. * @param private: param which you want to pass to handler
  178. *
  179. * @return int: Success: DUER_OK
  180. * Failed: Other
  181. */
  182. extern int duer_ota_downloader_register_data_notify(
  183. duer_ota_downloader_t *downloader,
  184. data_handler handler,
  185. void *private_data);
  186. /*
  187. * Set the private data you want pass to the OTA downloader OPS
  188. *
  189. * @param downloader: OTA Downloader object
  190. *
  191. * @param private_data: data which you want to pass
  192. *
  193. * @return int: Success: DUER_OK
  194. * Failed: Other
  195. */
  196. extern int duer_ota_downloader_set_private_data(
  197. duer_ota_downloader_t *downloader,
  198. void *private_data);
  199. /*
  200. * Get the private data
  201. *
  202. * @param downloader: OTA Downloader object
  203. *
  204. * @return void *: Success: Other
  205. * Failed: NULL
  206. */
  207. extern void *duer_ota_downloader_get_private_data(duer_ota_downloader_t *downloader);
  208. #endif // BAIDU_DUER_LIGHTDUER_INCLUDE_LIGHTDUER_OTA_DOWNLOADER_H