TinyWifiDongle 0.1.0
Header-only USB WiFi dongle for Arduino
Loading...
Searching...
No Matches
Esp32Dongle.h
Go to the documentation of this file.
1#pragma once
2#include <Arduino.h>
3#include "USB.h"
4
7#include "Esp32UsbNcm.h"
8#include "Esp32UsbTaskPin.h"
10
11namespace twd {
12
22 public:
23 Esp32Dongle() : bridge_(usb_, link_) {}
24
26 bool begin(const TinyWifiDongleConfig& cfg) {
27 if (!cfg.ssid) {
28 TWD_LOGGER.error("begin: no ssid");
29 return false;
30 }
31 ssid_ = cfg.ssid;
32 password_ = cfg.password ? cfg.password : "";
33 product_ = cfg.productName;
34 hasPassword_ = cfg.password != nullptr;
35
36 statsIntervalMs_ = cfg.statsIntervalMs;
37 if (TWD_LOGGER.enabled() && statsIntervalMs_) {
38 if (xTaskCreate(&Esp32Dongle::statsTask, "dongleStats", 6144, this, 10, nullptr) != pdPASS) {
39 TWD_LOGGER.warning("status line: could not start its task");
40 }
41 }
42 TWD_LOGGER.info("starting: ssid='%s'", ssid_.c_str());
43
44 usb_.setMaxFrame(cfg.usbMaxFrame);
45 usb_.setStallTimeout(cfg.stallTimeoutMs);
46 usb_.setHostMac(link_.macAddress()); // the access point only accepts the station's MAC
47 if (!usb_.begin(product_.c_str())) return false;
48 bridge_.begin();
49 if (!link_.begin(ssid_.c_str(), hasPassword_ ? password_.c_str() : nullptr)) return false;
50
51 if (cfg.manageUsb) {
52 USB.productName(product_.c_str());
53 if (!USB.begin()) {
54 TWD_LOGGER.error("USB: USB.begin() failed");
55 return false;
56 }
57 if (cfg.pinUsbTask && !usbPin_.begin(xPortGetCoreID())) { // the USB interrupt lives on this core
58 TWD_LOGGER.warning("USB: could not start the task that pins the USB task; send stalls are more likely");
59 }
60 }
61 return true;
62 }
63
65 bool usbReady() { return usb_.ready(); }
67 bool uplinkConnected() { return link_.connected(); }
68
69 private:
70 Esp32UsbNcm usb_;
72 L2Bridge bridge_;
73 Esp32UsbTaskPin usbPin_;
74 String ssid_, password_, product_;
75 bool hasPassword_ = false;
76 uint32_t statsIntervalMs_ = 0;
77
78 static void statsTask(void* arg) {
79 Esp32Dongle* s = (Esp32Dongle*)arg;
80 for (;;) {
81 vTaskDelay(pdMS_TO_TICKS(s->statsIntervalMs_));
82 if (s->usb_.txQueued() > 8) { // send path looks stuck: show the controller instead of the counters
83 s->usb_.logControllerState();
84 continue;
85 }
86 Esp32UsbNcm::Stats u = s->usb_.stats();
87 L2Bridge::Stats b = s->bridge_.stats();
88 // Short on purpose: the log port drops what does not fit its buffer.
89 TWD_LOGGER.info("w%d toL=%u toH=%u rf=%u dr=%u | u rx=%u tx=%u td=%u bz=%u q=%u rs=%u", (int)s->link_.connected(),
90 (unsigned)b.toLink, (unsigned)b.toHost, (unsigned)b.reflected, (unsigned)b.dropped,
91 (unsigned)u.rxFrames, (unsigned)u.txFrames, (unsigned)u.txDrops, (unsigned)u.txBusy,
92 (unsigned)s->usb_.txQueued(), (unsigned)u.resets);
93 }
94 }
95};
96
97} // namespace twd
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
ESP32-S2/S3 dongle: raw Ethernet frames between USB and WiFi.
Definition Esp32Dongle.h:21
bool begin(const TinyWifiDongleConfig &cfg)
Start USB and WiFi and bridge them; false if a part could not be started.
Definition Esp32Dongle.h:26
bool uplinkConnected()
True while the MCU is associated with the access point.
Definition Esp32Dongle.h:67
bool usbReady()
True once the USB host has configured the adapter.
Definition Esp32Dongle.h:65
USB CDC-NCM network device for the ESP32-S2/S3 (TinyUSB).
Definition Esp32UsbNcm.h:38
void logControllerState()
uint32_t txQueued() const
Number of frames waiting to be sent to the host.
Stats stats() const
Current counters.
Runs the TinyUSB device task on one fixed CPU core.
Layer-2 bridge between the USB host and the uplink network.
Definition L2Bridge.h:23
Stats stats() const
Current counters.
Definition L2Bridge.h:47
Settings of the dongle; every field has a usable default except the SSID.
const char * productName
USB product string shown on the host.
const char * password
Password of the network (nullptr for an open network).
const char * ssid
WiFi network the MCU joins (the "uplink").