mdns: Fix copyright messages, update API descrition

* Impove docs and comments on custom netifs
* Make predef interfaces const, minor docs fixes


* Original commit: espressif/esp-idf@42ba8a8338
This commit is contained in:
David Cermak
2022-01-03 16:36:08 +01:00
committed by suren-gabrielyan-espressif
parent f836ae7f65
commit 2c764b1b7a
6 changed files with 69 additions and 34 deletions

View File

@ -5,7 +5,7 @@ menu "mDNS"
range 1 9 range 1 9
default 3 default 3
help help
Number of network interfaces to be served by the mdns library. Number of network interfaces to be served by the mDNS library.
Lowering this number helps to reduce some static RAM usage. Lowering this number helps to reduce some static RAM usage.
config MDNS_MAX_SERVICES config MDNS_MAX_SERVICES
@ -72,7 +72,7 @@ menu "mDNS"
Currently the only strict feature: Do not repeat original questions in response packets Currently the only strict feature: Do not repeat original questions in response packets
(defined in RFC6762 sec. 6). (defined in RFC6762 sec. 6).
Default configuration is 0, i.e. non-strict mode, since some implementations, Default configuration is 0, i.e. non-strict mode, since some implementations,
such as lwIP mdns resolver (used by standard POSIX API like getaddrinfo, gethostbyname) such as lwIP mDNS resolver (used by standard POSIX API like getaddrinfo, gethostbyname)
could not correctly resolve advertised names. could not correctly resolve advertised names.
config MDNS_TIMER_PERIOD_MS config MDNS_TIMER_PERIOD_MS
@ -84,10 +84,10 @@ menu "mDNS"
and schedules mDNS searches. and schedules mDNS searches.
config MDNS_NETWORKING_SOCKET config MDNS_NETWORKING_SOCKET
bool "Use BSD sockets for mdns networking" bool "Use BSD sockets for mDNS networking"
default n default n
help help
Enables optional mdns networking implementation using BSD sockets Enables optional mDNS networking implementation using BSD sockets
in UDP multicast mode. in UDP multicast mode.
This option creates a new thread to serve receiving packets (TODO). This option creates a new thread to serve receiving packets (TODO).
This option uses additional N sockets, where N is number of interfaces. This option uses additional N sockets, where N is number of interfaces.
@ -104,21 +104,22 @@ menu "mDNS"
bool "Use predefined interface for WiFi Station" bool "Use predefined interface for WiFi Station"
default y default y
help help
Set up mdns for the default WiFi station. Set up mDNS for the default WiFi station.
Disable this option if you do not need mDNS on default WiFi STA. Disable this option if you do not need mDNS on default WiFi STA.
config MDNS_PREDEF_NETIF_AP config MDNS_PREDEF_NETIF_AP
bool "Use predefined interface for WiFi Access Point" bool "Use predefined interface for WiFi Access Point"
default y default y
help help
Set up mdns for the default WiFi Access Point. Set up mDNS for the default WiFi Access Point.
Disable this option if you do not need mDNS on default WiFi AP. Disable this option if you do not need mDNS on default WiFi AP.
config MDNS_PREDEF_NETIF_ETH config MDNS_PREDEF_NETIF_ETH
bool "Use predefined interface for Ethernet" bool "Use predefined interface for Ethernet"
depends on ETH_ENABLED
default y default y
help help
Set up mdns for the default Ethernet interface. Set up mDNS for the default Ethernet interface.
Disable this option if you do not need mDNS on default Ethernet. Disable this option if you do not need mDNS on default Ethernet.
endmenu # MDNS Predefined interfaces endmenu # MDNS Predefined interfaces

View File

