2026-04-06 17:36:41 +09:00
|
|
|
#include "dispatch.h"
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
#include "pico/stdlib.h"
|
2026-04-06 20:22:40 +09:00
|
|
|
#include "pico/bootrom.h"
|
2026-04-06 17:36:41 +09:00
|
|
|
#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-06 20:01:22 +09:00
|
|
|
void dispatch_init() {
|
|
|
|
|
tusb_init();
|
|
|
|
|
net_init();
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-06 20:22:40 +09:00
|
|
|
[[noreturn]] void dispatch_run(std::span<const handler_entry> handlers,
|
|
|
|
|
std::span<const w6300::socket_id> sockets) {
|
|
|
|
|
std::unordered_map<int8_t, std::vector<std::vector<uint8_t>> (*)(uint32_t)> 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;
|
|
|
|
|
static timer_queue timers;
|
2026-04-06 20:22:40 +09:00
|
|
|
static static_vector<uint8_t, 256> usb_rx_buf;
|
|
|
|
|
|
|
|
|
|
for (auto sn : sockets) {
|
|
|
|
|
w6300::set_socket_io_mode(sn, w6300::sock_io_mode::nonblock);
|
|
|
|
|
}
|
2026-04-06 17:36:41 +09:00
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
tud_task();
|
|
|
|
|
|
|
|
|
|
usb.drain();
|
|
|
|
|
timers.run();
|
|
|
|
|
|
|
|
|
|
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
|
|
|
usb_rx_buf.clear();
|
|
|
|
|
|
|
|
|
|
auto it = handler_map.find(msg->type_id);
|
|
|
|
|
if (it != handler_map.end()) {
|
|
|
|
|
for (auto& response : it->second(msg->message_id)) {
|
|
|
|
|
usb.send(response);
|
|
|
|
|
}
|
|
|
|
|
if (msg->type_id == RequestPICOBOOT::ext_id) {
|
|
|
|
|
sleep_ms(100);
|
|
|
|
|
reset_usb_boot(0, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto sn : sockets) {
|
|
|
|
|
static uint8_t udp_rx_buf[512];
|
|
|
|
|
w6300::ip_address src_addr = {};
|
|
|
|
|
w6300::port_num src_port{0};
|
|
|
|
|
|
|
|
|
|
auto result = w6300::recvfrom(sn, std::span{udp_rx_buf}, src_addr, src_port);
|
|
|
|
|
if (!result) continue;
|
|
|
|
|
|
|
|
|
|
auto msg = try_decode(udp_rx_buf, *result);
|
|
|
|
|
if (!msg) continue;
|
2026-04-06 17:36:41 +09:00
|
|
|
|
|
|
|
|
auto it = handler_map.find(msg->type_id);
|
|
|
|
|
if (it != handler_map.end()) {
|
2026-04-06 20:22:40 +09:00
|
|
|
for (auto& response : it->second(msg->message_id)) {
|
|
|
|
|
w6300::sendto(sn, std::span<const uint8_t>{response}, src_addr, src_port);
|
|
|
|
|
}
|
2026-04-06 17:36:41 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
__wfi();
|
|
|
|
|
}
|
|
|
|
|
}
|