From 295507bc1bae967632d1395eb0b9f5190d5e2ca8 Mon Sep 17 00:00:00 2001 From: Harshit Malpani Date: Mon, 28 Nov 2022 14:35:53 +0530 Subject: [PATCH] esp-tls: use gettimeofday() instead of xTaskGetTickCount() --- components/esp-tls/esp_tls.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/components/esp-tls/esp_tls.c b/components/esp-tls/esp_tls.c index 0f5ced694d..df41e7457c 100644 --- a/components/esp-tls/esp_tls.c +++ b/components/esp-tls/esp_tls.c @@ -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) { - 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) { int ret = esp_tls_low_level_conn(hostname, hostlen, port, cfg, tls); 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"); 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) { + gettimeofday(&time, NULL); + uint32_t current_time_ms = (time.tv_sec * 1000) + (time.tv_usec / 1000); + 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_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ESP_TLS_ERR_TYPE_ESP, ESP_ERR_ESP_TLS_CONNECTION_TIMEOUT); return 0;