TinyWifiDongle 0.1.0
Header-only USB WiFi dongle for Arduino
Loading...
Searching...
No Matches
Esp32WifiFrameLink.h
Go to the documentation of this file.
1#pragma once
2#include <Arduino.h>
3#include <WiFi.h>
4#include "esp_mac.h"
5#include "esp_netif.h"
6#include "esp_timer.h"
7#include "esp_private/wifi.h" // esp_wifi_internal_reg_rxcb / _tx / _free_rx_buffer
8
10
11namespace twd {
12
26class Esp32WifiFrameLink : public Uplink, public FrameLink {
27 public:
28 bool begin(const char* ssid, const char* password) override {
29 instance() = this;
30 esp_timer_create_args_t args = {};
31 args.callback = &Esp32WifiFrameLink::claimRxTimer;
32 args.arg = this;
33 args.name = "wifiRx";
34 if (esp_timer_create(&args, &claimTimer_) != ESP_OK) {
35 TWD_LOGGER.error("WiFi: could not create the receive hook timer");
36 return false;
37 }
38 WiFi.mode(WIFI_STA);
39 WiFi.setSleep(false); // modem sleep delays downlink packets; a dongle must stay awake
40 stopDhcpClient();
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);
45 return false;
46 }
47 return true;
48 }
49
51 bool connected() override { return associated_; }
52
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;
56 }
57
58 void setFrameReceiver(FrameReceiver* receiver) override { receiver_ = receiver; }
59
65 const uint8_t* macAddress() override {
66 if (!macValid_) {
67 WiFi.mode(WIFI_STA); // initializes WiFi (and the companion chip); harmless if already done
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);
71 }
72 macValid_ = true;
73 }
74 return mac_;
75 }
76
77 private:
78 static constexpr int kClaims = 4; // times the receive hook is re-asserted after a connect
79
80 FrameReceiver* receiver_ = nullptr;
81 esp_timer_handle_t claimTimer_ = nullptr;
82 uint8_t mac_[6] = {};
83 bool macValid_ = false;
84 volatile bool associated_ = false;
85 int claims_ = 0;
86
87 static Esp32WifiFrameLink*& instance() {
88 static Esp32WifiFrameLink* p = nullptr;
89 return p;
90 }
91
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);
95 }
96
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);
102 return ESP_OK;
103 }
104
105 // The IP stack registers its own receive hook when the station connects,
106 // possibly after our event handler ran. Register ours again a few times.
107 void claimRx() {
108 stopDhcpClient();
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));
111 }
112
113 static void claimRxTimer(void* arg) {
114 Esp32WifiFrameLink* s = (Esp32WifiFrameLink*)arg;
115 s->claimRx();
116 if (++s->claims_ < kClaims && s->associated_) esp_timer_start_once(s->claimTimer_, 250000);
117 }
118
119 static void onConnected(arduino_event_t*) {
120 Esp32WifiFrameLink* s = instance();
121 if (!s) return;
122 s->associated_ = true;
123 s->claims_ = 0;
124 s->claimRx();
125 esp_timer_stop(s->claimTimer_);
126 esp_timer_start_once(s->claimTimer_, 250000);
127 TWD_LOGGER.info("WiFi associated, forwarding raw frames");
128 }
129
130 static void onDisconnected(arduino_event_t* e) {
131 Esp32WifiFrameLink* s = instance();
132 if (!s) return;
133 s->associated_ = false;
134 TWD_LOGGER.warning("WiFi disconnected, reason=%d", (int)e->event_info.wifi_sta_disconnected.reason);
135 }
136};
137
138} // 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 error(const char *fmt,...)
Definition Logger.h:75
void warning(const char *fmt,...)
Definition Logger.h:69
void info(const char *fmt,...)
Definition Logger.h:63