Merge branch 'feature/use_gettimeofday_instead_of_xTaskGetTickCount' into 'master'

esp-tls: use gettimeofday() instead of xTaskGetTickCount()

See merge request espressif/esp-idf!21300
This commit is contained in:
Aditya Patwardhan 2022-11-30 14:08:21 +08:00
commit 7955a11384

View File

@ -459,7 +459,9 @@ esp_err_t esp_tls_plain_tcp_connect(const char *host, int hostlen, int port, con
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)
{ {
size_t start = xTaskGetTickCount(); struct timeval time = {};
gettimeofday(&time, NULL);
uint32_t start_time_ms = (time.tv_sec * 1000) + (time.tv_usec / 1000);
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) {
@ -468,9 +470,10 @@ int esp_tls_conn_new_sync(const char *hostname, int hostlen, int port, const esp
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) { } else if (ret == 0 && cfg->timeout_ms >= 0) {
size_t timeout_ticks = pdMS_TO_TICKS(cfg->timeout_ms); gettimeofday(&time, NULL);
uint32_t expired = xTaskGetTickCount() - start; uint32_t current_time_ms = (time.tv_sec * 1000) + (time.tv_usec / 1000);
if (expired >= timeout_ticks) { uint32_t elapsed_time_ms = current_time_ms - start_time_ms;
if (elapsed_time_ms >= cfg->timeout_ms) {
ESP_LOGW(TAG, "Failed to open new connection in specified timeout"); ESP_LOGW(TAG, "Failed to open new connection in specified timeout");
ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ESP_TLS_ERR_TYPE_ESP, ESP_ERR_ESP_TLS_CONNECTION_TIMEOUT); ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ESP_TLS_ERR_TYPE_ESP, ESP_ERR_ESP_TLS_CONNECTION_TIMEOUT);
return 0; return 0;