ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
packet_transport.h
Go to the documentation of this file.
1#pragma once
2
6#ifdef USE_SENSOR
8#endif
9#ifdef USE_BINARY_SENSOR
11#endif
12
13#include <map>
14#include <span>
15#include <vector>
16
25namespace esphome {
26namespace packet_transport {
27
28// std::less provides allocation-free comparison with const char *
29template<typename T> using string_map_t = std::map<std::string, T, std::less<>>;
30
31struct Provider {
32 std::vector<uint8_t> encryption_key;
33 const char *name;
36#ifdef USE_STATUS_SENSOR
38#endif
39};
40
41class PacketTransport;
42
43#ifdef USE_SENSOR
50#endif
51#ifdef USE_BINARY_SENSOR
58#endif
59
61 public:
62 void setup() override;
63 void loop() override;
64 void update() override;
65 void dump_config() override;
66
67#ifdef USE_SENSOR
68 void set_sensor_count(size_t count) { this->sensors_.init(count); }
69 void add_sensor(const char *id, sensor::Sensor *sensor) {
70 Sensor st{sensor, id, true, this};
71 this->sensors_.push_back(st);
72 }
73 void add_remote_sensor(const char *hostname, const char *remote_id, sensor::Sensor *sensor) {
74 this->add_provider(hostname);
75 this->remote_sensors_[hostname][remote_id] = sensor;
76 }
77#endif
78#ifdef USE_BINARY_SENSOR
79 void set_binary_sensor_count(size_t count) { this->binary_sensors_.init(count); }
80 void add_binary_sensor(const char *id, binary_sensor::BinarySensor *sensor) {
81 BinarySensor st{sensor, id, true, this};
82 this->binary_sensors_.push_back(st);
83 }
84
85 void add_remote_binary_sensor(const char *hostname, const char *remote_id, binary_sensor::BinarySensor *sensor) {
86 this->add_provider(hostname);
87 this->remote_binary_sensors_[hostname][remote_id] = sensor;
88 }
89#endif
90
91 void add_provider(const char *hostname) {
92 if (!this->providers_.contains(hostname)) {
93 Provider provider{};
94 provider.name = hostname;
95 this->providers_[hostname] = provider;
96#ifdef USE_SENSOR
98#endif
99#ifdef USE_BINARY_SENSOR
101#endif
102 }
103 }
104
105 void set_is_provider(bool is_provider) { this->is_provider_ = is_provider; }
106 void set_encryption_key(std::vector<uint8_t> key) { this->encryption_key_ = std::move(key); }
107 void set_rolling_code_enable(bool enable) { this->rolling_code_enable_ = enable; }
108 void set_ping_pong_enable(bool enable) { this->ping_pong_enable_ = enable; }
109 void set_ping_pong_recycle_time(uint32_t recycle_time) { this->ping_pong_recyle_time_ = recycle_time; }
110 void set_provider_encryption(const char *name, std::vector<uint8_t> key) {
111 this->providers_[name].encryption_key = std::move(key);
112 }
113#ifdef USE_STATUS_SENSOR
115 this->providers_[name].status_sensor = sensor;
116 }
117#endif
118 void set_platform_name(const char *name) { this->platform_name_ = name; }
119
120 protected:
121 // child classes must implement this
122 virtual void send_packet(const std::vector<uint8_t> &buf) const = 0;
123 virtual size_t get_max_packet_size() = 0;
124 virtual bool should_send() { return true; }
125
126 // to be called by child classes when a data packet is received.
127 void process_(std::span<const uint8_t> data);
128 void send_data_(bool all);
129 void flush_();
130 void add_data_(uint8_t key, const char *id, float data);
131 void add_data_(uint8_t key, const char *id, uint32_t data);
132 void increment_code_();
133 void add_binary_data_(uint8_t key, const char *id, bool data);
134 void init_data_();
135
136 bool updated_{};
145 const char *name_{};
147
148 std::vector<uint8_t> encryption_key_{};
149
150#ifdef USE_SENSOR
153#endif
154#ifdef USE_BINARY_SENSOR
157#endif
158
160 std::vector<uint8_t> ping_header_{};
161 std::vector<uint8_t> header_{};
162 std::vector<uint8_t> data_{};
164 const char *platform_name_{""};
165 void add_key_(const char *name, uint32_t key);
167
168 bool is_encrypted_() const { return !this->encryption_key_.empty(); }
169};
170
171} // namespace packet_transport
172} // namespace esphome
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:522
This class simplifies creating components that periodically check a state.
Definition component.h:602
Base class for all binary_sensor-type classes.
void set_provider_encryption(const char *name, std::vector< uint8_t > key)
void add_remote_binary_sensor(const char *hostname, const char *remote_id, binary_sensor::BinarySensor *sensor)
void add_data_(uint8_t key, const char *id, float data)
FixedVector< BinarySensor > binary_sensors_
void set_ping_pong_recycle_time(uint32_t recycle_time)
void add_key_(const char *name, uint32_t key)
string_map_t< string_map_t< binary_sensor::BinarySensor * > > remote_binary_sensors_
void add_remote_sensor(const char *hostname, const char *remote_id, sensor::Sensor *sensor)
void set_provider_status_sensor(const char *name, binary_sensor::BinarySensor *sensor)
void add_binary_data_(uint8_t key, const char *id, bool data)
void process_(std::span< const uint8_t > data)
Process a received packet.
void set_encryption_key(std::vector< uint8_t > key)
virtual void send_packet(const std::vector< uint8_t > &buf) const =0
string_map_t< string_map_t< sensor::Sensor * > > remote_sensors_
void add_binary_sensor(const char *id, binary_sensor::BinarySensor *sensor)
void add_sensor(const char *id, sensor::Sensor *sensor)
Base-class for all sensors.
Definition sensor.h:47
uint16_t id
std::map< std::string, T, std::less<> > string_map_t
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
static void uint32_t
binary_sensor::BinarySensor * sensor
std::vector< uint8_t > encryption_key
binary_sensor::BinarySensor * status_sensor