Move C++ tree into firmware/
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
#include <array>
|
||||
#include "timer_queue.h"
|
||||
|
||||
void dhcp_start(timer_queue& timers, const std::array<uint8_t, 6>& mac);
|
||||
@@ -0,0 +1,79 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
|
||||
namespace halfsiphash {
|
||||
|
||||
namespace detail {
|
||||
|
||||
constexpr uint32_t rotl(uint32_t x, int b) {
|
||||
return (x << b) | (x >> (32 - b));
|
||||
}
|
||||
|
||||
constexpr uint32_t load_le32(const uint8_t *p) {
|
||||
return static_cast<uint32_t>(p[0])
|
||||
| (static_cast<uint32_t>(p[1]) << 8)
|
||||
| (static_cast<uint32_t>(p[2]) << 16)
|
||||
| (static_cast<uint32_t>(p[3]) << 24);
|
||||
}
|
||||
|
||||
inline void store_le32(uint8_t *p, uint32_t v) {
|
||||
p[0] = static_cast<uint8_t>(v);
|
||||
p[1] = static_cast<uint8_t>(v >> 8);
|
||||
p[2] = static_cast<uint8_t>(v >> 16);
|
||||
p[3] = static_cast<uint8_t>(v >> 24);
|
||||
}
|
||||
|
||||
inline void sipround(uint32_t &v0, uint32_t &v1, uint32_t &v2, uint32_t &v3) {
|
||||
v0 += v1; v1 = rotl(v1, 5); v1 ^= v0; v0 = rotl(v0, 16);
|
||||
v2 += v3; v3 = rotl(v3, 8); v3 ^= v2;
|
||||
v0 += v3; v3 = rotl(v3, 7); v3 ^= v0;
|
||||
v2 += v1; v1 = rotl(v1, 13); v1 ^= v2; v2 = rotl(v2, 16);
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
// Compute HalfSipHash-2-4 with an 8-byte key, returning a 32-bit hash.
|
||||
inline uint32_t hash32(const uint8_t *data, size_t len, const uint8_t key[8]) {
|
||||
using namespace detail;
|
||||
|
||||
uint32_t k0 = load_le32(key);
|
||||
uint32_t k1 = load_le32(key + 4);
|
||||
|
||||
uint32_t v0 = 0 ^ k0;
|
||||
uint32_t v1 = 0 ^ k1;
|
||||
uint32_t v2 = UINT32_C(0x6c796765) ^ k0;
|
||||
uint32_t v3 = UINT32_C(0x74656462) ^ k1;
|
||||
|
||||
const uint8_t *end = data + len - (len % 4);
|
||||
for (const uint8_t *p = data; p != end; p += 4) {
|
||||
uint32_t m = load_le32(p);
|
||||
v3 ^= m;
|
||||
sipround(v0, v1, v2, v3);
|
||||
sipround(v0, v1, v2, v3);
|
||||
v0 ^= m;
|
||||
}
|
||||
|
||||
uint32_t b = static_cast<uint32_t>(len) << 24;
|
||||
switch (len & 3) {
|
||||
case 3: b |= static_cast<uint32_t>(end[2]) << 16; [[fallthrough]];
|
||||
case 2: b |= static_cast<uint32_t>(end[1]) << 8; [[fallthrough]];
|
||||
case 1: b |= static_cast<uint32_t>(end[0]); break;
|
||||
case 0: break;
|
||||
}
|
||||
|
||||
v3 ^= b;
|
||||
sipround(v0, v1, v2, v3);
|
||||
sipround(v0, v1, v2, v3);
|
||||
v0 ^= b;
|
||||
|
||||
v2 ^= 0xff;
|
||||
sipround(v0, v1, v2, v3);
|
||||
sipround(v0, v1, v2, v3);
|
||||
sipround(v0, v1, v2, v3);
|
||||
sipround(v0, v1, v2, v3);
|
||||
|
||||
return v1 ^ v3;
|
||||
}
|
||||
|
||||
} // namespace halfsiphash
|
||||
@@ -0,0 +1,792 @@
|
||||
#pragma once
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <iterator>
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
#include <string_view>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
enum class error_code {
|
||||
overflow,
|
||||
empty,
|
||||
lack,
|
||||
invalid,
|
||||
type_error,
|
||||
};
|
||||
|
||||
namespace format {
|
||||
constexpr uint8_t POSITIVE_FIXINT_MIN = 0x00;
|
||||
constexpr uint8_t POSITIVE_FIXINT_MAX = 0x7F;
|
||||
constexpr uint8_t FIXMAP_MIN = 0x80;
|
||||
constexpr uint8_t FIXMAP_MAX = 0x8F;
|
||||
constexpr uint8_t FIXARRAY_MIN = 0x90;
|
||||
constexpr uint8_t FIXARRAY_MAX = 0x9F;
|
||||
constexpr uint8_t FIXSTR_MIN = 0xA0;
|
||||
constexpr uint8_t FIXSTR_MAX = 0xBF;
|
||||
constexpr uint8_t NEGATIVE_FIXINT_MIN = 0xE0;
|
||||
constexpr uint8_t NEGATIVE_FIXINT_MAX = 0xFF;
|
||||
|
||||
constexpr uint8_t NIL = 0xC0;
|
||||
constexpr uint8_t NEVER_USED = 0xC1;
|
||||
constexpr uint8_t FALSE = 0xC2;
|
||||
constexpr uint8_t TRUE = 0xC3;
|
||||
constexpr uint8_t BIN8 = 0xC4;
|
||||
constexpr uint8_t BIN16 = 0xC5;
|
||||
constexpr uint8_t BIN32 = 0xC6;
|
||||
constexpr uint8_t EXT8 = 0xC7;
|
||||
constexpr uint8_t EXT16 = 0xC8;
|
||||
constexpr uint8_t EXT32 = 0xC9;
|
||||
constexpr uint8_t FLOAT32 = 0xCA;
|
||||
constexpr uint8_t FLOAT64 = 0xCB;
|
||||
constexpr uint8_t UINT8 = 0xCC;
|
||||
constexpr uint8_t UINT16 = 0xCD;
|
||||
constexpr uint8_t UINT32 = 0xCE;
|
||||
constexpr uint8_t UINT64 = 0xCF;
|
||||
constexpr uint8_t INT8 = 0xD0;
|
||||
constexpr uint8_t INT16 = 0xD1;
|
||||
constexpr uint8_t INT32 = 0xD2;
|
||||
constexpr uint8_t INT64 = 0xD3;
|
||||
constexpr uint8_t FIXEXT1 = 0xD4;
|
||||
constexpr uint8_t FIXEXT2 = 0xD5;
|
||||
constexpr uint8_t FIXEXT4 = 0xD6;
|
||||
constexpr uint8_t FIXEXT8 = 0xD7;
|
||||
constexpr uint8_t FIXEXT16 = 0xD8;
|
||||
constexpr uint8_t STR8 = 0xD9;
|
||||
constexpr uint8_t STR16 = 0xDA;
|
||||
constexpr uint8_t STR32 = 0xDB;
|
||||
constexpr uint8_t ARRAY16 = 0xDC;
|
||||
constexpr uint8_t ARRAY32 = 0xDD;
|
||||
constexpr uint8_t MAP16 = 0xDE;
|
||||
constexpr uint8_t MAP32 = 0xDF;
|
||||
|
||||
constexpr bool is_positive_fixint(uint8_t b) { return b <= POSITIVE_FIXINT_MAX; }
|
||||
constexpr bool is_fixmap(uint8_t b) { return b >= FIXMAP_MIN && b <= FIXMAP_MAX; }
|
||||
constexpr bool is_fixarray(uint8_t b) { return b >= FIXARRAY_MIN && b <= FIXARRAY_MAX; }
|
||||
constexpr bool is_fixstr(uint8_t b) { return b >= FIXSTR_MIN && b <= FIXSTR_MAX; }
|
||||
constexpr bool is_negative_fixint(uint8_t b) { return b >= NEGATIVE_FIXINT_MIN; }
|
||||
} // namespace format
|
||||
|
||||
template <typename T>
|
||||
using result = std::expected<T, error_code>;
|
||||
|
||||
template <typename T>
|
||||
result<T> body_number(const uint8_t *p, int size) {
|
||||
if (size < 1 + static_cast<int>(sizeof(T))) {
|
||||
return std::unexpected(error_code::lack);
|
||||
}
|
||||
if constexpr (sizeof(T) == 1) {
|
||||
return static_cast<T>(p[1]);
|
||||
} else if constexpr (sizeof(T) == 2) {
|
||||
return static_cast<T>((p[1] << 8) | p[2]);
|
||||
} else if constexpr (sizeof(T) == 4) {
|
||||
uint8_t buf[] = {p[4], p[3], p[2], p[1]};
|
||||
T val;
|
||||
__builtin_memcpy(&val, buf, sizeof(T));
|
||||
return val;
|
||||
} else if constexpr (sizeof(T) == 8) {
|
||||
uint8_t buf[] = {p[8], p[7], p[6], p[5], p[4], p[3], p[2], p[1]};
|
||||
T val;
|
||||
__builtin_memcpy(&val, buf, sizeof(T));
|
||||
return val;
|
||||
} else {
|
||||
return std::unexpected(error_code::invalid);
|
||||
}
|
||||
}
|
||||
|
||||
struct body_info {
|
||||
int header; // bytes before the body (includes format byte + length fields + ext type byte)
|
||||
uint32_t body; // body size in bytes (0 for containers, computed for variable-length)
|
||||
};
|
||||
|
||||
inline result<body_info> get_body_info(const uint8_t *p, int size) {
|
||||
if (size < 1) return std::unexpected(error_code::empty);
|
||||
uint8_t b = p[0];
|
||||
|
||||
using namespace format;
|
||||
|
||||
if (is_positive_fixint(b)) return body_info{1, 0};
|
||||
if (is_negative_fixint(b)) return body_info{1, 0};
|
||||
if (is_fixmap(b)) return body_info{1, 0}; // container
|
||||
if (is_fixarray(b)) return body_info{1, 0}; // container
|
||||
if (is_fixstr(b)) return body_info{1, static_cast<uint32_t>(b & 0x1F)};
|
||||
|
||||
switch (b) {
|
||||
case NIL: case FALSE: case TRUE:
|
||||
return body_info{1, 0};
|
||||
case NEVER_USED:
|
||||
return std::unexpected(error_code::invalid);
|
||||
|
||||
case BIN8: { auto n = body_number<uint8_t>(p, size); if (!n) return std::unexpected(n.error()); return body_info{1+1, *n}; }
|
||||
case BIN16: { auto n = body_number<uint16_t>(p, size); if (!n) return std::unexpected(n.error()); return body_info{1+2, *n}; }
|
||||
case BIN32: { auto n = body_number<uint32_t>(p, size); if (!n) return std::unexpected(n.error()); return body_info{1+4, *n}; }
|
||||
|
||||
case EXT8: { auto n = body_number<uint8_t>(p, size); if (!n) return std::unexpected(n.error()); return body_info{1+1+1, *n}; }
|
||||
case EXT16: { auto n = body_number<uint16_t>(p, size); if (!n) return std::unexpected(n.error()); return body_info{1+2+1, *n}; }
|
||||
case EXT32: { auto n = body_number<uint32_t>(p, size); if (!n) return std::unexpected(n.error()); return body_info{1+4+1, *n}; }
|
||||
|
||||
case FLOAT32: return body_info{1, 4};
|
||||
case FLOAT64: return body_info{1, 8};
|
||||
case UINT8: return body_info{1, 1};
|
||||
case UINT16: return body_info{1, 2};
|
||||
case UINT32: return body_info{1, 4};
|
||||
case UINT64: return body_info{1, 8};
|
||||
case INT8: return body_info{1, 1};
|
||||
case INT16: return body_info{1, 2};
|
||||
case INT32: return body_info{1, 4};
|
||||
case INT64: return body_info{1, 8};
|
||||
|
||||
case FIXEXT1: return body_info{1+1, 1};
|
||||
case FIXEXT2: return body_info{1+1, 2};
|
||||
case FIXEXT4: return body_info{1+1, 4};
|
||||
case FIXEXT8: return body_info{1+1, 8};
|
||||
case FIXEXT16: return body_info{1+1, 16};
|
||||
|
||||
case STR8: { auto n = body_number<uint8_t>(p, size); if (!n) return std::unexpected(n.error()); return body_info{1+1, *n}; }
|
||||
case STR16: { auto n = body_number<uint16_t>(p, size); if (!n) return std::unexpected(n.error()); return body_info{1+2, *n}; }
|
||||
case STR32: { auto n = body_number<uint32_t>(p, size); if (!n) return std::unexpected(n.error()); return body_info{1+4, *n}; }
|
||||
|
||||
case ARRAY16: case ARRAY32:
|
||||
case MAP16: case MAP32:
|
||||
return body_info{1 + (b == ARRAY16 || b == MAP16 ? 2 : 4), 0}; // container
|
||||
|
||||
default:
|
||||
return std::unexpected(error_code::invalid);
|
||||
}
|
||||
}
|
||||
|
||||
class packer {
|
||||
public:
|
||||
using buffer = std::vector<std::uint8_t>;
|
||||
|
||||
private:
|
||||
std::shared_ptr<buffer> m_buffer;
|
||||
|
||||
template <typename T> void push_big_endian(T n) {
|
||||
auto p = reinterpret_cast<std::uint8_t *>(&n) + (sizeof(T) - 1);
|
||||
for (size_t i = 0; i < sizeof(T); ++i, --p) {
|
||||
m_buffer->push_back(*p);
|
||||
}
|
||||
}
|
||||
|
||||
template <class Range> void push(const Range &r) {
|
||||
m_buffer->insert(m_buffer->end(), std::begin(r), std::end(r));
|
||||
}
|
||||
|
||||
public:
|
||||
packer() : m_buffer(std::make_shared<buffer>()) {}
|
||||
packer(const std::shared_ptr<buffer> &buf) : m_buffer(buf) {}
|
||||
|
||||
packer(const packer &) = delete;
|
||||
packer &operator=(const packer &) = delete;
|
||||
|
||||
using pack_result = result<std::reference_wrapper<packer>>;
|
||||
|
||||
pack_result pack_nil() {
|
||||
m_buffer->push_back(format::NIL);
|
||||
return *this;
|
||||
}
|
||||
|
||||
pack_result pack_bool(bool v) {
|
||||
m_buffer->push_back(v ? format::TRUE : format::FALSE);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
pack_result pack_integer(T n) {
|
||||
if constexpr (std::is_signed_v<T>) {
|
||||
if (n >= 0 && n <= 0x7F) {
|
||||
m_buffer->push_back(static_cast<uint8_t>(n));
|
||||
} else if (n >= -32 && n < 0) {
|
||||
m_buffer->push_back(static_cast<uint8_t>(n)); // negative fixint
|
||||
} else if (n >= std::numeric_limits<int8_t>::min() && n <= std::numeric_limits<int8_t>::max()) {
|
||||
m_buffer->push_back(format::INT8);
|
||||
m_buffer->push_back(static_cast<uint8_t>(n));
|
||||
} else if (n >= std::numeric_limits<int16_t>::min() && n <= std::numeric_limits<int16_t>::max()) {
|
||||
m_buffer->push_back(format::INT16);
|
||||
push_big_endian(static_cast<int16_t>(n));
|
||||
} else if (n >= std::numeric_limits<int32_t>::min() && n <= std::numeric_limits<int32_t>::max()) {
|
||||
m_buffer->push_back(format::INT32);
|
||||
push_big_endian(static_cast<int32_t>(n));
|
||||
} else {
|
||||
m_buffer->push_back(format::INT64);
|
||||
push_big_endian(static_cast<int64_t>(n));
|
||||
}
|
||||
} else {
|
||||
if (n <= 0x7F) {
|
||||
m_buffer->push_back(static_cast<uint8_t>(n));
|
||||
} else if (n <= std::numeric_limits<uint8_t>::max()) {
|
||||
m_buffer->push_back(format::UINT8);
|
||||
m_buffer->push_back(static_cast<uint8_t>(n));
|
||||
} else if (n <= std::numeric_limits<uint16_t>::max()) {
|
||||
m_buffer->push_back(format::UINT16);
|
||||
push_big_endian(static_cast<uint16_t>(n));
|
||||
} else if (n <= std::numeric_limits<uint32_t>::max()) {
|
||||
m_buffer->push_back(format::UINT32);
|
||||
push_big_endian(static_cast<uint32_t>(n));
|
||||
} else {
|
||||
m_buffer->push_back(format::UINT64);
|
||||
push_big_endian(static_cast<uint64_t>(n));
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
pack_result pack_float(float n) {
|
||||
m_buffer->push_back(format::FLOAT32);
|
||||
push_big_endian(n);
|
||||
return *this;
|
||||
}
|
||||
|
||||
pack_result pack_double(double n) {
|
||||
m_buffer->push_back(format::FLOAT64);
|
||||
push_big_endian(n);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <class Range>
|
||||
pack_result pack_str(const Range &r) {
|
||||
auto sz = static_cast<size_t>(std::distance(std::begin(r), std::end(r)));
|
||||
if (sz < 32) {
|
||||
m_buffer->push_back(format::FIXSTR_MIN | static_cast<uint8_t>(sz));
|
||||
} else if (sz <= std::numeric_limits<uint8_t>::max()) {
|
||||
m_buffer->push_back(format::STR8);
|
||||
m_buffer->push_back(static_cast<uint8_t>(sz));
|
||||
} else if (sz <= std::numeric_limits<uint16_t>::max()) {
|
||||
m_buffer->push_back(format::STR16);
|
||||
push_big_endian(static_cast<uint16_t>(sz));
|
||||
} else if (sz <= std::numeric_limits<uint32_t>::max()) {
|
||||
m_buffer->push_back(format::STR32);
|
||||
push_big_endian(static_cast<uint32_t>(sz));
|
||||
} else {
|
||||
return std::unexpected(error_code::overflow);
|
||||
}
|
||||
push(r);
|
||||
return *this;
|
||||
}
|
||||
|
||||
pack_result pack_str(const char *s) {
|
||||
return pack_str(std::string_view(s));
|
||||
}
|
||||
|
||||
template <class Range>
|
||||
pack_result pack_bin(const Range &r) {
|
||||
auto sz = static_cast<size_t>(std::distance(std::begin(r), std::end(r)));
|
||||
if (sz <= std::numeric_limits<uint8_t>::max()) {
|
||||
m_buffer->push_back(format::BIN8);
|
||||
m_buffer->push_back(static_cast<uint8_t>(sz));
|
||||
} else if (sz <= std::numeric_limits<uint16_t>::max()) {
|
||||
m_buffer->push_back(format::BIN16);
|
||||
push_big_endian(static_cast<uint16_t>(sz));
|
||||
} else if (sz <= std::numeric_limits<uint32_t>::max()) {
|
||||
m_buffer->push_back(format::BIN32);
|
||||
push_big_endian(static_cast<uint32_t>(sz));
|
||||
} else {
|
||||
return std::unexpected(error_code::overflow);
|
||||
}
|
||||
push(r);
|
||||
return *this;
|
||||
}
|
||||
|
||||
pack_result pack_array(size_t n) {
|
||||
if (n <= 15) {
|
||||
m_buffer->push_back(format::FIXARRAY_MIN | static_cast<uint8_t>(n));
|
||||
} else if (n <= std::numeric_limits<uint16_t>::max()) {
|
||||
m_buffer->push_back(format::ARRAY16);
|
||||
push_big_endian(static_cast<uint16_t>(n));
|
||||
} else if (n <= std::numeric_limits<uint32_t>::max()) {
|
||||
m_buffer->push_back(format::ARRAY32);
|
||||
push_big_endian(static_cast<uint32_t>(n));
|
||||
} else {
|
||||
return std::unexpected(error_code::overflow);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
pack_result pack_map(size_t n) {
|
||||
if (n <= 15) {
|
||||
m_buffer->push_back(format::FIXMAP_MIN | static_cast<uint8_t>(n));
|
||||
} else if (n <= std::numeric_limits<uint16_t>::max()) {
|
||||
m_buffer->push_back(format::MAP16);
|
||||
push_big_endian(static_cast<uint16_t>(n));
|
||||
} else if (n <= std::numeric_limits<uint32_t>::max()) {
|
||||
m_buffer->push_back(format::MAP32);
|
||||
push_big_endian(static_cast<uint32_t>(n));
|
||||
} else {
|
||||
return std::unexpected(error_code::overflow);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <class Range>
|
||||
pack_result pack_ext(char type, const Range &r) {
|
||||
auto sz = static_cast<size_t>(std::distance(std::begin(r), std::end(r)));
|
||||
|
||||
switch (sz) {
|
||||
case 1: m_buffer->push_back(format::FIXEXT1); break;
|
||||
case 2: m_buffer->push_back(format::FIXEXT2); break;
|
||||
case 4: m_buffer->push_back(format::FIXEXT4); break;
|
||||
case 8: m_buffer->push_back(format::FIXEXT8); break;
|
||||
case 16: m_buffer->push_back(format::FIXEXT16); break;
|
||||
default:
|
||||
if (sz <= std::numeric_limits<uint8_t>::max()) {
|
||||
m_buffer->push_back(format::EXT8);
|
||||
m_buffer->push_back(static_cast<uint8_t>(sz));
|
||||
} else if (sz <= std::numeric_limits<uint16_t>::max()) {
|
||||
m_buffer->push_back(format::EXT16);
|
||||
push_big_endian(static_cast<uint16_t>(sz));
|
||||
} else if (sz <= std::numeric_limits<uint32_t>::max()) {
|
||||
m_buffer->push_back(format::EXT32);
|
||||
push_big_endian(static_cast<uint32_t>(sz));
|
||||
} else {
|
||||
return std::unexpected(error_code::overflow);
|
||||
}
|
||||
}
|
||||
m_buffer->push_back(static_cast<uint8_t>(type));
|
||||
push(r);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
requires std::is_integral_v<T> && (!std::is_same_v<T, bool>)
|
||||
pack_result pack(T n) { return pack_integer(n); }
|
||||
|
||||
pack_result pack(bool v) { return pack_bool(v); }
|
||||
pack_result pack(float v) { return pack_float(v); }
|
||||
pack_result pack(double v) { return pack_double(v); }
|
||||
pack_result pack(const char *v) { return pack_str(v); }
|
||||
pack_result pack(std::string_view v) { return pack_str(v); }
|
||||
pack_result pack(const std::string &v) { return pack_str(v); }
|
||||
|
||||
pack_result pack(const std::vector<uint8_t> &v) { return pack_bin(v); }
|
||||
|
||||
template <size_t N>
|
||||
pack_result pack(const std::array<uint8_t, N> &v) { return pack_bin(v); }
|
||||
|
||||
template <typename... Ts>
|
||||
pack_result pack(const std::tuple<Ts...> &t) {
|
||||
auto r = pack_array(sizeof...(Ts));
|
||||
if (!r) return r;
|
||||
return pack_tuple_elements(t, std::index_sequence_for<Ts...>{});
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
requires requires(const T &v) { { T::ext_id } -> std::convertible_to<int8_t>; v.as_tuple(); }
|
||||
pack_result pack(const T &v) {
|
||||
packer inner;
|
||||
auto r = inner.pack(v.as_tuple());
|
||||
if (!r) return r;
|
||||
return pack_ext(T::ext_id, inner.get_payload());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
requires (requires(const T &v) { v.as_tuple(); } && !requires { { T::ext_id } -> std::convertible_to<int8_t>; })
|
||||
pack_result pack(const T &v) {
|
||||
return pack(v.as_tuple());
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename Tuple, size_t... Is>
|
||||
pack_result pack_tuple_elements(const Tuple &t, std::index_sequence<Is...>) {
|
||||
pack_result r = *this;
|
||||
((r = r ? r->get().pack(std::get<Is>(t)) : r), ...);
|
||||
return r;
|
||||
}
|
||||
|
||||
public:
|
||||
const buffer &get_payload() const { return *m_buffer; }
|
||||
};
|
||||
|
||||
class parser {
|
||||
const uint8_t *m_p = nullptr;
|
||||
int m_size = 0;
|
||||
|
||||
result<uint8_t> header_byte() const {
|
||||
if (m_size < 1) return std::unexpected(error_code::empty);
|
||||
return m_p[0];
|
||||
}
|
||||
|
||||
public:
|
||||
parser() = default;
|
||||
|
||||
parser(const std::vector<uint8_t> &v)
|
||||
: m_p(v.data()), m_size(static_cast<int>(v.size())) {}
|
||||
|
||||
parser(const uint8_t *p, int size)
|
||||
: m_p(p), m_size(size < 0 ? 0 : size) {}
|
||||
|
||||
bool is_empty() const { return m_size == 0; }
|
||||
const uint8_t *data() const { return m_p; }
|
||||
int size() const { return m_size; }
|
||||
|
||||
result<parser> advance(int n) const {
|
||||
if (n > m_size) return std::unexpected(error_code::lack);
|
||||
return parser(m_p + n, m_size - n);
|
||||
}
|
||||
|
||||
result<parser> next() const {
|
||||
auto hdr = header_byte();
|
||||
if (!hdr) return std::unexpected(hdr.error());
|
||||
|
||||
if (is_array()) {
|
||||
auto info = get_body_info(m_p, m_size);
|
||||
if (!info) return std::unexpected(info.error());
|
||||
auto cnt = count();
|
||||
if (!cnt) return std::unexpected(cnt.error());
|
||||
auto cur = advance(info->header);
|
||||
if (!cur) return std::unexpected(cur.error());
|
||||
for (uint32_t i = 0; i < *cnt; ++i) {
|
||||
auto n = cur->next();
|
||||
if (!n) return std::unexpected(n.error());
|
||||
cur = *n;
|
||||
}
|
||||
return *cur;
|
||||
} else if (is_map()) {
|
||||
auto info = get_body_info(m_p, m_size);
|
||||
if (!info) return std::unexpected(info.error());
|
||||
auto cnt = count();
|
||||
if (!cnt) return std::unexpected(cnt.error());
|
||||
auto cur = advance(info->header);
|
||||
if (!cur) return std::unexpected(cur.error());
|
||||
for (uint32_t i = 0; i < *cnt; ++i) {
|
||||
auto k = cur->next();
|
||||
if (!k) return std::unexpected(k.error());
|
||||
cur = *k;
|
||||
auto v = cur->next();
|
||||
if (!v) return std::unexpected(v.error());
|
||||
cur = *v;
|
||||
}
|
||||
return *cur;
|
||||
} else {
|
||||
auto info = get_body_info(m_p, m_size);
|
||||
if (!info) return std::unexpected(info.error());
|
||||
auto total = info->header + static_cast<int>(info->body);
|
||||
return advance(total);
|
||||
}
|
||||
}
|
||||
|
||||
bool is_nil() const {
|
||||
auto h = header_byte();
|
||||
return h && *h == format::NIL;
|
||||
}
|
||||
|
||||
bool is_bool() const {
|
||||
auto h = header_byte();
|
||||
return h && (*h == format::TRUE || *h == format::FALSE);
|
||||
}
|
||||
|
||||
bool is_number() const {
|
||||
auto h = header_byte();
|
||||
if (!h) return false;
|
||||
uint8_t b = *h;
|
||||
if (format::is_positive_fixint(b)) return true;
|
||||
if (format::is_negative_fixint(b)) return true;
|
||||
return b >= format::FLOAT32 && b <= format::INT64;
|
||||
}
|
||||
|
||||
bool is_string() const {
|
||||
auto h = header_byte();
|
||||
if (!h) return false;
|
||||
uint8_t b = *h;
|
||||
if (format::is_fixstr(b)) return true;
|
||||
return b == format::STR8 || b == format::STR16 || b == format::STR32;
|
||||
}
|
||||
|
||||
bool is_binary() const {
|
||||
auto h = header_byte();
|
||||
if (!h) return false;
|
||||
uint8_t b = *h;
|
||||
return b == format::BIN8 || b == format::BIN16 || b == format::BIN32;
|
||||
}
|
||||
|
||||
bool is_ext() const {
|
||||
auto h = header_byte();
|
||||
if (!h) return false;
|
||||
uint8_t b = *h;
|
||||
return (b >= format::FIXEXT1 && b <= format::FIXEXT16) ||
|
||||
b == format::EXT8 || b == format::EXT16 || b == format::EXT32;
|
||||
}
|
||||
|
||||
bool is_array() const {
|
||||
auto h = header_byte();
|
||||
if (!h) return false;
|
||||
uint8_t b = *h;
|
||||
if (format::is_fixarray(b)) return true;
|
||||
return b == format::ARRAY16 || b == format::ARRAY32;
|
||||
}
|
||||
|
||||
bool is_map() const {
|
||||
auto h = header_byte();
|
||||
if (!h) return false;
|
||||
uint8_t b = *h;
|
||||
if (format::is_fixmap(b)) return true;
|
||||
return b == format::MAP16 || b == format::MAP32;
|
||||
}
|
||||
|
||||
|
||||
result<bool> get_bool() const {
|
||||
auto h = header_byte();
|
||||
if (!h) return std::unexpected(h.error());
|
||||
if (*h == format::TRUE) return true;
|
||||
if (*h == format::FALSE) return false;
|
||||
return std::unexpected(error_code::type_error);
|
||||
}
|
||||
|
||||
result<std::string_view> get_string() const {
|
||||
auto h = header_byte();
|
||||
if (!h) return std::unexpected(h.error());
|
||||
uint8_t b = *h;
|
||||
size_t offset, len;
|
||||
if (format::is_fixstr(b)) {
|
||||
len = b & 0x1F;
|
||||
offset = 1;
|
||||
} else if (b == format::STR8) {
|
||||
auto n = body_number<uint8_t>(m_p, m_size);
|
||||
if (!n) return std::unexpected(n.error());
|
||||
len = *n; offset = 1 + 1;
|
||||
} else if (b == format::STR16) {
|
||||
auto n = body_number<uint16_t>(m_p, m_size);
|
||||
if (!n) return std::unexpected(n.error());
|
||||
len = *n; offset = 1 + 2;
|
||||
} else if (b == format::STR32) {
|
||||
auto n = body_number<uint32_t>(m_p, m_size);
|
||||
if (!n) return std::unexpected(n.error());
|
||||
len = *n; offset = 1 + 4;
|
||||
} else {
|
||||
return std::unexpected(error_code::type_error);
|
||||
}
|
||||
if (static_cast<int>(offset + len) > m_size) {
|
||||
return std::unexpected(error_code::lack);
|
||||
}
|
||||
return std::string_view(reinterpret_cast<const char *>(m_p + offset), len);
|
||||
}
|
||||
|
||||
result<std::string_view> get_binary_view() const {
|
||||
auto h = header_byte();
|
||||
if (!h) return std::unexpected(h.error());
|
||||
uint8_t b = *h;
|
||||
size_t offset, len;
|
||||
|
||||
if (b == format::BIN8) {
|
||||
auto n = body_number<uint8_t>(m_p, m_size);
|
||||
if (!n) return std::unexpected(n.error());
|
||||
len = *n; offset = 1 + 1;
|
||||
} else if (b == format::BIN16) {
|
||||
auto n = body_number<uint16_t>(m_p, m_size);
|
||||
if (!n) return std::unexpected(n.error());
|
||||
len = *n; offset = 1 + 2;
|
||||
} else if (b == format::BIN32) {
|
||||
auto n = body_number<uint32_t>(m_p, m_size);
|
||||
if (!n) return std::unexpected(n.error());
|
||||
len = *n; offset = 1 + 4;
|
||||
} else {
|
||||
return std::unexpected(error_code::type_error);
|
||||
}
|
||||
if (static_cast<int>(offset + len) > m_size) {
|
||||
return std::unexpected(error_code::lack);
|
||||
}
|
||||
return std::string_view(reinterpret_cast<const char *>(m_p + offset), len);
|
||||
}
|
||||
|
||||
result<std::tuple<int8_t, std::string_view>> get_ext() const {
|
||||
auto h = header_byte();
|
||||
if (!h) return std::unexpected(h.error());
|
||||
uint8_t b = *h;
|
||||
int8_t ext_type;
|
||||
size_t data_offset, data_len;
|
||||
|
||||
switch (b) {
|
||||
case format::FIXEXT1: ext_type = m_p[1]; data_offset = 2; data_len = 1; break;
|
||||
case format::FIXEXT2: ext_type = m_p[1]; data_offset = 2; data_len = 2; break;
|
||||
case format::FIXEXT4: ext_type = m_p[1]; data_offset = 2; data_len = 4; break;
|
||||
case format::FIXEXT8: ext_type = m_p[1]; data_offset = 2; data_len = 8; break;
|
||||
case format::FIXEXT16: ext_type = m_p[1]; data_offset = 2; data_len = 16; break;
|
||||
case format::EXT8: {
|
||||
auto n = body_number<uint8_t>(m_p, m_size);
|
||||
if (!n) return std::unexpected(n.error());
|
||||
ext_type = m_p[2]; data_offset = 3; data_len = *n;
|
||||
break;
|
||||
}
|
||||
case format::EXT16: {
|
||||
auto n = body_number<uint16_t>(m_p, m_size);
|
||||
if (!n) return std::unexpected(n.error());
|
||||
ext_type = m_p[3]; data_offset = 4; data_len = *n;
|
||||
break;
|
||||
}
|
||||
case format::EXT32: {
|
||||
auto n = body_number<uint32_t>(m_p, m_size);
|
||||
if (!n) return std::unexpected(n.error());
|
||||
ext_type = m_p[5]; data_offset = 6; data_len = *n;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return std::unexpected(error_code::type_error);
|
||||
}
|
||||
if (static_cast<int>(data_offset + data_len) > m_size) {
|
||||
return std::unexpected(error_code::lack);
|
||||
}
|
||||
return std::tuple{ext_type,
|
||||
std::string_view(reinterpret_cast<const char *>(m_p + data_offset), data_len)};
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
result<T> get_number() const {
|
||||
auto h = header_byte();
|
||||
if (!h) return std::unexpected(h.error());
|
||||
uint8_t b = *h;
|
||||
|
||||
if (format::is_positive_fixint(b)) return static_cast<T>(b);
|
||||
if (format::is_negative_fixint(b)) return static_cast<T>(static_cast<int8_t>(b));
|
||||
|
||||
switch (b) {
|
||||
case format::UINT8: { auto n = body_number<uint8_t>(m_p, m_size); if (!n) return std::unexpected(n.error()); return static_cast<T>(*n); }
|
||||
case format::UINT16: { auto n = body_number<uint16_t>(m_p, m_size); if (!n) return std::unexpected(n.error()); return static_cast<T>(*n); }
|
||||
case format::UINT32: { auto n = body_number<uint32_t>(m_p, m_size); if (!n) return std::unexpected(n.error()); return static_cast<T>(*n); }
|
||||
case format::UINT64: { auto n = body_number<uint64_t>(m_p, m_size); if (!n) return std::unexpected(n.error()); return static_cast<T>(*n); }
|
||||
case format::INT8: { auto n = body_number<int8_t>(m_p, m_size); if (!n) return std::unexpected(n.error()); return static_cast<T>(*n); }
|
||||
case format::INT16: { auto n = body_number<int16_t>(m_p, m_size); if (!n) return std::unexpected(n.error()); return static_cast<T>(*n); }
|
||||
case format::INT32: { auto n = body_number<int32_t>(m_p, m_size); if (!n) return std::unexpected(n.error()); return static_cast<T>(*n); }
|
||||
case format::INT64: { auto n = body_number<int64_t>(m_p, m_size); if (!n) return std::unexpected(n.error()); return static_cast<T>(*n); }
|
||||
case format::FLOAT32: { auto n = body_number<float>(m_p, m_size); if (!n) return std::unexpected(n.error()); return static_cast<T>(*n); }
|
||||
case format::FLOAT64: { auto n = body_number<double>(m_p, m_size); if (!n) return std::unexpected(n.error()); return static_cast<T>(*n); }
|
||||
default:
|
||||
return std::unexpected(error_code::type_error);
|
||||
}
|
||||
}
|
||||
|
||||
result<uint32_t> count() const {
|
||||
auto h = header_byte();
|
||||
if (!h) return std::unexpected(h.error());
|
||||
uint8_t b = *h;
|
||||
|
||||
if (format::is_fixarray(b)) return static_cast<uint32_t>(b & 0x0F);
|
||||
if (format::is_fixmap(b)) return static_cast<uint32_t>(b & 0x0F);
|
||||
|
||||
switch (b) {
|
||||
case format::ARRAY16: { auto n = body_number<uint16_t>(m_p, m_size); if (!n) return std::unexpected(n.error()); return static_cast<uint32_t>(*n); }
|
||||
case format::ARRAY32: { auto n = body_number<uint32_t>(m_p, m_size); if (!n) return std::unexpected(n.error()); return *n; }
|
||||
case format::MAP16: { auto n = body_number<uint16_t>(m_p, m_size); if (!n) return std::unexpected(n.error()); return static_cast<uint32_t>(*n); }
|
||||
case format::MAP32: { auto n = body_number<uint32_t>(m_p, m_size); if (!n) return std::unexpected(n.error()); return *n; }
|
||||
default:
|
||||
return std::unexpected(error_code::type_error);
|
||||
}
|
||||
}
|
||||
|
||||
result<parser> first_item() const {
|
||||
if (!is_array() && !is_map()) return std::unexpected(error_code::type_error);
|
||||
auto info = get_body_info(m_p, m_size);
|
||||
if (!info) return std::unexpected(info.error());
|
||||
return advance(info->header);
|
||||
}
|
||||
|
||||
parser operator[](int index) const {
|
||||
auto cur = first_item();
|
||||
if (!cur) return {};
|
||||
for (int i = 0; i < index; ++i) {
|
||||
auto n = cur->next();
|
||||
if (!n) return {};
|
||||
cur = *n;
|
||||
}
|
||||
return *cur;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
requires std::is_integral_v<T> && (!std::is_same_v<T, bool>)
|
||||
result<parser> unpack(const parser &p, T &out) {
|
||||
auto v = p.get_number<T>();
|
||||
if (!v) return std::unexpected(v.error());
|
||||
out = *v;
|
||||
return p.next();
|
||||
}
|
||||
|
||||
inline result<parser> unpack(const parser &p, bool &out) {
|
||||
auto v = p.get_bool();
|
||||
if (!v) return std::unexpected(v.error());
|
||||
out = *v;
|
||||
return p.next();
|
||||
}
|
||||
|
||||
inline result<parser> unpack(const parser &p, std::string_view &out) {
|
||||
auto v = p.get_string();
|
||||
if (!v) return std::unexpected(v.error());
|
||||
out = *v;
|
||||
return p.next();
|
||||
}
|
||||
|
||||
inline result<parser> unpack(const parser &p, std::string &out) {
|
||||
auto v = p.get_string();
|
||||
if (!v) return std::unexpected(v.error());
|
||||
out = std::string(v->data(), v->size());
|
||||
return p.next();
|
||||
}
|
||||
|
||||
template <size_t N>
|
||||
result<parser> unpack(const parser &p, std::array<uint8_t, N> &out) {
|
||||
auto v = p.get_binary_view();
|
||||
if (!v) return std::unexpected(v.error());
|
||||
if (v->size() != N) return std::unexpected(error_code::type_error);
|
||||
std::copy(v->begin(), v->end(), out.begin());
|
||||
return p.next();
|
||||
}
|
||||
|
||||
inline result<parser> unpack(const parser &p, std::vector<uint8_t> &out) {
|
||||
auto v = p.get_binary_view();
|
||||
if (!v) return std::unexpected(v.error());
|
||||
out.assign(v->begin(), v->end());
|
||||
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();
|
||||
if (!cur) return cur;
|
||||
((cur = cur ? unpack(*cur, std::get<Is>(t)) : cur), ...);
|
||||
return cur;
|
||||
}
|
||||
|
||||
template <typename... Ts>
|
||||
result<parser> unpack(const parser &p, std::tuple<Ts...> &t) {
|
||||
auto cnt = p.count();
|
||||
if (!cnt) return std::unexpected(cnt.error());
|
||||
if (*cnt != sizeof...(Ts)) return std::unexpected(error_code::type_error);
|
||||
auto r = unpack_tuple_elements(p, t, std::index_sequence_for<Ts...>{});
|
||||
if (!r) return r;
|
||||
return p.next();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
requires (requires(T &v) { v.as_tuple(); } && !requires { { T::ext_id } -> std::convertible_to<int8_t>; })
|
||||
result<parser> unpack(const parser &p, T &out) {
|
||||
auto tup = out.as_tuple();
|
||||
auto cnt = p.count();
|
||||
if (!cnt) return std::unexpected(cnt.error());
|
||||
if (*cnt != std::tuple_size_v<decltype(tup)>) return std::unexpected(error_code::type_error);
|
||||
auto r = unpack_tuple_elements(p, tup, std::make_index_sequence<std::tuple_size_v<decltype(tup)>>{});
|
||||
if (!r) return r;
|
||||
return p.next();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
requires requires(T &v) { { T::ext_id } -> std::convertible_to<int8_t>; v.as_tuple(); }
|
||||
result<parser> unpack(const parser &p, T &out) {
|
||||
auto ext = p.get_ext();
|
||||
if (!ext) return std::unexpected(ext.error());
|
||||
auto [ext_type, ext_data] = *ext;
|
||||
if (ext_type != T::ext_id) return std::unexpected(error_code::type_error);
|
||||
parser inner(reinterpret_cast<const uint8_t *>(ext_data.data()),
|
||||
static_cast<int>(ext_data.size()));
|
||||
auto tup = out.as_tuple();
|
||||
auto r = unpack_tuple_elements(inner, tup, std::make_index_sequence<std::tuple_size_v<decltype(tup)>>{});
|
||||
if (!r) return r;
|
||||
return p.next();
|
||||
}
|
||||
|
||||
} // namespace msgpack
|
||||
@@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
bool net_init();
|
||||
@@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <span>
|
||||
|
||||
template <uint16_t N>
|
||||
struct ring_buffer {
|
||||
std::array<uint8_t, N> data = {};
|
||||
uint16_t head = 0;
|
||||
uint16_t tail = 0;
|
||||
|
||||
uint16_t used() const { return tail - head; }
|
||||
uint16_t free() const { return N - used(); }
|
||||
bool empty() const { return head == tail; }
|
||||
|
||||
void push(std::span<const uint8_t> src) {
|
||||
if (src.size() > free()) return;
|
||||
for (auto b : src)
|
||||
data[(tail++) % N] = b;
|
||||
}
|
||||
|
||||
uint16_t peek(std::span<uint8_t> dst) const {
|
||||
uint16_t len = dst.size() < used() ? dst.size() : used();
|
||||
for (uint16_t i = 0; i < len; i++)
|
||||
dst[i] = data[(head + i) % N];
|
||||
return len;
|
||||
}
|
||||
|
||||
void consume(uint16_t len) {
|
||||
head += len;
|
||||
if (head >= N) {
|
||||
head -= N;
|
||||
tail -= N;
|
||||
}
|
||||
}
|
||||
|
||||
std::span<const uint8_t> read_contiguous() const {
|
||||
uint16_t offset = head % N;
|
||||
uint16_t contig = N - offset;
|
||||
uint16_t pending = used();
|
||||
uint16_t len = pending < contig ? pending : contig;
|
||||
return {data.data() + offset, len};
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,58 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <new>
|
||||
#include <utility>
|
||||
|
||||
template <typename T, int N>
|
||||
struct sorted_list {
|
||||
struct node {
|
||||
alignas(T) uint8_t storage[sizeof(T)];
|
||||
node* next = nullptr;
|
||||
|
||||
T& value() { return *reinterpret_cast<T*>(storage); }
|
||||
const T& value() const { return *reinterpret_cast<const T*>(storage); }
|
||||
};
|
||||
|
||||
node nodes[N];
|
||||
node* head = nullptr;
|
||||
node* free_head = &nodes[0];
|
||||
|
||||
sorted_list() {
|
||||
for (int i = 0; i < N - 1; i++) nodes[i].next = &nodes[i + 1];
|
||||
nodes[N - 1].next = nullptr;
|
||||
}
|
||||
|
||||
bool empty() const { return head == nullptr; }
|
||||
bool full() const { return free_head == nullptr; }
|
||||
|
||||
T& front() { return head->value(); }
|
||||
const T& front() const { return head->value(); }
|
||||
|
||||
void insert(T value) {
|
||||
if (full()) return;
|
||||
node* n = free_head;
|
||||
free_head = n->next;
|
||||
new (n->storage) T(std::move(value));
|
||||
|
||||
if (!head || n->value() < head->value()) {
|
||||
n->next = head;
|
||||
head = n;
|
||||
return;
|
||||
}
|
||||
|
||||
node* cur = head;
|
||||
while (cur->next && !(n->value() < cur->next->value()))
|
||||
cur = cur->next;
|
||||
n->next = cur->next;
|
||||
cur->next = n;
|
||||
}
|
||||
|
||||
void pop_front() {
|
||||
if (empty()) return;
|
||||
node* n = head;
|
||||
head = n->next;
|
||||
n->value().~T();
|
||||
n->next = free_head;
|
||||
free_head = n;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
|
||||
template <typename T, size_t Capacity>
|
||||
class static_vector {
|
||||
T m_data[Capacity];
|
||||
size_t m_size = 0;
|
||||
|
||||
public:
|
||||
void push_back(const T &v) {
|
||||
if (m_size < Capacity) m_data[m_size++] = v;
|
||||
}
|
||||
|
||||
void clear() { m_size = 0; }
|
||||
|
||||
size_t size() const { return m_size; }
|
||||
size_t capacity() const { return Capacity; }
|
||||
bool full() const { return m_size >= Capacity; }
|
||||
bool empty() const { return m_size == 0; }
|
||||
|
||||
T *data() { return m_data; }
|
||||
const T *data() const { return m_data; }
|
||||
|
||||
T &operator[](size_t i) { return m_data[i]; }
|
||||
const T &operator[](size_t i) const { return m_data[i]; }
|
||||
|
||||
T *begin() { return m_data; }
|
||||
T *end() { return m_data + m_size; }
|
||||
const T *begin() const { return m_data; }
|
||||
const T *end() const { return m_data + m_size; }
|
||||
};
|
||||
@@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
#include <functional>
|
||||
#include "pico/time.h"
|
||||
#include "sorted_list.h"
|
||||
|
||||
struct timer_entry {
|
||||
absolute_time_t when;
|
||||
std::function<void()> fn;
|
||||
};
|
||||
|
||||
inline bool operator<(const timer_entry& a, const timer_entry& b) {
|
||||
return absolute_time_diff_us(b.when, a.when) < 0;
|
||||
}
|
||||
|
||||
struct timer_queue {
|
||||
sorted_list<timer_entry, 16> queue;
|
||||
alarm_id_t alarm = -1;
|
||||
|
||||
void schedule(absolute_time_t when, std::function<void()> fn) {
|
||||
queue.insert({when, std::move(fn)});
|
||||
arm();
|
||||
}
|
||||
|
||||
void schedule_ms(uint32_t ms, std::function<void()> fn) {
|
||||
schedule(make_timeout_time_ms(ms), std::move(fn));
|
||||
}
|
||||
|
||||
void run() {
|
||||
while (!queue.empty()) {
|
||||
auto& front = queue.front();
|
||||
if (absolute_time_diff_us(get_absolute_time(), front.when) > 0) break;
|
||||
auto fn = std::move(front.fn);
|
||||
queue.pop_front();
|
||||
fn();
|
||||
}
|
||||
arm();
|
||||
}
|
||||
|
||||
bool empty() const { return queue.empty(); }
|
||||
|
||||
private:
|
||||
static int64_t alarm_cb(alarm_id_t, void*) { return 0; }
|
||||
|
||||
void arm() {
|
||||
if (alarm >= 0) cancel_alarm(alarm);
|
||||
alarm = -1;
|
||||
if (!queue.empty())
|
||||
alarm = add_alarm_at(queue.front().when, alarm_cb, nullptr, false);
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE
|
||||
#define CFG_TUD_CDC 1
|
||||
#define CFG_TUD_CDC_RX_BUFSIZE 256
|
||||
#define CFG_TUD_CDC_TX_BUFSIZE 256
|
||||
#define CFG_TUD_CDC_EP_BUFSIZE 64
|
||||
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <span>
|
||||
#include <vector>
|
||||
#include "tusb.h"
|
||||
#include "ring_buffer.h"
|
||||
|
||||
struct usb_cdc {
|
||||
ring_buffer<512> tx;
|
||||
|
||||
void send(std::span<const uint8_t> data) {
|
||||
tx.push(data);
|
||||
drain();
|
||||
}
|
||||
|
||||
void send(const std::vector<uint8_t>& data) {
|
||||
send(std::span<const uint8_t>{data});
|
||||
}
|
||||
|
||||
void drain() {
|
||||
while (!tx.empty()) {
|
||||
uint32_t avail = tud_cdc_write_available();
|
||||
if (avail == 0) break;
|
||||
auto chunk = tx.read_contiguous();
|
||||
if (chunk.size() > avail) chunk = chunk.first(avail);
|
||||
tud_cdc_write(chunk.data(), chunk.size());
|
||||
tx.consume(chunk.size());
|
||||
}
|
||||
tud_cdc_write_flush();
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,96 @@
|
||||
#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;
|
||||
auto as_tuple() const { return std::tie(board_id, mac); }
|
||||
auto as_tuple() { return std::tie(board_id, mac); }
|
||||
};
|
||||
|
||||
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());
|
||||
}
|
||||
Reference in New Issue
Block a user