From 132892c101fd2df316e59e1e570dbb55edc1d553 Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Tue, 23 Apr 2024 12:04:45 +0530 Subject: [PATCH] fix(esp-tls): Use 64 bit variable for time instead of 32 bit Use appropriate API available on respective platform for obtaining time Closes https://github.com/espressif/esp-idf/issues/13593 --- components/esp-tls/CMakeLists.txt | 6 ++-- components/esp-tls/esp_tls.c | 12 ++++---- components/esp-tls/esp_tls_platform_port.c | 28 +++++++++++++++++++ .../private_include/esp_tls_platform_port.h | 24 ++++++++++++++++ 4 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 components/esp-tls/esp_tls_platform_port.c create mode 100644 components/esp-tls/private_include/esp_tls_platform_port.h diff --git a/components/esp-tls/CMakeLists.txt b/components/esp-tls/CMakeLists.txt index 10c16fcb87..3809b6473b 100644 --- a/components/esp-tls/CMakeLists.txt +++ b/components/esp-tls/CMakeLists.txt @@ -1,4 +1,4 @@ -set(srcs esp_tls.c esp-tls-crypto/esp_tls_crypto.c esp_tls_error_capture.c) +set(srcs esp_tls.c esp-tls-crypto/esp_tls_crypto.c esp_tls_error_capture.c esp_tls_platform_port.c) if(CONFIG_ESP_TLS_USING_MBEDTLS) list(APPEND srcs "esp_tls_mbedtls.c") @@ -9,7 +9,7 @@ if(CONFIG_ESP_TLS_USING_WOLFSSL) "esp_tls_wolfssl.c") endif() -set(priv_req http_parser) +set(priv_req http_parser esp_timer) if(NOT ${IDF_TARGET} STREQUAL "linux") list(APPEND priv_req lwip) endif() @@ -17,7 +17,7 @@ endif() idf_component_register(SRCS "${srcs}" INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR} esp-tls-crypto PRIV_INCLUDE_DIRS "private_include" - # mbedtls is public requirements becasue esp_tls.h + # mbedtls is public requirements because esp_tls.h # includes mbedtls header files. REQUIRES mbedtls PRIV_REQUIRES ${priv_req}) diff --git a/components/esp-tls/esp_tls.c b/components/esp-tls/esp_tls.c index 483ca29472..a23eb27e8e 100644 --- a/components/esp-tls/esp_tls.c +++ b/components/esp-tls/esp_tls.c @@ -16,6 +16,7 @@ #include "sdkconfig.h" #include "esp_tls.h" #include "esp_tls_private.h" +#include "esp_tls_platform_port.h" #include "esp_tls_error_capture_internal.h" #include #include @@ -537,9 +538,8 @@ int esp_tls_conn_new_sync(const char *hostname, int hostlen, int port, const esp if (!cfg || !tls || !hostname || hostlen < 0) { return -1; } - struct timeval time = {}; - gettimeofday(&time, NULL); - uint32_t start_time_ms = (time.tv_sec * 1000) + (time.tv_usec / 1000); + uint64_t start_time_us; + start_time_us = esp_tls_get_platform_time(); while (1) { int ret = esp_tls_low_level_conn(hostname, hostlen, port, cfg, tls); if (ret == 1) { @@ -548,10 +548,8 @@ 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) { - 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) { + uint64_t elapsed_time_us = esp_tls_get_platform_time() - start_time_us; + if ((elapsed_time_us / 1000) >= 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; diff --git a/components/esp-tls/esp_tls_platform_port.c b/components/esp-tls/esp_tls_platform_port.c new file mode 100644 index 0000000000..3067a0976e --- /dev/null +++ b/components/esp-tls/esp_tls_platform_port.c @@ -0,0 +1,28 @@ +/* + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "sdkconfig.h" + +#if CONFIG_IDF_TARGET_LINUX +#include +#include +#else +#include "esp_timer.h" +#endif + +uint64_t esp_tls_get_platform_time(void) +{ +#if CONFIG_IDF_TARGET_LINUX + // Use gettimeofday for Linux/MacOS, Ideally esp_timer should be used but it is not implemented for Linux/MacOS. + struct timeval time = {}; + gettimeofday(&time, NULL); + uint64_t curr_time = ((uint64_t)time.tv_sec * 1000000) + (time.tv_usec); + return curr_time; +#else + // For all other esp targets use esp_timer + return esp_timer_get_time(); +#endif +} diff --git a/components/esp-tls/private_include/esp_tls_platform_port.h b/components/esp-tls/private_include/esp_tls_platform_port.h new file mode 100644 index 0000000000..44bc0f3d3b --- /dev/null +++ b/components/esp-tls/private_include/esp_tls_platform_port.h @@ -0,0 +1,24 @@ +/* + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +// This file contains APIs which have different implementation across different targets e.g., Linux, ESP. + +#pragma once + +#include + +/* @brief + * + * Behaviour + * Linux: + * Returns the system time (64 bit) using gettimeofday in microseconds. This shall get changed if someone changes the system time using settimeofday + * ESP targets: + * Returns the time (64 bit) since boot obtained using esp_timer_get_time() in microseconds + * @return + * time uint64_t bit time value + * + */ +uint64_t esp_tls_get_platform_time(void);