Skip to content

Commit

Permalink
tcpip_adapter: updated tcpip_adapter compatablity layer to include all
Browse files Browse the repository at this point in the history
public API and keep 100% backward compatibility
update build of tcpip adapter when ethernet disabled
  • Loading branch information
david-cermak committed Nov 13, 2019
1 parent 7ef3859 commit 7f5cda1
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 15 deletions.
2 changes: 1 addition & 1 deletion components/esp_eth/src/esp_eth.c
Expand Up @@ -223,7 +223,7 @@ esp_err_t esp_eth_driver_install(const esp_eth_config_t *config, esp_eth_handle_
ETH_CHECK(xTimerStart(eth_driver->check_link_timer, 0) == pdPASS, "start eth_link_timer failed", err_start_timer, ESP_FAIL);
eth_driver->base.post_attach = esp_eth_post_attach_driver_start;
*out_hdl = (esp_eth_handle_t)eth_driver;
tcpip_adapter_start_eth(eth_driver);
tcpip_adapter_compat_start_eth(eth_driver);
return ESP_OK;
err_start_timer:
xTimerDelete(eth_driver->check_link_timer, 0);
Expand Down
2 changes: 2 additions & 0 deletions components/esp_netif/esp_netif_defaults.c
Expand Up @@ -14,7 +14,9 @@

#include "esp_netif.h"
#include "esp_wifi_default.h"
#if CONFIG_ETH_ENABLED
#include "esp_eth.h"
#endif

//
// Purpose of this module is to provide
Expand Down
2 changes: 2 additions & 0 deletions components/mdns/mdns.c
Expand Up @@ -3110,8 +3110,10 @@ static void _mdns_handle_system_event(esp_event_base_t event_base,
s_esp_netifs[MDNS_IF_STA] = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_START) {
s_esp_netifs[MDNS_IF_AP] = esp_netif_get_handle_from_ifkey("WIFI_AP_DEF");
#if CONFIG_ETH_ENABLED
} else if (event_base == ETH_EVENT && event_id == ETHERNET_EVENT_START) {
s_esp_netifs[MDNS_IF_ETH] = esp_netif_get_handle_from_ifkey("ETH_DEF");
#endif
}

esp_netif_dhcp_status_t dcst;
Expand Down
78 changes: 74 additions & 4 deletions components/tcpip_adapter/include/tcpip_adapter.h
Expand Up @@ -96,13 +96,19 @@ esp_err_t tcpip_adapter_set_default_eth_handlers(void);

/**
* @brief Compatible version of network stack input function. Translates to esp_netif_receive()
* @param buffer
* @param len
* @param eb
* @return see esp_netif_receive
*/
esp_err_t tcpip_adapter_eth_input(void *buffer, uint16_t len, void *eb);

/**
* @brief Compatible version of network stack input function. Translates to esp_netif_receive()
*/
esp_err_t tcpip_adapter_sta_input(void *buffer, uint16_t len, void *eb);

/**
* @brief Compatible version of network stack input function. Translates to esp_netif_receive()
*/
esp_err_t tcpip_adapter_ap_input(void *buffer, uint16_t len, void *eb);

/**
* @brief Compatible version of former tcpip_adapter API to clear default WIFI handlers
* @return ESP_OK on success
Expand Down Expand Up @@ -175,4 +181,68 @@ int tcpip_adapter_get_netif_index(tcpip_adapter_if_t tcpip_if);
*/
esp_err_t tcpip_adapter_get_sta_list(const wifi_sta_list_t *wifi_sta_list, tcpip_adapter_sta_list_t *tcpip_sta_list);

/**
* @brief Compatible version of former tcpip_adapter API of esp_netif_action_start for default ethernet
*/
esp_err_t tcpip_adapter_eth_start(uint8_t *mac, tcpip_adapter_ip_info_t *ip_info, void *args);

/**
* @brief Compatible version of former tcpip_adapter API of esp_netif_action_start for default station
*/
esp_err_t tcpip_adapter_sta_start(uint8_t *mac, tcpip_adapter_ip_info_t *ip_info);

/**
* @brief Compatible version of former tcpip_adapter API of esp_netif_action_start for default softAP
*/
esp_err_t tcpip_adapter_ap_start(uint8_t *mac, tcpip_adapter_ip_info_t *ip_info);

/**
* @brief Compatible version of former tcpip_adapter API of esp_netif_action_stop
*/
esp_err_t tcpip_adapter_stop(tcpip_adapter_if_t tcpip_if);

/**
* @brief Compatible version of former tcpip_adapter API of esp_netif_up
*/
esp_err_t tcpip_adapter_up(tcpip_adapter_if_t tcpip_if);

/**
* @brief Compatible version of former tcpip_adapter API of esp_netif_down
*/
esp_err_t tcpip_adapter_down(tcpip_adapter_if_t tcpip_if);

/**
* @brief Compatible version of former tcpip_adapter API of esp_netif_get_old_ip_info
*/
esp_err_t tcpip_adapter_get_old_ip_info(tcpip_adapter_if_t tcpip_if, tcpip_adapter_ip_info_t *ip_info);

/**
* @brief Compatible version of former tcpip_adapter API of esp_netif_set_old_ip_info
*/
esp_err_t tcpip_adapter_set_old_ip_info(tcpip_adapter_if_t tcpip_if, const tcpip_adapter_ip_info_t *ip_info);

/**
* @brief Compatible version of former tcpip_adapter API of esp_netif_get_handle_from_netif_impl
*/
esp_interface_t tcpip_adapter_get_esp_if(void *dev);

/**
* @brief Compatible version of former tcpip_adapter API of esp_netif_set_hostname
*/
esp_err_t tcpip_adapter_set_hostname(tcpip_adapter_if_t tcpip_if, const char *hostname);

/**
* @brief Compatible version of former tcpip_adapter API of esp_netif_get_hostname
*/
esp_err_t tcpip_adapter_get_hostname(tcpip_adapter_if_t tcpip_if, const char **hostname);

/**
* @brief This function is called from wifi_init to assure backward compatibility mode
* of tcpip_adapter. In case of legacy use, default instances of ap and sta
* are created and handlers are registered
*
* @return ESP_OK on success
*/
esp_err_t tcpip_adapter_set_default_wifi_handlers(void);

#endif //_TCPIP_ADAPTER_H_
Expand Up @@ -15,26 +15,43 @@
#ifndef _TCPIP_ADAPTER_COMPAT_H_
#define _TCPIP_ADAPTER_COMPAT_H_

/**
* @brief This function is called from ethernet driver init code to facilitate
* autostart fo the driver in backward compatible tcpip_adapter way
*
* @note This api is provided in a separate header, which is included internally only (from wifi driver)
* rather then user initialization code.
*
* @param[in] h Handle to the ethernet driver
*
* @return ESP_OK on success
*/
esp_err_t tcpip_adapter_compat_start_eth(void* h);

/**
* @brief This function is called from wifi_init to assure backward compatibility mode
* of tcpip_adapter. In case of legacy use, default instances of ap and sta
* are created and handlers are registered
*
* @note This api is given in a separate header, which is included internally (from wifi driver)
* rather then user initialization code.
* @note This API is provided in a separate header, which is included internally only (from wifi_init)
* rather then user initialization code. At this same time this API is also a public API of former tcqpip_adapter
* and thus provided also in tcpip_adapter.h
*
* @return ESP_OK on success
*/
esp_err_t tcpip_adapter_set_default_wifi_handlers(void);

/**
* @brief This function is called from ethernet driver init code to facilitate
* autostart fo the driver in backward compatible tcpip_adapter way
* @brief This function is called from wifi_init to assure backward compatibility mode
* of tcpip_adapter. In case of legacy use, default instances of ap and sta
* are destroyed and handlers are unregistered
*
* @param[in] h Handle to the ethernet driver
* @note This API is provided in a separate header, which is included internally only (from wifi_init)
* rather then user initialization code. At this same time this API is also a public API of former tcqpip_adapter
* and thus provided also in tcpip_adapter.h
*
* @return ESP_OK on success
*/
esp_err_t tcpip_adapter_start_eth(void* h);
esp_err_t tcpip_adapter_clear_default_wifi_handlers(void);

#endif //_TCPIP_ADAPTER_COMPAT_H_
11 changes: 11 additions & 0 deletions components/tcpip_adapter/include/tcpip_adapter_types.h
Expand Up @@ -32,6 +32,17 @@
#define TCPIP_ADAPTER_IP_ADDRESS_LEASE_TIME ESP_NETIF_IP_ADDRESS_LEASE_TIME
#define TCPIP_ADAPTER_IP_REQUEST_RETRY_TIME ESP_NETIF_IP_REQUEST_RETRY_TIME

/** @brief Legacy error code definitions
*
*/
#define ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS ESP_ERR_ESP_NETIF_INVALID_PARAMS
#define ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY ESP_ERR_ESP_NETIF_IF_NOT_READY
#define ESP_ERR_TCPIP_ADAPTER_DHCPC_START_FAILED ESP_ERR_ESP_NETIF_DHCPC_START_FAILED
#define ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STARTED ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED
#define ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED
#define ESP_ERR_TCPIP_ADAPTER_NO_MEM ESP_ERR_ESP_NETIF_NO_MEM
#define ESP_ERR_TCPIP_ADAPTER_DHCP_NOT_STOPPED ESP_ERR_ESP_NETIF_DHCP_NOT_STOPPED

typedef enum {
TCPIP_ADAPTER_IF_STA = 0, /**< Wi-Fi STA (station) interface */
TCPIP_ADAPTER_IF_AP, /**< Wi-Fi soft-AP interface */
Expand Down
108 changes: 104 additions & 4 deletions components/tcpip_adapter/tcpip_adapter_compat.c
Expand Up @@ -19,14 +19,21 @@

#if CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER

#if CONFIG_ETH_ENABLED
#include "esp_eth.h"
#endif
#include "tcpip_adapter_types.h"
#include "esp_wifi_default.h"

//
// Accessing some internal default interfaces and esp-netifs
//
extern void _esp_wifi_set_default_ap_netif(esp_netif_t* esp_netif);
extern void _esp_wifi_set_default_sta_netif(esp_netif_t* esp_netif);
extern esp_err_t _esp_wifi_set_default_wifi_handlers(void);
extern esp_err_t _esp_wifi_clear_default_wifi_handlers(void);
extern esp_err_t esp_netif_up(esp_netif_t *esp_netif);
extern esp_err_t esp_netif_down(esp_netif_t *esp_netif);

//
// Purpose of this module is to provide backward compatible version of esp-netif
Expand Down Expand Up @@ -98,14 +105,16 @@ void tcpip_adapter_init(void)
}
}

