ESP-NETIF: easier creation for WiFi interfaces with custom parameters

Closes https://github.com/espressif/esp-idf/issues/4611
Closes IDFGH-2523
This commit is contained in:
David Cermak 2020-01-30 12:50:52 +01:00
parent c5d05b5c4f
commit 02cdef086f
5 changed files with 113 additions and 47 deletions

View file

@ -35,44 +35,16 @@
//
// Default configuration of common interfaces, such as STA, AP, ETH
//
const esp_netif_inherent_config_t _g_esp_netif_inherent_sta_config = {
.flags = ESP_NETIF_DHCP_CLIENT | ESP_NETIF_FLAG_GARP | ESP_NETIF_FLAG_EVENT_IP_MODIFIED,
.lost_ip_event = IP_EVENT_STA_LOST_IP,
.get_ip_event = IP_EVENT_STA_GOT_IP,
.if_key = "WIFI_STA_DEF",
.if_desc = "sta",
.route_prio = 100
};
const esp_netif_inherent_config_t _g_esp_netif_inherent_sta_config = ESP_NETIF_INHERENT_DEFAULT_WIFI_STA();
static const esp_netif_ip_info_t soft_ap_ip = {
const esp_netif_inherent_config_t _g_esp_netif_inherent_ap_config = ESP_NETIF_INHERENT_DEFAULT_WIFI_AP();
const esp_netif_inherent_config_t _g_esp_netif_inherent_eth_config = ESP_NETIF_INHERENT_DEFAULT_ETH();
const esp_netif_inherent_config_t _g_esp_netif_inherent_ppp_config = ESP_NETIF_INHERENT_DEFAULT_PPP();
const esp_netif_ip_info_t _g_esp_netif_soft_ap_ip = {
.ip = { .addr = IP4TOADDR( 192, 168, 4, 1) },
.gw = { .addr = IP4TOADDR( 192, 168, 4, 1) },
.netmask = { .addr = IP4TOADDR( 255, 255, 255, 0) },
};
const esp_netif_inherent_config_t _g_esp_netif_inherent_ap_config = {
.flags = ESP_NETIF_DHCP_SERVER | ESP_NETIF_FLAG_AUTOUP,
.ip_info = (esp_netif_ip_info_t*)&soft_ap_ip,
.if_key = "WIFI_AP_DEF",
.if_desc = "ap",
.route_prio = 10
};
const esp_netif_inherent_config_t _g_esp_netif_inherent_eth_config = {
.get_ip_event = IP_EVENT_ETH_GOT_IP,
.lost_ip_event = 0,
.flags = ESP_NETIF_DHCP_CLIENT | ESP_NETIF_FLAG_GARP | ESP_NETIF_FLAG_EVENT_IP_MODIFIED,
.if_key = "ETH_DEF",
.if_desc = "eth",
.route_prio = 50
};
const esp_netif_inherent_config_t _g_esp_netif_inherent_ppp_config = {
.flags = ESP_NETIF_FLAG_IS_PPP,
.lost_ip_event = IP_EVENT_PPP_LOST_IP,
.get_ip_event = IP_EVENT_PPP_GOT_IP,
.if_key = "PPP_DEF",
.if_desc = "ppp",
.route_prio = 128
};

View file

@ -19,10 +19,67 @@
extern "C" {
#endif
//
// Designated initializers work differently under C++ and C compiler
//
#ifdef __cplusplus
#define _ESP_NETIF_EMPTY_AGGREGATE_MEMBER(item) .item = { },
#else
#define _ESP_NETIF_EMPTY_AGGREGATE_MEMBER(item)
#endif
//
// Macros to assemble master configs with partial configs from netif, stack and driver
//
#define ESP_NETIF_INHERENT_DEFAULT_WIFI_STA() \
{ \
.flags = (esp_netif_flags_t)(ESP_NETIF_DHCP_CLIENT | ESP_NETIF_FLAG_GARP | ESP_NETIF_FLAG_EVENT_IP_MODIFIED), \
_ESP_NETIF_EMPTY_AGGREGATE_MEMBER(mac) \
_ESP_NETIF_EMPTY_AGGREGATE_MEMBER(ip_info) \
.get_ip_event = IP_EVENT_STA_GOT_IP, \
.lost_ip_event = IP_EVENT_STA_LOST_IP, \
.if_key = "WIFI_STA_DEF", \
.if_desc = "sta", \
.route_prio = 100 \
} \
#define ESP_NETIF_INHERENT_DEFAULT_WIFI_AP() \
{ \
.flags = (esp_netif_flags_t)(ESP_NETIF_DHCP_SERVER | ESP_NETIF_FLAG_AUTOUP), \
_ESP_NETIF_EMPTY_AGGREGATE_MEMBER(mac) \
.ip_info = &_g_esp_netif_soft_ap_ip, \
.get_ip_event = 0, \
.lost_ip_event = 0, \
.if_key = "WIFI_AP_DEF", \
.if_desc = "ap", \
.route_prio = 10 \
};
#define ESP_NETIF_INHERENT_DEFAULT_ETH() \
{ \
.flags = (esp_netif_flags_t)(ESP_NETIF_DHCP_CLIENT | ESP_NETIF_FLAG_GARP | ESP_NETIF_FLAG_EVENT_IP_MODIFIED), \
_ESP_NETIF_EMPTY_AGGREGATE_MEMBER(mac) \
_ESP_NETIF_EMPTY_AGGREGATE_MEMBER(ip_info) \
.get_ip_event = IP_EVENT_ETH_GOT_IP, \
.lost_ip_event = 0, \
.if_key = "ETH_DEF", \
.if_desc = "eth", \
.route_prio = 50 \
};
#define ESP_NETIF_INHERENT_DEFAULT_PPP() \
{ \
.flags = ESP_NETIF_FLAG_IS_PPP, \
_ESP_NETIF_EMPTY_AGGREGATE_MEMBER(mac) \
_ESP_NETIF_EMPTY_AGGREGATE_MEMBER(ip_info) \
.get_ip_event = IP_EVENT_PPP_GOT_IP, \
.lost_ip_event = IP_EVENT_PPP_LOST_IP, \
.if_key = "PPP_DEF", \
.if_desc = "ppp", \
.route_prio = 128 \
};
/**
* @brief Default configuration reference of ethernet interface
*/
@ -37,7 +94,7 @@ extern "C" {
* @brief Default configuration reference of WIFI AP
*/
#define ESP_NETIF_DEFAULT_WIFI_AP() \
{ \
{ \
.base = ESP_NETIF_BASE_DEFAULT_WIFI_AP, \
.driver = NULL, \
.stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP, \
@ -109,6 +166,8 @@ extern const esp_netif_inherent_config_t _g_esp_netif_inherent_ap_config;
extern const esp_netif_inherent_config_t _g_esp_netif_inherent_eth_config;
extern const esp_netif_inherent_config_t _g_esp_netif_inherent_ppp_config;
extern const esp_netif_ip_info_t _g_esp_netif_soft_ap_ip;
#ifdef __cplusplus
}
#endif

View file

@ -155,7 +155,7 @@ typedef enum esp_netif_ip_event_type {
typedef struct esp_netif_inherent_config {
esp_netif_flags_t flags; /*!< flags that define esp-netif behavior */
uint8_t mac[6]; /*!< initial mac address for this interface */
esp_netif_ip_info_t* ip_info; /*!< initial ip address for this interface */
const esp_netif_ip_info_t* ip_info; /*!< initial ip address for this interface */
uint32_t get_ip_event; /*!< event id to be raised when interface gets an IP */
uint32_t lost_ip_event; /*!< event id to be raised when interface losts its IP */
const char * if_key; /*!< string identifier of the interface */

View file

@ -81,6 +81,18 @@ esp_netif_t* esp_netif_create_default_wifi_ap(void);
*/
esp_netif_t* esp_netif_create_default_wifi_sta(void);
/**
* @brief Creates esp_netif WiFi object based on the custom configuration.
*
* @attention This API DOES NOT register default handlers!
*
* @param[in] wifi_if type of wifi interface
* @param[in] esp_netif_config inherent esp-netif configuration pointer
*
* @return pointer to esp-netif instance
*/
esp_netif_t* esp_netif_create_wifi(wifi_interface_t wifi_if, esp_netif_inherent_config_t *esp_netif_config);
/**
* @brief Creates default STA and AP network interfaces for esp-mesh.
*

View file

@ -272,22 +272,23 @@ static esp_err_t create_and_attach(wifi_interface_t wifi_if, esp_netif_t* esp_ne
return esp_netif_attach(esp_netif, driver);
}
esp_err_t esp_netif_attach_wifi_station(esp_netif_t *esp_netif)
static inline esp_err_t esp_netif_attach_wifi(esp_netif_t *esp_netif, wifi_interface_t wifi_if)
{
if (esp_netif == NULL) {
if (esp_netif == NULL || (wifi_if != WIFI_IF_STA && wifi_if != WIFI_IF_AP)) {
return ESP_ERR_INVALID_ARG;
}
s_wifi_netifs[WIFI_IF_STA] = esp_netif;
return create_and_attach(WIFI_IF_STA, esp_netif);
s_wifi_netifs[wifi_if] = esp_netif;
return create_and_attach(wifi_if, esp_netif);
}
esp_err_t esp_netif_attach_wifi_station(esp_netif_t *esp_netif)
{
return esp_netif_attach_wifi(esp_netif, WIFI_IF_STA);
}
esp_err_t esp_netif_attach_wifi_ap(esp_netif_t *esp_netif)
{
if (esp_netif == NULL) {
return ESP_ERR_INVALID_ARG;
}
s_wifi_netifs[WIFI_IF_AP] = esp_netif;
return create_and_attach(WIFI_IF_AP, esp_netif);
return esp_netif_attach_wifi(esp_netif, WIFI_IF_AP);
}
@ -321,6 +322,28 @@ esp_netif_t* esp_netif_create_default_wifi_sta(void)
return netif;
}
/**
* @brief User init custom wifi interface
*/
esp_netif_t* esp_netif_create_wifi(wifi_interface_t wifi_if, esp_netif_inherent_config_t *esp_netif_config)
{
esp_netif_config_t cfg = {
.base = esp_netif_config
};
if (wifi_if == WIFI_IF_STA) {
cfg.stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA;
} else if (wifi_if == WIFI_IF_AP) {
cfg.stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP;
} else {
return NULL;
}
esp_netif_t *netif = esp_netif_new(&cfg);
assert(netif);
esp_netif_attach_wifi(netif, wifi_if);
return netif;
}
/**
* @brief Creates mesh network interfaces based on default STA and AP,
* but without DHCP, this is to be enabled separately only on root node