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