2026-04-06 17:36:41 +09:00
|
|
|
#include "dispatch.h"
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
#include "pico/stdlib.h"
|
|
|
|
|
#include "tusb.h"
|
|
|
|
|
#include "wire.h"
|
2026-04-06 20:22:40 +09:00
|
|
|
#include "usb_cdc.h"
|
2026-04-06 17:36:41 +09:00
|
|
|
#include "timer_queue.h"
|
|
|
|
|
#include "net.h"
|
2026-04-07 09:18:43 +09:00
|
|
|
#include "debug_log.h"
|
2026-04-10 12:55:04 +09:00
|
|
|
#include "hardware/sync.h"
|
2026-04-06 17:36:41 +09:00
|
|
|
|
2026-04-07 07:34:24 +09:00
|
|
|
static timer_queue timers;
|
|
|
|
|
|
2026-04-06 20:01:22 +09:00
|
|
|
void dispatch_init() {
|
|
|
|
|
tusb_init();
|
|
|
|
|
net_init();
|
2026-04-07 09:18:43 +09:00
|
|
|
dlog("dispatch_init complete");
|
2026-04-06 20:01:22 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-07 07:34:24 +09:00
|
|
|
void dispatch_schedule_ms(uint32_t ms, std::function<void()> fn) {
|
|
|
|
|
timers.schedule_ms(ms, std::move(fn));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[[noreturn]] void dispatch_run(std::span<const handler_entry> handlers) {
|
2026-04-10 22:18:44 +09:00
|
|
|
std::unordered_map<int8_t, handler_fn> handler_map;
|
2026-04-06 17:36:41 +09:00
|
|
|
for (auto& entry : handlers) {
|
|
|
|
|
handler_map[entry.type_id] = entry.handle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static usb_cdc usb;
|
2026-04-06 20:22:40 +09:00
|
|
|
static static_vector<uint8_t, 256> usb_rx_buf;
|
2026-04-10 22:48:28 +09:00
|
|
|
static std::array<uint8_t, 1514> tx_buf;
|
2026-04-06 20:22:40 +09:00
|
|
|
|
2026-04-10 23:02:07 +09:00
|
|
|
net_set_handler([&](std::span<const uint8_t> payload, span_writer &out) -> msgpack::result<size_t> {
|
2026-04-07 21:36:50 +09:00
|
|
|
auto msg = try_decode(payload.data(), payload.size());
|
2026-04-10 22:18:44 +09:00
|
|
|
if (!msg) return 0;
|
2026-04-07 21:36:50 +09:00
|
|
|
auto it = handler_map.find(msg->type_id);
|
2026-04-10 22:18:44 +09:00
|
|
|
if (it == handler_map.end()) return 0;
|
|
|
|
|
return it->second(msg->message_id, msg->payload, out);
|
2026-04-07 21:36:50 +09:00
|
|
|
});
|
|
|
|
|
|
2026-04-06 17:36:41 +09:00
|
|
|
while (true) {
|
2026-04-10 12:55:04 +09:00
|
|
|
uint32_t save = save_and_disable_interrupts();
|
|
|
|
|
|
2026-04-07 12:09:18 +09:00
|
|
|
dlog_if_slow("tud_task", 1000, [&]{ tud_task(); });
|
|
|
|
|
dlog_if_slow("drain", 1000, [&]{ usb.drain(); });
|
|
|
|
|
dlog_if_slow("timers", 1000, [&]{ timers.run(); });
|
2026-04-10 22:48:28 +09:00
|
|
|
dlog_if_slow("net_poll", 1000, [&]{ net_poll(std::span{tx_buf}); });
|
2026-04-06 17:36:41 +09:00
|
|
|
|
|
|
|
|
while (tud_cdc_available()) {
|
|
|
|
|
uint8_t byte;
|
|
|
|
|
if (tud_cdc_read(&byte, 1) != 1) break;
|
|
|
|
|
|
2026-04-06 20:22:40 +09:00
|
|
|
usb_rx_buf.push_back(byte);
|
2026-04-06 17:36:41 +09:00
|
|
|
|
2026-04-06 20:22:40 +09:00
|
|
|
auto msg = try_decode(usb_rx_buf);
|
2026-04-06 17:36:41 +09:00
|
|
|
if (!msg) {
|
2026-04-06 20:22:40 +09:00
|
|
|
if (usb_rx_buf.full()) usb_rx_buf.clear();
|
2026-04-06 17:36:41 +09:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-06 20:22:40 +09:00
|
|
|
auto it = handler_map.find(msg->type_id);
|
2026-04-10 23:08:51 +09:00
|
|
|
if (it == handler_map.end()) { usb_rx_buf.clear(); continue; }
|
2026-04-10 23:02:07 +09:00
|
|
|
span_writer out(tx_buf);
|
|
|
|
|
auto resp = it->second(msg->message_id, msg->payload, out);
|
2026-04-10 23:08:51 +09:00
|
|
|
usb_rx_buf.clear();
|
2026-04-10 23:02:07 +09:00
|
|
|
if (!resp || *resp == 0) continue;
|
|
|
|
|
size_t resp_len = *resp;
|
|
|
|
|
if (resp_len <= usb.tx.free()) {
|
|
|
|
|
usb.send(std::span<const uint8_t>{tx_buf.data(), resp_len});
|
|
|
|
|
continue;
|
2026-04-06 20:22:40 +09:00
|
|
|
}
|
2026-04-10 23:02:07 +09:00
|
|
|
span_writer err_out(tx_buf);
|
|
|
|
|
auto err = encode_response_into(err_out, msg->message_id,
|
|
|
|
|
DeviceError{2, "response too large: " + std::to_string(resp_len)});
|
|
|
|
|
if (err) usb.send(std::span<const uint8_t>{tx_buf.data(), *err});
|
2026-04-06 20:22:40 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-10 12:55:04 +09:00
|
|
|
__wfi();
|
|
|
|
|
restore_interrupts(save);
|
2026-04-06 17:36:41 +09:00
|
|
|
}
|
|
|
|
|
}
|