time_sync.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <time.h>
  9. #include <sys/time.h>
  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/task.h"
  12. #include "freertos/event_groups.h"
  13. #include "esp_wifi.h"
  14. #include "esp_event.h"
  15. #include "esp_log.h"
  16. #include "esp_system.h"
  17. #include "nvs_flash.h"
  18. #include "nvs.h"
  19. #include "esp_sntp.h"
  20. #include "esp_netif.h"
  21. #include "lwip/err.h"
  22. #include "lwip/sockets.h"
  23. #include "lwip/sys.h"
  24. #include "lwip/netdb.h"
  25. #include "lwip/dns.h"
  26. #include "time_sync.h"
  27. static const char *TAG = "time_sync";
  28. #define STORAGE_NAMESPACE "storage"
  29. void initialize_sntp(void)
  30. {
  31. ESP_LOGI(TAG, "Initializing SNTP");
  32. sntp_setoperatingmode(SNTP_OPMODE_POLL);
  33. sntp_setservername(0, "time.windows.com");
  34. sntp_setservername(1, "pool.ntp.org");
  35. #ifdef CONFIG_SNTP_TIME_SYNC_METHOD_SMOOTH
  36. sntp_set_sync_mode(SNTP_SYNC_MODE_SMOOTH);
  37. #endif
  38. sntp_init();
  39. }
  40. static esp_err_t obtain_time(void)
  41. {
  42. /**
  43. * NTP server address could be aquired via DHCP,
  44. * see LWIP_DHCP_GET_NTP_SRV menuconfig option
  45. */
  46. #ifdef LWIP_DHCP_GET_NTP_SRV
  47. sntp_servermode_dhcp(1);
  48. #endif
  49. // wait for time to be set
  50. int retry = 0;
  51. const int retry_count = 10;
  52. while (sntp_get_sync_status() == SNTP_SYNC_STATUS_RESET && ++retry < retry_count) {
  53. ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count);
  54. vTaskDelay(2000 / portTICK_PERIOD_MS);
  55. }
  56. if (retry == retry_count) {
  57. return ESP_FAIL;
  58. }
  59. return ESP_OK;
  60. }
  61. esp_err_t fetch_and_store_time_in_nvs(void *args)
  62. {
  63. initialize_sntp();
  64. if (obtain_time() != ESP_OK) {
  65. return ESP_FAIL;
  66. }
  67. nvs_handle_t my_handle;
  68. esp_err_t err;
  69. time_t now;
  70. time(&now);
  71. //Open
  72. err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle);
  73. if (err != ESP_OK) {
  74. goto exit;
  75. }
  76. //Write
  77. err = nvs_set_i64(my_handle, "timestamp", now);
  78. if (err != ESP_OK) {
  79. goto exit;
  80. }
  81. err = nvs_commit(my_handle);
  82. if (err != ESP_OK) {
  83. goto exit;
  84. }
  85. nvs_close(my_handle);
  86. sntp_stop();
  87. exit:
  88. if (err != ESP_OK) {
  89. ESP_LOGE(TAG, "Error updating time in nvs");
  90. } else {
  91. ESP_LOGI(TAG, "Updated time in NVS");
  92. }
  93. return err;
  94. }
  95. esp_err_t update_time_from_nvs(void)
  96. {
  97. nvs_handle_t my_handle;
  98. esp_err_t err;
  99. err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle);
  100. if (err != ESP_OK) {
  101. ESP_LOGE(TAG, "Error opening NVS");
  102. goto exit;
  103. }
  104. int64_t timestamp = 0;
  105. err = nvs_get_i64(my_handle, "timestamp", &timestamp);
  106. if (err == ESP_ERR_NVS_NOT_FOUND) {
  107. ESP_LOGI(TAG, "Time not found in NVS. Syncing time from SNTP server.");
  108. if (fetch_and_store_time_in_nvs(NULL) != ESP_OK) {
  109. err = ESP_FAIL;
  110. } else {
  111. err = ESP_OK;
  112. }
  113. } else if (err == ESP_OK) {
  114. struct timeval get_nvs_time;
  115. get_nvs_time.tv_sec = timestamp;
  116. settimeofday(&get_nvs_time, NULL);
  117. }
  118. exit:
  119. nvs_close(my_handle);
  120. return err;
  121. }