split pub and priv

This commit is contained in:
2026-06-30 15:54:17 -05:00
parent ff98d59039
commit d28e81c510
7 changed files with 135 additions and 100 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
idf_component_register( idf_component_register(
SRCS "blk_box_common.cpp" SRCS "bbnow_pub.cpp"
INCLUDE_DIRS "include" "." INCLUDE_DIRS "include" "."
# PRIV_REQUIRES # PRIV_REQUIRES
) )
+36 -37
View File
@@ -1,38 +1,31 @@
#include "blk_box_common.hpp" #include "bbnow_pub.hpp"
#include <cassert> #include <cassert>
#include <cstddef>
#include <cstring>
#include <optional>
#include <stdint.h>
#include <vector>
#include <esp_log.h> #include <esp_log.h>
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) { switch (this->packet_type) {
case BlkBoxNowPubPacketType::NONE: case BBNowPubPacketType::NONE:
return -1; return -1;
case BlkBoxNowPubPacketType::DISCOVERY: case BBNowPubPacketType::DISCOVERY:
return BlkBoxNowPubPacket::DISCOVERY_PACKET_SIZE; return BBNowPubPacket::DISCOVERY_PACKET_SIZE;
} }
return -1; return -1;
} }
std::vector<uint8_t> BlkBoxNowPubPacket::serialize() { std::vector<uint8_t> BBNowPubPacket::serialize() {
size_t size = this->serialize_size();
if (size == -1) {
return std::vector<uint8_t>(0);
}
auto buf = std::vector<uint8_t>(); auto buf = std::vector<uint8_t>();
buf.reserve(size);
if (this->serialize_size() == -1) {
return buf;
}
this->serialize_into(buf); this->serialize_into(buf);
return buf; return buf;
} }
/// Serializes the packet into the given vector. size_t BBNowPubPacket::serialize_into(std::vector<uint8_t>& buf) {
size_t BlkBoxNowPubPacket::serialize_into(std::vector<uint8_t>& buf) {
size_t initial_size = buf.size(); size_t initial_size = buf.size();
size_t size = this->serialize_size(); size_t size = this->serialize_size();
if (size == -1) { if (size == -1) {
@@ -48,14 +41,16 @@ size_t BlkBoxNowPubPacket::serialize_into(std::vector<uint8_t>& buf) {
buf.push_back((PUB_MAGIC_NUMBER >> 0) & 0xFF); buf.push_back((PUB_MAGIC_NUMBER >> 0) & 0xFF);
// push packet type // push packet type
static_assert(sizeof(this->packet_type) == 1, "expect type field to be 1 byte"); static_assert(sizeof(this->packet_type) == 2, "expect type field to be 2 bytes");
buf.push_back(static_cast<uint8_t>(this->packet_type)); uint16_t packet_type = static_cast<uint16_t>(this->packet_type);
buf.push_back((packet_type >> 8) & 0xFF);
buf.push_back((packet_type >> 0) & 0xFF);
switch (this->packet_type) { switch (this->packet_type) {
case BlkBoxNowPubPacketType::NONE: case BBNowPubPacketType::NONE:
break; break;
case BlkBoxNowPubPacketType::DISCOVERY: case BBNowPubPacketType::DISCOVERY:
static_assert(sizeof(this->packet_data.discovery) == 2, "expect discovery data to be 2 byte"); static_assert(sizeof(this->packet_data.discovery) == 2, "expect discovery data to be 2 bytes");
uint16_t data = static_cast<uint16_t>(this->packet_data.discovery.expantion_type); uint16_t data = static_cast<uint16_t>(this->packet_data.discovery.expantion_type);
buf.push_back((data >> 8) & 0xFF); buf.push_back((data >> 8) & 0xFF);
buf.push_back((data >> 0) & 0xFF); buf.push_back((data >> 0) & 0xFF);
@@ -67,12 +62,15 @@ size_t BlkBoxNowPubPacket::serialize_into(std::vector<uint8_t>& buf) {
} }
/// Deserializes a packet from the given bytes. /// Deserializes a packet from the given bytes.
std::optional<BlkBoxNowPubPacket> BlkBoxNowPubPacket::deserialize(std::span<const uint8_t> bytes) { std::optional<BBNowPubPacket> BBNowPubPacket::deserialize(std::span<const uint8_t> bytes) {
// first check for magic number and type // 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 // not enough room for magic number and type
return std::nullopt; return std::nullopt;
} }
static_assert(sizeof(PUB_MAGIC_NUMBER) == 4, "expect magic number to be 4 bytes");
uint32_t magic_number = uint32_t magic_number =
(uint32_t)(bytes[0] << 24) | (uint32_t)(bytes[0] << 24) |
(uint32_t)(bytes[1] << 16) | (uint32_t)(bytes[1] << 16) |
@@ -85,15 +83,16 @@ std::optional<BlkBoxNowPubPacket> BlkBoxNowPubPacket::deserialize(std::span<cons
} }
// magic number matches! this is a blk_box pub packet! // magic number matches! this is a blk_box pub packet!
BlkBoxNowPubPacket packet; BBNowPubPacket packet;
uint8_t packet_type = bytes[4]; static_assert(sizeof(BBNowPubPacketType) == 2, "expect packet type to be 2 bytes");
packet.packet_type = static_cast<BlkBoxNowPubPacketType>(packet_type); uint16_t packet_type = ((uint16_t)bytes[4] << 8) | ((uint16_t)bytes[5]);
if (packet.packet_type > BlkBoxNowPubPacketType::MAX) { packet.packet_type = static_cast<BBNowPubPacketType>(packet_type);
if (packet.packet_type > BBNowPubPacketType::MAX) {
ESP_LOGW(TAG, "BlkBoxPubPacketType was NONE! discarding"); ESP_LOGW(TAG, "BlkBoxPubPacketType was NONE! discarding");
return std::nullopt; return std::nullopt;
} }
if (packet.packet_type > BlkBoxNowPubPacketType::MAX) { if (packet.packet_type > BBNowPubPacketType::MAX) {
ESP_LOGW(TAG, "BlkBoxPubPacketType not in range! discarding"); ESP_LOGW(TAG, "BlkBoxPubPacketType not in range! discarding");
return std::nullopt; return std::nullopt;
} }
@@ -110,10 +109,10 @@ std::optional<BlkBoxNowPubPacket> BlkBoxNowPubPacket::deserialize(std::span<cons
} }
switch (packet.packet_type) { switch (packet.packet_type) {
case BlkBoxNowPubPacketType::NONE: case BBNowPubPacketType::NONE:
break; break;
case BlkBoxNowPubPacketType::DISCOVERY: case BBNowPubPacketType::DISCOVERY:
uint16_t device = ((uint16_t)bytes[5] << 8) | ((uint16_t)bytes[6]); uint16_t device = ((uint16_t)bytes[6] << 8) | ((uint16_t)bytes[7]);
packet.packet_data.discovery.expantion_type = static_cast<BlkBoxExpantion>(device); packet.packet_data.discovery.expantion_type = static_cast<BlkBoxExpantion>(device);
if (packet.packet_data.discovery.expantion_type > BlkBoxExpantion::MAX) { if (packet.packet_data.discovery.expantion_type > BlkBoxExpantion::MAX) {
ESP_LOGW(TAG, "BlkBoxExpantion not in range! discarding"); ESP_LOGW(TAG, "BlkBoxExpantion not in range! discarding");
@@ -124,9 +123,9 @@ std::optional<BlkBoxNowPubPacket> BlkBoxNowPubPacket::deserialize(std::span<cons
return packet; return packet;
} }
BlkBoxNowPubPacket BlkBoxNowPubPacket::new_discovery(BlkBoxNowPubDiscoveryData data) { BBNowPubPacket BBNowPubPacket::new_discovery(BlkBoxNowPubDiscoveryData data) {
BlkBoxNowPubPacket packet; BBNowPubPacket packet;
packet.packet_type = BlkBoxNowPubPacketType::DISCOVERY; packet.packet_type = BBNowPubPacketType::DISCOVERY;
packet.packet_data.discovery = data; packet.packet_data.discovery = data;
return packet; return packet;
} }
+16
View File
@@ -0,0 +1,16 @@
/// Types for the BLK_BOX expantion devices
#ifndef _BB_EXPANTIONS_H
#define _BB_EXPANTIONS_H
#include <stdint.h>
/// 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 */
+9
View File
@@ -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 */
+6
View File
@@ -0,0 +1,6 @@
/// Types for the BLK_BOX NOW priv protocol
#ifndef _BBNOW_PRIV_H
#define _BBNOW_PRIV_H
#endif /* _BBNOW_PRIV_H */
+67
View File
@@ -0,0 +1,67 @@
/// 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 */
-62
View File
@@ -1,62 +0,0 @@
/// Types common across the blk_box ecosystem
#ifndef _BLK_BOX_COMMON_H
#define _BLK_BOX_COMMON_H
#include <cstddef>
#include <stdint.h>
#include <vector>
#include <span>
#include <optional>
/// 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<uint8_t> serialize();
/// Serializes the packet into the given vector.
size_t serialize_into(std::vector<uint8_t>& buf);
/// Deserializes a packet from the given bytes.
static std::optional<BlkBoxNowPubPacket> deserialize(std::span<const uint8_t> bytes);
/// Constructs an empty packet.
BlkBoxNowPubPacket() = default;
static BlkBoxNowPubPacket new_discovery(BlkBoxNowPubDiscoveryData data);
};
#endif /* _BLK_BOX_COMMON_H */