DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
time_source.h
Go to the documentation of this file.
1// 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 (DETWS_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; detws_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 (DETWS_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 DETWS_ENABLE_TIME_SOURCE is 0.
20 *
21 * @author Douglas Quigg (dstroy0)
22 * @date 2026
23 */
24
25#ifndef DETERMINISTICESPASYNCWEBSERVER_TIME_SOURCE_H
26#define DETERMINISTICESPASYNCWEBSERVER_TIME_SOURCE_H
27
28#include "ServerConfig.h"
29#include <stddef.h>
30#include <stdint.h>
31
32/**
33 * @brief A time source: returns the current Unix epoch seconds for this source,
34 * or 0 if it currently has no valid time.
35 */
36typedef uint32_t (*TimeSourceFn)(void);
37
38/**
39 * @brief Register a time source.
40 *
41 * @param name stable label (referenced by pointer; must outlive the source -
42 * point it at a string literal / static, like the rest of the lib).
43 * @param priority lower value = higher priority (queried first).
44 * @param fn the source callback.
45 * @return true if registered; false if @p fn is null or the table is full.
46 */
47bool detws_time_source_add(const char *name, uint8_t priority, TimeSourceFn fn);
48
49/**
50 * @brief Current best time.
51 *
52 * Queries registered sources in ascending priority and returns the first nonzero
53 * epoch (stopping at the first valid source). Returns 0 if none have valid time.
54 */
55uint32_t detws_time_now(void);
56
57/** @brief Name of the source that satisfied the last detws_time_now(), or nullptr. */
58const char *detws_time_source_active(void);
59
60/** @brief Clear all registered sources. */
62
63/**
64 * @brief The current best time (detws_time_now, any registered NTP / GPS / RTC / ... source)
65 * formatted as an RFC 7231 IMF-fixdate into @p out.
66 * @return bytes written, or 0 with an empty @p out when no source currently has a valid time.
67 *
68 * This is what lets the HTTP `Date:` header be fed by whatever time source is enabled, not just
69 * NTP: register RTC / GPS / NTP via detws_time_source_add() and the header follows the priority.
70 */
71size_t detws_time_http_date(char *out, size_t out_cap);
72
73#endif // DETERMINISTICESPASYNCWEBSERVER_TIME_SOURCE_H
User-facing configuration for DeterministicESPAsyncWebServer.
const char * detws_time_source_active(void)
Name of the source that satisfied the last detws_time_now(), or nullptr.
void detws_time_source_reset(void)
Clear all registered sources.
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:36
bool detws_time_source_add(const char *name, uint8_t priority, TimeSourceFn fn)
Register a time source.
uint32_t detws_time_now(void)
Current best time.
size_t detws_time_http_date(char *out, size_t out_cap)
The current best time (detws_time_now, any registered NTP / GPS / RTC / ... source) formatted as an R...