Move C++ tree into firmware/
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
cmake_minimum_required(VERSION 3.13)
|
||||
|
||||
set(PICO_BOARD pico2)
|
||||
|
||||
include(pico_sdk_import.cmake)
|
||||
|
||||
project(picomap C CXX ASM)
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD 23)
|
||||
pico_sdk_init()
|
||||
|
||||
add_executable(picomap
|
||||
main.cpp
|
||||
dhcp.cpp
|
||||
net.cpp
|
||||
tusb_config.cpp
|
||||
w6300/w6300.cpp
|
||||
)
|
||||
|
||||
target_include_directories(picomap PRIVATE
|
||||
include
|
||||
w6300
|
||||
)
|
||||
|
||||
target_compile_options(picomap PRIVATE -Wall -Wextra -Wno-unused-parameter)
|
||||
|
||||
pico_generate_pio_header(picomap ${CMAKE_CURRENT_LIST_DIR}/w6300/qspi.pio)
|
||||
|
||||
pico_enable_stdio_usb(picomap 0)
|
||||
pico_enable_stdio_uart(picomap 0)
|
||||
|
||||
pico_add_extra_outputs(picomap)
|
||||
|
||||
target_link_libraries(picomap pico_stdlib pico_rand tinyusb_device tinyusb_board hardware_pio hardware_spi hardware_dma hardware_clocks)
|
||||
@@ -0,0 +1,55 @@
|
||||
#include "dhcp.h"
|
||||
#include <span>
|
||||
#include "pico/rand.h"
|
||||
#include "w6300.h"
|
||||
|
||||
namespace dhcp_opt {
|
||||
constexpr uint8_t message_type = 53;
|
||||
constexpr uint8_t param_request = 55;
|
||||
constexpr uint8_t end = 255;
|
||||
constexpr uint8_t discover = 1;
|
||||
constexpr uint8_t subnet_mask = 1;
|
||||
}
|
||||
|
||||
struct __attribute__((packed)) dhcp_discover {
|
||||
uint8_t op = 1;
|
||||
uint8_t htype = 1;
|
||||
uint8_t hlen = 6;
|
||||
uint8_t hops = 0;
|
||||
uint32_t xid = __builtin_bswap32(static_cast<uint32_t>(get_rand_64()));
|
||||
uint16_t secs = 0;
|
||||
uint16_t flags = __builtin_bswap16(0x8000);
|
||||
std::array<uint8_t, 4> ciaddr = {};
|
||||
std::array<uint8_t, 4> yiaddr = {};
|
||||
std::array<uint8_t, 4> siaddr = {};
|
||||
std::array<uint8_t, 4> giaddr = {};
|
||||
std::array<uint8_t, 16> chaddr = {};
|
||||
std::array<uint8_t, 64> sname = {};
|
||||
std::array<uint8_t, 128> file = {};
|
||||
std::array<uint8_t, 4> magic = {99, 130, 83, 99};
|
||||
uint8_t opt_msg_type[3] = {dhcp_opt::message_type, 1, dhcp_opt::discover};
|
||||
uint8_t opt_params[3] = {dhcp_opt::param_request, 1, dhcp_opt::subnet_mask};
|
||||
uint8_t opt_end = dhcp_opt::end;
|
||||
};
|
||||
|
||||
static_assert(sizeof(dhcp_discover) == 247);
|
||||
|
||||
static void send_discover(timer_queue& timers, const std::array<uint8_t, 6>& mac) {
|
||||
auto sn = w6300::socket_id{0};
|
||||
w6300::open_socket(sn, w6300::protocol::udp, w6300::port_num{68}, w6300::sock_flag::none);
|
||||
|
||||
dhcp_discover pkt;
|
||||
std::copy(mac.begin(), mac.end(), pkt.chaddr.begin());
|
||||
|
||||
w6300::ip_address broadcast = {};
|
||||
broadcast.ip = {255, 255, 255, 255};
|
||||
broadcast.len = 4;
|
||||
|
||||
w6300::sendto(sn, std::span{reinterpret_cast<uint8_t*>(&pkt), sizeof(pkt)}, broadcast, w6300::port_num{67});
|
||||
|
||||
timers.schedule_ms(5000, [&timers, mac]() { send_discover(timers, mac); });
|
||||
}
|
||||
|
||||
void dhcp_start(timer_queue& timers, const std::array<uint8_t, 6>& mac) {
|
||||
send_discover(timers, mac);
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
#include "pico/stdlib.h"
|
||||
#include "pico/bootrom.h"
|
||||
#include "pico/unique_id.h"
|
||||
#include "tusb.h"
|
||||
#include "wire.h"
|
||||
#include "usb_cdc.h"
|
||||
#include "timer_queue.h"
|
||||
#include "dhcp.h"
|
||||
#include "net.h"
|
||||
#include "w6300.h"
|
||||
|
||||
static usb_cdc usb;
|
||||
static timer_queue timers;
|
||||
|
||||
int main() {
|
||||
tusb_init();
|
||||
|
||||
net_init();
|
||||
|
||||
auto ninfo = w6300::get_net_info();
|
||||
dhcp_start(timers, ninfo.mac);
|
||||
|
||||
static static_vector<uint8_t, 256> rx_buf;
|
||||
|
||||
while (true) {
|
||||
tud_task();
|
||||
|
||||
usb.drain();
|
||||
timers.run();
|
||||
|
||||
while (tud_cdc_available()) {
|
||||
uint8_t byte;
|
||||
if (tud_cdc_read(&byte, 1) != 1) break;
|
||||
|
||||
rx_buf.push_back(byte);
|
||||
|
||||
auto msg = try_decode(rx_buf);
|
||||
if (!msg) {
|
||||
if (rx_buf.full()) rx_buf.clear();
|
||||
continue;
|
||||
}
|
||||
|
||||
rx_buf.clear();
|
||||
|
||||
switch (msg->type_id) {
|
||||
case RequestPICOBOOT::ext_id:
|
||||
usb.send(encode_response(msg->message_id, ResponsePICOBOOT{}));
|
||||
sleep_ms(100);
|
||||
reset_usb_boot(0, 1);
|
||||
break;
|
||||
case RequestInfo::ext_id: {
|
||||
ResponseInfo resp;
|
||||
pico_unique_board_id_t uid;
|
||||
pico_get_unique_board_id(&uid);
|
||||
std::copy(uid.id, uid.id + 8, resp.board_id.begin());
|
||||
auto ninfo = w6300::get_net_info();
|
||||
resp.mac = ninfo.mac;
|
||||
usb.send(encode_response(msg->message_id, resp));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
__wfi();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#include "net.h"
|
||||
#include "pico/unique_id.h"
|
||||
#include "w6300.h"
|
||||
|
||||
bool net_init() {
|
||||
w6300::init_spi();
|
||||
w6300::init_critical_section();
|
||||
w6300::reset();
|
||||
w6300::init();
|
||||
if (!w6300::check()) return false;
|
||||
|
||||
pico_unique_board_id_t uid;
|
||||
pico_get_unique_board_id(&uid);
|
||||
w6300::net_info info = {};
|
||||
info.mac[0] = (uid.id[0] & 0xFC) | 0x02;
|
||||
info.mac[1] = uid.id[1];
|
||||
info.mac[2] = uid.id[2];
|
||||
info.mac[3] = uid.id[3];
|
||||
info.mac[4] = uid.id[4];
|
||||
info.mac[5] = uid.id[5];
|
||||
w6300::init_net(info);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
# This is a copy of <PICO_SDK_PATH>/external/pico_sdk_import.cmake
|
||||
|
||||
# This can be dropped into an external project to help locate this SDK
|
||||
# It should be include()ed prior to project()
|
||||
|
||||
# Copyright 2020 (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
|
||||
# following conditions are met:
|
||||
#
|
||||
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
|
||||
# disclaimer.
|
||||
#
|
||||
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
|
||||
# disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
#
|
||||
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products
|
||||
# derived from this software without specific prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH))
|
||||
set(PICO_SDK_PATH $ENV{PICO_SDK_PATH})
|
||||
message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')")
|
||||
endif ()
|
||||
|
||||
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT))
|
||||
set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT})
|
||||
message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')")
|
||||
endif ()
|
||||
|
||||
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH))
|
||||
set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH})
|
||||
message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')")
|
||||
endif ()
|
||||
|
||||
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_TAG} AND (NOT PICO_SDK_FETCH_FROM_GIT_TAG))
|
||||
set(PICO_SDK_FETCH_FROM_GIT_TAG $ENV{PICO_SDK_FETCH_FROM_GIT_TAG})
|
||||
message("Using PICO_SDK_FETCH_FROM_GIT_TAG from environment ('${PICO_SDK_FETCH_FROM_GIT_TAG}')")
|
||||
endif ()
|
||||
|
||||
if (PICO_SDK_FETCH_FROM_GIT AND NOT PICO_SDK_FETCH_FROM_GIT_TAG)
|
||||
set(PICO_SDK_FETCH_FROM_GIT_TAG "master")
|
||||
message("Using master as default value for PICO_SDK_FETCH_FROM_GIT_TAG")
|
||||
endif()
|
||||
|
||||
set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK")
|
||||
set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable")
|
||||
set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK")
|
||||
set(PICO_SDK_FETCH_FROM_GIT_TAG "${PICO_SDK_FETCH_FROM_GIT_TAG}" CACHE FILEPATH "release tag for SDK")
|
||||
|
||||
if (NOT PICO_SDK_PATH)
|
||||
if (PICO_SDK_FETCH_FROM_GIT)
|
||||
include(FetchContent)
|
||||
set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR})
|
||||
if (PICO_SDK_FETCH_FROM_GIT_PATH)
|
||||
get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}")
|
||||
endif ()
|
||||
FetchContent_Declare(
|
||||
pico_sdk
|
||||
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
|
||||
GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG}
|
||||
)
|
||||
|
||||
if (NOT pico_sdk)
|
||||
message("Downloading Raspberry Pi Pico SDK")
|
||||
# GIT_SUBMODULES_RECURSE was added in 3.17
|
||||
if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.17.0")
|
||||
FetchContent_Populate(
|
||||
pico_sdk
|
||||
QUIET
|
||||
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
|
||||
GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG}
|
||||
GIT_SUBMODULES_RECURSE FALSE
|
||||
|
||||
SOURCE_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-src
|
||||
BINARY_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-build
|
||||
SUBBUILD_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-subbuild
|
||||
)
|
||||
else ()
|
||||
FetchContent_Populate(
|
||||
pico_sdk
|
||||
QUIET
|
||||
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
|
||||
GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG}
|
||||
|
||||
SOURCE_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-src
|
||||
BINARY_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-build
|
||||
SUBBUILD_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-subbuild
|
||||
)
|
||||
endif ()
|
||||
|
||||
set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR})
|
||||
endif ()
|
||||
set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE})
|
||||
else ()
|
||||
message(FATAL_ERROR
|
||||
"SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git."
|
||||
)
|
||||
endif ()
|
||||
endif ()
|
||||
|
||||
get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
|
||||
if (NOT EXISTS ${PICO_SDK_PATH})
|
||||
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found")
|
||||
endif ()
|
||||
|
||||
set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake)
|
||||
if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE})
|
||||
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK")
|
||||
endif ()
|
||||
|
||||
set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE)
|
||||
|
||||
include(${PICO_SDK_INIT_CMAKE_FILE})
|
||||
@@ -0,0 +1,82 @@
|
||||
#include <cstring>
|
||||
#include "pico/unique_id.h"
|
||||
#include "tusb.h"
|
||||
|
||||
constexpr uint16_t USB_VID = 0x2E8A;
|
||||
constexpr uint16_t USB_PID = 0x000A;
|
||||
|
||||
static constexpr tusb_desc_device_t desc_device = {
|
||||
sizeof(tusb_desc_device_t),
|
||||
TUSB_DESC_DEVICE,
|
||||
0x0200,
|
||||
TUSB_CLASS_MISC,
|
||||
MISC_SUBCLASS_COMMON,
|
||||
MISC_PROTOCOL_IAD,
|
||||
CFG_TUD_ENDPOINT0_SIZE,
|
||||
USB_VID,
|
||||
USB_PID,
|
||||
0x0100,
|
||||
1, 2, 3,
|
||||
1,
|
||||
};
|
||||
|
||||
enum { ITF_NUM_CDC, ITF_NUM_CDC_DATA, ITF_NUM_TOTAL };
|
||||
|
||||
constexpr uint8_t EPNUM_CDC_NOTIF = 0x81;
|
||||
constexpr uint8_t EPNUM_CDC_OUT = 0x02;
|
||||
constexpr uint8_t EPNUM_CDC_IN = 0x82;
|
||||
constexpr uint16_t CONFIG_TOTAL_LEN = TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN;
|
||||
|
||||
static uint8_t const desc_configuration[] = {
|
||||
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100),
|
||||
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 0, EPNUM_CDC_NOTIF, 8, EPNUM_CDC_OUT, EPNUM_CDC_IN, 64),
|
||||
};
|
||||
|
||||
static constexpr const char* string_desc[] = {
|
||||
"\x09\x04",
|
||||
"picomap",
|
||||
"picomap",
|
||||
nullptr,
|
||||
};
|
||||
|
||||
static uint16_t desc_str_buf[33];
|
||||
|
||||
extern "C" {
|
||||
|
||||
uint8_t const* tud_descriptor_device_cb(void) {
|
||||
return reinterpret_cast<uint8_t const*>(&desc_device);
|
||||
}
|
||||
|
||||
uint8_t const* tud_descriptor_configuration_cb(uint8_t index) {
|
||||
return desc_configuration;
|
||||
}
|
||||
|
||||
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
|
||||
uint8_t chr_count;
|
||||
|
||||
if (index == 0) {
|
||||
memcpy(&desc_str_buf[1], string_desc[0], 2);
|
||||
chr_count = 1;
|
||||
} else if (index == 3) {
|
||||
pico_unique_board_id_t uid;
|
||||
pico_get_unique_board_id(&uid);
|
||||
chr_count = 0;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
desc_str_buf[1 + chr_count++] = "0123456789ABCDEF"[uid.id[i] >> 4];
|
||||
desc_str_buf[1 + chr_count++] = "0123456789ABCDEF"[uid.id[i] & 0xF];
|
||||
}
|
||||
} else {
|
||||
if (index >= sizeof(string_desc) / sizeof(string_desc[0])) return nullptr;
|
||||
const char* str = string_desc[index];
|
||||
if (!str) return nullptr;
|
||||
chr_count = strlen(str);
|
||||
if (chr_count > 31) chr_count = 31;
|
||||
for (uint8_t i = 0; i < chr_count; i++)
|
||||
desc_str_buf[1 + i] = str[i];
|
||||
}
|
||||
|
||||
desc_str_buf[0] = static_cast<uint16_t>((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
|
||||
return desc_str_buf;
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
@@ -0,0 +1,18 @@
|
||||
.program qspi
|
||||
.side_set 1
|
||||
|
||||
write_bits:
|
||||
out pins, 4 side 0
|
||||
jmp x-- write_bits side 1
|
||||
set pins 0 side 0
|
||||
public write_bits_end:
|
||||
read_byte_delay:
|
||||
set pindirs 0 side 0
|
||||
read_byte:
|
||||
set x 0 side 1
|
||||
read_bits:
|
||||
in pins, 4 side 0
|
||||
jmp x-- read_bits side 1
|
||||
in pins, 4 side 0
|
||||
jmp y-- read_byte side 0
|
||||
public read_bits_end:
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,267 @@
|
||||
#pragma once
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
|
||||
namespace w6300 {
|
||||
|
||||
constexpr int SOCK_COUNT = 8;
|
||||
|
||||
enum class socket_id : uint8_t {};
|
||||
enum class port_num : uint16_t {};
|
||||
|
||||
enum class sock_error : int16_t {
|
||||
busy = 0,
|
||||
sock_num = -1,
|
||||
sock_opt = -2,
|
||||
sock_init = -3,
|
||||
sock_closed = -4,
|
||||
sock_mode = -5,
|
||||
sock_flag = -6,
|
||||
sock_status = -7,
|
||||
arg = -10,
|
||||
port_zero = -11,
|
||||
ip_invalid = -12,
|
||||
timeout = -13,
|
||||
data_len = -14,
|
||||
buffer = -15,
|
||||
fatal_packlen = -1001,
|
||||
};
|
||||
|
||||
enum class protocol : uint8_t {
|
||||
tcp = 0x01,
|
||||
udp = 0x02,
|
||||
ipraw = 0x03,
|
||||
macraw = 0x07,
|
||||
tcp6 = 0x09,
|
||||
udp6 = 0x0A,
|
||||
ipraw6 = 0x0B,
|
||||
tcp_dual = 0x0D,
|
||||
udp_dual = 0x0E,
|
||||
};
|
||||
|
||||
enum class sock_flag : uint8_t {
|
||||
none = 0,
|
||||
multi_enable = 1 << 7,
|
||||
ether_own = 1 << 7,
|
||||
broad_block = 1 << 6,
|
||||
tcp_fpsh = 1 << 6,
|
||||
tcp_nodelay = 1 << 5,
|
||||
igmp_ver2 = 1 << 5,
|
||||
solicit_block = 1 << 5,
|
||||
ether_multi4b = 1 << 5,
|
||||
uni_block = 1 << 4,
|
||||
ether_multi6b = 1 << 4,
|
||||
force_arp = 1 << 0,
|
||||
dha_manual = 1 << 1,
|
||||
io_nonblock = 1 << 3,
|
||||
};
|
||||
constexpr sock_flag operator|(sock_flag a, sock_flag b) {
|
||||
return static_cast<sock_flag>(static_cast<uint8_t>(a) | static_cast<uint8_t>(b));
|
||||
}
|
||||
constexpr uint8_t operator&(sock_flag a, sock_flag b) {
|
||||
return static_cast<uint8_t>(a) & static_cast<uint8_t>(b);
|
||||
}
|
||||
|
||||
enum class pack_info : uint8_t {
|
||||
none = 0x00,
|
||||
first = 1 << 1,
|
||||
remained = 1 << 2,
|
||||
completed = 1 << 3,
|
||||
ipv6_lla = (1 << 7) | (1 << 4),
|
||||
ipv6_multi = (1 << 7) | (1 << 5),
|
||||
ipv6_allnode = (1 << 7) | (1 << 6),
|
||||
ipv6 = 1 << 7,
|
||||
};
|
||||
constexpr pack_info operator|(pack_info a, pack_info b) {
|
||||
return static_cast<pack_info>(static_cast<uint8_t>(a) | static_cast<uint8_t>(b));
|
||||
}
|
||||
constexpr uint8_t operator&(pack_info a, pack_info b) {
|
||||
return static_cast<uint8_t>(a) & static_cast<uint8_t>(b);
|
||||
}
|
||||
|
||||
enum class srcv6_prefer : uint8_t {
|
||||
auto_select = 0x00,
|
||||
lla = 0x02,
|
||||
gua = 0x03,
|
||||
};
|
||||
|
||||
enum class tcp_sock_info : uint8_t {
|
||||
mode = 1 << 2,
|
||||
op = 1 << 1,
|
||||
sip = 1 << 0,
|
||||
};
|
||||
|
||||
enum class sock_io_mode : uint8_t {
|
||||
block = 0,
|
||||
nonblock = 1,
|
||||
};
|
||||
|
||||
enum intr_kind : uint32_t {
|
||||
IK_PPPOE_TERMINATED = (1 << 0), IK_DEST_UNREACH = (1 << 1), IK_IP_CONFLICT = (1 << 2),
|
||||
IK_DEST_UNREACH6 = (1 << 4), IK_WOL = (1 << 7), IK_NET_ALL = 0x97,
|
||||
IK_SOCK_0 = (1 << 8), IK_SOCK_1 = (1 << 9), IK_SOCK_2 = (1 << 10), IK_SOCK_3 = (1 << 11),
|
||||
IK_SOCK_4 = (1 << 12), IK_SOCK_5 = (1 << 13), IK_SOCK_6 = (1 << 14), IK_SOCK_7 = (1 << 15),
|
||||
IK_SOCK_ALL = (0xFF << 8),
|
||||
IK_SOCKL_TOUT = (1 << 16), IK_SOCKL_ARP4 = (1 << 17), IK_SOCKL_PING4 = (1 << 18),
|
||||
IK_SOCKL_ARP6 = (1 << 19), IK_SOCKL_PING6 = (1 << 20), IK_SOCKL_NS = (1 << 21),
|
||||
IK_SOCKL_RS = (1 << 22), IK_SOCKL_RA = (1 << 23), IK_SOCKL_ALL = (0xFF << 16),
|
||||
IK_INT_ALL = 0x00FFFF97
|
||||
};
|
||||
|
||||
struct phy_conf {
|
||||
uint8_t by;
|
||||
uint8_t mode;
|
||||
uint8_t speed;
|
||||
uint8_t duplex;
|
||||
};
|
||||
|
||||
enum ipconf_mode : uint8_t {
|
||||
NETINFO_NONE = 0x00, NETINFO_STATIC_V4 = 0x01, NETINFO_STATIC_V6 = 0x02,
|
||||
NETINFO_STATIC_ALL = 0x03, NETINFO_SLAAC_V6 = 0x04,
|
||||
NETINFO_DHCP_V4 = 0x10, NETINFO_DHCP_V6 = 0x20, NETINFO_DHCP_ALL = 0x30
|
||||
};
|
||||
|
||||
enum dhcp_mode : uint8_t { NETINFO_STATIC = 1, NETINFO_DHCP };
|
||||
|
||||
struct net_info {
|
||||
std::array<uint8_t, 6> mac;
|
||||
std::array<uint8_t, 4> ip;
|
||||
std::array<uint8_t, 4> sn;
|
||||
std::array<uint8_t, 4> gw;
|
||||
std::array<uint8_t, 16> lla;
|
||||
std::array<uint8_t, 16> gua;
|
||||
std::array<uint8_t, 16> sn6;
|
||||
std::array<uint8_t, 16> gw6;
|
||||
std::array<uint8_t, 4> dns;
|
||||
std::array<uint8_t, 16> dns6;
|
||||
ipconf_mode ipmode;
|
||||
dhcp_mode dhcp;
|
||||
};
|
||||
|
||||
enum netmode_type : uint32_t {
|
||||
NM_IPB_V4 = (1 << 0), NM_IPB_V6 = (1 << 1), NM_WOL = (1 << 2),
|
||||
NM_PB6_MULTI = (1 << 4), NM_PB6_ALLNODE = (1 << 5), NM_MR_MASK = 0x37,
|
||||
NM_PPPoE = (1 << 8), NM_DHA_SELECT = (1 << 15), NM_MR2_MASK = (0x09 << 8),
|
||||
NM_PB4_ALL = (1 << 16), NM_TRSTB_V4 = (1 << 17), NM_PARP_V4 = (1 << 18),
|
||||
NM_UNRB_V4 = (1 << 19), NM_NET4_MASK = (0x0F << 16),
|
||||
NM_PB6_ALL = (1 << 24), NM_TRSTB_V6 = (1 << 25), NM_PARP_V6 = (1 << 26),
|
||||
NM_UNRB_V6 = (1 << 27), NM_NET6_MASK = (0x0F << 24),
|
||||
NM_MASK_ALL = 0x0F0F0937
|
||||
};
|
||||
|
||||
struct net_timeout {
|
||||
uint8_t s_retry_cnt;
|
||||
uint16_t s_time_100us;
|
||||
uint8_t sl_retry_cnt;
|
||||
uint16_t sl_time_100us;
|
||||
};
|
||||
|
||||
struct ip_address {
|
||||
std::array<uint8_t, 16> ip;
|
||||
uint8_t len;
|
||||
};
|
||||
|
||||
struct prefix {
|
||||
uint8_t len;
|
||||
uint8_t flag;
|
||||
uint32_t valid_lifetime;
|
||||
uint32_t preferred_lifetime;
|
||||
std::array<uint8_t, 16> prefix;
|
||||
};
|
||||
|
||||
struct arp_request {
|
||||
ip_address destinfo;
|
||||
std::array<uint8_t, 6> dha;
|
||||
};
|
||||
|
||||
struct ping_request {
|
||||
uint16_t id;
|
||||
uint16_t seq;
|
||||
ip_address destinfo;
|
||||
};
|
||||
|
||||
void init_spi();
|
||||
void init_critical_section();
|
||||
void reset();
|
||||
void init();
|
||||
bool check();
|
||||
void init_net(const net_info& info);
|
||||
|
||||
void soft_reset();
|
||||
int8_t init_buffers(std::span<const uint8_t> txsize, std::span<const uint8_t> rxsize);
|
||||
void clear_interrupt(intr_kind intr);
|
||||
intr_kind get_interrupt();
|
||||
void set_interrupt_mask(intr_kind intr);
|
||||
intr_kind get_interrupt_mask();
|
||||
|
||||
int8_t get_phy_link();
|
||||
int8_t get_phy_power_mode();
|
||||
void reset_phy();
|
||||
void set_phy_conf(const phy_conf& conf);
|
||||
phy_conf get_phy_conf();
|
||||
phy_conf get_phy_status();
|
||||
void set_phy_power_mode(uint8_t pmode);
|
||||
|
||||
void set_net_info(const net_info& info);
|
||||
net_info get_net_info();
|
||||
void set_net_mode(netmode_type mode);
|
||||
netmode_type get_net_mode();
|
||||
void set_timeout(const net_timeout& timeout);
|
||||
net_timeout get_timeout();
|
||||
|
||||
int8_t send_arp(arp_request& arp);
|
||||
int8_t send_ping(const ping_request& ping);
|
||||
int8_t send_dad(std::span<const uint8_t, 16> ipv6);
|
||||
int8_t send_slaac(prefix& pfx);
|
||||
int8_t send_unsolicited();
|
||||
int8_t get_prefix(prefix& pfx);
|
||||
|
||||
std::expected<socket_id, sock_error> open_socket(socket_id sn, protocol proto, port_num port, sock_flag flag);
|
||||
std::expected<void, sock_error> close(socket_id sn);
|
||||
std::expected<void, sock_error> listen(socket_id sn);
|
||||
std::expected<void, sock_error> disconnect(socket_id sn);
|
||||
std::expected<uint16_t, sock_error> send(socket_id sn, std::span<const uint8_t> buf);
|
||||
std::expected<uint16_t, sock_error> recv(socket_id sn, std::span<uint8_t> buf);
|
||||
|
||||
std::expected<void, sock_error> connect(socket_id sn, const ip_address& addr, port_num port);
|
||||
std::expected<uint16_t, sock_error> sendto(socket_id sn, std::span<const uint8_t> buf, const ip_address& addr, port_num port);
|
||||
std::expected<uint16_t, sock_error> recvfrom(socket_id sn, std::span<uint8_t> buf, ip_address& addr, port_num& port);
|
||||
|
||||
std::expected<void, sock_error> set_socket_io_mode(socket_id sn, sock_io_mode mode);
|
||||
sock_io_mode get_socket_io_mode(socket_id sn);
|
||||
uint16_t get_socket_max_tx(socket_id sn);
|
||||
uint16_t get_socket_max_rx(socket_id sn);
|
||||
std::expected<void, sock_error> clear_socket_interrupt(socket_id sn, uint8_t flags);
|
||||
uint8_t get_socket_interrupt(socket_id sn);
|
||||
std::expected<void, sock_error> set_socket_interrupt_mask(socket_id sn, uint8_t mask);
|
||||
uint8_t get_socket_interrupt_mask(socket_id sn);
|
||||
std::expected<void, sock_error> set_socket_prefer(socket_id sn, srcv6_prefer pref);
|
||||
srcv6_prefer get_socket_prefer(socket_id sn);
|
||||
|
||||
void set_socket_ttl(socket_id sn, uint8_t ttl);
|
||||
uint8_t get_socket_ttl(socket_id sn);
|
||||
void set_socket_tos(socket_id sn, uint8_t tos);
|
||||
uint8_t get_socket_tos(socket_id sn);
|
||||
void set_socket_mss(socket_id sn, uint16_t mss);
|
||||
uint16_t get_socket_mss(socket_id sn);
|
||||
void set_socket_dest_ip(socket_id sn, const ip_address& addr);
|
||||
ip_address get_socket_dest_ip(socket_id sn);
|
||||
void set_socket_dest_port(socket_id sn, port_num port);
|
||||
port_num get_socket_dest_port(socket_id sn);
|
||||
std::expected<void, sock_error> send_keepalive(socket_id sn);
|
||||
void set_socket_keepalive_auto(socket_id sn, uint8_t interval);
|
||||
uint8_t get_socket_keepalive_auto(socket_id sn);
|
||||
uint16_t get_socket_send_buf(socket_id sn);
|
||||
uint16_t get_socket_recv_buf(socket_id sn);
|
||||
uint8_t get_socket_status(socket_id sn);
|
||||
uint8_t get_socket_ext_status(socket_id sn);
|
||||
uint8_t get_socket_mode(socket_id sn);
|
||||
uint16_t get_socket_remain_size(socket_id sn);
|
||||
pack_info get_socket_pack_info(socket_id sn);
|
||||
|
||||
std::optional<uint16_t> peek_socket_msg(socket_id sn, std::span<const uint8_t> submsg);
|
||||
|
||||
} // namespace w6300
|
||||
Reference in New Issue
Block a user