Files
blk_box_lib/include/blk_box_drivers/bbnow_old.hpp
T

60 lines
2.4 KiB
C++

#ifndef _BBNOW_OLD_HPP
#define _BBNOW_OLD_HPP
#include <stdint.h>
#include "esp_now.h"
#include "bbnow_pub.hpp" // BBNowPubPacket, BBNowPubPacketType
/// The channel to use if not on a WIFI network
#define BBNOW_DEFAULT_CHANNEL 6
const static uint8_t BROADCAST_MAC[ESP_NOW_ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
/// Whether a queue item carries a packet to transmit or one that was received.
enum class BBNowQueueItemType : uint8_t {
TX = 0, /// a packet to send over esp_now
RX = 1, /// a packet received over esp_now
};
/// The high-level protocol family of a packet flowing through the queue.
/// `PRIV` is plumbed end-to-end but not yet implemented (see `bbnow_send_priv`).
enum class BBNowPacketKind : uint8_t {
PUB = 0, /// a BBNOW public packet
PRIV = 1, /// a BBNOW private packet (handling not implemented yet)
};
/// Item handed between callers, the esp_now callbacks, and the bbnow task.
/// A fixed-size buffer is used (rather than `std::vector`) so the item can be
/// copied by value through a FreeRTOS queue without dangling ownership.
struct BBNowQueueItem {
BBNowQueueItemType type{};
BBNowPacketKind kind{};
uint8_t addr[ESP_NOW_ETH_ALEN]{}; /// dest MAC for TX, src MAC for RX
uint8_t payload[ESP_NOW_MAX_DATA_LEN]{};
size_t len{};
};
/// Initializes esp_now and the bbnow packet queue.
void init_espnow();
/// Serializes and enqueues a public BBNOW packet for transmission.
/// \return ESP_OK if queued, or an error if the packet failed to serialize
/// or the bbnow task/queue is not running.
esp_err_t bbnow_send_pub(const BBNowPubPacket& pkt, const uint8_t* dest = BROADCAST_MAC);
/// Serializes and enqueues a private BBNOW packet for transmission.
/// \note SCAFFOLD: not yet implemented. Returns ESP_ERR_NOT_SUPPORTED.
/// When implemented it will mirror `bbnow_send_pub`, setting
/// `kind = BBNowPacketKind::PRIV` so the task and RX handler already
/// route it correctly.
esp_err_t bbnow_send_priv(const uint8_t* dest);
/// Creates the bbnow task that drains the queue: it sends TX items via
/// esp_now and dispatches RX items to the packet handlers. Idempotent.
void bbnow_start_task();
/// Hands a received packet to the bbnow queue for handling.
/// Used by the esp_now receive callback and for demos/tests of the RX path.
esp_err_t bbnow_enqueue_received(const uint8_t* src, const uint8_t* data, size_t len);
#endif /* _BBNOW_OLD_HPP */