ESPHome 2026.11.0-dev
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
14#endif
15#ifdef USE_WIFI
17#endif
18#ifdef USE_OPENTHREAD
20#endif
21#ifdef USE_ESP32
22#include "esp_netif.h"
23#endif
24
25namespace esphome::network {
26
27#if defined(USE_ESP32) && USE_NETWORK_IPV6
29void fill_ip6_addresses(esp_netif_t *netif, IPAddresses &addresses);
30#endif
31
32// The order of the components is important: WiFi should come after any possible main interfaces (it may be used as
33// an AP that uses a previous interface for NAT).
34
36ESPHOME_ALWAYS_INLINE inline bool is_connected() {
37 // With a single interface enabled the checks below collapse to `if (x) return true; return false;`, which
38 // clang-tidy wants folded into one return. Keep the per-interface form so every enabled interface is checked.
39 // NOLINTBEGIN(readability-simplify-boolean-expr)
40#ifdef USE_ETHERNET
42 return true;
43#endif
44
45#ifdef USE_MODEM
47 return true;
48#endif
49
50#ifdef USE_WIFI
52 return true;
53#endif
54
55#if defined(USE_OPENTHREAD) && !defined(USE_OPENTHREAD_BORDER_ROUTER)
56 // A border router is reached over its Wi-Fi backbone, so the Thread link alone does not count
58 return true;
59#endif
60
61#ifdef USE_HOST
62 return true; // Assume it's connected
63#endif
64 return false;
65 // NOLINTEND(readability-simplify-boolean-expr)
66}
67
70bool is_disabled();
72static constexpr size_t USE_ADDRESS_BUFFER_SIZE = 70;
77const char *get_use_address_to(std::span<char, USE_ADDRESS_BUFFER_SIZE> buf);
79
80} // namespace esphome::network
81#endif
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_ALWAYS_INLINE bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
Definition util.h:36
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
struct esp_netif_obj esp_netif_t