ESPHome 2026.6.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
26
27// std::less provides allocation-free comparison with const char *
28template<typename T> using string_map_t = std::map<std::string, T, std::less<>>;
29
30struct Provider {
31 std::vector<uint8_t> encryption_key;
32 const char *name;
35#ifdef USE_STATUS_SENSOR
37#endif
38};
39
40class PacketTransport;
41
42#ifdef USE_SENSOR
49#endif
50#ifdef USE_BINARY_SENSOR
57#endif
58
60 public:
61 void setup() override;
62 void loop() override;
63 void update() override;
64 void dump_config() override;
65
66#ifdef USE_SENSOR
67 void set_sensor_count(size_t count) { this->sensors_.init(count); }
68 void add_sensor(const char *id, sensor::Sensor *sensor) {
69 Sensor st{sensor, id, true, this};
70 this->sensors_.push_back(st);
71 }
72 void add_remote_sensor(const char *hostname, const char *remote_id, sensor::Sensor *sensor) {
73 this->add_provider(hostname);
74 this->remote_sensors_[hostname][remote_id] = sensor;
75 }
76#endif
77#ifdef USE_BINARY_SENSOR
78 void set_binary_sensor_count(size_t count) { this->binary_sensors_.init(count); }
79 void add_binary_sensor(const char *id, binary_sensor::BinarySensor *sensor) {
80 BinarySensor st{sensor, id, true, this};
81 this->binary_sensors_.push_back(st);
82 }
83
84 void add_remote_binary_sensor(const char *hostname, const char *remote_id, binary_sensor::BinarySensor *sensor) {
85 this->add_provider(hostname);
86 this->remote_binary_sensors_[hostname][remote_id] = sensor;
87 }
88#endif
89
90 void add_provider(const char *hostname) {
91 if (!this->providers_.contains(hostname)) {
92 Provider provider{};
93 provider.name = hostname;
94 this->providers_[hostname] = provider;
95#ifdef USE_SENSOR
97#endif
98#ifdef USE_BINARY_SENSOR
100#endif
101 }
102 }
103
104 void set_is_provider(bool is_provider) { this->is_provider_ = is_provider; }
105 void set_encryption_key(std::vector<uint8_t> key) { this->encryption_key_ = std::move(key); }
106 void set_rolling_code_enable(bool enable) { this->rolling_code_enable_ = enable; }
107 void set_ping_pong_enable(bool enable) { this->ping_pong_enable_ = enable; }
108 void set_ping_pong_recycle_time(uint32_t recycle_time) { this->ping_pong_recyle_time_ = recycle_time; }
109 void set_provider_encryption(const char *name, std::vector<uint8_t> key) {
110 this->providers_[name].encryption_key = std::move(key);
111 }
112#ifdef USE_STATUS_SENSOR
114 this->providers_[name].status_sensor = sensor;
115 }
116#endif
117 void set_platform_name(const char *name) { this->platform_name_ = name; }
118
119 protected:
120 // child classes must implement this
121 virtual void send_packet(const std::vector<uint8_t> &buf) const = 0;
122 virtual size_t get_max_packet_size() = 0;
123 virtual bool should_send() { return true; }
124
125 // to be called by child classes when a data packet is received.
126 void process_(std::span<const uint8_t> data);
127 void send_data_(bool all);
128 void flush_();
129 void add_data_(uint8_t key, const char *id, float data);
130 void add_data_(uint8_t key, const char *id, uint32_t data);
131 void increment_code_();
132 void add_binary_data_(uint8_t key, const char *id, bool data);
133 void init_data_();
134
135 bool updated_{};
144 const char *name_{};
146
147 std::vector<uint8_t> encryption_key_{};
148
149#ifdef USE_SENSOR
152#endif
153#ifdef USE_BINARY_SENSOR
156#endif
157
159 std::vector<uint8_t> ping_header_{};
160 std::vector<uint8_t> header_{};
161 std::vector<uint8_t> data_{};
163 const char *platform_name_{""};
164 void add_key_(const char *name, uint32_t key);
166
167 bool is_encrypted_() const { return !this->encryption_key_.empty(); }
168};
169
170} // namespace esphome::packet_transport
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:529
This class simplifies creating components that periodically check a state.
Definition component.h:585
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
Providing packet encoding functions for exchanging data with a remote host.
std::map< std::string, T, std::less<> > string_map_t
static void uint32_t
binary_sensor::BinarySensor * sensor
std::vector< uint8_t > encryption_key
binary_sensor::BinarySensor * status_sensor