diff --git a/components/esp_wifi/include/esp_wifi_default.h b/components/esp_wifi/include/esp_wifi_default.h index a6fb03014f..fc6af5441e 100644 --- a/components/esp_wifi/include/esp_wifi_default.h +++ b/components/esp_wifi/include/esp_wifi_default.h @@ -77,4 +77,22 @@ esp_netif_t* esp_netif_create_default_wifi_ap(void); */ esp_netif_t* esp_netif_create_default_wifi_sta(void); +/** + * @brief Creates default STA and AP network interfaces for esp-mesh. + * + * Both netifs are almost identical to the default station and softAP, but with + * DHCP client and server disabled. Please note that the DHCP client is typically + * enabled only if the device is promoted to a root node. + * + * Returns created interfaces which could be ignored setting parameters to NULL + * if an application code does not need to save the interface instances + * for further processing. + * + * @param[out] p_netif_sta pointer where the resultant STA interface is saved (if non NULL) + * @param[out] p_netif_ap pointer where the resultant AP interface is saved (if non NULL) + * + * @return ESP_OK on success + */ +esp_err_t esp_netif_create_default_wifi_mesh_netifs(esp_netif_t **p_netif_sta, esp_netif_t **p_netif_ap); + #endif //_ESP_WIFI_DEFAULT_H diff --git a/components/esp_wifi/src/wifi_default.c b/components/esp_wifi/src/wifi_default.c index 6301cc2030..8922ffe32b 100644 --- a/components/esp_wifi/src/wifi_default.c +++ b/components/esp_wifi/src/wifi_default.c @@ -16,6 +16,7 @@ #include "esp_log.h" #include "esp_private/wifi.h" #include "esp_wifi_netif.h" +#include // // Purpose of this module is to provide basic wifi initialization setup for @@ -318,4 +319,50 @@ esp_netif_t* esp_netif_create_default_wifi_sta(void) esp_netif_attach_wifi_station(netif); esp_wifi_set_default_wifi_sta_handlers(); 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 + */ +esp_err_t esp_netif_create_default_wifi_mesh_netifs(esp_netif_t **p_netif_sta, esp_netif_t **p_netif_ap) +{ + // Create "almost" default AP, with un-flagged DHCP server + esp_netif_inherent_config_t netif_cfg; + memcpy(&netif_cfg, ESP_NETIF_BASE_DEFAULT_WIFI_AP, sizeof(netif_cfg)); + netif_cfg.flags &= ~ESP_NETIF_DHCPS; + esp_netif_config_t cfg_ap = { + .base = &netif_cfg, + .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP, + }; + esp_netif_t *netif_ap = esp_netif_new(&cfg_ap); + assert(netif_ap); + ESP_ERROR_CHECK(esp_netif_attach_wifi_ap(netif_ap)); + ESP_ERROR_CHECK(esp_wifi_set_default_wifi_ap_handlers()); + + // ...and stop DHCP server to be compatible with former tcpip_adapter (to keep the ESP_NETIF_DHCP_STOPPED state) + ESP_ERROR_CHECK(esp_netif_dhcps_stop(netif_ap)); + + // Create "almost" default station, but with un-flagged DHCP client + memcpy(&netif_cfg, ESP_NETIF_BASE_DEFAULT_WIFI_STA, sizeof(netif_cfg)); + netif_cfg.flags &= ~ESP_NETIF_DHCPC; + esp_netif_config_t cfg_sta = { + .base = &netif_cfg, + .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA, + }; + esp_netif_t *netif_sta = esp_netif_new(&cfg_sta); + assert(netif_sta); + ESP_ERROR_CHECK(esp_netif_attach_wifi_station(netif_sta)); + ESP_ERROR_CHECK(esp_wifi_set_default_wifi_sta_handlers()); + + // ...and stop DHCP client (to be started separately if the station were promoted to root) + ESP_ERROR_CHECK(esp_netif_dhcpc_stop(netif_sta)); + + if (p_netif_sta) { + *p_netif_sta = netif_sta; + } + if (p_netif_ap) { + *p_netif_ap = netif_ap; + } + return ESP_OK; } \ No newline at end of file diff --git a/examples/mesh/internal_communication/main/mesh_main.c b/examples/mesh/internal_communication/main/mesh_main.c index 2c29d51e4e..46fff40af7 100644 --- a/examples/mesh/internal_communication/main/mesh_main.c +++ b/examples/mesh/internal_communication/main/mesh_main.c @@ -372,38 +372,6 @@ void ip_event_handler(void *arg, esp_event_base_t event_base, } -/** - * @brief Creates mesh network interfaces based on default STA and AP, - * but without DHCP, this is to be enabled separately only on root node - */ -static esp_err_t create_wifi_netifs(void) -{ - esp_netif_inherent_config_t netif_cfg; - memcpy(&netif_cfg, ESP_NETIF_BASE_DEFAULT_WIFI_AP, sizeof(netif_cfg)); - netif_cfg.flags &= ~ESP_NETIF_DHCPS; - esp_netif_config_t cfg_ap = { - .base = &netif_cfg, - .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP, - }; - esp_netif_t *netif_ap = esp_netif_new(&cfg_ap); - assert(netif_ap); - ESP_ERROR_CHECK(esp_netif_attach_wifi_ap(netif_ap)); - ESP_ERROR_CHECK(esp_wifi_set_default_wifi_sta_handlers()); - - memcpy(&netif_cfg, ESP_NETIF_BASE_DEFAULT_WIFI_STA, sizeof(netif_cfg)); - netif_cfg.flags &= ~ESP_NETIF_DHCPC; - esp_netif_config_t cfg_sta = { - .base = &netif_cfg, - .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA, - }; - - netif_sta = esp_netif_new(&cfg_sta); - assert(netif_sta); - ESP_ERROR_CHECK(esp_netif_attach_wifi_station(netif_sta)); - ESP_ERROR_CHECK(esp_wifi_set_default_wifi_sta_handlers()); - return ESP_OK; -} - void app_main(void) { ESP_ERROR_CHECK(mesh_light_init()); @@ -412,8 +380,8 @@ void app_main(void) esp_netif_init(); /* event initialization */ ESP_ERROR_CHECK(esp_event_loop_create_default()); - /* crete network interfaces for mesh */ - ESP_ERROR_CHECK(create_wifi_netifs()); + /* crete network interfaces for mesh (only station instance saved for further manipulation, soft AP instance ignored */ + ESP_ERROR_CHECK(esp_netif_create_default_wifi_mesh_netifs(&netif_sta, NULL)); /* wifi initialization */ wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&config)); diff --git a/examples/mesh/manual_networking/main/mesh_main.c b/examples/mesh/manual_networking/main/mesh_main.c index bb203b67ee..9ffc971a3b 100644 --- a/examples/mesh/manual_networking/main/mesh_main.c +++ b/examples/mesh/manual_networking/main/mesh_main.c @@ -296,38 +296,6 @@ void ip_event_handler(void *arg, esp_event_base_t event_base, ESP_LOGI(MESH_TAG, "IP:" IPSTR, IP2STR(&event->ip_info.ip)); } -/** - * @brief Creates mesh network interfaces based on default STA and AP, - * but without DHCP, this is to be enabled separately only on root node - */ -static esp_err_t create_wifi_netifs(void) -{ - esp_netif_inherent_config_t netif_cfg; - memcpy(&netif_cfg, ESP_NETIF_BASE_DEFAULT_WIFI_AP, sizeof(netif_cfg)); - netif_cfg.flags &= ~ESP_NETIF_DHCPS; - esp_netif_config_t cfg_ap = { - .base = &netif_cfg, - .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP, - }; - esp_netif_t *netif_ap = esp_netif_new(&cfg_ap); - assert(netif_ap); - ESP_ERROR_CHECK(esp_netif_attach_wifi_ap(netif_ap)); - ESP_ERROR_CHECK(esp_wifi_set_default_wifi_sta_handlers()); - - memcpy(&netif_cfg, ESP_NETIF_BASE_DEFAULT_WIFI_STA, sizeof(netif_cfg)); - netif_cfg.flags &= ~ESP_NETIF_DHCPC; - esp_netif_config_t cfg_sta = { - .base = &netif_cfg, - .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA, - }; - - netif_sta = esp_netif_new(&cfg_sta); - assert(netif_sta); - ESP_ERROR_CHECK(esp_netif_attach_wifi_station(netif_sta)); - ESP_ERROR_CHECK(esp_wifi_set_default_wifi_sta_handlers()); - return ESP_OK; -} - void app_main(void) { ESP_ERROR_CHECK(mesh_light_init()); @@ -336,8 +304,8 @@ void app_main(void) esp_netif_init(); /* event initialization */ ESP_ERROR_CHECK(esp_event_loop_create_default()); - /* crete network interfaces for mesh */ - ESP_ERROR_CHECK(create_wifi_netifs()); + /* crete network interfaces for mesh (only station instance saved for further manipulation, soft AP instance ignored */ + ESP_ERROR_CHECK(esp_netif_create_default_wifi_mesh_netifs(&netif_sta, NULL)); /* wifi initialization */ wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&config));