From 62fd7276b4fca8d178c9f546d947d4fa51ecb4e7 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 24 Sep 2025 12:19:14 +0200 Subject: [PATCH] feat(esp_netif): Add support for custom got-ip/lost-ip events --- .../esp_netif/include/esp_netif_types.h | 2 ++ .../network/esp_netif_programming.rst | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/components/esp_netif/include/esp_netif_types.h b/components/esp_netif/include/esp_netif_types.h index 7205eedcc3..a39f081ba4 100644 --- a/components/esp_netif/include/esp_netif_types.h +++ b/components/esp_netif/include/esp_netif_types.h @@ -111,6 +111,8 @@ typedef enum { IP_EVENT_TX_RX, /*!< transmitting/receiving data packet */ IP_EVENT_NETIF_UP, /*!< unified netif status: interface became up */ IP_EVENT_NETIF_DOWN, /*!< unified netif status: interface went down */ + IP_EVENT_CUSTOM_GOT_IP, /*!< custom netif got IP (for user-defined interfaces) */ + IP_EVENT_CUSTOM_LOST_IP, /*!< custom netif lost IP (for user-defined interfaces) */ } ip_event_t; /** @brief IP event base declaration */ diff --git a/docs/en/api-reference/network/esp_netif_programming.rst b/docs/en/api-reference/network/esp_netif_programming.rst index a07bf12750..136acb7d0e 100644 --- a/docs/en/api-reference/network/esp_netif_programming.rst +++ b/docs/en/api-reference/network/esp_netif_programming.rst @@ -15,6 +15,26 @@ In order to configure static IP addresses and DNS servers, it's necessary to dis To set IPv4 address, you can use :cpp:func:`esp_netif_set_ip_info()`. For IPv6, these two functions can be used for adding or removing addresses: :cpp:func:`esp_netif_add_ip6_address()`, :cpp:func:`esp_netif_remove_ip6_address()`. To configure DNS servers, please use :cpp:func:`esp_netif_set_dns_info()` API. +Custom Got/Lost IP Events +^^^^^^^^^^^^^^^^^^^^^^^^^ + +For user-defined interfaces, esp-netif lets you select which IP events are posted when an interface obtains or loses an IP address. Two generic event IDs are provided for this purpose: + +- ``IP_EVENT_CUSTOM_GOT_IP`` +- ``IP_EVENT_CUSTOM_LOST_IP`` + +Configure these by setting :cpp:member:`esp_netif_inherent_config_t::get_ip_event` and :cpp:member:`esp_netif_inherent_config_t::lost_ip_event` when creating the interface: + +.. code-block:: c + + esp_netif_inherent_config_t base = ESP_NETIF_INHERENT_DEFAULT_WIFI_STA(); + base.get_ip_event = IP_EVENT_CUSTOM_GOT_IP; + base.lost_ip_event = IP_EVENT_CUSTOM_LOST_IP; + esp_netif_config_t cfg = { .base = &base, .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA }; + esp_netif_t *netif = esp_netif_new(&cfg); + +Event data for these events is the same as the standard ones: :cpp:type:`ip_event_got_ip_t` is posted for “got IP”, and :cpp:type:`ip_event_got_ip_t` with :cpp:member:`ip_event_got_ip_t::ip_changed` set accordingly for updates. For lost IP, an empty :cpp:type:`ip_event_got_ip_t` with only :cpp:member:`ip_event_got_ip_t::esp_netif` set is posted. + .. _esp_netif_set_dhcp: Configure DHCP options