transport.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #ifndef _TRANSPORT_H_
  14. #define _TRANSPORT_H_
  15. #include <esp_err.h>
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. typedef struct transport_list_t* transport_list_handle_t;
  20. typedef struct transport_item_t* transport_handle_t;
  21. typedef int (*connect_func)(transport_handle_t t, const char *host, int port, int timeout_ms);
  22. typedef int (*io_func)(transport_handle_t t, const char *buffer, int len, int timeout_ms);
  23. typedef int (*io_read_func)(transport_handle_t t, char *buffer, int len, int timeout_ms);
  24. typedef int (*trans_func)(transport_handle_t t);
  25. typedef int (*poll_func)(transport_handle_t t, int timeout_ms);
  26. /**
  27. * @brief Create transport list
  28. *
  29. * @return A handle can hold all transports
  30. */
  31. transport_list_handle_t transport_list_init();
  32. /**
  33. * @brief Cleanup and free all transports, include itself,
  34. * this function will invoke transport_destroy of every transport have added this the list
  35. *
  36. * @param[in] list The list
  37. *
  38. * @return
  39. * - ESP_OK
  40. * - ESP_FAIL
  41. */
  42. esp_err_t transport_list_destroy(transport_list_handle_t list);
  43. /**
  44. * @brief Add a transport to the list, and define a scheme to indentify this transport in the list
  45. *
  46. * @param[in] list The list
  47. * @param[in] t The Transport
  48. * @param[in] scheme The scheme
  49. *
  50. * @return
  51. * - ESP_OK
  52. */
  53. esp_err_t transport_list_add(transport_list_handle_t list, transport_handle_t t, const char *scheme);
  54. /**
  55. * @brief This function will remove all transport from the list,
  56. * invoke transport_destroy of every transport have added this the list
  57. *
  58. * @param[in] list The list
  59. *
  60. * @return
  61. * - ESP_OK
  62. * - ESP_ERR_INVALID_ARG
  63. */
  64. esp_err_t transport_list_clean(transport_list_handle_t list);
  65. /**
  66. * @brief Get the transport by scheme, which has been defined when calling function `transport_list_add`
  67. *
  68. * @param[in] list The list
  69. * @param[in] tag The tag
  70. *
  71. * @return The transport handle
  72. */
  73. transport_handle_t transport_list_get_transport(transport_list_handle_t list, const char *scheme);
  74. /**
  75. * @brief Initialize a transport handle object
  76. *
  77. * @return The transport handle
  78. */
  79. transport_handle_t transport_init();
  80. /**
  81. * @brief Cleanup and free memory the transport
  82. *
  83. * @param[in] t The transport handle
  84. *
  85. * @return
  86. * - ESP_OK
  87. * - ESP_FAIL
  88. */
  89. esp_err_t transport_destroy(transport_handle_t t);
  90. /**
  91. * @brief Get default port number used by this transport
  92. *
  93. * @param[in] t The transport handle
  94. *
  95. * @return the port number
  96. */
  97. int transport_get_default_port(transport_handle_t t);
  98. /**
  99. * @brief Set default port number that can be used by this transport
  100. *
  101. * @param[in] t The transport handle
  102. * @param[in] port The port number
  103. *
  104. * @return
  105. * - ESP_OK
  106. * - ESP_FAIL
  107. */
  108. esp_err_t transport_set_default_port(transport_handle_t t, int port);
  109. /**
  110. * @brief Transport connection function, to make a connection to server
  111. *
  112. * @param t The transport handle
  113. * @param[in] host Hostname
  114. * @param[in] port Port
  115. * @param[in] timeout_ms The timeout milliseconds
  116. *
  117. * @return
  118. * - socket for will use by this transport
  119. * - (-1) if there are any errors, should check errno
  120. */
  121. int transport_connect(transport_handle_t t, const char *host, int port, int timeout_ms);
  122. /**
  123. * @brief Transport read function
  124. *
  125. * @param t The transport handle
  126. * @param buffer The buffer
  127. * @param[in] len The length
  128. * @param[in] timeout_ms The timeout milliseconds
  129. *
  130. * @return
  131. * - Number of bytes was read
  132. * - (-1) if there are any errors, should check errno
  133. */
  134. int transport_read(transport_handle_t t, char *buffer, int len, int timeout_ms);
  135. /**
  136. * @brief Poll the transport until readable or timeout
  137. *
  138. * @param[in] t The transport handle
  139. * @param[in] timeout_ms The timeout milliseconds
  140. *
  141. * @return
  142. * - 0 Timeout
  143. * - (-1) If there are any errors, should check errno
  144. * - other The transport can read
  145. */
  146. int transport_poll_read(transport_handle_t t, int timeout_ms);
  147. /**
  148. * @brief Transport write function
  149. *
  150. * @param t The transport handle
  151. * @param buffer The buffer
  152. * @param[in] len The length
  153. * @param[in] timeout_ms The timeout milliseconds
  154. *
  155. * @return
  156. * - Number of bytes was written
  157. * - (-1) if there are any errors, should check errno
  158. */
  159. int transport_write(transport_handle_t t, const char *buffer, int len, int timeout_ms);
  160. /**
  161. * @brief Poll the transport until writeable or timeout
  162. *
  163. * @param[in] t The transport handle
  164. * @param[in] timeout_ms The timeout milliseconds
  165. *
  166. * @return
  167. * - 0 Timeout
  168. * - (-1) If there are any errors, should check errno
  169. * - other The transport can write
  170. */
  171. int transport_poll_write(transport_handle_t t, int timeout_ms);
  172. /**
  173. * @brief Transport close
  174. *
  175. * @param t The transport handle
  176. *
  177. * @return
  178. * - 0 if ok
  179. * - (-1) if there are any errors, should check errno
  180. */
  181. int transport_close(transport_handle_t t);
  182. /**
  183. * @brief Get user data context of this transport
  184. *
  185. * @param[in] t The transport handle
  186. *
  187. * @return The user data context
  188. */
  189. void *transport_get_context_data(transport_handle_t t);
  190. /**
  191. * @brief Set the user context data for this transport
  192. *
  193. * @param[in] t The transport handle
  194. * @param data The user data context
  195. *
  196. * @return
  197. * - ESP_OK
  198. */
  199. esp_err_t transport_set_context_data(transport_handle_t t, void *data);
  200. /**
  201. * @brief Set transport functions for the transport handle
  202. *
  203. * @param[in] t The transport handle
  204. * @param[in] _connect The connect function pointer
  205. * @param[in] _read The read function pointer
  206. * @param[in] _write The write function pointer
  207. * @param[in] _close The close function pointer
  208. * @param[in] _poll_read The poll read function pointer
  209. * @param[in] _poll_write The poll write function pointer
  210. * @param[in] _destroy The destroy function pointer
  211. *
  212. * @return
  213. * - ESP_OK
  214. */
  215. esp_err_t transport_set_func(transport_handle_t t,
  216. connect_func _connect,
  217. io_read_func _read,
  218. io_func _write,
  219. trans_func _close,
  220. poll_func _poll_read,
  221. poll_func _poll_write,
  222. trans_func _destroy);
  223. #ifdef __cplusplus
  224. }
  225. #endif
  226. #endif