7#include "esp_private/wifi.h"
28 bool begin(
const char* ssid,
const char* password)
override {
30 esp_timer_create_args_t args = {};
31 args.callback = &Esp32WifiFrameLink::claimRxTimer;
34 if (esp_timer_create(&args, &claimTimer_) != ESP_OK) {
35 TWD_LOGGER.error(
"WiFi: could not create the receive hook timer");
41 WiFi.onEvent(&Esp32WifiFrameLink::onConnected, ARDUINO_EVENT_WIFI_STA_CONNECTED);
42 WiFi.onEvent(&Esp32WifiFrameLink::onDisconnected, ARDUINO_EVENT_WIFI_STA_DISCONNECTED);
43 if (WiFi.begin(ssid, password) == WL_CONNECT_FAILED) {
44 TWD_LOGGER.error(
"WiFi: could not start joining '%s' (invalid ssid or password?)", ssid);
53 bool sendFrame(
const uint8_t* frame, uint16_t len)
override {
54 if (!associated_)
return false;
55 return esp_wifi_internal_tx(WIFI_IF_STA,
const_cast<uint8_t*
>(frame), len) == ESP_OK;
68 if (esp_wifi_get_mac(WIFI_IF_STA, mac_) != ESP_OK) {
69 TWD_LOGGER.warning(
"WiFi: station MAC not available from the WiFi stack, using the one from the fuses");
70 esp_read_mac(mac_, ESP_MAC_WIFI_STA);
78 static constexpr int kClaims = 4;
81 esp_timer_handle_t claimTimer_ =
nullptr;
83 bool macValid_ =
false;
84 volatile bool associated_ =
false;
87 static Esp32WifiFrameLink*& instance() {
88 static Esp32WifiFrameLink* p =
nullptr;
92 static void stopDhcpClient() {
93 esp_netif_t* sta = esp_netif_get_handle_from_ifkey(
"WIFI_STA_DEF");
94 if (sta) esp_netif_dhcpc_stop(sta);
98 static esp_err_t rxHook(
void* buffer, uint16_t len,
void* eb) {
99 Esp32WifiFrameLink* s = instance();
100 if (s && s->receiver_) s->receiver_->onFrame((
const uint8_t*)buffer, len);
101 esp_wifi_internal_free_rx_buffer(eb);
109 esp_err_t err = esp_wifi_internal_reg_rxcb(WIFI_IF_STA, &Esp32WifiFrameLink::rxHook);
110 if (err != ESP_OK)
TWD_LOGGER.
error(
"WiFi: could not take over frame reception (%s)", esp_err_to_name(err));
113 static void claimRxTimer(
void* arg) {
114 Esp32WifiFrameLink* s = (Esp32WifiFrameLink*)arg;
116 if (++s->claims_ < kClaims && s->associated_) esp_timer_start_once(s->claimTimer_, 250000);
119 static void onConnected(arduino_event_t*) {
120 Esp32WifiFrameLink* s = instance();
122 s->associated_ =
true;
125 esp_timer_stop(s->claimTimer_);
126 esp_timer_start_once(s->claimTimer_, 250000);
130 static void onDisconnected(arduino_event_t* e) {
131 Esp32WifiFrameLink* s = instance();
133 s->associated_ =
false;
134 TWD_LOGGER.
warning(
"WiFi disconnected, reason=%d", (
int)e->event_info.wifi_sta_disconnected.reason);
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).
Raw Ethernet access to the WiFi network through the ESP32's station.
const uint8_t * macAddress() override
bool connected() override
True while associated with the access point (the MCU has no IP address).
void setFrameReceiver(FrameReceiver *receiver) override
Frames coming from the uplink network are passed to receiver.
bool begin(const char *ssid, const char *password) override
Start joining the network; returns at once, see connected().
bool sendFrame(const uint8_t *frame, uint16_t len) override
Sends a frame to the uplink network; false if it was dropped.
Raw Ethernet access to the uplink network (bridge mode).
Consumer of Ethernet frames.
void error(const char *fmt,...)
void warning(const char *fmt,...)
void info(const char *fmt,...)
The connection to the WiFi network the traffic is bridged to.