esp_tls: fail connection if esp_tls_conn_new() timeouts

This commit is contained in:
David Cermak 2020-01-13 16:22:09 +01:00 committed by bot
parent d19c1c4b3c
commit 233d4b94bd

View file

@ -315,6 +315,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) {
@ -323,6 +324,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;