Skip to content

Commit 39771b6

Browse files
fix(esp-tls): Use 64 bit variable for time instead of 32 bit
Use appropriate API available on respective platform for obtaining time Closes espressif#13593
1 parent e486f3b commit 39771b6

File tree

4 files changed

+60
-10
lines changed

4 files changed

+60
-10
lines changed

components/esp-tls/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
set(srcs esp_tls.c esp-tls-crypto/esp_tls_crypto.c esp_tls_error_capture.c)
1+
set(srcs esp_tls.c esp-tls-crypto/esp_tls_crypto.c esp_tls_error_capture.c esp_tls_platform_port.c)
22
if(CONFIG_ESP_TLS_USING_MBEDTLS)
33
list(APPEND srcs
44
"esp_tls_mbedtls.c")
@@ -9,15 +9,15 @@ if(CONFIG_ESP_TLS_USING_WOLFSSL)
99
"esp_tls_wolfssl.c")
1010
endif()
1111

12-
set(priv_req http_parser)
12+
set(priv_req http_parser esp_timer)
1313
if(NOT ${IDF_TARGET} STREQUAL "linux")
1414
list(APPEND priv_req lwip)
1515
endif()
1616

1717
idf_component_register(SRCS "${srcs}"
1818
INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR} esp-tls-crypto
1919
PRIV_INCLUDE_DIRS "private_include"
20-
# mbedtls is public requirements becasue esp_tls.h
20+
# mbedtls is public requirements because esp_tls.h
2121
# includes mbedtls header files.
2222
REQUIRES mbedtls
2323
PRIV_REQUIRES ${priv_req})

components/esp-tls/esp_tls.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "sdkconfig.h"
1717
#include "esp_tls.h"
1818
#include "esp_tls_private.h"
19+
#include "esp_tls_platform_port.h"
1920
#include "esp_tls_error_capture_internal.h"
2021
#include <fcntl.h>
2122
#include <errno.h>
@@ -537,9 +538,8 @@ int esp_tls_conn_new_sync(const char *hostname, int hostlen, int port, const esp
537538
if (!cfg || !tls || !hostname || hostlen < 0) {
538539
return -1;
539540
}
540-
struct timeval time = {};
541-
gettimeofday(&time, NULL);
542-
uint32_t start_time_ms = (time.tv_sec * 1000) + (time.tv_usec / 1000);
541+
uint64_t start_time_us;
542+
start_time_us = esp_tls_get_platform_time();
543543
while (1) {
544544
int ret = esp_tls_low_level_conn(hostname, hostlen, port, cfg, tls);
545545
if (ret == 1) {
@@ -548,10 +548,8 @@ int esp_tls_conn_new_sync(const char *hostname, int hostlen, int port, const esp
548548
ESP_LOGE(TAG, "Failed to open new connection");
549549
return -1;
550550
} else if (ret == 0 && cfg->timeout_ms >= 0) {
551-
gettimeofday(&time, NULL);
552-
uint32_t current_time_ms = (time.tv_sec * 1000) + (time.tv_usec / 1000);
553-
uint32_t elapsed_time_ms = current_time_ms - start_time_ms;
554-
if (elapsed_time_ms >= cfg->timeout_ms) {
551+
uint64_t elapsed_time_us = esp_tls_get_platform_time() - start_time_us;
552+
if ((elapsed_time_us / 1000) >= cfg->timeout_ms) {
555553
ESP_LOGW(TAG, "Failed to open new connection in specified timeout");
556554
ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ESP_TLS_ERR_TYPE_ESP, ESP_ERR_ESP_TLS_CONNECTION_TIMEOUT);
557555
return 0;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "sdkconfig.h"
8+
9+
#if CONFIG_IDF_TARGET_LINUX
10+
#include <sys/time.h>
11+
#include <time.h>
12+
#else
13+
#include "esp_timer.h"
14+
#endif
15+
16+
uint64_t esp_tls_get_platform_time(void)
17+
{
18+
#if CONFIG_IDF_TARGET_LINUX
19+
// Use gettimeofday for Linux/MacOS, Ideally esp_timer should be used but it is not implemented for Linux/MacOS.
20+
struct timeval time = {};
21+
gettimeofday(&time, NULL);
22+
uint64_t curr_time = ((uint64_t)time.tv_sec * 1000000) + (time.tv_usec);
23+
return curr_time;
24+
#else
25+
// For all other esp targets use esp_timer
26+
return esp_timer_get_time();
27+
#endif
28+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
// This file contains APIs which have different implementation across different targets e.g., Linux, ESP.
8+
9+
#pragma once
10+
11+
#include <stdint.h>
12+
13+
/* @brief
14+
*
15+
* Behaviour
16+
* Linux:
17+
* Returns the system time (64 bit) using gettimeofday in microseconds. This shall get changed if someone changes the system time using settimeofday
18+
* ESP targets:
19+
* Returns the time (64 bit) since boot obtained using esp_timer_get_time() in microseconds
20+
* @return
21+
* time uint64_t bit time value
22+
*
23+
*/
24+
uint64_t esp_tls_get_platform_time(void);

0 commit comments

Comments
 (0)