ESPHome 2026.7.4
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 const char *addr = nullptr;
28#if defined(USE_ETHERNET)
30#elif defined(USE_MODEM)
31 addr = modem::global_modem_component->get_use_address();
32#elif defined(USE_WIFI)
34#elif defined(USE_OPENTHREAD)
36#endif
37 if (addr != nullptr && addr[0] != '\0')
38 return addr;
39 // No explicit use_address configured: the address is the runtime device name
40 // (which includes the MAC suffix when name_add_mac_suffix is enabled) plus ".local"
41 const auto &name = App.get_name();
42 make_name_with_suffix_to(buf.data(), buf.size(), name.c_str(), name.size(), '.', "local", 5);
43 return buf.data();
44}
45
47#ifdef USE_ETHERNET
48 if (ethernet::global_eth_component != nullptr)
50#endif
51
52#ifdef USE_MODEM
53 if (modem::global_modem_component != nullptr)
54 return modem::global_modem_component->get_ip_addresses();
55#endif
56
57#ifdef USE_WIFI
58 if (wifi::global_wifi_component != nullptr)
60#endif
61#ifdef USE_OPENTHREAD
64#endif
65 return {};
66}
67
68} // namespace esphome::network
69#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:223
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:46
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.