ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
http_clock.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 http_clock.h
6 * @brief Which clock the HTTP `Date` header reads, and the IMF-fixdate it renders.
7 *
8 * One decision in one place: a build with the multi-source time registry takes the epoch from
9 * protocore_time_now(), a build with only the NTP client takes it from that, and a build with
10 * neither has no wall clock and emits no `Date` at all. ::HttpDate does the rendering; this module
11 * says what instant to render and holds the result until the caller has copied it.
12 *
13 * It exists because that decision used to live in two escape hatches - `protocore_time_http_date`
14 * in time_source and `protocore_ntp_http_date` in ntp_service - each defined OUTSIDE its own
15 * module's enable gate so response.c could call it either way. That is what kept both modules in
16 * every binary: gen_modules.py reads a module's gate off its own source, and a file with code
17 * outside its gate has no gate to read, so CMake adds it to every target and its symbols are
18 * reachable from translation units that never asked for a clock. With the sometimes-used part
19 * isolated here, behind one consumer, both clocks are gated for real.
20 *
21 * @author Douglas Quigg (dstroy0)
22 * @date 2026
23 */
24
25#ifndef PROTOCORE_HTTP_CLOCK_H
26#define PROTOCORE_HTTP_CLOCK_H
27
28#include "protocore_config.h" // the entry point: protocore_types.h for the widths
29
30#if PROTOCORE_ENABLE_HTTP_CLOCK
31
33
34/**
35 * @brief The operands and the outcome.
36 *
37 * No args record: the instant is whatever the build's clock reads now, and the destination is a
38 * region of the borrow rather than a buffer the caller states.
39 *
40 * @var HttpClockVars::n characters written, or 0 when no source has valid time yet
41 * @var HttpClockVars::imf the rendered date, inside the borrow the call was handed
42 */
43typedef struct
44{
45 uint8_t n;
46 const char *imf;
47} HttpClockVars;
48
49extern HttpClockVars HttpClockV;
50
51/**
52 * @brief The `Date` header's instant, rendered.
53 *
54 * @var HttpClockNs::n characters written, or 0 when no source has valid time yet
55 * @var HttpClockNs::imf the rendered date, inside the borrow the call was handed
56 * @var HttpClockNs::date render the current instant from whichever clock this build compiled in.
57 * Writes nothing and reports 0 before any source has valid time, which is
58 * the correct answer for a clock-less boot: RFC 7231 sec 7.1.1.2 has a
59 * `Date` omitted rather than guessed.
60 */
61typedef struct
62{
63 uint8_t n;
64 const char *imf;
65 void (*const date)(uint8_t *work);
66} HttpClockNs;
67
68void protocore_http_clock_date(uint8_t *work);
69
70/**
71 * @brief The bytes this module runs out of, for a caller that holds no borrow of its own.
72 *
73 * response.c is the one consumer and is built on file-scope state, so no borrow is in scope
74 * anywhere along the response path. It forwards this span and the entry carves its region out of
75 * it at ::PROTOCORE_HTTP_CLOCK_BORROW's fixed offset.
76 */
77uint8_t *protocore_http_clock_span(void);
78
79// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
80// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
81// `HttpClock.date(work)` resolves to a named function and becomes a DIRECT call. An extern table
82// leaves the call indirect and the symbol live at every level, -O2 -flto included.
83static const HttpClockNs HttpClock __attribute__((unused)) = {
84 .date = protocore_http_clock_date,
85};
86
88
89#endif // PROTOCORE_ENABLE_HTTP_CLOCK
90
91#endif // PROTOCORE_HTTP_CLOCK_H
#define PROTOCORE_BEGIN_DECLS
Give a header's declarations C linkage, so their symbol names carry no parameter types.
Definition types.h:96
#define PROTOCORE_END_DECLS
Definition types.h:97