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