Move headers and add string name to discovery response

This commit is contained in:
2026-09-18 14:07:18 -05:00
parent f85dee5d7a
commit 4fc4eb99ea
8 changed files with 74 additions and 33 deletions
+15 -8
View File
@@ -1,4 +1,4 @@
#include "bbnow_driver.hpp" #include "bbnow/driver.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_now.h" #include "esp_now.h"
@@ -25,17 +25,17 @@ struct ReceivedPacket {
QueueHandle_t pub_queue = nullptr; QueueHandle_t pub_queue = nullptr;
QueueHandle_t priv_queue = nullptr; QueueHandle_t priv_queue = nullptr;
constexpr BBNowDriver::MacAddress BROADCAST_MAC =
{0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
constexpr bool is_broadcast(const BBNowDriver::MacAddress &mac) { constexpr bool is_broadcast(const BBNowDriver::MacAddress &mac) {
return mac == BBNowDriver::MacAddress{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; return mac == BROADCAST_MAC;
} }
constexpr bool is_zero(const BBNowDriver::MacAddress &mac) { constexpr bool is_zero(const BBNowDriver::MacAddress &mac) {
return mac == BBNowDriver::MacAddress{}; return mac == BBNowDriver::MacAddress{};
} }
constexpr BBNowDriver::MacAddress BROADCAST_MAC = {0xff, 0xff, 0xff,
0xff, 0xff, 0xff};
bool is_broadcast(const uint8_t *mac) { bool is_broadcast(const uint8_t *mac) {
return mac != nullptr && return mac != nullptr &&
std::memcmp(mac, BROADCAST_MAC.data(), BROADCAST_MAC.size()) == 0; std::memcmp(mac, BROADCAST_MAC.data(), BROADCAST_MAC.size()) == 0;
@@ -286,8 +286,11 @@ size_t BBNowDriver::send_discovery(BlkBoxExpantion type) {
return send_pub(packet) == ESP_OK ? size : 0; return send_pub(packet) == ESP_OK ? size : 0;
} }
esp_err_t BBNowDriver::pair_expander(BlkBoxExpantion type, esp_err_t BBNowDriver::pair_expander(
const MacAddress &mac) { BlkBoxExpantion type,
const MacAddress &mac,
std::string name
) {
if (!initialized_) { if (!initialized_) {
return ESP_ERR_INVALID_STATE; return ESP_ERR_INVALID_STATE;
} }
@@ -298,6 +301,10 @@ esp_err_t BBNowDriver::pair_expander(BlkBoxExpantion type,
for (auto &expander : paired_expanders) { for (auto &expander : paired_expanders) {
if (expander.mac == mac) { if (expander.mac == mac) {
expander.type = type; expander.type = type;
// Preserve a stored name when re-pairing with an empty one.
if (!name.empty()) {
expander.name = std::move(name);
}
return ESP_OK; return ESP_OK;
} }
} }
@@ -315,7 +322,7 @@ esp_err_t BBNowDriver::pair_expander(BlkBoxExpantion type,
paired_expanders.push_back(PairedExpander{ paired_expanders.push_back(PairedExpander{
.mac = mac, .mac = mac,
.name = {}, .name = std::move(name),
.type = type, .type = type,
}); });
+28 -1
View File
@@ -1,6 +1,8 @@
#include "bbnow_priv.hpp" #include "bbnow/priv.h"
#include <algorithm>
#include <cassert> #include <cassert>
#include <cstring>
#include <esp_log.h> #include <esp_log.h>
const static char TAG[] = "bbnow_priv"; const static char TAG[] = "bbnow_priv";
@@ -105,6 +107,22 @@ size_t BBNowPrivPacket::serialize_into(
buf.push_back((expantion_type >> 8) & 0xFF); buf.push_back((expantion_type >> 8) & 0xFF);
buf.push_back((expantion_type >> 0) & 0xFF); buf.push_back((expantion_type >> 0) & 0xFF);
// Push the device name, NUL-padded to the fixed field width.
// Longer names are truncated to `EXPANDER_DEVICE_NAME_MAX_LEN`.
const size_t name_len = std::min(
data.name.size(),
static_cast<size_t>(EXPANDER_DEVICE_NAME_MAX_LEN)
);
buf.insert(
buf.end(), data.name.begin(), data.name.begin() + name_len
);
buf.insert(
buf.end(),
EXPANDER_DEVICE_NAME_MAX_LEN - name_len,
'\0'
);
break; break;
} }
@@ -248,6 +266,15 @@ BBNowPrivPacket::deserialize(
return std::nullopt; return std::nullopt;
} }
// Read the NUL-padded device name.
data.name.assign(
bytes.begin() + 6,
bytes.begin() + 6 + EXPANDER_DEVICE_NAME_MAX_LEN
);
data.name.resize(
strnlen(data.name.data(), EXPANDER_DEVICE_NAME_MAX_LEN)
);
packet.packet_data = std::move(data); packet.packet_data = std::move(data);
break; break;
} }
+1 -1
View File
@@ -1,4 +1,4 @@
#include "bbnow_pub.hpp" #include "bbnow/pub.h"
#include <cassert> #include <cassert>
#include <esp_log.h> #include <esp_log.h>
#include <utility> #include <utility>
@@ -1,7 +1,7 @@
/// Generic code for driving a BBNOW interface. /// Generic code for driving a BBNOW interface.
#ifndef _BBNOW_DRIVER_HPP #ifndef BBNOW_DRIVER_HPP
#define _BBNOW_DRIVER_HPP #define BBNOW_DRIVER_HPP
#include <array> #include <array>
#include <cstdint> #include <cstdint>
@@ -13,8 +13,8 @@
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "bb_expantions.hpp" #include "bb_expantions.hpp"
#include "bbnow_pub.hpp" #include "bbnow/pub.h"
#include "bbnow_priv.hpp" #include "bbnow/priv.h"
class BBNowDriver { class BBNowDriver {
public: public:
@@ -57,7 +57,8 @@ public:
static esp_err_t pair_expander( static esp_err_t pair_expander(
BlkBoxExpantion type, BlkBoxExpantion type,
const MacAddress& mac const MacAddress& mac,
std::string name
); );
static esp_err_t unpair_expander( static esp_err_t unpair_expander(
@@ -77,4 +78,4 @@ private:
static std::vector<PairedExpander> paired_expanders; static std::vector<PairedExpander> paired_expanders;
}; };
#endif /* _BBNOW_DRIVER_HPP */ #endif /* BBNOW_DRIVER_HPP */
@@ -1,12 +1,13 @@
/// Types for the BLK_BOX NOW priv protocol /// Types for the BLK_BOX NOW priv protocol
#ifndef _BBNOW_PRIV_H #ifndef BBNOW_PRIV_HPP
#define _BBNOW_PRIV_H #define BBNOW_PRIV_HPP
#include <stdint.h> #include <stdint.h>
#include <vector> #include <vector>
#include <optional> #include <optional>
#include <span> #include <span>
#include <string>
#include <variant> #include <variant>
#include "bb_expantions.hpp" #include "bb_expantions.hpp"
@@ -24,6 +25,10 @@ struct BBNowPrivAckData {
/// The data associated with the `DISCOVERY_RESPONSE` type. /// The data associated with the `DISCOVERY_RESPONSE` type.
struct BBNowPrivDiscoveryResponseData { struct BBNowPrivDiscoveryResponseData {
BlkBoxExpantion expantion_type{}; BlkBoxExpantion expantion_type{};
/// The advertised device name. NUL-padded to
/// `EXPANDER_DEVICE_NAME_MAX_LEN` on the wire; longer names are
/// truncated.
std::string name{};
}; };
/////////////////////////////////////// ///////////////////////////////////////
@@ -76,11 +81,12 @@ struct BBNowPrivPacket {
); );
/// Discovery response packet size. /// Discovery response packet size.
const static size_t DISCOVERY_RESPONSE_PACKET_SIZE = 6; const static size_t DISCOVERY_RESPONSE_PACKET_SIZE = 38;
static_assert( static_assert(
DISCOVERY_RESPONSE_PACKET_SIZE == sizeof(BBNowPrivPacketType) + DISCOVERY_RESPONSE_PACKET_SIZE == sizeof(BBNowPrivPacketType) +
sizeof(uint16_t) + sizeof(uint16_t) +
sizeof(BBNowPrivDiscoveryResponseData), sizeof(BlkBoxExpantion) +
EXPANDER_DEVICE_NAME_MAX_LEN,
"Discovery response packet size should match it's data size" "Discovery response packet size should match it's data size"
); );
@@ -143,4 +149,4 @@ struct BBNowPrivPacket {
); );
}; };
#endif /* _BBNOW_PRIV_H */ #endif /* BBNOW_PRIV_HPP */
@@ -1,7 +1,7 @@
/// Types for the BLK_BOX NOW pub protocol /// Types for the BLK_BOX NOW pub protocol
#ifndef _BBNOW_PUB_H #ifndef BBNOW_PUB_HPP
#define _BBNOW_PUB_H #define BBNOW_PUB_HPP
#include <stdint.h> #include <stdint.h>
#include <vector> #include <vector>
@@ -66,4 +66,4 @@ struct BBNowPubPacket {
static BBNowPubPacket new_discovery(BlkBoxNowPubDiscoveryData data); static BBNowPubPacket new_discovery(BlkBoxNowPubDiscoveryData data);
}; };
#endif /* _BBNOW_PUB_H */ #endif /* BBNOW_PUB_HPP */
+9
View File
@@ -0,0 +1,9 @@
/// Types for the BLK_BOX NOW protocol
#ifndef BBNOW_TYPES_HPP
#define BBNOW_TYPES_HPP
#include "bbnow/pub.h" // IWYU pragma: keep
#include "bbnow/priv.h" // IWYU pragma: keep
#endif /* BBNOW_TYPES_HPP */
-9
View File
@@ -1,9 +0,0 @@
/// Types for the BLK_BOX NOW protocol
#ifndef _BBNOW_TYPES_H
#define _BBNOW_TYPES_H
#include "bbnow_pub.hpp" // IWYU pragma: keep
#include "bbnow_priv.hpp" // IWYU pragma: keep
#endif /* _BBNOW_TYPES_H */