TinyWifiDongle 0.1.0
Header-only USB WiFi dongle for Arduino
Loading...
Searching...
No Matches
L2Bridge.h
Go to the documentation of this file.
1#pragma once
2#include <string.h>
3
4#include "Interfaces.h"
5
6namespace twd {
7
23class L2Bridge : public FrameReceiver {
24 public:
26 struct Stats {
27 uint32_t toLink;
28 uint32_t toHost;
29 uint32_t reflected;
30 uint32_t foreign;
31 uint32_t dropped;
32 };
33
35 L2Bridge(UsbTransport& usb, FrameLink& link) : usb_(usb), link_(link), down_(*this) {}
36
38 void begin() {
39 usb_.setReceiver(this);
40 link_.setFrameReceiver(&down_);
41 TWD_LOGGER.info("bridge: host adapter uses MAC %02x:%02x:%02x:%02x:%02x:%02x", link_.macAddress()[0],
42 link_.macAddress()[1], link_.macAddress()[2], link_.macAddress()[3], link_.macAddress()[4],
43 link_.macAddress()[5]);
44 }
45
47 Stats stats() const { return stats_; }
48
50 void onFrame(const uint8_t* frame, uint16_t len) override {
51 if (len < 14) return;
52 if (memcmp(frame + 6, link_.macAddress(), 6) != 0) {
53 stats_.foreign++;
54 return;
55 }
56 if (link_.sendFrame(frame, len)) {
57 stats_.toLink++;
58 } else {
59 stats_.dropped++;
60 }
61 }
62
63 private:
65 class Downstream : public FrameReceiver {
66 public:
67 explicit Downstream(L2Bridge& bridge) : bridge_(bridge) {}
68 void onFrame(const uint8_t* frame, uint16_t len) override { bridge_.fromLink(frame, len); }
69
70 private:
71 L2Bridge& bridge_;
72 };
73
74 void fromLink(const uint8_t* frame, uint16_t len) {
75 if (len < 14) return;
76 if (memcmp(frame + 6, link_.macAddress(), 6) == 0) {
77 stats_.reflected++;
78 return;
79 }
80 if (usb_.send(frame, len)) {
81 stats_.toHost++;
82 } else {
83 stats_.dropped++;
84 }
85 }
86
87 UsbTransport& usb_;
88 FrameLink& link_;
89 Downstream down_;
90 Stats stats_ = {};
91};
92
93} // 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
Layer-2 bridge between the USB host and the uplink network.
Definition L2Bridge.h:23
void onFrame(const uint8_t *frame, uint16_t len) override
Host -> uplink (called by the UsbTransport).
Definition L2Bridge.h:50
void begin()
Start forwarding; both sides must already be started.
Definition L2Bridge.h:38
L2Bridge(UsbTransport &usb, FrameLink &link)
Bridge usb and link.
Definition L2Bridge.h:35
Stats stats() const
Current counters.
Definition L2Bridge.h:47
Moves Ethernet frames between the MCU and the USB host.
Definition Interfaces.h:37
Counters since start.
Definition L2Bridge.h:26
uint32_t dropped
frames the other side could not take
Definition L2Bridge.h:31
uint32_t toHost
frames forwarded uplink -> host
Definition L2Bridge.h:28
uint32_t toLink
frames forwarded host -> uplink
Definition L2Bridge.h:27
uint32_t foreign
host frames with a foreign source MAC, dropped
Definition L2Bridge.h:30
uint32_t reflected
echoes of the host's own frames, dropped
Definition L2Bridge.h:29