Skip to content

feat(netif): Allow setting interface's routing priority #11617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions libraries/Network/src/NetworkInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -606,13 +606,33 @@ int NetworkInterface::impl_index() const {
return esp_netif_get_netif_impl_index(_esp_netif);
}

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

#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 5, 0)
int NetworkInterface::setRoutePrio(int prio) {
if (_esp_netif == NULL) {
return -1;
}
return esp_netif_set_route_prio(_esp_netif, prio);
}
#endif

/**
* This API overrides the automatic configuration of the default interface based on the route_prio
* If the selected netif is set default using this API, no other interface could be set-default disregarding
* its route_prio number (unless the selected netif gets destroyed)
*/
bool NetworkInterface::setDefault() {
if (_esp_netif == NULL) {
return false;
Expand Down Expand Up @@ -819,7 +839,11 @@ size_t NetworkInterface::printTo(Print &out) const {
if (flags & ESP_NETIF_FLAG_MLDV6_REPORT) {
bytes += out.print(",V6_REP");
}
bytes += out.println(")");
bytes += out.print(")");

bytes += out.print(" PRIO: ");
bytes += out.print(getRoutePrio());
bytes += out.println("");
Copy link
Preview

Copilot AI Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Using out.println("") to print a newline is less clear than using out.println() without arguments, which serves the same purpose.

Suggested change
bytes += out.println("");
bytes += out.println();

Copilot uses AI. Check for mistakes.


bytes += out.print(" ");
bytes += out.print("ether ");
Expand Down
5 changes: 4 additions & 1 deletion libraries/Network/src/NetworkInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ class NetworkInterface : public Printable {
const char *desc() const;
String impl_name() const;
int impl_index() const;
int route_prio() const;
int getRoutePrio() const;
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 5, 0)
int setRoutePrio(int prio);
#endif
bool setDefault();
bool isDefault() const;

Expand Down
Loading