Skip to content

Commit 3df1030

Browse files
committed
feat(net): Add support for selecting the default network interface
1 parent cff2a18 commit 3df1030

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

Diff for: libraries/Network/src/NetworkInterface.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66
#include "NetworkInterface.h"
7+
#include "NetworkManager.h"
78
#include "esp_netif.h"
89
#include "esp_netif_defaults.h"
910
#include "esp_system.h"
@@ -538,6 +539,43 @@ String NetworkInterface::impl_name(void) const
538539
return String(netif_name);
539540
}
540541

542+
int NetworkInterface::impl_index() const
543+
{
544+
if(_esp_netif == NULL){
545+
return -1;
546+
}
547+
return esp_netif_get_netif_impl_index(_esp_netif);
548+
}
549+
550+
int NetworkInterface::route_prio() const
551+
{
552+
if(_esp_netif == NULL){
553+
return -1;
554+
}
555+
return esp_netif_get_route_prio(_esp_netif);
556+
}
557+
558+
bool NetworkInterface::setDefault()
559+
{
560+
if(_esp_netif == NULL){
561+
return false;
562+
}
563+
esp_err_t err = esp_netif_set_default_netif(_esp_netif);
564+
if(err != ESP_OK){
565+
log_e("Failed to set default netif: %d", err);
566+
return false;
567+
}
568+
return true;
569+
}
570+
571+
bool NetworkInterface::isDefault() const
572+
{
573+
if(_esp_netif == NULL){
574+
return false;
575+
}
576+
return esp_netif_get_default_netif() == _esp_netif;
577+
}
578+
541579
uint8_t * NetworkInterface::macAddress(uint8_t* mac) const
542580
{
543581
if(!mac || _esp_netif == NULL){

Diff for: libraries/Network/src/NetworkInterface.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include "esp_netif_types.h"
99
#include "esp_event.h"
1010
#include "Arduino.h"
11-
#include "NetworkManager.h"
1211
#include "Printable.h"
1312

1413
typedef enum {
@@ -54,6 +53,10 @@ class NetworkInterface: public Printable {
5453
const char * ifkey() const;
5554
const char * desc() const;
5655
String impl_name() const;
56+
int impl_index() const;
57+
int route_prio() const;
58+
bool setDefault();
59+
bool isDefault() const;
5760

5861
uint8_t * macAddress(uint8_t* mac) const;
5962
String macAddress() const;

Diff for: libraries/Network/src/NetworkManager.cpp

+17-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66
#include "NetworkManager.h"
7-
#include "NetworkInterface.h"
87
#include "IPAddress.h"
98
#include "esp_netif.h"
109
#include "lwip/dns.h"
@@ -133,6 +132,23 @@ bool NetworkManager::setHostname(const char * name)
133132

134133
NetworkInterface * getNetifByID(Network_Interface_ID id);
135134

135+
bool NetworkManager::setDefaultInterface(NetworkInterface & ifc)
136+
{
137+
return ifc.setDefault();
138+
}
139+
140+
NetworkInterface * NetworkManager::getDefaultInterface()
141+
{
142+
esp_netif_t * netif = esp_netif_get_default_netif();
143+
for (int i = 0; i < ESP_NETIF_ID_MAX; ++i){
144+
NetworkInterface * iface = getNetifByID((Network_Interface_ID)i);
145+
if(iface != NULL && iface->netif() == netif){
146+
return iface;
147+
}
148+
}
149+
return NULL;
150+
}
151+
136152
size_t NetworkManager::printTo(Print & out) const {
137153
size_t bytes = 0;
138154

Diff for: libraries/Network/src/NetworkManager.h

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#pragma once
77

88
#include "NetworkEvents.h"
9+
#include "NetworkInterface.h"
910
#include "IPAddress.h"
1011
#include "WString.h"
1112

@@ -18,6 +19,9 @@ class NetworkManager : public NetworkEvents, public Printable {
1819
uint8_t * macAddress(uint8_t * mac);
1920
String macAddress();
2021

22+
bool setDefaultInterface(NetworkInterface & ifc);
23+
NetworkInterface * getDefaultInterface();
24+
2125
size_t printTo(Print & out) const;
2226

2327
static const char * getHostname();

0 commit comments

Comments
 (0)