Eliminate std::function: fn-pointer callbacks, per-test test_state structs, udp.cpp with link-time udp::client::handler, udp::address

This commit is contained in:
Ian Gulliver
2026-04-19 00:32:13 -07:00
parent 40f7fb5941
commit 32044a5cbd
11 changed files with 378 additions and 285 deletions

View File

@@ -12,11 +12,9 @@
#include "debug_log.h"
static constexpr ipv4::ip4_addr IP_BROADCAST_SUBNET = {169, 254, 255, 255};
static constexpr uint16_t PICOMAP_PORT = __builtin_bswap16(28781);
static net_state state;
static w6300::socket_id raw_socket{0};
static net_handler msg_handler;
static frame_cb_list frame_callbacks;
void net_send_raw(std::span<const uint8_t> data) {
@@ -28,42 +26,6 @@ void net_send_raw(std::span<const uint8_t> data) {
});
}
void net_handle_udp(std::span<const uint8_t> frame, span_writer& tx) {
parse_buffer pb(frame);
auto* eth_hdr = pb.consume<eth::header>();
auto* ip = pb.consume<ipv4::header>();
if (!ip) return;
size_t options_len = ip->header_len() - sizeof(ipv4::header);
if (options_len > 0 && !pb.skip(options_len)) return;
auto* uhdr = pb.consume<udp::header>();
if (!uhdr) return;
if (uhdr->dst_port != PICOMAP_PORT) return;
if (!msg_handler) return;
size_t udp_len = __builtin_bswap16(uhdr->length);
if (udp_len < sizeof(udp::header)) return;
size_t payload_len = udp_len - sizeof(udp::header);
if (pb.remaining_size() < payload_len) return;
eth::mac_addr dst_mac = eth_hdr->src;
ipv4::ip4_addr dst_ip = ip->src;
uint16_t dst_port = uhdr->src_port;
msg_handler(pb.remaining().subspan(0, payload_len),
[dst_mac, dst_ip, dst_port](encode_fn encode) {
prepend_buffer<4096> buf;
span_writer out(buf.payload_ptr(), 2048);
auto r = encode(out);
if (!r) return;
buf.append(*r);
udp::prepend(buf, dst_mac, state.mac, state.ip, dst_ip,
PICOMAP_PORT, dst_port, *r);
net_send_raw(buf.span());
});
}
static bool mac_match(const eth::mac_addr& dst) {
return dst == state.mac || dst == eth::MAC_BROADCAST || igmp::is_member_mac(dst);
}
@@ -121,12 +83,8 @@ const net_state& net_get_state() {
return state;
}
void net_set_handler(net_handler handler) {
msg_handler = std::move(handler);
}
frame_cb_handle net_add_frame_callback(net_frame_callback cb) {
auto h = frame_callbacks.insert(std::move(cb));
auto h = frame_callbacks.insert(cb);
if (!h) dlog("frame callback alloc failed");
return h;
}