Infuse-IoT SDK API 0.0.1
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
epoch.h
Go to the documentation of this file.
1
16#ifndef INFUSE_SDK_INCLUDE_INFUSE_TIME_EPOCH_H_
17#define INFUSE_SDK_INCLUDE_INFUSE_TIME_EPOCH_H_
18
19#include <stdint.h>
20#include <stdbool.h>
21#include <time.h>
22
23#include <zephyr/kernel.h>
24#include <zephyr/toolchain.h>
25#include <zephyr/sys/timeutil.h>
26
27/* Time conversion functions */
28#include "epoch_units.h"
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
40#define SECONDS_PER_MINUTE (60)
41#define SECONDS_PER_HOUR (60 * SECONDS_PER_MINUTE)
42#define SECONDS_PER_DAY (24 * SECONDS_PER_HOUR)
43#define SECONDS_PER_WEEK (7 * SECONDS_PER_DAY)
44
50#define INFUSE_EPOCH_TIME_GPS_UNIX_OFFSET_SECONDS_LEAP 18
51
55#define INFUSE_EPOCH_TIME_GPS_UNIX_OFFSET_SECONDS_BASE 315964800
56
60#define INFUSE_EPOCH_TIME_TICKS_PER_SEC (UINT16_MAX + 1)
61
62/* Source of time knowledge */
64 /* No time knowledge */
66 /* Time from GNSS constellation (GPS, Beidou, etc) */
68 /* Time from Network Time Protocol (IP) */
70 /* Time directly set by Remote Procedure Call */
72 /* Unknown time source value */
74 /* Time has been preserved across a reboot */
76} __packed;
77
88 struct timeutil_sync_instant old,
89 struct timeutil_sync_instant new, void *user_ctx);
90
91 /* User provided context pointer */
92 void *user_ctx;
93
94 sys_snode_t node;
95};
96
103
112static inline bool epoch_time_trusted_source(enum epoch_time_source source, bool recovered_ok)
113{
114 enum epoch_time_source base = source & ~TIME_SOURCE_RECOVERED;
115 bool recovered = !!(source & TIME_SOURCE_RECOVERED);
116 bool base_good = IN_RANGE(base, TIME_SOURCE_NONE + 1, TIME_SOURCE_INVALID - 1);
117
118 return base_good && (!recovered || recovered_ok);
119}
120
127static inline uint64_t epoch_time_seconds(uint64_t epoch_time)
128{
129 return epoch_time >> 16;
130}
131
138static inline uint16_t epoch_time_subseconds(uint64_t epoch_time)
139{
140 return epoch_time & 0xFFFF;
141}
142
149static inline uint16_t epoch_time_milliseconds(uint64_t epoch_time)
150{
152}
153
161static inline uint64_t epoch_time_from(uint64_t seconds, uint16_t subseconds)
162{
163 return (seconds << 16) | subseconds;
164}
165
174static inline uint64_t epoch_time_from_gps(uint16_t week, uint32_t week_seconds,
175 uint16_t subseconds)
176{
177 uint64_t seconds = (SECONDS_PER_WEEK * (uint64_t)week) + week_seconds;
178
179 return epoch_time_from(seconds, subseconds);
180}
181
192static inline uint32_t unix_time_from_epoch(uint64_t epoch_time)
193{
196}
197
209static inline uint64_t epoch_time_from_unix(uint32_t unix_time, uint16_t subseconds)
210{
211 uint64_t epoch_seconds = unix_time - INFUSE_EPOCH_TIME_GPS_UNIX_OFFSET_SECONDS_BASE +
213
214 return epoch_time_from(epoch_seconds, subseconds);
215}
216
224uint64_t ticks_from_epoch_time(uint64_t epoch_time);
225
233uint64_t epoch_time_from_ticks(uint64_t ticks);
234
243uint32_t epoch_period_from_array_ticks(uint64_t array_ticks, uint16_t array_num);
244
250static inline uint64_t epoch_time_now(void)
251{
252 return epoch_time_from_ticks(k_uptime_ticks());
253}
254
264void epoch_time_unix_calendar(uint64_t epoch_time, struct tm *calendar);
265
272
283 struct timeutil_sync_instant *reference);
284
292
303int epoch_time_reference_shift(const struct timeutil_sync_instant *ref_a,
304 const struct timeutil_sync_instant *ref_b, int64_t *epoch_shift);
305
306#ifdef CONFIG_ZTEST
307
311void epoch_time_reset(void);
312
313#endif /* CONFIG_ZTEST */
314
319#ifdef __cplusplus
320}
321#endif
322
323#endif /* INFUSE_SDK_INCLUDE_INFUSE_TIME_EPOCH_H_ */
Conversion between epoch ticks and other units.
#define k_epoch_to_ms_near32(t)
Convert epoch ticks to milliseconds.
Definition epoch_units.h:564
void epoch_time_unix_calendar(uint64_t epoch_time, struct tm *calendar)
Convert a epoch time to a unix time calendar.
epoch_time_source
Definition epoch.h:63
uint32_t epoch_period_from_array_ticks(uint64_t array_ticks, uint16_t array_num)
Get the epoch time period associated with an array.
static bool epoch_time_trusted_source(enum epoch_time_source source, bool recovered_ok)
Determine whether a given time source should be trusted.
Definition epoch.h:112
static uint64_t epoch_time_from(uint64_t seconds, uint16_t subseconds)
Convert seconds and subseconds to complete epoch time.
Definition epoch.h:161
uint64_t epoch_time_from_ticks(uint64_t ticks)
Get the epoch time associated with a local uptime.
static uint16_t epoch_time_subseconds(uint64_t epoch_time)
Extracts epoch subseconds from a complete epoch time.
Definition epoch.h:138
int epoch_time_reference_shift(const struct timeutil_sync_instant *ref_a, const struct timeutil_sync_instant *ref_b, int64_t *epoch_shift)
Determine the epoch time shift due to moving from ref_a to ref_b.
static uint16_t epoch_time_milliseconds(uint64_t epoch_time)
Extracts epoch milliseconds from a complete epoch time.
Definition epoch.h:149
static uint32_t unix_time_from_epoch(uint64_t epoch_time)
Convert epoch time to current unix time.
Definition epoch.h:192
int epoch_time_set_reference(enum epoch_time_source source, struct timeutil_sync_instant *reference)
Set the local to epoch time reference instant.
void epoch_time_register_callback(struct epoch_time_cb *cb)
Register to be notified of epoch time events.
uint32_t epoch_time_reference_age(void)
Query how many seconds ago the reference instant was set.
#define INFUSE_EPOCH_TIME_GPS_UNIX_OFFSET_SECONDS_LEAP
Current offset between GPS and UNIX timestamps.
Definition epoch.h:50
#define INFUSE_EPOCH_TIME_GPS_UNIX_OFFSET_SECONDS_BASE
Unix time at the instant of the GPS epoch.
Definition epoch.h:55
#define SECONDS_PER_WEEK
Definition epoch.h:43
enum epoch_time_source epoch_time_get_source(void)
Get the current source of epoch time knowledge.
uint64_t ticks_from_epoch_time(uint64_t epoch_time)
Get a tick count associated with a epoch time.
static uint64_t epoch_time_seconds(uint64_t epoch_time)
Extracts epoch seconds from a complete epoch time.
Definition epoch.h:127
static uint64_t epoch_time_from_gps(uint16_t week, uint32_t week_seconds, uint16_t subseconds)
Convert GPS time format to complete epoch time.
Definition epoch.h:174
static uint64_t epoch_time_now(void)
Get the current epoch time.
Definition epoch.h:250
static uint64_t epoch_time_from_unix(uint32_t unix_time, uint16_t subseconds)
Convert unix time to current epoch time.
Definition epoch.h:209
@ TIME_SOURCE_RECOVERED
Definition epoch.h:75
@ TIME_SOURCE_INVALID
Definition epoch.h:73
@ TIME_SOURCE_RPC
Definition epoch.h:71
@ TIME_SOURCE_NONE
Definition epoch.h:65
@ TIME_SOURCE_GNSS
Definition epoch.h:67
@ TIME_SOURCE_NTP
Definition epoch.h:69
epoch time event callback structure.
Definition epoch.h:79
void(* reference_time_updated)(enum epoch_time_source source, struct timeutil_sync_instant old, struct timeutil_sync_instant new, void *user_ctx)
The local reference instant has been updated.
Definition epoch.h:87
sys_snode_t node
Definition epoch.h:94
void * user_ctx
Definition epoch.h:92