diff --git a/platform/mbed_rtc_time.cpp b/platform/mbed_rtc_time.cpp index 37cb1cf526..b481ef908b 100644 --- a/platform/mbed_rtc_time.cpp +++ b/platform/mbed_rtc_time.cpp @@ -78,12 +78,22 @@ static void (*_rtc_write)(time_t t) = NULL; #ifdef __cplusplus extern "C" { #endif -#if defined (__ICCARM__) -time_t __time32(time_t *timer) -#else -time_t time(time_t *timer) -#endif +int settimeofday(const struct timeval *tv, MBED_UNUSED const struct timezone *tz) +{ + _mutex->lock(); + if (_rtc_init != NULL) { + _rtc_init(); + } + if (_rtc_write != NULL) { + _rtc_write(tv->tv_sec); + } + _mutex->unlock(); + + return 0; +} + +int gettimeofday(struct timeval *tv, MBED_UNUSED void *tz) { _mutex->lock(); if (_rtc_isenabled != NULL) { @@ -92,28 +102,40 @@ time_t time(time_t *timer) } } - time_t t = (time_t) -1; + time_t t = (time_t) - 1; if (_rtc_read != NULL) { t = _rtc_read(); } - if (timer != NULL) { - *timer = t; - } + tv->tv_sec = t; + tv->tv_usec = 0; + _mutex->unlock(); - return t; + + return 0; } +#if defined (__ICCARM__) +time_t __time32(time_t *timer) +#else +time_t time(time_t *timer) +#endif +{ + struct timeval tv; + gettimeofday(&tv, NULL); + + if (timer != NULL) { + *timer = tv.tv_sec; + } + + return tv.tv_sec; +} + + void set_time(time_t t) { - _mutex->lock(); - if (_rtc_init != NULL) { - _rtc_init(); - } - if (_rtc_write != NULL) { - _rtc_write(t); - } - _mutex->unlock(); + const struct timeval tv = { t, 0 }; + settimeofday(&tv, NULL); } void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void)) @@ -127,7 +149,6 @@ void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init } - #ifdef __cplusplus } #endif diff --git a/platform/mbed_rtc_time.h b/platform/mbed_rtc_time.h index 1dc021c806..5c5c9a5514 100644 --- a/platform/mbed_rtc_time.h +++ b/platform/mbed_rtc_time.h @@ -28,6 +28,14 @@ extern "C" { #endif +/* Timeval definition for non GCC_ARM toolchains */ +#if !defined(__GNUC__) || defined(__CC_ARM) || defined(__clang__) +struct timeval { + time_t tv_sec; + int32_t tv_usec; +}; +#endif + /** Implementation of the C time.h functions * * Provides mechanisms to set and read the current time, based @@ -90,6 +98,30 @@ void set_time(time_t t); */ void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void)); +/** Standard lib retarget, get time since Epoch + * + * @param tv Structure containing time_t secondsa and suseconds_t microseconds. Due to + * separate target specific RTC implementations only the seconds component is used. + * @param tz DEPRECATED IN THE STANDARD: This parameter is left in for legacy code. It is + * not used. + * @return 0 on success, -1 on a failure. + * @note Synchronization level: Thread safe + * + */ +int gettimeofday(struct timeval *tv, void *tz); + +/** Standard lib retarget, set time since Epoch + * + * @param tv Structure containing time_t secondsa and suseconds_t microseconds. Due to + * separate target specific RTC implementations only the seconds component is used. + * @param tz DEPRECATED IN THE STANDARD: This parameter is left in for legacy code. It is + * not used. + * @return Time in seconds on success, -1 on a failure. + * @note Synchronization level: Thread safe + * + */ +int settimeofday(const struct timeval *tv, const struct timezone *tz); + #ifdef __cplusplus } #endif