ProtoCore v0.0.1
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
physical.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 physical.cpp
6 * @brief Layer 1 (Physical) - vendor-neutral core.
7 *
8 * Holds the two things that are not silicon-specific: the pure IP-egress classifier (always compiled,
9 * host-tested), and the fallback link stubs used when the selected vendor has no physical backend yet
10 * (PC_PHYSICAL_HAS_BACKEND == 0 - host/native builds, or a vendor whose PHY driver is not written).
11 * Each vendor's real bring-up lives in board_drivers/physical/<vendor>/
12 * (board_drivers/physical/esp/physical_esp.cpp today), chosen
13 * by the PC_VENDOR_* selector. The stubs never bring a link up, so a new target builds and runs headless
14 * from day one - the L1 analogue of crypto/ falling back to its portable software field path.
15 */
16
17#include "physical.h"
18
19// Pure classifier (always compiled, host-tested): map the live egress IP to the interface it belongs to.
20pc_iface pc_net_classify_ip(uint32_t egress_ip, uint32_t sta_ip, uint32_t ap_ip)
21{
22 if (egress_ip == 0)
23 {
25 }
26 if (sta_ip != 0 && egress_ip == sta_ip)
27 {
29 }
30 if (ap_ip != 0 && egress_ip == ap_ip)
31 {
33 }
34 return pc_iface::PC_IFACE_ETH; // a live route that is neither WiFi IP -> wired
35}
36
37#if !PC_PHYSICAL_HAS_BACKEND
38// No L1 backend for the selected vendor (host/native, or a not-yet-written PHY): safe no-ops. The radio
39// bring-up calls "succeed" (nothing to do) while the link never reports ready, and every readout is empty.
40
41bool init_wifi_physical(const char *, const char *)
42{
43 return true;
44}
46{
47 return true;
48}
50{
51 return true;
52}
53bool init_wifi_ap_physical(const char *, const char *)
54{
55 return true;
56}
58{
59 return false; // no Ethernet PHY without a backend
60}
61bool eth_ready(void)
62{
63 return false;
64}
66{
67 return false; // no netif without a backend
68}
70{
71 return false;
72}
73bool pc_ipv6_ready(void)
74{
75 return false;
76}
77uint32_t pc_net_egress_ip(void)
78{
79 return 0;
80}
85uint32_t pc_net_ap_ip(void)
86{
87 return 0;
88}
89int8_t pc_net_rssi(void)
90{
91 return 0;
92}
93bool pc_net_mac(uint8_t *)
94{
95 return false;
96}
97bool pc_net_egress_mac(uint8_t *)
98{
99 return false;
100}
101// Radio control with no radio: report failure rather than pretending. A caller that asks for
102// monitor mode on a target without a radio must be able to tell, and pc_phy_ps_get() answering
103// "always on" is the truthful answer when nothing can sleep.
105{
106 return false;
107}
113{
114 return false;
115}
117{
118 return false;
119}
121{
122}
124{
125}
126size_t pc_net_ssid(char *out, size_t cap)
127{
128 if (out && cap)
129 {
130 out[0] = '\0';
131 }
132 return 0;
133}
134uint8_t pc_net_channel(void)
135{
136 return 0;
137}
138
139#endif // !PC_PHYSICAL_HAS_BACKEND
bool init_wifi_physical(const char *, const char *)
Connect to a WiFi access point.
Definition physical.cpp:41
bool pc_ipv6_ready(void)
True once the interface has a global IPv6 address (see net_global_ipv6()).
Definition physical.cpp:73
pc_iface pc_net_classify_ip(uint32_t egress_ip, uint32_t sta_ip, uint32_t ap_ip)
Classify an egress IPv4 against the WiFi station / softAP IPs (pure helper, exposed for unit testing)...
Definition physical.cpp:20
uint32_t pc_net_egress_ip(void)
IPv4 (network byte order) of the current egress interface, or 0 if none.
Definition physical.cpp:77
pc_phy_ps pc_phy_ps_get(void)
Read the active power-save mode (PC_PHY_PS_NONE when unsupported).
Definition physical.cpp:108
bool eth_ready(void)
True if the Ethernet link is up and an IP is assigned.
Definition physical.cpp:61
bool pc_phy_monitor_begin(uint8_t, pc_phy_frame_fn)
Enter monitor mode on channel, delivering frames to cb.
Definition physical.cpp:116
bool pc_net_egress_mac(uint8_t *)
Definition physical.cpp:97
bool init_wifi_ap_physical(const char *, const char *)
Bring up a softAP, enabling AP+STA coexistence so a station link can run alongside it.
Definition physical.cpp:53
bool net_global_ipv6(pc_ip *)
The interface's global (routable) IPv6 address, if it has one.
Definition physical.cpp:69
bool pc_phy_tx_power_set(int8_t)
Cap transmit power.
Definition physical.cpp:112
size_t pc_net_ssid(char *out, size_t cap)
Copy the associated SSID (null-terminated) into out.
Definition physical.cpp:126
bool wifi_ready()
True if the WiFi station link is up (associated + an IP is assigned).
Definition physical.cpp:45
int8_t pc_net_rssi(void)
Station link RSSI in dBm, or 0 if not associated (and on host builds).
Definition physical.cpp:89
void pc_phy_monitor_end(void)
Leave monitor mode.
Definition physical.cpp:123
uint32_t pc_net_ap_ip(void)
softAP IPv4 (network byte order), or 0 if the softAP is not up (and on host builds).
Definition physical.cpp:85
bool init_ipv6_physical(void)
Enable IPv6 (dual-stack) on the Wi-Fi interface (PC_ENABLE_IPV6).
Definition physical.cpp:65
bool init_eth_physical(void)
Bring up a wired Ethernet link (PC_ENABLE_ETHERNET).
Definition physical.cpp:57
pc_iface pc_net_egress(void)
Which interface currently carries outbound traffic.
Definition physical.cpp:81
void pc_phy_monitor_set_channel(uint8_t)
Retune monitor mode to channel.
Definition physical.cpp:120
uint8_t pc_net_channel(void)
Station WiFi channel (1..14), or 0 if not associated (and on host builds).
Definition physical.cpp:134
bool pc_phy_ps_set(pc_phy_ps)
Apply a power-save mode.
Definition physical.cpp:104
bool init_wifi_radio_physical(uint8_t)
Bring the WiFi radio up in station mode WITHOUT associating to an AP.
Definition physical.cpp:49
bool pc_net_mac(uint8_t *)
Definition physical.cpp:93
Layer 1 (Physical) - link bring-up and live egress-interface reporting.
void(* pc_phy_frame_fn)(const uint8_t *frame, uint16_t len, int8_t rssi, uint8_t channel)
One received frame, delivered in neutral terms.
Definition physical.h:208
pc_phy_ps
Radio power-save mode, in the library's own vocabulary.
Definition physical.h:191
@ PC_PHY_PS_NONE
Radio always on: lowest latency, highest average draw.
pc_iface
Network interface a connection arrived on (for per-route filtering).
@ PC_IFACE_AP
softAP interface (clients joined to the device).
@ PC_IFACE_ETH
Ethernet interface (wired PHY).
@ PC_IFACE_STA
Station interface (joined to an AP / your LAN).
@ PC_IFACE_ANY
Unknown / no filter (matches any interface).
A v4 or v6 address in network (big-endian) byte order.
Definition ip.h:52