Skip to content
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

Some example code for Network interface #10905

Closed
1 task done
CelliesProjects opened this issue Jan 26, 2025 · 5 comments
Closed
1 task done

Some example code for Network interface #10905

CelliesProjects opened this issue Jan 26, 2025 · 5 comments
Labels
Type: Feature request Feature request for Arduino ESP32

Comments

@CelliesProjects
Copy link
Contributor

Related area

Network

Hardware specification

All esp32

Is your feature request related to a problem?

I would like to see some examples for https://github.com/espressif/arduino-esp32/tree/master/libraries/Network

Describe the solution you'd like

Looking at the code I can figure out some stuff, but a couple of examples would be nice.

Describe alternatives you've considered

No response

Additional context

No response

I have checked existing list of Feature requests and the Contribution Guide

  • I confirm I have checked existing list of Feature requests and Contribution Guide.
@CelliesProjects CelliesProjects added the Type: Feature request Feature request for Arduino ESP32 label Jan 26, 2025
@me-no-dev
Copy link
Member

WiFi.STA, WiFi.AP, ETH and PPP all implement the NetworkInterface class. It's the glue between Arduino and LwIP. It's not really usable on it's own. One thing that can be called is the NetworkManager to let's say attach an event listener (because it implements the NetworkEvents) or set the default outgoing interface (STA, ETH or PPP)

@CelliesProjects
Copy link
Contributor Author

CelliesProjects commented Jan 28, 2025

@me-no-dev Thanks for the explanation!

I was reading tah network code because of this code that does not compile since 3.0.x:

inline __attribute__((always_inline)) bool
ESP32_VS1053_Stream::_networkIsActive()
{
    for (int i = TCPIP_ADAPTER_IF_STA; i < TCPIP_ADAPTER_IF_MAX; i++)
        if (tcpip_adapter_is_netif_up((tcpip_adapter_if_t)i))
            return true;
    return false;
}

How would I do this with 3.x.x?

For now I changed this to something like return WiFi.isConnected() but I would like to be able to check all the interfaces.

@me-no-dev
Copy link
Member

Yes please use our new API. The reason why your code does not work is that tcpip_adapter_* API is no longer present in ESP-IDF. It has been replaced by esp_netif APIs and our new network layer is based on that. You can use bool WiFi.STA.connected();, bool WiFi.AP.connected();, bool ETH.connected(); and bool PPP.connected();

@me-no-dev
Copy link
Member

Instead of .connected() you can use .linkUp() if you just want to see if the interface is UP (not necessarily connected)

@CelliesProjects
Copy link
Contributor Author

CelliesProjects commented Jan 28, 2025

After taking another look at NetworkManager i found this inside hostByName()

// This should generally check if we have a global address assigned to one of the interfaces.
// If such address is not assigned, there is no point in trying to get V6 from DNS as we will not be able to reach it.
bool hasGlobalV6Now = false;
bool hasGlobalV4Now = false;
for (int i = 0; i < ESP_NETIF_ID_MAX; ++i) {
NetworkInterface *iface = getNetifByID((Network_Interface_ID)i);
if (iface != NULL) {
if (iface->hasGlobalIPv6()) {
hasGlobalV6Now = true;
}
if (iface->hasIP()) {
hasGlobalV4Now = true;
}
}
if (hasGlobalV6Now && hasGlobalV4Now) {
break;
}
}

Almost exactly what I need!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Feature request Feature request for Arduino ESP32
Projects
None yet
Development

No branches or pull requests

2 participants