diff --git a/bbnow_driver.cpp b/bbnow_driver.cpp index ffb59b3..cbb11a2 100644 --- a/bbnow_driver.cpp +++ b/bbnow_driver.cpp @@ -1,4 +1,4 @@ -#include "bbnow_driver.hpp" +#include "bbnow/driver.h" #include "esp_log.h" #include "esp_now.h" @@ -25,17 +25,17 @@ struct ReceivedPacket { QueueHandle_t pub_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) { - return mac == BBNowDriver::MacAddress{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; + return mac == BROADCAST_MAC; } constexpr bool is_zero(const BBNowDriver::MacAddress &mac) { return mac == BBNowDriver::MacAddress{}; } -constexpr BBNowDriver::MacAddress BROADCAST_MAC = {0xff, 0xff, 0xff, - 0xff, 0xff, 0xff}; - bool is_broadcast(const uint8_t *mac) { return mac != nullptr && 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; } -esp_err_t BBNowDriver::pair_expander(BlkBoxExpantion type, - const MacAddress &mac) { +esp_err_t BBNowDriver::pair_expander( + BlkBoxExpantion type, + const MacAddress &mac, + std::string name +) { if (!initialized_) { return ESP_ERR_INVALID_STATE; } @@ -298,6 +301,10 @@ esp_err_t BBNowDriver::pair_expander(BlkBoxExpantion type, for (auto &expander : paired_expanders) { if (expander.mac == mac) { 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; } } @@ -315,7 +322,7 @@ esp_err_t BBNowDriver::pair_expander(BlkBoxExpantion type, paired_expanders.push_back(PairedExpander{ .mac = mac, - .name = {}, + .name = std::move(name), .type = type, }); diff --git a/bbnow_priv.cpp b/bbnow_priv.cpp index 9abe662..a97ce4e 100644 --- a/bbnow_priv.cpp +++ b/bbnow_priv.cpp @@ -1,6 +1,8 @@ -#include "bbnow_priv.hpp" +#include "bbnow/priv.h" +#include #include +#include #include 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 >> 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(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; } @@ -248,6 +266,15 @@ BBNowPrivPacket::deserialize( 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); break; } diff --git a/bbnow_pub.cpp b/bbnow_pub.cpp index c3b74de..086fd98 100644 --- a/bbnow_pub.cpp +++ b/bbnow_pub.cpp @@ -1,4 +1,4 @@ -#include "bbnow_pub.hpp" +#include "bbnow/pub.h" #include #include #include diff --git a/include/bbnow_driver.hpp b/include/bbnow/driver.h similarity index 89% rename from include/bbnow_driver.hpp rename to include/bbnow/driver.h index 54805f2..27a2799 100644 --- a/include/bbnow_driver.hpp +++ b/include/bbnow/driver.h @@ -1,7 +1,7 @@ /// Generic code for driving a BBNOW interface. -#ifndef _BBNOW_DRIVER_HPP -#define _BBNOW_DRIVER_HPP +#ifndef BBNOW_DRIVER_HPP +#define BBNOW_DRIVER_HPP #include #include @@ -13,8 +13,8 @@ #include "freertos/FreeRTOS.h" #include "bb_expantions.hpp" -#include "bbnow_pub.hpp" -#include "bbnow_priv.hpp" +#include "bbnow/pub.h" +#include "bbnow/priv.h" class BBNowDriver { public: @@ -57,7 +57,8 @@ public: static esp_err_t pair_expander( BlkBoxExpantion type, - const MacAddress& mac + const MacAddress& mac, + std::string name ); static esp_err_t unpair_expander( @@ -77,4 +78,4 @@ private: static std::vector paired_expanders; }; -#endif /* _BBNOW_DRIVER_HPP */ +#endif /* BBNOW_DRIVER_HPP */ diff --git a/include/bbnow_priv.hpp b/include/bbnow/priv.h similarity index 91% rename from include/bbnow_priv.hpp rename to include/bbnow/priv.h index 21a0b3c..a68c976 100644 --- a/include/bbnow_priv.hpp +++ b/include/bbnow/priv.h @@ -1,12 +1,13 @@ /// Types for the BLK_BOX NOW priv protocol -#ifndef _BBNOW_PRIV_H -#define _BBNOW_PRIV_H +#ifndef BBNOW_PRIV_HPP +#define BBNOW_PRIV_HPP #include #include #include #include +#include #include #include "bb_expantions.hpp" @@ -24,6 +25,10 @@ struct BBNowPrivAckData { /// The data associated with the `DISCOVERY_RESPONSE` type. struct BBNowPrivDiscoveryResponseData { 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. - const static size_t DISCOVERY_RESPONSE_PACKET_SIZE = 6; + const static size_t DISCOVERY_RESPONSE_PACKET_SIZE = 38; static_assert( DISCOVERY_RESPONSE_PACKET_SIZE == sizeof(BBNowPrivPacketType) + sizeof(uint16_t) + - sizeof(BBNowPrivDiscoveryResponseData), + sizeof(BlkBoxExpantion) + + EXPANDER_DEVICE_NAME_MAX_LEN, "Discovery response packet size should match it's data size" ); @@ -143,4 +149,4 @@ struct BBNowPrivPacket { ); }; -#endif /* _BBNOW_PRIV_H */ +#endif /* BBNOW_PRIV_HPP */ diff --git a/include/bbnow_pub.hpp b/include/bbnow/pub.h similarity index 96% rename from include/bbnow_pub.hpp rename to include/bbnow/pub.h index bd1451a..3f69436 100644 --- a/include/bbnow_pub.hpp +++ b/include/bbnow/pub.h @@ -1,7 +1,7 @@ /// Types for the BLK_BOX NOW pub protocol -#ifndef _BBNOW_PUB_H -#define _BBNOW_PUB_H +#ifndef BBNOW_PUB_HPP +#define BBNOW_PUB_HPP #include #include @@ -66,4 +66,4 @@ struct BBNowPubPacket { static BBNowPubPacket new_discovery(BlkBoxNowPubDiscoveryData data); }; -#endif /* _BBNOW_PUB_H */ +#endif /* BBNOW_PUB_HPP */ diff --git a/include/bbnow/types.h b/include/bbnow/types.h new file mode 100644 index 0000000..ce657cc --- /dev/null +++ b/include/bbnow/types.h @@ -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 */ diff --git a/include/bbnow_types.hpp b/include/bbnow_types.hpp deleted file mode 100644 index de06d19..0000000 --- a/include/bbnow_types.hpp +++ /dev/null @@ -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 */