/// Types common across the blk_box ecosystem #ifndef _BLK_BOX_COMMON_H #define _BLK_BOX_COMMON_H #include #include #include #include #include /// The different expantions to the BLK_BOX. enum class BlkBoxExpantion: uint16_t { NONE = 0, WIRES = 1, TENS = 2, MAX = 2, }; /// The packet type for `BlkBoxNowPubPacket`s. enum class BlkBoxNowPubPacketType: uint8_t { NONE = 0, DISCOVERY = 1, MAX = 1, }; /// The data associated with the `DISCOVERY` type. struct BlkBoxNowPubDiscoveryData { BlkBoxExpantion expantion_type{}; }; union BlkBoxNowPubPacketData { BlkBoxNowPubDiscoveryData discovery; }; struct BlkBoxNowPubPacket { const static uint32_t PUB_MAGIC_NUMBER = 0x8934'DBE3; // DISCOVERY packet size const static size_t DISCOVERY_PACKET_SIZE = sizeof(PUB_MAGIC_NUMBER) + sizeof(BlkBoxNowPubPacketType) + sizeof(BlkBoxNowPubDiscoveryData); static_assert(DISCOVERY_PACKET_SIZE == 7, "Discovery packet should be 7 bytes"); /// The type of packet this is. BlkBoxNowPubPacketType packet_type{}; /// The data held by the packet. BlkBoxNowPubPacketData packet_data{}; /// The size of the serialized packet (in bytes). size_t serialize_size(); /// Serializes the packet to a vector. std::vector serialize(); /// Serializes the packet into the given vector. size_t serialize_into(std::vector& buf); /// Deserializes a packet from the given bytes. static std::optional deserialize(std::span bytes); /// Constructs an empty packet. BlkBoxNowPubPacket() = default; static BlkBoxNowPubPacket new_discovery(BlkBoxNowPubDiscoveryData data); }; #endif /* _BLK_BOX_COMMON_H */