esp_websocket_client.h 12 KB

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