TinyWifiDongle 0.1.0
Header-only USB WiFi dongle for Arduino
Loading...
Searching...
No Matches
Rp2040WifiFrameLink.h
Go to the documentation of this file.
1#pragma once
2#include <Arduino.h>
3#include <LwipEthernet.h> // __getEthernetContext()
4#include <WiFi.h>
5#include <lwip/dhcp.h>
6#include <lwip/netif.h>
7#include <lwip/pbuf.h>
8#include <pico/async_context.h>
9#include <pico/cyw43_arch.h>
10
12
13// Defined by the core's lwIP_CYW43 library: the lwIP interface of the WiFi station.
14struct netif* __getCYW43Netif();
15
16namespace twd {
17
36class Rp2040WifiFrameLink : public Uplink, public FrameLink {
37 public:
39 static constexpr uint32_t kNotJoinedReportMs = 10000;
40
41 bool begin(const char* ssid, const char* password) override {
42 instance() = this;
43 macAddress(); // starts the chip if that has not happened yet
44 // The core's begin() starts an access point when it finds the mode WIFI_AP, which a previous
45 // setup page can leave behind (WiFi.mode(WIFI_OFF) does not reset it), so ask for the station.
46 WiFi.mode(WIFI_STA);
47 ssid_ = ssid;
48 if (password) {
49 WiFi.beginNoBlock(ssid, password);
50 } else {
51 WiFi.beginNoBlock(ssid);
52 }
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");
57 return false;
58 }
59 return true;
60 }
61
63 bool connected() override { return connected_; }
64
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;
68 }
69
70 void setFrameReceiver(FrameReceiver* receiver) override { receiver_ = receiver; }
71
73 const uint8_t* macAddress() override {
74 if (!macValid_) {
75 WiFi.macAddress(mac_);
76 macValid_ = true;
77 }
78 return mac_;
79 }
80
81 private:
82 FrameReceiver* receiver_ = nullptr;
83 uint8_t mac_[6] = {};
84 bool macValid_ = false;
85 volatile bool connected_ = false;
86 async_at_time_worker_t timer_ = {};
87 String ssid_;
88 uint32_t downSince_ = 0; // millis() when the link went down, 0 = up
89 uint32_t reportedAt_ = 0; // millis() of the last "not associated" warning
90
91 static Rp2040WifiFrameLink*& instance() {
92 static Rp2040WifiFrameLink* p = nullptr;
93 return p;
94 }
95
97 static err_t inputHook(struct pbuf* p, struct netif*) {
98 static uint8_t buffer[1600]; // one context, so one buffer is enough
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);
103 }
104 pbuf_free(p);
105 return ERR_OK;
106 }
107
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);
111 (void)ctx;
112 }
113
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";
121 }
122 }
123
124 void maintain() {
125 struct netif* n = __getCYW43Netif();
126 if (!n) {
127 if (connected_) TWD_LOGGER.warning("WiFi disconnected: the station interface is gone");
128 connected_ = false;
129 return;
130 }
131 if (n->input != &Rp2040WifiFrameLink::inputHook) {
132 n->input = &Rp2040WifiFrameLink::inputHook;
133 TWD_LOGGER.info("WiFi station: frames are bridged");
134 }
135 if (dhcp_supplied_address(n) || netif_dhcp_data(n)) dhcp_stop(n); // the host asks the network, not us
136 bool up = netif_is_link_up(n);
137 if (up && !connected_) TWD_LOGGER.info("WiFi associated, forwarding raw frames");
138 if (!up && connected_) TWD_LOGGER.warning("WiFi disconnected");
139 uint32_t now = millis();
140 if (up) {
141 downSince_ = 0;
142 } else if (!downSince_) {
143 downSince_ = reportedAt_ = now | 1;
144 } else if (now - reportedAt_ >= kNotJoinedReportMs) { // the core retries silently: say why nothing happens
145 TWD_LOGGER.warning("WiFi: not associated with '%s' for %u s (%s)", ssid_.c_str(),
146 (unsigned)((now - downSince_) / 1000), linkStatus());
147 reportedAt_ = now;
148 }
149 connected_ = up;
150 }
151};
152
153} // namespace twd
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).
Definition Logger.h:107
Consumer of Ethernet frames.
Definition Interfaces.h:23
void warning(const char *fmt,...)
Definition Logger.h:69
void info(const char *fmt,...)
Definition Logger.h:63