Ethertype handler registry: arp/ipv4 self-register via __attribute__((constructor)), drop threaded mac/ip/subnet params

This commit is contained in:
Ian Gulliver
2026-04-19 08:26:57 -07:00
parent 59829b569e
commit 7e493b7d70
6 changed files with 50 additions and 30 deletions

View File

@@ -1,22 +1,30 @@
#include "net.h"
#include <array>
#include "pico/unique_id.h"
#include "pico/time.h"
#include "eth.h"
#include "arp.h"
#include "ipv4.h"
#include "udp.h"
#include "igmp.h"
#include "parse_buffer.h"
#include "prepend_buffer.h"
#include "w6300.h"
#include "debug_log.h"
static constexpr ipv4::ip4_addr IP_BROADCAST_SUBNET = {169, 254, 255, 255};
static net_state state;
static w6300::socket_id raw_socket{0};
static frame_cb_list frame_callbacks;
struct ethertype_entry {
uint16_t ethertype_be;
ethertype_handler fn;
};
static std::array<ethertype_entry, 8> eth_handlers;
static size_t eth_handler_count = 0;
void net_register_ethertype(uint16_t ethertype_be, ethertype_handler fn) {
if (eth_handler_count < eth_handlers.size())
eth_handlers[eth_handler_count++] = {ethertype_be, fn};
}
void net_send_raw(std::span<const uint8_t> data) {
dlog_if_slow("net_send_raw", 1000, [&]{
auto result = w6300::send(raw_socket, data);
@@ -41,13 +49,11 @@ static void process_frame(std::span<const uint8_t> frame, span_writer& tx) {
frame_callbacks.remove(n);
});
switch (eth_hdr.ethertype) {
case eth::ETH_ARP:
arp::handle(frame, tx, state.mac, state.ip);
break;
case eth::ETH_IPV4:
ipv4::handle(frame, tx, state.mac, state.ip, IP_BROADCAST_SUBNET);
break;
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;
}
}
}