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