فهرست منبع

Merge branch 'feature/use_gettimeofday_instead_of_xTaskGetTickCount' into 'master'

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

See merge request espressif/esp-idf!21300
Aditya Patwardhan 3 سال پیش
والد
کامیت
7955a11384
1فایلهای تغییر یافته به همراه7 افزوده شده و 4 حذف شده
  1. 7 4
      components/esp-tls/esp_tls.c

+ 7 - 4
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;