ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
time_source.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file time_source.h
6 * @brief Multi-source time fallback matrix (PROTOCORE_ENABLE_TIME_SOURCE).
7 *
8 * A small, zero-heap registry of user-defined time sources (NTP, an RTC, GPS, a
9 * manually-set clock, ...). Each source is a callback returning the current Unix
10 * epoch seconds, or 0 when that source currently has no valid time. Sources are
11 * registered with a priority; protocore_time_now() queries them in ascending priority
12 * order and returns the first nonzero result, so the device falls back
13 * automatically when its preferred clock is unavailable (e.g. GPS loses its fix
14 * -> RTC -> NTP). The validity rule lives in each user callback (return 0 when
15 * invalid/stale), keeping this layer a pure prioritizer.
16 *
17 * Everything lives in a fixed BSS table (PROTOCORE_TIME_SOURCE_MAX entries); no heap.
18 * The whole core is host-testable. The API is declared unconditionally and
19 * compiles to no-op stubs when PROTOCORE_ENABLE_TIME_SOURCE is 0.
20 *
21 * @author Douglas Quigg (dstroy0)
22 * @date 2026
23 */
24
25#ifndef PROTOCORE_TIME_SOURCE_H
26#define PROTOCORE_TIME_SOURCE_H
27
28#include "protocore_config.h"
29
30/**
31 * @brief A time source: returns the current Unix epoch seconds for this source,
32 * or 0 if it currently has no valid time.
33 *
34 * Sits outside the feature gate: the disabled build still defines the no-op registry entry points,
35 * so it has to be able to spell their argument type.
36 */
37typedef uint32_t (*TimeSourceFn)(void);
38
39#if PROTOCORE_ENABLE_TIME_SOURCE
40
42
43/**
44 * @brief Register a time source.
45 *
46 * @param name stable label (referenced by pointer; must outlive the source -
47 * point it at a string literal / static, like the rest of the lib).
48 * @param priority lower value = higher priority (queried first).
49 * @param fn the source callback.
50 * @return true if registered; false if @p fn is null or the table is full.
51 */
52proto_bool protocore_time_source_add(const char *name, uint8_t priority, TimeSourceFn fn);
53
54/**
55 * @brief Current best time.
56 *
57 * Queries registered sources in ascending priority and returns the first nonzero
58 * epoch (stopping at the first valid source). Returns 0 if none have valid time.
59 */
60uint32_t protocore_time_now(void);
61
62/** @brief Name of the source that satisfied the last protocore_time_now(), or nullptr. */
63const char *protocore_time_source_active(void);
64
65/** @brief Clear all registered sources. */
66void protocore_time_source_reset(void);
67
69
70#endif // PROTOCORE_ENABLE_TIME_SOURCE
71
72#endif // PROTOCORE_TIME_SOURCE_H
uint32_t(* TimeSourceFn)(void)
A time source: returns the current Unix epoch seconds for this source, or 0 if it currently has no va...
Definition time_source.h:37
#define PROTOCORE_BEGIN_DECLS
Give a header's declarations C linkage, so their symbol names carry no parameter types.
Definition types.h:96
_Bool proto_bool
The truth value.
Definition types.h:64
#define PROTOCORE_END_DECLS
Definition types.h:97