Zephyr: add support for RTC time
For ASN date validation, the actual wall clock time is needed from an RTC. This commit adds support to read the RTC time in case it is available in the Zephyr system. If the RTC is not available or an error occurs during the readout, we fallback to the old implementation which only supports relative time since boot. Signed-off-by: Tobias Frauenschläger <t.frauenschlaeger@me.com>
This commit is contained in:
parent
4d8bbd7091
commit
182eaa0b63
@ -3164,6 +3164,30 @@ time_t z_time(time_t * timer)
|
||||
{
|
||||
struct timespec ts;
|
||||
|
||||
#if defined(CONFIG_RTC) && \
|
||||
(defined(CONFIG_PICOLIBC) || defined(CONFIG_NEWLIB_LIBC))
|
||||
/* Try to obtain the actual time from an RTC */
|
||||
static const struct device *rtc = DEVICE_DT_GET(DT_NODELABEL(rtc));
|
||||
|
||||
if (device_is_ready(rtc)) {
|
||||
struct rtc_time rtc_time;
|
||||
struct tm *tm_time = rtc_time_to_tm(&rtc_time);
|
||||
|
||||
int ret = rtc_get_time(rtc, &rtc_time);
|
||||
|
||||
if (ret == 0) {
|
||||
time_t epochTime = mktime(tm_time);
|
||||
|
||||
if (timer != NULL)
|
||||
*timer = epochTime;
|
||||
|
||||
return epochTime;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Fallback to uptime since boot. This works for relative times, but
|
||||
* not for ASN.1 date validation */
|
||||
if (clock_gettime(CLOCK_REALTIME, &ts) == 0)
|
||||
if (timer != NULL)
|
||||
*timer = ts.tv_sec;
|
||||
|
@ -985,6 +985,14 @@ WOLFSSL_ABI WOLFSSL_API int wolfCrypt_Cleanup(void);
|
||||
#include <time.h>
|
||||
#endif
|
||||
|
||||
#if defined(CONFIG_RTC)
|
||||
#if defined(CONFIG_PICOLIBC) || defined(CONFIG_NEWLIB_LIBC)
|
||||
#include <zephyr/drivers/rtc.h>
|
||||
#else
|
||||
#warning "RTC support needs picolibc or newlib (nano)"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
time_t z_time(time_t *timer);
|
||||
|
||||
#define XTIME(tl) z_time((tl))
|
||||
|
Loading…
x
Reference in New Issue
Block a user