From 424203e411de84cca982f9c179175ff7d46f22f3 Mon Sep 17 00:00:00 2001 From: liuhan Date: Fri, 16 Apr 2021 19:17:21 +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 a352f03df7..bfcce0fdda 100644 --- a/components/esp_netif/lwip/esp_netif_lwip.c +++ b/components/esp_netif/lwip/esp_netif_lwip.c @@ -70,6 +70,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 // @@ -628,7 +638,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", @@ -642,6 +652,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) { @@ -691,6 +702,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; @@ -713,6 +725,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) { @@ -762,10 +777,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); @@ -1050,6 +1069,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)) { @@ -1059,6 +1079,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) { @@ -1070,6 +1091,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; @@ -1136,6 +1158,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) { @@ -1430,6 +1453,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"); @@ -1437,6 +1461,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); } @@ -1471,8 +1498,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); @@ -1670,6 +1701,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) { @@ -1797,6 +1829,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 92ab45a9fd..8c59a8b6e0 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/ping/esp_ping.c" "apps/ping/ping.c" "apps/ping/ping_sock.c" @@ -137,6 +136,10 @@ else() list(APPEND srcs "port/esp32/no_vfs_syscalls.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 15845441f2..6f3914874a 100644 --- a/components/lwip/Kconfig +++ b/components/lwip/Kconfig @@ -249,10 +249,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. @@ -262,6 +270,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 6b9ca1df69..04a5dcdaa7 100644 --- a/components/lwip/component.mk +++ b/components/lwip/component.mk @@ -40,6 +40,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 d4ea532c38..db9f03beda 100644 --- a/components/lwip/port/esp32/include/lwipopts.h +++ b/components/lwip/port/esp32/include/lwipopts.h @@ -956,7 +956,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