Skip to content

Commit 30f37c0

Browse files
mdns: Add support for registering custom netif
* Original commit: espressif/esp-idf@bec42ff
1 parent ddc58e8 commit 30f37c0

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

components/mdns/include/mdns.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,11 @@ esp_err_t mdns_query_a(const char * host_name, uint32_t timeout, esp_ip4_addr_t
719719
esp_err_t mdns_query_aaaa(const char * host_name, uint32_t timeout, esp_ip6_addr_t * addr);
720720
#endif
721721

722+
723+
esp_err_t mdns_add_custom_netif(esp_netif_t *esp_netif);
724+
esp_err_t mdns_post_custom_action(esp_netif_t *esp_netif, mdns_event_actions_t event_action);
725+
esp_err_t mdns_delete_custom_netif(esp_netif_t *esp_netif);
726+
722727
#ifdef __cplusplus
723728
}
724729
#endif

components/mdns/mdns.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5086,6 +5086,52 @@ esp_err_t mdns_post_custom_action(esp_netif_t *esp_netif, mdns_event_actions_t e
50865086
return mdns_post_custom_action_tcpip_if(_mdns_get_if_from_esp_netif(esp_netif), event_action);
50875087
}
50885088

5089+
esp_err_t mdns_add_custom_netif(esp_netif_t *esp_netif)
5090+
{
5091+
if (!_mdns_server) {
5092+
return ESP_ERR_INVALID_STATE;
5093+
}
5094+
5095+
esp_err_t err = ESP_ERR_NO_MEM;
5096+
MDNS_SERVICE_LOCK();
5097+
for (mdns_if_t i=0; i<MDNS_MAX_INTERFACES; ++i) {
5098+
if (s_esp_netifs[i].netif == esp_netif) {
5099+
MDNS_SERVICE_UNLOCK();
5100+
return ESP_ERR_INVALID_STATE;
5101+
}
5102+
}
5103+
5104+
for (mdns_if_t i=0; i<MDNS_MAX_INTERFACES; ++i) {
5105+
if (!s_esp_netifs[i].predefined && s_esp_netifs[i].netif == NULL) {
5106+
s_esp_netifs[i].netif = esp_netif;
5107+
err = ESP_OK;
5108+
break;
5109+
}
5110+
}
5111+
MDNS_SERVICE_UNLOCK();
5112+
return err;
5113+
}
5114+
5115+
esp_err_t mdns_delete_custom_netif(esp_netif_t *esp_netif)
5116+
{
5117+
if (!_mdns_server) {
5118+
return ESP_ERR_INVALID_STATE;
5119+
}
5120+
5121+
esp_err_t err = ESP_ERR_NOT_FOUND;
5122+
MDNS_SERVICE_LOCK();
5123+
for (mdns_if_t i=0; i<MDNS_MAX_INTERFACES; ++i) {
5124+
if (!s_esp_netifs[i].predefined && s_esp_netifs[i].netif == esp_netif) {
5125+
s_esp_netifs[i].netif = NULL;
5126+
err = ESP_OK;
5127+
break;
5128+
}
5129+
}
5130+
MDNS_SERVICE_UNLOCK();
5131+
return err;
5132+
}
5133+
5134+
50895135
esp_err_t mdns_init(void)
50905136
{
50915137
esp_err_t err = ESP_OK;
@@ -5133,7 +5179,9 @@ esp_err_t mdns_init(void)
51335179
}
51345180
#endif
51355181

5182+
#if CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP || CONFIG_MDNS_PREDEF_NETIF_ETH
51365183
set_default_duplicated_interfaces();
5184+
#endif
51375185

51385186
uint8_t i;
51395187
#if CONFIG_LWIP_IPV6
@@ -5166,8 +5214,10 @@ esp_err_t mdns_init(void)
51665214
_mdns_disable_pcb(i, MDNS_IP_PROTOCOL_V4);
51675215
s_esp_netifs[i].duplicate = MDNS_MAX_INTERFACES;
51685216
}
5217+
#if CONFIG_MDNS_PREDEF_NETIF_STA || CONFIG_MDNS_PREDEF_NETIF_AP || CONFIG_MDNS_PREDEF_NETIF_ETH
51695218
free_event_handlers:
51705219
unregister_predefined_handlers();
5220+
#endif
51715221
vQueueDelete(_mdns_server->action_queue);
51725222
free_lock:
51735223
vSemaphoreDelete(_mdns_server->lock);

examples/protocols/mdns/main/Kconfig.projbuild

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,13 @@ menu "Example Configuration"
4848
help
4949
Set the GPIO number used as mDNS test button
5050

51+
config MDNS_ADD_CUSTOM_NETIF
52+
bool "Add user netif to mdns service"
53+
default n
54+
help
55+
If enabled, we try to add a custom netif to mdns service.
56+
Note that for using with common connection example code, we have to disable
57+
all predefined interfaces in mdns component setup (since we're adding one
58+
of the default interfaces)
59+
5160
endmenu

examples/protocols/mdns/main/mdns_example_main.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,14 @@ void app_main(void)
266266
*/
267267
ESP_ERROR_CHECK(example_connect());
268268

269+
#if defined(CONFIG_MDNS_ADD_CUSTOM_NETIF) && !defined(CONFIG_MDNS_PREDEF_NETIF_STA) && !defined(CONFIG_MDNS_PREDEF_NETIF_ETH)
270+
/* Demonstration of adding a custom netif to mdns service, but we're adding the default example one,
271+
* so we must disable all predefined interfaces (PREDEF_NETIF_STA, AP and ETH) first
272+
*/
273+
ESP_ERROR_CHECK(mdns_add_custom_netif(EXAMPLE_INTERFACE));
274+
ESP_ERROR_CHECK(mdns_post_custom_action(EXAMPLE_INTERFACE, MDNS_EVENT_ENABLE_IP4));
275+
ESP_ERROR_CHECK(mdns_post_custom_action(EXAMPLE_INTERFACE, MDNS_EVENT_ANNOUNCE_IP4));
276+
#endif
269277
initialise_button();
270278
xTaskCreate(&mdns_example_task, "mdns_example_task", 2048, NULL, 5, NULL);
271279
}

0 commit comments

Comments
 (0)