esp_websocket_client.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 _ESP_WEBSOCKET_CLIENT_H_
  14. #define _ESP_WEBSOCKET_CLIENT_H_
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #include <string.h>
  18. #include "freertos/FreeRTOS.h"
  19. #include "esp_err.h"
  20. #include "esp_event.h"
  21. #include <sys/socket.h>
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. typedef struct esp_websocket_client *esp_websocket_client_handle_t;
  26. ESP_EVENT_DECLARE_BASE(WEBSOCKET_EVENTS); // declaration of the task events family
  27. /**
  28. * @brief Websocket Client events id
  29. */
  30. typedef enum {
  31. WEBSOCKET_EVENT_ANY = -1,
  32. WEBSOCKET_EVENT_ERROR = 0, /*!< This event occurs when there are any errors during execution */
  33. WEBSOCKET_EVENT_CONNECTED, /*!< Once the Websocket has been connected to the server, no data exchange has been performed */
  34. WEBSOCKET_EVENT_DISCONNECTED, /*!< The connection has been disconnected */
  35. WEBSOCKET_EVENT_DATA, /*!< When receiving data from the server, possibly multiple portions of the packet */
  36. WEBSOCKET_EVENT_CLOSED, /*!< The connection has been closed cleanly */
  37. WEBSOCKET_EVENT_MAX
  38. } esp_websocket_event_id_t;
  39. /**
  40. * @brief Websocket event data
  41. */
  42. typedef struct {
  43. const char *data_ptr; /*!< Data pointer */
  44. int data_len; /*!< Data length */
  45. uint8_t op_code; /*!< Received opcode */
  46. esp_websocket_client_handle_t client; /*!< esp_websocket_client_handle_t context */
  47. void *user_context; /*!< user_data context, from esp_websocket_client_config_t user_data */
  48. int payload_len; /*!< Total payload length, payloads exceeding buffer will be posted through multiple events */
  49. int payload_offset; /*!< Actual offset for the data associated with this event */
  50. } esp_websocket_event_data_t;
  51. /**
  52. * @brief Websocket Client transport
  53. */
  54. typedef enum {
  55. WEBSOCKET_TRANSPORT_UNKNOWN = 0x0, /*!< Transport unknown */
  56. WEBSOCKET_TRANSPORT_OVER_TCP, /*!< Transport over tcp */
  57. WEBSOCKET_TRANSPORT_OVER_SSL, /*!< Transport over ssl */
  58. } esp_websocket_transport_t;
  59. /**
  60. * @brief Websocket client setup configuration
  61. */
  62. typedef struct {
  63. const char *uri; /*!< Websocket URI, the information on the URI can be overrides the other fields below, if any */
  64. const char *host; /*!< Domain or IP as string */
  65. int port; /*!< Port to connect, default depend on esp_websocket_transport_t (80 or 443) */
  66. const char *username; /*!< Using for Http authentication - Not supported for now */
  67. const char *password; /*!< Using for Http authentication - Not supported for now */
  68. const char *path; /*!< HTTP Path, if not set, default is `/` */
  69. bool disable_auto_reconnect; /*!< Disable the automatic reconnect function when disconnected */
  70. void *user_context; /*!< HTTP user data context */
  71. int task_prio; /*!< Websocket task priority */
  72. int task_stack; /*!< Websocket task stack */
  73. int buffer_size; /*!< Websocket buffer size */
  74. const char *cert_pem; /*!< Pointer to certificate data in PEM or DER format for server verify (with SSL), default is NULL, not required to verify the server. PEM-format must have a terminating NULL-character. DER-format requires the length to be passed in cert_len. */
  75. size_t cert_len; /*!< Length of the buffer pointed to by cert_pem. May be 0 for null-terminated pem */
  76. const char *client_cert; /*!< Pointer to certificate data in PEM or DER format for SSL mutual authentication, default is NULL, not required if mutual authentication is not needed. If it is not NULL, also `client_key` has to be provided. PEM-format must have a terminating NULL-character. DER-format requires the length to be passed in client_cert_len. */
  77. size_t client_cert_len; /*!< Length of the buffer pointed to by client_cert. May be 0 for null-terminated pem */
  78. const char *client_key; /*!< Pointer to private key data in PEM or DER format for SSL mutual authentication, default is NULL, not required if mutual authentication is not needed. If it is not NULL, also `client_cert` has to be provided. PEM-format must have a terminating NULL-character. DER-format requires the length to be passed in client_key_len */
  79. size_t client_key_len; /*!< Length of the buffer pointed to by client_key_pem. May be 0 for null-terminated pem */
  80. esp_websocket_transport_t transport; /*!< Websocket transport type, see `esp_websocket_transport_t */
  81. const char *subprotocol; /*!< Websocket subprotocol */
  82. const char *user_agent; /*!< Websocket user-agent */
  83. const char *headers; /*!< Websocket additional headers */
  84. int pingpong_timeout_sec; /*!< Period before connection is aborted due to no PONGs received */
  85. bool disable_pingpong_discon; /*!< Disable auto-disconnect due to no PONG received within pingpong_timeout_sec */
  86. bool use_global_ca_store; /*!< Use a global ca_store for all the connections in which this bool is set. */
  87. bool skip_cert_common_name_check;/*!< Skip any validation of server certificate CN field */
  88. bool keep_alive_enable; /*!< Enable keep-alive timeout */
  89. int keep_alive_idle; /*!< Keep-alive idle time. Default is 5 (second) */
  90. int keep_alive_interval; /*!< Keep-alive interval time. Default is 5 (second) */
  91. int keep_alive_count; /*!< Keep-alive packet retry send count. Default is 3 counts */
  92. size_t ping_interval_sec; /*!< Websocket ping interval, defaults to 10 seconds if not set */
  93. struct ifreq *if_name; /*!< The name of interface for data to go through. Use the default interface without setting */
  94. } esp_websocket_client_config_t;
  95. /**
  96. * @brief Start a Websocket session
  97. * This function must be the first function to call,
  98. * and it returns a esp_websocket_client_handle_t that you must use as input to other functions in the interface.
  99. * This call MUST have a corresponding call to esp_websocket_client_destroy when the operation is complete.
  100. *
  101. * @param[in] config The configuration
  102. *
  103. * @return
  104. * - `esp_websocket_client_handle_t`
  105. * - NULL if any errors
  106. */
  107. esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_client_config_t *config);
  108. /**
  109. * @brief Set URL for client, when performing this behavior, the options in the URL will replace the old ones
  110. * Must stop the WebSocket client before set URI if the client has been connected
  111. *
  112. * @param[in] client The client
  113. * @param[in] uri The uri
  114. *
  115. * @return esp_err_t
  116. */
  117. esp_err_t esp_websocket_client_set_uri(esp_websocket_client_handle_t client, const char *uri);
  118. /**
  119. * @brief Open the WebSocket connection
  120. *
  121. * @param[in] client The client
  122. *
  123. * @return esp_err_t
  124. */
  125. esp_err_t esp_websocket_client_start(esp_websocket_client_handle_t client);
  126. /**
  127. * @brief Stops the WebSocket connection without websocket closing handshake
  128. *
  129. * This API stops ws client and closes TCP connection directly without sending
  130. * close frames. It is a good practice to close the connection in a clean way
  131. * using esp_websocket_client_close().
  132. *
  133. * Notes:
  134. * - Cannot be called from the websocket event handler
  135. *
  136. * @param[in] client The client
  137. *
  138. * @return esp_err_t
  139. */
  140. esp_err_t esp_websocket_client_stop(esp_websocket_client_handle_t client);
  141. /**
  142. * @brief Destroy the WebSocket connection and free all resources.
  143. * This function must be the last function to call for an session.
  144. * It is the opposite of the esp_websocket_client_init function and must be called with the same handle as input that a esp_websocket_client_init call returned.
  145. * This might close all connections this handle has used.
  146. *
  147. * Notes:
  148. * - Cannot be called from the websocket event handler
  149. *
  150. * @param[in] client The client
  151. *
  152. * @return esp_err_t
  153. */
  154. esp_err_t esp_websocket_client_destroy(esp_websocket_client_handle_t client);
  155. /**
  156. * @brief Generic write data to the WebSocket connection; defaults to binary send
  157. *
  158. * @param[in] client The client
  159. * @param[in] data The data
  160. * @param[in] len The length
  161. * @param[in] timeout Write data timeout in RTOS ticks
  162. *
  163. * @return
  164. * - Number of data was sent
  165. * - (-1) if any errors
  166. */
  167. int esp_websocket_client_send(esp_websocket_client_handle_t client, const char *data, int len, TickType_t timeout);
  168. /**
  169. * @brief Write binary data to the WebSocket connection (data send with WS OPCODE=02, i.e. binary)
  170. *
  171. * @param[in] client The client
  172. * @param[in] data The data
  173. * @param[in] len The length
  174. * @param[in] timeout Write data timeout in RTOS ticks
  175. *
  176. * @return
  177. * - Number of data was sent
  178. * - (-1) if any errors
  179. */
  180. int esp_websocket_client_send_bin(esp_websocket_client_handle_t client, const char *data, int len, TickType_t timeout);
  181. /**
  182. * @brief Write textual data to the WebSocket connection (data send with WS OPCODE=01, i.e. text)
  183. *
  184. * @param[in] client The client
  185. * @param[in] data The data
  186. * @param[in] len The length
  187. * @param[in] timeout Write data timeout in RTOS ticks
  188. *
  189. * @return
  190. * - Number of data was sent
  191. * - (-1) if any errors
  192. */
  193. int esp_websocket_client_send_text(esp_websocket_client_handle_t client, const char *data, int len, TickType_t timeout);
  194. /**
  195. * @brief Close the WebSocket connection in a clean way
  196. *
  197. * Sequence of clean close initiated by client:
  198. * * Client sends CLOSE frame
  199. * * Client waits until server echos the CLOSE frame
  200. * * Client waits until server closes the connection
  201. * * Client is stopped the same way as by the `esp_websocket_client_stop()`
  202. *
  203. * Notes:
  204. * - Cannot be called from the websocket event handler
  205. *
  206. * @param[in] client The client
  207. * @param[in] timeout Timeout in RTOS ticks for waiting
  208. *
  209. * @return esp_err_t
  210. */
  211. esp_err_t esp_websocket_client_close(esp_websocket_client_handle_t client, TickType_t timeout);
  212. /**
  213. * @brief Close the WebSocket connection in a clean way with custom code/data
  214. * Closing sequence is the same as for esp_websocket_client_close()
  215. *
  216. * Notes:
  217. * - Cannot be called from the websocket event handler
  218. *
  219. * @param[in] client The client
  220. * @param[in] code Close status code as defined in RFC6455 section-7.4
  221. * @param[in] data Additional data to closing message
  222. * @param[in] len The length of the additional data
  223. * @param[in] timeout Timeout in RTOS ticks for waiting
  224. *
  225. * @return esp_err_t
  226. */
  227. esp_err_t esp_websocket_client_close_with_code(esp_websocket_client_handle_t client, int code, const char *data, int len, TickType_t timeout);
  228. /**
  229. * @brief Check the WebSocket client connection state
  230. *
  231. * @param[in] client The client handle
  232. *
  233. * @return
  234. * - true
  235. * - false
  236. */
  237. bool esp_websocket_client_is_connected(esp_websocket_client_handle_t client);
  238. /**
  239. * @brief Register the Websocket Events
  240. *
  241. * @param client The client handle
  242. * @param event The event id
  243. * @param event_handler The callback function
  244. * @param event_handler_arg User context
  245. * @return esp_err_t
  246. */
  247. esp_err_t esp_websocket_register_events(esp_websocket_client_handle_t client,
  248. esp_websocket_event_id_t event,
  249. esp_event_handler_t event_handler,
  250. void *event_handler_arg);
  251. #ifdef __cplusplus
  252. }
  253. #endif
  254. #endif