Split net stack into eth/arp/ipv4/icmp, deferred handler responses, ping tests
This commit is contained in:
@@ -31,12 +31,18 @@ void dispatch_schedule_ms(uint32_t ms, std::function<void()> fn) {
|
||||
static static_vector<uint8_t, 256> usb_rx_buf;
|
||||
static std::array<uint8_t, 1514> tx_buf;
|
||||
|
||||
net_set_handler([&](std::span<const uint8_t> payload, span_writer &out) -> msgpack::result<size_t> {
|
||||
auto dispatch_msg = [&](const DecodedMessage& msg, std::function<void(std::span<const uint8_t>)> send) {
|
||||
auto it = handler_map.find(msg.type_id);
|
||||
if (it == handler_map.end()) return;
|
||||
responder resp{msg.message_id, std::move(send)};
|
||||
it->second(resp, msg.payload);
|
||||
};
|
||||
|
||||
net_set_handler([&](std::span<const uint8_t> payload,
|
||||
std::function<void(std::span<const uint8_t>)> send) {
|
||||
auto msg = try_decode(payload.data(), payload.size());
|
||||
if (!msg) return 0;
|
||||
auto it = handler_map.find(msg->type_id);
|
||||
if (it == handler_map.end()) return 0;
|
||||
return it->second(msg->message_id, msg->payload, out);
|
||||
if (!msg) return;
|
||||
dispatch_msg(*msg, std::move(send));
|
||||
});
|
||||
|
||||
while (true) {
|
||||
@@ -59,21 +65,18 @@ void dispatch_schedule_ms(uint32_t ms, std::function<void()> fn) {
|
||||
continue;
|
||||
}
|
||||
|
||||
auto it = handler_map.find(msg->type_id);
|
||||
if (it == handler_map.end()) { usb_rx_buf.clear(); continue; }
|
||||
span_writer out(tx_buf);
|
||||
auto resp = it->second(msg->message_id, msg->payload, out);
|
||||
dispatch_msg(*msg, [&](std::span<const uint8_t> data) {
|
||||
if (data.size() <= usb.tx.free()) {
|
||||
usb.send(data);
|
||||
} else {
|
||||
uint8_t err_buf[256];
|
||||
span_writer err_out(err_buf, sizeof(err_buf));
|
||||
auto err = encode_response_into(err_out, msg->message_id,
|
||||
DeviceError{2, "response too large: " + std::to_string(data.size())});
|
||||
if (err) usb.send(std::span<const uint8_t>{err_buf, *err});
|
||||
}
|
||||
});
|
||||
usb_rx_buf.clear();
|
||||
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;
|
||||
}
|
||||
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});
|
||||
}
|
||||
|
||||
__wfi();
|
||||
|
||||
Reference in New Issue
Block a user