31 lines
1007 B
C++
31 lines
1007 B
C++
#pragma once
|
|
#include <cstdint>
|
|
#include <span>
|
|
#include <vector>
|
|
#include "wire.h"
|
|
#include "w6300.h"
|
|
|
|
using handler_fn = std::vector<std::vector<uint8_t>> (*)(uint32_t message_id, std::span<const uint8_t> payload);
|
|
|
|
struct handler_entry {
|
|
int8_t type_id;
|
|
handler_fn handle;
|
|
};
|
|
|
|
template <typename Req, auto Fn>
|
|
std::vector<std::vector<uint8_t>> typed_handler(uint32_t message_id, std::span<const uint8_t> payload) {
|
|
msgpack::parser p(payload.data(), static_cast<int>(payload.size()));
|
|
Req req;
|
|
auto tup = req.as_tuple();
|
|
auto r = msgpack::unpack(p, tup);
|
|
if (!r) {
|
|
return {encode_response(message_id, DeviceError{1, "decode request ext_id=" +
|
|
std::to_string(Req::ext_id) + ": msgpack error " + std::to_string(static_cast<int>(r.error()))})};
|
|
}
|
|
return Fn(message_id, req);
|
|
}
|
|
|
|
void dispatch_init();
|
|
[[noreturn]] void dispatch_run(std::span<const handler_entry> handlers,
|
|
std::span<const w6300::socket_id> sockets = {});
|