forked from espressif/esp-idf
esp_tls: added connection timeout to esp_tls_conn_new_sync()
This commit is contained in:
@@ -689,8 +689,9 @@ esp_tls_t *esp_tls_conn_new(const char *hostname, int hostlen, int port, const e
|
|||||||
|
|
||||||
int esp_tls_conn_new_sync(const char *hostname, int hostlen, int port, const esp_tls_cfg_t *cfg, esp_tls_t *tls)
|
int esp_tls_conn_new_sync(const char *hostname, int hostlen, int port, const esp_tls_cfg_t *cfg, esp_tls_t *tls)
|
||||||
{
|
{
|
||||||
/* esp_tls_conn_new_sync() is a sync alternative to esp_tls_conn_new_async() with symetric function prototype
|
/* esp_tls_conn_new_sync() is a sync alternative to esp_tls_conn_new_async() with symmetric function prototype
|
||||||
it is an alternative to esp_tls_conn_new() which is left for compatibility reasons */
|
it is an alternative to esp_tls_conn_new() which is left for compatibility reasons */
|
||||||
|
size_t start = xTaskGetTickCount();
|
||||||
while (1) {
|
while (1) {
|
||||||
int ret = esp_tls_low_level_conn(hostname, hostlen, port, cfg, tls);
|
int ret = esp_tls_low_level_conn(hostname, hostlen, port, cfg, tls);
|
||||||
if (ret == 1) {
|
if (ret == 1) {
|
||||||
@@ -698,6 +699,14 @@ int esp_tls_conn_new_sync(const char *hostname, int hostlen, int port, const esp
|
|||||||
} else if (ret == -1) {
|
} else if (ret == -1) {
|
||||||
ESP_LOGE(TAG, "Failed to open new connection");
|
ESP_LOGE(TAG, "Failed to open new connection");
|
||||||
return -1;
|
return -1;
|
||||||
|
} else if (ret == 0 && cfg->timeout_ms >= 0) {
|
||||||
|
size_t timeout_ticks = pdMS_TO_TICKS(cfg->timeout_ms);
|
||||||
|
uint32_t expired = xTaskGetTickCount() - start;
|
||||||
|
if (expired >= timeout_ticks) {
|
||||||
|
ESP_LOGW(TAG, "Failed to open new connection in specified timeout");
|
||||||
|
ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_ESP, ESP_ERR_ESP_TLS_CONNECTION_TIMEOUT);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@@ -741,6 +750,7 @@ esp_tls_t *esp_tls_conn_http_new(const char *url, const esp_tls_cfg_t *cfg)
|
|||||||
get_port(url, &u), cfg, tls) == 1) {
|
get_port(url, &u), cfg, tls) == 1) {
|
||||||
return tls;
|
return tls;
|
||||||
}
|
}
|
||||||
|
esp_tls_conn_delete(tls);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -49,6 +49,8 @@ extern "C" {
|
|||||||
#define ESP_ERR_MBEDTLS_SSL_WRITE_FAILED (ESP_ERR_ESP_TLS_BASE + 0x0E) /*!< mbedtls api returned error */
|
#define ESP_ERR_MBEDTLS_SSL_WRITE_FAILED (ESP_ERR_ESP_TLS_BASE + 0x0E) /*!< mbedtls api returned error */
|
||||||
#define ESP_ERR_MBEDTLS_PK_PARSE_KEY_FAILED (ESP_ERR_ESP_TLS_BASE + 0x0F) /*!< mbedtls api returned failed */
|
#define ESP_ERR_MBEDTLS_PK_PARSE_KEY_FAILED (ESP_ERR_ESP_TLS_BASE + 0x0F) /*!< mbedtls api returned failed */
|
||||||
#define ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED (ESP_ERR_ESP_TLS_BASE + 0x10) /*!< mbedtls api returned failed */
|
#define ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED (ESP_ERR_ESP_TLS_BASE + 0x10) /*!< mbedtls api returned failed */
|
||||||
|
#define ESP_ERR_MBEDTLS_SSL_CONF_PSK_FAILED (ESP_ERR_ESP_TLS_BASE + 0x11) /*!< mbedtls api returned failed */
|
||||||
|
#define ESP_ERR_ESP_TLS_CONNECTION_TIMEOUT (ESP_ERR_ESP_TLS_BASE + 0x12) /*!< new connection in esp_tls_low_level_conn connection timeouted */
|
||||||
|
|
||||||
typedef struct esp_tls_last_error* esp_tls_error_handle_t;
|
typedef struct esp_tls_last_error* esp_tls_error_handle_t;
|
||||||
|
|
||||||
@@ -273,7 +275,7 @@ esp_tls_t *esp_tls_conn_new(const char *hostname, int hostlen, int port, const e
|
|||||||
* @return
|
* @return
|
||||||
* - -1 If connection establishment fails.
|
* - -1 If connection establishment fails.
|
||||||
* - 1 If connection establishment is successful.
|
* - 1 If connection establishment is successful.
|
||||||
* - 0 Reserved for connection state is in progress.
|
* - 0 If connection state is in progress.
|
||||||
*/
|
*/
|
||||||
int esp_tls_conn_new_sync(const char *hostname, int hostlen, int port, const esp_tls_cfg_t *cfg, esp_tls_t *tls);
|
int esp_tls_conn_new_sync(const char *hostname, int hostlen, int port, const esp_tls_cfg_t *cfg, esp_tls_t *tls);
|
||||||
|
|
||||||
|
@@ -581,6 +581,13 @@ static const esp_err_msg_t esp_err_msg_table[] = {
|
|||||||
# endif
|
# endif
|
||||||
# ifdef ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED
|
# ifdef ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED
|
||||||
ERR_TBL_IT(ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED), /* 32784 0x8010 mbedtls api returned failed */
|
ERR_TBL_IT(ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED), /* 32784 0x8010 mbedtls api returned failed */
|
||||||
|
# endif
|
||||||
|
# ifdef ESP_ERR_MBEDTLS_SSL_CONF_PSK_FAILED
|
||||||
|
ERR_TBL_IT(ESP_ERR_MBEDTLS_SSL_CONF_PSK_FAILED), /* 32785 0x8011 mbedtls api returned failed */
|
||||||
|
# endif
|
||||||
|
# ifdef ESP_ERR_ESP_TLS_CONNECTION_TIMEOUT
|
||||||
|
ERR_TBL_IT(ESP_ERR_ESP_TLS_CONNECTION_TIMEOUT), /* 32786 0x8012 new connection in esp_tls_low_level_conn
|
||||||
|
connection timeouted */
|
||||||
# endif
|
# endif
|
||||||
// components/esp_https_ota/include/esp_https_ota.h
|
// components/esp_https_ota/include/esp_https_ota.h
|
||||||
# ifdef ESP_ERR_HTTPS_OTA_BASE
|
# ifdef ESP_ERR_HTTPS_OTA_BASE
|
||||||
|
@@ -71,7 +71,7 @@ static int ssl_connect(esp_transport_handle_t t, const char *host, int port, int
|
|||||||
ssl->cfg.timeout_ms = timeout_ms;
|
ssl->cfg.timeout_ms = timeout_ms;
|
||||||
ssl->ssl_initialized = true;
|
ssl->ssl_initialized = true;
|
||||||
ssl->tls = esp_tls_init();
|
ssl->tls = esp_tls_init();
|
||||||
if (esp_tls_conn_new_sync(host, strlen(host), port, &ssl->cfg, ssl->tls) < 0) {
|
if (esp_tls_conn_new_sync(host, strlen(host), port, &ssl->cfg, ssl->tls) <= 0) {
|
||||||
ESP_LOGE(TAG, "Failed to open a new connection");
|
ESP_LOGE(TAG, "Failed to open a new connection");
|
||||||
esp_transport_set_errors(t, ssl->tls->error_handle);
|
esp_transport_set_errors(t, ssl->tls->error_handle);
|
||||||
esp_tls_conn_delete(ssl->tls);
|
esp_tls_conn_delete(ssl->tls);
|
||||||
|
Reference in New Issue
Block a user