Skip to content

Commit 4a3c6d7

Browse files
authored
feat(netif): Allow setting interface's routing priority (#11617)
* feat(netif): Allow setting interface's routing priority * feat(netif): Rename route priority method names and add notes * feat(netif): Print route prio for each interface
1 parent 3ad17de commit 4a3c6d7

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

libraries/Network/src/NetworkInterface.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,13 +606,33 @@ int NetworkInterface::impl_index() const {
606606
return esp_netif_get_netif_impl_index(_esp_netif);
607607
}
608608

609-
int NetworkInterface::route_prio() const {
609+
/**
610+
* Every netif has a parameter named route_prio, you can refer to file esp_netif_defaults.h.
611+
* A higher value of route_prio indicates a higher priority.
612+
* The active interface with highest priority will be used for default route (gateway).
613+
* Defaults are: STA=100, BR=70, ETH=50, PPP=20, AP/NAN=10
614+
*/
615+
int NetworkInterface::getRoutePrio() const {
610616
if (_esp_netif == NULL) {
611617
return -1;
612618
}
613619
return esp_netif_get_route_prio(_esp_netif);
614620
}
615621

622+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 5, 0)
623+
int NetworkInterface::setRoutePrio(int prio) {
624+
if (_esp_netif == NULL) {
625+
return -1;
626+
}
627+
return esp_netif_set_route_prio(_esp_netif, prio);
628+
}
629+
#endif
630+
631+
/**
632+
* This API overrides the automatic configuration of the default interface based on the route_prio
633+
* If the selected netif is set default using this API, no other interface could be set-default disregarding
634+
* its route_prio number (unless the selected netif gets destroyed)
635+
*/
616636
bool NetworkInterface::setDefault() {
617637
if (_esp_netif == NULL) {
618638
return false;
@@ -819,7 +839,11 @@ size_t NetworkInterface::printTo(Print &out) const {
819839
if (flags & ESP_NETIF_FLAG_MLDV6_REPORT) {
820840
bytes += out.print(",V6_REP");
821841
}
822-
bytes += out.println(")");
842+
bytes += out.print(")");
843+
844+
bytes += out.print(" PRIO: ");
845+
bytes += out.print(getRoutePrio());
846+
bytes += out.println("");
823847

824848
bytes += out.print(" ");
825849
bytes += out.print("ether ");

libraries/Network/src/NetworkInterface.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ class NetworkInterface : public Printable {
5757
const char *desc() const;
5858
String impl_name() const;
5959
int impl_index() const;
60-
int route_prio() const;
60+
int getRoutePrio() const;
61+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 5, 0)
62+
int setRoutePrio(int prio);
63+
#endif
6164
bool setDefault();
6265
bool isDefault() const;
6366

0 commit comments

Comments
 (0)