diff --git a/components/esp_netif/esp_netif_defaults.c b/components/esp_netif/esp_netif_defaults.c index 39278857fb..4dd497648a 100644 --- a/components/esp_netif/esp_netif_defaults.c +++ b/components/esp_netif/esp_netif_defaults.c @@ -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 }; diff --git a/components/esp_netif/include/esp_netif_defaults.h b/components/esp_netif/include/esp_netif_defaults.h index d03c45965a..71f861abd3 100644 --- a/components/esp_netif/include/esp_netif_defaults.h +++ b/components/esp_netif/include/esp_netif_defaults.h @@ -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 diff --git a/components/esp_netif/include/esp_netif_types.h b/components/esp_netif/include/esp_netif_types.h index 9c785ad883..242ce53c36 100644 --- a/components/esp_netif/include/esp_netif_types.h +++ b/components/esp_netif/include/esp_netif_types.h @@ -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 */ diff --git a/components/esp_wifi/include/esp_wifi_default.h b/components/esp_wifi/include/esp_wifi_default.h index 614a4ff9fd..4ce7f8b28e 100644 --- a/components/esp_wifi/include/esp_wifi_default.h +++ b/components/esp_wifi/include/esp_wifi_default.h @@ -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. * diff --git a/components/esp_wifi/src/wifi_default.c b/components/esp_wifi/src/wifi_default.c index d5b1c89299..7fb4b91142 100644 --- a/components/esp_wifi/src/wifi_default.c +++ b/components/esp_wifi/src/wifi_default.c @@ -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