InterfaceFilter - per-interface (STA / softAP) routes
Layer: L7 Application ยท Build flags: none (core features only)
What this example teaches
A device running both a station link (your LAN) and a softAP (its own network) can expose different routes on each. This brings up AP+STA mode and gates a config page to the softAP and an app API to the station, so a first-boot setup page is never reachable from the LAN and vice versa.
Tell the server which IP is the AP. The interface is decided by comparing each connection's local IP to the softAP IP, so you must register it once after starting the AP:
Physical.wifi->init_ap(AP_SSID, AP_PASS);
...
server.set_ap_ip(Physical.wifi->ap_ip());
PhysicalVars PhysicalV
The operands and the outcome.
uint8_t * protocore_physical_span(void)
The PROTOCORE_PHYSICAL_BORROW bytes this module's state lives in.
Gate routes by interface. Pass PROTOCORE_IF_WIFI_AP or PROTOCORE_IF_WIFI_STA as the last on() argument; omit it for "any interface":
@ HTTP_GET
Safe, idempotent read.
A request for /setup arriving on the station link is treated as unmatched (404), and /api/data on the softAP likewise - so each surface is isolated to its network.
Build and run
pio ci --board=esp32dev --project-option="framework=arduino" \
--lib="." examples/L7-Application/InterfaceFilter/InterfaceFilter.ino
# from the LAN (station): curl http://<sta-ip>/api/data -> 200 ; /setup -> 404
# joined to the softAP: curl http://192.168.4.1/setup -> 200 ; /api/data -> 404
Annotated source
The complete sketch (InterfaceFilter.ino), reproduced verbatim with added explanatory comments:
#include "protocore.h"
static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";
static const char *AP_SSID = "PC-Setup";
static const char *AP_PASS = "configme123";
PC server;
void handle_setup(uint8_t slot_id,
HttpReq *req)
{
(void)req;
server.send(slot_id, 200, "text/html", "<h1>Setup</h1><p>softAP only</p>");
}
void handle_api(uint8_t slot_id,
HttpReq *req)
{
(void)req;
server.send(slot_id, 200, "application/json", "{\"data\":42,\"iface\":\"sta\"}");
}
void handle_root(uint8_t slot_id,
HttpReq *req)
{
(void)req;
server.send(slot_id, 200, "text/plain", "available on both interfaces");
}
void setup()
{
Serial.begin(115200);
Physical.wifi->init_ap(AP_SSID, AP_PASS);
Serial.print("Connecting to WiFi");
{
delay(250);
Serial.print('.');
}
Serial.print("\nSTA 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.print("AP IP: ");
Serial.println(Physical.wifi->ap_ip());
server.set_ap_ip(Physical.wifi->ap_ip());
int32_t result = server.begin(80);
if (result < 0)
{
Serial.printf("begin() failed (error %d)\n", result);
return;
}
Serial.println("Server started on port 80");
}
void loop()
{
server.handle();
}
Layer 1 (Physical) - link bring-up, the interface registry, and live egress reporting.
EMBED_INLINE uint32_t protocore_physical_egress_ipv4(void)
Physical.egress_ip as an expression: the address a sketch prints once its link is up.
EMBED_INLINE embed_bool protocore_physical_wifi_up(void)
Physical.wifi_ready as an expression, for a bring-up loop to wait on.