ESPHome 2025.5.0
Loading...
Searching...
No Matches
packet_transport.h
Go to the documentation of this file.
1#pragma once
2
5#ifdef USE_SENSOR
7#endif
8#ifdef USE_BINARY_SENSOR
10#endif
11#
12#include <vector>
13#include <map>
14
23namespace esphome {
24namespace packet_transport {
25
26struct Provider {
27 std::vector<uint8_t> encryption_key;
28 const char *name;
29 uint32_t last_code[2];
30};
31
32#ifdef USE_SENSOR
33struct Sensor {
35 const char *id;
36 bool updated;
37};
38#endif
39#ifdef USE_BINARY_SENSOR
45#endif
46
48 public:
49 void setup() override;
50 void loop() override;
51 void update() override;
52 void dump_config() override;
53
54#ifdef USE_SENSOR
55 void add_sensor(const char *id, sensor::Sensor *sensor) {
56 Sensor st{sensor, id, true};
57 this->sensors_.push_back(st);
58 }
59 void add_remote_sensor(const char *hostname, const char *remote_id, sensor::Sensor *sensor) {
60 this->add_provider(hostname);
61 this->remote_sensors_[hostname][remote_id] = sensor;
62 }
63#endif
64#ifdef USE_BINARY_SENSOR
65 void add_binary_sensor(const char *id, binary_sensor::BinarySensor *sensor) {
66 BinarySensor st{sensor, id, true};
67 this->binary_sensors_.push_back(st);
68 }
69
70 void add_remote_binary_sensor(const char *hostname, const char *remote_id, binary_sensor::BinarySensor *sensor) {
71 this->add_provider(hostname);
72 this->remote_binary_sensors_[hostname][remote_id] = sensor;
73 }
74#endif
75
76 void add_provider(const char *hostname) {
77 if (this->providers_.count(hostname) == 0) {
78 Provider provider;
79 provider.encryption_key = std::vector<uint8_t>{};
80 provider.last_code[0] = 0;
81 provider.last_code[1] = 0;
82 provider.name = hostname;
83 this->providers_[hostname] = provider;
84#ifdef USE_SENSOR
85 this->remote_sensors_[hostname] = std::map<std::string, sensor::Sensor *>();
86#endif
87#ifdef USE_BINARY_SENSOR
88 this->remote_binary_sensors_[hostname] = std::map<std::string, binary_sensor::BinarySensor *>();
89#endif
90 }
91 }
92
93 void set_encryption_key(std::vector<uint8_t> key) { this->encryption_key_ = std::move(key); }
94 void set_rolling_code_enable(bool enable) { this->rolling_code_enable_ = enable; }
95 void set_ping_pong_enable(bool enable) { this->ping_pong_enable_ = enable; }
96 void set_ping_pong_recycle_time(uint32_t recycle_time) { this->ping_pong_recyle_time_ = recycle_time; }
97 void set_provider_encryption(const char *name, std::vector<uint8_t> key) {
98 this->providers_[name].encryption_key = std::move(key);
99 }
100 void set_platform_name(const char *name) { this->platform_name_ = name; }
101
102 protected:
103 // child classes must implement this
104 virtual void send_packet(const std::vector<uint8_t> &buf) const = 0;
105 virtual size_t get_max_packet_size() = 0;
106 virtual bool should_send() { return true; }
107
108 // to be called by child classes when a data packet is received.
109 void process_(const std::vector<uint8_t> &data);
110 void send_data_(bool all);
111 void flush_();
112 void add_data_(uint8_t key, const char *id, float data);
113 void add_data_(uint8_t key, const char *id, uint32_t data);
114 void increment_code_();
115 void add_binary_data_(uint8_t key, const char *id, bool data);
116 void init_data_();
117
118 bool updated_{};
119 uint32_t ping_key_{};
120 uint32_t rolling_code_[2]{};
124 uint32_t last_key_time_{};
127 const char *name_{};
129
130 std::vector<uint8_t> encryption_key_{};
131
132#ifdef USE_SENSOR
133 std::vector<Sensor> sensors_{};
134 std::map<std::string, std::map<std::string, sensor::Sensor *>> remote_sensors_{};
135#endif
136#ifdef USE_BINARY_SENSOR
137 std::vector<BinarySensor> binary_sensors_{};
138 std::map<std::string, std::map<std::string, binary_sensor::BinarySensor *>> remote_binary_sensors_{};
139#endif
140
141 std::map<std::string, Provider> providers_{};
142 std::vector<uint8_t> ping_header_{};
143 std::vector<uint8_t> header_{};
144 std::vector<uint8_t> data_{};
145 std::map<const char *, uint32_t> ping_keys_{};
146 const char *platform_name_{""};
147 void add_key_(const char *name, uint32_t key);
149
150 inline bool is_encrypted_() { return !this->encryption_key_.empty(); }
151};
152
153} // namespace packet_transport
154} // namespace esphome
This class simplifies creating components that periodically check a state.
Definition component.h:301
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)
std::map< std::string, std::map< std::string, binary_sensor::BinarySensor * > > remote_binary_sensors_
std::vector< BinarySensor > binary_sensors_
void set_ping_pong_recycle_time(uint32_t recycle_time)
void add_key_(const char *name, uint32_t key)
void process_(const std::vector< uint8_t > &data)
Process a received packet.
void add_remote_sensor(const char *hostname, const char *remote_id, sensor::Sensor *sensor)
std::map< std::string, std::map< std::string, sensor::Sensor * > > remote_sensors_
void add_binary_data_(uint8_t key, const char *id, bool data)
std::map< const char *, uint32_t > ping_keys_
void set_encryption_key(std::vector< uint8_t > key)
virtual void send_packet(const std::vector< uint8_t > &buf) const =0
std::map< std::string, Provider > providers_
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:57
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
T id(T value)
Helper function to make id(var) known from lambdas work in custom components.
Definition helpers.h:798
binary_sensor::BinarySensor * sensor
std::vector< uint8_t > encryption_key