From a2d59525e53099ee1ad63c3d60ff853f573ab535 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Wed, 21 Nov 2018 13:46:45 +0800 Subject: [PATCH] mdns: use esp_event library to handle events --- components/esp32/CMakeLists.txt | 1 - components/esp_wifi/CMakeLists.txt | 1 + components/mdns/mdns.c | 142 ++++++++++++------ .../mdns/private_include/mdns_networking.h | 3 +- .../mdns/private_include/mdns_private.h | 5 +- .../protocols/mdns/main/mdns_example_main.c | 2 +- 6 files changed, 100 insertions(+), 54 deletions(-) diff --git a/components/esp32/CMakeLists.txt b/components/esp32/CMakeLists.txt index 9749eef58..a6b61623e 100644 --- a/components/esp32/CMakeLists.txt +++ b/components/esp32/CMakeLists.txt @@ -24,7 +24,6 @@ else() "hw_random.c" "int_wdt.c" "intr_alloc.c" - "mesh_event.c" "panic.c" "pm_esp32.c" "pm_trace.c" diff --git a/components/esp_wifi/CMakeLists.txt b/components/esp_wifi/CMakeLists.txt index 433d20f72..63ec85038 100644 --- a/components/esp_wifi/CMakeLists.txt +++ b/components/esp_wifi/CMakeLists.txt @@ -2,6 +2,7 @@ set(COMPONENT_SRCS "src/coexist.c" "src/fast_crypto_ops.c" "src/lib_printf.c" + "src/mesh_event.c" "src/phy_init.c" "src/restore.c" "src/wifi_init.c") diff --git a/components/mdns/mdns.c b/components/mdns/mdns.c index 0f52ecf8e..8c9767942 100644 --- a/components/mdns/mdns.c +++ b/components/mdns/mdns.c @@ -3048,56 +3048,69 @@ void _mdns_disable_pcb(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protoc /** * @brief Dispatch interface changes based on system events */ -void _mdns_handle_system_event(system_event_id_t event, tcpip_adapter_if_t interface) +static void _mdns_handle_system_event(esp_event_base_t event_base, + int32_t event_id, tcpip_adapter_if_t interface) { if (!_mdns_server) { return; } - + tcpip_adapter_dhcp_status_t dcst; - switch(event) { - case SYSTEM_EVENT_STA_CONNECTED: - if (!tcpip_adapter_dhcpc_get_status(TCPIP_ADAPTER_IF_STA, &dcst)) { - if (dcst != TCPIP_ADAPTER_DHCP_STARTED) { + if (event_base == WIFI_EVENT) { + switch(event_id) { + case WIFI_EVENT_STA_CONNECTED: + if (!tcpip_adapter_dhcpc_get_status(TCPIP_ADAPTER_IF_STA, &dcst)) { + if (dcst != TCPIP_ADAPTER_DHCP_STARTED) { + _mdns_enable_pcb(TCPIP_ADAPTER_IF_STA, MDNS_IP_PROTOCOL_V4); + } + } + break; + case WIFI_EVENT_STA_DISCONNECTED: + _mdns_disable_pcb(TCPIP_ADAPTER_IF_STA, MDNS_IP_PROTOCOL_V4); + _mdns_disable_pcb(TCPIP_ADAPTER_IF_STA, MDNS_IP_PROTOCOL_V6); + break; + case WIFI_EVENT_AP_START: + _mdns_enable_pcb(TCPIP_ADAPTER_IF_AP, MDNS_IP_PROTOCOL_V4); + break; + case WIFI_EVENT_AP_STOP: + _mdns_disable_pcb(TCPIP_ADAPTER_IF_AP, MDNS_IP_PROTOCOL_V4); + _mdns_disable_pcb(TCPIP_ADAPTER_IF_AP, MDNS_IP_PROTOCOL_V6); + break; + default: + break; + } + } else if (event_base == ETH_EVENT) { + switch (event_id) { + case ETHERNET_EVENT_CONNECTED: + if (!tcpip_adapter_dhcpc_get_status(TCPIP_ADAPTER_IF_ETH, &dcst)) { + if (dcst != TCPIP_ADAPTER_DHCP_STARTED) { + _mdns_enable_pcb(TCPIP_ADAPTER_IF_ETH, MDNS_IP_PROTOCOL_V4); + } + } + break; + case ETHERNET_EVENT_DISCONNECTED: + _mdns_disable_pcb(TCPIP_ADAPTER_IF_ETH, MDNS_IP_PROTOCOL_V4); + _mdns_disable_pcb(TCPIP_ADAPTER_IF_ETH, MDNS_IP_PROTOCOL_V6); + break; + default: + break; + } + } else if (event_base == IP_EVENT) { + switch (event_id) { + case IP_EVENT_STA_GOT_IP: _mdns_enable_pcb(TCPIP_ADAPTER_IF_STA, MDNS_IP_PROTOCOL_V4); - } - } - break; - case SYSTEM_EVENT_STA_GOT_IP: - _mdns_enable_pcb(TCPIP_ADAPTER_IF_STA, MDNS_IP_PROTOCOL_V4); - _mdns_announce_pcb(TCPIP_ADAPTER_IF_STA, MDNS_IP_PROTOCOL_V6, NULL, 0, true); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - _mdns_disable_pcb(TCPIP_ADAPTER_IF_STA, MDNS_IP_PROTOCOL_V4); - _mdns_disable_pcb(TCPIP_ADAPTER_IF_STA, MDNS_IP_PROTOCOL_V6); - break; - case SYSTEM_EVENT_AP_START: - _mdns_enable_pcb(TCPIP_ADAPTER_IF_AP, MDNS_IP_PROTOCOL_V4); - break; - case SYSTEM_EVENT_AP_STOP: - _mdns_disable_pcb(TCPIP_ADAPTER_IF_AP, MDNS_IP_PROTOCOL_V4); - _mdns_disable_pcb(TCPIP_ADAPTER_IF_AP, MDNS_IP_PROTOCOL_V6); - break; - case SYSTEM_EVENT_GOT_IP6: - _mdns_enable_pcb(interface, MDNS_IP_PROTOCOL_V6); - _mdns_announce_pcb(interface, MDNS_IP_PROTOCOL_V4, NULL, 0, true); - break; - case SYSTEM_EVENT_ETH_CONNECTED: - if (!tcpip_adapter_dhcpc_get_status(TCPIP_ADAPTER_IF_ETH, &dcst)) { - if (dcst != TCPIP_ADAPTER_DHCP_STARTED) { + _mdns_announce_pcb(TCPIP_ADAPTER_IF_STA, MDNS_IP_PROTOCOL_V6, NULL, 0, true); + break; + case IP_EVENT_ETH_GOT_IP: _mdns_enable_pcb(TCPIP_ADAPTER_IF_ETH, MDNS_IP_PROTOCOL_V4); - } + break; + case IP_EVENT_GOT_IP6: + _mdns_enable_pcb(interface, MDNS_IP_PROTOCOL_V6); + _mdns_announce_pcb(interface, MDNS_IP_PROTOCOL_V4, NULL, 0, true); + break; + default: + break; } - break; - case SYSTEM_EVENT_ETH_GOT_IP: - _mdns_enable_pcb(TCPIP_ADAPTER_IF_ETH, MDNS_IP_PROTOCOL_V4); - break; - case SYSTEM_EVENT_ETH_DISCONNECTED: - _mdns_disable_pcb(TCPIP_ADAPTER_IF_ETH, MDNS_IP_PROTOCOL_V4); - _mdns_disable_pcb(TCPIP_ADAPTER_IF_ETH, MDNS_IP_PROTOCOL_V6); - break; - default: - break; } } @@ -3709,7 +3722,8 @@ static void _mdns_execute_action(mdns_action_t * action) switch(action->type) { case ACTION_SYSTEM_EVENT: - _mdns_handle_system_event(action->data.sys_event.event_id, action->data.sys_event.interface); + _mdns_handle_system_event(action->data.sys_event.event_base, + action->data.sys_event.event_id, action->data.sys_event.interface); break; case ACTION_HOSTNAME_SET: _mdns_send_final_bye(true); @@ -4098,23 +4112,34 @@ static esp_err_t _mdns_service_task_stop() * */ esp_err_t mdns_handle_system_event(void *ctx, system_event_t *event) +{ + /* no-op, kept for compatibility */ + return ESP_OK; +} + +static void event_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) { if (!_mdns_server) { - return ESP_OK; + return; } - mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t)); + mdns_action_t * action = (mdns_action_t *)calloc(1, sizeof(mdns_action_t)); if (!action) { HOOK_MALLOC_FAILED; - return ESP_OK; + return; } action->type = ACTION_SYSTEM_EVENT; - action->data.sys_event.event_id = event->event_id; - action->data.sys_event.interface = (event->event_id == SYSTEM_EVENT_GOT_IP6)?event->event_info.got_ip6.if_index:0; + action->data.sys_event.event_base = event_base; + action->data.sys_event.event_id = event_id; + if (event_base == IP_EVENT && event_id == IP_EVENT_GOT_IP6) { + ip_event_got_ip6_t* event = (ip_event_got_ip6_t*) event_data; + action->data.sys_event.interface = event->if_index; + } + if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) { free(action); - } - return ESP_OK; + } } esp_err_t mdns_init() @@ -4144,6 +4169,16 @@ esp_err_t mdns_init() goto free_lock; } + if ((err = esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)) != ESP_OK) { + goto free_event_handlers; + } + if ((err = esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)) != ESP_OK) { + goto free_event_handlers; + } + if ((err = esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)) != ESP_OK) { + goto free_event_handlers; + } + uint8_t i; ip6_addr_t tmp_addr6; tcpip_adapter_ip_info_t if_ip_info; @@ -4170,6 +4205,10 @@ free_all_and_disable_pcbs: _mdns_disable_pcb(i, MDNS_IP_PROTOCOL_V6); _mdns_disable_pcb(i, MDNS_IP_PROTOCOL_V4); } +free_event_handlers: + esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler); + esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, &event_handler); + esp_event_handler_unregister(ETH_EVENT, ESP_EVENT_ANY_ID, &event_handler); vQueueDelete(_mdns_server->action_queue); free_lock: vSemaphoreDelete(_mdns_server->lock); @@ -4215,6 +4254,9 @@ void mdns_free() free(h); } vSemaphoreDelete(_mdns_server->lock); + esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler); + esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, &event_handler); + esp_event_handler_unregister(ETH_EVENT, ESP_EVENT_ANY_ID, &event_handler); free(_mdns_server); _mdns_server = NULL; } diff --git a/components/mdns/private_include/mdns_networking.h b/components/mdns/private_include/mdns_networking.h index 5b56f8066..1d13e308f 100644 --- a/components/mdns/private_include/mdns_networking.h +++ b/components/mdns/private_include/mdns_networking.h @@ -20,7 +20,8 @@ #include "esp_wifi.h" #include "esp_system.h" #include "esp_timer.h" -#include "esp_event_loop.h" +#include "esp_event.h" +#include "esp_eth.h" /** diff --git a/components/mdns/private_include/mdns_private.h b/components/mdns/private_include/mdns_private.h index a7383c03d..a5b0747aa 100644 --- a/components/mdns/private_include/mdns_private.h +++ b/components/mdns/private_include/mdns_private.h @@ -14,6 +14,8 @@ #ifndef MDNS_PRIVATE_H_ #define MDNS_PRIVATE_H_ +#include "esp_event_base.h" + //#define MDNS_ENABLE_DEBUG #ifdef MDNS_ENABLE_DEBUG @@ -346,7 +348,8 @@ typedef struct { char * hostname; char * instance; struct { - system_event_id_t event_id; + esp_event_base_t event_base; + int32_t event_id; tcpip_adapter_if_t interface; } sys_event; struct { diff --git a/examples/protocols/mdns/main/mdns_example_main.c b/examples/protocols/mdns/main/mdns_example_main.c index 27d7d2b43..20cdbc3b0 100644 --- a/examples/protocols/mdns/main/mdns_example_main.c +++ b/examples/protocols/mdns/main/mdns_example_main.c @@ -247,8 +247,8 @@ static void mdns_example_task(void *pvParameters) void app_main() { ESP_ERROR_CHECK( nvs_flash_init() ); - initialise_mdns(); initialise_wifi(); + initialise_mdns(); initialise_button(); xTaskCreate(&mdns_example_task, "mdns_example_task", 2048, NULL, 5, NULL); }