@ -722,9 +722,9 @@ esp_err_t mdns_query_aaaa(const char * host_name, uint32_t timeout, esp_ip6_addr
/** /**
* @brief Register custom esp_netif with mDNS functionality * @brief Register custom esp_netif with mDNS functionality
* mDNS service runs by on default interfaces (STA, AP, ETH). This API enables running the service * mDNS service runs by default on preconfigured interfaces (STA, AP, ETH).
* on any customized interface, either using standard WiFi or Ethernet driver or any kind of user * This API enables running the service on any customized interface,
* defined driver. * either using standard WiFi or Ethernet driver or any kind of user defined driver.
* *
* @param esp_netif Pointer to esp-netif interface * @param esp_netif Pointer to esp-netif interface
* @return * @return
@ -732,7 +732,7 @@ esp_err_t mdns_query_aaaa(const char * host_name, uint32_t timeout, esp_ip6_addr
* - ESP_ERR_INVALID_STATE mDNS is not running or this netif is already registered * - ESP_ERR_INVALID_STATE mDNS is not running or this netif is already registered
* - ESP_ERR_NO_MEM not enough memory for this in interface in the netif list (see CONFIG_MDNS_MAX_INTERFACES) * - ESP_ERR_NO_MEM not enough memory for this in interface in the netif list (see CONFIG_MDNS_MAX_INTERFACES)
*/ */
esp_err_t mdns_register_esp_netif(esp_netif_t *esp_netif); esp_err_t mdns_register_netif(esp_netif_t *esp_netif);
/** /**
* @brief Unregister esp-netif already registered in mDNS service * @brief Unregister esp-netif already registered in mDNS service
@ -743,11 +743,19 @@ esp_err_t mdns_register_esp_netif(esp_netif_t *esp_netif);
* - ESP_ERR_INVALID_STATE mDNS is not running * - ESP_ERR_INVALID_STATE mDNS is not running
* - ESP_ERR_NOT_FOUND this esp-netif was not registered in mDNS service * - ESP_ERR_NOT_FOUND this esp-netif was not registered in mDNS service
*/ */
esp_err_t mdns_unregister_esp_netif(esp_netif_t *esp_netif); esp_err_t mdns_unregister_netif(esp_netif_t *esp_netif);
/** /**
* @brief Set esp_netif to a desired state, or perform a desired action, such as enable/disable this interface * @brief Set esp_netif to a desired state, or perform a desired action, such as enable/disable this interface
* or send announcement packets to this netif * or send announcement packets to this netif
*
* * This function is used to enable (probe, resolve conflicts and announce), announce, or disable (send bye) mDNS
* services on the specified network interface.
* * This function must be called if users registers a specific interface using mdns_register_netif()
* to enable mDNS services on that interface.
* * This function could be used in IP/connection event handlers to automatically enable/announce mDNS services
* when network properties change and/or disable them on disconnection.
*
* @param esp_netif Pointer to esp-netif interface * @param esp_netif Pointer to esp-netif interface
* @param event_action Disable/Enable/Announce on this interface over IPv4/IPv6 protocol. * @param event_action Disable/Enable/Announce on this interface over IPv4/IPv6 protocol.
* Actions enumerated in mdns_event_actions_t type. * Actions enumerated in mdns_event_actions_t type.
@ -756,7 +764,7 @@ esp_err_t mdns_unregister_esp_netif(esp_netif_t *esp_netif);
* - ESP_ERR_INVALID_STATE mDNS is not running or this netif is not registered * - ESP_ERR_INVALID_STATE mDNS is not running or this netif is not registered
* - ESP_ERR_NO_MEM memory error * - ESP_ERR_NO_MEM memory error
*/ */
esp_err_t mdns_set_esp_netif_action(esp_netif_t *esp_netif, mdns_event_actions_t event_action); esp_err_t mdns_netif_action(esp_netif_t *esp_netif, mdns_event_actions_t event_action);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -68,9 +68,9 @@ typedef enum {
typedef struct mdns_interfaces mdns_interfaces_t; typedef struct mdns_interfaces mdns_interfaces_t;
struct mdns_interfaces { struct mdns_interfaces {
bool predefined; const bool predefined;
esp_netif_t * netif; esp_netif_t * netif;
mdns_predef_if_t predef_if; const mdns_predef_if_t predef_if;
mdns_if_t duplicate; mdns_if_t duplicate;
}; };
@ -91,10 +91,13 @@ static mdns_interfaces_t s_esp_netifs[MDNS_MAX_INTERFACES] = {
}; };
/* /**
* @brief Convert mdns if to esp-netif handle * @brief Convert Predefined interface to the netif id from the internal netif list
* @param predef_if Predefined interface enum
* @return Ordinal number of internal list of mdns network interface.
* Returns MDNS_MAX_INTERFACES if the predefined interface wasn't found in the list
*/ */
static mdns_if_t mdns_if_from_predef_if(mdns_predef_if_t predef_if) static mdns_if_t mdns_if_from_preset_if(mdns_predef_if_t predef_if)
{ {
for (int i=0; i<MDNS_MAX_INTERFACES; ++i) { for (int i=0; i<MDNS_MAX_INTERFACES; ++i) {
if (s_esp_netifs[i].predefined && s_esp_netifs[i].predef_if == predef_if) { if (s_esp_netifs[i].predefined && s_esp_netifs[i].predef_if == predef_if) {
@ -104,9 +107,14 @@ static mdns_if_t mdns_if_from_predef_if(mdns_predef_if_t predef_if)
return MDNS_MAX_INTERFACES; return MDNS_MAX_INTERFACES;
} }
static inline esp_netif_t *esp_netif_from_preset_if(mdns_predef_if_t preset_if) /**
* @brief Convert Predefined interface to esp-netif handle
* @param predef_if Predefined interface enum
* @return esp_netif pointer from system list of network interfaces
*/
static inline esp_netif_t *esp_netif_from_preset_if(mdns_predef_if_t predef_if)
{ {
switch (preset_if) { switch (predef_if) {
case MDNS_IF_STA: case MDNS_IF_STA:
return esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"); return esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
case MDNS_IF_AP: case MDNS_IF_AP:
@ -120,12 +128,25 @@ static inline esp_netif_t *esp_netif_from_preset_if(mdns_predef_if_t preset_if)
} }
} }
/**
* @brief Gets the actual esp_netif pointer from the internal network interface list
*
* The supplied ordinal number could
* - point to a predef netif -> "STA", "AP", "ETH"
* - if no entry in the list (NULL) -> check if the system added this netif
* - point to a custom netif -> just return the entry in the list
* - users is responsible for the lifetime of this netif (to be valid between mdns-init -> deinit)
*
* @param tcpip_if Ordinal number of the interface
* @return Pointer ot the esp_netif object if the interface is available, NULL otherwise
*/
esp_netif_t *_mdns_get_esp_netif(mdns_if_t tcpip_if) esp_netif_t *_mdns_get_esp_netif(mdns_if_t tcpip_if)
{ {
if (tcpip_if < MDNS_MAX_INTERFACES) { if (tcpip_if < MDNS_MAX_INTERFACES) {
if (s_esp_netifs[tcpip_if].netif == NULL && s_esp_netifs[tcpip_if].predefined) { if (s_esp_netifs[tcpip_if].netif == NULL && s_esp_netifs[tcpip_if].predefined) {
// if a predefined interface face and used local copy is NULL, try to search for the default interface key // If the local copy is NULL and this netif is predefined -> we can find it in the global netif list
s_esp_netifs[tcpip_if].netif = esp_netif_from_preset_if(s_esp_netifs[tcpip_if].predef_if); s_esp_netifs[tcpip_if].netif = esp_netif_from_preset_if(s_esp_netifs[tcpip_if].predef_if);
// failing to find it means that the netif is *not* available -> return NULL
} }
return s_esp_netifs[tcpip_if].netif; return s_esp_netifs[tcpip_if].netif;
} }
@ -146,12 +167,13 @@ static inline void _mdns_clean_netif_ptr(mdns_if_t tcpip_if) {
/* /*
* @brief Convert esp-netif handle to mdns if * @brief Convert esp-netif handle to mdns if
*/ */
static mdns_if_t _mdns_get_if_from_esp_netif(esp_netif_t *interface) static mdns_if_t _mdns_get_if_from_esp_netif(esp_netif_t *esp_netif)
{ {
for (int i=0; i<MDNS_MAX_INTERFACES; ++i) { for (int i=0; i<MDNS_MAX_INTERFACES; ++i) {
if (interface == s_esp_netifs[i].netif) if (esp_netif == s_esp_netifs[i].netif) {
return i; return i;
} }
}
return MDNS_MAX_INTERFACES; return MDNS_MAX_INTERFACES;
} }
@ -3852,17 +3874,17 @@ static void perform_event_action(mdns_if_t mdns_if, mdns_event_actions_t action)
*/ */
static inline void post_mdns_disable_pcb(mdns_predef_if_t preset_if, mdns_ip_protocol_t protocol) static inline void post_mdns_disable_pcb(mdns_predef_if_t preset_if, mdns_ip_protocol_t protocol)
{ {
mdns_post_custom_action_tcpip_if(mdns_if_from_predef_if(preset_if), protocol==MDNS_IP_PROTOCOL_V4 ? MDNS_EVENT_DISABLE_IP4 : MDNS_EVENT_DISABLE_IP6); mdns_post_custom_action_tcpip_if(mdns_if_from_preset_if(preset_if), protocol == MDNS_IP_PROTOCOL_V4 ? MDNS_EVENT_DISABLE_IP4 : MDNS_EVENT_DISABLE_IP6);
} }
static inline void post_mdns_enable_pcb(mdns_predef_if_t preset_if, mdns_ip_protocol_t protocol) static inline void post_mdns_enable_pcb(mdns_predef_if_t preset_if, mdns_ip_protocol_t protocol)
{ {
mdns_post_custom_action_tcpip_if(mdns_if_from_predef_if(preset_if), protocol==MDNS_IP_PROTOCOL_V4 ? MDNS_EVENT_ENABLE_IP4 : MDNS_EVENT_ENABLE_IP6); mdns_post_custom_action_tcpip_if(mdns_if_from_preset_if(preset_if), protocol == MDNS_IP_PROTOCOL_V4 ? MDNS_EVENT_ENABLE_IP4 : MDNS_EVENT_ENABLE_IP6);
} }
static inline void post_mdns_announce_pcb(mdns_predef_if_t preset_if, mdns_ip_protocol_t protocol) static inline void post_mdns_announce_pcb(mdns_predef_if_t preset_if, mdns_ip_protocol_t protocol)
{ {
mdns_post_custom_action_tcpip_if(mdns_if_from_predef_if(preset_if), protocol==MDNS_IP_PROTOCOL_V4 ? MDNS_EVENT_ANNOUNCE_IP4 : MDNS_EVENT_ANNOUNCE_IP6); mdns_post_custom_action_tcpip_if(mdns_if_from_preset_if(preset_if), protocol == MDNS_IP_PROTOCOL_V4 ? MDNS_EVENT_ANNOUNCE_IP4 : MDNS_EVENT_ANNOUNCE_IP6);
} }
void mdns_preset_if_handle_system_event(void *arg, esp_event_base_t event_base, void mdns_preset_if_handle_system_event(void *arg, esp_event_base_t event_base,
@ -5081,12 +5103,12 @@ static inline void unregister_predefined_handlers(void)
* Public Methods * Public Methods
* */ * */
esp_err_t mdns_set_esp_netif_action(esp_netif_t *esp_netif, mdns_event_actions_t event_action) esp_err_t mdns_netif_action(esp_netif_t *esp_netif, mdns_event_actions_t event_action)
{ {
return mdns_post_custom_action_tcpip_if(_mdns_get_if_from_esp_netif(esp_netif), event_action); return mdns_post_custom_action_tcpip_if(_mdns_get_if_from_esp_netif(esp_netif), event_action);
} }
esp_err_t mdns_register_esp_netif(esp_netif_t *esp_netif) esp_err_t mdns_register_netif(esp_netif_t *esp_netif)
{ {
if (!_mdns_server) { if (!_mdns_server) {
return ESP_ERR_INVALID_STATE; return ESP_ERR_INVALID_STATE;
@ -5112,7 +5134,7 @@ esp_err_t mdns_register_esp_netif(esp_netif_t *esp_netif)
return err; return err;
} }
esp_err_t mdns_unregister_esp_netif(esp_netif_t *esp_netif) esp_err_t mdns_unregister_netif(esp_netif_t *esp_netif)
{ {
if (!_mdns_server) { if (!_mdns_server) {
return ESP_ERR_INVALID_STATE; return ESP_ERR_INVALID_STATE;

View File

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */

View File

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */

View File

@ -270,9 +270,13 @@ void app_main(void)
/* Demonstration of adding a custom netif to mdns service, but we're adding the default example one, /* 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 * so we must disable all predefined interfaces (PREDEF_NETIF_STA, AP and ETH) first
*/ */
ESP_ERROR_CHECK(mdns_register_esp_netif(EXAMPLE_INTERFACE)); ESP_ERROR_CHECK(mdns_register_netif(EXAMPLE_INTERFACE));
ESP_ERROR_CHECK(mdns_set_esp_netif_action(EXAMPLE_INTERFACE, MDNS_EVENT_ENABLE_IP4)); /* It is not enough to just register the interface, we have to enable is manually.
ESP_ERROR_CHECK(mdns_set_esp_netif_action(EXAMPLE_INTERFACE, MDNS_EVENT_ANNOUNCE_IP4)); * This is typically performed in "GOT_IP" event handler, but we call it here directly
* since the `EXAMPLE_INTERFACE` netif is connected already, to keep the example simple.
*/
ESP_ERROR_CHECK(mdns_netif_action(EXAMPLE_INTERFACE, MDNS_EVENT_ENABLE_IP4));
ESP_ERROR_CHECK(mdns_netif_action(EXAMPLE_INTERFACE, MDNS_EVENT_ANNOUNCE_IP4));
#endif #endif
initialise_button(); initialise_button();
xTaskCreate(&mdns_example_task, "mdns_example_task", 2048, NULL, 5, NULL); xTaskCreate(&mdns_example_task, "mdns_example_task", 2048, NULL, 5, NULL);