98 lines
3.0 KiB
C++
98 lines
3.0 KiB
C++
#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;
|
|
std::array<uint8_t, 16> link_local;
|
|
auto as_tuple() const { return std::tie(board_id, mac, link_local); }
|
|
auto as_tuple() { return std::tie(board_id, mac, link_local); }
|
|
};
|
|
|
|
static constexpr uint8_t hash_key[8] = {};
|
|
|
|
struct DecodedMessage {
|
|
uint32_t message_id;
|
|
int8_t type_id;
|
|
};
|
|
|
|
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());
|
|
|
|
return DecodedMessage{env.message_id, std::get<0>(*ext)};
|
|
}
|
|
|
|
template <size_t N>
|
|
inline msgpack::result<DecodedMessage> try_decode(const static_vector<uint8_t, N> &buf) {
|
|
return try_decode(buf.data(), buf.size());
|
|
}
|