9static const char *
const TAG =
"sml";
15 : server_id(std::move(server_id)), obis_code(std::move(obis_code)) {}
29 const char c =
read();
77#ifdef ESPHOME_LOG_HAS_DEBUG
78 ESP_LOGD(TAG,
"OBIS info:");
79 for (
auto const &obis_info : obis_info_vec) {
81 info +=
" (" +
bytes_repr(obis_info.server_id) +
") ";
82 info += obis_info.code_repr();
83 info +=
" [0x" +
bytes_repr(obis_info.value) +
"]";
84 ESP_LOGD(TAG,
"%s", info.c_str());
90 for (
auto const &obis_info : obis_info_vec) {
96 const auto obis_code = obis_info.
code_repr();
98 if ((!sml_listener->server_id.empty()) && (
bytes_repr(obis_info.
server_id) != sml_listener->server_id))
100 if (obis_code != sml_listener->obis_code)
102 sml_listener->publish_val(obis_info);
111 if (buffer.size() < 2) {
112 ESP_LOGW(TAG,
"Checksum error in received SML data.");
116 uint16_t crc_received = (buffer.at(buffer.size() - 2) << 8) | buffer.at(buffer.size() - 1);
117 uint16_t crc_calculated =
crc16(buffer.data() + 8, buffer.size() - 10, 0x6e23, 0x8408,
true,
true);
118 crc_calculated = (crc_calculated >> 8) | (crc_calculated << 8);
119 if (crc_received == crc_calculated) {
120 ESP_LOGV(TAG,
"Checksum verification successful with CRC16/X25.");
124 crc_calculated =
crc16(buffer.data() + 8, buffer.size() - 10, 0xed50, 0x8408);
125 if (crc_received == crc_calculated) {
126 ESP_LOGV(TAG,
"Checksum verification successful with CRC16/KERMIT.");
130 ESP_LOGW(TAG,
"Checksum error in received SML data.");
std::string code_repr() const
std::vector< ObisInfo > get_obis_info()
void process_sml_file_(const BytesView &sml_data)
char check_start_end_bytes_(uint8_t byte)
std::vector< SmlListener * > sml_listeners_
CallbackManager< void(const std::vector< uint8_t > &, bool)> data_callbacks_
void add_on_data_callback(std::function< void(std::vector< uint8_t >, bool)> &&callback)
void register_sml_listener(SmlListener *listener)
void dump_config() override
void log_obis_info_(const std::vector< ObisInfo > &obis_info_vec)
void publish_value_(const ObisInfo &obis_info)
void publish_obis_info_(const std::vector< ObisInfo > &obis_info_vec)
SmlListener(std::string server_id, std::string obis_code)
std::string bytes_repr(const BytesView &buffer)
const std::vector< uint8_t > START_SEQ
const char END_BYTES_DETECTED
bool check_sml_data(const bytes &buffer)
uint8_t get_code(uint8_t byte)
const uint16_t START_MASK
std::vector< uint8_t > bytes
const char START_BYTES_DETECTED
Providing packet encoding functions for exchanging data with a remote host.
uint16_t crc16(const uint8_t *data, uint16_t len, uint16_t crc, uint16_t reverse_poly, bool refin, bool refout)
Calculate a CRC-16 checksum of data with size len.