Skip to content

Commit f836ae7

Browse files
mdns: Add API to control custom network interfaces
* Original commit: espressif/esp-idf@b02468d
1 parent e9a1c26 commit f836ae7

File tree

4 files changed

+46
-17
lines changed

4 files changed

+46
-17
lines changed

components/mdns/include/mdns.h

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -720,9 +720,43 @@ esp_err_t mdns_query_aaaa(const char * host_name, uint32_t timeout, esp_ip6_addr
720720
#endif
721721

722722

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);
723+
/**
724+
* @brief Register custom esp_netif with mDNS functionality
725+
* mDNS service runs by on default interfaces (STA, AP, ETH). This API enables running the service
726+
* on any customized interface, either using standard WiFi or Ethernet driver or any kind of user
727+
* defined driver.
728+
*
729+
* @param esp_netif Pointer to esp-netif interface
730+
* @return
731+
* - ESP_OK success
732+
* - ESP_ERR_INVALID_STATE mDNS is not running or this netif is already registered
733+
* - ESP_ERR_NO_MEM not enough memory for this in interface in the netif list (see CONFIG_MDNS_MAX_INTERFACES)
734+
*/
735+
esp_err_t mdns_register_esp_netif(esp_netif_t *esp_netif);
736+
737+
/**
738+
* @brief Unregister esp-netif already registered in mDNS service
739+
*
740+
* @param esp_netif Pointer to esp-netif interface
741+
* @return
742+
* - ESP_OK success
743+
* - ESP_ERR_INVALID_STATE mDNS is not running
744+
* - ESP_ERR_NOT_FOUND this esp-netif was not registered in mDNS service
745+
*/
746+
esp_err_t mdns_unregister_esp_netif(esp_netif_t *esp_netif);
747+
748+
/**
749+
* @brief Set esp_netif to a desired state, or perform a desired action, such as enable/disable this interface
750+
* or send announcement packets to this netif
751+
* @param esp_netif Pointer to esp-netif interface
752+
* @param event_action Disable/Enable/Announce on this interface over IPv4/IPv6 protocol.
753+
* Actions enumerated in mdns_event_actions_t type.
754+
* @return
755+
* - ESP_OK success
756+
* - ESP_ERR_INVALID_STATE mDNS is not running or this netif is not registered
757+
* - ESP_ERR_NO_MEM memory error
758+
*/
759+
esp_err_t mdns_set_esp_netif_action(esp_netif_t *esp_netif, mdns_event_actions_t event_action);
726760

727761
#ifdef __cplusplus
728762
}

components/mdns/mdns.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3824,7 +3824,7 @@ void _mdns_disable_pcb(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
38243824
*/
38253825
static void perform_event_action(mdns_if_t mdns_if, mdns_event_actions_t action)
38263826
{
3827-
if (!_mdns_server || mdns_if > MDNS_MAX_INTERFACES) {
3827+
if (!_mdns_server || mdns_if >= MDNS_MAX_INTERFACES) {
38283828
return;
38293829
}
38303830
if (action & MDNS_EVENT_ENABLE_IP4) {
@@ -5028,7 +5028,7 @@ static esp_err_t _mdns_service_task_stop(void)
50285028
static esp_err_t mdns_post_custom_action_tcpip_if(mdns_if_t mdns_if, mdns_event_actions_t event_action)
50295029
{
50305030
if (!_mdns_server || mdns_if >= MDNS_MAX_INTERFACES) {
5031-
return ESP_FAIL;
5031+
return ESP_ERR_INVALID_STATE;
50325032
}
50335033

50345034
mdns_action_t * action = (mdns_action_t *)calloc(1, sizeof(mdns_action_t));
@@ -5081,12 +5081,12 @@ static inline void unregister_predefined_handlers(void)
50815081
* Public Methods
50825082
* */
50835083

5084-
esp_err_t mdns_post_custom_action(esp_netif_t *esp_netif, mdns_event_actions_t event_action)
5084+
esp_err_t mdns_set_esp_netif_action(esp_netif_t *esp_netif, mdns_event_actions_t event_action)
50855085
{
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)
5089+
esp_err_t mdns_register_esp_netif(esp_netif_t *esp_netif)
50905090
{
50915091
if (!_mdns_server) {
50925092
return ESP_ERR_INVALID_STATE;
@@ -5112,7 +5112,7 @@ esp_err_t mdns_add_custom_netif(esp_netif_t *esp_netif)
51125112
return err;
51135113
}
51145114

5115-
esp_err_t mdns_delete_custom_netif(esp_netif_t *esp_netif)
5115+
esp_err_t mdns_unregister_esp_netif(esp_netif_t *esp_netif)
51165116
{
51175117
if (!_mdns_server) {
51185118
return ESP_ERR_INVALID_STATE;

components/mdns/mdns_console.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,13 @@
1111

1212
static const char * ip_protocol_str[] = {"V4", "V6", "MAX"};
1313

14-
static const char * if_str(esp_netif_t *netif)
15-
{
16-
return esp_netif_get_ifkey(netif);
17-
}
18-
1914
static void mdns_print_results(mdns_result_t * results)
2015
{
2116
mdns_result_t * r = results;
2217
mdns_ip_addr_t * a = NULL;
2318
int i = 1;
2419
while (r) {
25-
printf("%d: Interface: %s, Type: %s\n", i++, if_str(r->esp_netif), ip_protocol_str[r->ip_protocol]);
20+
printf("%d: Interface: %s, Type: %s\n", i++, esp_netif_get_ifkey(r->esp_netif), ip_protocol_str[r->ip_protocol]);
2621
if (r->instance_name) {
2722
printf(" PTR : %s\n", r->instance_name);
2823
}

examples/protocols/mdns/main/mdns_example_main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,9 @@ void app_main(void)
270270
/* Demonstration of adding a custom netif to mdns service, but we're adding the default example one,
271271
* so we must disable all predefined interfaces (PREDEF_NETIF_STA, AP and ETH) first
272272
*/
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));
273+
ESP_ERROR_CHECK(mdns_register_esp_netif(EXAMPLE_INTERFACE));
274+
ESP_ERROR_CHECK(mdns_set_esp_netif_action(EXAMPLE_INTERFACE, MDNS_EVENT_ENABLE_IP4));
275+
ESP_ERROR_CHECK(mdns_set_esp_netif_action(EXAMPLE_INTERFACE, MDNS_EVENT_ANNOUNCE_IP4));
276276
#endif
277277
initialise_button();
278278
xTaskCreate(&mdns_example_task, "mdns_example_task", 2048, NULL, 5, NULL);

0 commit comments

Comments
 (0)