ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
util.cpp
Go to the documentation of this file.
1#include "util.h"
5#include "esphome/core/log.h"
6#ifdef USE_NETWORK
7
8namespace esphome::network {
9
10#if defined(USE_ESP32) && USE_NETWORK_IPV6
11ESPHOME_LOG_TAG(TAG, "network");
12
13void fill_ip6_addresses(esp_netif_t *netif, IPAddresses &addresses) {
14 esp_ip6_addr_t ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
15 size_t count = esp_netif_get_all_ip6(netif, ip6s);
16 // Slot 0 holds the IPv4 address
17 if (count > addresses.size() - 1) {
18 // Warn once per interface
19 static esp_netif_t *warned[3]{};
20 for (auto &slot : warned) {
21 if (slot == netif) {
22 break;
23 }
24 if (slot == nullptr) {
25 slot = netif;
26 ESP_LOGW(TAG, "%s has %zu IPv6 addresses, only %zu are reported", esp_netif_get_desc(netif), count,
27 addresses.size() - 1);
28 break;
29 }
30 }
31 count = addresses.size() - 1;
32 }
33 for (size_t i = 0; i < count; i++) {
34 addresses[i + 1] = IPAddress(&ip6s[i]);
35 }
36}
37#endif
38
39// The order of the components is important: WiFi should come after any possible main interfaces (it may be used as
40// an AP that uses a previous interface for NAT).
41
43 // The network is disabled only when every configured interface with a
44 // disable() lifecycle is disabled; one enabled interface means traffic can flow.
45 bool disabled = false;
46#ifdef USE_MODEM
47 if (modem::global_modem_component != nullptr) {
49 return false;
50 disabled = true;
51 }
52#endif
53
54#ifdef USE_WIFI
55 if (wifi::global_wifi_component != nullptr) {
57 return false;
58 disabled = true;
59 }
60#endif
61
62#ifdef USE_ETHERNET
63 if (ethernet::global_eth_component != nullptr) {
65 return false;
66 disabled = true;
67 }
68#endif
69 return disabled;
70}
71
72const char *get_use_address_to(std::span<char, USE_ADDRESS_BUFFER_SIZE> buf) {
73 // Global component pointers are guaranteed to be set by component constructors when USE_* is defined.
74 // A wifi-first network: priority: list sets USE_NETWORK_PRIMARY_INTERFACE_WIFI to lift
75 // wifi ahead of the fixed ethernet-first order below; an ethernet-first list already
76 // matches that order, so no define exists for it.
77 const char *addr = nullptr;
78#if defined(USE_NETWORK_PRIMARY_INTERFACE_WIFI) && defined(USE_WIFI)
80#elif defined(USE_ETHERNET)
82#elif defined(USE_MODEM)
84#elif defined(USE_WIFI)
86#elif defined(USE_OPENTHREAD)
87 addr = openthread::global_openthread_component->get_use_address();
88#endif
89 if (addr != nullptr && addr[0] != '\0')
90 return addr;
91 // No explicit use_address configured: the address is the runtime device name
92 // (which includes the MAC suffix when name_add_mac_suffix is enabled) plus ".local"
93 const auto &name = App.get_name();
94 make_name_with_suffix_to(buf.data(), buf.size(), name.c_str(), name.size(), '.', "local", 5);
95 return buf.data();
96}
97
99 // With a wifi-first network: priority: list, prefer wifi while it has a valid IP;
100 // otherwise fall through to the fixed ethernet-first order below. Selection based
101 // on the runtime-active interface is a planned follow-up.
102#if defined(USE_NETWORK_PRIMARY_INTERFACE_WIFI) && defined(USE_WIFI)
103 if (wifi::global_wifi_component != nullptr) {
105 for (const auto &ip : ips) {
106 if (ip.is_set())
107 return ips;
108 }
109 }
110#endif
111
112#ifdef USE_ETHERNET
113 if (ethernet::global_eth_component != nullptr)
115#endif
116
117#ifdef USE_MODEM
118 if (modem::global_modem_component != nullptr)
120#endif
121
122#ifdef USE_WIFI
123 if (wifi::global_wifi_component != nullptr)
125#endif
126#ifdef USE_OPENTHREAD
128 return openthread::global_openthread_component->get_ip_addresses();
129#endif
130 return {};
131}
132
133} // namespace esphome::network
134#endif
const StringRef & get_name() const
Get the name of this Application set by pre_setup().
const char * get_use_address() const
Returns nullptr when no explicit use_address is configured and the address is derived at runtime from...
network::IPAddresses get_ip_addresses()
const char * get_use_address() const
network::IPAddresses get_ip_addresses()
const char * get_use_address() const
Returns nullptr when no explicit use_address is configured and the address is derived at runtime from...
unsigned count
Definition gif.cpp:51
EthernetComponent * global_eth_component
ModemComponent * global_modem_component
std::array< IPAddress, 5 > IPAddresses
Definition ip_address.h:369
void fill_ip6_addresses(esp_netif_t *netif, IPAddresses &addresses)
Copies the IPv6 addresses of an esp_netif into addresses[1..], warning once when some do not fit.
Definition util.cpp:13
ESPHOME_LOG_TAG(TAG, "network")
const char * get_use_address_to(std::span< char, USE_ADDRESS_BUFFER_SIZE > buf)
Get the active network address for logging.
Definition util.cpp:72
network::IPAddresses get_ip_addresses()
Definition util.cpp:98
bool is_disabled()
Return whether the network is disabled: every configured interface with a disable() lifecycle (modem,...
Definition util.cpp:42
OpenThreadComponent * global_openthread_component
WiFiComponent * global_wifi_component
size_t make_name_with_suffix_to(char *buffer, size_t buffer_size, const char *name, size_t name_len, char sep, const char *suffix_ptr, size_t suffix_len)
Format name + separator + suffix directly into buffer without heap allocation.
Definition helpers.cpp:277
Application App
Global storage of Application pointer - only one Application can exist.
struct esp_netif_obj esp_netif_t