From c370a8f7ce0d08e4d9f333a7c9aed400c2b11173 Mon Sep 17 00:00:00 2001 From: KonstantinKondrashov Date: Mon, 8 Jun 2020 22:57:38 +0800 Subject: [PATCH 1/2] newlib: Fix adjtime, returns the amount of time remaining from any previous adjustment If the olddelta argument is not a null pointer, the adjtime function returns information about any previous time adjustment that has not yet completed. Closes: https://github.com/espressif/esp-idf/issues/5194 --- components/newlib/time.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/components/newlib/time.c b/components/newlib/time.c index b1d25af153..246315702c 100644 --- a/components/newlib/time.c +++ b/components/newlib/time.c @@ -193,6 +193,18 @@ static void adjtime_corr_stop (void) int adjtime(const struct timeval *delta, struct timeval *outdelta) { #if defined( WITH_FRC ) || defined( WITH_RTC ) + if(outdelta != NULL){ + _lock_acquire(&s_adjust_time_lock); + adjust_boot_time(); + if (adjtime_start != 0) { + outdelta->tv_sec = adjtime_total_correction / 1000000L; + outdelta->tv_usec = adjtime_total_correction % 1000000L; + } else { + outdelta->tv_sec = 0; + outdelta->tv_usec = 0; + } + _lock_release(&s_adjust_time_lock); + } if(delta != NULL){ int64_t sec = delta->tv_sec; int64_t usec = delta->tv_usec; @@ -211,18 +223,6 @@ int adjtime(const struct timeval *delta, struct timeval *outdelta) adjtime_total_correction = sec * 1000000L + usec; _lock_release(&s_adjust_time_lock); } - if(outdelta != NULL){ - _lock_acquire(&s_adjust_time_lock); - adjust_boot_time(); - if (adjtime_start != 0) { - outdelta->tv_sec = adjtime_total_correction / 1000000L; - outdelta->tv_usec = adjtime_total_correction % 1000000L; - } else { - outdelta->tv_sec = 0; - outdelta->tv_usec = 0; - } - _lock_release(&s_adjust_time_lock); - } return 0; #else return -1; From 4d735d82796fad029d224e222512fcedac635b0a Mon Sep 17 00:00:00 2001 From: KonstantinKondrashov Date: Mon, 15 Jun 2020 22:46:41 +0800 Subject: [PATCH 2/2] newlib: Fix UT for adjtime --- components/newlib/test/test_time.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/components/newlib/test/test_time.c b/components/newlib/test/test_time.c index 10be5b491d..29d5bc11bd 100644 --- a/components/newlib/test/test_time.c +++ b/components/newlib/test/test_time.c @@ -81,25 +81,27 @@ TEST_CASE("test adjtime function", "[newlib]") tv_delta.tv_sec = 0; tv_delta.tv_usec = -900000; TEST_ASSERT_EQUAL(adjtime(&tv_delta, &tv_outdelta), 0); - TEST_ASSERT_TRUE(tv_outdelta.tv_usec <= 0); - - tv_delta.tv_sec = 0; - tv_delta.tv_usec = 900000; - TEST_ASSERT_EQUAL(adjtime(&tv_delta, &tv_outdelta), 0); - TEST_ASSERT_TRUE(tv_outdelta.tv_usec >= 0); + TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, 0); + TEST_ASSERT_EQUAL(tv_outdelta.tv_usec, 0); + TEST_ASSERT_EQUAL(adjtime(NULL, &tv_outdelta), 0); + TEST_ASSERT_LESS_THAN(-800000, tv_outdelta.tv_usec); tv_delta.tv_sec = -4; tv_delta.tv_usec = -900000; - TEST_ASSERT_EQUAL(adjtime(&tv_delta, &tv_outdelta), 0); + TEST_ASSERT_EQUAL(adjtime(&tv_delta, NULL), 0); + TEST_ASSERT_EQUAL(adjtime(NULL, &tv_outdelta), 0); TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, -4); - TEST_ASSERT_TRUE(tv_outdelta.tv_usec <= 0); + TEST_ASSERT_LESS_THAN(-800000, tv_outdelta.tv_usec); // after settimeofday() adjtime() is stopped tv_delta.tv_sec = 15; tv_delta.tv_usec = 900000; TEST_ASSERT_EQUAL(adjtime(&tv_delta, &tv_outdelta), 0); + TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, -4); + TEST_ASSERT_LESS_THAN(-800000, tv_outdelta.tv_usec); + TEST_ASSERT_EQUAL(adjtime(NULL, &tv_outdelta), 0); TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, 15); - TEST_ASSERT_TRUE(tv_outdelta.tv_usec >= 0); + TEST_ASSERT_GREATER_OR_EQUAL(800000, tv_outdelta.tv_usec); TEST_ASSERT_EQUAL(gettimeofday(&tv_time, NULL), 0); TEST_ASSERT_EQUAL(settimeofday(&tv_time, NULL), 0); @@ -112,21 +114,24 @@ TEST_CASE("test adjtime function", "[newlib]") tv_delta.tv_sec = 15; tv_delta.tv_usec = 900000; TEST_ASSERT_EQUAL(adjtime(&tv_delta, &tv_outdelta), 0); + TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, 0); + TEST_ASSERT_EQUAL(tv_outdelta.tv_usec, 0); + TEST_ASSERT_EQUAL(adjtime(NULL, &tv_outdelta), 0); TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, 15); - TEST_ASSERT_TRUE(tv_outdelta.tv_usec >= 0); + TEST_ASSERT_GREATER_OR_EQUAL(800000, tv_outdelta.tv_usec); TEST_ASSERT_EQUAL(gettimeofday(&tv_time, NULL), 0); TEST_ASSERT_EQUAL(adjtime(NULL, &tv_outdelta), 0); TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, 15); - TEST_ASSERT_TRUE(tv_outdelta.tv_usec >= 0); + TEST_ASSERT_GREATER_OR_EQUAL(800000, tv_outdelta.tv_usec); tv_delta.tv_sec = 1; tv_delta.tv_usec = 0; TEST_ASSERT_EQUAL(adjtime(&tv_delta, NULL), 0); vTaskDelay(1000 / portTICK_PERIOD_MS); TEST_ASSERT_EQUAL(adjtime(NULL, &tv_outdelta), 0); - TEST_ASSERT_TRUE(tv_outdelta.tv_sec == 0); + TEST_ASSERT_EQUAL(tv_outdelta.tv_sec, 0); // the correction will be equal to (1_000_000us >> 6) = 15_625 us. TEST_ASSERT_TRUE(1000000L - tv_outdelta.tv_usec >= 15600); TEST_ASSERT_TRUE(1000000L - tv_outdelta.tv_usec <= 15650);