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

@@ -2,13 +2,29 @@
#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;
std::vector<std::vector<uint8_t>> (*handle)(uint32_t message_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 = {});

View File

@@ -1,10 +1,11 @@
#pragma once
#include <cstdint>
#include <span>
#include <string_view>
#include <vector>
#include "wire.h"
extern std::string_view firmware_name;
std::vector<std::vector<uint8_t>> handle_picoboot(uint32_t message_id);
std::vector<std::vector<uint8_t>> handle_info(uint32_t message_id);
std::vector<std::vector<uint8_t>> handle_picoboot(uint32_t message_id, std::span<const uint8_t> payload);
std::vector<std::vector<uint8_t>> handle_info(uint32_t message_id, std::span<const uint8_t> payload);

View File

@@ -367,6 +367,18 @@ public:
pack_result pack(const std::vector<uint8_t> &v) { return pack_bin(v); }
template <typename T>
requires (!std::is_same_v<T, uint8_t>)
pack_result pack(const std::vector<T> &v) {
auto r = pack_array(v.size());
if (!r) return r;
for (auto& elem : v) {
r = r->get().pack(elem);
if (!r) return r;
}
return r;
}
template <size_t N>
pack_result pack(const std::array<uint8_t, N> &v) { return pack_bin(v); }
@@ -744,6 +756,21 @@ inline result<parser> unpack(const parser &p, std::vector<uint8_t> &out) {
return p.next();
}
template <typename T>
requires (!std::is_same_v<T, uint8_t>)
result<parser> unpack(const parser &p, std::vector<T> &out) {
auto cnt = p.count();
if (!cnt) return std::unexpected(cnt.error());
out.resize(*cnt);
result<parser> cur = p.first_item();
for (size_t i = 0; i < *cnt; i++) {
if (!cur) return cur;
cur = unpack(*cur, out[i]);
}
if (!cur) return cur;
return p.next();
}
template <typename... Ts, size_t... Is>
result<parser> unpack_tuple_elements(const parser &p, std::tuple<Ts...> &t, std::index_sequence<Is...>) {
result<parser> cur = p.first_item();

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());
}