#include "dispatch.h" #include #include "pico/stdlib.h" #include "wire.h" #include "timer_queue.h" #include "net.h" #include "igmp.h" #include "udp.h" #include "debug_log.h" #include "hardware/sync.h" static timer_queue timers; static std::array handler_map{}; static void igmp_reannounce() { auto& ns = net_get_state(); igmp::send_all_reports(ns.mac, ns.ip); dispatch_schedule_ms(60000, igmp_reannounce); } void dispatch_init() { net_init(); dispatch_schedule_ms(60000, igmp_reannounce); dlog("dispatch_init complete"); } timer_handle dispatch_schedule_ms(uint32_t ms, void (*fn)()) { auto h = timers.schedule_ms(ms, fn); if (!h) dlogf("timer alloc failed: %lu ms", static_cast(ms)); return h; } bool dispatch_cancel_timer(timer_handle h) { return timers.cancel(h); } void udp::client::handler(std::span payload, const udp::address& from) { auto msg = try_decode(payload.data(), payload.size()); if (!msg) return; 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, from}; handler_map[msg->type_id](resp, msg->payload); } [[noreturn]] void dispatch_run(std::span handlers) { for (auto& entry : handlers) handler_map[entry.type_id] = entry.handle; static std::array tx_buf; while (true) { uint32_t save = save_and_disable_interrupts(); dlog_if_slow("timers", 1000, [&]{ timers.run(); }); dlog_if_slow("net_poll", 1000, [&]{ net_poll(std::span{tx_buf}); }); __wfi(); restore_interrupts(save); } }