From 0024733ea089603bbee97f353443b59838690f15 Mon Sep 17 00:00:00 2001 From: Michael Schwarcz Date: Mon, 29 Oct 2018 13:16:10 +0200 Subject: [PATCH] Add update/get APIs for RTC time. - rtc_update API does nothing if no RTC. - rtc_get API returns 0 if no RTC. --- secure_time/secure_time_impl.c | 9 ++++----- secure_time/secure_time_utils.cpp | 19 +++++++++++++++++++ secure_time/secure_time_utils.h | 14 ++++++++++++++ 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/secure_time/secure_time_impl.c b/secure_time/secure_time_impl.c index d6489989bd..97b2fa8e53 100644 --- a/secure_time/secure_time_impl.c +++ b/secure_time/secure_time_impl.c @@ -19,7 +19,6 @@ #include "secure_time_storage.h" #include "secure_time_crypto.h" #include "mbed_error.h" -#include "platform/mbed_rtc_time.h" #include #if SECURE_TIME_ENABLED @@ -292,7 +291,7 @@ int32_t secure_time_set_trusted_commit_impl(const void *blob, size_t blob_size) // Set RTC with new time if it is around 1-2 minutes forward/backward // than current RTC time. if(llabs(new_time - rtc_time) > SECURE_TIME_MIN_RTC_LATENCY_SEC) { - set_time(new_time); + secure_time_update_rtc_time(new_time); } // Read the current stored time from secure storage. @@ -337,9 +336,9 @@ static void set_time_forward(uint64_t new_time, uint64_t curr_os_time) secure_time_update_boot_time(new_time); // Set RTC with new time if it is around 1-2 minutes forward than current time. - uint64_t rtc_time = (uint64_t)time(NULL); + uint64_t rtc_time = secure_time_get_rtc_time(); if((new_time - rtc_time) > SECURE_TIME_MIN_RTC_LATENCY_SEC) { - set_time(new_time); + secure_time_update_rtc_time(new_time); } // Write new time to secure storage entry of current stored time if it's more than 1 day forward @@ -419,7 +418,7 @@ uint64_t secure_time_get_impl(void) secure_time_get_stored_time(&stored_time); // Get current RTC time - uint64_t rtc_time = (uint64_t)time(NULL); + uint64_t rtc_time = secure_time_get_rtc_time(); // Set new time according to the latest between the RTC and the stored time uint64_t new_time = SECURE_TIME_MAX(stored_time, rtc_time); diff --git a/secure_time/secure_time_utils.cpp b/secure_time/secure_time_utils.cpp index 5316b7aa2c..af70b8387f 100644 --- a/secure_time/secure_time_utils.cpp +++ b/secure_time/secure_time_utils.cpp @@ -17,6 +17,7 @@ #include "secure_time_client_spe.h" #include "mbed_error.h" #include "rtos/Kernel.h" +#include "platform/mbed_rtc_time.h" #if SECURE_TIME_ENABLED @@ -41,4 +42,22 @@ uint64_t secure_time_get_boot_time(void) return g_boot_time_in_secs; } +void secure_time_update_rtc_time(uint64_t new_time) +{ +#if DEVICE_RTC + set_time((time_t)new_time); +#endif +} + +uint64_t secure_time_get_rtc_time(void) +{ + uint64_t rtc_time = 0; + +#if DEVICE_RTC + rtc_time = (uint64_t)time(NULL); +#endif + + return rtc_time; +} + #endif // SECURE_TIME_ENABLED diff --git a/secure_time/secure_time_utils.h b/secure_time/secure_time_utils.h index 37f88ab5e9..a045cb046d 100644 --- a/secure_time/secure_time_utils.h +++ b/secure_time/secure_time_utils.h @@ -82,6 +82,20 @@ void secure_time_update_boot_time(uint64_t new_time); */ uint64_t secure_time_get_seconds_since_boot(void); +/* + * Update the device RTC time according to the new time. + * + * @param[in] new_time Time value in seconds since EPOCH. + */ +void secure_time_update_rtc_time(uint64_t new_time); + +/* + * Return the device RTC time in seconds since EPOCH. + * + * @return 64-bit value of seconds, 0 if RTC not supported. + */ +uint64_t secure_time_get_rtc_time(void); + #ifdef __cplusplus } #endif