[teleinfo] Store the frame as bytes and read with read_byte() (#20531)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Frédéric Metrich
2026-10-11 09:41:02 -10:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 16a474cce5
commit 5c5737303f
2 changed files with 15 additions and 14 deletions
+13 -12
View File
@@ -6,30 +6,30 @@ namespace esphome::teleinfo {
ESPHOME_LOG_TAG(TAG, "teleinfo");
/* Helpers */
static int get_field(char *dest, char *buf_start, char *buf_end, int sep, int max_len) {
char *field_end;
static int get_field(char *dest, uint8_t *buf_start, uint8_t *buf_end, int sep, int max_len) {
uint8_t *field_end;
int len;
field_end = static_cast<char *>(memchr(buf_start, sep, buf_end - buf_start));
field_end = static_cast<uint8_t *>(memchr(buf_start, sep, buf_end - buf_start));
if (!field_end)
return 0;
len = field_end - buf_start;
if (len >= max_len)
return len;
strncpy(dest, buf_start, len);
memcpy(dest, buf_start, len);
dest[len] = '\0';
return len;
}
/* TeleInfo methods */
bool TeleInfo::check_crc_(const char *grp, const char *grp_end) {
bool TeleInfo::check_crc_(const uint8_t *grp, const uint8_t *grp_end) {
int grp_len = grp_end - grp;
if (grp_len < this->checksum_area_end_) {
ESP_LOGE(TAG, "Empty or too short group");
return false;
}
uint8_t raw_crc = grp[grp_len - 1];
uint8_t crc_tmp = sum_bytes(reinterpret_cast<const uint8_t *>(grp), grp_len - this->checksum_area_end_);
uint8_t crc_tmp = sum_bytes(grp, grp_len - this->checksum_area_end_);
crc_tmp &= 0x3F;
crc_tmp += 0x20;
@@ -46,7 +46,8 @@ bool TeleInfo::read_chars_until_(bool drop, uint8_t c) {
while (available() > 0 && j < 128) {
j++;
received = read();
if (!read_byte(&received))
break;
if (received == c)
return true;
if (drop)
@@ -87,9 +88,9 @@ void TeleInfo::loop() {
state_ = END_FRAME_RECEIVED;
break;
case END_FRAME_RECEIVED:
char *buf_finger;
char *grp_end;
char *buf_end;
uint8_t *buf_finger;
uint8_t *grp_end;
uint8_t *buf_end;
int field_len;
buf_finger = buf_;
@@ -116,7 +117,7 @@ void TeleInfo::loop() {
* 0xa | Tag | 0x9 | Timestamp | 0x9 | 0x9 | CRC | 0xd
*
*/
while ((buf_finger = static_cast<char *>(memchr(buf_finger, (int) 0xa, buf_end - buf_finger)))) {
while ((buf_finger = static_cast<uint8_t *>(memchr(buf_finger, (int) 0xa, buf_end - buf_finger)))) {
/*
* Make sure timesamp is nullified between each tag as some tags don't
* have a timestamp
@@ -126,7 +127,7 @@ void TeleInfo::loop() {
buf_finger += 1;
/* Group len */
grp_end = static_cast<char *>(memchr(buf_finger, 0xd, buf_end - buf_finger));
grp_end = static_cast<uint8_t *>(memchr(buf_finger, 0xd, buf_end - buf_finger));
if (!grp_end) {
ESP_LOGE(TAG, "No group found");
break;
+2 -2
View File
@@ -33,7 +33,7 @@ class TeleInfo final : public PollingComponent, public uart::UARTDevice {
protected:
int checksum_area_end_;
int separator_;
char buf_[MAX_BUF_SIZE];
uint8_t buf_[MAX_BUF_SIZE];
uint32_t buf_index_{0};
char tag_[MAX_TAG_SIZE];
char val_[MAX_VAL_SIZE];
@@ -45,7 +45,7 @@ class TeleInfo final : public PollingComponent, public uart::UARTDevice {
END_FRAME_RECEIVED,
} state_{STATE_OFF};
bool read_chars_until_(bool drop, uint8_t c);
bool check_crc_(const char *grp, const char *grp_end);
bool check_crc_(const uint8_t *grp, const uint8_t *grp_end);
void publish_value_(const std::string &tag, const std::string &val);
};
} // namespace esphome::teleinfo