mdns: use esp_event library to handle events

This commit is contained in:
Ivan Grokhotkov 2018-11-21 13:46:45 +08:00
parent b1d1e37f87
commit a2d59525e5
6 changed files with 100 additions and 54 deletions

View file

@ -24,7 +24,6 @@ else()
"hw_random.c" "hw_random.c"
"int_wdt.c" "int_wdt.c"
"intr_alloc.c" "intr_alloc.c"
"mesh_event.c"
"panic.c" "panic.c"
"pm_esp32.c" "pm_esp32.c"
"pm_trace.c" "pm_trace.c"

View file

@ -2,6 +2,7 @@ set(COMPONENT_SRCS
"src/coexist.c" "src/coexist.c"
"src/fast_crypto_ops.c" "src/fast_crypto_ops.c"
"src/lib_printf.c" "src/lib_printf.c"
"src/mesh_event.c"
"src/phy_init.c" "src/phy_init.c"
"src/restore.c" "src/restore.c"
"src/wifi_init.c") "src/wifi_init.c")

View file

@ -3048,57 +3048,70 @@ void _mdns_disable_pcb(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t ip_protoc
/** /**
* @brief Dispatch interface changes based on system events * @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) { if (!_mdns_server) {
return; return;
} }
tcpip_adapter_dhcp_status_t dcst; tcpip_adapter_dhcp_status_t dcst;
switch(event) { if (event_base == WIFI_EVENT) {
case SYSTEM_EVENT_STA_CONNECTED: switch(event_id) {
case WIFI_EVENT_STA_CONNECTED:
if (!tcpip_adapter_dhcpc_get_status(TCPIP_ADAPTER_IF_STA, &dcst)) { if (!tcpip_adapter_dhcpc_get_status(TCPIP_ADAPTER_IF_STA, &dcst)) {
if (dcst != TCPIP_ADAPTER_DHCP_STARTED) { if (dcst != TCPIP_ADAPTER_DHCP_STARTED) {
_mdns_enable_pcb(TCPIP_ADAPTER_IF_STA, MDNS_IP_PROTOCOL_V4); _mdns_enable_pcb(TCPIP_ADAPTER_IF_STA, MDNS_IP_PROTOCOL_V4);
} }
} }
break; break;
case SYSTEM_EVENT_STA_GOT_IP: case WIFI_EVENT_STA_DISCONNECTED:
_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_V4);
_mdns_disable_pcb(TCPIP_ADAPTER_IF_STA, MDNS_IP_PROTOCOL_V6); _mdns_disable_pcb(TCPIP_ADAPTER_IF_STA, MDNS_IP_PROTOCOL_V6);
break; break;
case SYSTEM_EVENT_AP_START: case WIFI_EVENT_AP_START:
_mdns_enable_pcb(TCPIP_ADAPTER_IF_AP, MDNS_IP_PROTOCOL_V4); _mdns_enable_pcb(TCPIP_ADAPTER_IF_AP, MDNS_IP_PROTOCOL_V4);
break; break;
case SYSTEM_EVENT_AP_STOP: 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_V4);
_mdns_disable_pcb(TCPIP_ADAPTER_IF_AP, MDNS_IP_PROTOCOL_V6); _mdns_disable_pcb(TCPIP_ADAPTER_IF_AP, MDNS_IP_PROTOCOL_V6);
break; break;
case SYSTEM_EVENT_GOT_IP6: default:
_mdns_enable_pcb(interface, MDNS_IP_PROTOCOL_V6);
_mdns_announce_pcb(interface, MDNS_IP_PROTOCOL_V4, NULL, 0, true);
break; break;
case SYSTEM_EVENT_ETH_CONNECTED: }
} 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 (!tcpip_adapter_dhcpc_get_status(TCPIP_ADAPTER_IF_ETH, &dcst)) {
if (dcst != TCPIP_ADAPTER_DHCP_STARTED) { if (dcst != TCPIP_ADAPTER_DHCP_STARTED) {
_mdns_enable_pcb(TCPIP_ADAPTER_IF_ETH, MDNS_IP_PROTOCOL_V4); _mdns_enable_pcb(TCPIP_ADAPTER_IF_ETH, MDNS_IP_PROTOCOL_V4);
} }
} }
break; break;
case SYSTEM_EVENT_ETH_GOT_IP: case ETHERNET_EVENT_DISCONNECTED:
_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_V4);
_mdns_disable_pcb(TCPIP_ADAPTER_IF_ETH, MDNS_IP_PROTOCOL_V6); _mdns_disable_pcb(TCPIP_ADAPTER_IF_ETH, MDNS_IP_PROTOCOL_V6);
break; break;
default: default:
break; 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);
_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;
}
}
} }
/* /*
@ -3709,7 +3722,8 @@ static void _mdns_execute_action(mdns_action_t * action)
switch(action->type) { switch(action->type) {
case ACTION_SYSTEM_EVENT: 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; break;
case ACTION_HOSTNAME_SET: case ACTION_HOSTNAME_SET:
_mdns_send_final_bye(true); _mdns_send_final_bye(true);
@ -4099,22 +4113,33 @@ static esp_err_t _mdns_service_task_stop()
esp_err_t mdns_handle_system_event(void *ctx, system_event_t *event) esp_err_t mdns_handle_system_event(void *ctx, system_event_t *event)
{ {
if (!_mdns_server) { /* no-op, kept for compatibility */
return ESP_OK; return ESP_OK;
} }
mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t)); static void event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
if (!_mdns_server) {
return;
}
mdns_action_t * action = (mdns_action_t *)calloc(1, sizeof(mdns_action_t));
if (!action) { if (!action) {
HOOK_MALLOC_FAILED; HOOK_MALLOC_FAILED;
return ESP_OK; return;
} }
action->type = ACTION_SYSTEM_EVENT; action->type = ACTION_SYSTEM_EVENT;
action->data.sys_event.event_id = event->event_id; action->data.sys_event.event_base = event_base;
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_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) { if (xQueueSend(_mdns_server->action_queue, &action, (portTickType)0) != pdPASS) {
free(action); free(action);
} }
return ESP_OK;
} }
esp_err_t mdns_init() esp_err_t mdns_init()
@ -4144,6 +4169,16 @@ esp_err_t mdns_init()
goto free_lock; 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; uint8_t i;
ip6_addr_t tmp_addr6; ip6_addr_t tmp_addr6;
tcpip_adapter_ip_info_t if_ip_info; 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_V6);
_mdns_disable_pcb(i, MDNS_IP_PROTOCOL_V4); _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); vQueueDelete(_mdns_server->action_queue);
free_lock: free_lock:
vSemaphoreDelete(_mdns_server->lock); vSemaphoreDelete(_mdns_server->lock);
@ -4215,6 +4254,9 @@ void mdns_free()
free(h); free(h);
} }
vSemaphoreDelete(_mdns_server->lock); 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); free(_mdns_server);
_mdns_server = NULL; _mdns_server = NULL;
} }

