DeterministicESPAsyncWebServer v6.28.0
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
link_manager.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 link_manager.cpp
6 * @brief Multi-interface egress selection + graceful escalation/failover (see link_manager.h).
7 */
8
10
11#if DETWS_ENABLE_LINK_MANAGER
12
13int detws_link_select(const LinkManager *m)
14{
15 if (!m || !m->ifaces)
16 return -1;
17 int best = -1;
18 for (size_t i = 0; i < m->n; i++)
19 {
20 if (!m->ifaces[i].up)
21 continue;
22 // Higher priority wins; the lower index breaks a tie (best is the first at that priority).
23 if (best < 0 || m->ifaces[i].priority > m->ifaces[best].priority)
24 best = (int)i;
25 }
26 return best;
27}
28
29void detws_link_init(LinkManager *m, LinkIface *ifaces, size_t n)
30{
31 if (!m)
32 return;
33 m->ifaces = ifaces;
34 m->n = ifaces ? n : 0;
35 m->active = detws_link_select(m);
36}
37
38int detws_link_active(const LinkManager *m)
39{
40 return m ? m->active : -1;
41}
42
43bool detws_link_set(LinkManager *m, size_t idx, bool up, int *from, int *to)
44{
45 if (!m || !m->ifaces || idx >= m->n)
46 {
47 if (from)
48 *from = m ? m->active : -1;
49 if (to)
50 *to = m ? m->active : -1;
51 return false;
52 }
53 int prev = m->active;
54 m->ifaces[idx].up = up;
55 m->active = detws_link_select(m);
56 if (from)
57 *from = prev;
58 if (to)
59 *to = m->active;
60 return m->active != prev;
61}
62
63#endif // DETWS_ENABLE_LINK_MANAGER