Files
blk_box_common/include/bbnow_pub.hpp
T
2026-06-30 15:54:17 -05:00

68 lines
2.0 KiB
C++

/// Types for the BLK_BOX NOW pub protocol
#ifndef _BBNOW_PUB_H
#define _BBNOW_PUB_H
#include <stdint.h>
#include <vector>
#include <optional>
#include <span>
#include "bb_expantions.hpp"
///////////////////////////////////////
// (1) DISCOVERY //
/////////////////////////////////////
/// The data associated with the `DISCOVERY` type.
struct BlkBoxNowPubDiscoveryData {
BlkBoxExpantion expantion_type{};
};
///////////////////////////////////////
// PUB PACKET //
/////////////////////////////////////
/// The packet type for `BBNowPubPacket`s.
enum class BBNowPubPacketType: uint16_t {
NONE = 0,
DISCOVERY = 1,
MAX = 1,
};
/// The data field for the packet
union BlkBoxNowPubPacketData {
BlkBoxNowPubDiscoveryData discovery;
};
struct BBNowPubPacket {
const static uint32_t PUB_MAGIC_NUMBER = 0x6262'6E70;
// DISCOVERY packet size
const static size_t DISCOVERY_PACKET_SIZE = sizeof(PUB_MAGIC_NUMBER) + sizeof(BBNowPubPacketType) + sizeof(BlkBoxNowPubDiscoveryData);
static_assert(DISCOVERY_PACKET_SIZE == 8, "Discovery packet should be 8 bytes");
/// The type of packet this is.
BBNowPubPacketType 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<uint8_t> serialize();
/// Serializes the packet into the given vector.
///
/// Returns the number of bytes written into the buffer.
size_t serialize_into(std::vector<uint8_t>& buf);
/// Deserializes a packet from the given bytes.
static std::optional<BBNowPubPacket> deserialize(std::span<const uint8_t> bytes);
/// Constructs an empty packet.
BBNowPubPacket() = default;
/// Constructs a new `Discovery` type packet with the given data.
static BBNowPubPacket new_discovery(BlkBoxNowPubDiscoveryData data);
};
#endif /* _BBNOW_PUB_H */