ESPHome  2025.2.0
udp_component.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 #if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
13 #endif
14 #ifdef USE_SOCKET_IMPL_LWIP_TCP
15 #include <WiFiUdp.h>
16 #endif
17 #include <vector>
18 #include <map>
19 
20 namespace esphome {
21 namespace udp {
22 
23 struct Provider {
24  std::vector<uint8_t> encryption_key;
25  const char *name;
26  uint32_t last_code[2];
27 };
28 
29 #ifdef USE_SENSOR
30 struct Sensor {
32  const char *id;
33  bool updated;
34 };
35 #endif
36 #ifdef USE_BINARY_SENSOR
37 struct BinarySensor {
39  const char *id;
40  bool updated;
41 };
42 #endif
43 
45  public:
46  void setup() override;
47  void loop() override;
48  void update() override;
49  void dump_config() override;
50 
51 #ifdef USE_SENSOR
52  void add_sensor(const char *id, sensor::Sensor *sensor) {
53  Sensor st{sensor, id, true};
54  this->sensors_.push_back(st);
55  }
56  void add_remote_sensor(const char *hostname, const char *remote_id, sensor::Sensor *sensor) {
57  this->add_provider(hostname);
58  this->remote_sensors_[hostname][remote_id] = sensor;
59  }
60 #endif
61 #ifdef USE_BINARY_SENSOR
63  BinarySensor st{sensor, id, true};
64  this->binary_sensors_.push_back(st);
65  }
66 
67  void add_remote_binary_sensor(const char *hostname, const char *remote_id, binary_sensor::BinarySensor *sensor) {
68  this->add_provider(hostname);
69  this->remote_binary_sensors_[hostname][remote_id] = sensor;
70  }
71 #endif
72  void add_address(const char *addr) { this->addresses_.emplace_back(addr); }
73  void set_listen_address(const char *listen_addr) { this->listen_address_ = network::IPAddress(listen_addr); }
74  void set_port(uint16_t port) { this->port_ = port; }
75  float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
76 
77  void add_provider(const char *hostname) {
78  if (this->providers_.count(hostname) == 0) {
79  Provider provider;
80  provider.encryption_key = std::vector<uint8_t>{};
81  provider.last_code[0] = 0;
82  provider.last_code[1] = 0;
83  provider.name = hostname;
84  this->providers_[hostname] = provider;
85 #ifdef USE_SENSOR
86  this->remote_sensors_[hostname] = std::map<std::string, sensor::Sensor *>();
87 #endif
88 #ifdef USE_BINARY_SENSOR
89  this->remote_binary_sensors_[hostname] = std::map<std::string, binary_sensor::BinarySensor *>();
90 #endif
91  }
92  }
93 
94  void set_encryption_key(std::vector<uint8_t> key) { this->encryption_key_ = std::move(key); }
95  void set_rolling_code_enable(bool enable) { this->rolling_code_enable_ = enable; }
96  void set_ping_pong_enable(bool enable) { this->ping_pong_enable_ = enable; }
97  void set_ping_pong_recycle_time(uint32_t recycle_time) { this->ping_pong_recyle_time_ = recycle_time; }
98  void set_provider_encryption(const char *name, std::vector<uint8_t> key) {
99  this->providers_[name].encryption_key = std::move(key);
100  }
101 
102  protected:
103  void send_data_(bool all);
104  void process_(uint8_t *buf, size_t len);
105  void flush_();
106  void add_data_(uint8_t key, const char *id, float data);
107  void add_data_(uint8_t key, const char *id, uint32_t data);
108  void increment_code_();
109  void add_binary_data_(uint8_t key, const char *id, bool data);
110  void init_data_();
111 
112  bool updated_{};
113  uint16_t port_{18511};
114  uint32_t ping_key_{};
115  uint32_t rolling_code_[2]{};
116  bool rolling_code_enable_{};
117  bool ping_pong_enable_{};
118  uint32_t ping_pong_recyle_time_{};
119  uint32_t last_key_time_{};
120  bool resend_ping_key_{};
121  bool resend_data_{};
122  bool should_send_{};
123  const char *name_{};
124  bool should_listen_{};
126 
127 #if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
128  std::unique_ptr<socket::Socket> broadcast_socket_ = nullptr;
129  std::unique_ptr<socket::Socket> listen_socket_ = nullptr;
130  std::vector<struct sockaddr> sockaddrs_{};
131 #endif
132 #ifdef USE_SOCKET_IMPL_LWIP_TCP
133  std::vector<IPAddress> ipaddrs_{};
134  WiFiUDP udp_client_{};
135 #endif
136  std::vector<uint8_t> encryption_key_{};
137  std::vector<std::string> addresses_{};
138 
139 #ifdef USE_SENSOR
140  std::vector<Sensor> sensors_{};
141  std::map<std::string, std::map<std::string, sensor::Sensor *>> remote_sensors_{};
142 #endif
143 #ifdef USE_BINARY_SENSOR
144  std::vector<BinarySensor> binary_sensors_{};
145  std::map<std::string, std::map<std::string, binary_sensor::BinarySensor *>> remote_binary_sensors_{};
146 #endif
147 
148  optional<network::IPAddress> listen_address_{};
149  std::map<std::string, Provider> providers_{};
150  std::vector<uint8_t> ping_header_{};
151  std::vector<uint8_t> header_{};
152  std::vector<uint8_t> data_{};
153  std::map<const char *, uint32_t> ping_keys_{};
154  void add_key_(const char *name, uint32_t key);
155  void send_ping_pong_request_();
156  void send_packet_(void *data, size_t len);
157  void process_ping_request_(const char *name, uint8_t *ptr, size_t len);
158 
159  inline bool is_encrypted_() { return !this->encryption_key_.empty(); }
160 };
161 
162 } // namespace udp
163 } // namespace esphome
void setup()
void add_remote_sensor(const char *hostname, const char *remote_id, sensor::Sensor *sensor)
Definition: udp_component.h:56
void loop()
void add_sensor(const char *id, sensor::Sensor *sensor)
Definition: udp_component.h:52
void set_rolling_code_enable(bool enable)
Definition: udp_component.h:95
std::vector< uint8_t > encryption_key
Definition: udp_component.h:24
binary_sensor::BinarySensor * sensor
Definition: udp_component.h:38
void add_provider(const char *hostname)
Definition: udp_component.h:77
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition: component.cpp:26
void set_encryption_key(std::vector< uint8_t > key)
Definition: udp_component.h:94
T id(T value)
Helper function to make id(var) known from lambdas work in custom components.
Definition: helpers.h:777
This class simplifies creating components that periodically check a state.
Definition: component.h:283
void add_remote_binary_sensor(const char *hostname, const char *remote_id, binary_sensor::BinarySensor *sensor)
Definition: udp_component.h:67
void add_binary_sensor(const char *id, binary_sensor::BinarySensor *sensor)
Definition: udp_component.h:62
void set_provider_encryption(const char *name, std::vector< uint8_t > key)
Definition: udp_component.h:98
void add_address(const char *addr)
Definition: udp_component.h:72
void set_listen_address(const char *listen_addr)
Definition: udp_component.h:73
void set_ping_pong_enable(bool enable)
Definition: udp_component.h:96
float get_setup_priority() const override
Definition: udp_component.h:75
ESPPreferenceObject pref_
std::string size_t len
Definition: helpers.h:301
sensor::Sensor * sensor
Definition: udp_component.h:31
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:37
void set_ping_pong_recycle_time(uint32_t recycle_time)
Definition: udp_component.h:97
Base-class for all sensors.
Definition: sensor.h:57
esphome::sensor::Sensor * sensor
Definition: statsd.h:38
void set_port(uint16_t port)
Definition: udp_component.h:74