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