merge net into eth lib; ipv4 owns its IP via ipv4::init/get_ip; drop state struct in favor of eth::get_mac/ipv4::get_ip
This commit is contained in:
12
eth/CMakeLists.txt
Normal file
12
eth/CMakeLists.txt
Normal file
@@ -0,0 +1,12 @@
|
||||
add_library(eth STATIC eth.cpp)
|
||||
|
||||
target_include_directories(eth PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
target_compile_options(eth PRIVATE -Wall -Wextra -Wno-unused-parameter)
|
||||
|
||||
target_link_libraries(eth PUBLIC
|
||||
util
|
||||
debug_log
|
||||
w6300
|
||||
pico_unique_id
|
||||
)
|
||||
154
eth/eth.cpp
Normal file
154
eth/eth.cpp
Normal file
@@ -0,0 +1,154 @@
|
||||
#include "eth.h"
|
||||
#include <array>
|
||||
#include "pico/unique_id.h"
|
||||
#include "parse_buffer.h"
|
||||
#include "prepend_buffer.h"
|
||||
#include "w6300.h"
|
||||
#include "debug_log.h"
|
||||
|
||||
namespace eth {
|
||||
|
||||
namespace {
|
||||
|
||||
mac_addr g_mac;
|
||||
w6300::socket_id raw_socket{0};
|
||||
frame_cb_list frame_callbacks;
|
||||
|
||||
struct ethertype_entry {
|
||||
uint16_t ethertype_be;
|
||||
ethertype_handler fn;
|
||||
};
|
||||
std::array<ethertype_entry, 8> eth_handlers;
|
||||
size_t eth_handler_count = 0;
|
||||
|
||||
std::array<mac_filter, 4> mac_filters;
|
||||
size_t mac_filter_count = 0;
|
||||
|
||||
bool default_mac_filter(const mac_addr& dst) {
|
||||
return dst == g_mac || dst == MAC_BROADCAST;
|
||||
}
|
||||
|
||||
bool mac_match(const mac_addr& dst) {
|
||||
for (size_t i = 0; i < mac_filter_count; i++) {
|
||||
if (mac_filters[i](dst)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void process_frame(std::span<const uint8_t> frame, span_writer& tx) {
|
||||
if (frame.size() < sizeof(header)) {
|
||||
return;
|
||||
}
|
||||
auto& eth_hdr = *reinterpret_cast<const header*>(frame.data());
|
||||
|
||||
if (!mac_match(eth_hdr.dst)) {
|
||||
return;
|
||||
}
|
||||
|
||||
frame_callbacks.for_each([&](frame_cb_list::node* n) {
|
||||
if (n->value(frame)) {
|
||||
frame_callbacks.remove(n);
|
||||
}
|
||||
});
|
||||
|
||||
for (size_t i = 0; i < eth_handler_count; i++) {
|
||||
if (eth_handlers[i].ethertype_be == eth_hdr.ethertype) {
|
||||
eth_handlers[i].fn(frame, tx);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
__attribute__((constructor))
|
||||
void register_default_mac_filter() {
|
||||
register_mac_filter(default_mac_filter);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void register_ethertype(uint16_t ethertype_be, ethertype_handler fn) {
|
||||
if (eth_handler_count >= eth_handlers.size()) {
|
||||
dlogf("eth::register_ethertype overflow: ethertype=0x%04x dropped", __builtin_bswap16(ethertype_be));
|
||||
return;
|
||||
}
|
||||
eth_handlers[eth_handler_count++] = {ethertype_be, fn};
|
||||
}
|
||||
|
||||
void register_mac_filter(mac_filter fn) {
|
||||
if (mac_filter_count >= mac_filters.size()) {
|
||||
dlog("eth::register_mac_filter overflow: filter dropped");
|
||||
return;
|
||||
}
|
||||
mac_filters[mac_filter_count++] = fn;
|
||||
}
|
||||
|
||||
void send_raw(std::span<const uint8_t> data) {
|
||||
dlog_if_slow("eth::send_raw", 1000, [&]{
|
||||
auto result = w6300::send(raw_socket, data);
|
||||
if (!result) {
|
||||
dlogf("w6300 send failed: %zu bytes, err %d",
|
||||
data.size(), static_cast<int>(result.error()));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
bool init() {
|
||||
w6300::init_spi();
|
||||
w6300::reset();
|
||||
w6300::init();
|
||||
if (!w6300::check()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
pico_unique_board_id_t uid;
|
||||
pico_get_unique_board_id(&uid);
|
||||
g_mac[0] = (uid.id[0] & 0xFC) | 0x02;
|
||||
g_mac[1] = uid.id[1];
|
||||
g_mac[2] = uid.id[2];
|
||||
g_mac[3] = uid.id[3];
|
||||
g_mac[4] = uid.id[4];
|
||||
g_mac[5] = uid.id[5];
|
||||
|
||||
w6300::open_socket(raw_socket, w6300::protocol::macraw, w6300::sock_flag::none);
|
||||
w6300::set_interrupt_mask(w6300::ik_sock_0);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
const mac_addr& get_mac() {
|
||||
return g_mac;
|
||||
}
|
||||
|
||||
frame_cb_handle add_frame_callback(frame_callback cb) {
|
||||
auto h = frame_callbacks.insert(cb);
|
||||
if (!h) {
|
||||
dlog("eth::add_frame_callback overflow: callback dropped");
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
||||
void remove_frame_callback(frame_cb_handle h) {
|
||||
frame_callbacks.remove(h);
|
||||
}
|
||||
|
||||
void poll(std::span<uint8_t> tx) {
|
||||
if (!w6300::irq_pending) {
|
||||
return;
|
||||
}
|
||||
w6300::irq_pending = false;
|
||||
w6300::clear_interrupt(w6300::ik_int_all);
|
||||
static uint8_t rx_buf[1518];
|
||||
for (int i = 0; i < 16 && w6300::get_socket_recv_buf(raw_socket) > 0; i++) {
|
||||
auto result = w6300::recv(raw_socket, std::span{rx_buf});
|
||||
if (!result) {
|
||||
break;
|
||||
}
|
||||
span_writer tx_writer(tx);
|
||||
process_frame({rx_buf, *result}, tx_writer);
|
||||
}
|
||||
w6300::rearm_gpio_irq();
|
||||
}
|
||||
|
||||
} // namespace eth
|
||||
47
eth/eth.h
Normal file
47
eth/eth.h
Normal file
@@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <span>
|
||||
#include "callback_list.h"
|
||||
#include "span_writer.h"
|
||||
|
||||
namespace eth {
|
||||
|
||||
using mac_addr = std::array<uint8_t, 6>;
|
||||
|
||||
static constexpr mac_addr MAC_BROADCAST = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||
static constexpr uint16_t ETH_ARP = __builtin_bswap16(0x0806);
|
||||
static constexpr uint16_t ETH_IPV4 = __builtin_bswap16(0x0800);
|
||||
|
||||
struct __attribute__((packed)) header {
|
||||
mac_addr dst;
|
||||
mac_addr src;
|
||||
uint16_t ethertype;
|
||||
};
|
||||
static_assert(sizeof(header) == 14);
|
||||
|
||||
template <typename Buf>
|
||||
void prepend(Buf& buf, const mac_addr& dst, const mac_addr& src, uint16_t ethertype) {
|
||||
auto* h = buf.template prepend<header>();
|
||||
h->dst = dst;
|
||||
h->src = src;
|
||||
h->ethertype = ethertype;
|
||||
}
|
||||
|
||||
using frame_callback = bool (*)(std::span<const uint8_t> frame);
|
||||
using frame_cb_list = callback_list<frame_callback, 16>;
|
||||
using frame_cb_handle = frame_cb_list::node*;
|
||||
|
||||
using ethertype_handler = void (*)(std::span<const uint8_t> frame, span_writer& tx);
|
||||
using mac_filter = bool (*)(const mac_addr& dst);
|
||||
|
||||
bool init();
|
||||
const mac_addr& get_mac();
|
||||
frame_cb_handle add_frame_callback(frame_callback cb);
|
||||
void remove_frame_callback(frame_cb_handle h);
|
||||
void poll(std::span<uint8_t> tx);
|
||||
void send_raw(std::span<const uint8_t> data);
|
||||
void register_ethertype(uint16_t ethertype_be, ethertype_handler fn);
|
||||
void register_mac_filter(mac_filter fn);
|
||||
|
||||
} // namespace eth
|
||||
Reference in New Issue
Block a user