feat(tcp_transport): update TCP transport from esp-idf

Commit ID: 03d07741
This commit is contained in:
Dong Heng
2019-11-18 17:55:17 +08:00
parent 595b51b15f
commit fd8ed3b340
15 changed files with 569 additions and 111 deletions

View File

@@ -22,7 +22,7 @@ extern "C" {
#endif
typedef struct esp_transport_list_t* esp_transport_list_handle_t;
typedef struct esp_transport_internal* esp_transport_list_handle_t;
typedef struct esp_transport_item_t* esp_transport_handle_t;
typedef int (*connect_func)(esp_transport_handle_t t, const char *host, int port, int timeout_ms);
@@ -33,12 +33,14 @@ 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);
typedef struct esp_tls_last_error* esp_tls_error_handle_t;
/**
* @brief Create transport list
*
* @return A handle can hold all transports
*/
esp_transport_list_handle_t esp_transport_list_init();
esp_transport_list_handle_t esp_transport_list_init(void);
/**
* @brief Cleanup and free all transports, include itself,
@@ -91,7 +93,7 @@ esp_transport_handle_t esp_transport_list_get_transport(esp_transport_list_handl
*
* @return The transport handle
*/
esp_transport_handle_t esp_transport_init();
esp_transport_handle_t esp_transport_init(void);
/**
* @brief Cleanup and free memory the transport
@@ -298,6 +300,21 @@ esp_err_t esp_transport_set_async_connect_func(esp_transport_handle_t t, connect
*/
esp_err_t esp_transport_set_parent_transport_func(esp_transport_handle_t t, payload_transfer_func _parent_transport);
/**
* @brief Returns esp_tls error handle.
* Warning: The returned pointer is valid only as long as esp_transport_handle_t exists. Once transport
* handle gets destroyed, this value (esp_tls_error_handle_t) is freed automatically.
*
* @param[in] A transport handle
*
* @return
* - valid pointer of esp_error_handle_t
* - NULL if invalid transport handle
*/
esp_tls_error_handle_t esp_transport_get_error_handle(esp_transport_handle_t t);
#ifdef __cplusplus
}
#endif

View File

