2018-09-25 04:34:04 -04:00
|
|
|
/*
|
|
|
|
* This file is subject to the terms and conditions defined in
|
|
|
|
* file 'LICENSE', which is part of this source code package.
|
|
|
|
* Tuan PM <tuanpm at live dot com>
|
|
|
|
*/
|
|
|
|
|
2018-09-26 05:56:47 -04:00
|
|
|
#ifndef _ESP_TRANSPORT_WS_H_
|
|
|
|
#define _ESP_TRANSPORT_WS_H_
|
2018-09-25 04:34:04 -04:00
|
|
|
|
|
|
|
#include "esp_transport.h"
|
2021-01-11 12:15:13 -05:00
|
|
|
#include <stdbool.h>
|
2018-09-25 04:34:04 -04:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2019-09-16 09:22:29 -04:00
|
|
|
typedef enum ws_transport_opcodes {
|
2019-11-14 05:09:38 -05:00
|
|
|
WS_TRANSPORT_OPCODES_CONT = 0x00,
|
2019-09-16 09:22:29 -04:00
|
|
|
WS_TRANSPORT_OPCODES_TEXT = 0x01,
|
|
|
|
WS_TRANSPORT_OPCODES_BINARY = 0x02,
|
|
|
|
WS_TRANSPORT_OPCODES_CLOSE = 0x08,
|
|
|
|
WS_TRANSPORT_OPCODES_PING = 0x09,
|
|
|
|
WS_TRANSPORT_OPCODES_PONG = 0x0a,
|
2020-03-25 16:22:25 -04:00
|
|
|
WS_TRANSPORT_OPCODES_FIN = 0x80,
|
2021-01-11 12:15:13 -05:00
|
|
|
WS_TRANSPORT_OPCODES_NONE = 0x100, /*!< not a valid opcode to indicate no message previously received
|
|
|
|
* from the API esp_transport_ws_get_read_opcode() */
|
2019-09-16 09:22:29 -04:00
|
|
|
} ws_transport_opcodes_t;
|
2018-09-25 04:34:04 -04:00
|
|
|
|
2021-01-11 12:15:13 -05:00
|
|
|
/**
|
|
|
|
* WS transport configuration structure
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
const char *ws_path; /*!< HTTP path to update protocol to websocket */
|
|
|
|
const char *sub_protocol; /*!< WS subprotocol */
|
|
|
|
const char *user_agent; /*!< WS user agent */
|
|
|
|
const char *headers; /*!< WS additional headers */
|
|
|
|
bool propagate_control_frames; /*!< If true, control frames are passed to the reader
|
|
|
|
* If false, only user frames are propagated, control frames are handled
|
|
|
|
* automatically during read operations
|
|
|
|
*/
|
|
|
|
} esp_transport_ws_config_t;
|
|
|
|
|
2018-09-25 04:34:04 -04:00
|
|
|
/**
|
2018-09-26 05:56:47 -04:00
|
|
|
* @brief Create web socket transport
|
2018-09-25 04:34:04 -04:00
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* - transport
|
|
|
|
* - NULL
|
|
|
|
*/
|
2018-09-26 05:56:47 -04:00
|
|
|
esp_transport_handle_t esp_transport_ws_init(esp_transport_handle_t parent_handle);
|
2018-09-25 04:34:04 -04:00
|
|
|
|
2019-06-20 03:37:40 -04:00
|
|
|
/**
|
|
|
|
* @brief Set HTTP path to update protocol to websocket
|
|
|
|
*
|
|
|
|
* @param t websocket transport handle
|
|
|
|
* @param path The HTTP Path
|
|
|
|
*/
|
2018-09-26 05:56:47 -04:00
|
|
|
void esp_transport_ws_set_path(esp_transport_handle_t t, const char *path);
|
2018-09-25 04:34:04 -04:00
|
|
|
|
2019-06-20 03:37:40 -04:00
|
|
|
/**
|
|
|
|
* @brief Set websocket sub protocol header
|
|
|
|
*
|
|
|
|
* @param t websocket transport handle
|
|
|
|
* @param sub_protocol Sub protocol string
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* - ESP_OK on success
|
|
|
|
* - One of the error codes
|
|
|
|
*/
|
|
|
|
esp_err_t esp_transport_ws_set_subprotocol(esp_transport_handle_t t, const char *sub_protocol);
|
2018-09-25 04:34:04 -04:00
|
|
|
|
2019-11-15 06:15:55 -05:00
|
|
|
/**
|
|
|
|
* @brief Set websocket user-agent header
|
|
|
|
*
|
|
|
|
* @param t websocket transport handle
|
|
|
|
* @param sub_protocol user-agent string
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* - ESP_OK on success
|
|
|
|
* - One of the error codes
|
|
|
|
*/
|
|
|
|
esp_err_t esp_transport_ws_set_user_agent(esp_transport_handle_t t, const char *user_agent);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set websocket additional headers
|
|
|
|
*
|
|
|
|
* @param t websocket transport handle
|
|
|
|
* @param sub_protocol additional header strings each terminated with \r\n
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* - ESP_OK on success
|
|
|
|
* - One of the error codes
|
|
|
|
*/
|
|
|
|
esp_err_t esp_transport_ws_set_headers(esp_transport_handle_t t, const char *headers);
|
|
|
|
|
2021-01-11 12:15:13 -05:00
|
|
|
/**
|
|
|
|
* @brief Set websocket transport parameters
|
|
|
|
*
|
|
|
|
* @param t websocket transport handle
|
|
|
|
* @param config pointer to websocket config structure
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* - ESP_OK on success
|
|
|
|
* - One of the error codes
|
|
|
|
*/
|
|
|
|
esp_err_t esp_transport_ws_set_config(esp_transport_handle_t t, const esp_transport_ws_config_t *config);
|
|
|
|
|
2019-09-16 09:22:29 -04:00
|
|
|
/**
|
|
|
|
* @brief Sends websocket raw message with custom opcode and payload
|
|
|
|
*
|
|
|
|
* Note that generic esp_transport_write for ws handle sends
|
|
|
|
* binary massages by default if size is > 0 and
|
|
|
|
* ping message if message size is set to 0.
|
|
|
|
* This API is provided to support explicit messages with arbitrary opcode,
|
|
|
|
* should it be PING, PONG or TEXT message with arbitrary data.
|
|
|
|
*
|
|
|
|
* @param[in] t Websocket transport handle
|
|
|
|
* @param[in] opcode ws operation code
|
|
|
|
* @param[in] buffer The buffer
|
|
|
|
* @param[in] len The length
|
2019-11-12 11:42:51 -05:00
|
|
|
* @param[in] timeout_ms The timeout milliseconds (-1 indicates block forever)
|
2019-09-16 09:22:29 -04:00
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* - Number of bytes was written
|
|
|
|
* - (-1) if there are any errors, should check errno
|
|
|
|
*/
|
|
|
|
int esp_transport_ws_send_raw(esp_transport_handle_t t, ws_transport_opcodes_t opcode, const char *b, int len, int timeout_ms);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns websocket op-code for last received data
|
|
|
|
*
|
|
|
|
* @param t websocket transport handle
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* - Received op-code as enum
|
|
|
|
*/
|
|
|
|
ws_transport_opcodes_t esp_transport_ws_get_read_opcode(esp_transport_handle_t t);
|
|
|
|
|
2019-11-14 05:09:38 -05:00
|
|
|
/**
|
|
|
|
* @brief Returns payload length of the last received data
|
|
|
|
*
|
|
|
|
* @param t websocket transport handle
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* - Number of bytes in the payload
|
|
|
|
*/
|
|
|
|
int esp_transport_ws_get_read_payload_len(esp_transport_handle_t t);
|
|
|
|
|
2020-07-17 11:59:05 -04:00
|
|
|
/**
|
|
|
|
* @brief Polls the active connection for termination
|
|
|
|
*
|
|
|
|
* This API is typically used by the client to wait for clean connection closure
|
|
|
|
* by websocket server
|
|
|
|
*
|
|
|
|
* @param t Websocket transport handle
|
|
|
|
* @param[in] timeout_ms The timeout milliseconds
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* 0 - no activity on read and error socket descriptor within timeout
|
|
|
|
* 1 - Success: either connection terminated by FIN or the most common RST err codes
|
|
|
|
* -1 - Failure: Unexpected error code or socket is normally readable
|
|
|
|
*/
|
|
|
|
int esp_transport_ws_poll_connection_closed(esp_transport_handle_t t, int timeout_ms);
|
2019-09-16 09:22:29 -04:00
|
|
|
|
2018-09-25 04:34:04 -04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-09-26 05:56:47 -04:00
|
|
|
#endif /* _ESP_TRANSPORT_WS_H_ */
|