From 58db392bf3174c47dedf1f8e9bcfc3ffd035302d Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Fri, 10 Apr 2026 22:21:31 +0900 Subject: [PATCH] Simplify handler signatures: return response struct, typed_handler does encoding --- firmware/firmware.cpp | 6 +++--- firmware/include/dispatch.h | 2 +- firmware/include/handlers.h | 8 +++----- firmware/lib/handlers.cpp | 12 ++++++------ firmware/test.cpp | 12 ++++++------ 5 files changed, 19 insertions(+), 21 deletions(-) diff --git a/firmware/firmware.cpp b/firmware/firmware.cpp index ee13de7..5f2675a 100644 --- a/firmware/firmware.cpp +++ b/firmware/firmware.cpp @@ -4,9 +4,9 @@ std::string_view firmware_name = "picomap"; static constexpr handler_entry handlers[] = { - {RequestPICOBOOT::ext_id, handle_picoboot}, - {RequestInfo::ext_id, handle_info}, - {RequestLog::ext_id, handle_log}, + {RequestPICOBOOT::ext_id, typed_handler}, + {RequestInfo::ext_id, typed_handler}, + {RequestLog::ext_id, typed_handler}, }; int main() { diff --git a/firmware/include/dispatch.h b/firmware/include/dispatch.h index e185ca9..34a8dc5 100644 --- a/firmware/include/dispatch.h +++ b/firmware/include/dispatch.h @@ -21,7 +21,7 @@ size_t typed_handler(uint32_t message_id, std::span payload, span return encode_response_into(out, message_id, DeviceError{1, "decode request ext_id=" + std::to_string(Req::ext_id) + ": msgpack error " + std::to_string(static_cast(r.error()))}); } - return Fn(message_id, req, out); + return encode_response_into(out, message_id, Fn(req)); } void dispatch_init(); diff --git a/firmware/include/handlers.h b/firmware/include/handlers.h index f18368f..63d5b61 100644 --- a/firmware/include/handlers.h +++ b/firmware/include/handlers.h @@ -1,11 +1,9 @@ #pragma once -#include -#include #include #include "wire.h" extern std::string_view firmware_name; -size_t handle_picoboot(uint32_t message_id, std::span payload, span_writer &out); -size_t handle_info(uint32_t message_id, std::span payload, span_writer &out); -size_t handle_log(uint32_t message_id, std::span payload, span_writer &out); +ResponsePICOBOOT handle_picoboot(const RequestPICOBOOT&); +ResponseInfo handle_info(const RequestInfo&); +ResponseLog handle_log(const RequestLog&); diff --git a/firmware/lib/handlers.cpp b/firmware/lib/handlers.cpp index 707239a..f916a8c 100644 --- a/firmware/lib/handlers.cpp +++ b/firmware/lib/handlers.cpp @@ -5,12 +5,12 @@ #include "net.h" #include "debug_log.h" -size_t handle_picoboot(uint32_t message_id, std::span, span_writer &out) { +ResponsePICOBOOT handle_picoboot(const RequestPICOBOOT&) { dispatch_schedule_ms(100, []{ reset_usb_boot(0, 1); }); - return encode_response_into(out, message_id, ResponsePICOBOOT{}); + return {}; } -size_t handle_info(uint32_t message_id, std::span, span_writer &out) { +ResponseInfo handle_info(const RequestInfo&) { ResponseInfo resp; pico_unique_board_id_t uid; pico_get_unique_board_id(&uid); @@ -19,12 +19,12 @@ size_t handle_info(uint32_t message_id, std::span, span_writer &o resp.mac = ns.mac; resp.ip = ns.ip; resp.firmware_name = firmware_name; - return encode_response_into(out, message_id, resp); + return resp; } -size_t handle_log(uint32_t message_id, std::span, span_writer &out) { +ResponseLog handle_log(const RequestLog&) { ResponseLog resp; for (auto& e : dlog_drain()) resp.entries.push_back(LogEntry{e.timestamp_us, std::move(e.message)}); - return encode_response_into(out, message_id, resp); + return resp; } diff --git a/firmware/test.cpp b/firmware/test.cpp index f698eab..4e6005e 100644 --- a/firmware/test.cpp +++ b/firmware/test.cpp @@ -97,17 +97,17 @@ static const std::unordered_map tests = { {"discovery", test_discovery}, }; -static size_t handle_test(uint32_t message_id, const RequestTest& req, span_writer &out) { +static ResponseTest handle_test(const RequestTest& req) { auto it = tests.find(req.name); if (it == tests.end()) - return encode_response_into(out, message_id, ResponseTest{false, {"unknown test: " + req.name}}); - return encode_response_into(out, message_id, it->second()); + return {false, {"unknown test: " + req.name}}; + return it->second(); } static constexpr handler_entry handlers[] = { - {RequestPICOBOOT::ext_id, handle_picoboot}, - {RequestInfo::ext_id, handle_info}, - {RequestLog::ext_id, handle_log}, + {RequestPICOBOOT::ext_id, typed_handler}, + {RequestInfo::ext_id, typed_handler}, + {RequestLog::ext_id, typed_handler}, {RequestTest::ext_id, typed_handler}, };