diff --git a/firmware/include/arp.h b/firmware/include/arp.h index a27b221..f073b01 100644 --- a/firmware/include/arp.h +++ b/firmware/include/arp.h @@ -1,5 +1,4 @@ #pragma once -#include #include #include "eth.h" #include "ipv4.h" @@ -21,7 +20,6 @@ struct __attribute__((packed)) header { static_assert(sizeof(header) == 28); void handle(std::span frame, span_writer& tx, - eth::mac_addr our_mac, ipv4::ip4_addr our_ip, - std::function)> send_raw); + eth::mac_addr our_mac, ipv4::ip4_addr our_ip); } // namespace arp diff --git a/firmware/include/icmp.h b/firmware/include/icmp.h index f92ca8e..4946587 100644 --- a/firmware/include/icmp.h +++ b/firmware/include/icmp.h @@ -1,6 +1,5 @@ #pragma once #include -#include #include #include "eth.h" #include "ipv4.h" @@ -18,8 +17,7 @@ struct __attribute__((packed)) echo { static_assert(sizeof(echo) == 8); void handle(std::span frame, span_writer& tx, - eth::mac_addr our_mac, ipv4::ip4_addr our_ip, - std::function)> send_raw); + eth::mac_addr our_mac, ipv4::ip4_addr our_ip); template void prepend_echo_request(Buf& buf, diff --git a/firmware/include/igmp.h b/firmware/include/igmp.h index fa8529f..c066e1a 100644 --- a/firmware/include/igmp.h +++ b/firmware/include/igmp.h @@ -1,6 +1,5 @@ #pragma once #include -#include #include #include "eth.h" #include "ipv4.h" @@ -24,15 +23,12 @@ bool is_member(const ipv4::ip4_addr& ip); bool is_member_mac(const eth::mac_addr& mac); void join(const ipv4::ip4_addr& group, - eth::mac_addr our_mac, ipv4::ip4_addr our_ip, - std::function)> send_raw); + eth::mac_addr our_mac, ipv4::ip4_addr our_ip); -void send_all_reports(eth::mac_addr our_mac, ipv4::ip4_addr our_ip, - std::function)> send_raw); +void send_all_reports(eth::mac_addr our_mac, ipv4::ip4_addr our_ip); void handle(std::span frame, span_writer& tx, - eth::mac_addr our_mac, ipv4::ip4_addr our_ip, - std::function)> send_raw); + eth::mac_addr our_mac, ipv4::ip4_addr our_ip); template void prepend_report(Buf& buf, const eth::mac_addr& src_mac, ipv4::ip4_addr src_ip, diff --git a/firmware/include/ipv4.h b/firmware/include/ipv4.h index 602b017..03f6b2b 100644 --- a/firmware/include/ipv4.h +++ b/firmware/include/ipv4.h @@ -58,7 +58,6 @@ void prepend(Buf& buf, const eth::mac_addr& dst_mac, const eth::mac_addr& src_ma void handle(std::span frame, span_writer& tx, eth::mac_addr our_mac, ip4_addr our_ip, ip4_addr subnet_broadcast, - std::function)> send_raw, std::function, span_writer&)> handle_udp); } // namespace ipv4 diff --git a/firmware/lib/arp.cpp b/firmware/lib/arp.cpp index 1bafa43..02971b4 100644 --- a/firmware/lib/arp.cpp +++ b/firmware/lib/arp.cpp @@ -1,4 +1,5 @@ #include "arp.h" +#include "net.h" #include "parse_buffer.h" #include "prepend_buffer.h" @@ -10,8 +11,7 @@ static constexpr uint16_t ARP_OP_REQUEST = __builtin_bswap16(1); static constexpr uint16_t ARP_OP_REPLY = __builtin_bswap16(2); void handle(std::span frame, span_writer& tx, - eth::mac_addr our_mac, ipv4::ip4_addr our_ip, - std::function)> send_raw) { + eth::mac_addr our_mac, ipv4::ip4_addr our_ip) { parse_buffer pb(frame); pb.consume(); auto* arp_hdr = pb.consume
(); @@ -36,7 +36,7 @@ void handle(std::span frame, span_writer& tx, reply->tpa = arp_hdr->spa; eth::prepend(buf, arp_hdr->sha, our_mac, eth::ETH_ARP); - send_raw(buf.span()); + net_send_raw(buf.span()); } } // namespace arp diff --git a/firmware/lib/dispatch.cpp b/firmware/lib/dispatch.cpp index ae00e52..e141fe5 100644 --- a/firmware/lib/dispatch.cpp +++ b/firmware/lib/dispatch.cpp @@ -1,5 +1,4 @@ #include "dispatch.h" -#include #include "pico/stdlib.h" #include "wire.h" #include "timer_queue.h" @@ -12,7 +11,7 @@ static timer_queue timers; static void igmp_reannounce() { auto& ns = net_get_state(); - igmp::send_all_reports(ns.mac, ns.ip, net_send_raw); + igmp::send_all_reports(ns.mac, ns.ip); dispatch_schedule_ms(60000, igmp_reannounce); } @@ -33,7 +32,7 @@ bool dispatch_cancel_timer(timer_handle h) { } [[noreturn]] void dispatch_run(std::span handlers) { - std::unordered_map handler_map; + std::array handler_map{}; for (auto& entry : handlers) { handler_map[entry.type_id] = entry.handle; } @@ -41,13 +40,12 @@ bool dispatch_cancel_timer(timer_handle h) { static std::array tx_buf; auto dispatch_msg = [&](const DecodedMessage& msg, send_fn send) { - auto it = handler_map.find(msg.type_id); - if (it == handler_map.end()) { + if (msg.type_id < 0 || !handler_map[msg.type_id]) { dlogf("dispatch: unknown type_id %d", msg.type_id); return; } responder resp{msg.message_id, std::move(send)}; - it->second(resp, msg.payload); + handler_map[msg.type_id](resp, msg.payload); }; net_set_handler([&](std::span payload, diff --git a/firmware/lib/icmp.cpp b/firmware/lib/icmp.cpp index 595b642..0fe864b 100644 --- a/firmware/lib/icmp.cpp +++ b/firmware/lib/icmp.cpp @@ -1,14 +1,14 @@ #include "icmp.h" #include #include "ipv4.h" +#include "net.h" #include "parse_buffer.h" #include "prepend_buffer.h" namespace icmp { void handle(std::span frame, span_writer& tx, - eth::mac_addr our_mac, ipv4::ip4_addr our_ip, - std::function)> send_raw) { + eth::mac_addr our_mac, ipv4::ip4_addr our_ip) { parse_buffer pb(frame); auto* eth_hdr = pb.consume(); auto* ip = pb.consume(); @@ -34,7 +34,7 @@ void handle(std::span frame, span_writer& tx, reply->checksum = ipv4::checksum(reply, icmp_len); ipv4::prepend(buf, eth_hdr->src, our_mac, our_ip, ip->src, 1, icmp_len); - send_raw(buf.span()); + net_send_raw(buf.span()); } bool parse_echo_reply(std::span frame, ipv4::ip4_addr& src_ip, uint16_t expected_id) { diff --git a/firmware/lib/igmp.cpp b/firmware/lib/igmp.cpp index c8aafd6..b8878cd 100644 --- a/firmware/lib/igmp.cpp +++ b/firmware/lib/igmp.cpp @@ -1,6 +1,7 @@ #include "igmp.h" #include #include "ipv4.h" +#include "net.h" #include "parse_buffer.h" #include "prepend_buffer.h" @@ -34,31 +35,27 @@ bool is_member_mac(const eth::mac_addr& mac) { } static void send_report(const ipv4::ip4_addr& group, - eth::mac_addr our_mac, ipv4::ip4_addr our_ip, - std::function)> send_raw) { + eth::mac_addr our_mac, ipv4::ip4_addr our_ip) { prepend_buffer<4096> buf; prepend_report(buf, our_mac, our_ip, group); - send_raw(buf.span()); + net_send_raw(buf.span()); } void join(const ipv4::ip4_addr& group, - eth::mac_addr our_mac, ipv4::ip4_addr our_ip, - std::function)> send_raw) { + eth::mac_addr our_mac, ipv4::ip4_addr our_ip) { for (auto& g : groups) if (g.ip == group) return; groups.push_back({group, mac_for_ip(group)}); - send_report(group, our_mac, our_ip, send_raw); + send_report(group, our_mac, our_ip); } -void send_all_reports(eth::mac_addr our_mac, ipv4::ip4_addr our_ip, - std::function)> send_raw) { +void send_all_reports(eth::mac_addr our_mac, ipv4::ip4_addr our_ip) { for (auto& g : groups) - send_report(g.ip, our_mac, our_ip, send_raw); + send_report(g.ip, our_mac, our_ip); } void handle(std::span frame, span_writer& tx, - eth::mac_addr our_mac, ipv4::ip4_addr our_ip, - std::function)> send_raw) { + eth::mac_addr our_mac, ipv4::ip4_addr our_ip) { parse_buffer pb(frame); pb.consume(); auto* ip = pb.consume(); @@ -73,11 +70,11 @@ void handle(std::span frame, span_writer& tx, if (msg->group == ipv4::ip4_addr{0, 0, 0, 0}) { for (auto& g : groups) - send_report(g.ip, our_mac, our_ip, send_raw); + send_report(g.ip, our_mac, our_ip); } else { for (auto& g : groups) { if (g.ip == msg->group) { - send_report(g.ip, our_mac, our_ip, send_raw); + send_report(g.ip, our_mac, our_ip); break; } } diff --git a/firmware/lib/ipv4.cpp b/firmware/lib/ipv4.cpp index 96ee2e0..b06282c 100644 --- a/firmware/lib/ipv4.cpp +++ b/firmware/lib/ipv4.cpp @@ -25,7 +25,6 @@ static bool ip_match(const ip4_addr& dst, const ip4_addr& our_ip, const ip4_addr void handle(std::span frame, span_writer& tx, eth::mac_addr our_mac, ip4_addr our_ip, ip4_addr subnet_broadcast, - std::function)> send_raw, std::function, span_writer&)> handle_udp) { parse_buffer pb(frame); pb.consume(); @@ -40,10 +39,10 @@ void handle(std::span frame, span_writer& tx, case 1: if (!ip_match(ip->dst, our_ip, subnet_broadcast)) return; - icmp::handle(frame, tx, our_mac, our_ip, send_raw); + icmp::handle(frame, tx, our_mac, our_ip); break; case 2: - igmp::handle(frame, tx, our_mac, our_ip, send_raw); + igmp::handle(frame, tx, our_mac, our_ip); break; case 17: if (!ip_match(ip->dst, our_ip, subnet_broadcast)) diff --git a/firmware/lib/net.cpp b/firmware/lib/net.cpp index 089ac0e..26beb4c 100644 --- a/firmware/lib/net.cpp +++ b/firmware/lib/net.cpp @@ -81,10 +81,10 @@ static void process_frame(std::span frame, span_writer& tx) { switch (eth_hdr.ethertype) { case eth::ETH_ARP: - arp::handle(frame, tx, state.mac, state.ip, net_send_raw); + arp::handle(frame, tx, state.mac, state.ip); break; case eth::ETH_IPV4: - ipv4::handle(frame, tx, state.mac, state.ip, IP_BROADCAST_SUBNET, net_send_raw, handle_udp); + ipv4::handle(frame, tx, state.mac, state.ip, IP_BROADCAST_SUBNET, handle_udp); break; } } @@ -112,7 +112,7 @@ bool net_init() { w6300::open_socket(raw_socket, w6300::protocol::macraw, w6300::sock_flag::none); w6300::set_interrupt_mask(w6300::ik_sock_0); - igmp::join(igmp::PICOMAP_DISCOVERY_GROUP, state.mac, state.ip, net_send_raw); + igmp::join(igmp::PICOMAP_DISCOVERY_GROUP, state.mac, state.ip); return true; }