9 static const char *
const TAG =
"sml";
15 : server_id(
std::move(server_id)), obis_code(
std::move(obis_code)) {}
18 this->incoming_mask_ = (this->incoming_mask_ << 2) |
get_code(byte);
22 if ((this->incoming_mask_ >> 6) ==
END_MASK)
23 return END_BYTES_DETECTED;
29 const char c = read();
32 this->sml_data_.emplace_back(c);
34 switch (this->check_start_end_bytes_(c)) {
37 this->sml_data_.clear();
44 this->record_ =
false;
49 this->data_callbacks_.call(this->sml_data_, valid);
55 this->sml_data_.erase(this->sml_data_.begin(), this->sml_data_.begin() +
START_SEQ.size());
56 this->sml_data_.resize(this->sml_data_.size() - 8);
57 this->process_sml_file_(this->sml_data_);
66 this->data_callbacks_.add(std::move(callback));
72 this->publish_obis_info_(obis_info);
74 this->log_obis_info_(obis_info);
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());
89 for (
auto const &obis_info : obis_info_vec) {
90 this->publish_value_(obis_info);
95 for (
auto const &sml_listener : sml_listeners_) {
96 if ((!sml_listener->server_id.empty()) && (
bytes_repr(obis_info.
server_id) != sml_listener->server_id))
98 if (obis_info.
code_repr() != sml_listener->obis_code)
100 sml_listener->publish_val(obis_info);
109 if (buffer.size() < 2) {
110 ESP_LOGW(TAG,
"Checksum error in received SML data.");
114 uint16_t crc_received = (buffer.at(buffer.size() - 2) << 8) | buffer.at(buffer.size() - 1);
115 uint16_t crc_calculated =
crc16(buffer.data() + 8, buffer.size() - 10, 0x6e23, 0x8408,
true,
true);
116 crc_calculated = (crc_calculated >> 8) | (crc_calculated << 8);
117 if (crc_received == crc_calculated) {
118 ESP_LOGV(TAG,
"Checksum verification successful with CRC16/X25.");
122 crc_calculated =
crc16(buffer.data() + 8, buffer.size() - 10, 0xed50, 0x8408);
123 if (crc_received == crc_calculated) {
124 ESP_LOGV(TAG,
"Checksum verification successful with CRC16/KERMIT.");
128 ESP_LOGW(TAG,
"Checksum error in received SML data.");
std::string bytes_repr(const bytes &buffer)
const uint16_t START_MASK
void log_obis_info_(const std::vector< ObisInfo > &obis_info_vec)
const std::vector< uint8_t > START_SEQ
SmlListener(std::string server_id, std::string obis_code)
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.
void publish_value_(const ObisInfo &obis_info)
void register_sml_listener(SmlListener *listener)
const char END_BYTES_DETECTED
const char START_BYTES_DETECTED
char check_start_end_bytes_(uint8_t byte)
uint8_t get_code(uint8_t byte)
void process_sml_file_(const bytes &sml_data)
void add_on_data_callback(std::function< void(std::vector< uint8_t >, bool)> &&callback)
std::vector< ObisInfo > get_obis_info()
Implementation of SPI Controller mode.
std::vector< uint8_t > bytes
void publish_obis_info_(const std::vector< ObisInfo > &obis_info_vec)
bool check_sml_data(const bytes &buffer)
void dump_config() override
std::string code_repr() const