68 lines
1.8 KiB
C++
68 lines
1.8 KiB
C++
#include "dispatch.h"
|
|
#include <array>
|
|
#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_fn, 128> handler_map{};
|
|
static uint16_t listen_port_be = 0;
|
|
|
|
uint16_t dispatch_listen_port_be() { return listen_port_be; }
|
|
|
|
static void igmp_reannounce() {
|
|
igmp::send_all_reports();
|
|
dispatch_schedule_ms(60000, igmp_reannounce);
|
|
}
|
|
|
|
static void on_udp_message(std::span<const uint8_t> 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);
|
|
}
|
|
|
|
void dispatch_init(uint16_t port_be) {
|
|
listen_port_be = port_be;
|
|
udp::register_port(port_be, on_udp_message);
|
|
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<unsigned long>(ms));
|
|
return h;
|
|
}
|
|
|
|
bool dispatch_cancel_timer(timer_handle h) {
|
|
return timers.cancel(h);
|
|
}
|
|
|
|
[[noreturn]] void dispatch_run(std::span<const handler_entry> handlers) {
|
|
for (auto& entry : handlers)
|
|
handler_map[entry.type_id] = entry.handle;
|
|
|
|
static std::array<uint8_t, 4096> 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);
|
|
}
|
|
}
|