View file

@ -20,7 +20,8 @@
#include "esp_wifi.h" #include "esp_wifi.h"
#include "esp_system.h" #include "esp_system.h"
#include "esp_timer.h" #include "esp_timer.h"
#include "esp_event_loop.h" #include "esp_event.h"
#include "esp_eth.h"
/** /**

View file

@ -14,6 +14,8 @@
#ifndef MDNS_PRIVATE_H_ #ifndef MDNS_PRIVATE_H_
#define MDNS_PRIVATE_H_ #define MDNS_PRIVATE_H_
#include "esp_event_base.h"
//#define MDNS_ENABLE_DEBUG //#define MDNS_ENABLE_DEBUG
#ifdef MDNS_ENABLE_DEBUG #ifdef MDNS_ENABLE_DEBUG
@ -346,7 +348,8 @@ typedef struct {
char * hostname; char * hostname;
char * instance; char * instance;
struct { struct {
system_event_id_t event_id; esp_event_base_t event_base;
int32_t event_id;
tcpip_adapter_if_t interface; tcpip_adapter_if_t interface;
} sys_event; } sys_event;
struct { struct {

View file

@ -247,8 +247,8 @@ static void mdns_example_task(void *pvParameters)
void app_main() void app_main()
{ {
ESP_ERROR_CHECK( nvs_flash_init() ); ESP_ERROR_CHECK( nvs_flash_init() );
initialise_mdns();
initialise_wifi(); initialise_wifi();
initialise_mdns();
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);
} }