From 7d5ae1ee263d654512af2886366cd3a2922a5843 Mon Sep 17 00:00:00 2001 From: liuhan Date: Wed, 14 Apr 2021 19:57:44 +0800 Subject: [PATCH] esp_netif: Add CONFIG_LWIP_DHCPS to sperate the code --- components/esp_netif/lwip/esp_netif_lwip.c | 35 ++++++++++++++++++- components/lwip/CMakeLists.txt | 5 ++- components/lwip/Kconfig | 9 +++++ components/lwip/component.mk | 4 +++ components/lwip/port/esp32/include/lwipopts.h | 6 ++++ 5 files changed, 57 insertions(+), 2 deletions(-) diff --git a/components/esp_netif/lwip/esp_netif_lwip.c b/components/esp_netif/lwip/esp_netif_lwip.c index ae3f16854a..7d0aeaf7e0 100644 --- a/components/esp_netif/lwip/esp_netif_lwip.c +++ b/components/esp_netif/lwip/esp_netif_lwip.c @@ -72,6 +72,16 @@ return esp_netif_lwip_ipc_call(function, netif, (void *)(param)); \ } +/** + * @brief If netif protocol not enabled in menuconfig, log the error and return appropriate code indicating failure +*/ + +#define LOG_NETIF_DISABLED_AND_DO(proto, action) \ +do { \ + ESP_LOGE(TAG, "%s not supported, please enable it in lwIP component configuration", proto); \ + action; \ +} while(0) + // // Internal types // @@ -630,7 +640,7 @@ esp_err_t esp_netif_get_mac(esp_netif_t *esp_netif, uint8_t mac[]) return ESP_OK; } - +#if ESP_DHCPS static void esp_netif_dhcps_cb(u8_t client_ip[4]) { ESP_LOGI(TAG, "DHCP server assigned IP to a station, IP is: %d.%d.%d.%d", @@ -644,6 +654,7 @@ static void esp_netif_dhcps_cb(u8_t client_ip[4]) ESP_LOGE(TAG, "dhcps cb: failed to post IP_EVENT_AP_STAIPASSIGNED (%x)", ret); } } +#endif static esp_err_t esp_netif_config_sanity_check(const esp_netif_t * esp_netif) { @@ -693,6 +704,7 @@ static esp_err_t esp_netif_start_api(esp_netif_api_msg_t *msg) netif_set_up(p_netif); } if (esp_netif->flags & ESP_NETIF_DHCP_SERVER) { +#if ESP_DHCPS if (esp_netif->dhcps_status != ESP_NETIF_DHCP_STARTED) { if (p_netif != NULL && netif_is_up(p_netif)) { esp_netif_ip_info_t *default_ip = esp_netif->ip_info; @@ -715,6 +727,9 @@ static esp_err_t esp_netif_start_api(esp_netif_api_msg_t *msg) } ESP_LOGD(TAG, "DHCP server already started"); return ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED; +#else + LOG_NETIF_DISABLED_AND_DO("DHCP Server", return ESP_ERR_NOT_SUPPORTED); +#endif } else if (esp_netif->flags & ESP_NETIF_DHCP_CLIENT) { if (esp_netif->dhcpc_status != ESP_NETIF_DHCP_STARTED) { if (p_netif != NULL) { @@ -764,10 +779,14 @@ static esp_err_t esp_netif_stop_api(esp_netif_api_msg_t *msg) } if (esp_netif->flags & ESP_NETIF_DHCP_SERVER) { +#if ESP_DHCPS dhcps_stop(lwip_netif); // TODO(IDF-1099): dhcps checks status by its self if (ESP_NETIF_DHCP_STOPPED != esp_netif->dhcps_status) { esp_netif->dhcps_status = ESP_NETIF_DHCP_INIT; } +#else + LOG_NETIF_DISABLED_AND_DO("DHCP Server", return ESP_ERR_NOT_SUPPORTED); +#endif } else if (esp_netif->flags & ESP_NETIF_DHCP_CLIENT) { dhcp_release(lwip_netif); dhcp_stop(lwip_netif); @@ -1052,6 +1071,7 @@ static esp_err_t esp_netif_dhcpc_start_api(esp_netif_api_msg_t *msg) esp_err_t esp_netif_dhcpc_start(esp_netif_t *esp_netif) _RUN_IN_LWIP_TASK_IF_SUPPORTED(esp_netif_dhcpc_start_api, esp_netif, NULL) +#if ESP_DHCPS esp_err_t esp_netif_dhcps_get_status(esp_netif_t *esp_netif, esp_netif_dhcp_status_t *status) { if (!esp_netif || (esp_netif->flags & ESP_NETIF_DHCP_CLIENT) || _IS_NETIF_ANY_POINT2POINT_TYPE(esp_netif)) { @@ -1061,6 +1081,7 @@ esp_err_t esp_netif_dhcps_get_status(esp_netif_t *esp_netif, esp_netif_dhcp_stat *status = esp_netif->dhcps_status; return ESP_OK; } +#endif esp_err_t esp_netif_dhcpc_get_status(esp_netif_t *esp_netif, esp_netif_dhcp_status_t *status) { @@ -1072,6 +1093,7 @@ esp_err_t esp_netif_dhcpc_get_status(esp_netif_t *esp_netif, esp_netif_dhcp_stat return ESP_OK; } +#if ESP_DHCPS static esp_err_t esp_netif_dhcps_start_api(esp_netif_api_msg_t *msg) { esp_netif_t *esp_netif = msg->esp_netif; @@ -1138,6 +1160,7 @@ static esp_err_t esp_netif_dhcps_stop_api(esp_netif_api_msg_t *msg) } esp_err_t esp_netif_dhcps_stop(esp_netif_t *esp_netif) _RUN_IN_LWIP_TASK_IF_SUPPORTED(esp_netif_dhcps_stop_api, esp_netif, NULL) +#endif static esp_err_t esp_netif_set_hostname_api(esp_netif_api_msg_t *msg) { @@ -1432,6 +1455,7 @@ static esp_err_t esp_netif_set_dns_info_api(esp_netif_api_msg_t *msg) lwip_ip->type = IPADDR_TYPE_V4; #endif if (esp_netif->flags & ESP_NETIF_DHCP_SERVER) { +#if ESP_DHCPS // if DHCP server configured to set DNS in dhcps API if (type != ESP_NETIF_DNS_MAIN) { ESP_LOGD(TAG, "set dns invalid type"); @@ -1439,6 +1463,9 @@ static esp_err_t esp_netif_set_dns_info_api(esp_netif_api_msg_t *msg) } else { dhcps_dns_setserver(lwip_ip); } +#else + LOG_NETIF_DISABLED_AND_DO("DHCP Server", return ESP_ERR_NOT_SUPPORTED); +#endif } else { dns_setserver(type, lwip_ip); } @@ -1473,8 +1500,12 @@ static esp_err_t esp_netif_get_dns_info_api(esp_netif_api_msg_t *msg) } if (esp_netif->flags & ESP_NETIF_DHCP_SERVER) { +#if ESP_DHCPS ip4_addr_t dns_ip = dhcps_dns_getserver(); memcpy(&dns->ip.u_addr.ip4, &dns_ip, sizeof(ip4_addr_t)); +#else + LOG_NETIF_DISABLED_AND_DO("DHCP Server", return ESP_ERR_NOT_SUPPORTED); +#endif } else { const ip_addr_t* dns_ip = NULL; dns_ip = dns_getserver(type); @@ -1672,6 +1703,7 @@ int32_t esp_netif_get_event_id(esp_netif_t *esp_netif, esp_netif_ip_event_type_t } } +#if ESP_DHCPS esp_err_t esp_netif_dhcps_option(esp_netif_t *esp_netif, esp_netif_dhcp_option_mode_t opt_op, esp_netif_dhcp_option_id_t opt_id, void *opt_val, uint32_t opt_len) { @@ -1799,6 +1831,7 @@ esp_err_t esp_netif_dhcps_option(esp_netif_t *esp_netif, esp_netif_dhcp_option_m return ESP_OK; } +#endif esp_err_t esp_netif_dhcpc_option(esp_netif_t *esp_netif, esp_netif_dhcp_option_mode_t opt_op, esp_netif_dhcp_option_id_t opt_id, void *opt_val, uint32_t opt_len) diff --git a/components/lwip/CMakeLists.txt b/components/lwip/CMakeLists.txt index 3348838c83..acd587de92 100644 --- a/components/lwip/CMakeLists.txt +++ b/components/lwip/CMakeLists.txt @@ -7,7 +7,6 @@ set(include_dirs ) set(srcs - "apps/dhcpserver/dhcpserver.c" "apps/sntp/sntp.c" "lwip/src/api/api_lib.c" "lwip/src/api/api_msg.c" @@ -145,6 +144,10 @@ if(CONFIG_LWIP_ICMP) "apps/ping/ping_sock.c") endif() +if(CONFIG_LWIP_DHCPS) + list(APPEND srcs "apps/dhcpserver/dhcpserver.c") +endif() + idf_component_register(SRCS "${srcs}" INCLUDE_DIRS "${include_dirs}" LDFRAGMENTS linker.lf diff --git a/components/lwip/Kconfig b/components/lwip/Kconfig index 2aeb27b87f..ebaa8f7c7a 100644 --- a/components/lwip/Kconfig +++ b/components/lwip/Kconfig @@ -258,10 +258,18 @@ menu "LWIP" menu "DHCP server" + config LWIP_DHCPS + bool "DHCPS: Enable IPv4 Dynamic Host Configuration Protocol Server (DHCPS)" + default y + help + Enabling this option allows the device to run the DHCP server + (to dynamically assign IPv4 addresses to clients). + config LWIP_DHCPS_LEASE_UNIT int "Multiplier for lease time, in seconds" range 1 3600 default 60 + depends on LWIP_DHCPS help The DHCP server is calculating lease time multiplying the sent and received times by this number of seconds per unit. @@ -271,6 +279,7 @@ menu "LWIP" int "Maximum number of stations" range 1 64 default 8 + depends on LWIP_DHCPS help The maximum number of DHCP clients that are connected to the server. After this number is exceeded, DHCP server removes of the oldest device diff --git a/components/lwip/component.mk b/components/lwip/component.mk index 56610098e1..950860e3ae 100644 --- a/components/lwip/component.mk +++ b/components/lwip/component.mk @@ -47,6 +47,10 @@ ifdef CONFIG_LWIP_PPP_SUPPORT COMPONENT_SRCDIRS += lwip/src/netif/ppp lwip/src/netif/ppp/polarssl endif +ifndef CONFIG_LWIP_DHCPS + COMPONENT_OBJEXCLUDE += apps/dhcpserver/dhcpserver.o +endif + CFLAGS += -Wno-address # lots of LWIP source files evaluate macros that check address of stack variables lwip/src/netif/ppp/ppp.o: CFLAGS += -Wno-uninitialized diff --git a/components/lwip/port/esp32/include/lwipopts.h b/components/lwip/port/esp32/include/lwipopts.h index aa12738f58..97afe316ad 100644 --- a/components/lwip/port/esp32/include/lwipopts.h +++ b/components/lwip/port/esp32/include/lwipopts.h @@ -986,7 +986,13 @@ #define ESP_STATS_MEM CONFIG_LWIP_STATS #define ESP_STATS_DROP CONFIG_LWIP_STATS #define ESP_STATS_TCP 0 +#ifdef CONFIG_LWIP_DHCPS +#define ESP_DHCPS 1 #define ESP_DHCPS_TIMER 1 +#else +#define ESP_DHCPS 0 +#define ESP_DHCPS_TIMER 0 +#endif /* CONFIG_LWIP_DHCPS */ #define ESP_LWIP_LOGI(...) ESP_LOGI("lwip", __VA_ARGS__) #define ESP_PING 1 #define ESP_HAS_SELECT 1