lwip: Support DHCP restore last IP

This commit is contained in:
David Cermak
2022-03-23 19:20:06 +01:00
parent 7a04eb8d66
commit 356bc603c4
6 changed files with 66 additions and 69 deletions

View File

@@ -38,7 +38,7 @@
#include "esp_netif_lwip_slip.h" #include "esp_netif_lwip_slip.h"
#include "dhcpserver/dhcpserver.h" #include "dhcpserver/dhcpserver.h"
#include "dhcpserver/dhcpserver_options.h" #include "dhcpserver/dhcpserver_options.h"
#include "netif/dhcp_state.h"
#include "esp_event.h" #include "esp_event.h"
#include "esp_log.h" #include "esp_log.h"
@@ -987,6 +987,9 @@ void esp_netif_internal_dhcpc_cb(struct netif *netif)
if (ESP_OK != ret) { if (ESP_OK != ret) {
ESP_LOGE(TAG, "dhcpc cb: failed to post got ip event (%x)", 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 { } else {
ESP_LOGD(TAG, "if%p ip unchanged", esp_netif); 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_LOGD(TAG, "dhcp client stop successfully");
esp_netif->dhcpc_status = ESP_NETIF_DHCP_STOPPED; 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; return ESP_OK;
} }

View File

@@ -4,12 +4,13 @@
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
#ifndef _LWIP_DEFAULT_HOOKS_H_ #ifndef LWIP_ESP_DEFAULT_HOOKS_H_
#define _LWIP_DEFAULT_HOOKS_H_ #define LWIP_ESP_DEFAULT_HOOKS_H_
#include "lwip/ip_addr.h" #include "lwip/ip_addr.h"
#include "lwip/arch.h" #include "lwip/arch.h"
#include "lwip/err.h" #include "lwip/err.h"
#include "lwip/pbuf.h" #include "lwip/pbuf.h"
#include "netif/dhcp_state.h"
#ifdef ESP_IDF_LWIP_HOOK_FILENAME #ifdef ESP_IDF_LWIP_HOOK_FILENAME
#include 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
#endif /* _LWIP_DEFAULT_HOOKS_H_ */ #endif /* LWIP_ESP_DEFAULT_HOOKS_H_ */

View File

@@ -24,7 +24,6 @@
#include "esp_random.h" #include "esp_random.h"
#endif // __linux__ #endif // __linux__
#include "sdkconfig.h" #include "sdkconfig.h"
#include "netif/dhcp_state.h"
#include "sntp/sntp_get_set_time.h" #include "sntp/sntp_get_set_time.h"
#include "sockets_ext.h" #include "sockets_ext.h"
@@ -304,13 +303,19 @@
* is restored after reset/power-up. * is restored after reset/power-up.
*/ */
#ifdef CONFIG_LWIP_DHCP_RESTORE_LAST_IP #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) * Make the post-init hook check if we could restore the previously bound address
#define LWIP_DHCP_IP_ADDR_ERASE(esp_netif) dhcp_ip_addr_erase(esp_netif) * - 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 #else
#define LWIP_DHCP_IP_ADDR_RESTORE() 0 #define LWIP_HOOK_DHCP_PRE_DISCOVERY(netif, result) (false)
#define LWIP_DHCP_IP_ADDR_STORE()
#define LWIP_DHCP_IP_ADDR_ERASE(esp_netif)
#endif /* CONFIG_LWIP_DHCP_RESTORE_LAST_IP */ #endif /* CONFIG_LWIP_DHCP_RESTORE_LAST_IP */
/** /**

View File

@@ -4,9 +4,11 @@
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
#ifndef LWIP_ESP_DHCP_STATE_H
#define LWIP_ESP_DHCP_STATE_H
#ifndef _DHCP_STATE_H_ #include <stdbool.h>
#define _DHCP_STATE_H_ #include "lwip/netif.h"
#include <stdbool.h> #include <stdbool.h>
@@ -14,14 +16,14 @@
extern "C" { extern "C" {
#endif #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 #ifdef __cplusplus
} }
#endif #endif
#endif /* _DHCP_STATE_H_ */ #endif /* LWIP_ESP_DHCP_STATE_H */

View File

@@ -1,79 +1,64 @@
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD /*
// * SPDX-FileCopyrightText: 2018-2022 Espressif Systems (Shanghai) CO LTD
// Licensed under the Apache License, Version 2.0 (the "License"); *
// you may not use this file except in compliance with the License. * SPDX-License-Identifier: Apache-2.0
// You may obtain a copy of the License at */
// 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 <stdio.h>
#include <assert.h>
#include "nvs.h" #include "nvs.h"
#include "lwip/opt.h"
#include "lwip/dhcp.h" #include "lwip/dhcp.h"
#include "lwip/netif.h" #include "lwip/netif.h"
#include "esp_interface.h"
#include "esp_netif.h"
#include "esp_netif_net_stack.h"
#include "netif/dhcp_state.h" #include "netif/dhcp_state.h"
#define DHCP_NAMESPACE "dhcp_state" #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; nvs_handle_t nvs;
char if_key[IF_KEY_SIZE];
bool err = false; bool err = false;
struct netif *net = (struct netif *)netif; struct dhcp *dhcp = netif_dhcp_data(netif);
struct dhcp *dhcp = netif_dhcp_data(net);
esp_netif_t *esp_netif = esp_netif_get_handle_from_netif_impl(netif);
if(VALID_NETIF_ID(esp_netif)) {
uint32_t *ip_addr = &dhcp->offered_ip_addr.addr; uint32_t *ip_addr = &dhcp->offered_ip_addr.addr;
if (nvs_open(DHCP_NAMESPACE, NVS_READONLY, &nvs) == ESP_OK) { 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) { if (nvs_get_u32(nvs, gen_if_key(netif, if_key), ip_addr) == ESP_OK) {
err = true; err = true;
} }
nvs_close(nvs); nvs_close(nvs);
} }
}
return err; return err;
} }
void dhcp_ip_addr_store(void *netif) void dhcp_ip_addr_store(struct netif *netif)
{ {
nvs_handle_t nvs; nvs_handle_t nvs;
struct netif *net = (struct netif *)netif; char if_key[IF_KEY_SIZE];
struct dhcp *dhcp = netif_dhcp_data(net); struct dhcp *dhcp = netif_dhcp_data(netif);
uint32_t ip_addr = dhcp->offered_ip_addr.addr; 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) { if (nvs_open(DHCP_NAMESPACE, NVS_READWRITE, &nvs) == ESP_OK) {
nvs_set_u32(nvs,esp_netif_get_ifkey(esp_netif), ip_addr); nvs_set_u32(nvs, gen_if_key(netif, if_key), ip_addr);
nvs_commit(nvs); nvs_commit(nvs);
nvs_close(nvs); nvs_close(nvs);
} }
}
} }
void dhcp_ip_addr_erase(void *esp_netif) void dhcp_ip_addr_erase(struct netif *netif)
{ {
nvs_handle_t nvs; nvs_handle_t nvs;
char if_key[IF_KEY_SIZE];
if(VALID_NETIF_ID(esp_netif)) {
if (nvs_open(DHCP_NAMESPACE, NVS_READWRITE, &nvs) == ESP_OK) { if (nvs_open(DHCP_NAMESPACE, NVS_READWRITE, &nvs) == ESP_OK) {
nvs_erase_key(nvs, esp_netif_get_ifkey(esp_netif)); nvs_erase_key(nvs, gen_if_key(netif, if_key));
nvs_commit(nvs); nvs_commit(nvs);
nvs_close(nvs); nvs_close(nvs);
} }
}
} }

View File

@@ -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/netinet/tcp.h
components/lwip/port/esp32/include/sntp/sntp_get_set_time.h components/lwip/port/esp32/include/sntp/sntp_get_set_time.h
components/lwip/port/esp32/include/sys/socket.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/ethernetif.c
components/lwip/port/esp32/netif/wlanif.c components/lwip/port/esp32/netif/wlanif.c
components/lwip/port/esp32/no_vfs_syscalls.c components/lwip/port/esp32/no_vfs_syscalls.c