Move headers and add string name to discovery response
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
/// Generic code for driving a BBNOW interface.
|
||||
|
||||
#ifndef BBNOW_DRIVER_HPP
|
||||
#define BBNOW_DRIVER_HPP
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "esp_err.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
|
||||
#include "bb_expantions.hpp"
|
||||
#include "bbnow/pub.h"
|
||||
#include "bbnow/priv.h"
|
||||
|
||||
class BBNowDriver {
|
||||
public:
|
||||
using MacAddress = std::array<uint8_t, 6>;
|
||||
|
||||
struct PairedExpander {
|
||||
MacAddress mac;
|
||||
std::string name;
|
||||
BlkBoxExpantion type;
|
||||
};
|
||||
|
||||
static constexpr uint8_t DEFAULT_CHANNEL = 6;
|
||||
|
||||
static esp_err_t init();
|
||||
static esp_err_t deinit();
|
||||
|
||||
static esp_err_t send_pub(const BBNowPubPacket& packet);
|
||||
static bool has_pub();
|
||||
static bool recv_pub(
|
||||
MacAddress& source,
|
||||
BBNowPubPacket& packet,
|
||||
TickType_t max_delay = 0
|
||||
);
|
||||
|
||||
static esp_err_t send_priv(
|
||||
const MacAddress& destination,
|
||||
const BBNowPrivPacket& packet
|
||||
);
|
||||
static bool has_priv();
|
||||
static bool recv_priv(
|
||||
MacAddress& source,
|
||||
BBNowPrivPacket& packet,
|
||||
TickType_t max_delay = 0
|
||||
);
|
||||
|
||||
/// Broadcast a discovery packet.
|
||||
static size_t send_discovery(
|
||||
BlkBoxExpantion type
|
||||
);
|
||||
|
||||
static esp_err_t pair_expander(
|
||||
BlkBoxExpantion type,
|
||||
const MacAddress& mac,
|
||||
std::string name
|
||||
);
|
||||
|
||||
static esp_err_t unpair_expander(
|
||||
const MacAddress& mac
|
||||
);
|
||||
|
||||
static std::span<const PairedExpander> get_paired_expanders();
|
||||
|
||||
private:
|
||||
static constexpr MacAddress BROADCAST_MAC = {
|
||||
0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff
|
||||
};
|
||||
|
||||
static bool initialized_;
|
||||
|
||||
static std::vector<PairedExpander> paired_expanders;
|
||||
};
|
||||
|
||||
#endif /* BBNOW_DRIVER_HPP */
|
||||
@@ -0,0 +1,152 @@
|
||||
/// Types for the BLK_BOX NOW priv protocol
|
||||
|
||||
#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"
|
||||
|
||||
///////////////////////////////////////
|
||||
// ACK //
|
||||
/////////////////////////////////////
|
||||
/// The data associated with the `ACK` type.
|
||||
struct BBNowPrivAckData {
|
||||
};
|
||||
|
||||
///////////////////////////////////////
|
||||
// DISCOVERY RESPONSE //
|
||||
/////////////////////////////////////
|
||||
/// 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{};
|
||||
};
|
||||
|
||||
///////////////////////////////////////
|
||||
// EXPANDER MESSAGE //
|
||||
/////////////////////////////////////
|
||||
/// The data associated with the `EXPANDER_MESSAGE` type.
|
||||
struct BBNowPrivExpanderMessageData {
|
||||
// TODO: this should get flattened into a concrete type, an enum over the expander types.
|
||||
std::vector<uint8_t> data;
|
||||
};
|
||||
|
||||
///////////////////////////////////////
|
||||
// INFO QUERY //
|
||||
/////////////////////////////////////
|
||||
/// The data associated with the `INFO_QUERY` type.
|
||||
struct BBNowPrivInfoQueryData {
|
||||
uint8_t battery_percentage;
|
||||
// TODO: add other status fields that may be useful to query.
|
||||
};
|
||||
|
||||
///////////////////////////////////////
|
||||
// PRIV PACKET //
|
||||
/////////////////////////////////////
|
||||
/// The packet type for `BBNowPrivPacket`s.
|
||||
enum class BBNowPrivPacketType: uint16_t {
|
||||
NONE = 0,
|
||||
ACK = 1,
|
||||
// TODO: we will need to add more here for handshaking in the future.
|
||||
DISCOVERY_RESPONSE = 2,
|
||||
EXPANDER_MESSAGE = 3,
|
||||
INFO_QUERY = 4,
|
||||
MAX = 4,
|
||||
};
|
||||
|
||||
/// The data field for the packet.
|
||||
using BBNowPrivPacketData = std::variant<
|
||||
std::monostate,
|
||||
BBNowPrivAckData,
|
||||
BBNowPrivDiscoveryResponseData,
|
||||
BBNowPrivExpanderMessageData,
|
||||
BBNowPrivInfoQueryData
|
||||
>;
|
||||
|
||||
struct BBNowPrivPacket {
|
||||
/// ACK packet size.
|
||||
const static size_t ACK_PACKET_SIZE = 4;
|
||||
static_assert(
|
||||
ACK_PACKET_SIZE == sizeof(BBNowPrivPacketType) + sizeof(uint16_t),
|
||||
"ACK packet size should match it's data size"
|
||||
);
|
||||
|
||||
/// Discovery response packet size.
|
||||
const static size_t DISCOVERY_RESPONSE_PACKET_SIZE = 38;
|
||||
static_assert(
|
||||
DISCOVERY_RESPONSE_PACKET_SIZE == sizeof(BBNowPrivPacketType) +
|
||||
sizeof(uint16_t) +
|
||||
sizeof(BlkBoxExpantion) +
|
||||
EXPANDER_DEVICE_NAME_MAX_LEN,
|
||||
"Discovery response packet size should match it's data size"
|
||||
);
|
||||
|
||||
/// Info query packet size.
|
||||
const static size_t INFO_QUERY_PACKET_SIZE = 5;
|
||||
static_assert(
|
||||
INFO_QUERY_PACKET_SIZE ==
|
||||
sizeof(BBNowPrivPacketType) +
|
||||
sizeof(uint16_t) +
|
||||
sizeof(BBNowPrivInfoQueryData),
|
||||
"Info query packet size should match its data size"
|
||||
);
|
||||
|
||||
/// The type of packet this is.
|
||||
BBNowPrivPacketType packet_type{};
|
||||
|
||||
/// Sequence number associated with this packet.
|
||||
uint16_t sequence{};
|
||||
|
||||
/// The data held by the packet.
|
||||
BBNowPrivPacketData packet_data{};
|
||||
|
||||
/// The size of the serialized packet (in bytes).
|
||||
size_t serialize_size() const;
|
||||
|
||||
/// Serializes the packet to a vector.
|
||||
std::vector<uint8_t> serialize() const;
|
||||
|
||||
/// 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) const;
|
||||
|
||||
/// Deserializes a packet from the given bytes.
|
||||
static std::optional<BBNowPrivPacket>
|
||||
deserialize(std::span<const uint8_t> bytes);
|
||||
|
||||
/// Constructs an empty packet.
|
||||
BBNowPrivPacket() = default;
|
||||
|
||||
/// Constructs a new `ACK` packet.
|
||||
static BBNowPrivPacket new_ack(uint16_t sequence);
|
||||
|
||||
/// Constructs a new `DISCOVERY_RESPONSE` packet.
|
||||
static BBNowPrivPacket new_discovery_response(
|
||||
uint16_t sequence,
|
||||
BBNowPrivDiscoveryResponseData data
|
||||
);
|
||||
|
||||
/// Constructs a new `EXPANDER_MESSAGE` packet.
|
||||
static BBNowPrivPacket new_expander_message(
|
||||
uint16_t sequence,
|
||||
BBNowPrivExpanderMessageData data
|
||||
);
|
||||
|
||||
/// Constructs a new `INFO_QUERY` packet.
|
||||
static BBNowPrivPacket new_info_query(
|
||||
uint16_t sequence,
|
||||
BBNowPrivInfoQueryData data
|
||||
);
|
||||
};
|
||||
|
||||
#endif /* BBNOW_PRIV_HPP */
|
||||
@@ -0,0 +1,69 @@
|
||||
/// Types for the BLK_BOX NOW pub protocol
|
||||
|
||||
#ifndef BBNOW_PUB_HPP
|
||||
#define BBNOW_PUB_HPP
|
||||
|
||||
#include <stdint.h>
|
||||
#include <vector>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <variant>
|
||||
|
||||
#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.
|
||||
using BlkBoxNowPubPacketData = std::variant<
|
||||
std::monostate,
|
||||
BlkBoxNowPubDiscoveryData
|
||||
>;
|
||||
|
||||
struct BBNowPubPacket {
|
||||
// "bbnp" in ascii
|
||||
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() const;
|
||||
/// Serializes the packet to a vector.
|
||||
std::vector<uint8_t> serialize() const;
|
||||
/// 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) const;
|
||||
/// 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_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 */
|
||||
Reference in New Issue
Block a user