examples: common component initialization code to use new esp_netif

instead of tcpip_adapter
This commit is contained in:
David Cermak 2019-08-31 16:08:46 +02:00
parent ffe043b1a8
commit 21464465ea
3 changed files with 59 additions and 14 deletions

View file

@ -1,2 +1,4 @@
idf_component_register(SRCS "connect.c" "stdin_out.c"
INCLUDE_DIRS "include")
INCLUDE_DIRS "include"
PRIV_REQUIRES esp_netif
)

View file

@ -16,7 +16,7 @@
#include "esp_eth.h"
#endif
#include "esp_log.h"
#include "tcpip_adapter.h"
#include "esp_netif.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
@ -33,11 +33,12 @@
#endif
static EventGroupHandle_t s_connect_event_group;
static ip4_addr_t s_ip_addr;
static esp_ip4_addr_t s_ip_addr;
static const char *s_connection_name;
static esp_netif_t *s_example_esp_netif = NULL;
#ifdef CONFIG_EXAMPLE_CONNECT_IPV6
static ip6_addr_t s_ipv6_addr;
static esp_ip6_addr_t s_ipv6_addr;
#endif
static const char *TAG = "example_connect";
@ -51,6 +52,7 @@ static void stop(void);
static void on_got_ip(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
ESP_LOGI(TAG, "Got IP event!");
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
memcpy(&s_ip_addr, &event->ip_info.ip, sizeof(s_ip_addr));
xEventGroupSetBits(s_connect_event_group, GOT_IPV4_BIT);
@ -61,6 +63,7 @@ static void on_got_ip(void *arg, esp_event_base_t event_base,
static void on_got_ipv6(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
ESP_LOGI(TAG, "Got IPv6 event!");
ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data;
memcpy(&s_ipv6_addr, &event->ip6_info.ip, sizeof(s_ipv6_addr));
xEventGroupSetBits(s_connect_event_group, GOT_IPV6_BIT);
@ -76,6 +79,7 @@ esp_err_t example_connect(void)
s_connect_event_group = xEventGroupCreate();
start();
ESP_ERROR_CHECK(esp_register_shutdown_handler(&stop));
ESP_LOGI(TAG, "Waiting for IP");
xEventGroupWaitBits(s_connect_event_group, CONNECTED_BITS, true, true, portMAX_DELAY);
ESP_LOGI(TAG, "Connected to %s", s_connection_name);
ESP_LOGI(TAG, "IPv4 address: " IPSTR, IP2STR(&s_ip_addr));
@ -113,10 +117,10 @@ static void on_wifi_disconnect(void *arg, esp_event_base_t event_base,
#ifdef CONFIG_EXAMPLE_CONNECT_IPV6
static void on_wifi_connect(void *arg, esp_event_base_t event_base,
static void on_wifi_connect(void *esp_netif, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA);
esp_netif_create_ip6_linklocal(esp_netif);
}
#endif // CONFIG_EXAMPLE_CONNECT_IPV6
@ -126,10 +130,20 @@ static void start(void)
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
esp_netif_config_t netif_config = ESP_NETIF_DEFAULT_WIFI_STA();
esp_netif_t* netif = esp_netif_new(&netif_config);
assert(netif);
esp_wifi_set_default_wifi_sta_handlers(netif);
s_example_esp_netif = netif;
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &on_wifi_disconnect, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &on_got_ip, NULL));
#ifdef CONFIG_EXAMPLE_CONNECT_IPV6
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &on_wifi_connect, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &on_wifi_connect, netif));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6, NULL));
#endif
@ -162,6 +176,9 @@ static void stop(void)
}
ESP_ERROR_CHECK(err);
ESP_ERROR_CHECK(esp_wifi_deinit());
esp_netif_destroy(s_example_esp_netif);
s_example_esp_netif = NULL;
}
#endif // CONFIG_EXAMPLE_CONNECT_WIFI
@ -170,13 +187,13 @@ static void stop(void)
#ifdef CONFIG_EXAMPLE_CONNECT_IPV6
/** Event handler for Ethernet events */
static void on_eth_event(void *arg, esp_event_base_t event_base,
static void on_eth_event(void *esp_netif, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
switch (event_id) {
case ETHERNET_EVENT_CONNECTED:
ESP_LOGI(TAG, "Ethernet Link Up");
tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_ETH);
esp_netif_create_ip6_linklocal(esp_netif);
break;
default:
break;
@ -191,10 +208,19 @@ static esp_eth_phy_t *s_phy = NULL;
static void start(void)
{
ESP_ERROR_CHECK(tcpip_adapter_set_default_eth_handlers());
esp_netif_config_t netif_config = ESP_NETIF_DEFAULT_ETH();
esp_netif_t* netif = esp_netif_new(&netif_config);
assert(netif);
ESP_ERROR_CHECK(esp_eth_set_default_handlers(netif));
s_example_esp_netif = netif;
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &on_got_ip, NULL));
#ifdef CONFIG_EXAMPLE_CONNECT_IPV6
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event, netif));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6, NULL));
#endif
eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
@ -241,7 +267,10 @@ static void start(void)
#endif
esp_eth_config_t config = ETH_DEFAULT_CONFIG(s_mac, s_phy);
ESP_ERROR_CHECK(esp_eth_driver_install(&config, &s_eth_handle));
esp_netif_attach(netif, s_eth_handle);
s_connection_name = "Ethernet";
}
@ -255,6 +284,15 @@ static void stop(void)
ESP_ERROR_CHECK(esp_eth_driver_uninstall(s_eth_handle));
ESP_ERROR_CHECK(s_phy->del(s_phy));
ESP_ERROR_CHECK(s_mac->del(s_mac));
esp_eth_clear_default_handlers(s_example_esp_netif);
esp_netif_destroy(s_example_esp_netif);
s_example_esp_netif = NULL;
}
#endif // CONFIG_EXAMPLE_CONNECT_ETHERNET
esp_netif_t *get_example_netif(void)
{
return s_example_esp_netif;
}

View file

@ -14,14 +14,14 @@ extern "C" {
#endif
#include "esp_err.h"
#include "tcpip_adapter.h"
#include "esp_netif.h"
#ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET
#define EXAMPLE_INTERFACE TCPIP_ADAPTER_IF_ETH
#define EXAMPLE_INTERFACE get_example_netif()
#endif
#ifdef CONFIG_EXAMPLE_CONNECT_WIFI
#define EXAMPLE_INTERFACE TCPIP_ADAPTER_IF_STA
#define EXAMPLE_INTERFACE get_example_netif()
#endif
/**
@ -54,6 +54,11 @@ esp_err_t example_disconnect(void);
*/
esp_err_t example_configure_stdin_stdout(void);
/**
* @brief Returns esp-netif pointer created by example_connect()
*
*/
esp_netif_t *get_example_netif(void);
#ifdef __cplusplus
}
#endif