@@ -16,6 +16,7 @@
#define _ESP_TRANSPORT_SSL_H_
#include "esp_transport.h"
#include "esp_tls.h"
#ifdef __cplusplus
extern "C" {
@@ -27,7 +28,7 @@ extern "C" {
*
* @return the allocated esp_transport_handle_t, or NULL if the handle can not be allocated
*/
esp_transport_handle_t esp_transport_ssl_init();
esp_transport_handle_t esp_transport_ssl_init(void);
/**
* @brief Set SSL certificate data (as PEM format).
@@ -40,6 +41,24 @@ esp_transport_handle_t esp_transport_ssl_init();
*/
void esp_transport_ssl_set_cert_data(esp_transport_handle_t t, const char *data, int len);
/**
* @brief Set SSL certificate data (as DER format).
* Note that, this function stores the pointer to data, rather than making a copy.
* So this data must remain valid until after the connection is cleaned up
*
* @param t ssl transport
* @param[in] data The der data
* @param[in] len The length
*/
void esp_transport_ssl_set_cert_data_der(esp_transport_handle_t t, const char *data, int len);
/**
* @brief Enable global CA store for SSL connection
*
* @param t ssl transport
*/
void esp_transport_ssl_enable_global_ca_store(esp_transport_handle_t t);
/**
* @brief Set SSL client certificate data for mutual authentication (as PEM format).
* Note that, this function stores the pointer to data, rather than making a copy.
@@ -51,6 +70,17 @@ void esp_transport_ssl_set_cert_data(esp_transport_handle_t t, const char *data,
*/
void esp_transport_ssl_set_client_cert_data(esp_transport_handle_t t, const char *data, int len);
/**
* @brief Set SSL client certificate data for mutual authentication (as DER format).
* Note that, this function stores the pointer to data, rather than making a copy.
* So this data must remain valid until after the connection is cleaned up
*
* @param t ssl transport
* @param[in] data The der data
* @param[in] len The length
*/
void esp_transport_ssl_set_client_cert_data_der(esp_transport_handle_t t, const char *data, int len);
/**
* @brief Set SSL client key data for mutual authentication (as PEM format).
* Note that, this function stores the pointer to data, rather than making a copy.
@@ -62,6 +92,40 @@ void esp_transport_ssl_set_client_cert_data(esp_transport_handle_t t, const char
*/
void esp_transport_ssl_set_client_key_data(esp_transport_handle_t t, const char *data, int len);
/**
* @brief Set SSL client key data for mutual authentication (as DER format).
* Note that, this function stores the pointer to data, rather than making a copy.
* So this data must remain valid until after the connection is cleaned up
*
* @param t ssl transport
* @param[in] data The der data
* @param[in] len The length
*/
void esp_transport_ssl_set_client_key_data_der(esp_transport_handle_t t, const char *data, int len);
/**
* @brief Skip validation of certificate's common name field
*
* @note Skipping CN validation is not recommended
*
* @param t ssl transport
*/
void esp_transport_ssl_skip_common_name_check(esp_transport_handle_t t);
/**
* @brief Set PSK key and hint for PSK server/client verification in esp-tls component.
* Important notes:
* - This function stores the pointer to data, rather than making a copy.
* So this data must remain valid until after the connection is cleaned up
* - ESP_TLS_PSK_VERIFICATION config option must be enabled in menuconfig
* - certificate verification takes priority so it must not be configured
* to enable PSK method.
*
* @param t ssl transport
* @param[in] psk_hint_key psk key and hint structure defined in esp_tls.h
*/
void esp_transport_ssl_set_psk_key_hint(esp_transport_handle_t t, const psk_hint_key_t* psk_hint_key);
#ifdef __cplusplus
}
#endif

View File

@@ -26,7 +26,7 @@ extern "C" {
*
* @return the allocated esp_transport_handle_t, or NULL if the handle can not be allocated
*/
esp_transport_handle_t esp_transport_tcp_init();
esp_transport_handle_t esp_transport_tcp_init(void);
#ifdef __cplusplus

View File

@@ -20,6 +20,15 @@
extern "C" {
#endif
/**
* @brief Utility macro to be used for NULL ptr check after malloc
*
*/
#define ESP_TRANSPORT_MEM_CHECK(TAG, a, action) if (!(a)) { \
ESP_LOGE(TAG,"%s:%d (%s): %s", __FILE__, __LINE__, __FUNCTION__, "Memory exhausted"); \
action; \
}
/**
* @brief Convert milliseconds to timeval struct
*
@@ -29,11 +38,6 @@ extern "C" {
void esp_transport_utils_ms_to_timeval(int timeout_ms, struct timeval *tv);
#define ESP_TRANSPORT_MEM_CHECK(TAG, a, action) if (!(a)) { \
ESP_LOGE(TAG,"%s:%d (%s): %s", __FILE__, __LINE__, __FUNCTION__, "Memory exhausted"); \
action; \
}
#ifdef __cplusplus
}
#endif

View File

@@ -13,6 +13,13 @@
extern "C" {
#endif
typedef enum ws_transport_opcodes {
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,
} ws_transport_opcodes_t;
/**
* @brief Create web socket transport
@@ -23,8 +30,56 @@ extern "C" {
*/
esp_transport_handle_t esp_transport_ws_init(esp_transport_handle_t parent_handle);
/**
* @brief Set HTTP path to update protocol to websocket
*
* @param t websocket transport handle
* @param path The HTTP Path
*/
void esp_transport_ws_set_path(esp_transport_handle_t t, const char *path);
/**
* @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);
/**
* @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
* @param[in] timeout_ms The timeout milliseconds
*
* @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);
#ifdef __cplusplus