DeterministicESPAsyncWebServer v6.28.0
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
atc.cpp
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 atc.cpp
6 * @brief ATC field-I/O interop snapshot (see atc.h).
7 */
8
9#include "services/atc/atc.h"
10
11#if DETWS_ENABLE_ATC
12
13#include <string.h>
14
16
17namespace
18{
19void put_json_str(DetSb *b, const char *s)
20{
21 det_sb_put(b, "\"");
22 for (const char *p = s ? s : ""; *p; p++)
23 {
24 if (*p == '"' || *p == '\\')
25 {
26 char esc[3] = {'\\', *p, '\0'};
27 det_sb_put(b, esc);
28 }
29 else
30 {
31 if (b->len + 1 >= b->cap)
32 {
33 b->ok = false;
34 return;
35 }
36 b->p[b->len++] = *p;
37 }
38 }
39 det_sb_put(b, "\"");
40}
41
42void put_u8(DetSb *b, uint8_t v)
43{
44 char t[4];
45 int n = 0;
46 do
47 {
48 t[n++] = (char)('0' + v % 10);
49 v /= 10;
50 } while (v);
51 char o[4];
52 for (int i = 0; i < n; i++)
53 o[i] = t[n - 1 - i];
54 o[n] = '\0';
55 det_sb_put(b, o);
56}
57
58// Append the points of one direction (outputs or inputs) as a JSON array.
59void put_array(DetSb *b, const AtcFieldIo *io, bool outputs)
60{
61 det_sb_put(b, "[");
62 bool first = true;
63 for (size_t i = 0; i < io->count; i++)
64 {
65 if (io->points[i].is_output != outputs)
66 continue;
67 if (!first)
68 det_sb_put(b, ",");
69 first = false;
70 det_sb_put(b, "{\"name\":");
71 put_json_str(b, io->points[i].name);
72 det_sb_put(b, ",\"value\":");
73 put_u8(b, io->points[i].value);
74 det_sb_put(b, "}");
75 }
76 det_sb_put(b, "]");
77}
78} // namespace
79
80size_t detws_atc_snapshot_json(const AtcFieldIo *io, char *out, size_t cap)
81{
82 if (!io || !out || (io->count && !io->points))
83 return 0;
84 DetSb b = {out, cap, 0, cap > 0};
85 det_sb_put(&b, "{\"inputs\":");
86 put_array(&b, io, false);
87 det_sb_put(&b, ",\"outputs\":");
88 put_array(&b, io, true);
89 det_sb_put(&b, "}");
90 if (!b.ok)
91 return 0;
92 out[b.len] = '\0';
93 return b.len;
94}
95
96bool detws_atc_set_output(AtcFieldIo *io, const char *name, uint8_t value)
97{
98 if (!io || !name || !io->points)
99 return false;
100 for (size_t i = 0; i < io->count; i++)
101 if (io->points[i].is_output && io->points[i].name && strcmp(io->points[i].name, name) == 0)
102 {
103 io->points[i].value = value;
104 return true;
105 }
106 return false;
107}
108
109uint8_t detws_atc_get(const AtcFieldIo *io, const char *name, bool *found)
110{
111 if (found)
112 *found = false;
113 if (!io || !name || !io->points)
114 return 0;
115 for (size_t i = 0; i < io->count; i++)
116 if (io->points[i].name && strcmp(io->points[i].name, name) == 0)
117 {
118 if (found)
119 *found = true;
120 return io->points[i].value;
121 }
122 return 0;
123}
124
125#endif // DETWS_ENABLE_ATC
ATC (Advanced Traffic Controller) field-I/O interop snapshot (DETWS_ENABLE_ATC).
Bounded no-heap string builder that fails closed on overflow (one shared copy).
void det_sb_put(DetSb *b, const char *s)
Append NUL-terminated s; leaves the buffer untouched and clears ok if it would not fit.
Definition strbuf.h:36
Bump-append target; ok latches false once an append would overflow cap.
Definition strbuf.h:28
size_t len
Definition strbuf.h:31
char * p
Definition strbuf.h:29
bool ok
Definition strbuf.h:32
size_t cap
Definition strbuf.h:30