ESPHome  2023.11.6
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 #if USE_ESP32_FRAMEWORK_ARDUINO
18 #define arduino_ns Arduino_h
19 #elif USE_LIBRETINY
20 #define arduino_ns arduino
21 #elif USE_ARDUINO
22 #define arduino_ns
23 #endif
24 
25 #ifdef USE_ESP32
26 #include <cstring>
27 #include <esp_netif.h>
28 #endif
29 
30 namespace esphome {
31 namespace network {
32 
33 struct IPAddress {
34  public:
35  IPAddress() { ip_addr_set_zero(&ip_addr_); }
36  IPAddress(uint8_t first, uint8_t second, uint8_t third, uint8_t fourth) {
37  IP_ADDR4(&ip_addr_, first, second, third, fourth);
38  }
39  IPAddress(const ip_addr_t *other_ip) { ip_addr_copy(ip_addr_, *other_ip); }
40  IPAddress(const std::string &in_address) { ipaddr_aton(in_address.c_str(), &ip_addr_); }
41  IPAddress(ip4_addr_t *other_ip) {
42  memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(ip4_addr_t));
43 #if USE_ESP32 && LWIP_IPV6
44  ip_addr_.type = IPADDR_TYPE_V4;
45 #endif
46  }
47 #if USE_ARDUINO
48  IPAddress(const arduino_ns::IPAddress &other_ip) { ip_addr_set_ip4_u32(&ip_addr_, other_ip); }
49 #endif
50 #if LWIP_IPV6
51  IPAddress(ip6_addr_t *other_ip) {
52  memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(ip6_addr_t));
53  ip_addr_.type = IPADDR_TYPE_V6;
54  }
55 #endif /* LWIP_IPV6 */
56 
57 #ifdef USE_ESP32
58 #if LWIP_IPV6
59  IPAddress(esp_ip6_addr_t *other_ip) {
60  memcpy((void *) &ip_addr_.u_addr.ip6, (void *) other_ip, sizeof(esp_ip6_addr_t));
61  ip_addr_.type = IPADDR_TYPE_V6;
62  }
63 #endif /* LWIP_IPV6 */
64  IPAddress(esp_ip4_addr_t *other_ip) { memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(esp_ip4_addr_t)); }
65  operator esp_ip_addr_t() const {
66  esp_ip_addr_t tmp;
67 #if LWIP_IPV6
68  memcpy((void *) &tmp, (void *) &ip_addr_, sizeof(ip_addr_));
69 #else
70  memcpy((void *) &tmp.u_addr.ip4, (void *) &ip_addr_, sizeof(ip_addr_));
71 #endif /* LWIP_IPV6 */
72  return tmp;
73  }
74  operator esp_ip4_addr_t() const {
75  esp_ip4_addr_t tmp;
76 #if LWIP_IPV6
77  memcpy((void *) &tmp, (void *) &ip_addr_.u_addr.ip4, sizeof(esp_ip4_addr_t));
78 #else
79  memcpy((void *) &tmp, (void *) &ip_addr_, sizeof(ip_addr_));
80 #endif /* LWIP_IPV6 */
81  return tmp;
82  }
83 #endif /* USE_ESP32 */
84 
85  operator ip_addr_t() const { return ip_addr_; }
86 #if LWIP_IPV6
87  operator ip4_addr_t() const { return *ip_2_ip4(&ip_addr_); }
88 #endif /* LWIP_IPV6 */
89 
90 #if USE_ARDUINO
91  operator arduino_ns::IPAddress() const { return ip_addr_get_ip4_u32(&ip_addr_); }
92 #endif
93 
94  bool is_set() { return !ip_addr_isany(&ip_addr_); }
95  bool is_ip4() { return IP_IS_V4(&ip_addr_); }
96  bool is_ip6() { return IP_IS_V6(&ip_addr_); }
97  std::string str() const { return ipaddr_ntoa(&ip_addr_); }
98  bool operator==(const IPAddress &other) const { return ip_addr_cmp(&ip_addr_, &other.ip_addr_); }
99  bool operator!=(const IPAddress &other) const { return !ip_addr_cmp(&ip_addr_, &other.ip_addr_); }
100  IPAddress &operator+=(uint8_t increase) {
101  if (IP_IS_V4(&ip_addr_)) {
102 #if LWIP_IPV6
103  (((u8_t *) (&ip_addr_.u_addr.ip4))[3]) += increase;
104 #else
105  (((u8_t *) (&ip_addr_.addr))[3]) += increase;
106 #endif /* LWIP_IPV6 */
107  }
108  return *this;
109  }
110 
111  protected:
112  ip_addr_t ip_addr_;
113 };
114 
115 } // namespace network
116 } // namespace esphome
IPAddress(esp_ip6_addr_t *other_ip)
Definition: ip_address.h:59
IPAddress(esp_ip4_addr_t *other_ip)
Definition: ip_address.h:64
IPAddress & operator+=(uint8_t increase)
Definition: ip_address.h:100
bool operator==(const IPAddress &other) const
Definition: ip_address.h:98
std::string str() const
Definition: ip_address.h:97
IPAddress(const ip_addr_t *other_ip)
Definition: ip_address.h:39
IPAddress(const arduino_ns::IPAddress &other_ip)
Definition: ip_address.h:48
IPAddress(ip4_addr_t *other_ip)
Definition: ip_address.h:41
IPAddress(const std::string &in_address)
Definition: ip_address.h:40
IPAddress(uint8_t first, uint8_t second, uint8_t third, uint8_t fourth)
Definition: ip_address.h:36
IPAddress(ip6_addr_t *other_ip)
Definition: ip_address.h:51
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool operator!=(const IPAddress &other) const
Definition: ip_address.h:99