ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
dlms_meter.h
Go to the documentation of this file.
1#pragma once
2
3// Ignore Zephyr. It doesn't have any encryption library.
4#if defined(USE_ESP32) || defined(USE_ARDUINO) || defined(USE_HOST)
5
6#include <array>
7#include <vector>
8
13
14#ifdef USE_SENSOR
16#endif
17#ifdef USE_TEXT_SENSOR
19#endif
20#ifdef USE_BINARY_SENSOR
22#endif
23
24#include <dlms_parser/dlms_parser.h>
25
26#if defined(USE_HOST) && __has_include(<mbedtls/gcm.h>)
27#include <dlms_parser/decryption/aes_128_gcm_decryptor_mbedtls.h>
28namespace esphome::dlms_meter {
29using Aes128GcmDecryptorImpl = dlms_parser::Aes128GcmDecryptorMbedTls;
30} // namespace esphome::dlms_meter
31#elif __has_include(<psa/crypto.h>)
32#include <dlms_parser/decryption/aes_128_gcm_decryptor_tfpsa.h>
33namespace esphome::dlms_meter {
34using Aes128GcmDecryptorImpl = dlms_parser::Aes128GcmDecryptorTfPsa;
35} // namespace esphome::dlms_meter
36#elif defined(USE_HOST)
37#error "dlms_meter on the host needs the Mbed TLS development files: apt install libmbedtls-dev or brew install mbedtls"
38#elif !defined(USE_ESP8266) && __has_include(<mbedtls/gcm.h>)
39#if __has_include(<mbedtls/esp_config.h>)
40#include <mbedtls/esp_config.h>
41#endif
42#include <dlms_parser/decryption/aes_128_gcm_decryptor_mbedtls.h>
43namespace esphome::dlms_meter {
44using Aes128GcmDecryptorImpl = dlms_parser::Aes128GcmDecryptorMbedTls;
45} // namespace esphome::dlms_meter
46#elif __has_include(<bearssl/bearssl.h>)
47#include <dlms_parser/decryption/aes_128_gcm_decryptor_bearssl.h>
48namespace esphome::dlms_meter {
49using Aes128GcmDecryptorImpl = dlms_parser::Aes128GcmDecryptorBearSsl;
50} // namespace esphome::dlms_meter
51#else
52#error "The platform doesn't provide a compatible encryption library for dlms_parser"
53#endif
54
55namespace esphome::dlms_meter {
56
57// Entity storage is sized by codegen; a platform with no entities emits no define
58#ifndef DLMS_MAX_SENSORS
59static constexpr uint8_t DLMS_MAX_SENSORS = 0;
60#endif
61#ifndef DLMS_MAX_TEXT_SENSORS
62static constexpr uint8_t DLMS_MAX_TEXT_SENSORS = 0;
63#endif
64#ifndef DLMS_MAX_BINARY_SENSORS
65static constexpr uint8_t DLMS_MAX_BINARY_SENSORS = 0;
66#endif
67
68#ifdef USE_SENSOR
69struct SensorItem {
70 dlms_parser::ObisId obis;
72};
73#endif
74#ifdef USE_TEXT_SENSOR
76 dlms_parser::ObisId obis;
78};
79#endif
80#ifdef USE_BINARY_SENSOR
85#endif
86
88 const char *pattern;
89 const char *name;
91 dlms_parser::ObisId default_obis;
92};
93
94class DlmsMeterComponent final : public Component, public uart::UARTDevice {
95 public:
96 DlmsMeterComponent(uint32_t receive_timeout_ms, bool skip_crc_check, const char *decryption_key,
97 const char *authentication_key, std::vector<CustomPattern> custom_patterns);
98
99 void setup() override;
100 void dump_config() override;
101 void loop() override;
102
103#ifdef USE_SENSOR
104 void register_sensor(const dlms_parser::ObisId &obis, sensor::Sensor *sensor);
105#endif
106#ifdef USE_TEXT_SENSOR
107 void register_text_sensor(const dlms_parser::ObisId &obis, text_sensor::TextSensor *sensor);
108#endif
109#ifdef USE_BINARY_SENSOR
110 void register_binary_sensor(const dlms_parser::ObisId &obis, binary_sensor::BinarySensor *sensor);
111#endif
112
113 protected:
114 void read_rx_buffer_();
115 void process_frame_();
116 void on_data_(const dlms_parser::AxdrCapture &capture);
117
118 std::array<uint8_t, 2048> rx_buffer_;
121
123 bool skip_crc_check_{false};
124
125 std::vector<CustomPattern> custom_patterns_;
126
128 dlms_parser::DlmsParser parser_;
129
130#ifdef USE_SENSOR
132#endif
133#ifdef USE_TEXT_SENSOR
135#endif
136#ifdef USE_BINARY_SENSOR
138#endif
139};
140
141} // namespace esphome::dlms_meter
142
143#endif
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:335
Base class for all binary_sensor-type classes.
DlmsMeterComponent(uint32_t receive_timeout_ms, bool skip_crc_check, const char *decryption_key, const char *authentication_key, std::vector< CustomPattern > custom_patterns)
StaticVector< SensorItem, DLMS_MAX_SENSORS > sensors_
Definition dlms_meter.h:131
std::vector< CustomPattern > custom_patterns_
Definition dlms_meter.h:125
void on_data_(const dlms_parser::AxdrCapture &capture)
std::array< uint8_t, 2048 > rx_buffer_
Definition dlms_meter.h:118
void register_text_sensor(const dlms_parser::ObisId &obis, text_sensor::TextSensor *sensor)
StaticVector< TextSensorItem, DLMS_MAX_TEXT_SENSORS > text_sensors_
Definition dlms_meter.h:134
void register_sensor(const dlms_parser::ObisId &obis, sensor::Sensor *sensor)
StaticVector< BinarySensorItem, DLMS_MAX_BINARY_SENSORS > binary_sensors_
Definition dlms_meter.h:137
void register_binary_sensor(const dlms_parser::ObisId &obis, binary_sensor::BinarySensor *sensor)
Base-class for all sensors.
Definition sensor.h:48
dlms_parser::Aes128GcmDecryptorMbedTls Aes128GcmDecryptorImpl
Definition dlms_meter.h:29
static void uint32_t
binary_sensor::BinarySensor * sensor
Definition dlms_meter.h:83
dlms_parser::ObisId default_obis
Definition dlms_meter.h:91
dlms_parser::ObisId obis
Definition dlms_meter.h:70
text_sensor::TextSensor * sensor
Definition dlms_meter.h:77