DeterministicESPAsyncWebServer v6.28.0
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
southbound.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 southbound.cpp
6 * @brief Southbound protocol-driver framework registry + dispatch (see southbound.h).
7 */
8
10
11#if DETWS_ENABLE_SOUTHBOUND
12
13#include <string.h>
14
15#ifndef DETWS_SOUTHBOUND_MAX_DRIVERS
16#define DETWS_SOUTHBOUND_MAX_DRIVERS 8 ///< bounded registry; no heap.
17#endif
18
19namespace
20{
21// All southbound-registry state, owned by one instance (internal linkage): the bounded
22// driver table and its count, grouped so it is one named owner, unreachable cross-TU.
23struct SouthboundCtx
24{
25 const SouthboundDriver *drivers[DETWS_SOUTHBOUND_MAX_DRIVERS] = {};
26 size_t count = 0;
27};
28SouthboundCtx s_sb;
29} // namespace
30
31int detws_southbound_register(const SouthboundDriver *drv)
32{
33 if (!drv || !drv->name)
34 return Sb::SB_ERR_ARG;
35 if (detws_southbound_find(drv->name))
36 return Sb::SB_ERR_DUP;
37 if (s_sb.count >= DETWS_SOUTHBOUND_MAX_DRIVERS)
38 return Sb::SB_ERR_FULL;
39 s_sb.drivers[s_sb.count++] = drv;
40 return Sb::SB_OK;
41}
42
43void detws_southbound_clear(void)
44{
45 for (size_t i = 0; i < DETWS_SOUTHBOUND_MAX_DRIVERS; i++)
46 s_sb.drivers[i] = nullptr;
47 s_sb.count = 0;
48}
49
50size_t detws_southbound_count(void)
51{
52 return s_sb.count;
53}
54
55const SouthboundDriver *detws_southbound_find(const char *name)
56{
57 if (!name)
58 return nullptr;
59 for (size_t i = 0; i < s_sb.count; i++)
60 if (s_sb.drivers[i] && s_sb.drivers[i]->name && strcmp(s_sb.drivers[i]->name, name) == 0)
61 return s_sb.drivers[i];
62 return nullptr;
63}
64
65int detws_southbound_read(const char *name, uint32_t point, int32_t *value_out)
66{
67 if (!value_out)
68 return Sb::SB_ERR_ARG;
69 const SouthboundDriver *d = detws_southbound_find(name);
70 if (!d)
71 return Sb::SB_ERR_NOT_FOUND;
72 if (!d->read)
73 return Sb::SB_ERR_UNSUPPORTED;
74 return d->read(d->ctx, point, value_out);
75}
76
77int detws_southbound_write(const char *name, uint32_t point, int32_t value)
78{
79 const SouthboundDriver *d = detws_southbound_find(name);
80 if (!d)
81 return Sb::SB_ERR_NOT_FOUND;
82 if (!d->write)
83 return Sb::SB_ERR_UNSUPPORTED;
84 return d->write(d->ctx, point, value);
85}
86
87int detws_southbound_read_block(const char *name, uint32_t first, int32_t *out, size_t n)
88{
89 if (!out || n == 0)
90 return Sb::SB_ERR_ARG;
91 const SouthboundDriver *d = detws_southbound_find(name);
92 if (!d)
93 return Sb::SB_ERR_NOT_FOUND;
94 if (!d->read_block)
95 return Sb::SB_ERR_UNSUPPORTED;
96 return d->read_block(d->ctx, first, out, n);
97}
98
99int detws_southbound_write_block(const char *name, uint32_t first, const int32_t *in, size_t n)
100{
101 if (!in || n == 0)
102 return Sb::SB_ERR_ARG;
103 const SouthboundDriver *d = detws_southbound_find(name);
104 if (!d)
105 return Sb::SB_ERR_NOT_FOUND;
106 if (!d->write_block)
107 return Sb::SB_ERR_UNSUPPORTED;
108 return d->write_block(d->ctx, first, in, n);
109}
110
111#endif // DETWS_ENABLE_SOUTHBOUND
Southbound protocol-driver framework (DETWS_ENABLE_SOUTHBOUND).