static void tcpip_adapter_eth_start(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data)
#if CONFIG_ETH_ENABLED
static void tcpip_adapter_attach_eth_to_netif(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data)
{
esp_eth_handle_t eth_handle = *(esp_eth_handle_t*)data;
esp_netif_attach(esp_netif, eth_handle);
}

esp_err_t tcpip_adapter_clear_default_eth_handlers(void)
{
ESP_ERROR_CHECK(esp_event_handler_unregister(ETH_EVENT, ETHERNET_EVENT_START, tcpip_adapter_attach_eth_to_netif));
return esp_eth_clear_default_handlers(netif_from_if(TCPIP_ADAPTER_IF_ETH));
}

Expand All @@ -117,7 +126,7 @@ esp_err_t tcpip_adapter_set_default_eth_handlers(void)

s_esp_netifs[TCPIP_ADAPTER_IF_ETH] = eth_netif;
// provide a separate "after driver start" hook to attach
esp_err_t ret = esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_START, tcpip_adapter_eth_start, eth_netif);
esp_err_t ret = esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_START, tcpip_adapter_attach_eth_to_netif, eth_netif);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to register ");
return ret;
Expand All @@ -128,13 +137,24 @@ esp_err_t tcpip_adapter_set_default_eth_handlers(void)
return ESP_OK;

}
#endif

