From 356bc603c4647323cd1f3ec8361bd719525829d9 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 23 Mar 2022 19:20:06 +0100 Subject: [PATCH] lwip: Support DHCP restore last IP --- components/esp_netif/lwip/esp_netif_lwip.c | 9 +- .../port/esp32/include/lwip_default_hooks.h | 7 +- components/lwip/port/esp32/include/lwipopts.h | 19 +++-- .../port/esp32/include/netif/dhcp_state.h | 14 +-- components/lwip/port/esp32/netif/dhcp_state.c | 85 ++++++++----------- tools/ci/check_copyright_ignore.txt | 1 - 6 files changed, 66 insertions(+), 69 deletions(-) diff --git a/components/esp_netif/lwip/esp_netif_lwip.c b/components/esp_netif/lwip/esp_netif_lwip.c index 7fe1d2d67a..783ea1b81a 100644 --- a/components/esp_netif/lwip/esp_netif_lwip.c +++ b/components/esp_netif/lwip/esp_netif_lwip.c @@ -38,7 +38,7 @@ #include "esp_netif_lwip_slip.h" #include "dhcpserver/dhcpserver.h" #include "dhcpserver/dhcpserver_options.h" - +#include "netif/dhcp_state.h" #include "esp_event.h" #include "esp_log.h" @@ -987,6 +987,9 @@ void esp_netif_internal_dhcpc_cb(struct netif *netif) if (ESP_OK != ret) { ESP_LOGE(TAG, "dhcpc cb: failed to post got ip event (%x)", ret); } +#ifdef CONFIG_LWIP_DHCP_RESTORE_LAST_IP + dhcp_ip_addr_store(netif); +#endif /* CONFIG_LWIP_DHCP_RESTORE_LAST_IP */ } else { ESP_LOGD(TAG, "if%p ip unchanged", esp_netif); } @@ -1089,7 +1092,9 @@ static esp_err_t esp_netif_dhcpc_stop_api(esp_netif_api_msg_t *msg) ESP_LOGD(TAG, "dhcp client stop successfully"); esp_netif->dhcpc_status = ESP_NETIF_DHCP_STOPPED; - LWIP_DHCP_IP_ADDR_ERASE(esp_netif); +#ifdef CONFIG_LWIP_DHCP_RESTORE_LAST_IP + dhcp_ip_addr_erase(esp_netif->lwip_netif); +#endif /* CONFIG_LWIP_DHCP_RESTORE_LAST_IP */ return ESP_OK; } diff --git a/components/lwip/port/esp32/include/lwip_default_hooks.h b/components/lwip/port/esp32/include/lwip_default_hooks.h index 82f30c8bbb..1577b60e6b 100644 --- a/components/lwip/port/esp32/include/lwip_default_hooks.h +++ b/components/lwip/port/esp32/include/lwip_default_hooks.h @@ -4,12 +4,13 @@ * SPDX-License-Identifier: Apache-2.0 */ -#ifndef _LWIP_DEFAULT_HOOKS_H_ -#define _LWIP_DEFAULT_HOOKS_H_ +#ifndef LWIP_ESP_DEFAULT_HOOKS_H_ +#define LWIP_ESP_DEFAULT_HOOKS_H_ #include "lwip/ip_addr.h" #include "lwip/arch.h" #include "lwip/err.h" #include "lwip/pbuf.h" +#include "netif/dhcp_state.h" #ifdef ESP_IDF_LWIP_HOOK_FILENAME #include ESP_IDF_LWIP_HOOK_FILENAME @@ -60,4 +61,4 @@ ip4_route_src_hook(const ip4_addr_t *src,const ip4_addr_t *dest); } #endif -#endif /* _LWIP_DEFAULT_HOOKS_H_ */ +#endif /* LWIP_ESP_DEFAULT_HOOKS_H_ */ diff --git a/components/lwip/port/esp32/include/lwipopts.h b/components/lwip/port/esp32/include/lwipopts.h index 362283ad20..7e6bc365c3 100644 --- a/components/lwip/port/esp32/include/lwipopts.h +++ b/components/lwip/port/esp32/include/lwipopts.h @@ -24,7 +24,6 @@ #include "esp_random.h" #endif // __linux__ #include "sdkconfig.h" -#include "netif/dhcp_state.h" #include "sntp/sntp_get_set_time.h" #include "sockets_ext.h" @@ -304,13 +303,19 @@ * is restored after reset/power-up. */ #ifdef CONFIG_LWIP_DHCP_RESTORE_LAST_IP -#define LWIP_DHCP_IP_ADDR_RESTORE() dhcp_ip_addr_restore(netif) -#define LWIP_DHCP_IP_ADDR_STORE() dhcp_ip_addr_store(netif) -#define LWIP_DHCP_IP_ADDR_ERASE(esp_netif) dhcp_ip_addr_erase(esp_netif) +/* + * Make the post-init hook check if we could restore the previously bound address + * - if yes reset the state to bound and mark result as ERR_OK (which skips discovery state) + * - if no, return false to continue normally to the discovery state + */ +#define LWIP_HOOK_DHCP_POST_INIT(netif, result) \ + (dhcp_ip_addr_restore(netif) ? ( dhcp_set_state(dhcp, DHCP_STATE_BOUND), \ + dhcp_network_changed(netif), \ + (result) = ERR_OK , \ + true ) : \ + false) #else -#define LWIP_DHCP_IP_ADDR_RESTORE() 0 -#define LWIP_DHCP_IP_ADDR_STORE() -#define LWIP_DHCP_IP_ADDR_ERASE(esp_netif) +#define LWIP_HOOK_DHCP_PRE_DISCOVERY(netif, result) (false) #endif /* CONFIG_LWIP_DHCP_RESTORE_LAST_IP */ /** diff --git a/components/lwip/port/esp32/include/netif/dhcp_state.h b/components/lwip/port/esp32/include/netif/dhcp_state.h index 0c2536b01c..d8bf13bb94 100644 --- a/components/lwip/port/esp32/include/netif/dhcp_state.h +++ b/components/lwip/port/esp32/include/netif/dhcp_state.h @@ -4,9 +4,11 @@ * SPDX-License-Identifier: Apache-2.0 */ +#ifndef LWIP_ESP_DHCP_STATE_H +#define LWIP_ESP_DHCP_STATE_H -#ifndef _DHCP_STATE_H_ -#define _DHCP_STATE_H_ +#include +#include "lwip/netif.h" #include @@ -14,14 +16,14 @@ extern "C" { #endif -bool dhcp_ip_addr_restore(void *netif); +bool dhcp_ip_addr_restore(struct netif *netif); -void dhcp_ip_addr_store(void *netif); +void dhcp_ip_addr_store(struct netif *netif); -void dhcp_ip_addr_erase(void *esp_netif); +void dhcp_ip_addr_erase(struct netif *netif); #ifdef __cplusplus } #endif -#endif /* _DHCP_STATE_H_ */ +#endif /* LWIP_ESP_DHCP_STATE_H */ diff --git a/components/lwip/port/esp32/netif/dhcp_state.c b/components/lwip/port/esp32/netif/dhcp_state.c index 8785a6ded5..10b0981219 100644 --- a/components/lwip/port/esp32/netif/dhcp_state.c +++ b/components/lwip/port/esp32/netif/dhcp_state.c @@ -1,79 +1,64 @@ -// Copyright 2018 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at +/* + * SPDX-FileCopyrightText: 2018-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include -#include #include "nvs.h" -#include "lwip/opt.h" #include "lwip/dhcp.h" #include "lwip/netif.h" -#include "esp_interface.h" -#include "esp_netif.h" -#include "esp_netif_net_stack.h" #include "netif/dhcp_state.h" #define DHCP_NAMESPACE "dhcp_state" +#define IF_KEY_SIZE 3 -// DHCP_Client has to be enabled for this netif -#define VALID_NETIF_ID(netif) (ESP_NETIF_DHCP_CLIENT&esp_netif_get_flags(netif)) +/* + * As a NVS key, use string representation of the interface index number + */ +static inline char *gen_if_key(struct netif *netif, char *name) +{ + lwip_itoa(name, IF_KEY_SIZE, netif->num); + return name; +} -bool dhcp_ip_addr_restore(void *netif) +bool dhcp_ip_addr_restore(struct netif *netif) { nvs_handle_t nvs; + char if_key[IF_KEY_SIZE]; bool err = false; - struct netif *net = (struct netif *)netif; - struct dhcp *dhcp = netif_dhcp_data(net); - esp_netif_t *esp_netif = esp_netif_get_handle_from_netif_impl(netif); + struct dhcp *dhcp = netif_dhcp_data(netif); - if(VALID_NETIF_ID(esp_netif)) { - uint32_t *ip_addr = &dhcp->offered_ip_addr.addr; - if (nvs_open(DHCP_NAMESPACE, NVS_READONLY, &nvs) == ESP_OK) { - if (nvs_get_u32(nvs, esp_netif_get_ifkey(esp_netif), ip_addr) == ESP_OK) { - err = true; - } - nvs_close(nvs); + uint32_t *ip_addr = &dhcp->offered_ip_addr.addr; + if (nvs_open(DHCP_NAMESPACE, NVS_READONLY, &nvs) == ESP_OK) { + if (nvs_get_u32(nvs, gen_if_key(netif, if_key), ip_addr) == ESP_OK) { + err = true; } + nvs_close(nvs); } return err; } -void dhcp_ip_addr_store(void *netif) +void dhcp_ip_addr_store(struct netif *netif) { nvs_handle_t nvs; - struct netif *net = (struct netif *)netif; - struct dhcp *dhcp = netif_dhcp_data(net); + char if_key[IF_KEY_SIZE]; + struct dhcp *dhcp = netif_dhcp_data(netif); uint32_t ip_addr = dhcp->offered_ip_addr.addr; - esp_netif_t *esp_netif = esp_netif_get_handle_from_netif_impl(netif); - if(VALID_NETIF_ID(esp_netif)) { - if (nvs_open(DHCP_NAMESPACE, NVS_READWRITE, &nvs) == ESP_OK) { - nvs_set_u32(nvs,esp_netif_get_ifkey(esp_netif), ip_addr); - nvs_commit(nvs); - nvs_close(nvs); - } + if (nvs_open(DHCP_NAMESPACE, NVS_READWRITE, &nvs) == ESP_OK) { + nvs_set_u32(nvs, gen_if_key(netif, if_key), ip_addr); + nvs_commit(nvs); + nvs_close(nvs); } } -void dhcp_ip_addr_erase(void *esp_netif) +void dhcp_ip_addr_erase(struct netif *netif) { nvs_handle_t nvs; - - if(VALID_NETIF_ID(esp_netif)) { - if (nvs_open(DHCP_NAMESPACE, NVS_READWRITE, &nvs) == ESP_OK) { - nvs_erase_key(nvs, esp_netif_get_ifkey(esp_netif)); - nvs_commit(nvs); - nvs_close(nvs); - } + char if_key[IF_KEY_SIZE]; + if (nvs_open(DHCP_NAMESPACE, NVS_READWRITE, &nvs) == ESP_OK) { + nvs_erase_key(nvs, gen_if_key(netif, if_key)); + nvs_commit(nvs); + nvs_close(nvs); } } diff --git a/tools/ci/check_copyright_ignore.txt b/tools/ci/check_copyright_ignore.txt index a0d014044f..d4b3dfeb1b 100644 --- a/tools/ci/check_copyright_ignore.txt +++ b/tools/ci/check_copyright_ignore.txt @@ -979,7 +979,6 @@ components/lwip/port/esp32/include/netinet/in.h components/lwip/port/esp32/include/netinet/tcp.h components/lwip/port/esp32/include/sntp/sntp_get_set_time.h components/lwip/port/esp32/include/sys/socket.h -components/lwip/port/esp32/netif/dhcp_state.c components/lwip/port/esp32/netif/ethernetif.c components/lwip/port/esp32/netif/wlanif.c components/lwip/port/esp32/no_vfs_syscalls.c