ESPHome
2026.11.0b6
Loading...
Searching...
No Matches
esphome
components
ld600x
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
7
namespace
esphome::ld600x
{
8
9
ESPHOME_LOG_TAG
(TAG,
"ld600x"
);
10
11
void
FrameParser::init
(uint8_t *
buffer
, uint16_t
capacity
) {
12
this->
data_buf_
=
buffer
;
13
this->
max_data_len_
=
capacity
;
14
this->
reset
();
15
}
16
17
void
FrameParser::start_header_
() {
18
this->
pos_
= 0;
19
this->
xor_
= TF_SOF;
20
this->
parse_state_
=
ParseState::PARSE_STATE_HEADER
;
21
}
22
23
bool
FrameParser::feed
(uint8_t
byte
) {
24
switch
(this->
parse_state_
) {
25
case
ParseState::PARSE_STATE_DISCARD
:
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
;
33
case
ParseState::PARSE_STATE_SOF
:
34
if
(
byte
== TF_SOF)
35
this->
start_header_
();
36
return
false
;
37
case
ParseState::PARSE_STATE_HEADER
: {
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)
43
this->
parse_state_
=
ParseState::PARSE_STATE_HCK
;
44
return
false
;
45
}
46
case
ParseState::PARSE_STATE_DATA
:
47
this->
data_buf_
[this->
pos_
++] = byte;
48
this->
xor_
^= byte;
49
if
(this->
pos_
>= this->
data_len_
) {
50
this->
parse_state_
=
ParseState::PARSE_STATE_DCK
;
51
}
52
return
false
;
53
case
ParseState::PARSE_STATE_HCK
:
54
case
ParseState::PARSE_STATE_DCK
: {
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_
;
73
this->
parse_state_
=
ParseState::PARSE_STATE_DISCARD
;
74
return
false
;
75
}
76
this->
pos_
= 0;
77
this->
xor_
= 0;
78
this->
parse_state_
=
ParseState::PARSE_STATE_DATA
;
79
return
false
;
80
}
81
}
82
return
false
;
83
}
84
85
size_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
esphome::ld600x::FrameParser::max_data_len_
uint16_t max_data_len_
Definition
ld600x_frame.h:52
esphome::ld600x::FrameParser::feed
bool feed(uint8_t byte)
Definition
ld600x_frame.cpp:23
esphome::ld600x::FrameParser::frame_id_
uint16_t frame_id_
Definition
ld600x_frame.h:49
esphome::ld600x::FrameParser::start_header_
void start_header_()
Definition
ld600x_frame.cpp:17
esphome::ld600x::FrameParser::pos_
uint16_t pos_
Definition
ld600x_frame.h:51
esphome::ld600x::FrameParser::frame_type_
uint16_t frame_type_
Definition
ld600x_frame.h:48
esphome::ld600x::FrameParser::parse_state_
ParseState parse_state_
Definition
ld600x_frame.h:53
esphome::ld600x::FrameParser::capacity
uint16_t capacity() const
Definition
ld600x_frame.h:31
esphome::ld600x::FrameParser::ParseState::PARSE_STATE_DATA
@ PARSE_STATE_DATA
esphome::ld600x::FrameParser::ParseState::PARSE_STATE_DISCARD
@ PARSE_STATE_DISCARD
esphome::ld600x::FrameParser::ParseState::PARSE_STATE_HEADER
@ PARSE_STATE_HEADER
esphome::ld600x::FrameParser::ParseState::PARSE_STATE_HCK
@ PARSE_STATE_HCK
esphome::ld600x::FrameParser::ParseState::PARSE_STATE_DCK
@ PARSE_STATE_DCK
esphome::ld600x::FrameParser::ParseState::PARSE_STATE_SOF
@ PARSE_STATE_SOF
esphome::ld600x::FrameParser::xor_
uint8_t xor_
Definition
ld600x_frame.h:54
esphome::ld600x::FrameParser::data_len_
uint16_t data_len_
Definition
ld600x_frame.h:47
esphome::ld600x::FrameParser::data_buf_
uint8_t * data_buf_
Definition
ld600x_frame.h:46
esphome::ld600x::FrameParser::reset
void reset()
Definition
ld600x_frame.h:32
esphome::ld600x::FrameParser::init
void init(uint8_t *buffer, uint16_t capacity)
Definition
ld600x_frame.cpp:11
type
uint8_t type
Definition
e131_packet.cpp:21
out
std::vector< uint8_t > & out
Definition
gif.cpp:49
buffer
uint32_t buffer
Definition
gif.cpp:50
ld600x_frame.h
log.h
esphome::ld600x
Definition
ld600x.cpp:14
esphome::ld600x::ESPHOME_LOG_TAG
ESPHOME_LOG_TAG(TAG, "ld600x")
esphome::ld600x::encode_frame
size_t encode_frame(uint16_t frame_id, uint16_t type, const uint8_t *data, uint16_t len, uint8_t *out)
Definition
ld600x_frame.cpp:85
esphome::len
const void size_t len
Definition
hal.h:68
esphome::pos
size_t size_t pos
Definition
helpers.h:1333
esphome::put_be16
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
esphome::capacity
uint16_t uint16_t & capacity
Definition
helpers.cpp:34
Generated by
1.12.0