2026-04-04 23:16:25 +09:00
|
|
|
#pragma once
|
|
|
|
|
#include <array>
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <tuple>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include "msgpack.h"
|
|
|
|
|
#include "halfsiphash.h"
|
|
|
|
|
#include "static_vector.h"
|
|
|
|
|
|
|
|
|
|
struct Envelope {
|
|
|
|
|
static constexpr int8_t ext_id = 0;
|
|
|
|
|
uint32_t message_id;
|
|
|
|
|
uint32_t checksum;
|
|
|
|
|
std::vector<uint8_t> payload;
|
|
|
|
|
auto as_tuple() const { return std::tie(message_id, checksum, payload); }
|
|
|
|
|
auto as_tuple() { return std::tie(message_id, checksum, payload); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct DeviceError {
|
|
|
|
|
static constexpr int8_t ext_id = 1;
|
|
|
|
|
uint32_t code;
|
|
|
|
|
std::string message;
|
|
|
|
|
auto as_tuple() const { return std::tie(code, message); }
|
|
|
|
|
auto as_tuple() { return std::tie(code, message); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct RequestPICOBOOT {
|
|
|
|
|
static constexpr int8_t ext_id = 2;
|
|
|
|
|
auto as_tuple() const { return std::tie(); }
|
|
|
|
|
auto as_tuple() { return std::tie(); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct ResponsePICOBOOT {
|
|
|
|
|
static constexpr int8_t ext_id = 3;
|
|
|
|
|
auto as_tuple() const { return std::tie(); }
|
|
|
|
|
auto as_tuple() { return std::tie(); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct RequestInfo {
|
|
|
|
|
static constexpr int8_t ext_id = 4;
|
|
|
|
|
auto as_tuple() const { return std::tie(); }
|
|
|
|
|
auto as_tuple() { return std::tie(); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct ResponseInfo {
|
|
|
|
|
static constexpr int8_t ext_id = 5;
|
|
|
|
|
std::array<uint8_t, 8> board_id;
|
|
|
|
|
std::array<uint8_t, 6> mac;
|
2026-04-07 06:58:39 +09:00
|
|
|
std::array<uint8_t, 4> ip;
|
2026-04-06 20:09:30 +09:00
|
|
|
std::string firmware_name;
|
2026-04-07 06:58:39 +09:00
|
|
|
auto as_tuple() const { return std::tie(board_id, mac, ip, firmware_name); }
|
|
|
|
|
auto as_tuple() { return std::tie(board_id, mac, ip, firmware_name); }
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-07 09:18:43 +09:00
|
|
|
struct RequestLog {
|
|
|
|
|
static constexpr int8_t ext_id = 6;
|
|
|
|
|
auto as_tuple() const { return std::tie(); }
|
|
|
|
|
auto as_tuple() { return std::tie(); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct LogEntry {
|
|
|
|
|
uint32_t timestamp_us;
|
|
|
|
|
std::string message;
|
|
|
|
|
auto as_tuple() const { return std::tie(timestamp_us, message); }
|
|
|
|
|
auto as_tuple() { return std::tie(timestamp_us, message); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct ResponseLog {
|
|
|
|
|
static constexpr int8_t ext_id = 7;
|
|
|
|
|
std::vector<LogEntry> entries;
|
|
|
|
|
auto as_tuple() const { return std::tie(entries); }
|
|
|
|
|
auto as_tuple() { return std::tie(entries); }
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-07 06:58:39 +09:00
|
|
|
struct RequestTest {
|
|
|
|
|
static constexpr int8_t ext_id = 127;
|
|
|
|
|
std::string name;
|
|
|
|
|
auto as_tuple() const { return std::tie(name); }
|
|
|
|
|
auto as_tuple() { return std::tie(name); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct ResponseTest {
|
|
|
|
|
static constexpr int8_t ext_id = 126;
|
|
|
|
|
bool pass;
|
|
|
|
|
std::vector<std::string> messages;
|
|
|
|
|
auto as_tuple() const { return std::tie(pass, messages); }
|
|
|
|
|
auto as_tuple() { return std::tie(pass, messages); }
|
2026-04-04 23:16:25 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static constexpr uint8_t hash_key[8] = {};
|
|
|
|
|
|
|
|
|
|
struct DecodedMessage {
|
|
|
|
|
uint32_t message_id;
|
|
|
|
|
int8_t type_id;
|
2026-04-07 06:58:39 +09:00
|
|
|
std::vector<uint8_t> payload;
|
2026-04-04 23:16:25 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
inline std::vector<uint8_t> pack_envelope(uint32_t message_id, const std::vector<uint8_t> &payload) {
|
|
|
|
|
uint32_t checksum = halfsiphash::hash32(payload.data(), payload.size(), hash_key);
|
|
|
|
|
msgpack::packer p;
|
|
|
|
|
p.pack(Envelope{message_id, checksum, payload});
|
|
|
|
|
return p.get_payload();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
inline std::vector<uint8_t> encode_response(uint32_t message_id, const T &msg) {
|
|
|
|
|
msgpack::packer inner;
|
|
|
|
|
inner.pack(msg);
|
|
|
|
|
return pack_envelope(message_id, inner.get_payload());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline msgpack::result<DecodedMessage> try_decode(const uint8_t *data, size_t len) {
|
|
|
|
|
msgpack::parser p(data, static_cast<int>(len));
|
|
|
|
|
|
|
|
|
|
Envelope env;
|
|
|
|
|
auto r = msgpack::unpack(p, env);
|
|
|
|
|
if (!r) return std::unexpected(r.error());
|
|
|
|
|
|
|
|
|
|
uint32_t expected = halfsiphash::hash32(env.payload.data(), env.payload.size(), hash_key);
|
|
|
|
|
if (env.checksum != expected) return std::unexpected(msgpack::error_code::invalid);
|
|
|
|
|
|
|
|
|
|
msgpack::parser inner(env.payload.data(), static_cast<int>(env.payload.size()));
|
|
|
|
|
if (!inner.is_ext()) return std::unexpected(msgpack::error_code::type_error);
|
|
|
|
|
auto ext = inner.get_ext();
|
|
|
|
|
if (!ext) return std::unexpected(ext.error());
|
|
|
|
|
|
2026-04-07 06:58:39 +09:00
|
|
|
auto& [type_id, ext_data] = *ext;
|
|
|
|
|
return DecodedMessage{env.message_id, type_id,
|
|
|
|
|
std::vector<uint8_t>(reinterpret_cast<const uint8_t*>(ext_data.data()),
|
|
|
|
|
reinterpret_cast<const uint8_t*>(ext_data.data()) + ext_data.size())};
|
2026-04-04 23:16:25 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <size_t N>
|
|
|
|
|
inline msgpack::result<DecodedMessage> try_decode(const static_vector<uint8_t, N> &buf) {
|
|
|
|
|
return try_decode(buf.data(), buf.size());
|
|
|
|
|
}
|
2026-04-07 06:58:39 +09:00
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
inline msgpack::result<T> decode_response(const uint8_t *data, size_t len) {
|
|
|
|
|
msgpack::parser p(data, static_cast<int>(len));
|
|
|
|
|
|
|
|
|
|
Envelope env;
|
|
|
|
|
auto r = msgpack::unpack(p, env);
|
|
|
|
|
if (!r) return std::unexpected(r.error());
|
|
|
|
|
|
|
|
|
|
uint32_t expected = halfsiphash::hash32(env.payload.data(), env.payload.size(), hash_key);
|
|
|
|
|
if (env.checksum != expected) return std::unexpected(msgpack::error_code::invalid);
|
|
|
|
|
|
|
|
|
|
msgpack::parser inner(env.payload.data(), static_cast<int>(env.payload.size()));
|
|
|
|
|
T out;
|
|
|
|
|
auto r2 = msgpack::unpack(inner, out);
|
|
|
|
|
if (!r2) return std::unexpected(r2.error());
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline std::vector<uint8_t> encode_request(uint32_t message_id, const auto &msg) {
|
|
|
|
|
msgpack::packer inner;
|
|
|
|
|
inner.pack(msg);
|
|
|
|
|
return pack_envelope(message_id, inner.get_payload());
|
|
|
|
|
}
|