TinyWifiDongle 0.1.0
Header-only USB WiFi dongle for Arduino
Loading...
Searching...
No Matches
Rp2040Dongle.h
Go to the documentation of this file.
1#pragma once
2#include <Arduino.h>
3
6#include "Rp2040UsbNcm.h"
8
9namespace twd {
10
25 public:
26 Rp2040Dongle() : bridge_(usb_, link_) {}
27
29 bool begin(const TinyWifiDongleConfig& cfg) {
30 if (!cfg.ssid) {
31 TWD_LOGGER.error("begin: no ssid");
32 return false;
33 }
34 TWD_LOGGER.info("starting: ssid='%s'", cfg.ssid);
35
36 usb_.setBuffers(cfg.framesToHost, cfg.framesFromHost);
37 usb_.setHostMac(link_.macAddress()); // the access point only accepts the station's MAC
38 if (!usb_.begin(cfg.productName)) return false;
39 bridge_.begin();
40 if (!link_.begin(cfg.ssid, cfg.password)) return false;
41
42 if (TWD_LOGGER.enabled() && cfg.statsIntervalMs) {
43 statsMs_ = cfg.statsIntervalMs;
44 stats_.user_data = this;
45 stats_.do_work = &Rp2040Dongle::statsCb;
46 if (!async_context_add_at_time_worker_in_ms(__getEthernetContext(), &stats_, statsMs_)) {
47 TWD_LOGGER.warning("status line: could not start its worker");
48 }
49 }
50 return true;
51 }
52
54 bool usbReady() { return usb_.ready(); }
56 bool uplinkConnected() { return link_.connected(); }
57
59 L2Bridge::Stats bridgeStats() const { return bridge_.stats(); }
61 Rp2040UsbNcm::Stats usbStats() const { return usb_.stats(); }
62
63 private:
64 Rp2040UsbNcm usb_;
66 L2Bridge bridge_;
67 async_at_time_worker_t stats_ = {};
68 uint32_t statsMs_ = 0;
69
70 static void statsCb(async_context_t*, async_at_time_worker_t* w) {
71 Rp2040Dongle* s = (Rp2040Dongle*)w->user_data;
72 L2Bridge::Stats b = s->bridge_.stats();
73 Rp2040UsbNcm::Stats u = s->usb_.stats();
74 TWD_LOGGER.info("w%d toL=%u toH=%u rf=%u dr=%u | u rx=%u tx=%u td=%u rd=%u q=%u", (int)s->link_.connected(),
75 (unsigned)b.toLink, (unsigned)b.toHost, (unsigned)b.reflected, (unsigned)b.dropped,
76 (unsigned)u.rxFrames, (unsigned)u.txFrames, (unsigned)u.txDrops, (unsigned)u.rxDrops,
77 (unsigned)s->usb_.txQueued());
78 w->next_time = make_timeout_time_ms(s->statsMs_);
79 }
80};
81
82} // 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
Layer-2 bridge between the USB host and the uplink network.
Definition L2Bridge.h:23
bool uplinkConnected()
True while the board is associated with the access point.
bool begin(const TinyWifiDongleConfig &cfg)
Start USB and WiFi and bridge them; false if a part could not be started.
bool usbReady()
True once the USB host has configured the adapter.
L2Bridge::Stats bridgeStats() const
Counters of the bridge since start.
Rp2040UsbNcm::Stats usbStats() const
Counters of the USB side since start.
USB CDC-NCM network device for the Raspberry Pi Pico W / Pico 2 W.
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").
Counters since start.
Definition L2Bridge.h:26
Counters since start.