ESPHome 2026.6.2
Loading...
Searching...
No Matches
dlms_meter.h
Go to the documentation of this file.
1#pragma once
2
6#include "esphome/core/log.h"
9
10#ifdef USE_SENSOR
12#endif
13#ifdef USE_TEXT_SENSOR
15#endif
16#ifdef USE_BINARY_SENSOR
18#endif
19
20#include <dlms_parser/dlms_parser.h>
21
22#include <vector>
23#include <string>
24#include <array>
25#include <optional>
26#include <span>
27
28#if __has_include(<psa/crypto.h>)
29#include <dlms_parser/decryption/aes_128_gcm_decryptor_tfpsa.h>
30#elif !defined(USE_ESP8266) && __has_include(<mbedtls/gcm.h>)
31#if __has_include(<mbedtls/esp_config.h>)
32#include <mbedtls/esp_config.h>
33#endif
34#include <dlms_parser/decryption/aes_128_gcm_decryptor_mbedtls.h>
35#elif __has_include(<bearssl/bearssl.h>)
36#include <dlms_parser/decryption/aes_128_gcm_decryptor_bearssl.h>
37#else
38#define DLMS_METER_NO_CRYPTO
39#endif
40
41#ifndef DLMS_MAX_SENSORS
42static constexpr uint8_t DLMS_MAX_SENSORS = 0;
43#endif
44#ifndef DLMS_MAX_TEXT_SENSORS
45static constexpr uint8_t DLMS_MAX_TEXT_SENSORS = 0;
46#endif
47#ifndef DLMS_MAX_BINARY_SENSORS
48static constexpr uint8_t DLMS_MAX_BINARY_SENSORS = 0;
49#endif
50
51namespace esphome::dlms_meter {
52
53#ifdef DLMS_METER_NO_CRYPTO
54// Fallback dummy decryptor for platforms without supported crypto (e.g., Zephyr during clang-tidy)
55class Aes128GcmDecryptorDummy : public dlms_parser::Aes128GcmDecryptor {
56 public:
57 void set_decryption_key(const dlms_parser::Aes128GcmDecryptionKey &key) override {}
58 bool decrypt_in_place(std::span<const uint8_t> iv, std::span<uint8_t> ciphertext_and_plaintext,
59 std::span<const uint8_t> aad, std::span<const uint8_t> tag) override {
60 return false;
61 }
62};
63#endif
64
65#if __has_include(<psa/crypto.h>)
66using Aes128GcmDecryptorImpl = dlms_parser::Aes128GcmDecryptorTfPsa;
67#elif !defined(USE_ESP8266) && __has_include(<mbedtls/gcm.h>)
68using Aes128GcmDecryptorImpl = dlms_parser::Aes128GcmDecryptorMbedTls;
69#elif __has_include(<bearssl/bearssl.h>)
70using Aes128GcmDecryptorImpl = dlms_parser::Aes128GcmDecryptorBearSsl;
71#else
73#endif
74
75#ifdef USE_SENSOR
76struct SensorItem {
77 std::string obis_code;
79};
80#endif
81#ifdef USE_TEXT_SENSOR
86#endif
87#ifdef USE_BINARY_SENSOR
92#endif
93
95 std::string pattern;
96 std::optional<std::string> name;
97 int priority{0};
98 std::optional<std::array<uint8_t, 6>> default_obis;
99};
100
102 public:
103 DlmsMeterComponent(uint32_t receive_timeout_ms, bool skip_crc_check,
104 std::optional<std::array<uint8_t, 16>> decryption_key,
105 std::optional<std::array<uint8_t, 16>> authentication_key,
106 std::vector<CustomPattern> custom_patterns);
107
108 void setup() override;
109 void dump_config() override;
110 void loop() override;
111
112#ifdef USE_SENSOR
113 void register_sensor(const std::string &obis_code, sensor::Sensor *sensor);
114#endif
115#ifdef USE_TEXT_SENSOR
116 void register_text_sensor(const std::string &obis_code, text_sensor::TextSensor *sensor);
117#endif
118#ifdef USE_BINARY_SENSOR
119 void register_binary_sensor(const std::string &obis_code, binary_sensor::BinarySensor *sensor);
120#endif
121
122 protected:
123 void read_rx_buffer_();
124 void flush_rx_buffer_();
125 void process_frame_();
126 void on_data_(const char *obis_code, float float_val, const char *str_val, bool is_numeric);
127
128 std::array<uint8_t, 2048> rx_buffer_;
131
133 bool skip_crc_check_{false};
134
135 std::vector<CustomPattern> custom_patterns_;
136
138 dlms_parser::DlmsParser parser_;
139
140#ifdef USE_SENSOR
142#endif
143#ifdef USE_TEXT_SENSOR
145#endif
146#ifdef USE_BINARY_SENSOR
148#endif
149};
150
151} // namespace esphome::dlms_meter
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:217
Base class for all binary_sensor-type classes.
bool decrypt_in_place(std::span< const uint8_t > iv, std::span< uint8_t > ciphertext_and_plaintext, std::span< const uint8_t > aad, std::span< const uint8_t > tag) override
Definition dlms_meter.h:58
void set_decryption_key(const dlms_parser::Aes128GcmDecryptionKey &key) override
Definition dlms_meter.h:57
StaticVector< SensorItem, DLMS_MAX_SENSORS > sensors_
Definition dlms_meter.h:141
std::vector< CustomPattern > custom_patterns_
Definition dlms_meter.h:135
void register_sensor(const std::string &obis_code, sensor::Sensor *sensor)
std::array< uint8_t, 2048 > rx_buffer_
Definition dlms_meter.h:128
void register_binary_sensor(const std::string &obis_code, binary_sensor::BinarySensor *sensor)
StaticVector< TextSensorItem, DLMS_MAX_TEXT_SENSORS > text_sensors_
Definition dlms_meter.h:144
StaticVector< BinarySensorItem, DLMS_MAX_BINARY_SENSORS > binary_sensors_
Definition dlms_meter.h:147
void on_data_(const char *obis_code, float float_val, const char *str_val, bool is_numeric)
void register_text_sensor(const std::string &obis_code, text_sensor::TextSensor *sensor)
DlmsMeterComponent(uint32_t receive_timeout_ms, bool skip_crc_check, std::optional< std::array< uint8_t, 16 > > decryption_key, std::optional< std::array< uint8_t, 16 > > authentication_key, std::vector< CustomPattern > custom_patterns)
Base-class for all sensors.
Definition sensor.h:47
dlms_parser::Aes128GcmDecryptorTfPsa Aes128GcmDecryptorImpl
Definition dlms_meter.h:66
const char * tag
Definition log.h:74
static void uint32_t
binary_sensor::BinarySensor * sensor
Definition dlms_meter.h:90
std::optional< std::array< uint8_t, 6 > > default_obis
Definition dlms_meter.h:98
std::optional< std::string > name
Definition dlms_meter.h:96
text_sensor::TextSensor * sensor
Definition dlms_meter.h:84