Zero-copy ethernet TX: encode directly into prepend_buffer, add info/test skills

This commit is contained in:
Ian Gulliver
2026-04-12 07:50:20 +09:00
parent 846d6bb201
commit 761b740c10
6 changed files with 40 additions and 12 deletions

View File

@@ -0,0 +1,7 @@
---
name: info
description: Query device info from connected Picos. Use when the user says "info", "check devices", or wants to verify devices are responding.
user-invocable: true
---
Run `go run ./cmd/picomap/ info` from the project root. This queries all connected devices and prints board ID, MAC, IP, and firmware name.

View File

@@ -0,0 +1,10 @@
---
name: test
description: Run device tests on connected Picos. Use when the user says "test", "run tests", or wants to verify firmware behavior.
user-invocable: true
---
Run `go run ./cmd/picomap/ test all` from the project root. This runs all registered tests on the test device.
To list available tests: `go run ./cmd/picomap/ test list`
To run a specific test: `go run ./cmd/picomap/ test run <name>`

View File

@@ -6,17 +6,17 @@
#include <span> #include <span>
#include "wire.h" #include "wire.h"
#include "timer_queue.h" #include "timer_queue.h"
#include "net.h"
struct responder { struct responder {
uint32_t message_id; uint32_t message_id;
std::function<void(std::span<const uint8_t>)> send; send_fn send;
template <typename T> template <typename T>
void respond(const T& msg) const { void respond(const T& msg) const {
uint8_t buf[1024]; send([&](span_writer& out) {
span_writer out(buf, sizeof(buf)); return encode_response_into(out, message_id, msg);
auto r = encode_response_into(out, message_id, msg); });
if (r) send({buf, *r});
} }
}; };

View File

@@ -13,8 +13,11 @@ struct net_state {
ipv4::ip4_addr ip; ipv4::ip4_addr ip;
}; };
using encode_fn = std::function<msgpack::result<size_t>(span_writer&)>;
using send_fn = std::function<void(encode_fn)>;
using net_handler = std::function<void(std::span<const uint8_t> payload, using net_handler = std::function<void(std::span<const uint8_t> payload,
std::function<void(std::span<const uint8_t>)> send)>; send_fn send)>;
using net_frame_callback = std::function<bool(std::span<const uint8_t> frame)>; using net_frame_callback = std::function<bool(std::span<const uint8_t> frame)>;

View File

@@ -45,7 +45,7 @@ bool dispatch_cancel_timer(timer_handle h) {
static static_vector<uint8_t, 4096> usb_rx_buf; static static_vector<uint8_t, 4096> usb_rx_buf;
static std::array<uint8_t, 4096> tx_buf; static std::array<uint8_t, 4096> tx_buf;
auto dispatch_msg = [&](const DecodedMessage& msg, std::function<void(std::span<const uint8_t>)> send) { auto dispatch_msg = [&](const DecodedMessage& msg, send_fn send) {
auto it = handler_map.find(msg.type_id); auto it = handler_map.find(msg.type_id);
if (it == handler_map.end()) { if (it == handler_map.end()) {
dlogf("dispatch: unknown type_id %d", msg.type_id); dlogf("dispatch: unknown type_id %d", msg.type_id);
@@ -56,7 +56,7 @@ bool dispatch_cancel_timer(timer_handle h) {
}; };
net_set_handler([&](std::span<const uint8_t> payload, net_set_handler([&](std::span<const uint8_t> payload,
std::function<void(std::span<const uint8_t>)> send) { send_fn send) {
auto msg = try_decode(payload.data(), payload.size()); auto msg = try_decode(payload.data(), payload.size());
if (!msg) return; if (!msg) return;
dispatch_msg(*msg, std::move(send)); dispatch_msg(*msg, std::move(send));
@@ -82,7 +82,12 @@ bool dispatch_cancel_timer(timer_handle h) {
continue; continue;
} }
dispatch_msg(*msg, [&](std::span<const uint8_t> data) { dispatch_msg(*msg, [&](encode_fn encode) {
uint8_t buf[4096];
span_writer out(buf, sizeof(buf));
auto r = encode(out);
if (!r) return;
std::span<const uint8_t> data{buf, *r};
if (data.size() <= usb.tx.free()) { if (data.size() <= usb.tx.free()) {
if (!usb.send(data)) if (!usb.send(data))
dlogf("usb send failed: %zu bytes, %u free", data.size(), usb.tx.free()); dlogf("usb send failed: %zu bytes, %u free", data.size(), usb.tx.free());

View File

@@ -52,11 +52,14 @@ static void handle_udp(std::span<const uint8_t> frame, span_writer& tx) {
uint16_t dst_port = uhdr->src_port; uint16_t dst_port = uhdr->src_port;
msg_handler(pb.remaining().subspan(0, payload_len), msg_handler(pb.remaining().subspan(0, payload_len),
[dst_mac, dst_ip, dst_port](std::span<const uint8_t> resp_data) { [dst_mac, dst_ip, dst_port](encode_fn encode) {
prepend_buffer<4096> buf; prepend_buffer<4096> buf;
buf.append_copy(resp_data); span_writer out(buf.payload_ptr(), 2048);
auto r = encode(out);
if (!r) return;
buf.append(*r);
udp::prepend(buf, dst_mac, state.mac, state.ip, dst_ip, udp::prepend(buf, dst_mac, state.mac, state.ip, dst_ip,
PICOMAP_PORT, dst_port, resp_data.size()); PICOMAP_PORT, dst_port, *r);
net_send_raw(buf.span()); net_send_raw(buf.span());
}); });
} }