2018-08-29 04:22:54 -04:00
|
|
|
// 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
|
|
|
|
|
|
|
|
// 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 "lwip/opt.h"
|
|
|
|
#include "lwip/dhcp.h"
|
|
|
|
#include "lwip/netif.h"
|
|
|
|
#include "esp_interface.h"
|
2019-06-28 10:47:34 -04:00
|
|
|
#include "esp_netif.h"
|
|
|
|
#include "esp_netif_net_stack.h"
|
2018-08-29 04:22:54 -04:00
|
|
|
#include "netif/dhcp_state.h"
|
|
|
|
|
|
|
|
#define DHCP_NAMESPACE "dhcp_state"
|
|
|
|
|
2019-06-28 10:47:34 -04:00
|
|
|
// DHCP_Client has to be enabled for this netif
|
2019-10-30 15:29:13 -04:00
|
|
|
#define VALID_NETIF_ID(netif) (ESP_NETIF_DHCP_CLIENT&esp_netif_get_flags(netif))
|
2018-08-29 04:22:54 -04:00
|
|
|
|
|
|
|
bool dhcp_ip_addr_restore(void *netif)
|
|
|
|
{
|
2019-04-01 10:13:55 -04:00
|
|
|
nvs_handle_t nvs;
|
2018-08-29 04:22:54 -04:00
|
|
|
bool err = false;
|
|
|
|
struct netif *net = (struct netif *)netif;
|
|
|
|
struct dhcp *dhcp = netif_dhcp_data(net);
|
2019-06-28 10:47:34 -04:00
|
|
|
esp_netif_t *esp_netif = esp_netif_get_handle_from_netif_impl(netif);
|
2018-08-29 04:22:54 -04:00
|
|
|
|
2019-06-28 10:47:34 -04:00
|
|
|
if(VALID_NETIF_ID(esp_netif)) {
|
2018-08-29 04:22:54 -04:00
|
|
|
uint32_t *ip_addr = &dhcp->offered_ip_addr.addr;
|
|
|
|
if (nvs_open(DHCP_NAMESPACE, NVS_READONLY, &nvs) == ESP_OK) {
|
2019-06-28 10:47:34 -04:00
|
|
|
if (nvs_get_u32(nvs, esp_netif_get_ifkey(esp_netif), ip_addr) == ESP_OK) {
|
2018-08-29 04:22:54 -04:00
|
|
|
err = true;
|
|
|
|
}
|
|
|
|
nvs_close(nvs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dhcp_ip_addr_store(void *netif)
|
|
|
|
{
|
2019-04-01 10:13:55 -04:00
|
|
|
nvs_handle_t nvs;
|
2018-08-29 04:22:54 -04:00
|
|
|
struct netif *net = (struct netif *)netif;
|
|
|
|
struct dhcp *dhcp = netif_dhcp_data(net);
|
|
|
|
uint32_t ip_addr = dhcp->offered_ip_addr.addr;
|
2019-06-28 10:47:34 -04:00
|
|
|
esp_netif_t *esp_netif = esp_netif_get_handle_from_netif_impl(netif);
|
2018-08-29 04:22:54 -04:00
|
|
|
|
2019-06-28 10:47:34 -04:00
|
|
|
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);
|
2018-08-29 04:22:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-28 10:47:34 -04:00
|
|
|
void dhcp_ip_addr_erase(void *esp_netif)
|
2018-08-29 04:22:54 -04:00
|
|
|
{
|
2019-04-01 10:13:55 -04:00
|
|
|
nvs_handle_t nvs;
|
2018-08-29 04:22:54 -04:00
|
|
|
|
2019-06-28 10:47:34 -04:00
|
|
|
if(VALID_NETIF_ID(esp_netif)) {
|
2018-08-29 04:22:54 -04:00
|
|
|
if (nvs_open(DHCP_NAMESPACE, NVS_READWRITE, &nvs) == ESP_OK) {
|
2019-06-28 10:47:34 -04:00
|
|
|
nvs_erase_key(nvs, esp_netif_get_ifkey(esp_netif));
|
2018-08-29 04:22:54 -04:00
|
|
|
nvs_commit(nvs);
|
|
|
|
nvs_close(nvs);
|
|
|
|
}
|
|
|
|
}
|
2020-11-10 02:40:01 -05:00
|
|
|
}
|