Page 1 of 1

How to catch LWIP TCP/IP Events

Posted: Fri Mar 27, 2020 1:15 pm
by saif_iot
I use the official sample LWIP / TCP / TLS example:
https://github.com/espressif/esp-idf/tr ... tcp_client
https://github.com/espressif/esp-idf/tr ... ps_mbedtls

All work fine, so we need catch WiFi events -> ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &MyEventFunctionCallback, NULL));
If WiFi Connected/Disconnect -> if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)...

What about Socket Connect/Disconnect events ?
If we create a persistent socket (long during connection), we get no event if the server close the socket for example!.

Please, How to catch Socket Connect/Disconnect events ?

Thank you,

Re: How to catch LWIP TCP/IP Events

Posted: Fri Mar 27, 2020 2:22 pm
by ESP_Sprite
There are no such events. You can notice that your socket is closed because recv() will return with a return value of 0; send() will return an error if the socket is entirely lost on the other end.

Re: How to catch LWIP TCP/IP Events

Posted: Mon Mar 30, 2020 1:32 pm
by saif_iot
I got it, I was thinking to use a scheduled task to check the return of send(), but this will create conflicts in our server, so maybe re-connect if send() return error will be a better solution.

Thank you so much ESP_Sprite.
Sincerely,

Re: How to catch LWIP TCP/IP Events

Posted: Tue Mar 31, 2020 11:07 am
by jollayc
CONFIG_LWIP_TCP_KEEP_CONNECTION_WHEN_IP_CHANGES is disabled by default. This disables the default lwIP behaviour of keeping TCP connections open if an interface IP changes, in case the interface IP changes back (for example, if an interface connection goes down and comes back up). Enable this option to keep TCP connections open in this case, until they time out normally. This may increase the number of sockets in use if a network interface goes down temporarily.

Some common lwIP “app” APIs are supported indirectly by ESP-IDF:

DHCP Server & Client are supported indirectly via the ESP-NETIF functionality

Simple Network Time Protocol (SNTP) is supported via the lwip/include/apps/sntp/sntp.h lwip/lwip/src/include/lwip/apps/sntp.h functions (DQ Feedback see also SNTP Time Synchronization)

ICMP Ping is supported using a variation on the lwIP ping API. See ICMP Echo.

NetBIOS lookup is available using the standard lwIP API. protocols/http_server/restful_server has an option to demonstrate using NetBIOS to look up a host on the LAN.

mDNS uses a different implementation to the lwIP default mDNS (see mDNS Service), but lwIP can look up mDNS hosts using standard APIs such as gethostbyname() and the convention hostname.local, provided the CONFIG_LWIP_DNS_SUPPORT_MDNS_QUERIES setting is enabled.

Re: How to catch LWIP TCP/IP Events

Posted: Tue Mar 31, 2020 2:07 pm
by saif_iot
Thank you Jollayc for those information, I will use CONFIG_LWIP_TCP_KEEP_CONNECTION_WHEN_IP_CHANGES to reduce losing socket connection's.