From 2a48db0f484c1af51d69327f9eb5dec80eebc99f Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Tue, 20 Nov 2018 18:24:49 +0800 Subject: [PATCH] examples/ethernet: use esp_event library to handle events --- .../ethernet/main/ethernet_example_main.c | 54 +++++++++---------- examples/ethernet/iperf/main/cmd_ethernet.c | 27 +++++----- 2 files changed, 39 insertions(+), 42 deletions(-) diff --git a/examples/ethernet/ethernet/main/ethernet_example_main.c b/examples/ethernet/ethernet/main/ethernet_example_main.c index 54a608d99..9dd67a1c5 100644 --- a/examples/ethernet/ethernet/main/ethernet_example_main.c +++ b/examples/ethernet/ethernet/main/ethernet_example_main.c @@ -12,6 +12,7 @@ #include "freertos/task.h" #include "esp_system.h" #include "esp_event_loop.h" +#include "esp_err.h" #include "esp_event.h" #include "esp_log.h" #include "esp_eth.h" @@ -92,51 +93,48 @@ static void eth_gpio_config_rmii(void) phy_rmii_smi_configure_pins(PIN_SMI_MDC, PIN_SMI_MDIO); } -/** - * @brief event handler for ethernet - * - * @param ctx - * @param event - * @return esp_err_t - */ -static esp_err_t eth_event_handler(void *ctx, system_event_t *event) +/** Event handler for Ethernet events */ +static void eth_event_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) { - tcpip_adapter_ip_info_t ip; - - switch (event->event_id) { - case SYSTEM_EVENT_ETH_CONNECTED: + switch (event_id) { + case ETHERNET_EVENT_CONNECTED: ESP_LOGI(TAG, "Ethernet Link Up"); break; - case SYSTEM_EVENT_ETH_DISCONNECTED: + case ETHERNET_EVENT_DISCONNECTED: ESP_LOGI(TAG, "Ethernet Link Down"); break; - case SYSTEM_EVENT_ETH_START: + case ETHERNET_EVENT_START: ESP_LOGI(TAG, "Ethernet Started"); break; - case SYSTEM_EVENT_ETH_GOT_IP: - memset(&ip, 0, sizeof(tcpip_adapter_ip_info_t)); - ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(ESP_IF_ETH, &ip)); - ESP_LOGI(TAG, "Ethernet Got IP Addr"); - ESP_LOGI(TAG, "~~~~~~~~~~~"); - ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip.ip)); - ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip.netmask)); - ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip.gw)); - ESP_LOGI(TAG, "~~~~~~~~~~~"); - break; - case SYSTEM_EVENT_ETH_STOP: + case ETHERNET_EVENT_STOP: ESP_LOGI(TAG, "Ethernet Stopped"); break; default: break; } - return ESP_OK; +} + +/** Event handler for IP_EVENT_ETH_GOT_IP */ +static void got_ip_event_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) +{ + ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; + const tcpip_adapter_ip_info_t* ip_info = &event->ip_info; + + ESP_LOGI(TAG, "Ethernet Got IP Address"); + ESP_LOGI(TAG, "~~~~~~~~~~~"); + ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip_info->ip)); + ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask)); + ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw)); + ESP_LOGI(TAG, "~~~~~~~~~~~"); } void app_main() { tcpip_adapter_init(); - ESP_ERROR_CHECK(esp_event_loop_init(eth_event_handler, NULL)); + ESP_ERROR_CHECK(esp_event_loop_create_default()); eth_config_t config = DEFAULT_ETHERNET_PHY_CONFIG; config.phy_addr = CONFIG_PHY_ADDRESS; @@ -150,5 +148,7 @@ void app_main() #endif ESP_ERROR_CHECK(esp_eth_init(&config)); + ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL)); ESP_ERROR_CHECK(esp_eth_enable()) ; } diff --git a/examples/ethernet/iperf/main/cmd_ethernet.c b/examples/ethernet/iperf/main/cmd_ethernet.c index 75a5ebb99..959826a50 100644 --- a/examples/ethernet/iperf/main/cmd_ethernet.c +++ b/examples/ethernet/iperf/main/cmd_ethernet.c @@ -13,7 +13,7 @@ #include "tcpip_adapter.h" #include "esp_log.h" #include "esp_console.h" -#include "esp_event_loop.h" +#include "esp_event.h" #include "esp_eth.h" #include "argtable3/argtable3.h" #include "iperf.h" @@ -216,31 +216,26 @@ static int eth_cmd_iperf(int argc, char **argv) return 0; } -static esp_err_t eth_event_handler(void *ctx, system_event_t *event) +static void event_handler(void* arg, esp_event_base_t event_base, + int32_t event_id, void* event_data) { - switch (event->event_id) { - case SYSTEM_EVENT_ETH_START: + if (event_base == ETH_EVENT && event_id == ETHERNET_EVENT_START) { started = true; - break; - case SYSTEM_EVENT_ETH_GOT_IP: - memset(&ip, 0, sizeof(tcpip_adapter_ip_info_t)); - ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(ESP_IF_ETH, &ip)); - xEventGroupSetBits(eth_event_group, GOTIP_BIT); - break; - case SYSTEM_EVENT_ETH_STOP: + } else if (event_base == ETH_EVENT && event_id == ETHERNET_EVENT_STOP) { xEventGroupClearBits(eth_event_group, GOTIP_BIT); started = false; - default: - break; + } else if (event_base == IP_EVENT && event_id == IP_EVENT_ETH_GOT_IP) { + ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; + memcpy(&ip, &event->ip_info, sizeof(ip)); + xEventGroupSetBits(eth_event_group, GOTIP_BIT); } - return ESP_OK; } void register_ethernet() { eth_event_group = xEventGroupCreate(); tcpip_adapter_init(); - ESP_ERROR_CHECK(esp_event_loop_init(eth_event_handler, NULL)); + ESP_ERROR_CHECK(esp_event_loop_create_default()); eth_config_t config = DEFAULT_ETHERNET_PHY_CONFIG; config.phy_addr = CONFIG_PHY_ADDRESS; @@ -254,6 +249,8 @@ void register_ethernet() #endif ESP_ERROR_CHECK(esp_eth_init(&config)); + ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &event_handler, NULL)); eth_control_args.control = arg_str1(NULL, NULL, "", "Start/Stop Ethernet or Get info of Ethernet"); eth_control_args.end = arg_end(1);