ESPHome  2025.4.0
sml_parser.cpp
Go to the documentation of this file.
1 #include "esphome/core/helpers.h"
2 #include "constants.h"
3 #include "sml_parser.h"
4 
5 namespace esphome {
6 namespace sml {
7 
8 SmlFile::SmlFile(const BytesView &buffer) : buffer_(buffer) {
9  // extract messages
10  this->pos_ = 0;
11  while (this->pos_ < this->buffer_.size()) {
12  if (this->buffer_[this->pos_] == 0x00)
13  break; // EndOfSmlMsg
14 
15  SmlNode message;
16  if (!this->setup_node(&message))
17  break;
18  this->messages.emplace_back(std::move(message));
19  }
20 }
21 
23  // If the TL field is 0x00, this is the end of the message
24  // (see 6.3.1 of SML protocol definition)
25  if (this->buffer_[this->pos_] == 0x00) {
26  // Increment past this byte and signal that the message is done
27  this->pos_ += 1;
28  return true;
29  }
30 
31  // Extract data from initial TL field
32  uint8_t type = (this->buffer_[this->pos_] >> 4) & 0x07; // type without overlength info
33  bool overlength = (this->buffer_[this->pos_] >> 4) & 0x08; // overlength information
34  uint8_t length = this->buffer_[this->pos_] & 0x0f; // length (including TL bytes)
35 
36  // Check if we need additional length bytes
37  if (overlength) {
38  // Shift the current length to the higher nibble
39  // and add the lower nibble of the next byte to the length
40  length = (length << 4) + (this->buffer_[this->pos_ + 1] & 0x0f);
41  // We are basically done with the first TL field now,
42  // so increment past that, we now point to the second TL field
43  this->pos_ += 1;
44  // Decrement the length for value fields (not lists),
45  // since the byte we just handled is counted as part of the field
46  // in case of values but not for lists
47  if (type != SML_LIST)
48  length -= 1;
49 
50  // Technically, this is not enough, the standard allows for more than two length fields.
51  // However I don't think it is very common to have more than 255 entries in a list
52  }
53 
54  // We are done with the last TL field(s), so advance the position
55  this->pos_ += 1;
56  // and decrement the length for non-list fields
57  if (type != SML_LIST)
58  length -= 1;
59 
60  // Check if the buffer length is long enough
61  if (this->pos_ + length > this->buffer_.size())
62  return false;
63 
64  node->type = type;
65 
66  if (type == SML_LIST) {
67  node->nodes.reserve(length);
68  for (size_t i = 0; i != length; i++) {
69  SmlNode child_node;
70  if (!this->setup_node(&child_node))
71  return false;
72  node->nodes.emplace_back(std::move(child_node));
73  }
74  } else {
75  // Value starts at the current position
76  // Value ends "length" bytes later,
77  // (since the TL field is counted but already subtracted from length)
78  node->value_bytes = buffer_.subview(this->pos_, length);
79  // Increment the pointer past all consumed bytes
80  this->pos_ += length;
81  }
82  return true;
83 }
84 
85 std::vector<ObisInfo> SmlFile::get_obis_info() {
86  std::vector<ObisInfo> obis_info;
87  for (auto const &message : messages) {
88  const auto &message_body = message.nodes[3];
89  uint16_t message_type = bytes_to_uint(message_body.nodes[0].value_bytes);
90  if (message_type != SML_GET_LIST_RES)
91  continue;
92 
93  const auto &get_list_response = message_body.nodes[1];
94  const auto &server_id = get_list_response.nodes[1].value_bytes;
95  const auto &val_list = get_list_response.nodes[4];
96 
97  for (auto const &val_list_entry : val_list.nodes) {
98  obis_info.emplace_back(server_id, val_list_entry);
99  }
100  }
101  return obis_info;
102 }
103 
104 std::string bytes_repr(const BytesView &buffer) {
105  std::string repr;
106  for (auto const value : buffer) {
107  repr += str_sprintf("%02x", value & 0xff);
108  }
109  return repr;
110 }
111 
112 uint64_t bytes_to_uint(const BytesView &buffer) {
113  uint64_t val = 0;
114  for (auto const value : buffer) {
115  val = (val << 8) + value;
116  }
117  return val;
118 }
119 
120 int64_t bytes_to_int(const BytesView &buffer) {
121  uint64_t tmp = bytes_to_uint(buffer);
122  int64_t val;
123 
124  // sign extension for abbreviations of leading ones (e.g. 3 byte transmissions, see 6.2.2 of SML protocol definition)
125  // see https://stackoverflow.com/questions/42534749/signed-extension-from-24-bit-to-32-bit-in-c
126  if (buffer.size() < 8) {
127  const int bits = buffer.size() * 8;
128  const uint64_t m = 1ull << (bits - 1);
129  tmp = (tmp ^ m) - m;
130  }
131 
132  val = (int64_t) tmp;
133  return val;
134 }
135 
136 std::string bytes_to_string(const BytesView &buffer) { return std::string(buffer.begin(), buffer.end()); }
137 
138 ObisInfo::ObisInfo(const BytesView &server_id, const SmlNode &val_list_entry) : server_id(server_id) {
139  this->code = val_list_entry.nodes[0].value_bytes;
140  this->status = val_list_entry.nodes[1].value_bytes;
141  this->unit = bytes_to_uint(val_list_entry.nodes[3].value_bytes);
142  this->scaler = bytes_to_int(val_list_entry.nodes[4].value_bytes);
143  const auto &value_node = val_list_entry.nodes[5];
144  this->value = value_node.value_bytes;
145  this->value_type = value_node.type;
146 }
147 
148 std::string ObisInfo::code_repr() const {
149  return str_sprintf("%d-%d:%d.%d.%d", this->code[0], this->code[1], this->code[2], this->code[3], this->code[4]);
150 }
151 
152 } // namespace sml
153 } // namespace esphome
size_t size() const noexcept
Definition: sml_parser.h:23
int64_t bytes_to_int(const BytesView &buffer)
Definition: sml_parser.cpp:120
std::string bytes_to_string(const BytesView &buffer)
Definition: sml_parser.cpp:136
BytesView subview(size_t offset, size_t count) const noexcept
Definition: sml_parser.h:30
BytesView value_bytes
Definition: sml_parser.h:47
mopeka_std_values val[4]
std::string str_sprintf(const char *fmt,...)
Definition: helpers.cpp:323
uint8_t type
std::vector< ObisInfo > get_obis_info()
Definition: sml_parser.cpp:85
const BytesView buffer_
Definition: sml_parser.h:72
const uint8_t * begin() const noexcept
Definition: sml_parser.h:35
uint16_t length
Definition: tt21100.cpp:12
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool setup_node(SmlNode *node)
Definition: sml_parser.cpp:22
uint64_t bytes_to_uint(const BytesView &buffer)
Definition: sml_parser.cpp:112
uint8_t m
Definition: bl0906.h:208
const uint8_t * end() const noexcept
Definition: sml_parser.h:37
SmlFile(const BytesView &buffer)
Definition: sml_parser.cpp:8
ObisInfo(const BytesView &server_id, const SmlNode &val_list_entry)
Definition: sml_parser.cpp:138
std::string code_repr() const
Definition: sml_parser.cpp:148
std::vector< SmlNode > messages
Definition: sml_parser.h:68
std::string bytes_repr(const BytesView &buffer)
Definition: sml_parser.cpp:104
std::vector< SmlNode > nodes
Definition: sml_parser.h:48