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
15
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
39
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 /* Time from observed ePacket headers */
74 /* Unknown time source value */
76 /* Time has been preserved across a reboot */
78} __packed;
79
90 struct timeutil_sync_instant old,
91 struct timeutil_sync_instant new, void *user_ctx);
92
93 /* User provided context pointer */
94 void *user_ctx;
95
96 sys_snode_t node;
97};
98
105
113{
114 return source & ~TIME_SOURCE_RECOVERED;
115}
116
125static inline bool epoch_time_trusted_source(enum epoch_time_source source, bool recovered_ok)
126{
127 enum epoch_time_source base = epoch_time_source_base(source);
128 bool recovered = !!(source & TIME_SOURCE_RECOVERED);
129 bool base_good = IN_RANGE(base, TIME_SOURCE_NONE + 1, TIME_SOURCE_INVALID - 1);
130
131 return base_good && (!recovered || recovered_ok);
132}
133
144static inline bool epoch_time_precise_source(enum epoch_time_source source)
145{
146 enum epoch_time_source base = epoch_time_source_base(source);
147
148 /* Delays associated with RPC and ePacket methods are not controllable
149 * by the device.
150 */
151 return (base == TIME_SOURCE_GNSS) || (base == TIME_SOURCE_NTP);
152}
153
160static inline uint64_t epoch_time_seconds(uint64_t epoch_time)
161{
162 return epoch_time >> 16;
163}
164
171static inline uint16_t epoch_time_subseconds(uint64_t epoch_time)
172{
173 return epoch_time & 0xFFFF;
174}
175
182static inline uint16_t epoch_time_milliseconds(uint64_t epoch_time)
183{
185}
186
194static inline uint64_t epoch_time_from(uint64_t seconds, uint16_t subseconds)
195{
196 return (seconds << 16) | subseconds;
197}
198
207static inline uint64_t epoch_time_from_gps(uint16_t week, uint32_t week_seconds,
208 uint16_t subseconds)
209{
210 uint64_t seconds = (SECONDS_PER_WEEK * (uint64_t)week) + week_seconds;
211
212 return epoch_time_from(seconds, subseconds);
213}
214
225static inline uint32_t unix_time_from_epoch(uint64_t epoch_time)
226{
229}
230
242static inline uint64_t epoch_time_from_unix(uint32_t unix_time, uint16_t subseconds)
243{
244 uint64_t epoch_seconds = unix_time - INFUSE_EPOCH_TIME_GPS_UNIX_OFFSET_SECONDS_BASE +
246
247 return epoch_time_from(epoch_seconds, subseconds);
248}
249
257uint64_t ticks_from_epoch_time(uint64_t epoch_time);
258
266uint64_t epoch_time_from_ticks(uint64_t ticks);
267
276uint32_t epoch_period_from_array_ticks(uint64_t array_ticks, uint16_t array_num);
277
283static inline uint64_t epoch_time_now(void)
284{
285 return epoch_time_from_ticks(k_uptime_ticks());
286}
287
297void epoch_time_unix_calendar(uint64_t epoch_time, struct tm *calendar);
298
305
316 struct timeutil_sync_instant *reference);
317
325
336int epoch_time_reference_shift(const struct timeutil_sync_instant *ref_a,
337 const struct timeutil_sync_instant *ref_b, int64_t *epoch_shift);
338
339#ifdef CONFIG_ZTEST
340
344void epoch_time_reset(void);
345
346#endif /* CONFIG_ZTEST */
347
351
352#ifdef __cplusplus
353}
354#endif
355
356#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:125
static uint64_t epoch_time_from(uint64_t seconds, uint16_t subseconds)
Convert seconds and subseconds to complete epoch time.
Definition epoch.h:194
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:171
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:182
static enum epoch_time_source epoch_time_source_base(enum epoch_time_source source)
Extract the base time source (no flags) from a time source.
Definition epoch.h:112
static bool epoch_time_precise_source(enum epoch_time_source source)
Determine whether a given time source is considered 'precise'.
Definition epoch.h:144
static uint32_t unix_time_from_epoch(uint64_t epoch_time)
Convert epoch time to current unix time.
Definition epoch.h:225
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:160
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:207
static uint64_t epoch_time_now(void)
Get the current epoch time.
Definition epoch.h:283
static uint64_t epoch_time_from_unix(uint32_t unix_time, uint16_t subseconds)
Convert unix time to current epoch time.
Definition epoch.h:242
@ TIME_SOURCE_RECOVERED
Definition epoch.h:77
@ TIME_SOURCE_INVALID
Definition epoch.h:75
@ TIME_SOURCE_RPC
Definition epoch.h:71
@ TIME_SOURCE_NONE
Definition epoch.h:65
@ TIME_SOURCE_EPACKET
Definition epoch.h:73
@ TIME_SOURCE_GNSS
Definition epoch.h:67
@ TIME_SOURCE_NTP
Definition epoch.h:69
epoch time event callback structure.
Definition epoch.h:81
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:89
sys_snode_t node
Definition epoch.h:96
void * user_ctx
Definition epoch.h:94