Switch to IPv4 zeroconf, add test framework with discovery test, fix serial enumeration

This commit is contained in:
Ian Gulliver
2026-04-07 06:58:39 +09:00
parent b8c0e6be66
commit e60479bad8
14 changed files with 308 additions and 33 deletions

View File

@@ -47,10 +47,25 @@ 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;
std::array<uint8_t, 4> ip;
std::string firmware_name;
auto as_tuple() const { return std::tie(board_id, mac, link_local, firmware_name); }
auto as_tuple() { return std::tie(board_id, mac, link_local, firmware_name); }
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); }
};
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); }
};
static constexpr uint8_t hash_key[8] = {};
@@ -58,6 +73,7 @@ static constexpr uint8_t hash_key[8] = {};
struct DecodedMessage {
uint32_t message_id;
int8_t type_id;
std::vector<uint8_t> payload;
};
inline std::vector<uint8_t> pack_envelope(uint32_t message_id, const std::vector<uint8_t> &payload) {
@@ -89,10 +105,37 @@ inline msgpack::result<DecodedMessage> try_decode(const uint8_t *data, size_t le
auto ext = inner.get_ext();
if (!ext) return std::unexpected(ext.error());
return DecodedMessage{env.message_id, std::get<0>(*ext)};
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())};
}
template <size_t N>
inline msgpack::result<DecodedMessage> try_decode(const static_vector<uint8_t, N> &buf) {
return try_decode(buf.data(), buf.size());
}
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());
}