From 3df1030be4ba973a348a7c48b620fdc783be7459 Mon Sep 17 00:00:00 2001 From: me-no-dev Date: Fri, 5 Apr 2024 14:32:26 +0300 Subject: [PATCH] feat(net): Add support for selecting the default network interface --- libraries/Network/src/NetworkInterface.cpp | 38 ++++++++++++++++++++++ libraries/Network/src/NetworkInterface.h | 5 ++- libraries/Network/src/NetworkManager.cpp | 18 +++++++++- libraries/Network/src/NetworkManager.h | 4 +++ 4 files changed, 63 insertions(+), 2 deletions(-) diff --git a/libraries/Network/src/NetworkInterface.cpp b/libraries/Network/src/NetworkInterface.cpp index 1865246cf76..564e9ac4a63 100644 --- a/libraries/Network/src/NetworkInterface.cpp +++ b/libraries/Network/src/NetworkInterface.cpp @@ -4,6 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ #include "NetworkInterface.h" +#include "NetworkManager.h" #include "esp_netif.h" #include "esp_netif_defaults.h" #include "esp_system.h" @@ -538,6 +539,43 @@ String NetworkInterface::impl_name(void) const return String(netif_name); } +int NetworkInterface::impl_index() const +{ + if(_esp_netif == NULL){ + return -1; + } + return esp_netif_get_netif_impl_index(_esp_netif); +} + +int NetworkInterface::route_prio() const +{ + if(_esp_netif == NULL){ + return -1; + } + return esp_netif_get_route_prio(_esp_netif); +} + +bool NetworkInterface::setDefault() +{ + if(_esp_netif == NULL){ + return false; + } + esp_err_t err = esp_netif_set_default_netif(_esp_netif); + if(err != ESP_OK){ + log_e("Failed to set default netif: %d", err); + return false; + } + return true; +} + +bool NetworkInterface::isDefault() const +{ + if(_esp_netif == NULL){ + return false; + } + return esp_netif_get_default_netif() == _esp_netif; +} + uint8_t * NetworkInterface::macAddress(uint8_t* mac) const { if(!mac || _esp_netif == NULL){ diff --git a/libraries/Network/src/NetworkInterface.h b/libraries/Network/src/NetworkInterface.h index 462c77fe3ec..5a79f82596f 100644 --- a/libraries/Network/src/NetworkInterface.h +++ b/libraries/Network/src/NetworkInterface.h @@ -8,7 +8,6 @@ #include "esp_netif_types.h" #include "esp_event.h" #include "Arduino.h" -#include "NetworkManager.h" #include "Printable.h" typedef enum { @@ -54,6 +53,10 @@ class NetworkInterface: public Printable { const char * ifkey() const; const char * desc() const; String impl_name() const; + int impl_index() const; + int route_prio() const; + bool setDefault(); + bool isDefault() const; uint8_t * macAddress(uint8_t* mac) const; String macAddress() const; diff --git a/libraries/Network/src/NetworkManager.cpp b/libraries/Network/src/NetworkManager.cpp index 5642ae30967..580db99166d 100644 --- a/libraries/Network/src/NetworkManager.cpp +++ b/libraries/Network/src/NetworkManager.cpp @@ -4,7 +4,6 @@ * SPDX-License-Identifier: Apache-2.0 */ #include "NetworkManager.h" -#include "NetworkInterface.h" #include "IPAddress.h" #include "esp_netif.h" #include "lwip/dns.h" @@ -133,6 +132,23 @@ bool NetworkManager::setHostname(const char * name) NetworkInterface * getNetifByID(Network_Interface_ID id); +bool NetworkManager::setDefaultInterface(NetworkInterface & ifc) +{ + return ifc.setDefault(); +} + +NetworkInterface * NetworkManager::getDefaultInterface() +{ + esp_netif_t * netif = esp_netif_get_default_netif(); + for (int i = 0; i < ESP_NETIF_ID_MAX; ++i){ + NetworkInterface * iface = getNetifByID((Network_Interface_ID)i); + if(iface != NULL && iface->netif() == netif){ + return iface; + } + } + return NULL; +} + size_t NetworkManager::printTo(Print & out) const { size_t bytes = 0; diff --git a/libraries/Network/src/NetworkManager.h b/libraries/Network/src/NetworkManager.h index fd783d61a34..10f42aab25a 100644 --- a/libraries/Network/src/NetworkManager.h +++ b/libraries/Network/src/NetworkManager.h @@ -6,6 +6,7 @@ #pragma once #include "NetworkEvents.h" +#include "NetworkInterface.h" #include "IPAddress.h" #include "WString.h" @@ -18,6 +19,9 @@ class NetworkManager : public NetworkEvents, public Printable { uint8_t * macAddress(uint8_t * mac); String macAddress(); + bool setDefaultInterface(NetworkInterface & ifc); + NetworkInterface * getDefaultInterface(); + size_t printTo(Print & out) const; static const char * getHostname();