kfsw-platform/src/wallclock.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <errno.h> | ||
| 2 | #include <stdbool.h> | ||
| 3 | #include <stdint.h> | ||
| 4 | #include <time.h> | ||
| 5 | |||
| 6 | #include <zephyr/device.h> | ||
| 7 | #include <zephyr/devicetree.h> | ||
| 8 | #include <zephyr/sys/util.h> | ||
| 9 | #include <zephyr/sys/timeutil.h> | ||
| 10 | |||
| 11 | #include <kfsw/platform/wallclock.h> | ||
| 12 | |||
| 13 | #if DT_HAS_CHOSEN(kfsw_rtc) && defined(CONFIG_RTC) | ||
| 14 | |||
| 15 | #include <zephyr/drivers/rtc.h> | ||
| 16 | |||
| 17 | /* Wall clock on the RTC. The counter survives a software reset. */ | ||
| 18 | static const struct device *const rtc_device = DEVICE_DT_GET(DT_CHOSEN(kfsw_rtc)); | ||
| 19 | |||
| 20 | bool kfsw_wallclock_is_present(void) | ||
| 21 | { | ||
| 22 | return device_is_ready(rtc_device); | ||
| 23 | } | ||
| 24 | |||
| 25 | int kfsw_wallclock_get(int64_t *seconds) | ||
| 26 | { | ||
| 27 | struct rtc_time value; | ||
| 28 | int64_t unix_seconds; | ||
| 29 | int result; | ||
| 30 | |||
| 31 | if (seconds == NULL) { | ||
| 32 | return -EINVAL; | ||
| 33 | } | ||
| 34 | if (!device_is_ready(rtc_device)) { | ||
| 35 | return -ENOTSUP; | ||
| 36 | } | ||
| 37 | |||
| 38 | result = rtc_get_time(rtc_device, &value); | ||
| 39 | if (result != 0) { | ||
| 40 | /* -ENODATA means the RTC runs but was never set. */ | ||
| 41 | return result; | ||
| 42 | } | ||
| 43 | |||
| 44 | /* struct rtc_time is layout-compatible with struct tm. */ | ||
| 45 | unix_seconds = timeutil_timegm64((const struct tm *)&value); | ||
| 46 | if (unix_seconds < 0) { | ||
| 47 | return -ERANGE; | ||
| 48 | } | ||
| 49 | *seconds = unix_seconds; | ||
| 50 | return 0; | ||
| 51 | } | ||
| 52 | |||
| 53 | int kfsw_wallclock_set(int64_t seconds) | ||
| 54 | { | ||
| 55 | struct rtc_time value = {0}; | ||
| 56 | struct tm broken_down; | ||
| 57 | time_t as_time; | ||
| 58 | |||
| 59 | if (seconds < 0) { | ||
| 60 | return -EINVAL; | ||
| 61 | } | ||
| 62 | if (!device_is_ready(rtc_device)) { | ||
| 63 | return -ENOTSUP; | ||
| 64 | } | ||
| 65 | |||
| 66 | as_time = (time_t)seconds; | ||
| 67 | if (gmtime_r(&as_time, &broken_down) == NULL) { | ||
| 68 | return -EINVAL; | ||
| 69 | } | ||
| 70 | |||
| 71 | value.tm_sec = broken_down.tm_sec; | ||
| 72 | value.tm_min = broken_down.tm_min; | ||
| 73 | value.tm_hour = broken_down.tm_hour; | ||
| 74 | value.tm_mday = broken_down.tm_mday; | ||
| 75 | value.tm_mon = broken_down.tm_mon; | ||
| 76 | value.tm_year = broken_down.tm_year; | ||
| 77 | value.tm_wday = broken_down.tm_wday; | ||
| 78 | value.tm_yday = broken_down.tm_yday; | ||
| 79 | value.tm_isdst = -1; | ||
| 80 | value.tm_nsec = 0; | ||
| 81 | |||
| 82 | return rtc_set_time(rtc_device, &value); | ||
| 83 | } | ||
| 84 | |||
| 85 | #else /* no wall clock in this composition */ | ||
| 86 | |||
| 87 | ✗ | bool kfsw_wallclock_is_present(void) | |
| 88 | { | ||
| 89 | ✗ | return false; | |
| 90 | } | ||
| 91 | |||
| 92 | 36 | int kfsw_wallclock_get(int64_t *seconds) | |
| 93 | { | ||
| 94 | ARG_UNUSED(seconds); | ||
| 95 | 36 | return -ENOTSUP; | |
| 96 | } | ||
| 97 | |||
| 98 | ✗ | int kfsw_wallclock_set(int64_t seconds) | |
| 99 | { | ||
| 100 | ARG_UNUSED(seconds); | ||
| 101 | ✗ | return -ENOTSUP; | |
| 102 | } | ||
| 103 | |||
| 104 | #endif | ||
| 105 |