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