DeterministicESPAsyncWebServer v6.28.0
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
sep2.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 sep2.cpp
6 * @brief IEEE 2030.5 resource codec (see sep2.h).
7 */
8
10
11#if DETWS_ENABLE_SEP2
12
13#include <string.h>
14
16
17namespace
18{
19void put_i64(DetSb *b, int64_t v)
20{
21 if (!b->ok)
22 return;
23 char tmp[21];
24 int n = 0;
25 bool neg = v < 0;
26 uint64_t u = neg ? (uint64_t)(-(v + 1)) + 1 : (uint64_t)v;
27 do
28 {
29 tmp[n++] = (char)('0' + (int)(u % 10));
30 u /= 10;
31 } while (u);
32 char out[22];
33 int k = 0;
34 if (neg)
35 out[k++] = '-';
36 for (int i = 0; i < n; i++)
37 out[k++] = tmp[n - 1 - i];
38 out[k] = '\0';
39 det_sb_put(b, out);
40}
41
42const char *NS = " xmlns=\"urn:ieee:std:2030.5:ns\"";
43const char *DECL = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
44} // namespace
45
46size_t detws_sep2_device_capability(uint32_t poll_rate, const char *edev_list_href, const char *derp_list_href,
47 char *out, size_t cap)
48{
49 DetSb b = {out, cap, 0, out != nullptr && cap > 0};
50 det_sb_put(&b, DECL);
51 det_sb_put(&b, "<DeviceCapability");
52 det_sb_put(&b, NS);
53 det_sb_put(&b, " pollRate=\"");
54 put_i64(&b, poll_rate);
55 det_sb_put(&b, "\">");
56 det_sb_put(&b, "<EndDeviceListLink href=\"");
57 det_sb_xml(&b, edev_list_href);
58 det_sb_put(&b, "\"/>");
59 det_sb_put(&b, "<DERProgramListLink href=\"");
60 det_sb_xml(&b, derp_list_href);
61 det_sb_put(&b, "\"/>");
62 det_sb_put(&b, "</DeviceCapability>");
63 return det_sb_finish(&b);
64}
65
66size_t detws_sep2_end_device(uint64_t sfdi, const char *lfdi, const char *href, char *out, size_t cap)
67{
68 DetSb b = {out, cap, 0, out != nullptr && cap > 0};
69 det_sb_put(&b, DECL);
70 det_sb_put(&b, "<EndDevice");
71 det_sb_put(&b, NS);
72 det_sb_put(&b, " href=\"");
73 det_sb_xml(&b, href);
74 det_sb_put(&b, "\"><sFDI>");
75 put_i64(&b, (int64_t)sfdi);
76 det_sb_put(&b, "</sFDI><lFDI>");
77 det_sb_xml(&b, lfdi);
78 det_sb_put(&b, "</lFDI></EndDevice>");
79 return det_sb_finish(&b);
80}
81
82size_t detws_sep2_der_control(const char *mrid, uint32_t start, uint32_t duration, int32_t opmod_target_w, char *out,
83 size_t cap)
84{
85 DetSb b = {out, cap, 0, out != nullptr && cap > 0};
86 det_sb_put(&b, DECL);
87 det_sb_put(&b, "<DERControl");
88 det_sb_put(&b, NS);
89 det_sb_put(&b, "><mRID>");
90 det_sb_xml(&b, mrid);
91 det_sb_put(&b, "</mRID><interval><start>");
92 put_i64(&b, start);
93 det_sb_put(&b, "</start><duration>");
94 put_i64(&b, duration);
95 det_sb_put(&b, "</duration></interval><DERControlBase><opModFixedW>");
96 put_i64(&b, opmod_target_w);
97 det_sb_put(&b, "</opModFixedW></DERControlBase></DERControl>");
98 return det_sb_finish(&b);
99}
100
101#endif // DETWS_ENABLE_SEP2
IEEE 2030.5 (Smart Energy Profile 2.0) resource codec (DETWS_ENABLE_SEP2).
Bounded no-heap string builder that fails closed on overflow (one shared copy).
size_t det_sb_finish(DetSb *b)
NUL-terminate and return the built length, or 0 if the build overflowed.
Definition strbuf.h:90
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
void det_sb_xml(DetSb *b, const char *s)
Append s XML-escaped (& < > "); a NULL s appends nothing.
Definition strbuf.h:51
Bump-append target; ok latches false once an append would overflow cap.
Definition strbuf.h:28
bool ok
Definition strbuf.h:32