Corium 1.1.0
High-Performance Zero-Heap C++20 MPSC Application Runtime
Loading...
Searching...
No Matches
WirePacket.hpp
Go to the documentation of this file.
1
7#pragma once
8
9#include <array>
10#include <cstddef>
11#include <cstdint>
12#include <cstring>
13#include <span>
14
15namespace corium::wire {
16
18inline constexpr uint16_t CORIUM_WIRE_MAGIC = 0xC041;
19
21[[nodiscard]] constexpr uint16_t calculateCrc16(std::span<const uint8_t> data) noexcept {
22 uint16_t crc = 0xFFFF;
23 for (uint8_t byte : data) {
24 crc ^= static_cast<uint16_t>(byte) << 8;
25 for (int i = 0; i < 8; ++i) {
26 if (crc & 0x8000) {
27 crc = (crc << 1) ^ 0x1021;
28 } else {
29 crc = crc << 1;
30 }
31 }
32 }
33 return crc;
34}
35
37template <typename T>
38[[nodiscard]] constexpr uint8_t computeTypeSignature() noexcept {
39 auto sig = static_cast<uint8_t>(sizeof(T) & 0x0F);
40 sig |= static_cast<uint8_t>((alignof(T) & 0x0F) << 4);
41 return sig;
42}
43
45inline constexpr uint8_t CORIUM_WIRE_VERSION = 1;
46
48#pragma pack(push, 1)
49struct WireHeader {
52 uint8_t typeIndex{0};
53 uint8_t flags{0};
54 uint8_t reserved{0};
55 uint16_t payloadLength{0};
56 uint16_t checksum{0};
57};
58#pragma pack(pop)
59
63template <size_t MaxPayloadSize = 64>
64struct WirePacket {
66 std::array<uint8_t, MaxPayloadSize> payload{};
67
68 constexpr WirePacket() = default;
69
71 void finalize(uint8_t typeIdx, uint16_t length, uint8_t flags = 0, uint8_t version = CORIUM_WIRE_VERSION) noexcept {
73 header.version = version;
74 header.typeIndex = typeIdx;
75 header.flags = flags;
76 header.reserved = 0;
77 header.payloadLength = length > MaxPayloadSize ? static_cast<uint16_t>(MaxPayloadSize) : length;
78 header.checksum = calculateCrc16(std::span<const uint8_t>(payload.data(), header.payloadLength));
79 }
80
82 [[nodiscard]] bool isValid() const noexcept {
84 return false;
85 }
87 return false;
88 }
89 if (header.payloadLength > MaxPayloadSize) {
90 return false;
91 }
92 uint16_t expected = calculateCrc16(std::span<const uint8_t>(payload.data(), header.payloadLength));
93 return header.checksum == expected;
94 }
95
97 [[nodiscard]] constexpr size_t totalWireSize() const noexcept {
98 return sizeof(WireHeader) + header.payloadLength;
99 }
100};
101
102} // namespace corium::wire
Definition EventJournal.hpp:22
constexpr uint8_t CORIUM_WIRE_VERSION
Current schema version for Corium binary wire packets.
Definition WirePacket.hpp:45
constexpr uint8_t computeTypeSignature() noexcept
Calculate an ABI type signature based on size and alignment.
Definition WirePacket.hpp:38
constexpr uint16_t CORIUM_WIRE_MAGIC
Default magic header identifier for Corium binary wire packets (0xC041).
Definition WirePacket.hpp:18
constexpr uint16_t calculateCrc16(std::span< const uint8_t > data) noexcept
Calculate CRC-16-CCITT checksum over a byte span without lookup tables.
Definition WirePacket.hpp:21
Header structure framing binary wire packets for serial, CAN, SPI, or network transport.
Definition WirePacket.hpp:49
uint16_t payloadLength
Definition WirePacket.hpp:55
uint8_t reserved
Definition WirePacket.hpp:54
uint8_t flags
Definition WirePacket.hpp:53
uint16_t checksum
Definition WirePacket.hpp:56
uint8_t typeIndex
Definition WirePacket.hpp:52
uint8_t version
Definition WirePacket.hpp:51
uint16_t magic
Definition WirePacket.hpp:50
Statically-sized zero-heap binary wire packet.
Definition WirePacket.hpp:64
constexpr WirePacket()=default
constexpr size_t totalWireSize() const noexcept
Total serialized wire size in bytes (header + payload length).
Definition WirePacket.hpp:97
WireHeader header
Definition WirePacket.hpp:65
bool isValid() const noexcept
Validate packet magic identifier, schema version, payload length bounds, and CRC16 checksum.
Definition WirePacket.hpp:82
std::array< uint8_t, MaxPayloadSize > payload
Definition WirePacket.hpp:66
void finalize(uint8_t typeIdx, uint16_t length, uint8_t flags=0, uint8_t version=CORIUM_WIRE_VERSION) noexcept
Finalize packet header, set payload length and calculate CRC16 checksum.
Definition WirePacket.hpp:71