esp_err_t tcpip_adapter_eth_input(void *buffer, uint16_t len, void *eb)
{
return esp_netif_receive(netif_from_if(TCPIP_ADAPTER_IF_ETH), buffer, len, eb);
}

esp_err_t tcpip_adapter_start_eth(void* eth_driver)
esp_err_t tcpip_adapter_sta_input(void *buffer, uint16_t len, void *eb)
{
return esp_netif_receive(netif_from_if(TCPIP_ADAPTER_IF_STA), buffer, len, eb);
}

esp_err_t tcpip_adapter_ap_input(void *buffer, uint16_t len, void *eb)
{
return esp_netif_receive(netif_from_if(TCPIP_ADAPTER_IF_AP), buffer, len, eb);
}

esp_err_t tcpip_adapter_compat_start_eth(void* eth_driver)
{
if (s_tcpip_adapter_compat) {
esp_netif_t *esp_netif = netif_from_if(TCPIP_ADAPTER_IF_ETH);
Expand Down Expand Up @@ -275,9 +295,89 @@ esp_err_t tcpip_adapter_get_sta_list(const wifi_sta_list_t *wifi_sta_list, tcpip
return esp_netif_get_sta_list(wifi_sta_list, tcpip_sta_list);
}

static esp_err_t tcpip_adapter_compat_start_netif(esp_netif_t *netif, uint8_t *mac, tcpip_adapter_ip_info_t *ip_info)
{
if (netif == NULL || mac == NULL || ip_info == NULL) {
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
}
esp_netif_set_mac(netif, mac);
esp_netif_set_ip_info(netif, (esp_netif_ip_info_t *)ip_info);
esp_netif_action_start(netif, NULL, 0, NULL);
return ESP_OK;
}

esp_err_t tcpip_adapter_eth_start(uint8_t *mac, tcpip_adapter_ip_info_t *ip_info, void *args)
{
return tcpip_adapter_compat_start_netif(netif_from_if(TCPIP_ADAPTER_IF_ETH),
mac, ip_info);
}

esp_err_t tcpip_adapter_sta_start(uint8_t *mac, tcpip_adapter_ip_info_t *ip_info)
{
return tcpip_adapter_compat_start_netif(netif_from_if(TCPIP_ADAPTER_IF_STA),
mac, ip_info);
}

esp_err_t tcpip_adapter_ap_start(uint8_t *mac, tcpip_adapter_ip_info_t *ip_info)
{
return tcpip_adapter_compat_start_netif(netif_from_if(TCPIP_ADAPTER_IF_AP),
mac, ip_info);
}

esp_err_t tcpip_adapter_stop(tcpip_adapter_if_t tcpip_if)
{
esp_netif_t *netif = netif_from_if(tcpip_if);
if (netif == NULL) {
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
}
esp_netif_action_stop(netif_from_if(tcpip_if), NULL, 0, NULL);
return ESP_OK;
}

esp_err_t tcpip_adapter_up(tcpip_adapter_if_t tcpip_if)
{
return esp_netif_up(netif_from_if(tcpip_if));
}

esp_err_t tcpip_adapter_down(tcpip_adapter_if_t tcpip_if)
{
return esp_netif_down(netif_from_if(tcpip_if));
}

esp_err_t tcpip_adapter_get_old_ip_info(tcpip_adapter_if_t tcpip_if, tcpip_adapter_ip_info_t *ip_info)
{
return esp_netif_get_old_ip_info(netif_from_if(tcpip_if), (esp_netif_ip_info_t *)ip_info);
}

esp_err_t tcpip_adapter_set_old_ip_info(tcpip_adapter_if_t tcpip_if, const tcpip_adapter_ip_info_t *ip_info)
{
return esp_netif_set_old_ip_info(netif_from_if(tcpip_if), (esp_netif_ip_info_t *)ip_info);
}

esp_interface_t tcpip_adapter_get_esp_if(void *dev)
{
esp_netif_t *netif = esp_netif_get_handle_from_netif_impl(dev);
for (int i=0; i< TCPIP_ADAPTER_IF_MAX; ++i) {
if (s_esp_netifs[i] == netif) {
return i;
}
}
return ESP_IF_MAX;
}

esp_err_t tcpip_adapter_set_hostname(tcpip_adapter_if_t tcpip_if, const char *hostname)
{
return esp_netif_set_hostname(netif_from_if(tcpip_if), hostname);
}

esp_err_t tcpip_adapter_get_hostname(tcpip_adapter_if_t tcpip_if, const char **hostname)
{
return esp_netif_get_hostname(netif_from_if(tcpip_if), hostname);
}

#else

esp_err_t tcpip_adapter_start_eth(void* eth_driver)
esp_err_t tcpip_adapter_compat_start_eth(void* eth_driver)
{
return ESP_OK;
}
Expand Down

0 comments on commit 7f5cda1

Please sign in to comment.