Simplify handler signatures: return response struct, typed_handler does encoding
This commit is contained in:
@@ -4,9 +4,9 @@
|
|||||||
std::string_view firmware_name = "picomap";
|
std::string_view firmware_name = "picomap";
|
||||||
|
|
||||||
static constexpr handler_entry handlers[] = {
|
static constexpr handler_entry handlers[] = {
|
||||||
{RequestPICOBOOT::ext_id, handle_picoboot},
|
{RequestPICOBOOT::ext_id, typed_handler<RequestPICOBOOT, handle_picoboot>},
|
||||||
{RequestInfo::ext_id, handle_info},
|
{RequestInfo::ext_id, typed_handler<RequestInfo, handle_info>},
|
||||||
{RequestLog::ext_id, handle_log},
|
{RequestLog::ext_id, typed_handler<RequestLog, handle_log>},
|
||||||
};
|
};
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ size_t typed_handler(uint32_t message_id, std::span<const uint8_t> payload, span
|
|||||||
return encode_response_into(out, message_id, DeviceError{1, "decode request ext_id=" +
|
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<int>(r.error()))});
|
std::to_string(Req::ext_id) + ": msgpack error " + std::to_string(static_cast<int>(r.error()))});
|
||||||
}
|
}
|
||||||
return Fn(message_id, req, out);
|
return encode_response_into(out, message_id, Fn(req));
|
||||||
}
|
}
|
||||||
|
|
||||||
void dispatch_init();
|
void dispatch_init();
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <cstdint>
|
|
||||||
#include <span>
|
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include "wire.h"
|
#include "wire.h"
|
||||||
|
|
||||||
extern std::string_view firmware_name;
|
extern std::string_view firmware_name;
|
||||||
|
|
||||||
size_t handle_picoboot(uint32_t message_id, std::span<const uint8_t> payload, span_writer &out);
|
ResponsePICOBOOT handle_picoboot(const RequestPICOBOOT&);
|
||||||
size_t handle_info(uint32_t message_id, std::span<const uint8_t> payload, span_writer &out);
|
ResponseInfo handle_info(const RequestInfo&);
|
||||||
size_t handle_log(uint32_t message_id, std::span<const uint8_t> payload, span_writer &out);
|
ResponseLog handle_log(const RequestLog&);
|
||||||
|
|||||||
@@ -5,12 +5,12 @@
|
|||||||
#include "net.h"
|
#include "net.h"
|
||||||
#include "debug_log.h"
|
#include "debug_log.h"
|
||||||
|
|
||||||
size_t handle_picoboot(uint32_t message_id, std::span<const uint8_t>, span_writer &out) {
|
ResponsePICOBOOT handle_picoboot(const RequestPICOBOOT&) {
|
||||||
dispatch_schedule_ms(100, []{ reset_usb_boot(0, 1); });
|
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<const uint8_t>, span_writer &out) {
|
ResponseInfo handle_info(const RequestInfo&) {
|
||||||
ResponseInfo resp;
|
ResponseInfo resp;
|
||||||
pico_unique_board_id_t uid;
|
pico_unique_board_id_t uid;
|
||||||
pico_get_unique_board_id(&uid);
|
pico_get_unique_board_id(&uid);
|
||||||
@@ -19,12 +19,12 @@ size_t handle_info(uint32_t message_id, std::span<const uint8_t>, span_writer &o
|
|||||||
resp.mac = ns.mac;
|
resp.mac = ns.mac;
|
||||||
resp.ip = ns.ip;
|
resp.ip = ns.ip;
|
||||||
resp.firmware_name = firmware_name;
|
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<const uint8_t>, span_writer &out) {
|
ResponseLog handle_log(const RequestLog&) {
|
||||||
ResponseLog resp;
|
ResponseLog resp;
|
||||||
for (auto& e : dlog_drain())
|
for (auto& e : dlog_drain())
|
||||||
resp.entries.push_back(LogEntry{e.timestamp_us, std::move(e.message)});
|
resp.entries.push_back(LogEntry{e.timestamp_us, std::move(e.message)});
|
||||||
return encode_response_into(out, message_id, resp);
|
return resp;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -97,17 +97,17 @@ static const std::unordered_map<std::string_view, test_fn> tests = {
|
|||||||
{"discovery", test_discovery},
|
{"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);
|
auto it = tests.find(req.name);
|
||||||
if (it == tests.end())
|
if (it == tests.end())
|
||||||
return encode_response_into(out, message_id, ResponseTest{false, {"unknown test: " + req.name}});
|
return {false, {"unknown test: " + req.name}};
|
||||||
return encode_response_into(out, message_id, it->second());
|
return it->second();
|
||||||
}
|
}
|
||||||
|
|
||||||
static constexpr handler_entry handlers[] = {
|
static constexpr handler_entry handlers[] = {
|
||||||
{RequestPICOBOOT::ext_id, handle_picoboot},
|
{RequestPICOBOOT::ext_id, typed_handler<RequestPICOBOOT, handle_picoboot>},
|
||||||
{RequestInfo::ext_id, handle_info},
|
{RequestInfo::ext_id, typed_handler<RequestInfo, handle_info>},
|
||||||
{RequestLog::ext_id, handle_log},
|
{RequestLog::ext_id, typed_handler<RequestLog, handle_log>},
|
||||||
{RequestTest::ext_id, typed_handler<RequestTest, handle_test>},
|
{RequestTest::ext_id, typed_handler<RequestTest, handle_test>},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user