Skip to content

Commit e6b35e3

Browse files
committed
feat(net): Add method Network.isOnline()
1 parent 420adce commit e6b35e3

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

libraries/Network/src/NetworkManager.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,18 @@ NetworkInterface *NetworkManager::getDefaultInterface() {
185185
return NULL;
186186
}
187187

188+
bool NetworkManager::isOnline() {
189+
for (int i = 0; i < ESP_NETIF_ID_MAX; ++i) {
190+
if (i != ESP_NETIF_ID_AP) {
191+
NetworkInterface *iface = getNetifByID((Network_Interface_ID)i);
192+
if (iface != NULL && (iface->hasIp() || iface->hasGlobalIPv6())) {
193+
return true;
194+
}
195+
}
196+
}
197+
return false;
198+
}
199+
188200
size_t NetworkManager::printTo(Print &out) const {
189201
size_t bytes = 0;
190202

libraries/Network/src/NetworkManager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class NetworkManager : public NetworkEvents, public Printable {
2222
bool setDefaultInterface(NetworkInterface &ifc);
2323
NetworkInterface *getDefaultInterface();
2424

25+
// Returns true if any interface (except AP) has assigned IPv4 or global IPv6
26+
bool isOnline();
27+
2528
size_t printTo(Print &out) const;
2629

2730
static const char *getHostname();

0 commit comments

Comments
 (0)