241 size_t write(
const uint8_t* buffer,
size_t size)
override {
243 for (
size_t i = 0; i < size; i++) {
244 if (
write(buffer[i]) == 1) {
259 size_t write(
const uint8_t
byte)
override {
283 uint32_t
end = millis() + _timeout;
399 if (buffer_size > 0 && buffer_size <=
getMaxMTU()) {
403 ESP_LOGE(
TAG,
"Invalid TX buffer size: %d. Must be between 1 and %d.",
416 static constexpr const char*
TAG =
"ESP32TransceiverStream";
417 static constexpr int MTU = 116;
466 size_t read_bytes = 0;
471 if (read_bytes != 0) {
472 ESP_LOGE(
TAG,
"Invalid packet size received: %d", read_bytes);
479 ESP_LOGE(
TAG,
"Failed to parse frame");
490 int expected = (
last_seq + 1) % 256;
492 ESP_LOGI(
TAG,
"Retransmission ignored: seq %d", seq);
494 }
else if (seq != expected) {
495 ESP_LOGI(
TAG,
"Frame sequence skipped: expected %d, got %d", expected,
505 ESP_LOGD(
TAG,
"Received frame payload too large for buffer: %d bytes",
532 ESP_LOGD(
TAG,
"Attempt %d: Sending frame, len: %d", attempt, len);
534 ESP_LOGE(
TAG,
"Failed to send frame: size %d", len);
542 millis() < timeout) {
583 ESP_LOGD(
TAG,
"Sending frame, len: %d", len);
587 ESP_LOGE(
TAG,
"Failed to send frame: size %d", len);
596 const uint8_t*
frame,
const uint8_t* ack,
597 esp_ieee802154_frame_info_t* ack_frame_info,
void* user_data) {
608 const uint8_t*
frame, esp_ieee802154_tx_error_t error,
void* user_data) {
IEEE 802.15.4 Address abstraction.
Definition Frame.h:77
Class to manage an IEEE 802.15.4 transceiver using the ESP-IDF API. On the sending side we support br...
Definition ESP32TransceiverIEEE802_15_4.h:109
void incrementSequenceNumber(int n=1)
Increment the sequence number in the current frame by a specified value.
Definition ESP32TransceiverIEEE802_15_4.h:377
bool isCCAActive() const
Check if CCA (Clear Channel Assessment) is enabled.
Definition ESP32TransceiverIEEE802_15_4.h:419
bool setRxWhenIdleActive(bool rx_when_idle)
Set RX when idle mode for the transceiver.
Definition ESP32TransceiverIEEE802_15_4.cpp:451
void setReceiveBufferSize(int size)
Definition ESP32TransceiverIEEE802_15_4.cpp:288
void setReceiveTask(void(*task)(void *pvParameters))
Set a custom FreeRTOS task function for processing received packets.
Definition ESP32TransceiverIEEE802_15_4.h:354
int8_t getTxPower()
Get the current transmit power of the transceiver.
Definition ESP32TransceiverIEEE802_15_4.h:263
bool setChannel(channel_t channel)
Change the IEEE 802.15.4 channel.
Definition ESP32TransceiverIEEE802_15_4.cpp:256
uint32_t getAckTimeoutUs() const
Get the current acknowledgment timeout in microseconds.
Definition ESP32TransceiverIEEE802_15_4.h:406
bool setPromiscuousModeActive(bool promiscuous)
Set promiscuous mode for the transceiver.
Definition ESP32TransceiverIEEE802_15_4.cpp:441
bool setTxFailedCallback(ieee802154_transceiver_tx_failed_callback_t callback, void *user_data)
Set the callback function for failed frame transmission.
Definition ESP32TransceiverIEEE802_15_4.cpp:340
void setFrameControlField(const FrameControlField &fcf)
Set the Frame Control Field (FCF) for outgoing frames.
Definition ESP32TransceiverIEEE802_15_4.h:331
void setCCAActive(bool cca_enabled)
Enable or disable CCA (Clear Channel Assessment).
Definition ESP32TransceiverIEEE802_15_4.h:413
void setDestinationAddress(const Address &address)
Set the destination address for outgoing frames.
Definition ESP32TransceiverIEEE802_15_4.h:317
bool end(void)
Deinitialize the IEEE 802.15.4 transceiver.
Definition ESP32TransceiverIEEE802_15_4.cpp:154
bool send(uint8_t *data, size_t len)
Transmit an IEEE 802.15.4 frame on the current channel.
Definition ESP32TransceiverIEEE802_15_4.cpp:227
bool setCoordinatorActive(bool coordinator)
Set the coordinator mode for the transceiver.
Definition ESP32TransceiverIEEE802_15_4.cpp:432
void setBroadcast()
Set the destination address to the broadcast address (0xFFFF).
Definition ESP32TransceiverIEEE802_15_4.h:324
void setAutoIncrementSequenceNumber(bool auto_increment)
Enable or disable automatic incrementing of the sequence number after each successful transmission.
Definition ESP32TransceiverIEEE802_15_4.h:388
StreamBufferHandle_t getMessageBuffer() const
Get the FreeRTOS message buffer handle for received frames.
Definition ESP32TransceiverIEEE802_15_4.h:368
bool begin()
Initialize the IEEE 802.15.4 transceiver with a specified channel.
Definition ESP32TransceiverIEEE802_15_4.cpp:23
bool setTxDoneCallback(ieee802154_transceiver_tx_done_callback_t callback, void *user_data)
Set the callback function for successful frame transmission.
Definition ESP32TransceiverIEEE802_15_4.cpp:333
channel_t getChannel() const
Get the current channel of the transceiver.
Definition ESP32TransceiverIEEE802_15_4.h:249
bool setTxPower(int power)
Set the transmit power of the transceiver.
Definition ESP32TransceiverIEEE802_15_4.cpp:460
FrameControlField & getFrameControlField()
Get a reference to the Frame Control Field (FCF) for outgoing frames.
Definition ESP32TransceiverIEEE802_15_4.h:341
void setAckTimeoutUs(uint32_t timeout_us)
Set the time in us to wait for the ack frame.
Definition ESP32TransceiverIEEE802_15_4.h:398
Arduino Stream interface for IEEE 802.15.4 transceiver.
Definition ESP32TransceiverStreamIEEE802_15_4.h:29
void setBroadcast()
Set the destination address to the broadcast address (0xFFFF).
Definition ESP32TransceiverStreamIEEE802_15_4.h:375
void setFrameControlField(const FrameControlField &fcf)
Set the Frame Control Field (FCF) for outgoing frames.
Definition ESP32TransceiverStreamIEEE802_15_4.h:105
ESP32TransceiverIEEE802_15_4 & getTransceiver()
Get a reference to the underlying transceiver instance.
Definition ESP32TransceiverStreamIEEE802_15_4.h:381
int getSendDelay() const
Get the delay between send retries in milliseconds.
Definition ESP32TransceiverStreamIEEE802_15_4.h:89
static void ieee802154_transceiver_tx_done_callback(const uint8_t *frame, const uint8_t *ack, esp_ieee802154_frame_info_t *ack_frame_info, void *user_data)
Callback for successful frame transmission.
Definition ESP32TransceiverStreamIEEE802_15_4.h:595
RingBuffer tx_buffer
Definition ESP32TransceiverStreamIEEE802_15_4.h:423
bool setPromiscuousModeActive(bool promiscuous)
Set promiscuous mode for the transceiver.
Definition ESP32TransceiverStreamIEEE802_15_4.h:368
bool begin()
Initialize the stream and underlying transceiver.
Definition ESP32TransceiverStreamIEEE802_15_4.h:199
void setSendRetryCount(int count)
Defines the retry count for faild send requests.
Definition ESP32TransceiverStreamIEEE802_15_4.h:178
bool setTxPower(int power)
Set the transmit power in dBm.
Definition ESP32TransceiverStreamIEEE802_15_4.h:352
RingBuffer rx_buffer
Definition ESP32TransceiverStreamIEEE802_15_4.h:422
int getTxBufferSize() const
Get the current transmit buffer size for the stream.
Definition ESP32TransceiverStreamIEEE802_15_4.h:412
bool receive()
Internal method to receive frames and fill the receive buffer.
Definition ESP32TransceiverStreamIEEE802_15_4.h:451
void setRxBufferSize(size_t size)
Set the size of the receive buffer. This defines how many bytes we can get by calling readBytes();.
Definition ESP32TransceiverStreamIEEE802_15_4.h:156
void setSendDelay(int delay_ms)
Set the delay between sends or send retries.
Definition ESP32TransceiverStreamIEEE802_15_4.h:172
bool is_send_confirations_enabled
Definition ESP32TransceiverStreamIEEE802_15_4.h:433
void setAckTimeoutUs(uint32_t timeout_us)
Set the time in us to wait for the ack frame.
Definition ESP32TransceiverStreamIEEE802_15_4.h:123
int peek()
Peek at the next byte in the receive buffer (not implemented).
Definition ESP32TransceiverStreamIEEE802_15_4.h:293
bool setRxWhenIdleActive(bool rx_when_idle)
Set RX when idle mode for the transceiver.
Definition ESP32TransceiverStreamIEEE802_15_4.h:96
bool setTxBufferSize(int buffer_size)
Set the transmit buffer size for the stream. This defines how much data can be buffered for sending b...
Definition ESP32TransceiverStreamIEEE802_15_4.h:398
bool isAckActive() const
Check if acknowledgment requests are enabled for outgoing frames.
Definition ESP32TransceiverStreamIEEE802_15_4.h:147
bool isSendConfirmations()
Definition ESP32TransceiverStreamIEEE802_15_4.h:441
int getMaxMTU() const
Get the maximum transmission unit (MTU) size for the data content.
Definition ESP32TransceiverStreamIEEE802_15_4.h:388
bool owns_transceiver
Definition ESP32TransceiverStreamIEEE802_15_4.h:421
void setDestinationAddress(const Address &address)
Set the destination address for the stream.
Definition ESP32TransceiverStreamIEEE802_15_4.h:113
ESP32TransceiverStreamIEEE802_15_4(ESP32TransceiverIEEE802_15_4 &transceiver)
Construct a new ESP32TransceiverStream object.
Definition ESP32TransceiverStreamIEEE802_15_4.h:35
~ESP32TransceiverStreamIEEE802_15_4()
Destroy the ESP32TransceiverStream object.
Definition ESP32TransceiverStreamIEEE802_15_4.h:59
bool isCCAActive() const
Check if CCA (Clear Channel Assessment) is enabled.
Definition ESP32TransceiverStreamIEEE802_15_4.h:193
void sendWithoutConfirmations()
Internal method to send a frame without waiting for confirmations. Adds a delay after sending to allo...
Definition ESP32TransceiverStreamIEEE802_15_4.h:580
FrameControlField & getFrameControlField()
Get the current Frame Control Field (FCF) in use.
Definition ESP32TransceiverStreamIEEE802_15_4.h:69
bool is_auto_flush
Definition ESP32TransceiverStreamIEEE802_15_4.h:434
size_t write(const uint8_t *buffer, size_t size) override
Write a buffer of bytes to the transceiver.
Definition ESP32TransceiverStreamIEEE802_15_4.h:241
send_confirmation_state_t
Definition ESP32TransceiverStreamIEEE802_15_4.h:426
@ CONFIRMATION_ERROR
Definition ESP32TransceiverStreamIEEE802_15_4.h:429
@ WAITING_FOR_CONFIRMATION
Definition ESP32TransceiverStreamIEEE802_15_4.h:427
@ CONFIRMATION_RECEIVED
Definition ESP32TransceiverStreamIEEE802_15_4.h:428
void end()
Deinitialize the stream and underlying transceiver.
Definition ESP32TransceiverStreamIEEE802_15_4.h:233
static void ieee802154_transceiver_tx_failed_callback(const uint8_t *frame, esp_ieee802154_tx_error_t error, void *user_data)
Callback for failed frame transmission.
Definition ESP32TransceiverStreamIEEE802_15_4.h:607
esp_ieee802154_tx_error_t last_tx_error
Definition ESP32TransceiverStreamIEEE802_15_4.h:439
int8_t getTxPower() const
Get the current transmit power in dBm.
Definition ESP32TransceiverStreamIEEE802_15_4.h:345
bool setCoordinatorActive(bool coordinator)
Set the coordinator mode for the transceiver.
Definition ESP32TransceiverStreamIEEE802_15_4.h:359
static constexpr int MTU
Definition ESP32TransceiverStreamIEEE802_15_4.h:417
uint32_t getAckTimeoutUs() const
Get the current acknowledgment timeout in microseconds.
Definition ESP32TransceiverStreamIEEE802_15_4.h:131
volatile send_confirmation_state_t send_confirmation_state
Definition ESP32TransceiverStreamIEEE802_15_4.h:431
bool setChannel(channel_t channel)
Set the IEEE 802.15.4 channel.
Definition ESP32TransceiverStreamIEEE802_15_4.h:337
ESP32TransceiverIEEE802_15_4 * p_transceiver
Definition ESP32TransceiverStreamIEEE802_15_4.h:420
size_t getRxBufferSize() const
Get the current receive buffer size in bytes.
Definition ESP32TransceiverStreamIEEE802_15_4.h:77
int availableForWrite() override
Get the number of bytes available for writing.
Definition ESP32TransceiverStreamIEEE802_15_4.h:310
int receive_msg_buffer_size
Definition ESP32TransceiverStreamIEEE802_15_4.h:418
void setRxMessageBufferSize(int size)
Set the size of the message buffer used for receiving frames.
Definition ESP32TransceiverStreamIEEE802_15_4.h:163
int read() override
Read a single byte from the receive buffer.
Definition ESP32TransceiverStreamIEEE802_15_4.h:270
bool is_open_frame
Definition ESP32TransceiverStreamIEEE802_15_4.h:425
void flush() override
Flush the transmit buffer and send its contents as a frame.
Definition ESP32TransceiverStreamIEEE802_15_4.h:317
bool begin(FrameControlField fcf)
Initialize the stream and underlying transceiver.
Definition ESP32TransceiverStreamIEEE802_15_4.h:225
virtual int available() override
Get the number of bytes available to read.
Definition ESP32TransceiverStreamIEEE802_15_4.h:304
int send_delay_ms
Delay after sending a frame when confirmations are not used.
Definition ESP32TransceiverStreamIEEE802_15_4.h:436
size_t readBytes(uint8_t *buffer, size_t size)
Read multiple bytes from the receive buffer.
Definition ESP32TransceiverStreamIEEE802_15_4.h:281
bool isSequenceNumbers()
Definition ESP32TransceiverStreamIEEE802_15_4.h:443
void setAckActive(bool ack_active)
Enable or disable acknowledgment requests for outgoing frames.
Definition ESP32TransceiverStreamIEEE802_15_4.h:138
channel_t getChannel() const
Get the current IEEE 802.15.4 channel.
Definition ESP32TransceiverStreamIEEE802_15_4.h:330
Frame frame
Definition ESP32TransceiverStreamIEEE802_15_4.h:424
ESP32TransceiverStreamIEEE802_15_4(channel_t channel, int16_t panID, Address localAdr)
Construct a new ESP32TransceiverStream object with transceiver parameters.
Definition ESP32TransceiverStreamIEEE802_15_4.h:49
int last_seq
Definition ESP32TransceiverStreamIEEE802_15_4.h:437
static constexpr const char * TAG
Definition ESP32TransceiverStreamIEEE802_15_4.h:416
int getRxMessageBufferSize() const
Get the current message buffer size for receiving frames.
Definition ESP32TransceiverStreamIEEE802_15_4.h:83
size_t write(const uint8_t byte) override
Write a single byte to the transceiver.
Definition ESP32TransceiverStreamIEEE802_15_4.h:259
int send_retry_count
Definition ESP32TransceiverStreamIEEE802_15_4.h:438
void sendWithConfirmations()
Internal method to send a frame with confirmation handling. Retries sending the frame if confirmation...
Definition ESP32TransceiverStreamIEEE802_15_4.h:523
void setCCAActive(bool cca_enabled)
Enable or disable CCA (Clear Channel Assessment).
Definition ESP32TransceiverStreamIEEE802_15_4.h:185
Efficient ring buffer for storing frame data.
Definition RingBuffer.h:18
bool isEmpty() const
Definition RingBuffer.h:65
int readArray(uint8_t *dest, size_t len)
Definition RingBuffer.h:84
int read()
Definition RingBuffer.h:73
bool write(uint8_t byte)
Definition RingBuffer.h:30
int available() const
Definition RingBuffer.h:51
bool isFull() const
Definition RingBuffer.h:61
int availableForWrite() const
Definition RingBuffer.h:102
size_t size() const
Definition RingBuffer.h:69
void resize(size_t new_size)
Definition RingBuffer.h:24
bool peek(uint8_t &out) const
Definition RingBuffer.h:93
int writeArray(const uint8_t *data, size_t len)
Definition RingBuffer.h:38
Definition ESP32TransceiverIEEE802_15_4.cpp:15
channel_t
Enum for IEEE 802.15.4 channel numbers (11-26).
Definition ESP32TransceiverIEEE802_15_4.h:27
IEEE 802.15.4 Frame Control Field (FCF) structure Bit fields are ordered LSB to MSB to match IEEE 802...
Definition Frame.h:53
uint8_t sequenceNumberSuppression
Definition Frame.h:61
uint8_t ackRequest
Definition Frame.h:57
IEEE 802.15.4 MAC frame structure.
Definition Frame.h:183
uint8_t sequenceNumber
Definition Frame.h:185
uint8_t * payload
Definition Frame.h:193
size_t payloadLen
Definition Frame.h:192
bool parse(const uint8_t *data, bool verbose)
parse frame from raw data
Definition Frame.cpp:127
Structure to hold frame data and frame info.
Definition Frame.h:243
uint8_t frame[MAX_FRAME_LEN]
Definition Frame.h:244