ESPHome 2026.11.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 __has_include(<psa/crypto.h>)
27#include <dlms_parser/decryption/aes_128_gcm_decryptor_tfpsa.h>
28#elif !defined(USE_ESP8266) && __has_include(<mbedtls/gcm.h>)
29#if __has_include(<mbedtls/esp_config.h>)
30#include <mbedtls/esp_config.h>
31#endif
32#include <dlms_parser/decryption/aes_128_gcm_decryptor_mbedtls.h>
33#elif __has_include(<bearssl/bearssl.h>)
34#include <dlms_parser/decryption/aes_128_gcm_decryptor_bearssl.h>
35#else
36#error "The platform doesn't provide a compatible encryption library for dlms_parser"
37#endif
38
39namespace esphome::dlms_meter {
40
41// Entity storage is sized by codegen; a platform with no entities emits no define
42#ifndef DLMS_MAX_SENSORS
43static constexpr uint8_t DLMS_MAX_SENSORS = 0;
44#endif
45#ifndef DLMS_MAX_TEXT_SENSORS
46static constexpr uint8_t DLMS_MAX_TEXT_SENSORS = 0;
47#endif
48#ifndef DLMS_MAX_BINARY_SENSORS
49static constexpr uint8_t DLMS_MAX_BINARY_SENSORS = 0;
50#endif
51
52#ifdef USE_SENSOR
53struct SensorItem {
54 dlms_parser::ObisId obis;
56};
57#endif
58#ifdef USE_TEXT_SENSOR
60 dlms_parser::ObisId obis;
62};
63#endif
64#ifdef USE_BINARY_SENSOR
69#endif
70
72 const char *pattern;
73 const char *name;
75 dlms_parser::ObisId default_obis;
76};
77
78#if __has_include(<psa/crypto.h>)
79using Aes128GcmDecryptorImpl = dlms_parser::Aes128GcmDecryptorTfPsa;
80#elif !defined(USE_ESP8266) && __has_include(<mbedtls/gcm.h>)
81using Aes128GcmDecryptorImpl = dlms_parser::Aes128GcmDecryptorMbedTls;
82#else
83using Aes128GcmDecryptorImpl = dlms_parser::Aes128GcmDecryptorBearSsl;
84#endif
85
86class DlmsMeterComponent final : public Component, public uart::UARTDevice {
87 public:
88 DlmsMeterComponent(uint32_t receive_timeout_ms, bool skip_crc_check, const char *decryption_key,
89 const char *authentication_key, std::vector<CustomPattern> custom_patterns);
90
91 void setup() override;
92 void dump_config() override;
93 void loop() override;
94
95#ifdef USE_SENSOR
96 void register_sensor(const dlms_parser::ObisId &obis, sensor::Sensor *sensor);
97#endif
98#ifdef USE_TEXT_SENSOR
99 void register_text_sensor(const dlms_parser::ObisId &obis, text_sensor::TextSensor *sensor);
100#endif
101#ifdef USE_BINARY_SENSOR
102 void register_binary_sensor(const dlms_parser::ObisId &obis, binary_sensor::BinarySensor *sensor);
103#endif
104
105 protected:
106 void read_rx_buffer_();
107 void flush_rx_buffer_();
108 void process_frame_();
109 void on_data_(const dlms_parser::AxdrCapture &capture);
110
111 std::array<uint8_t, 2048> rx_buffer_;
114
116 bool skip_crc_check_{false};
117
118 std::vector<CustomPattern> custom_patterns_;
119
121 dlms_parser::DlmsParser parser_;
122
123#ifdef USE_SENSOR
125#endif
126#ifdef USE_TEXT_SENSOR
128#endif
129#ifdef USE_BINARY_SENSOR
131#endif
132};
133
134} // namespace esphome::dlms_meter
135
136#endif
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:286
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:124
std::vector< CustomPattern > custom_patterns_
Definition dlms_meter.h:118
void on_data_(const dlms_parser::AxdrCapture &capture)
std::array< uint8_t, 2048 > rx_buffer_
Definition dlms_meter.h:111
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:127
void register_sensor(const dlms_parser::ObisId &obis, sensor::Sensor *sensor)
StaticVector< BinarySensorItem, DLMS_MAX_BINARY_SENSORS > binary_sensors_
Definition dlms_meter.h:130
void register_binary_sensor(const dlms_parser::ObisId &obis, binary_sensor::BinarySensor *sensor)
Base-class for all sensors.
Definition sensor.h:47
dlms_parser::Aes128GcmDecryptorTfPsa Aes128GcmDecryptorImpl
Definition dlms_meter.h:79
static void uint32_t
binary_sensor::BinarySensor * sensor
Definition dlms_meter.h:67
dlms_parser::ObisId default_obis
Definition dlms_meter.h:75
dlms_parser::ObisId obis
Definition dlms_meter.h:54
text_sensor::TextSensor * sensor
Definition dlms_meter.h:61