From 30f37c05655e07a0678bfb5c9622bf781723f6b8 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 14 Dec 2021 18:07:23 +0100 Subject: [PATCH] mdns: Add support for registering custom netif * Original commit: espressif/esp-idf@bec42ff85d5091d71e1cb1063bea20d7c6ac8c76 --- components/mdns/include/mdns.h | 5 ++ components/mdns/mdns.c | 50 +++++++++++++++++++ .../protocols/mdns/main/Kconfig.projbuild | 9 ++++ .../protocols/mdns/main/mdns_example_main.c | 8 +++ 4 files changed, 72 insertions(+) diff --git a/components/mdns/include/mdns.h b/components/mdns/include/mdns.h index ab4e97ac3..58121135a 100644 --- a/components/mdns/include/mdns.h +++ b/components/mdns/include/mdns.h @@ -719,6 +719,11 @@ esp_err_t mdns_query_a(const char * host_name, uint32_t timeout, esp_ip4_addr_t esp_err_t mdns_query_aaaa(const char * host_name, uint32_t timeout, esp_ip6_addr_t * addr); #endif + +esp_err_t mdns_add_custom_netif(esp_netif_t *esp_netif); +esp_err_t mdns_post_custom_action(esp_netif_t *esp_netif, mdns_event_actions_t event_action); +esp_err_t mdns_delete_custom_netif(esp_netif_t *esp_netif); + #ifdef __cplusplus } #endif diff --git a/components/mdns/mdns.c b/components/mdns/mdns.c index 50996cf7f..519365397 100644 --- a/components/mdns/mdns.c +++ b/components/mdns/mdns.c @@ -5086,6 +5086,52 @@ esp_err_t mdns_post_custom_action(esp_netif_t *esp_netif, mdns_event_actions_t e return mdns_post_custom_action_tcpip_if(_mdns_get_if_from_esp_netif(esp_netif), event_action); } +esp_err_t mdns_add_custom_netif(esp_netif_t *esp_netif) +{ + if (!_mdns_server) { + return ESP_ERR_INVALID_STATE; + } + + esp_err_t err = ESP_ERR_NO_MEM; + MDNS_SERVICE_LOCK(); + for (mdns_if_t i=0; iaction_queue); free_lock: vSemaphoreDelete(_mdns_server->lock); diff --git a/examples/protocols/mdns/main/Kconfig.projbuild b/examples/protocols/mdns/main/Kconfig.projbuild index bc4b1a055..b66e6f867 100644 --- a/examples/protocols/mdns/main/Kconfig.projbuild +++ b/examples/protocols/mdns/main/Kconfig.projbuild @@ -48,4 +48,13 @@ menu "Example Configuration" help Set the GPIO number used as mDNS test button + config MDNS_ADD_CUSTOM_NETIF + bool "Add user netif to mdns service" + default n + help + If enabled, we try to add a custom netif to mdns service. + Note that for using with common connection example code, we have to disable + all predefined interfaces in mdns component setup (since we're adding one + of the default interfaces) + endmenu diff --git a/examples/protocols/mdns/main/mdns_example_main.c b/examples/protocols/mdns/main/mdns_example_main.c index 5a90b5165..c1a9ba868 100644 --- a/examples/protocols/mdns/main/mdns_example_main.c +++ b/examples/protocols/mdns/main/mdns_example_main.c @@ -266,6 +266,14 @@ void app_main(void) */ ESP_ERROR_CHECK(example_connect()); +#if defined(CONFIG_MDNS_ADD_CUSTOM_NETIF) && !defined(CONFIG_MDNS_PREDEF_NETIF_STA) && !defined(CONFIG_MDNS_PREDEF_NETIF_ETH) + /* Demonstration of adding a custom netif to mdns service, but we're adding the default example one, + * so we must disable all predefined interfaces (PREDEF_NETIF_STA, AP and ETH) first + */ + ESP_ERROR_CHECK(mdns_add_custom_netif(EXAMPLE_INTERFACE)); + ESP_ERROR_CHECK(mdns_post_custom_action(EXAMPLE_INTERFACE, MDNS_EVENT_ENABLE_IP4)); + ESP_ERROR_CHECK(mdns_post_custom_action(EXAMPLE_INTERFACE, MDNS_EVENT_ANNOUNCE_IP4)); +#endif initialise_button(); xTaskCreate(&mdns_example_task, "mdns_example_task", 2048, NULL, 5, NULL); }