9static int get_field(
char *dest, uint8_t *buf_start, uint8_t *buf_end,
int sep,
int max_len) {
13 field_end =
static_cast<uint8_t *
>(memchr(buf_start, sep, buf_end - buf_start));
16 len = field_end - buf_start;
19 memcpy(dest, buf_start,
len);
26 int grp_len = grp_end - grp;
28 ESP_LOGE(TAG,
"Empty or too short group");
31 uint8_t raw_crc = grp[grp_len - 1];
32 uint8_t crc_tmp =
sum_bytes(grp, grp_len - this->checksum_area_end_);
36 if (raw_crc != crc_tmp) {
37 ESP_LOGE(TAG,
"bad crc: got %d except %d", raw_crc, crc_tmp);
60 ESP_LOGW(TAG,
"Internal buffer full");
120 while ((buf_finger =
static_cast<uint8_t *
>(memchr(buf_finger, (
int) 0xa, buf_end - buf_finger)))) {
130 grp_end =
static_cast<uint8_t *
>(memchr(buf_finger, 0xd, buf_end - buf_finger));
132 ESP_LOGE(TAG,
"No group found");
140 field_len = get_field(
tag_, buf_finger, grp_end,
separator_, MAX_TAG_SIZE);
141 if (!field_len || field_len >= MAX_TAG_SIZE) {
142 ESP_LOGE(TAG,
"Invalid tag.");
147 buf_finger += field_len + 1;
156 if (!field_len || field_len >= MAX_TIMESTAMP_SIZE) {
157 ESP_LOGE(TAG,
"Invalid timestamp for tag %s",
tag_);
162 buf_finger += field_len + 1;
165 field_len = get_field(
val_, buf_finger, grp_end,
separator_, MAX_VAL_SIZE);
166 if (!field_len || field_len >= MAX_VAL_SIZE) {
167 ESP_LOGE(TAG,
"Invalid value for tag %s",
tag_);
172 buf_finger = grp_end + 1;
182 if (tag != element->tag)
184 element->publish_val(
val);
189 if (historical_mode) {
void publish_value_(const std::string &tag, const std::string &val)
bool check_crc_(const uint8_t *grp, const uint8_t *grp_end)
char timestamp_[MAX_TIMESTAMP_SIZE]
TeleInfo(bool historical_mode)
void register_teleinfo_listener(TeleInfoListener *listener)
std::vector< TeleInfoListener * > teleinfo_listeners_
enum esphome::teleinfo::TeleInfo::State STATE_OFF
uint8_t buf_[MAX_BUF_SIZE]
void dump_config() override
bool read_chars_until_(bool drop, uint8_t c)
bool read_byte(uint8_t *data)
ESPHOME_LOG_TAG(TAG, "teleinfo_sensor")
constexpr uint8_t sum_bytes(const uint8_t *data, size_t len, uint8_t sum=0)
Add up the bytes of data with size len, truncated to 8 bits: the checksum many serial and IR protocol...