ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
sml.cpp
Go to the documentation of this file.
1#include "sml.h"
3#include "esphome/core/log.h"
4#include "sml_parser.h"
5
6namespace esphome::sml {
7
8static const char *const TAG = "sml";
9
10const char START_BYTES_DETECTED = 1;
11const char END_BYTES_DETECTED = 2;
12
13SmlListener::SmlListener(std::string server_id, std::string obis_code)
14 : server_id(std::move(server_id)), obis_code(std::move(obis_code)) {}
15
16char Sml::check_start_end_bytes_(uint8_t byte) {
17 this->incoming_mask_ = (this->incoming_mask_ << 2) | get_code(byte);
18
19 if (this->incoming_mask_ == START_MASK)
21 if ((this->incoming_mask_ >> 6) == END_MASK)
22 return END_BYTES_DETECTED;
23 return 0;
24}
25
26void Sml::loop() {
27 while (available()) {
28 const char c = read();
29
30 if (this->record_)
31 this->sml_data_.emplace_back(c);
32
33 switch (this->check_start_end_bytes_(c)) {
35 this->record_ = true;
36 this->sml_data_.clear();
37 // add start sequence (for callbacks)
38 this->sml_data_.insert(this->sml_data_.begin(), START_SEQ.begin(), START_SEQ.end());
39 break;
40 };
41 case END_BYTES_DETECTED: {
42 if (this->record_) {
43 this->record_ = false;
44
45 bool valid = check_sml_data(this->sml_data_);
46
47 // call callbacks
48 this->data_callbacks_.call(this->sml_data_, valid);
49
50 if (!valid)
51 break;
52
53 // remove start/end sequence
55 BytesView(this->sml_data_).subview(START_SEQ.size(), this->sml_data_.size() - START_SEQ.size() - 8));
56 }
57 break;
58 };
59 };
60 }
61}
62
63void Sml::process_sml_file_(const BytesView &sml_data) {
64 SmlFile sml_file(sml_data);
65 std::vector<ObisInfo> obis_info = sml_file.get_obis_info();
66 this->publish_obis_info_(obis_info);
67
68 this->log_obis_info_(obis_info);
69}
70
71void Sml::log_obis_info_(const std::vector<ObisInfo> &obis_info_vec) {
72#ifdef ESPHOME_LOG_HAS_DEBUG
73 ESP_LOGD(TAG, "OBIS info:");
74 for (auto const &obis_info : obis_info_vec) {
75 std::string info;
76 info += " (" + bytes_repr(obis_info.server_id) + ") ";
77 info += obis_info.code_repr();
78 info += " [0x" + bytes_repr(obis_info.value) + "]";
79 ESP_LOGD(TAG, "%s", info.c_str());
80 }
81#endif
82}
83
84void Sml::publish_obis_info_(const std::vector<ObisInfo> &obis_info_vec) {
85 for (auto const &obis_info : obis_info_vec) {
86 this->publish_value_(obis_info);
87 }
88}
89
90void Sml::publish_value_(const ObisInfo &obis_info) {
91 const auto obis_code = obis_info.code_repr();
92 for (auto const &sml_listener : sml_listeners_) {
93 if ((!sml_listener->server_id.empty()) && (bytes_repr(obis_info.server_id) != sml_listener->server_id))
94 continue;
95 if (obis_code != sml_listener->obis_code)
96 continue;
97 sml_listener->publish_val(obis_info);
98 }
99}
100
101void Sml::dump_config() { ESP_LOGCONFIG(TAG, "SML:"); }
102
103void Sml::register_sml_listener(SmlListener *listener) { sml_listeners_.emplace_back(listener); }
104
105bool check_sml_data(const bytes &buffer) {
106 if (buffer.size() < 2) {
107 ESP_LOGW(TAG, "Checksum error in received SML data.");
108 return false;
109 }
110
111 uint16_t crc_received = (buffer.at(buffer.size() - 2) << 8) | buffer.at(buffer.size() - 1);
112 uint16_t crc_calculated = crc16(buffer.data() + 8, buffer.size() - 10, 0x6e23, 0x8408, true, true);
113 crc_calculated = (crc_calculated >> 8) | (crc_calculated << 8);
114 if (crc_received == crc_calculated) {
115 ESP_LOGV(TAG, "Checksum verification successful with CRC16/X25.");
116 return true;
117 }
118
119 crc_calculated = crc16(buffer.data() + 8, buffer.size() - 10, 0xed50, 0x8408);
120 if (crc_received == crc_calculated) {
121 ESP_LOGV(TAG, "Checksum verification successful with CRC16/KERMIT.");
122 return true;
123 }
124
125 ESP_LOGW(TAG, "Checksum error in received SML data.");
126 return false;
127}
128
129uint8_t get_code(uint8_t byte) {
130 switch (byte) {
131 case 0x1b:
132 return 1;
133 case 0x01:
134 return 2;
135 case 0x1a:
136 return 3;
137 default:
138 return 0;
139 }
140}
141
142} // namespace esphome::sml
std::string code_repr() const
std::vector< ObisInfo > get_obis_info()
uint16_t incoming_mask_
Definition sml.h:37
void process_sml_file_(const BytesView &sml_data)
Definition sml.cpp:63
char check_start_end_bytes_(uint8_t byte)
Definition sml.cpp:16
void loop() override
Definition sml.cpp:26
std::vector< SmlListener * > sml_listeners_
Definition sml.h:25
CallbackManager< void(const std::vector< uint8_t > &, bool)> data_callbacks_
Definition sml.h:40
void register_sml_listener(SmlListener *listener)
Definition sml.cpp:103
void dump_config() override
Definition sml.cpp:101
bytes sml_data_
Definition sml.h:38
void log_obis_info_(const std::vector< ObisInfo > &obis_info_vec)
Definition sml.cpp:71
void publish_value_(const ObisInfo &obis_info)
Definition sml.cpp:90
void publish_obis_info_(const std::vector< ObisInfo > &obis_info_vec)
Definition sml.cpp:84
bool record_
Definition sml.h:36
SmlListener(std::string server_id, std::string obis_code)
Definition sml.cpp:13
std::string bytes_repr(const BytesView &buffer)
const char END_BYTES_DETECTED
Definition sml.cpp:11
bool check_sml_data(const bytes &buffer)
Definition sml.cpp:105
uint8_t get_code(uint8_t byte)
Definition sml.cpp:129
const uint16_t START_MASK
Definition constants.h:21
constexpr std::array< uint8_t, 8 > START_SEQ
Definition constants.h:24
std::vector< uint8_t > bytes
Definition sml_parser.h:12
const uint16_t END_MASK
Definition constants.h:22
const char START_BYTES_DETECTED
Definition sml.cpp:10
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.
Definition helpers.cpp:86
bool valid