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
pull/13854/head
Aditya Patwardhan 2024-04-23 12:04:45 +05:30
rodzic ef716a2390
commit 132892c101
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: E628B2648FBF0DD8
4 zmienionych plików z 60 dodań i 10 usunięć

Wyświetl plik

@ -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})

Wyświetl plik

@ -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 <fcntl.h>
#include <errno.h>
@ -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;

Wyświetl plik

@ -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 <sys/time.h>
#include <time.h>
#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
}

Wyświetl plik

@ -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 <stdint.h>
/* @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);