ESPHome  2024.3.1
ip_address.h
Go to the documentation of this file.
1 #pragma once
2 #include <cstdint>
3 #include <string>
4 #include <cstdio>
5 #include <array>
6 #include "esphome/core/macros.h"
7 
8 #if defined(USE_ESP_IDF) || defined(USE_LIBRETINY) || USE_ARDUINO_VERSION_CODE > VERSION_CODE(3, 0, 0)
9 #include <lwip/ip_addr.h>
10 #endif
11 
12 #if USE_ARDUINO
13 #include <Arduino.h>
14 #include <IPAddress.h>
15 #endif /* USE_ADRDUINO */
16 
17 #ifdef USE_HOST
18 #include <arpa/inet.h>
19 using ip_addr_t = in_addr;
20 using ip4_addr_t = in_addr;
21 #define ipaddr_aton(x, y) inet_aton((x), (y))
22 #endif
23 
24 #if USE_ESP32_FRAMEWORK_ARDUINO
25 #define arduino_ns Arduino_h
26 #elif USE_LIBRETINY
27 #define arduino_ns arduino
28 #elif USE_ARDUINO
29 #define arduino_ns
30 #endif
31 
32 #ifdef USE_ESP32
33 #include <cstring>
34 #include <esp_netif.h>
35 #endif
36 
37 namespace esphome {
38 namespace network {
39 
40 struct IPAddress {
41  public:
42 #ifdef USE_HOST
43  IPAddress() { ip_addr_.s_addr = 0; }
44  IPAddress(uint8_t first, uint8_t second, uint8_t third, uint8_t fourth) {
45  this->ip_addr_.s_addr = htonl((first << 24) | (second << 16) | (third << 8) | fourth);
46  }
47  IPAddress(const std::string &in_address) { inet_aton(in_address.c_str(), &ip_addr_); }
48  IPAddress(const ip_addr_t *other_ip) { ip_addr_ = *other_ip; }
49 #else
50  IPAddress() { ip_addr_set_zero(&ip_addr_); }
51  IPAddress(uint8_t first, uint8_t second, uint8_t third, uint8_t fourth) {
52  IP_ADDR4(&ip_addr_, first, second, third, fourth);
53  }
54  IPAddress(const ip_addr_t *other_ip) { ip_addr_copy(ip_addr_, *other_ip); }
55  IPAddress(const std::string &in_address) { ipaddr_aton(in_address.c_str(), &ip_addr_); }
56  IPAddress(ip4_addr_t *other_ip) {
57  memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(ip4_addr_t));
58 #if USE_ESP32 && LWIP_IPV6
59  ip_addr_.type = IPADDR_TYPE_V4;
60 #endif
61  }
62 #if USE_ARDUINO
63  IPAddress(const arduino_ns::IPAddress &other_ip) { ip_addr_set_ip4_u32(&ip_addr_, other_ip); }
64 #endif
65 #if LWIP_IPV6
66  IPAddress(ip6_addr_t *other_ip) {
67  memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(ip6_addr_t));
68  ip_addr_.type = IPADDR_TYPE_V6;
69  }
70 #endif /* LWIP_IPV6 */
71 
72 #ifdef USE_ESP32
73 #if LWIP_IPV6
74  IPAddress(esp_ip6_addr_t *other_ip) {
75  memcpy((void *) &ip_addr_.u_addr.ip6, (void *) other_ip, sizeof(esp_ip6_addr_t));
76  ip_addr_.type = IPADDR_TYPE_V6;
77  }
78 #endif /* LWIP_IPV6 */
79  IPAddress(esp_ip4_addr_t *other_ip) { memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(esp_ip4_addr_t)); }
80  IPAddress(esp_ip_addr_t *other_ip) {
81 #if LWIP_IPV6
82  memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(ip_addr_));
83 #else
84  memcpy((void *) &ip_addr_, (void *) &other_ip->u_addr.ip4, sizeof(ip_addr_));
85 #endif
86  }
87  operator esp_ip_addr_t() const {
88  esp_ip_addr_t tmp;
89 #if LWIP_IPV6
90  memcpy((void *) &tmp, (void *) &ip_addr_, sizeof(ip_addr_));
91 #else
92  memcpy((void *) &tmp.u_addr.ip4, (void *) &ip_addr_, sizeof(ip_addr_));
93 #endif /* LWIP_IPV6 */
94  return tmp;
95  }
96  operator esp_ip4_addr_t() const {
97  esp_ip4_addr_t tmp;
98 #if LWIP_IPV6
99  memcpy((void *) &tmp, (void *) &ip_addr_.u_addr.ip4, sizeof(esp_ip4_addr_t));
100 #else
101  memcpy((void *) &tmp, (void *) &ip_addr_, sizeof(ip_addr_));
102 #endif /* LWIP_IPV6 */
103  return tmp;
104  }
105 #endif /* USE_ESP32 */
106 
107  operator ip_addr_t() const { return ip_addr_; }
108 #if LWIP_IPV6
109  operator ip4_addr_t() const { return *ip_2_ip4(&ip_addr_); }
110 #endif /* LWIP_IPV6 */
111 
112 #if USE_ARDUINO
113  operator arduino_ns::IPAddress() const { return ip_addr_get_ip4_u32(&ip_addr_); }
114 #endif
115 
116  bool is_set() { return !ip_addr_isany(&ip_addr_); }
117  bool is_ip4() { return IP_IS_V4(&ip_addr_); }
118  bool is_ip6() { return IP_IS_V6(&ip_addr_); }
119  std::string str() const { return ipaddr_ntoa(&ip_addr_); }
120  bool operator==(const IPAddress &other) const { return ip_addr_cmp(&ip_addr_, &other.ip_addr_); }
121  bool operator!=(const IPAddress &other) const { return !ip_addr_cmp(&ip_addr_, &other.ip_addr_); }
122  IPAddress &operator+=(uint8_t increase) {
123  if (IP_IS_V4(&ip_addr_)) {
124 #if LWIP_IPV6
125  (((u8_t *) (&ip_addr_.u_addr.ip4))[3]) += increase;
126 #else
127  (((u8_t *) (&ip_addr_.addr))[3]) += increase;
128 #endif /* LWIP_IPV6 */
129  }
130  return *this;
131  }
132 #endif
133 
134  protected:
136 };
137 
138 using IPAddresses = std::array<IPAddress, 5>;
139 
140 } // namespace network
141 } // namespace esphome
IPAddress(esp_ip6_addr_t *other_ip)
Definition: ip_address.h:74
IPAddress(esp_ip4_addr_t *other_ip)
Definition: ip_address.h:79
IPAddress & operator+=(uint8_t increase)
Definition: ip_address.h:122
bool operator==(const IPAddress &other) const
Definition: ip_address.h:120
std::string str() const
Definition: ip_address.h:119
IPAddress(esp_ip_addr_t *other_ip)
Definition: ip_address.h:80
IPAddress(const ip_addr_t *other_ip)
Definition: ip_address.h:48
IPAddress(const arduino_ns::IPAddress &other_ip)
Definition: ip_address.h:63
IPAddress(ip4_addr_t *other_ip)
Definition: ip_address.h:56
IPAddress(const std::string &in_address)
Definition: ip_address.h:47
in_addr ip4_addr_t
Definition: ip_address.h:20
IPAddress(uint8_t first, uint8_t second, uint8_t third, uint8_t fourth)
Definition: ip_address.h:44
std::array< IPAddress, 5 > IPAddresses
Definition: ip_address.h:138
IPAddress(ip6_addr_t *other_ip)
Definition: ip_address.h:66
in_addr ip_addr_t
Definition: ip_address.h:19
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
bool operator!=(const IPAddress &other) const
Definition: ip_address.h:121