ESPHome 2026.9.0
Loading...
Searching...
No Matches
util.h
Go to the documentation of this file.
1#pragma once
3#ifdef USE_NETWORK
4#include <span>
5#include <string>
7#include "ip_address.h"
8
9#ifdef USE_ETHERNET
11#endif
12#ifdef USE_MODEM
13#include "esphome/components/modem/modem_component.h"
14#endif
15#ifdef USE_WIFI
17#endif
18#ifdef USE_OPENTHREAD
20#endif
21
22namespace esphome::network {
23
24// The order of the components is important: WiFi should come after any possible main interfaces (it may be used as
25// an AP that uses a previous interface for NAT).
26
28ESPHOME_ALWAYS_INLINE inline bool is_connected() {
29 // With a single interface enabled the checks below collapse to `if (x) return true; return false;`, which
30 // clang-tidy wants folded into one return. Keep the per-interface form so every enabled interface is checked.
31 // NOLINTBEGIN(readability-simplify-boolean-expr)
32#ifdef USE_ETHERNET
34 return true;
35#endif
36
37#ifdef USE_MODEM
38 if (modem::global_modem_component != nullptr && modem::global_modem_component->is_connected())
39 return true;
40#endif
41
42#ifdef USE_WIFI
44 return true;
45#endif
46
47#ifdef USE_OPENTHREAD
49 return true;
50#endif
51
52#ifdef USE_HOST
53 return true; // Assume it's connected
54#endif
55 return false;
56 // NOLINTEND(readability-simplify-boolean-expr)
57}
58
61bool is_disabled();
63static constexpr size_t USE_ADDRESS_BUFFER_SIZE = 70;
68const char *get_use_address_to(std::span<char, USE_ADDRESS_BUFFER_SIZE> buf);
70
71} // namespace esphome::network
72#endif
EthernetComponent * global_eth_component
std::array< IPAddress, 5 > IPAddresses
Definition ip_address.h:301
ESPHOME_ALWAYS_INLINE bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
Definition util.h:28
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