2021-09-24 03:57:06 -04:00
|
|
|
/*
|
2022-08-22 06:30:24 -04:00
|
|
|
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
2021-09-24 03:57:06 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "freertos/event_groups.h"
|
|
|
|
#include "esp_wifi.h"
|
|
|
|
#include "esp_event.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_system.h"
|
|
|
|
#include "nvs_flash.h"
|
|
|
|
#include "nvs.h"
|
|
|
|
#include "esp_sntp.h"
|
|
|
|
#include "esp_netif.h"
|
|
|
|
|
|
|
|
#include "lwip/err.h"
|
|
|
|
#include "lwip/sockets.h"
|
|
|
|
#include "lwip/sys.h"
|
|
|
|
#include "lwip/netdb.h"
|
|
|
|
#include "lwip/dns.h"
|
|
|
|
#include "time_sync.h"
|
|
|
|
|
|
|
|
static const char *TAG = "time_sync";
|
|
|
|
|
|
|
|
#define STORAGE_NAMESPACE "storage"
|
|
|
|
|
|
|
|
void initialize_sntp(void)
|
|
|
|
{
|
|
|
|
ESP_LOGI(TAG, "Initializing SNTP");
|
|
|
|
sntp_setoperatingmode(SNTP_OPMODE_POLL);
|
2022-08-22 06:30:24 -04:00
|
|
|
sntp_setservername(0, "time.windows.com");
|
|
|
|
sntp_setservername(1, "pool.ntp.org");
|
2021-09-24 03:57:06 -04:00
|
|
|
#ifdef CONFIG_SNTP_TIME_SYNC_METHOD_SMOOTH
|
|
|
|
sntp_set_sync_mode(SNTP_SYNC_MODE_SMOOTH);
|
|
|
|
#endif
|
|
|
|
sntp_init();
|
|
|
|
}
|
|
|
|
|
2022-08-22 06:30:24 -04:00
|
|
|
static esp_err_t obtain_time(void)
|
2021-09-24 03:57:06 -04:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* NTP server address could be aquired via DHCP,
|
|
|
|
* see LWIP_DHCP_GET_NTP_SRV menuconfig option
|
|
|
|
*/
|
|
|
|
#ifdef LWIP_DHCP_GET_NTP_SRV
|
|
|
|
sntp_servermode_dhcp(1);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// wait for time to be set
|
|
|
|
int retry = 0;
|
|
|
|
const int retry_count = 10;
|
|
|
|
while (sntp_get_sync_status() == SNTP_SYNC_STATUS_RESET && ++retry < retry_count) {
|
|
|
|
ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count);
|
|
|
|
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
|
|
|
}
|
2022-08-22 06:30:24 -04:00
|
|
|
if (retry == retry_count) {
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
2021-09-24 03:57:06 -04:00
|
|
|
}
|
|
|
|
|
2022-08-22 06:30:24 -04:00
|
|
|
esp_err_t fetch_and_store_time_in_nvs(void *args)
|
2021-09-24 03:57:06 -04:00
|
|
|
{
|
|
|
|
initialize_sntp();
|
2022-08-22 06:30:24 -04:00
|
|
|
if (obtain_time() != ESP_OK) {
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
2021-09-24 03:57:06 -04:00
|
|
|
|
|
|
|
nvs_handle_t my_handle;
|
|
|
|
esp_err_t err;
|
|
|
|
|
|
|
|
time_t now;
|
|
|
|
time(&now);
|
|
|
|
|
|
|
|
//Open
|
|
|
|
err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Write
|
|
|
|
err = nvs_set_i64(my_handle, "timestamp", now);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = nvs_commit(my_handle);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
nvs_close(my_handle);
|
|
|
|
sntp_stop();
|
|
|
|
|
|
|
|
exit:
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "Error updating time in nvs");
|
|
|
|
} else {
|
|
|
|
ESP_LOGI(TAG, "Updated time in NVS");
|
|
|
|
}
|
2022-08-22 06:30:24 -04:00
|
|
|
return err;
|
2021-09-24 03:57:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t update_time_from_nvs(void)
|
|
|
|
{
|
|
|
|
nvs_handle_t my_handle;
|
|
|
|
esp_err_t err;
|
|
|
|
|
|
|
|
err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "Error opening NVS");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t timestamp = 0;
|
|
|
|
|
|
|
|
err = nvs_get_i64(my_handle, "timestamp", ×tamp);
|
|
|
|
if (err == ESP_ERR_NVS_NOT_FOUND) {
|
2022-08-22 06:30:24 -04:00
|
|
|
ESP_LOGI(TAG, "Time not found in NVS. Syncing time from SNTP server.");
|
|
|
|
if (fetch_and_store_time_in_nvs(NULL) != ESP_OK) {
|
|
|
|
err = ESP_FAIL;
|
|
|
|
} else {
|
|
|
|
err = ESP_OK;
|
|
|
|
}
|
2021-09-24 03:57:06 -04:00
|
|
|
} else if (err == ESP_OK) {
|
|
|
|
struct timeval get_nvs_time;
|
|
|
|
get_nvs_time.tv_sec = timestamp;
|
|
|
|
settimeofday(&get_nvs_time, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
nvs_close(my_handle);
|
|
|
|
return err;
|
|
|
|
}
|