esp_websocket_client.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 "esp_event_loop.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_MAX
  37. } esp_websocket_event_id_t;
  38. /**
  39. * @brief Websocket event data
  40. */
  41. typedef struct {
  42. const char *data_ptr; /*!< Data pointer */
  43. int data_len; /*!< Data length */
  44. } esp_websocket_event_data_t;
  45. /**
  46. * @brief Websocket Client transport
  47. */
  48. typedef enum {
  49. WEBSOCKET_TRANSPORT_UNKNOWN = 0x0, /*!< Transport unknown */
  50. WEBSOCKET_TRANSPORT_OVER_TCP, /*!< Transport over tcp */
  51. WEBSOCKET_TRANSPORT_OVER_SSL, /*!< Transport over ssl */
  52. } esp_websocket_transport_t;
  53. /**
  54. * @brief Websocket Client events data
  55. */
  56. typedef struct {
  57. esp_websocket_event_id_t event_id; /*!< event_id, to know the cause of the event */
  58. esp_websocket_client_handle_t client; /*!< esp_websocket_client_handle_t context */
  59. void *user_context;/*!< user_data context, from esp_websocket_client_config_t user_data */
  60. char *data; /*!< data of the event */
  61. int data_len; /*!< length of data */
  62. } esp_websocket_event_t;
  63. typedef esp_websocket_event_t* esp_websocket_event_handle_t;
  64. typedef esp_err_t (* websocket_event_callback_t)(esp_websocket_event_handle_t event);
  65. /**
  66. * @brief Websocket client setup configuration
  67. */
  68. typedef struct {
  69. const char *uri; /*!< Websocket URI, the information on the URI can be overrides the other fields below, if any */
  70. const char *host; /*!< Domain or IP as string */
  71. int port; /*!< Port to connect, default depend on esp_websocket_transport_t (80 or 443) */
  72. const char *username; /*!< Using for Http authentication - Not supported for now */
  73. const char *password; /*!< Using for Http authentication - Not supported for now */
  74. const char *path; /*!< HTTP Path, if not set, default is `/` */
  75. bool disable_auto_reconnect; /*!< Disable the automatic reconnect function when disconnected */
  76. void *user_context; /*!< HTTP user data context */
  77. int task_prio; /*!< Websocket task priority */
  78. int task_stack; /*!< Websocket task stack */
  79. int buffer_size; /*!< Websocket buffer size */
  80. const char *cert_pem; /*!< SSL Certification, PEM format as string, if the client requires to verify server */
  81. esp_websocket_transport_t transport; /*!< Websocket transport type, see `esp_websocket_transport_t */
  82. } esp_websocket_client_config_t;
  83. /**
  84. * @brief Start a Websocket session
  85. * This function must be the first function to call,
  86. * and it returns a esp_websocket_client_handle_t that you must use as input to other functions in the interface.
  87. * This call MUST have a corresponding call to esp_websocket_client_destroy when the operation is complete.
  88. *
  89. * @param[in] config The configuration
  90. *
  91. * @return
  92. * - `esp_websocket_client_handle_t`
  93. * - NULL if any errors
  94. */
  95. esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_client_config_t *config);
  96. /**
  97. * @brief Set URL for client, when performing this behavior, the options in the URL will replace the old ones
  98. * Must stop the WebSocket client before set URI if the client has been connected
  99. *
  100. * @param[in] client The client
  101. * @param[in] uri The uri
  102. *
  103. * @return esp_err_t
  104. */
  105. esp_err_t esp_websocket_client_set_uri(esp_websocket_client_handle_t client, const char *uri);
  106. /**
  107. * @brief Open the WebSocket connection
  108. *
  109. * @param[in] client The client
  110. *
  111. * @return esp_err_t
  112. */
  113. esp_err_t esp_websocket_client_start(esp_websocket_client_handle_t client);
  114. /**
  115. * @brief Close the WebSocket connection
  116. *
  117. * @param[in] client The client
  118. *
  119. * @return esp_err_t
  120. */
  121. esp_err_t esp_websocket_client_stop(esp_websocket_client_handle_t client);
  122. /**
  123. * @brief Destroy the WebSocket connection and free all resources.
  124. * This function must be the last function to call for an session.
  125. * 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.
  126. * This might close all connections this handle has used.
  127. *
  128. * @param[in] client The client
  129. *
  130. * @return esp_err_t
  131. */
  132. esp_err_t esp_websocket_client_destroy(esp_websocket_client_handle_t client);
  133. /**
  134. * @brief Write data to the WebSocket connection
  135. *
  136. * @param[in] client The client
  137. * @param[in] data The data
  138. * @param[in] len The length
  139. * @param[in] timeout Write data timeout
  140. *
  141. * @return
  142. * - Number of data was sent
  143. * - (-1) if any errors
  144. */
  145. int esp_websocket_client_send(esp_websocket_client_handle_t client, const char *data, int len, TickType_t timeout);
  146. /**
  147. * @brief Check the WebSocket connection status
  148. *
  149. * @param[in] client The client handle
  150. *
  151. * @return
  152. * - true
  153. * - false
  154. */
  155. bool esp_websocket_client_is_connected(esp_websocket_client_handle_t client);
  156. /**
  157. * @brief Register the Websocket Events
  158. *
  159. * @param client The client handle
  160. * @param event The event id
  161. * @param event_handler The callback function
  162. * @param event_handler_arg User context
  163. * @return esp_err_t
  164. */
  165. esp_err_t esp_websocket_register_events(esp_websocket_client_handle_t client,
  166. esp_websocket_event_id_t event,
  167. esp_event_handler_t event_handler,
  168. void* event_handler_arg);
  169. #ifdef __cplusplus
  170. }
  171. #endif
  172. #endif