3#include <LwipEthernet.h>
8#include <pico/async_context.h>
9#include <pico/cyw43_arch.h>
41 bool begin(
const char* ssid,
const char* password)
override {
49 WiFi.beginNoBlock(ssid, password);
51 WiFi.beginNoBlock(ssid);
53 timer_.user_data =
this;
54 timer_.do_work = &Rp2040WifiFrameLink::maintainCb;
55 if (!async_context_add_at_time_worker_in_ms(__getEthernetContext(), &timer_, 500)) {
56 TWD_LOGGER.error(
"WiFi: could not add the worker to the async context");
65 bool sendFrame(
const uint8_t* frame, uint16_t len)
override {
66 if (!connected_)
return false;
67 return cyw43_send_ethernet(&cyw43_state, CYW43_ITF_STA, len, frame,
false) == 0;
75 WiFi.macAddress(mac_);
84 bool macValid_ =
false;
85 volatile bool connected_ =
false;
86 async_at_time_worker_t timer_ = {};
88 uint32_t downSince_ = 0;
89 uint32_t reportedAt_ = 0;
91 static Rp2040WifiFrameLink*& instance() {
92 static Rp2040WifiFrameLink* p =
nullptr;
97 static err_t inputHook(
struct pbuf* p,
struct netif*) {
98 static uint8_t buffer[1600];
99 Rp2040WifiFrameLink* s = instance();
100 if (s && s->receiver_ && p->tot_len <=
sizeof(buffer)) {
101 pbuf_copy_partial(p, buffer, p->tot_len, 0);
102 s->receiver_->onFrame(buffer, p->tot_len);
108 static void maintainCb(async_context_t* ctx, async_at_time_worker_t* w) {
109 ((Rp2040WifiFrameLink*)w->user_data)->maintain();
110 w->next_time = make_timeout_time_ms(500);
114 static const char* linkStatus() {
115 switch (cyw43_wifi_link_status(&cyw43_state, CYW43_ITF_STA)) {
116 case CYW43_LINK_JOIN:
return "joined, link not up";
117 case CYW43_LINK_FAIL:
return "connection failed";
118 case CYW43_LINK_NONET:
return "network not found";
119 case CYW43_LINK_BADAUTH:
return "wrong password";
120 default:
return "joining";
127 if (connected_)
TWD_LOGGER.
warning(
"WiFi disconnected: the station interface is gone");
131 if (n->input != &Rp2040WifiFrameLink::inputHook) {
132 n->input = &Rp2040WifiFrameLink::inputHook;
135 if (dhcp_supplied_address(n) || netif_dhcp_data(n)) dhcp_stop(n);
136 bool up = netif_is_link_up(n);
137 if (up && !connected_)
TWD_LOGGER.
info(
"WiFi associated, forwarding raw frames");
139 uint32_t now = millis();
142 }
else if (!downSince_) {
143 downSince_ = reportedAt_ = now | 1;
145 TWD_LOGGER.
warning(
"WiFi: not associated with '%s' for %u s (%s)", ssid_.c_str(),
146 (
unsigned)((now - downSince_) / 1000), linkStatus());
Portable abstractions the dongle is built from.
static twd::LoggerClass TWD_LOGGER
The logger used by all components; configure it in the sketch with TWD_LOGGER.begin(Serial).
struct netif * __getCYW43Netif()
Raw Ethernet access to the uplink network (bridge mode).
Consumer of Ethernet frames.
void warning(const char *fmt,...)
void info(const char *fmt,...)
Raw Ethernet access to the WiFi network through the CYW43 chip of the Pico W / Pico 2 W.
static constexpr uint32_t kNotJoinedReportMs
While the station is not associated, a warning is logged this often.
void setFrameReceiver(FrameReceiver *receiver) override
Frames coming from the uplink network are passed to receiver.
const uint8_t * macAddress() override
The station MAC of the chip; starts the chip on the first call. The result is cached.
bool sendFrame(const uint8_t *frame, uint16_t len) override
Sends a frame to the uplink network; false if it was dropped.
bool begin(const char *ssid, const char *password) override
Start joining the network; returns at once, see connected().
bool connected() override
True while the station's link is up.
The connection to the WiFi network the traffic is bridged to.