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