DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
ntp_service.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 ntp_service.h
6 * @brief Optional SNTP wall-clock time sync (DETWS_ENABLE_NTP).
7 *
8 * Thin wrapper over the ESP-IDF SNTP client (`configTzTime`). Gives a
9 * clock-less device wall-clock time for log timestamps, the HTTP `Date` header,
10 * and future TLS certificate-validity checks. No-op stub when DETWS_ENABLE_NTP
11 * is 0 or on non-Arduino builds.
12 *
13 * @author Douglas Quigg (dstroy0)
14 * @date 2026
15 */
16
17#ifndef DETERMINISTICESPASYNCWEBSERVER_NTP_SERVICE_H
18#define DETERMINISTICESPASYNCWEBSERVER_NTP_SERVICE_H
19
20#include "ServerConfig.h"
21#include <stddef.h>
22#include <stdint.h>
23#include <time.h>
24
25/**
26 * @brief Start the SNTP client.
27 *
28 * Returns immediately; the first sync arrives asynchronously (poll
29 * detws_ntp_synced()). Call once after the WiFi link is up.
30 *
31 * @param tz POSIX TZ string (e.g. "UTC0", "EST5EDT,M3.2.0,M11.1.0").
32 * Pass nullptr for UTC.
33 * @param server1 Primary NTP server (default "pool.ntp.org").
34 * @param server2 Secondary NTP server (default "time.nist.gov").
35 * @return true if the client was started; false if disabled at compile time.
36 */
37bool detws_ntp_begin(const char *tz = nullptr, const char *server1 = "pool.ntp.org",
38 const char *server2 = "time.nist.gov");
39
40/**
41 * @brief True once a plausible wall-clock time has been obtained from SNTP.
42 *
43 * Checks that the system clock has advanced past 2021-01-01, which only happens
44 * after a successful sync (the RTC starts at the epoch on a cold boot).
45 */
46bool detws_ntp_synced();
47
48/**
49 * @brief Current Unix epoch seconds, or 0 if not yet synced (or disabled).
50 */
51time_t detws_ntp_epoch();
52
53/**
54 * @brief Format the current time as an RFC 7231 IMF-fixdate (HTTP `Date`).
55 *
56 * Writes e.g. "Sun, 06 Nov 1994 08:49:37 GMT" into @p out. Always GMT.
57 *
58 * @param out Destination buffer (>= 30 bytes recommended).
59 * @param out_cap Capacity of @p out.
60 * @return Number of characters written (excluding the null), or 0 if time is
61 * not yet available / disabled.
62 */
63size_t detws_ntp_http_date(char *out, size_t out_cap);
64
65/**
66 * @brief NTP as a time source for the multi-source registry (services/time_source).
67 *
68 * Register with detws_time_source_add("ntp", priority, ntp_time_source) so the aggregated
69 * detws_time_now() (and thus the HTTP `Date` header when DETWS_ENABLE_TIME_SOURCE is set) can
70 * be fed by NTP alongside an RTC / GPS. Returns the current epoch, or 0 when not synced.
71 */
72uint32_t ntp_time_source(void);
73
74#if !defined(ARDUINO)
75/**
76 * @brief Host-only test seam: inject a wall-clock epoch so time-dependent paths
77 * (e.g. the optional HTTP Date header) are exercisable off-device. 0 = none.
78 */
79void detws_ntp_set_test_epoch(time_t epoch);
80#endif
81
82#endif // DETERMINISTICESPASYNCWEBSERVER_NTP_SERVICE_H
User-facing configuration for DeterministicESPAsyncWebServer.
void detws_ntp_set_test_epoch(time_t epoch)
time_t detws_ntp_epoch()
Current Unix epoch seconds, or 0 if not yet synced (or disabled).
uint32_t ntp_time_source(void)
NTP as a time source for the multi-source registry (services/time_source).
bool detws_ntp_synced()
True once a plausible wall-clock time has been obtained from SNTP.
bool detws_ntp_begin(const char *tz=nullptr, const char *server1="pool.ntp.org", const char *server2="time.nist.gov")
Start the SNTP client.
size_t detws_ntp_http_date(char *out, size_t out_cap)
Format the current time as an RFC 7231 IMF-fixdate (HTTP Date).