diff --git a/components/esp_http_client/esp_http_client.c b/components/esp_http_client/esp_http_client.c index 163cc41a67..27181f3ec1 100644 --- a/components/esp_http_client/esp_http_client.c +++ b/components/esp_http_client/esp_http_client.c @@ -93,8 +93,8 @@ struct esp_http_client { int process_again; struct http_parser *parser; struct http_parser_settings *parser_settings; - transport_list_handle_t transport_list; - transport_handle_t transport; + esp_transport_list_handle_t transport_list; + esp_transport_handle_t transport; esp_http_data_t *request; esp_http_data_t *response; void *user_data; @@ -443,7 +443,7 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co { esp_http_client_handle_t client; - transport_handle_t tcp; + esp_transport_handle_t tcp; bool _success; _success = ( @@ -466,10 +466,10 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co } _success = ( - (client->transport_list = transport_list_init()) && - (tcp = transport_tcp_init()) && - (transport_set_default_port(tcp, DEFAULT_HTTP_PORT) == ESP_OK) && - (transport_list_add(client->transport_list, tcp, "http") == ESP_OK) + (client->transport_list = esp_transport_list_init()) && + (tcp = esp_transport_tcp_init()) && + (esp_transport_set_default_port(tcp, DEFAULT_HTTP_PORT) == ESP_OK) && + (esp_transport_list_add(client->transport_list, tcp, "http") == ESP_OK) ); if (!_success) { ESP_LOGE(TAG, "Error initialize transport"); @@ -477,11 +477,11 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co return NULL; } #ifdef CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS - transport_handle_t ssl; + esp_transport_handle_t ssl; _success = ( - (ssl = transport_ssl_init()) && - (transport_set_default_port(ssl, DEFAULT_HTTPS_PORT) == ESP_OK) && - (transport_list_add(client->transport_list, ssl, "https") == ESP_OK) + (ssl = esp_transport_ssl_init()) && + (esp_transport_set_default_port(ssl, DEFAULT_HTTPS_PORT) == ESP_OK) && + (esp_transport_list_add(client->transport_list, ssl, "https") == ESP_OK) ); if (!_success) { @@ -491,7 +491,7 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co } if (config->cert_pem) { - transport_ssl_set_cert_data(ssl, config->cert_pem, strlen(config->cert_pem)); + esp_transport_ssl_set_cert_data(ssl, config->cert_pem, strlen(config->cert_pem)); } #endif @@ -545,7 +545,7 @@ esp_err_t esp_http_client_cleanup(esp_http_client_handle_t client) return ESP_FAIL; } esp_http_client_close(client); - transport_list_destroy(client->transport_list); + esp_transport_list_destroy(client->transport_list); http_header_destroy(client->request->headers); free(client->request->buffer->data); free(client->request->buffer); @@ -740,7 +740,7 @@ static int esp_http_client_get_data(esp_http_client_handle_t client) ESP_LOGD(TAG, "data_process=%d, content_length=%d", client->response->data_process, client->response->content_length); - int rlen = transport_read(client->transport, res_buffer->data, client->buffer_size, client->timeout_ms); + int rlen = esp_transport_read(client->transport, res_buffer->data, client->buffer_size, client->timeout_ms); if (rlen >= 0) { http_parser_execute(client->parser, client->parser_settings, res_buffer->data, rlen); } @@ -778,7 +778,7 @@ int esp_http_client_read(esp_http_client_handle_t client, char *buffer, int len) if (byte_to_read > client->buffer_size) { byte_to_read = client->buffer_size; } - rlen = transport_read(client->transport, res_buffer->data, byte_to_read, client->timeout_ms); + rlen = esp_transport_read(client->transport, res_buffer->data, byte_to_read, client->timeout_ms); ESP_LOGD(TAG, "need_read=%d, byte_to_read=%d, rlen=%d, ridx=%d", need_read, byte_to_read, rlen, ridx); if (rlen <= 0) { @@ -893,7 +893,7 @@ int esp_http_client_fetch_headers(esp_http_client_handle_t client) client->response->status_code = -1; while (client->state < HTTP_STATE_RES_COMPLETE_HEADER) { - buffer->len = transport_read(client->transport, buffer->data, client->buffer_size, client->timeout_ms); + buffer->len = esp_transport_read(client->transport, buffer->data, client->buffer_size, client->timeout_ms); if (buffer->len < 0) { return ESP_FAIL; } @@ -924,7 +924,7 @@ static esp_err_t esp_http_client_connect(esp_http_client_handle_t client) if (client->state < HTTP_STATE_CONNECTED) { ESP_LOGD(TAG, "Begin connect to: %s://%s:%d", client->connection_info.scheme, client->connection_info.host, client->connection_info.port); - client->transport = transport_list_get_transport(client->transport_list, client->connection_info.scheme); + client->transport = esp_transport_list_get_transport(client->transport_list, client->connection_info.scheme); if (client->transport == NULL) { ESP_LOGE(TAG, "No transport found"); #ifndef CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS @@ -935,12 +935,12 @@ static esp_err_t esp_http_client_connect(esp_http_client_handle_t client) return ESP_ERR_HTTP_INVALID_TRANSPORT; } if (!client->is_async) { - if (transport_connect(client->transport, client->connection_info.host, client->connection_info.port, client->timeout_ms) < 0) { + if (esp_transport_connect(client->transport, client->connection_info.host, client->connection_info.port, client->timeout_ms) < 0) { ESP_LOGE(TAG, "Connection failed, sock < 0"); return ESP_ERR_HTTP_CONNECT; } } else { - int ret = transport_connect_async(client->transport, client->connection_info.host, client->connection_info.port, client->timeout_ms); + int ret = esp_transport_connect_async(client->transport, client->connection_info.host, client->connection_info.port, client->timeout_ms); if (ret == ASYNC_TRANS_CONNECT_FAIL) { ESP_LOGE(TAG, "Connection failed"); return ESP_ERR_HTTP_CONNECT; @@ -1036,7 +1036,7 @@ static esp_err_t esp_http_client_request_send(esp_http_client_handle_t client) client->data_write_left = wlen; client->data_written_index = 0; while (client->data_write_left > 0) { - int wret = transport_write(client->transport, client->request->buffer->data + client->data_written_index, client->data_write_left, client->timeout_ms); + int wret = esp_transport_write(client->transport, client->request->buffer->data + client->data_written_index, client->data_write_left, client->timeout_ms); if (wret <= 0) { ESP_LOGE(TAG, "Error write request"); esp_http_client_close(client); @@ -1102,9 +1102,9 @@ int esp_http_client_write(esp_http_client_handle_t client, const char *buffer, i int wlen = 0, widx = 0; while (len > 0) { - wlen = transport_write(client->transport, buffer + widx, len, client->timeout_ms); + wlen = esp_transport_write(client->transport, buffer + widx, len, client->timeout_ms); /* client->async_block is initialised in case of non-blocking IO, and in this case we return how - much ever data was written by the transport_write() API. */ + much ever data was written by the esp_transport_write() API. */ if (client->is_async || wlen <= 0) { return wlen; } @@ -1119,7 +1119,7 @@ esp_err_t esp_http_client_close(esp_http_client_handle_t client) if (client->state >= HTTP_STATE_INIT) { http_dispatch_event(client, HTTP_EVENT_DISCONNECTED, NULL, 0); client->state = HTTP_STATE_INIT; - return transport_close(client->transport); + return esp_transport_close(client->transport); } return ESP_OK; } diff --git a/components/esp_http_client/lib/include/http_utils.h b/components/esp_http_client/lib/include/http_utils.h index 1a224984e4..d57840228f 100644 --- a/components/esp_http_client/lib/include/http_utils.h +++ b/components/esp_http_client/lib/include/http_utils.h @@ -80,7 +80,7 @@ char *http_utils_join_string(const char *first_str, int len_first, const char *s int http_utils_str_starts_with(const char *str, const char *start); -#define HTTP_MEM_CHECK(TAG, a, action) TRANSPORT_MEM_CHECK(TAG, a, action) +#define HTTP_MEM_CHECK(TAG, a, action) ESP_TRANSPORT_MEM_CHECK(TAG, a, action) #endif diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index ca893e2c23..fe3ac2af1b 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit ca893e2c23a104cead0bb756a11af7b80464c2d4 +Subproject commit fe3ac2af1b708f44e710f35c4731584a3f69acd5 diff --git a/components/tcp_transport/CMakeLists.txt b/components/tcp_transport/CMakeLists.txt index 3fe93384cd..e8125a7266 100644 --- a/components/tcp_transport/CMakeLists.txt +++ b/components/tcp_transport/CMakeLists.txt @@ -9,11 +9,3 @@ set(COMPONENT_ADD_INCLUDEDIRS "include") set(COMPONENT_REQUIRES lwip esp-tls) register_component() - -if(GCC_NOT_5_2_0) - # Temporary suppress "format-overflow" warning until it is fixed - set_source_files_properties( - transport_ws.c - PROPERTIES COMPILE_FLAGS - -Wno-format-overflow) -endif() diff --git a/components/tcp_transport/include/esp_transport.h b/components/tcp_transport/include/esp_transport.h index f10a8f8d63..1f8e07809a 100644 --- a/components/tcp_transport/include/esp_transport.h +++ b/components/tcp_transport/include/esp_transport.h @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef _TRANSPORT_H_ -#define _TRANSPORT_H_ +#ifndef _ESP_TRANSPORT_H_ +#define _ESP_TRANSPORT_H_ #include @@ -22,27 +22,27 @@ extern "C" { #endif -typedef struct transport_list_t* transport_list_handle_t; -typedef struct transport_item_t* transport_handle_t; +typedef struct esp_transport_list_t* esp_transport_list_handle_t; +typedef struct esp_transport_item_t* esp_transport_handle_t; -typedef int (*connect_func)(transport_handle_t t, const char *host, int port, int timeout_ms); -typedef int (*io_func)(transport_handle_t t, const char *buffer, int len, int timeout_ms); -typedef int (*io_read_func)(transport_handle_t t, char *buffer, int len, int timeout_ms); -typedef int (*trans_func)(transport_handle_t t); -typedef int (*poll_func)(transport_handle_t t, int timeout_ms); -typedef int (*connect_async_func)(transport_handle_t t, const char *host, int port, int timeout_ms); -typedef transport_handle_t (*payload_transfer_func)(transport_handle_t); +typedef int (*connect_func)(esp_transport_handle_t t, const char *host, int port, int timeout_ms); +typedef int (*io_func)(esp_transport_handle_t t, const char *buffer, int len, int timeout_ms); +typedef int (*io_read_func)(esp_transport_handle_t t, char *buffer, int len, int timeout_ms); +typedef int (*trans_func)(esp_transport_handle_t t); +typedef int (*poll_func)(esp_transport_handle_t t, int timeout_ms); +typedef int (*connect_async_func)(esp_transport_handle_t t, const char *host, int port, int timeout_ms); +typedef esp_transport_handle_t (*payload_transfer_func)(esp_transport_handle_t); /** * @brief Create transport list * * @return A handle can hold all transports */ -transport_list_handle_t transport_list_init(); +esp_transport_list_handle_t esp_transport_list_init(); /** * @brief Cleanup and free all transports, include itself, - * this function will invoke transport_destroy of every transport have added this the list + * this function will invoke esp_transport_destroy of every transport have added this the list * * @param[in] list The list * @@ -50,7 +50,7 @@ transport_list_handle_t transport_list_init(); * - ESP_OK * - ESP_FAIL */ -esp_err_t transport_list_destroy(transport_list_handle_t list); +esp_err_t esp_transport_list_destroy(esp_transport_list_handle_t list); /** * @brief Add a transport to the list, and define a scheme to indentify this transport in the list @@ -62,11 +62,11 @@ esp_err_t transport_list_destroy(transport_list_handle_t list); * @return * - ESP_OK */ -esp_err_t transport_list_add(transport_list_handle_t list, transport_handle_t t, const char *scheme); +esp_err_t esp_transport_list_add(esp_transport_list_handle_t list, esp_transport_handle_t t, const char *scheme); /** * @brief This function will remove all transport from the list, - * invoke transport_destroy of every transport have added this the list + * invoke esp_transport_destroy of every transport have added this the list * * @param[in] list The list * @@ -74,24 +74,24 @@ esp_err_t transport_list_add(transport_list_handle_t list, transport_handle_t t, * - ESP_OK * - ESP_ERR_INVALID_ARG */ -esp_err_t transport_list_clean(transport_list_handle_t list); +esp_err_t esp_transport_list_clean(esp_transport_list_handle_t list); /** - * @brief Get the transport by scheme, which has been defined when calling function `transport_list_add` + * @brief Get the transport by scheme, which has been defined when calling function `esp_transport_list_add` * * @param[in] list The list * @param[in] tag The tag * * @return The transport handle */ -transport_handle_t transport_list_get_transport(transport_list_handle_t list, const char *scheme); +esp_transport_handle_t esp_transport_list_get_transport(esp_transport_list_handle_t list, const char *scheme); /** * @brief Initialize a transport handle object * * @return The transport handle */ -transport_handle_t transport_init(); +esp_transport_handle_t esp_transport_init(); /** * @brief Cleanup and free memory the transport @@ -102,7 +102,7 @@ transport_handle_t transport_init(); * - ESP_OK * - ESP_FAIL */ -esp_err_t transport_destroy(transport_handle_t t); +esp_err_t esp_transport_destroy(esp_transport_handle_t t); /** * @brief Get default port number used by this transport @@ -111,7 +111,7 @@ esp_err_t transport_destroy(transport_handle_t t); * * @return the port number */ -int transport_get_default_port(transport_handle_t t); +int esp_transport_get_default_port(esp_transport_handle_t t); /** * @brief Set default port number that can be used by this transport @@ -123,7 +123,7 @@ int transport_get_default_port(transport_handle_t t); * - ESP_OK * - ESP_FAIL */ -esp_err_t transport_set_default_port(transport_handle_t t, int port); +esp_err_t esp_transport_set_default_port(esp_transport_handle_t t, int port); /** * @brief Transport connection function, to make a connection to server @@ -137,7 +137,7 @@ esp_err_t transport_set_default_port(transport_handle_t t, int port); * - socket for will use by this transport * - (-1) if there are any errors, should check errno */ -int transport_connect(transport_handle_t t, const char *host, int port, int timeout_ms); +int esp_transport_connect(esp_transport_handle_t t, const char *host, int port, int timeout_ms); /** * @brief Non-blocking transport connection function, to make a connection to server @@ -151,7 +151,7 @@ int transport_connect(transport_handle_t t, const char *host, int port, int time * - socket for will use by this transport * - (-1) if there are any errors, should check errno */ -int transport_connect_async(transport_handle_t t, const char *host, int port, int timeout_ms); +int esp_transport_connect_async(esp_transport_handle_t t, const char *host, int port, int timeout_ms); /** * @brief Transport read function @@ -165,7 +165,7 @@ int transport_connect_async(transport_handle_t t, const char *host, int port, in * - Number of bytes was read * - (-1) if there are any errors, should check errno */ -int transport_read(transport_handle_t t, char *buffer, int len, int timeout_ms); +int esp_transport_read(esp_transport_handle_t t, char *buffer, int len, int timeout_ms); /** * @brief Poll the transport until readable or timeout @@ -178,7 +178,7 @@ int transport_read(transport_handle_t t, char *buffer, int len, int timeout_ms); * - (-1) If there are any errors, should check errno * - other The transport can read */ -int transport_poll_read(transport_handle_t t, int timeout_ms); +int esp_transport_poll_read(esp_transport_handle_t t, int timeout_ms); /** * @brief Transport write function @@ -192,7 +192,7 @@ int transport_poll_read(transport_handle_t t, int timeout_ms); * - Number of bytes was written * - (-1) if there are any errors, should check errno */ -int transport_write(transport_handle_t t, const char *buffer, int len, int timeout_ms); +int esp_transport_write(esp_transport_handle_t t, const char *buffer, int len, int timeout_ms); /** * @brief Poll the transport until writeable or timeout @@ -205,7 +205,7 @@ int transport_write(transport_handle_t t, const char *buffer, int len, int timeo * - (-1) If there are any errors, should check errno * - other The transport can write */ -int transport_poll_write(transport_handle_t t, int timeout_ms); +int esp_transport_poll_write(esp_transport_handle_t t, int timeout_ms); /** * @brief Transport close @@ -216,7 +216,7 @@ int transport_poll_write(transport_handle_t t, int timeout_ms); * - 0 if ok * - (-1) if there are any errors, should check errno */ -int transport_close(transport_handle_t t); +int esp_transport_close(esp_transport_handle_t t); /** * @brief Get user data context of this transport @@ -225,7 +225,7 @@ int transport_close(transport_handle_t t); * * @return The user data context */ -void *transport_get_context_data(transport_handle_t t); +void *esp_transport_get_context_data(esp_transport_handle_t t); /** * @brief Get transport handle of underlying protocol @@ -236,7 +236,7 @@ void *transport_get_context_data(transport_handle_t t); * * @return Payload transport handle */ -transport_handle_t transport_get_payload_transport_handle(transport_handle_t t); +esp_transport_handle_t esp_transport_get_payload_transport_handle(esp_transport_handle_t t); /** * @brief Set the user context data for this transport @@ -247,7 +247,7 @@ transport_handle_t transport_get_payload_transport_handle(transport_handle_t t); * @return * - ESP_OK */ -esp_err_t transport_set_context_data(transport_handle_t t, void *data); +esp_err_t esp_transport_set_context_data(esp_transport_handle_t t, void *data); /** * @brief Set transport functions for the transport handle @@ -265,7 +265,7 @@ esp_err_t transport_set_context_data(transport_handle_t t, void *data); * @return * - ESP_OK */ -esp_err_t transport_set_func(transport_handle_t t, +esp_err_t esp_transport_set_func(esp_transport_handle_t t, connect_func _connect, io_read_func _read, io_func _write, @@ -286,9 +286,9 @@ esp_err_t transport_set_func(transport_handle_t t, * - ESP_OK * - ESP_FAIL */ -esp_err_t transport_set_async_connect_func(transport_handle_t t, connect_async_func _connect_async_func); +esp_err_t esp_transport_set_async_connect_func(esp_transport_handle_t t, connect_async_func _connect_async_func); #ifdef __cplusplus } #endif -#endif +#endif /* _ESP_TRANSPORT_ */ diff --git a/components/tcp_transport/include/esp_transport_ssl.h b/components/tcp_transport/include/esp_transport_ssl.h index b8353ea8ff..2065db6b0e 100644 --- a/components/tcp_transport/include/esp_transport_ssl.h +++ b/components/tcp_transport/include/esp_transport_ssl.h @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef _TRANSPORT_SSL_H_ -#define _TRANSPORT_SSL_H_ +#ifndef _ESP_TRANSPORT_SSL_H_ +#define _ESP_TRANSPORT_SSL_H_ #include "esp_transport.h" @@ -23,11 +23,11 @@ extern "C" { /** - * @brief Create new SSL transport, the transport handle must be release transport_destroy callback + * @brief Create new SSL transport, the transport handle must be release esp_transport_destroy callback * - * @return the allocated transport_handle_t, or NULL if the handle can not be allocated + * @return the allocated esp_transport_handle_t, or NULL if the handle can not be allocated */ -transport_handle_t transport_ssl_init(); +esp_transport_handle_t esp_transport_ssl_init(); /** * @brief Set SSL certificate data (as PEM format). @@ -38,11 +38,11 @@ transport_handle_t transport_ssl_init(); * @param[in] data The pem data * @param[in] len The length */ -void transport_ssl_set_cert_data(transport_handle_t t, const char *data, int len); +void esp_transport_ssl_set_cert_data(esp_transport_handle_t t, const char *data, int len); #ifdef __cplusplus } #endif -#endif +#endif /* _ESP_TRANSPORT_SSL_H_ */ diff --git a/components/tcp_transport/include/esp_transport_tcp.h b/components/tcp_transport/include/esp_transport_tcp.h index 182ea95634..57ad453309 100644 --- a/components/tcp_transport/include/esp_transport_tcp.h +++ b/components/tcp_transport/include/esp_transport_tcp.h @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef _TRANSPORT_TCP_H_ -#define _TRANSPORT_TCP_H_ +#ifndef _ESP_TRANSPORT_TCP_H_ +#define _ESP_TRANSPORT_TCP_H_ #include "esp_transport.h" @@ -22,15 +22,15 @@ extern "C" { #endif /** - * @brief Create TCP transport, the transport handle must be release transport_destroy callback + * @brief Create TCP transport, the transport handle must be release esp_transport_destroy callback * - * @return the allocated transport_handle_t, or NULL if the handle can not be allocated + * @return the allocated esp_transport_handle_t, or NULL if the handle can not be allocated */ -transport_handle_t transport_tcp_init(); +esp_transport_handle_t esp_transport_tcp_init(); #ifdef __cplusplus } #endif -#endif +#endif /* _ESP_TRANSPORT_TCP_H_ */ diff --git a/components/tcp_transport/include/esp_transport_utils.h b/components/tcp_transport/include/esp_transport_utils.h index 0e72812ea2..405b4f6b46 100644 --- a/components/tcp_transport/include/esp_transport_utils.h +++ b/components/tcp_transport/include/esp_transport_utils.h @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef _TRANSPORT_UTILS_H_ -#define _TRANSPORT_UTILS_H_ +#ifndef _ESP_TRANSPORT_UTILS_H_ +#define _ESP_TRANSPORT_UTILS_H_ #include #ifdef __cplusplus @@ -26,10 +26,10 @@ extern "C" { * @param[in] timeout_ms The timeout milliseconds * @param[out] tv Timeval struct */ -void transport_utils_ms_to_timeval(int timeout_ms, struct timeval *tv); +void esp_transport_utils_ms_to_timeval(int timeout_ms, struct timeval *tv); -#define TRANSPORT_MEM_CHECK(TAG, a, action) if (!(a)) { \ +#define ESP_TRANSPORT_MEM_CHECK(TAG, a, action) if (!(a)) { \ ESP_LOGE(TAG,"%s:%d (%s): %s", __FILE__, __LINE__, __FUNCTION__, "Memory exhausted"); \ action; \ } @@ -37,4 +37,4 @@ void transport_utils_ms_to_timeval(int timeout_ms, struct timeval *tv); #ifdef __cplusplus } #endif -#endif /* _TRANSPORT_UTILS_H_ */ \ No newline at end of file +#endif /* _ESP_TRANSPORT_UTILS_H_ */ \ No newline at end of file diff --git a/components/tcp_transport/include/esp_transport_ws.h b/components/tcp_transport/include/esp_transport_ws.h index 76653b0d2e..582c5c7da2 100644 --- a/components/tcp_transport/include/esp_transport_ws.h +++ b/components/tcp_transport/include/esp_transport_ws.h @@ -4,8 +4,8 @@ * Tuan PM */ -#ifndef _TRANSPORT_WS_H_ -#define _TRANSPORT_WS_H_ +#ifndef _ESP_TRANSPORT_WS_H_ +#define _ESP_TRANSPORT_WS_H_ #include "esp_transport.h" @@ -13,29 +13,17 @@ extern "C" { #endif -#define WS_FIN 0x80 -#define WS_OPCODE_TEXT 0x01 -#define WS_OPCODE_BINARY 0x02 -#define WS_OPCODE_CLOSE 0x08 -#define WS_OPCODE_PING 0x09 -#define WS_OPCODE_PONG 0x0a -// Second byte -#define WS_MASK 0x80 -#define WS_SIZE16 126 -#define WS_SIZE64 127 -#define MAX_WEBSOCKET_HEADER_SIZE 10 -#define WS_RESPONSE_OK 101 /** - * @brief Create TCP transport + * @brief Create web socket transport * * @return * - transport * - NULL */ -transport_handle_t transport_ws_init(transport_handle_t parent_handle); +esp_transport_handle_t esp_transport_ws_init(esp_transport_handle_t parent_handle); -void transport_ws_set_path(transport_handle_t t, const char *path); +void esp_transport_ws_set_path(esp_transport_handle_t t, const char *path); @@ -43,4 +31,4 @@ void transport_ws_set_path(transport_handle_t t, const char *path); } #endif -#endif +#endif /* _ESP_TRANSPORT_WS_H_ */ diff --git a/components/tcp_transport/transport.c b/components/tcp_transport/transport.c index 6597b53d4e..c6f9d0ca80 100644 --- a/components/tcp_transport/transport.c +++ b/components/tcp_transport/transport.c @@ -27,7 +27,7 @@ static const char *TAG = "TRANSPORT"; /** * Transport layer structure, which will provide functions, basic properties for transport types */ -struct transport_item_t { +struct esp_transport_item_t { int port; int socket; /*!< Socket to use in this transport */ char *scheme; /*!< Tag name */ @@ -43,37 +43,37 @@ struct transport_item_t { connect_async_func _connect_async; /*!< non-blocking connect function of this transport */ payload_transfer_func _parrent_transfer; /*!< Function returning underlying transport layer */ - STAILQ_ENTRY(transport_item_t) next; + STAILQ_ENTRY(esp_transport_item_t) next; }; /** * This list will hold all transport available */ -STAILQ_HEAD(transport_list_t, transport_item_t); +STAILQ_HEAD(esp_transport_list_t, esp_transport_item_t); -transport_list_handle_t transport_list_init() +esp_transport_list_handle_t esp_transport_list_init() { - transport_list_handle_t list = calloc(1, sizeof(struct transport_list_t)); - TRANSPORT_MEM_CHECK(TAG, list, return NULL); + esp_transport_list_handle_t list = calloc(1, sizeof(struct esp_transport_list_t)); + ESP_TRANSPORT_MEM_CHECK(TAG, list, return NULL); STAILQ_INIT(list); return list; } -esp_err_t transport_list_add(transport_list_handle_t list, transport_handle_t t, const char *scheme) +esp_err_t esp_transport_list_add(esp_transport_list_handle_t list, esp_transport_handle_t t, const char *scheme) { if (list == NULL || t == NULL) { return ESP_ERR_INVALID_ARG; } t->scheme = calloc(1, strlen(scheme) + 1); - TRANSPORT_MEM_CHECK(TAG, t->scheme, return ESP_ERR_NO_MEM); + ESP_TRANSPORT_MEM_CHECK(TAG, t->scheme, return ESP_ERR_NO_MEM); strcpy(t->scheme, scheme); STAILQ_INSERT_TAIL(list, t, next); return ESP_OK; } -transport_handle_t transport_list_get_transport(transport_list_handle_t list, const char *scheme) +esp_transport_handle_t esp_transport_list_get_transport(esp_transport_list_handle_t list, const char *scheme) { if (!list) { return NULL; @@ -81,7 +81,7 @@ transport_handle_t transport_list_get_transport(transport_list_handle_t list, co if (scheme == NULL) { return STAILQ_FIRST(list); } - transport_handle_t item; + esp_transport_handle_t item; STAILQ_FOREACH(item, list, next) { if (strcasecmp(item->scheme, scheme) == 0) { return item; @@ -90,37 +90,37 @@ transport_handle_t transport_list_get_transport(transport_list_handle_t list, co return NULL; } -esp_err_t transport_list_destroy(transport_list_handle_t list) +esp_err_t esp_transport_list_destroy(esp_transport_list_handle_t list) { - transport_list_clean(list); + esp_transport_list_clean(list); free(list); return ESP_OK; } -esp_err_t transport_list_clean(transport_list_handle_t list) +esp_err_t esp_transport_list_clean(esp_transport_list_handle_t list) { - transport_handle_t item = STAILQ_FIRST(list); - transport_handle_t tmp; + esp_transport_handle_t item = STAILQ_FIRST(list); + esp_transport_handle_t tmp; while (item != NULL) { tmp = STAILQ_NEXT(item, next); if (item->_destroy) { item->_destroy(item); } - transport_destroy(item); + esp_transport_destroy(item); item = tmp; } STAILQ_INIT(list); return ESP_OK; } -transport_handle_t transport_init() +esp_transport_handle_t esp_transport_init() { - transport_handle_t t = calloc(1, sizeof(struct transport_item_t)); - TRANSPORT_MEM_CHECK(TAG, t, return NULL); + esp_transport_handle_t t = calloc(1, sizeof(struct esp_transport_item_t)); + ESP_TRANSPORT_MEM_CHECK(TAG, t, return NULL); return t; } -transport_handle_t transport_get_payload_transport_handle(transport_handle_t t) +esp_transport_handle_t esp_transport_get_payload_transport_handle(esp_transport_handle_t t) { if (t && t->_read) { return t->_parrent_transfer(t); @@ -128,7 +128,7 @@ transport_handle_t transport_get_payload_transport_handle(transport_handle_t t) return NULL; } -esp_err_t transport_destroy(transport_handle_t t) +esp_err_t esp_transport_destroy(esp_transport_handle_t t) { if (t->scheme) { free(t->scheme); @@ -137,7 +137,7 @@ esp_err_t transport_destroy(transport_handle_t t) return ESP_OK; } -int transport_connect(transport_handle_t t, const char *host, int port, int timeout_ms) +int esp_transport_connect(esp_transport_handle_t t, const char *host, int port, int timeout_ms) { int ret = -1; if (t && t->_connect) { @@ -146,7 +146,7 @@ int transport_connect(transport_handle_t t, const char *host, int port, int time return ret; } -int transport_connect_async(transport_handle_t t, const char *host, int port, int timeout_ms) +int esp_transport_connect_async(esp_transport_handle_t t, const char *host, int port, int timeout_ms) { int ret = -1; if (t && t->_connect) { @@ -155,7 +155,7 @@ int transport_connect_async(transport_handle_t t, const char *host, int port, in return ret; } -int transport_read(transport_handle_t t, char *buffer, int len, int timeout_ms) +int esp_transport_read(esp_transport_handle_t t, char *buffer, int len, int timeout_ms) { if (t && t->_read) { return t->_read(t, buffer, len, timeout_ms); @@ -163,7 +163,7 @@ int transport_read(transport_handle_t t, char *buffer, int len, int timeout_ms) return -1; } -int transport_write(transport_handle_t t, const char *buffer, int len, int timeout_ms) +int esp_transport_write(esp_transport_handle_t t, const char *buffer, int len, int timeout_ms) { if (t && t->_write) { return t->_write(t, buffer, len, timeout_ms); @@ -171,7 +171,7 @@ int transport_write(transport_handle_t t, const char *buffer, int len, int timeo return -1; } -int transport_poll_read(transport_handle_t t, int timeout_ms) +int esp_transport_poll_read(esp_transport_handle_t t, int timeout_ms) { if (t && t->_poll_read) { return t->_poll_read(t, timeout_ms); @@ -179,7 +179,7 @@ int transport_poll_read(transport_handle_t t, int timeout_ms) return -1; } -int transport_poll_write(transport_handle_t t, int timeout_ms) +int esp_transport_poll_write(esp_transport_handle_t t, int timeout_ms) { if (t && t->_poll_write) { return t->_poll_write(t, timeout_ms); @@ -187,7 +187,7 @@ int transport_poll_write(transport_handle_t t, int timeout_ms) return -1; } -int transport_close(transport_handle_t t) +int esp_transport_close(esp_transport_handle_t t) { if (t && t->_close) { return t->_close(t); @@ -195,7 +195,7 @@ int transport_close(transport_handle_t t) return 0; } -void *transport_get_context_data(transport_handle_t t) +void *esp_transport_get_context_data(esp_transport_handle_t t) { if (t) { return t->data; @@ -203,7 +203,7 @@ void *transport_get_context_data(transport_handle_t t) return NULL; } -esp_err_t transport_set_context_data(transport_handle_t t, void *data) +esp_err_t esp_transport_set_context_data(esp_transport_handle_t t, void *data) { if (t) { t->data = data; @@ -212,7 +212,7 @@ esp_err_t transport_set_context_data(transport_handle_t t, void *data) return ESP_FAIL; } -esp_err_t transport_set_func(transport_handle_t t, +esp_err_t esp_transport_set_func(esp_transport_handle_t t, connect_func _connect, io_read_func _read, io_func _write, @@ -237,7 +237,7 @@ esp_err_t transport_set_func(transport_handle_t t, return ESP_OK; } -int transport_get_default_port(transport_handle_t t) +int esp_transport_get_default_port(esp_transport_handle_t t) { if (t == NULL) { return -1; @@ -245,7 +245,7 @@ int transport_get_default_port(transport_handle_t t) return t->port; } -esp_err_t transport_set_default_port(transport_handle_t t, int port) +esp_err_t esp_transport_set_default_port(esp_transport_handle_t t, int port) { if (t == NULL) { return ESP_FAIL; @@ -254,12 +254,12 @@ esp_err_t transport_set_default_port(transport_handle_t t, int port) return ESP_OK; } -transport_handle_t transport_get_handle(transport_handle_t t) +esp_transport_handle_t transport_get_handle(esp_transport_handle_t t) { return t; } -esp_err_t transport_set_async_connect_func(transport_handle_t t, connect_async_func _connect_async_func) +esp_err_t esp_transport_set_async_connect_func(esp_transport_handle_t t, connect_async_func _connect_async_func) { if (t == NULL) { return ESP_FAIL; diff --git a/components/tcp_transport/transport_ssl.c b/components/tcp_transport/transport_ssl.c index cd242ca5bf..9034fd4e70 100644 --- a/components/tcp_transport/transport_ssl.c +++ b/components/tcp_transport/transport_ssl.c @@ -43,13 +43,13 @@ typedef struct { transport_ssl_conn_state_t conn_state; } transport_ssl_t; -transport_handle_t transport_get_handle(transport_handle_t t); +esp_transport_handle_t transport_get_handle(esp_transport_handle_t t); -static int ssl_close(transport_handle_t t); +static int ssl_close(esp_transport_handle_t t); -static int ssl_connect_async(transport_handle_t t, const char *host, int port, int timeout_ms) +static int ssl_connect_async(esp_transport_handle_t t, const char *host, int port, int timeout_ms) { - transport_ssl_t *ssl = transport_get_context_data(t); + transport_ssl_t *ssl = esp_transport_get_context_data(t); if (ssl->conn_state == TRANS_SSL_INIT) { if (ssl->cfg.cacert_pem_buf) { ssl->verify_server = true; @@ -69,9 +69,9 @@ static int ssl_connect_async(transport_handle_t t, const char *host, int port, i return 0; } -static int ssl_connect(transport_handle_t t, const char *host, int port, int timeout_ms) +static int ssl_connect(esp_transport_handle_t t, const char *host, int port, int timeout_ms) { - transport_ssl_t *ssl = transport_get_context_data(t); + transport_ssl_t *ssl = esp_transport_get_context_data(t); if (ssl->cfg.cacert_pem_buf) { ssl->verify_server = true; } @@ -85,35 +85,35 @@ static int ssl_connect(transport_handle_t t, const char *host, int port, int tim return 0; } -static int ssl_poll_read(transport_handle_t t, int timeout_ms) +static int ssl_poll_read(esp_transport_handle_t t, int timeout_ms) { - transport_ssl_t *ssl = transport_get_context_data(t); + transport_ssl_t *ssl = esp_transport_get_context_data(t); fd_set readset; FD_ZERO(&readset); FD_SET(ssl->tls->sockfd, &readset); struct timeval timeout; - transport_utils_ms_to_timeval(timeout_ms, &timeout); + esp_transport_utils_ms_to_timeval(timeout_ms, &timeout); return select(ssl->tls->sockfd + 1, &readset, NULL, NULL, &timeout); } -static int ssl_poll_write(transport_handle_t t, int timeout_ms) +static int ssl_poll_write(esp_transport_handle_t t, int timeout_ms) { - transport_ssl_t *ssl = transport_get_context_data(t); + transport_ssl_t *ssl = esp_transport_get_context_data(t); fd_set writeset; FD_ZERO(&writeset); FD_SET(ssl->tls->sockfd, &writeset); struct timeval timeout; - transport_utils_ms_to_timeval(timeout_ms, &timeout); + esp_transport_utils_ms_to_timeval(timeout_ms, &timeout); return select(ssl->tls->sockfd + 1, NULL, &writeset, NULL, &timeout); } -static int ssl_write(transport_handle_t t, const char *buffer, int len, int timeout_ms) +static int ssl_write(esp_transport_handle_t t, const char *buffer, int len, int timeout_ms) { int poll, ret; - transport_ssl_t *ssl = transport_get_context_data(t); + transport_ssl_t *ssl = esp_transport_get_context_data(t); - if ((poll = transport_poll_write(t, timeout_ms)) <= 0) { + if ((poll = esp_transport_poll_write(t, timeout_ms)) <= 0) { ESP_LOGW(TAG, "Poll timeout or error, errno=%s, fd=%d, timeout_ms=%d", strerror(errno), ssl->tls->sockfd, timeout_ms); return poll; } @@ -124,13 +124,13 @@ static int ssl_write(transport_handle_t t, const char *buffer, int len, int time return ret; } -static int ssl_read(transport_handle_t t, char *buffer, int len, int timeout_ms) +static int ssl_read(esp_transport_handle_t t, char *buffer, int len, int timeout_ms) { int poll, ret; - transport_ssl_t *ssl = transport_get_context_data(t); + transport_ssl_t *ssl = esp_transport_get_context_data(t); if (esp_tls_get_bytes_avail(ssl->tls) <= 0) { - if ((poll = transport_poll_read(t, timeout_ms)) <= 0) { + if ((poll = esp_transport_poll_read(t, timeout_ms)) <= 0) { return poll; } } @@ -141,10 +141,10 @@ static int ssl_read(transport_handle_t t, char *buffer, int len, int timeout_ms) return ret; } -static int ssl_close(transport_handle_t t) +static int ssl_close(esp_transport_handle_t t) { int ret = -1; - transport_ssl_t *ssl = transport_get_context_data(t); + transport_ssl_t *ssl = esp_transport_get_context_data(t); if (ssl->ssl_initialized) { esp_tls_conn_delete(ssl->tls); ssl->ssl_initialized = false; @@ -153,31 +153,31 @@ static int ssl_close(transport_handle_t t) return ret; } -static int ssl_destroy(transport_handle_t t) +static int ssl_destroy(esp_transport_handle_t t) { - transport_ssl_t *ssl = transport_get_context_data(t); - transport_close(t); + transport_ssl_t *ssl = esp_transport_get_context_data(t); + esp_transport_close(t); free(ssl); return 0; } -void transport_ssl_set_cert_data(transport_handle_t t, const char *data, int len) +void esp_transport_ssl_set_cert_data(esp_transport_handle_t t, const char *data, int len) { - transport_ssl_t *ssl = transport_get_context_data(t); + transport_ssl_t *ssl = esp_transport_get_context_data(t); if (t && ssl) { ssl->cfg.cacert_pem_buf = (void *)data; ssl->cfg.cacert_pem_bytes = len + 1; } } -transport_handle_t transport_ssl_init() +esp_transport_handle_t esp_transport_ssl_init() { - transport_handle_t t = transport_init(); + esp_transport_handle_t t = esp_transport_init(); transport_ssl_t *ssl = calloc(1, sizeof(transport_ssl_t)); - TRANSPORT_MEM_CHECK(TAG, ssl, return NULL); - transport_set_context_data(t, ssl); - transport_set_func(t, ssl_connect, ssl_read, ssl_write, ssl_close, ssl_poll_read, ssl_poll_write, ssl_destroy, transport_get_handle); - transport_set_async_connect_func(t, ssl_connect_async); + ESP_TRANSPORT_MEM_CHECK(TAG, ssl, return NULL); + esp_transport_set_context_data(t, ssl); + esp_transport_set_func(t, ssl_connect, ssl_read, ssl_write, ssl_close, ssl_poll_read, ssl_poll_write, ssl_destroy, transport_get_handle); + esp_transport_set_async_connect_func(t, ssl_connect_async); return t; } diff --git a/components/tcp_transport/transport_tcp.c b/components/tcp_transport/transport_tcp.c index 0c75627eba..3939e98214 100644 --- a/components/tcp_transport/transport_tcp.c +++ b/components/tcp_transport/transport_tcp.c @@ -32,7 +32,7 @@ typedef struct { int sock; } transport_tcp_t; -transport_handle_t transport_get_handle(transport_handle_t t); +esp_transport_handle_t transport_get_handle(esp_transport_handle_t t); static int resolve_dns(const char *host, struct sockaddr_in *ip) { @@ -51,11 +51,11 @@ static int resolve_dns(const char *host, struct sockaddr_in *ip) { return ESP_OK; } -static int tcp_connect(transport_handle_t t, const char *host, int port, int timeout_ms) +static int tcp_connect(esp_transport_handle_t t, const char *host, int port, int timeout_ms) { struct sockaddr_in remote_ip; struct timeval tv; - transport_tcp_t *tcp = transport_get_context_data(t); + transport_tcp_t *tcp = esp_transport_get_context_data(t); bzero(&remote_ip, sizeof(struct sockaddr_in)); @@ -76,7 +76,7 @@ static int tcp_connect(transport_handle_t t, const char *host, int port, int tim remote_ip.sin_family = AF_INET; remote_ip.sin_port = htons(port); - transport_utils_ms_to_timeval(timeout_ms, &tv); + esp_transport_utils_ms_to_timeval(timeout_ms, &tv); setsockopt(tcp->sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); @@ -90,21 +90,21 @@ static int tcp_connect(transport_handle_t t, const char *host, int port, int tim return tcp->sock; } -static int tcp_write(transport_handle_t t, const char *buffer, int len, int timeout_ms) +static int tcp_write(esp_transport_handle_t t, const char *buffer, int len, int timeout_ms) { int poll; - transport_tcp_t *tcp = transport_get_context_data(t); - if ((poll = transport_poll_write(t, timeout_ms)) <= 0) { + transport_tcp_t *tcp = esp_transport_get_context_data(t); + if ((poll = esp_transport_poll_write(t, timeout_ms)) <= 0) { return poll; } return write(tcp->sock, buffer, len); } -static int tcp_read(transport_handle_t t, char *buffer, int len, int timeout_ms) +static int tcp_read(esp_transport_handle_t t, char *buffer, int len, int timeout_ms) { - transport_tcp_t *tcp = transport_get_context_data(t); + transport_tcp_t *tcp = esp_transport_get_context_data(t); int poll = -1; - if ((poll = transport_poll_read(t, timeout_ms)) <= 0) { + if ((poll = esp_transport_poll_read(t, timeout_ms)) <= 0) { return poll; } int read_len = read(tcp->sock, buffer, len); @@ -114,31 +114,31 @@ static int tcp_read(transport_handle_t t, char *buffer, int len, int timeout_ms) return read_len; } -static int tcp_poll_read(transport_handle_t t, int timeout_ms) +static int tcp_poll_read(esp_transport_handle_t t, int timeout_ms) { - transport_tcp_t *tcp = transport_get_context_data(t); + transport_tcp_t *tcp = esp_transport_get_context_data(t); fd_set readset; FD_ZERO(&readset); FD_SET(tcp->sock, &readset); struct timeval timeout; - transport_utils_ms_to_timeval(timeout_ms, &timeout); + esp_transport_utils_ms_to_timeval(timeout_ms, &timeout); return select(tcp->sock + 1, &readset, NULL, NULL, &timeout); } -static int tcp_poll_write(transport_handle_t t, int timeout_ms) +static int tcp_poll_write(esp_transport_handle_t t, int timeout_ms) { - transport_tcp_t *tcp = transport_get_context_data(t); + transport_tcp_t *tcp = esp_transport_get_context_data(t); fd_set writeset; FD_ZERO(&writeset); FD_SET(tcp->sock, &writeset); struct timeval timeout; - transport_utils_ms_to_timeval(timeout_ms, &timeout); + esp_transport_utils_ms_to_timeval(timeout_ms, &timeout); return select(tcp->sock + 1, NULL, &writeset, NULL, &timeout); } -static int tcp_close(transport_handle_t t) +static int tcp_close(esp_transport_handle_t t) { - transport_tcp_t *tcp = transport_get_context_data(t); + transport_tcp_t *tcp = esp_transport_get_context_data(t); int ret = -1; if (tcp->sock >= 0) { ret = close(tcp->sock); @@ -147,22 +147,22 @@ static int tcp_close(transport_handle_t t) return ret; } -static esp_err_t tcp_destroy(transport_handle_t t) +static esp_err_t tcp_destroy(esp_transport_handle_t t) { - transport_tcp_t *tcp = transport_get_context_data(t); - transport_close(t); + transport_tcp_t *tcp = esp_transport_get_context_data(t); + esp_transport_close(t); free(tcp); return 0; } -transport_handle_t transport_tcp_init() +esp_transport_handle_t esp_transport_tcp_init() { - transport_handle_t t = transport_init(); + esp_transport_handle_t t = esp_transport_init(); transport_tcp_t *tcp = calloc(1, sizeof(transport_tcp_t)); - TRANSPORT_MEM_CHECK(TAG, tcp, return NULL); + ESP_TRANSPORT_MEM_CHECK(TAG, tcp, return NULL); tcp->sock = -1; - transport_set_func(t, tcp_connect, tcp_read, tcp_write, tcp_close, tcp_poll_read, tcp_poll_write, tcp_destroy, transport_get_handle); - transport_set_context_data(t, tcp); + esp_transport_set_func(t, tcp_connect, tcp_read, tcp_write, tcp_close, tcp_poll_read, tcp_poll_write, tcp_destroy, transport_get_handle); + esp_transport_set_context_data(t, tcp); return t; } diff --git a/components/tcp_transport/transport_utils.c b/components/tcp_transport/transport_utils.c index 36699a9242..9ef56dc244 100644 --- a/components/tcp_transport/transport_utils.c +++ b/components/tcp_transport/transport_utils.c @@ -6,7 +6,7 @@ #include "esp_transport_utils.h" -void transport_utils_ms_to_timeval(int timeout_ms, struct timeval *tv) +void esp_transport_utils_ms_to_timeval(int timeout_ms, struct timeval *tv) { tv->tv_sec = timeout_ms / 1000; tv->tv_usec = (timeout_ms - (tv->tv_sec * 1000)) * 1000; diff --git a/components/tcp_transport/transport_ws.c b/components/tcp_transport/transport_ws.c index bac4226828..28de6c5bee 100644 --- a/components/tcp_transport/transport_ws.c +++ b/components/tcp_transport/transport_ws.c @@ -14,16 +14,28 @@ static const char *TAG = "TRANSPORT_WS"; #define DEFAULT_WS_BUFFER (1024) +#define WS_FIN 0x80 +#define WS_OPCODE_TEXT 0x01 +#define WS_OPCODE_BINARY 0x02 +#define WS_OPCODE_CLOSE 0x08 +#define WS_OPCODE_PING 0x09 +#define WS_OPCODE_PONG 0x0a +// Second byte +#define WS_MASK 0x80 +#define WS_SIZE16 126 +#define WS_SIZE64 127 +#define MAX_WEBSOCKET_HEADER_SIZE 10 +#define WS_RESPONSE_OK 101 typedef struct { char *path; char *buffer; - transport_handle_t parent; + esp_transport_handle_t parent; } transport_ws_t; -transport_handle_t ws_transport_get_payload_transport_handle(transport_handle_t t) +static esp_transport_handle_t ws_get_payload_transport_handle(esp_transport_handle_t t) { - transport_ws_t *ws = transport_get_context_data(t); + transport_ws_t *ws = esp_transport_get_context_data(t); return ws->parent; } @@ -64,10 +76,10 @@ static char *get_http_header(const char *buffer, const char *key) return NULL; } -static int ws_connect(transport_handle_t t, const char *host, int port, int timeout_ms) +static int ws_connect(esp_transport_handle_t t, const char *host, int port, int timeout_ms) { - transport_ws_t *ws = transport_get_context_data(t); - if (transport_connect(ws->parent, host, port, timeout_ms) < 0) { + transport_ws_t *ws = esp_transport_get_context_data(t); + if (esp_transport_connect(ws->parent, host, port, timeout_ms) < 0) { ESP_LOGE(TAG, "Error connect to ther server"); } @@ -96,11 +108,11 @@ static int ws_connect(transport_handle_t t, const char *host, int port, int time return -1; } ESP_LOGD(TAG, "Write upgrate request\r\n%s", ws->buffer); - if (transport_write(ws->parent, ws->buffer, len, timeout_ms) <= 0) { + if (esp_transport_write(ws->parent, ws->buffer, len, timeout_ms) <= 0) { ESP_LOGE(TAG, "Error write Upgrade header %s", ws->buffer); return -1; } - if ((len = transport_read(ws->parent, ws->buffer, DEFAULT_WS_BUFFER, timeout_ms)) <= 0) { + if ((len = esp_transport_read(ws->parent, ws->buffer, DEFAULT_WS_BUFFER, timeout_ms)) <= 0) { ESP_LOGE(TAG, "Error read response for Upgrade header %s", ws->buffer); return -1; } @@ -132,15 +144,15 @@ static int ws_connect(transport_handle_t t, const char *host, int port, int time return 0; } -static int ws_write(transport_handle_t t, const char *buff, int len, int timeout_ms) +static int ws_write(esp_transport_handle_t t, const char *buff, int len, int timeout_ms) { - transport_ws_t *ws = transport_get_context_data(t); + transport_ws_t *ws = esp_transport_get_context_data(t); char ws_header[MAX_WEBSOCKET_HEADER_SIZE]; char *mask; int header_len = 0, i; char *buffer = (char *)buff; int poll_write; - if ((poll_write = transport_poll_write(ws->parent, timeout_ms)) <= 0) { + if ((poll_write = esp_transport_poll_write(ws->parent, timeout_ms)) <= 0) { return poll_write; } @@ -161,25 +173,25 @@ static int ws_write(transport_handle_t t, const char *buff, int len, int timeout for (i = 0; i < len; ++i) { buffer[i] = (buffer[i] ^ mask[i % 4]); } - if (transport_write(ws->parent, ws_header, header_len, timeout_ms) != header_len) { + if (esp_transport_write(ws->parent, ws_header, header_len, timeout_ms) != header_len) { ESP_LOGE(TAG, "Error write header"); return -1; } - return transport_write(ws->parent, buffer, len, timeout_ms); + return esp_transport_write(ws->parent, buffer, len, timeout_ms); } -static int ws_read(transport_handle_t t, char *buffer, int len, int timeout_ms) +static int ws_read(esp_transport_handle_t t, char *buffer, int len, int timeout_ms) { - transport_ws_t *ws = transport_get_context_data(t); + transport_ws_t *ws = esp_transport_get_context_data(t); int payload_len; int payload_len_buff = len; char *data_ptr = buffer, opcode, mask, *mask_key = NULL; int rlen; int poll_read; - if ((poll_read = transport_poll_read(ws->parent, timeout_ms)) <= 0) { + if ((poll_read = esp_transport_poll_read(ws->parent, timeout_ms)) <= 0) { return poll_read; } - if ((rlen = transport_read(ws->parent, buffer, len, timeout_ms)) <= 0) { + if ((rlen = esp_transport_read(ws->parent, buffer, len, timeout_ms)) <= 0) { ESP_LOGE(TAG, "Error read data"); return rlen; } @@ -223,56 +235,56 @@ static int ws_read(transport_handle_t t, char *buffer, int len, int timeout_ms) return payload_len; } -static int ws_poll_read(transport_handle_t t, int timeout_ms) +static int ws_poll_read(esp_transport_handle_t t, int timeout_ms) { - transport_ws_t *ws = transport_get_context_data(t); - return transport_poll_read(ws->parent, timeout_ms); + transport_ws_t *ws = esp_transport_get_context_data(t); + return esp_transport_poll_read(ws->parent, timeout_ms); } -static int ws_poll_write(transport_handle_t t, int timeout_ms) +static int ws_poll_write(esp_transport_handle_t t, int timeout_ms) { - transport_ws_t *ws = transport_get_context_data(t); - return transport_poll_write(ws->parent, timeout_ms);; + transport_ws_t *ws = esp_transport_get_context_data(t); + return esp_transport_poll_write(ws->parent, timeout_ms);; } -static int ws_close(transport_handle_t t) +static int ws_close(esp_transport_handle_t t) { - transport_ws_t *ws = transport_get_context_data(t); - return transport_close(ws->parent); + transport_ws_t *ws = esp_transport_get_context_data(t); + return esp_transport_close(ws->parent); } -static esp_err_t ws_destroy(transport_handle_t t) +static esp_err_t ws_destroy(esp_transport_handle_t t) { - transport_ws_t *ws = transport_get_context_data(t); + transport_ws_t *ws = esp_transport_get_context_data(t); free(ws->buffer); free(ws->path); free(ws); return 0; } -void transport_ws_set_path(transport_handle_t t, const char *path) +void esp_transport_ws_set_path(esp_transport_handle_t t, const char *path) { - transport_ws_t *ws = transport_get_context_data(t); + transport_ws_t *ws = esp_transport_get_context_data(t); ws->path = realloc(ws->path, strlen(path) + 1); strcpy(ws->path, path); } -transport_handle_t transport_ws_init(transport_handle_t parent_handle) +esp_transport_handle_t esp_transport_ws_init(esp_transport_handle_t parent_handle) { - transport_handle_t t = transport_init(); + esp_transport_handle_t t = esp_transport_init(); transport_ws_t *ws = calloc(1, sizeof(transport_ws_t)); - TRANSPORT_MEM_CHECK(TAG, ws, return NULL); + ESP_TRANSPORT_MEM_CHECK(TAG, ws, return NULL); ws->parent = parent_handle; ws->path = strdup("/"); - TRANSPORT_MEM_CHECK(TAG, ws->path, return NULL); + ESP_TRANSPORT_MEM_CHECK(TAG, ws->path, return NULL); ws->buffer = malloc(DEFAULT_WS_BUFFER); - TRANSPORT_MEM_CHECK(TAG, ws->buffer, { + ESP_TRANSPORT_MEM_CHECK(TAG, ws->buffer, { free(ws->path); free(ws); return NULL; }); - transport_set_func(t, ws_connect, ws_read, ws_write, ws_close, ws_poll_read, ws_poll_write, ws_destroy, ws_transport_get_payload_transport_handle); - transport_set_context_data(t, ws); + esp_transport_set_func(t, ws_connect, ws_read, ws_write, ws_close, ws_poll_read, ws_poll_write, ws_destroy, ws_get_payload_transport_handle); + esp_transport_set_context_data(t, ws); return t; }