ESPHome 2026.8.0-dev
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#ifdef USE_MODEM
14 if (modem::global_modem_component != nullptr)
15 return modem::global_modem_component->is_disabled();
16#endif
17
18#ifdef USE_WIFI
19 if (wifi::global_wifi_component != nullptr)
21#endif
22 return false;
23}
24
25const char *get_use_address_to(std::span<char, USE_ADDRESS_BUFFER_SIZE> buf) {
26 // Global component pointers are guaranteed to be set by component constructors when USE_* is defined.
27 // A wifi-first network: priority: list sets USE_NETWORK_PRIMARY_INTERFACE_WIFI to lift
28 // wifi ahead of the fixed ethernet-first order below; an ethernet-first list already
29 // matches that order, so no define exists for it.
30 const char *addr = nullptr;
31#if defined(USE_NETWORK_PRIMARY_INTERFACE_WIFI) && defined(USE_WIFI)
33#elif defined(USE_ETHERNET)
35#elif defined(USE_MODEM)
36 addr = modem::global_modem_component->get_use_address();
37#elif defined(USE_WIFI)
39#elif defined(USE_OPENTHREAD)
41#endif
42 if (addr != nullptr && addr[0] != '\0')
43 return addr;
44 // No explicit use_address configured: the address is the runtime device name
45 // (which includes the MAC suffix when name_add_mac_suffix is enabled) plus ".local"
46 const auto &name = App.get_name();
47 make_name_with_suffix_to(buf.data(), buf.size(), name.c_str(), name.size(), '.', "local", 5);
48 return buf.data();
49}
50
52 // With a wifi-first network: priority: list, prefer wifi while it has a valid IP;
53 // otherwise fall through to the fixed ethernet-first order below. Selection based
54 // on the runtime-active interface is a planned follow-up.
55#if defined(USE_NETWORK_PRIMARY_INTERFACE_WIFI) && defined(USE_WIFI)
56 if (wifi::global_wifi_component != nullptr) {
58 for (const auto &ip : ips) {
59 if (ip.is_set())
60 return ips;
61 }
62 }
63#endif
64
65#ifdef USE_ETHERNET
66 if (ethernet::global_eth_component != nullptr)
68#endif
69
70#ifdef USE_MODEM
71 if (modem::global_modem_component != nullptr)
72 return modem::global_modem_component->get_ip_addresses();
73#endif
74
75#ifdef USE_WIFI
76 if (wifi::global_wifi_component != nullptr)
78#endif
79#ifdef USE_OPENTHREAD
82#endif
83 return {};
84}
85
86} // namespace esphome::network
87#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:25
network::IPAddresses get_ip_addresses()
Definition util.cpp:51
bool is_disabled()
Return whether the network is disabled (only wifi for now)
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.