ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
ld600x_frame.cpp
Go to the documentation of this file.
1#include "ld600x_frame.h"
2
3#include "esphome/core/log.h"
4
5#include <cstring>
6
7namespace esphome::ld600x {
8
9ESPHOME_LOG_TAG(TAG, "ld600x");
10
11void FrameParser::init(uint8_t *buffer, uint16_t capacity) {
12 this->data_buf_ = buffer;
13 this->max_data_len_ = capacity;
14 this->reset();
15}
16
18 this->pos_ = 0;
19 this->xor_ = TF_SOF;
21}
22
23bool FrameParser::feed(uint8_t byte) {
24 switch (this->parse_state_) {
26 // pos_ counts the payload down; the byte after it is the data checksum.
27 if (this->pos_ == 0) {
28 this->reset();
29 } else {
30 this->pos_--;
31 }
32 return false;
34 if (byte == TF_SOF)
35 this->start_header_();
36 return false;
38 // Big-endian id, length and type, shifted in as they arrive so the buffer only ever holds a payload
39 uint16_t &field = this->pos_ < 2 ? this->frame_id_ : this->pos_ < 4 ? this->data_len_ : this->frame_type_;
40 field = static_cast<uint16_t>((field << 8) | byte);
41 this->xor_ ^= byte;
42 if (++this->pos_ == TF_HEADER_LEN)
44 return false;
45 }
47 this->data_buf_[this->pos_++] = byte;
48 this->xor_ ^= byte;
49 if (this->pos_ >= this->data_len_) {
51 }
52 return false;
55 const bool header = this->parse_state_ == ParseState::PARSE_STATE_HCK;
56 if (byte != static_cast<uint8_t>(~this->xor_)) {
57 ESP_LOGV(TAG, "%s checksum mismatch", header ? LOG_STR_LITERAL("Header") : LOG_STR_LITERAL("Data"));
58 // The byte may be the SOF of the next frame when the real checksum was lost on the wire.
59 if (byte == TF_SOF) {
60 this->start_header_();
61 } else {
62 this->reset();
63 }
64 return false;
65 }
66 if (!header || this->data_len_ == 0) {
67 this->reset();
68 return true;
69 }
70 if (this->data_len_ > this->max_data_len_) {
71 ESP_LOGW(TAG, "Frame too large: %u", static_cast<unsigned>(this->data_len_));
72 this->pos_ = this->data_len_;
74 return false;
75 }
76 this->pos_ = 0;
77 this->xor_ = 0;
79 return false;
80 }
81 }
82 return false;
83}
84
85size_t encode_frame(uint16_t frame_id, uint16_t type, const uint8_t *data, uint16_t len, uint8_t *out) {
86 out[0] = TF_SOF;
87 put_be16(out + 1, frame_id);
88 put_be16(out + 3, len);
89 put_be16(out + 5, type);
90 // TinyFrame checksums are the inverted XOR of the bytes they cover, SOF included for the header.
91 out[TF_HEADER_LEN + 1] = static_cast<uint8_t>(~xor_bytes(out, TF_HEADER_LEN + 1));
92 size_t pos = TF_PAYLOAD_OFFSET;
93 if (len > 0 && data != nullptr) {
94 std::memcpy(out + pos, data, len);
95 pos += len;
96 out[pos++] = static_cast<uint8_t>(~xor_bytes(data, len));
97 }
98 return pos;
99}
100
101} // namespace esphome::ld600x
void init(uint8_t *buffer, uint16_t capacity)
uint8_t type
std::vector< uint8_t > & out
Definition gif.cpp:49
uint32_t buffer
Definition gif.cpp:50
ESPHOME_LOG_TAG(TAG, "ld600x")
size_t encode_frame(uint16_t frame_id, uint16_t type, const uint8_t *data, uint16_t len, uint8_t *out)
const void size_t len
Definition hal.h:68
size_t size_t pos
Definition helpers.h:1333
constexpr uint8_t * put_be16(uint8_t *dst, uint16_t value)
Write the low 16, 24 or 32 bits of value to dst, most significant byte first (be) or least significan...
Definition helpers.h:1112
uint16_t uint16_t & capacity
Definition helpers.cpp:34