ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
InterfaceFilter Directory Reference

Detailed Description

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);
PhysicalV.wifi.ssid = SSID;
PhysicalV.wifi.password = PASSWORD;
Physical.wifi_init(protocore_physical_span());
...
server.set_ap_ip(Physical.wifi->ap_ip()); // required for STA/AP classification
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":

server.on("/setup", HttpMethod::HTTP_GET, handle_setup, protocore_if_kind::PROTOCORE_IF_WIFI_AP); // softAP only
server.on("/api/data", HttpMethod::HTTP_GET, handle_api, protocore_if_kind::PROTOCORE_IF_WIFI_STA); // station only
server.on("/", HttpMethod::HTTP_GET, handle_root); // any interface
@ HTTP_GET
Safe, idempotent read.
Definition http.h:24

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:

// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
// SPDX-License-Identifier: AGPL-3.0-or-later
#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);
// AP + STA so both interfaces exist simultaneously.
Physical.wifi->init_ap(AP_SSID, AP_PASS);
PhysicalV.wifi.ssid = SSID;
PhysicalV.wifi.password = PASSWORD;
Physical.wifi_init(protocore_physical_span());
Serial.print("Connecting to WiFi");
{
delay(250);
Serial.print('.');
}
Serial.print("\nSTA IP: ");
uint32_t ip = protocore_physical_egress_ipv4(); // library egress IP (network byte order), no Arduino WiFi
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());
// Required for STA/AP classification (IPAddress converts to uint32_t).
server.set_ap_ip(Physical.wifi->ap_ip());
server.on("/setup", HttpMethod::HTTP_GET, handle_setup, protocore_if_kind::PROTOCORE_IF_WIFI_AP); // softAP only
server.on("/api/data", HttpMethod::HTTP_GET, handle_api, protocore_if_kind::PROTOCORE_IF_WIFI_STA); // station only
server.on("/", HttpMethod::HTTP_GET, handle_root); // any interface
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.
Definition physical.h:465
EMBED_INLINE embed_bool protocore_physical_wifi_up(void)
Physical.wifi_ready as an expression, for a bring-up loop to wait on.
Definition physical.h:451