Move headers and add string name to discovery response
This commit is contained in:
+15
-8
@@ -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,
|
||||
});
|
||||
|
||||
|
||||
+28
-1
@@ -1,6 +1,8 @@
|
||||
#include "bbnow_priv.hpp"
|
||||
#include "bbnow/priv.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <esp_log.h>
|
||||
|
||||
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<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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
#include "bbnow_pub.hpp"
|
||||
#include "bbnow/pub.h"
|
||||
#include <cassert>
|
||||
#include <esp_log.h>
|
||||
#include <utility>
|
||||
|
||||
@@ -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 <array>
|
||||
#include <cstdint>
|
||||
@@ -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<PairedExpander> paired_expanders;
|
||||
};
|
||||
|
||||
#endif /* _BBNOW_DRIVER_HPP */
|
||||
#endif /* BBNOW_DRIVER_HPP */
|
||||
@@ -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 <stdint.h>
|
||||
#include <vector>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
|
||||
#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 */
|
||||
@@ -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 <stdint.h>
|
||||
#include <vector>
|
||||
@@ -66,4 +66,4 @@ struct BBNowPubPacket {
|
||||
static BBNowPubPacket new_discovery(BlkBoxNowPubDiscoveryData data);
|
||||
};
|
||||
|
||||
#endif /* _BBNOW_PUB_H */
|
||||
#endif /* BBNOW_PUB_HPP */
|
||||
@@ -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 */
|
||||
@@ -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 */
|
||||
Reference in New Issue
Block a user