ESPHome 2026.8.2
Loading...
Searching...
No Matches
util.cpp
Go to the documentation of this file.
1#include "util.h"
5#ifdef USE_NETWORK
6
7namespace esphome::network {
8
9// The order of the components is important: WiFi should come after any possible main interfaces (it may be used as
10// an AP that uses a previous interface for NAT).
11
13 // The network is disabled only when every configured interface with a
14 // disable() lifecycle is disabled; one enabled interface means traffic can flow.
15 bool disabled = false;
16#ifdef USE_MODEM
17 if (modem::global_modem_component != nullptr) {
18 if (!modem::global_modem_component->is_disabled())
19 return false;
20 disabled = true;
21 }
22#endif
23
24#ifdef USE_WIFI
25 if (wifi::global_wifi_component != nullptr) {
27 return false;
28 disabled = true;
29 }
30#endif
31
32#ifdef USE_ETHERNET
33 if (ethernet::global_eth_component != nullptr) {
35 return false;
36 disabled = true;
37 }
38#endif
39 return disabled;
40}
41
42const char *get_use_address_to(std::span<char, USE_ADDRESS_BUFFER_SIZE> buf) {
43 // Global component pointers are guaranteed to be set by component constructors when USE_* is defined.
44 // A wifi-first network: priority: list sets USE_NETWORK_PRIMARY_INTERFACE_WIFI to lift
45 // wifi ahead of the fixed ethernet-first order below; an ethernet-first list already
46 // matches that order, so no define exists for it.
47 const char *addr = nullptr;
48#if defined(USE_NETWORK_PRIMARY_INTERFACE_WIFI) && defined(USE_WIFI)
50#elif defined(USE_ETHERNET)
52#elif defined(USE_MODEM)
53 addr = modem::global_modem_component->get_use_address();
54#elif defined(USE_WIFI)
56#elif defined(USE_OPENTHREAD)
58#endif
59 if (addr != nullptr && addr[0] != '\0')
60 return addr;
61 // No explicit use_address configured: the address is the runtime device name
62 // (which includes the MAC suffix when name_add_mac_suffix is enabled) plus ".local"
63 const auto &name = App.get_name();
64 make_name_with_suffix_to(buf.data(), buf.size(), name.c_str(), name.size(), '.', "local", 5);
65 return buf.data();
66}
67
69 // With a wifi-first network: priority: list, prefer wifi while it has a valid IP;
70 // otherwise fall through to the fixed ethernet-first order below. Selection based
71 // on the runtime-active interface is a planned follow-up.
72#if defined(USE_NETWORK_PRIMARY_INTERFACE_WIFI) && defined(USE_WIFI)
73 if (wifi::global_wifi_component != nullptr) {
75 for (const auto &ip : ips) {
76 if (ip.is_set())
77 return ips;
78 }
79 }
80#endif
81
82#ifdef USE_ETHERNET
83 if (ethernet::global_eth_component != nullptr)
85#endif
86
87#ifdef USE_MODEM
88 if (modem::global_modem_component != nullptr)
89 return modem::global_modem_component->get_ip_addresses();
90#endif
91
92#ifdef USE_WIFI
93 if (wifi::global_wifi_component != nullptr)
95#endif
96#ifdef USE_OPENTHREAD
99#endif
100 return {};
101}
102
103} // namespace esphome::network
104#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...
const char * get_use_address() const
Returns nullptr when no explicit use_address is configured and the address is derived at runtime from...
Definition openthread.h:44
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...
EthernetComponent * global_eth_component
std::array< IPAddress, 5 > IPAddresses
Definition ip_address.h:299
const char * get_use_address_to(std::span< char, USE_ADDRESS_BUFFER_SIZE > buf)
Get the active network address for logging.
Definition util.cpp:42
network::IPAddresses get_ip_addresses()
Definition util.cpp:68
bool is_disabled()
Return whether the network is disabled: every configured interface with a disable() lifecycle (modem,...
Definition util.cpp:12
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)
Zero-allocation version: format name + separator + suffix directly into buffer.
Definition helpers.cpp:241
Application App
Global storage of Application pointer - only one Application can exist.