ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
dlms_meter.cpp
Go to the documentation of this file.
1#include "dlms_meter.h"
2
3#include <cmath>
4
5#if defined(USE_ESP8266_FRAMEWORK_ARDUINO)
6#include <bearssl/bearssl.h>
7#elif defined(USE_ESP32)
8#include "mbedtls/esp_config.h"
9#include "mbedtls/gcm.h"
10#endif
11
12namespace esphome::dlms_meter {
13
14static constexpr const char *TAG = "dlms_meter";
15
17 const char *provider_name = this->provider_ == PROVIDER_NETZNOE ? "Netz NOE" : "Generic";
18 ESP_LOGCONFIG(TAG,
19 "DLMS Meter:\n"
20 " Provider: %s\n"
21 " Read Timeout: %u ms",
22 provider_name, this->read_timeout_);
23#define DLMS_METER_LOG_SENSOR(s) LOG_SENSOR(" ", #s, this->s##_sensor_);
24 DLMS_METER_SENSOR_LIST(DLMS_METER_LOG_SENSOR, )
25#define DLMS_METER_LOG_TEXT_SENSOR(s) LOG_TEXT_SENSOR(" ", #s, this->s##_text_sensor_);
26 DLMS_METER_TEXT_SENSOR_LIST(DLMS_METER_LOG_TEXT_SENSOR, )
27}
28
30 // Read while data is available, netznoe uses two frames so allow 2x max frame length
31 size_t avail = this->available();
32 if (avail > 0) {
33 size_t remaining = MBUS_MAX_FRAME_LENGTH * 2 - this->receive_buffer_.size();
34 if (remaining == 0) {
35 ESP_LOGW(TAG, "Receive buffer full, dropping remaining bytes");
36 } else {
37 // Read all available bytes in batches to reduce UART call overhead.
38 // Cap reads to remaining buffer capacity.
39 if (avail > remaining) {
40 avail = remaining;
41 }
42 uint8_t buf[64];
43 while (avail > 0) {
44 size_t to_read = std::min(avail, sizeof(buf));
45 if (!this->read_array(buf, to_read)) {
46 break;
47 }
48 avail -= to_read;
49 this->receive_buffer_.insert(this->receive_buffer_.end(), buf, buf + to_read);
50 this->last_read_ = millis();
51 }
52 }
53 }
54
55 if (!this->receive_buffer_.empty() && millis() - this->last_read_ > this->read_timeout_) {
56 this->mbus_payload_.clear();
57 if (!this->parse_mbus_(this->mbus_payload_))
58 return;
59
60 uint16_t message_length;
61 uint8_t systitle_length;
62 uint16_t header_offset;
63 if (!this->parse_dlms_(this->mbus_payload_, message_length, systitle_length, header_offset))
64 return;
65
66 if (message_length < DECODER_START_OFFSET || message_length > MAX_MESSAGE_LENGTH) {
67 ESP_LOGE(TAG, "DLMS: Message length invalid: %u", message_length);
68 this->receive_buffer_.clear();
69 return;
70 }
71
72 // Decrypt in place and then decode the OBIS codes
73 if (!this->decrypt_(this->mbus_payload_, message_length, systitle_length, header_offset))
74 return;
75 this->decode_obis_(&this->mbus_payload_[header_offset + DLMS_PAYLOAD_OFFSET], message_length);
76 }
77}
78
79bool DlmsMeterComponent::parse_mbus_(std::vector<uint8_t> &mbus_payload) {
80 ESP_LOGV(TAG, "Parsing M-Bus frames");
81 uint16_t frame_offset = 0; // Offset is used if the M-Bus message is split into multiple frames
82
83 while (frame_offset < this->receive_buffer_.size()) {
84 // Ensure enough bytes remain for the minimal intro header before accessing indices
85 if (this->receive_buffer_.size() - frame_offset < MBUS_HEADER_INTRO_LENGTH) {
86 ESP_LOGE(TAG, "MBUS: Not enough data for frame header (need %d, have %d)", MBUS_HEADER_INTRO_LENGTH,
87 (this->receive_buffer_.size() - frame_offset));
88 this->receive_buffer_.clear();
89 return false;
90 }
91
92 // Check start bytes
93 if (this->receive_buffer_[frame_offset + MBUS_START1_OFFSET] != START_BYTE_LONG_FRAME ||
94 this->receive_buffer_[frame_offset + MBUS_START2_OFFSET] != START_BYTE_LONG_FRAME) {
95 ESP_LOGE(TAG, "MBUS: Start bytes do not match");
96 this->receive_buffer_.clear();
97 return false;
98 }
99
100 // Both length bytes must be identical
101 if (this->receive_buffer_[frame_offset + MBUS_LENGTH1_OFFSET] !=
102 this->receive_buffer_[frame_offset + MBUS_LENGTH2_OFFSET]) {
103 ESP_LOGE(TAG, "MBUS: Length bytes do not match");
104 this->receive_buffer_.clear();
105 return false;
106 }
107
108 uint8_t frame_length = this->receive_buffer_[frame_offset + MBUS_LENGTH1_OFFSET]; // Get length of this frame
109
110 // Check if received data is enough for the given frame length
111 if (this->receive_buffer_.size() - frame_offset <
112 frame_length + 3) { // length field inside packet does not account for second start- + checksum- + stop- byte
113 ESP_LOGE(TAG, "MBUS: Frame too big for received data");
114 this->receive_buffer_.clear();
115 return false;
116 }
117
118 // Ensure we have full frame (header + payload + checksum + stop byte) before accessing stop byte
119 size_t required_total =
120 frame_length + MBUS_HEADER_INTRO_LENGTH + MBUS_FOOTER_LENGTH; // payload + header + 2 footer bytes
121 if (this->receive_buffer_.size() - frame_offset < required_total) {
122 ESP_LOGE(TAG, "MBUS: Incomplete frame (need %d, have %d)", (unsigned int) required_total,
123 this->receive_buffer_.size() - frame_offset);
124 this->receive_buffer_.clear();
125 return false;
126 }
127
128 if (this->receive_buffer_[frame_offset + frame_length + MBUS_HEADER_INTRO_LENGTH + MBUS_FOOTER_LENGTH - 1] !=
129 STOP_BYTE) {
130 ESP_LOGE(TAG, "MBUS: Invalid stop byte");
131 this->receive_buffer_.clear();
132 return false;
133 }
134
135 // Verify checksum: sum of all bytes starting at MBUS_HEADER_INTRO_LENGTH, take last byte
136 uint8_t checksum = 0; // use uint8_t so only the 8 least significant bits are stored
137 for (uint16_t i = 0; i < frame_length; i++) {
138 checksum += this->receive_buffer_[frame_offset + MBUS_HEADER_INTRO_LENGTH + i];
139 }
140 if (checksum != this->receive_buffer_[frame_offset + frame_length + MBUS_HEADER_INTRO_LENGTH]) {
141 ESP_LOGE(TAG, "MBUS: Invalid checksum: %x != %x", checksum,
142 this->receive_buffer_[frame_offset + frame_length + MBUS_HEADER_INTRO_LENGTH]);
143 this->receive_buffer_.clear();
144 return false;
145 }
146
147 mbus_payload.insert(mbus_payload.end(), &this->receive_buffer_[frame_offset + MBUS_FULL_HEADER_LENGTH],
148 &this->receive_buffer_[frame_offset + MBUS_HEADER_INTRO_LENGTH + frame_length]);
149
150 frame_offset += MBUS_HEADER_INTRO_LENGTH + frame_length + MBUS_FOOTER_LENGTH;
151 }
152 return true;
153}
154
155bool DlmsMeterComponent::parse_dlms_(const std::vector<uint8_t> &mbus_payload, uint16_t &message_length,
156 uint8_t &systitle_length, uint16_t &header_offset) {
157 ESP_LOGV(TAG, "Parsing DLMS header");
158 if (mbus_payload.size() < DLMS_HEADER_LENGTH + DLMS_HEADER_EXT_OFFSET) {
159 ESP_LOGE(TAG, "DLMS: Payload too short");
160 this->receive_buffer_.clear();
161 return false;
162 }
163
164 if (mbus_payload[DLMS_CIPHER_OFFSET] != GLO_CIPHERING) { // Only general-glo-ciphering is supported (0xDB)
165 ESP_LOGE(TAG, "DLMS: Unsupported cipher");
166 this->receive_buffer_.clear();
167 return false;
168 }
169
170 systitle_length = mbus_payload[DLMS_SYST_OFFSET];
171
172 if (systitle_length != 0x08) { // Only system titles with length of 8 are supported
173 ESP_LOGE(TAG, "DLMS: Unsupported system title length");
174 this->receive_buffer_.clear();
175 return false;
176 }
177
178 message_length = mbus_payload[DLMS_LENGTH_OFFSET];
179 header_offset = 0;
180
181 if (this->provider_ == PROVIDER_NETZNOE) {
182 // for some reason EVN seems to set the standard "length" field to 0x81 and then the actual length is in the next
183 // byte. Check some bytes to see if received data still matches expectation
184 if (message_length == NETZ_NOE_MAGIC_BYTE &&
185 mbus_payload[DLMS_LENGTH_OFFSET + 1] == NETZ_NOE_EXPECTED_MESSAGE_LENGTH &&
186 mbus_payload[DLMS_LENGTH_OFFSET + 2] == NETZ_NOE_EXPECTED_SECURITY_CONTROL_BYTE) {
187 message_length = mbus_payload[DLMS_LENGTH_OFFSET + 1];
188 header_offset = 1;
189 } else {
190 ESP_LOGE(TAG, "Wrong Length - Security Control Byte sequence detected for provider EVN");
191 }
192 } else {
193 if (message_length == TWO_BYTE_LENGTH) {
194 message_length = encode_uint16(mbus_payload[DLMS_LENGTH_OFFSET + 1], mbus_payload[DLMS_LENGTH_OFFSET + 2]);
195 header_offset = DLMS_HEADER_EXT_OFFSET;
196 }
197 }
198 if (message_length < DLMS_LENGTH_CORRECTION) {
199 ESP_LOGE(TAG, "DLMS: Message length too short: %u", message_length);
200 this->receive_buffer_.clear();
201 return false;
202 }
203 message_length -= DLMS_LENGTH_CORRECTION; // Correct message length due to part of header being included in length
204
205 if (mbus_payload.size() - DLMS_HEADER_LENGTH - header_offset != message_length) {
206 ESP_LOGV(TAG, "DLMS: Length mismatch - payload=%d, header=%d, offset=%d, message=%d", mbus_payload.size(),
207 DLMS_HEADER_LENGTH, header_offset, message_length);
208 ESP_LOGE(TAG, "DLMS: Message has invalid length");
209 this->receive_buffer_.clear();
210 return false;
211 }
212
213 if (mbus_payload[header_offset + DLMS_SECBYTE_OFFSET] != 0x21 &&
214 mbus_payload[header_offset + DLMS_SECBYTE_OFFSET] !=
215 0x20) { // Only certain security suite is supported (0x21 || 0x20)
216 ESP_LOGE(TAG, "DLMS: Unsupported security control byte");
217 this->receive_buffer_.clear();
218 return false;
219 }
220
221 return true;
222}
223
224bool DlmsMeterComponent::decrypt_(std::vector<uint8_t> &mbus_payload, uint16_t message_length, uint8_t systitle_length,
225 uint16_t header_offset) {
226 ESP_LOGV(TAG, "Decrypting payload");
227 uint8_t iv[12]; // Reserve space for the IV, always 12 bytes
228 // Copy system title to IV (System title is before length; no header offset needed!)
229 // Add 1 to the offset in order to skip the system title length byte
230 memcpy(&iv[0], &mbus_payload[DLMS_SYST_OFFSET + 1], systitle_length);
231 memcpy(&iv[8], &mbus_payload[header_offset + DLMS_FRAMECOUNTER_OFFSET],
232 DLMS_FRAMECOUNTER_LENGTH); // Copy frame counter to IV
233
234 uint8_t *payload_ptr = &mbus_payload[header_offset + DLMS_PAYLOAD_OFFSET];
235
236#if defined(USE_ESP8266_FRAMEWORK_ARDUINO)
237 br_gcm_context gcm_ctx;
238 br_aes_ct_ctr_keys bc;
239 br_aes_ct_ctr_init(&bc, this->decryption_key_.data(), this->decryption_key_.size());
240 br_gcm_init(&gcm_ctx, &bc.vtable, br_ghash_ctmul32);
241 br_gcm_reset(&gcm_ctx, iv, sizeof(iv));
242 br_gcm_flip(&gcm_ctx);
243 br_gcm_run(&gcm_ctx, 0, payload_ptr, message_length);
244#elif defined(USE_ESP32)
245 size_t outlen = 0;
246 mbedtls_gcm_context gcm_ctx;
247 mbedtls_gcm_init(&gcm_ctx);
248 mbedtls_gcm_setkey(&gcm_ctx, MBEDTLS_CIPHER_ID_AES, this->decryption_key_.data(), this->decryption_key_.size() * 8);
249 mbedtls_gcm_starts(&gcm_ctx, MBEDTLS_GCM_DECRYPT, iv, sizeof(iv));
250 auto ret = mbedtls_gcm_update(&gcm_ctx, payload_ptr, message_length, payload_ptr, message_length, &outlen);
251 mbedtls_gcm_free(&gcm_ctx);
252 if (ret != 0) {
253 ESP_LOGE(TAG, "Decryption failed with error: %d", ret);
254 this->receive_buffer_.clear();
255 return false;
256 }
257#else
258#error "Invalid Platform"
259#endif
260
261 if (payload_ptr[0] != DATA_NOTIFICATION || payload_ptr[5] != TIMESTAMP_DATETIME) {
262 ESP_LOGE(TAG, "OBIS: Packet was decrypted but data is invalid");
263 this->receive_buffer_.clear();
264 return false;
265 }
266 ESP_LOGV(TAG, "Decrypted payload: %d bytes", message_length);
267 return true;
268}
269
270void DlmsMeterComponent::decode_obis_(uint8_t *plaintext, uint16_t message_length) {
271 ESP_LOGV(TAG, "Decoding payload");
272 MeterData data{};
273 uint16_t current_position = DECODER_START_OFFSET;
274 bool power_factor_found = false;
275
276 while (current_position + OBIS_CODE_OFFSET <= message_length) {
277 if (plaintext[current_position + OBIS_TYPE_OFFSET] != DataType::OCTET_STRING) {
278 ESP_LOGE(TAG, "OBIS: Unsupported OBIS header type: %x", plaintext[current_position + OBIS_TYPE_OFFSET]);
279 this->receive_buffer_.clear();
280 return;
281 }
282
283 uint8_t obis_code_length = plaintext[current_position + OBIS_LENGTH_OFFSET];
284 if (obis_code_length != OBIS_CODE_LENGTH_STANDARD && obis_code_length != OBIS_CODE_LENGTH_EXTENDED) {
285 ESP_LOGE(TAG, "OBIS: Unsupported OBIS header length: %x", obis_code_length);
286 this->receive_buffer_.clear();
287 return;
288 }
289 if (current_position + OBIS_CODE_OFFSET + obis_code_length > message_length) {
290 ESP_LOGE(TAG, "OBIS: Buffer too short for OBIS code");
291 this->receive_buffer_.clear();
292 return;
293 }
294
295 uint8_t *obis_code = &plaintext[current_position + OBIS_CODE_OFFSET];
296 uint8_t obis_medium = obis_code[OBIS_A];
297 uint16_t obis_cd = encode_uint16(obis_code[OBIS_C], obis_code[OBIS_D]);
298
299 bool timestamp_found = false;
300 bool meter_number_found = false;
301 if (this->provider_ == PROVIDER_NETZNOE) {
302 // Do not advance Position when reading the Timestamp at DECODER_START_OFFSET
303 if ((obis_code_length == OBIS_CODE_LENGTH_EXTENDED) && (current_position == DECODER_START_OFFSET)) {
304 timestamp_found = true;
305 } else if (power_factor_found) {
306 meter_number_found = true;
307 power_factor_found = false;
308 } else {
309 current_position += obis_code_length + OBIS_CODE_OFFSET; // Advance past code and position
310 }
311 } else {
312 current_position += obis_code_length + OBIS_CODE_OFFSET; // Advance past code, position and type
313 }
314 if (!timestamp_found && !meter_number_found && obis_medium != Medium::ELECTRICITY &&
315 obis_medium != Medium::ABSTRACT) {
316 ESP_LOGE(TAG, "OBIS: Unsupported OBIS medium: %x", obis_medium);
317 this->receive_buffer_.clear();
318 return;
319 }
320
321 if (current_position >= message_length) {
322 ESP_LOGE(TAG, "OBIS: Buffer too short for data type");
323 this->receive_buffer_.clear();
324 return;
325 }
326
327 float value = 0.0f;
328 uint8_t value_size = 0;
329 uint8_t data_type = plaintext[current_position];
330 current_position++;
331
332 switch (data_type) {
334 value_size = 4;
335 if (current_position + value_size > message_length) {
336 ESP_LOGE(TAG, "OBIS: Buffer too short for DOUBLE_LONG_UNSIGNED");
337 this->receive_buffer_.clear();
338 return;
339 }
340 value = encode_uint32(plaintext[current_position + 0], plaintext[current_position + 1],
341 plaintext[current_position + 2], plaintext[current_position + 3]);
342 current_position += value_size;
343 break;
344 }
346 value_size = 2;
347 if (current_position + value_size > message_length) {
348 ESP_LOGE(TAG, "OBIS: Buffer too short for LONG_UNSIGNED");
349 this->receive_buffer_.clear();
350 return;
351 }
352 value = encode_uint16(plaintext[current_position + 0], plaintext[current_position + 1]);
353 current_position += value_size;
354 break;
355 }
357 uint8_t data_length = plaintext[current_position];
358 current_position++; // Advance past string length
359 if (current_position + data_length > message_length) {
360 ESP_LOGE(TAG, "OBIS: Buffer too short for OCTET_STRING");
361 this->receive_buffer_.clear();
362 return;
363 }
364 // Handle timestamp (normal OBIS code or NETZNOE special case)
365 if (obis_cd == OBIS_TIMESTAMP || timestamp_found) {
366 if (data_length < 8) {
367 ESP_LOGE(TAG, "OBIS: Timestamp data too short: %u", data_length);
368 this->receive_buffer_.clear();
369 return;
370 }
371 uint16_t year = encode_uint16(plaintext[current_position + 0], plaintext[current_position + 1]);
372 uint8_t month = plaintext[current_position + 2];
373 uint8_t day = plaintext[current_position + 3];
374 uint8_t hour = plaintext[current_position + 5];
375 uint8_t minute = plaintext[current_position + 6];
376 uint8_t second = plaintext[current_position + 7];
377 if (year > 9999 || month > 12 || day > 31 || hour > 23 || minute > 59 || second > 59) {
378 ESP_LOGE(TAG, "Invalid timestamp values: %04u-%02u-%02uT%02u:%02u:%02uZ", year, month, day, hour, minute,
379 second);
380 this->receive_buffer_.clear();
381 return;
382 }
383 snprintf(data.timestamp, sizeof(data.timestamp), "%04u-%02u-%02uT%02u:%02u:%02uZ", year, month, day, hour,
384 minute, second);
385 } else if (meter_number_found) {
386 snprintf(data.meternumber, sizeof(data.meternumber), "%.*s", data_length, &plaintext[current_position]);
387 }
388 current_position += data_length;
389 break;
390 }
391 default:
392 ESP_LOGE(TAG, "OBIS: Unsupported OBIS data type: %x", data_type);
393 this->receive_buffer_.clear();
394 return;
395 }
396
397 // Skip break after data
398 if (this->provider_ == PROVIDER_NETZNOE) {
399 // Don't skip the break on the first timestamp, as there's none
400 if (!timestamp_found) {
401 current_position += 2;
402 }
403 } else {
404 current_position += 2;
405 }
406
407 // Check for additional data (scaler-unit structure)
408 if (current_position < message_length && plaintext[current_position] == DataType::INTEGER) {
409 // Apply scaler: real_value = raw_value × 10^scaler
410 if (current_position + 1 < message_length) {
411 int8_t scaler = static_cast<int8_t>(plaintext[current_position + 1]);
412 if (scaler != 0) {
413 value *= powf(10.0f, scaler);
414 }
415 }
416
417 // on EVN Meters there is no additional break
418 if (this->provider_ == PROVIDER_NETZNOE) {
419 current_position += 4;
420 } else {
421 current_position += 6;
422 }
423 }
424
425 // Handle numeric values (LONG_UNSIGNED and DOUBLE_LONG_UNSIGNED)
426 if (value_size > 0) {
427 switch (obis_cd) {
428 case OBIS_VOLTAGE_L1:
429 data.voltage_l1 = value;
430 break;
431 case OBIS_VOLTAGE_L2:
432 data.voltage_l2 = value;
433 break;
434 case OBIS_VOLTAGE_L3:
435 data.voltage_l3 = value;
436 break;
437 case OBIS_CURRENT_L1:
438 data.current_l1 = value;
439 break;
440 case OBIS_CURRENT_L2:
441 data.current_l2 = value;
442 break;
443 case OBIS_CURRENT_L3:
444 data.current_l3 = value;
445 break;
446 case OBIS_ACTIVE_POWER_PLUS:
447 data.active_power_plus = value;
448 break;
449 case OBIS_ACTIVE_POWER_MINUS:
450 data.active_power_minus = value;
451 break;
452 case OBIS_ACTIVE_ENERGY_PLUS:
453 data.active_energy_plus = value;
454 break;
455 case OBIS_ACTIVE_ENERGY_MINUS:
456 data.active_energy_minus = value;
457 break;
458 case OBIS_REACTIVE_ENERGY_PLUS:
459 data.reactive_energy_plus = value;
460 break;
461 case OBIS_REACTIVE_ENERGY_MINUS:
462 data.reactive_energy_minus = value;
463 break;
464 case OBIS_POWER_FACTOR:
465 data.power_factor = value;
466 power_factor_found = true;
467 break;
468 default:
469 ESP_LOGW(TAG, "Unsupported OBIS code 0x%04X", obis_cd);
470 }
471 }
472 }
473
474 this->receive_buffer_.clear();
475
476 ESP_LOGI(TAG, "Received valid data");
477 this->publish_sensors(data);
478 this->status_clear_warning();
479}
480
481} // namespace esphome::dlms_meter
uint8_t checksum
Definition bl0906.h:3
void status_clear_warning()
bool parse_dlms_(const std::vector< uint8_t > &mbus_payload, uint16_t &message_length, uint8_t &systitle_length, uint16_t &header_offset)
bool decrypt_(std::vector< uint8_t > &mbus_payload, uint16_t message_length, uint8_t systitle_length, uint16_t header_offset)
std::vector< uint8_t > mbus_payload_
Definition dlms_meter.h:88
bool parse_mbus_(std::vector< uint8_t > &mbus_payload)
void decode_obis_(uint8_t *plaintext, uint16_t message_length)
void publish_sensors(MeterData &data)
Definition dlms_meter.h:64
std::vector< uint8_t > receive_buffer_
Definition dlms_meter.h:87
std::array< uint8_t, 16 > decryption_key_
Definition dlms_meter.h:93
DLMS_METER_SENSOR_LIST(SUB_SENSOR,) DLMS_METER_TEXT_SENSOR_LIST(SUB_TEXT_SENSOR
optional< std::array< uint8_t, N > > read_array()
Definition uart.h:38
uint8_t month
Definition date_entity.h:1
uint16_t year
Definition date_entity.h:0
uint8_t day
Definition date_entity.h:2
uint8_t second
uint8_t minute
uint8_t hour
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four bytes in most to least significant byte order.
Definition helpers.h:536
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:528
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:25