Selaa lähdekoodia

Merge branch 'bugfix/esp_tls_blocking_timeout_v3.3' into 'release/v3.3'

esp-tls: add timeout for blocking connection (v3.3)

See merge request espressif/esp-idf!7397
Angus Gratton 6 vuotta sitten
vanhempi
sitoutus
f8151b2bfe
1 muutettua tiedostoa jossa 9 lisäystä ja 0 poistoa
  1. 9 0
      components/esp-tls/esp_tls.c

+ 9 - 0
components/esp-tls/esp_tls.c

@@ -481,6 +481,7 @@ esp_tls_t *esp_tls_conn_new(const char *hostname, int hostlen, int port, const e
     }
     /* esp_tls_conn_new() API establishes connection in a blocking manner thus this loop ensures that esp_tls_conn_new()
        API returns only after connection is established unless there is an error*/
+    size_t start = xTaskGetTickCount();
     while (1) {
         int ret = esp_tls_low_level_conn(hostname, hostlen, port, cfg, tls);
         if (ret == 1) {
@@ -489,6 +490,14 @@ esp_tls_t *esp_tls_conn_new(const char *hostname, int hostlen, int port, const e
             esp_tls_conn_delete(tls);
             ESP_LOGE(TAG, "Failed to open new connection");
             return NULL;
+        } 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_tls_conn_delete(tls);
+                ESP_LOGE(TAG, "Failed to open new connection in specified timeout");
+                return NULL;
+            }
         }
     }
     return NULL;