From d28e81c5109f70919ec9107d1adaff133052cb56 Mon Sep 17 00:00:00 2001 From: Mitchell Date: Tue, 30 Jun 2026 15:54:17 -0500 Subject: [PATCH] split pub and priv --- CMakeLists.txt | 2 +- blk_box_common.cpp => bbnow_pub.cpp | 73 ++++++++++++++--------------- include/bb_expantions.hpp | 16 +++++++ include/bbnow.hpp | 9 ++++ include/bbnow_priv.hpp | 6 +++ include/bbnow_pub.hpp | 67 ++++++++++++++++++++++++++ include/blk_box_common.hpp | 62 ------------------------ 7 files changed, 135 insertions(+), 100 deletions(-) rename blk_box_common.cpp => bbnow_pub.cpp (61%) create mode 100644 include/bb_expantions.hpp create mode 100644 include/bbnow.hpp create mode 100644 include/bbnow_priv.hpp create mode 100644 include/bbnow_pub.hpp delete mode 100644 include/blk_box_common.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index bcf55fa..d1ab58c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register( - SRCS "blk_box_common.cpp" + SRCS "bbnow_pub.cpp" INCLUDE_DIRS "include" "." # PRIV_REQUIRES ) diff --git a/blk_box_common.cpp b/bbnow_pub.cpp similarity index 61% rename from blk_box_common.cpp rename to bbnow_pub.cpp index 8a77466..162c24a 100644 --- a/blk_box_common.cpp +++ b/bbnow_pub.cpp @@ -1,38 +1,31 @@ -#include "blk_box_common.hpp" +#include "bbnow_pub.hpp" #include -#include -#include -#include -#include -#include #include -const static char TAG[] = "blk_box_common"; +const static char TAG[] = "bbnow_pub"; -size_t BlkBoxNowPubPacket::serialize_size() { +size_t BBNowPubPacket::serialize_size() { switch (this->packet_type) { - case BlkBoxNowPubPacketType::NONE: + case BBNowPubPacketType::NONE: return -1; - case BlkBoxNowPubPacketType::DISCOVERY: - return BlkBoxNowPubPacket::DISCOVERY_PACKET_SIZE; + case BBNowPubPacketType::DISCOVERY: + return BBNowPubPacket::DISCOVERY_PACKET_SIZE; } return -1; } -std::vector BlkBoxNowPubPacket::serialize() { - size_t size = this->serialize_size(); - if (size == -1) { - return std::vector(0); - } +std::vector BBNowPubPacket::serialize() { auto buf = std::vector(); - buf.reserve(size); + + if (this->serialize_size() == -1) { + return buf; + } this->serialize_into(buf); return buf; } -/// Serializes the packet into the given vector. -size_t BlkBoxNowPubPacket::serialize_into(std::vector& buf) { +size_t BBNowPubPacket::serialize_into(std::vector& buf) { size_t initial_size = buf.size(); size_t size = this->serialize_size(); if (size == -1) { @@ -48,14 +41,16 @@ size_t BlkBoxNowPubPacket::serialize_into(std::vector& buf) { buf.push_back((PUB_MAGIC_NUMBER >> 0) & 0xFF); // push packet type - static_assert(sizeof(this->packet_type) == 1, "expect type field to be 1 byte"); - buf.push_back(static_cast(this->packet_type)); + static_assert(sizeof(this->packet_type) == 2, "expect type field to be 2 bytes"); + uint16_t packet_type = static_cast(this->packet_type); + buf.push_back((packet_type >> 8) & 0xFF); + buf.push_back((packet_type >> 0) & 0xFF); switch (this->packet_type) { - case BlkBoxNowPubPacketType::NONE: + case BBNowPubPacketType::NONE: break; - case BlkBoxNowPubPacketType::DISCOVERY: - static_assert(sizeof(this->packet_data.discovery) == 2, "expect discovery data to be 2 byte"); + case BBNowPubPacketType::DISCOVERY: + static_assert(sizeof(this->packet_data.discovery) == 2, "expect discovery data to be 2 bytes"); uint16_t data = static_cast(this->packet_data.discovery.expantion_type); buf.push_back((data >> 8) & 0xFF); buf.push_back((data >> 0) & 0xFF); @@ -67,12 +62,15 @@ size_t BlkBoxNowPubPacket::serialize_into(std::vector& buf) { } /// Deserializes a packet from the given bytes. -std::optional BlkBoxNowPubPacket::deserialize(std::span bytes) { +std::optional BBNowPubPacket::deserialize(std::span bytes) { // first check for magic number and type - if (bytes.size() < 5) { + static_assert(sizeof(PUB_MAGIC_NUMBER) + sizeof(BBNowPubPacketType) == 6, "expect magic and type to be 6 bytes"); + if (bytes.size() < 6) { // not enough room for magic number and type return std::nullopt; } + + static_assert(sizeof(PUB_MAGIC_NUMBER) == 4, "expect magic number to be 4 bytes"); uint32_t magic_number = (uint32_t)(bytes[0] << 24) | (uint32_t)(bytes[1] << 16) | @@ -85,15 +83,16 @@ std::optional BlkBoxNowPubPacket::deserialize(std::span(packet_type); - if (packet.packet_type > BlkBoxNowPubPacketType::MAX) { + static_assert(sizeof(BBNowPubPacketType) == 2, "expect packet type to be 2 bytes"); + uint16_t packet_type = ((uint16_t)bytes[4] << 8) | ((uint16_t)bytes[5]); + packet.packet_type = static_cast(packet_type); + if (packet.packet_type > BBNowPubPacketType::MAX) { ESP_LOGW(TAG, "BlkBoxPubPacketType was NONE! discarding"); return std::nullopt; } - if (packet.packet_type > BlkBoxNowPubPacketType::MAX) { + if (packet.packet_type > BBNowPubPacketType::MAX) { ESP_LOGW(TAG, "BlkBoxPubPacketType not in range! discarding"); return std::nullopt; } @@ -110,10 +109,10 @@ std::optional BlkBoxNowPubPacket::deserialize(std::span(device); if (packet.packet_data.discovery.expantion_type > BlkBoxExpantion::MAX) { ESP_LOGW(TAG, "BlkBoxExpantion not in range! discarding"); @@ -124,9 +123,9 @@ std::optional BlkBoxNowPubPacket::deserialize(std::span + +/// The expantion device ids in the BLK_BOX ecosystem. +enum class BlkBoxExpantion: uint16_t { + NONE = 0, + WIRES = 1, + TENS = 2, + MAX = 2, +}; + +#endif /* _BB_EXPANTIONS_H */ diff --git a/include/bbnow.hpp b/include/bbnow.hpp new file mode 100644 index 0000000..b831af3 --- /dev/null +++ b/include/bbnow.hpp @@ -0,0 +1,9 @@ +/// Types for the BLK_BOX NOW protocol + +#ifndef _BBNOW_H +#define _BBNOW_H + +#include "bbnow_pub.hpp" // IWYU pragma: keep +#include "bbnow_priv.hpp" // IWYU pragma: keep + +#endif /* _BBNOW_H */ diff --git a/include/bbnow_priv.hpp b/include/bbnow_priv.hpp new file mode 100644 index 0000000..9975d29 --- /dev/null +++ b/include/bbnow_priv.hpp @@ -0,0 +1,6 @@ +/// Types for the BLK_BOX NOW priv protocol + +#ifndef _BBNOW_PRIV_H +#define _BBNOW_PRIV_H + +#endif /* _BBNOW_PRIV_H */ diff --git a/include/bbnow_pub.hpp b/include/bbnow_pub.hpp new file mode 100644 index 0000000..8264e99 --- /dev/null +++ b/include/bbnow_pub.hpp @@ -0,0 +1,67 @@ +/// Types for the BLK_BOX NOW pub protocol + +#ifndef _BBNOW_PUB_H +#define _BBNOW_PUB_H + +#include +#include +#include +#include + +#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 serialize(); + /// Serializes the packet into the given vector. + /// + /// Returns the number of bytes written into the buffer. + 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. + BBNowPubPacket() = default; + /// Constructs a new `Discovery` type packet with the given data. + static BBNowPubPacket new_discovery(BlkBoxNowPubDiscoveryData data); +}; + +#endif /* _BBNOW_PUB_H */ diff --git a/include/blk_box_common.hpp b/include/blk_box_common.hpp deleted file mode 100644 index 8be6db1..0000000 --- a/include/blk_box_common.hpp +++ /dev/null @@ -1,62 +0,0 @@ -/// 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 */