ProtoCore v0.0.2
Deterministic, zero-heap network stack for embedded targets
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 PC_ENABLE_LINK_MANAGER
12
13int pc_link_select(const LinkManager *m)
14{
15 if (!m || !m->ifaces)
16 {
17 return -1;
18 }
19 int best = -1;
20 for (size_t i = 0; i < m->n; i++)
21 {
22 if (!m->ifaces[i].up)
23 {
24 continue;
25 }
26 // Higher priority wins; the lower index breaks a tie (best is the first at that priority).
27 if (best < 0 || m->ifaces[i].priority > m->ifaces[best].priority)
28 {
29 best = (int)i;
30 }
31 }
32 return best;
33}
34
35void pc_link_init(LinkManager *m, LinkIface *ifaces, size_t n)
36{
37 if (!m)
38 {
39 return;
40 }
41 m->ifaces = ifaces;
42 m->n = ifaces ? n : 0;
43 m->active = pc_link_select(m);
44}
45
46int pc_link_active(const LinkManager *m)
47{
48 return m ? m->active : -1;
49}
50
51bool pc_link_set(LinkManager *m, size_t idx, bool up, int *from, int *to)
52{
53 if (!m || !m->ifaces || idx >= m->n)
54 {
55 if (from)
56 {
57 *from = m ? m->active : -1;
58 }
59 if (to)
60 {
61 *to = m ? m->active : -1;
62 }
63 return false;
64 }
65 int prev = m->active;
66 m->ifaces[idx].up = up;
67 m->active = pc_link_select(m);
68 if (from)
69 {
70 *from = prev;
71 }
72 if (to)
73 {
74 *to = m->active;
75 }
76 return m->active != prev;
77}
78
79#endif // PC_ENABLE_LINK_MANAGER