Layer: L7 Application ยท Build flags: none
What this example teaches
A device with both WiFi and Ethernet has a "default route" the OS picks for outbound traffic, and it can flip on a cable pull. Physical.link->egress() reports the live egress interface and Physical.link->egress_ip() its IP, queried on demand - no manager and no polling loop. The OS (esp_netif) does the failover; this just reports the current state, useful for logging, telemetry tags, or an "online via
Ethernet / WiFi" badge in a UI.
Query on demand:
uint32_t ip = Physical.link->egress_ip();
enum PROTO_ENUM_PACKED protocore_if_kind
What an interface is, and the filter that selects one.
The handler maps the enum to a name and formats the IP as JSON. Wire an Ethernet PHY alongside WiFi and the reported interface flips when you pull the cable.
Build and run
pio ci --board=esp32dev --project-option="framework=arduino" \
--lib="." examples/L7-Application/NetEgress/NetEgress.ino
curl http://<ip>/net # {"egress":"wifi-sta","ip":"192.168.1.42"}
Annotated source
The complete sketch (NetEgress.ino), reproduced verbatim with added explanatory comments:
#include "protocore.h"
static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";
PC server;
{
switch (i)
{
case protocore_if_kind::PROTOCORE_IF_ETH:
return "ethernet";
case protocore_if_kind::PROTOCORE_IF_WIFI_AP:
return "softap";
case protocore_if_kind::PROTOCORE_IF_WIFI_STA:
return "wifi-sta";
default:
return "none";
}
}
void setup()
{
Serial.begin(115200);
Physical.wifi->init(SSID, PASSWORD);
Serial.print("Connecting to WiFi");
while (!Physical.wifi->ready())
{
delay(250);
Serial.print('.');
}
uint32_t ip = Physical.link->egress_ip();
Serial.printf("IP: %u.%u.%u.%u\n", (unsigned)(ip & 0xFF), (unsigned)((ip >> 8) & 0xFF),
(unsigned)((ip >> 16) & 0xFF), (unsigned)((ip >> 24) & 0xFF));
Serial.printf("egress interface: %s\n", iface_name(Physical.link->egress()));
uint32_t ip = Physical.link->egress_ip();
char body[96];
snprintf(body, sizeof(body), "{\"egress\":\"%s\",\"ip\":\"%u.%u.%u.%u\"}", iface_name(Physical.link->egress()),
(unsigned)(ip & 0xFF), (unsigned)((ip >> 8) & 0xFF), (unsigned)((ip >> 16) & 0xFF),
(unsigned)((ip >> 24) & 0xFF));
server.send(id, 200, "application/json", body);
});
server.begin(80);
}
void loop()
{
server.handle();
}
@ HTTP_GET
Safe, idempotent read.
Layer 1 (Physical) - link bring-up, the interface registry, and live egress reporting.