time_sync.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_netif.h"
  20. #include "esp_netif_sntp.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. esp_sntp_config_t config = ESP_NETIF_SNTP_DEFAULT_CONFIG_MULTIPLE(2,
  33. ESP_SNTP_SERVER_LIST("time.windows.com", "pool.ntp.org" ) );
  34. esp_netif_sntp_init(&config);
  35. }
  36. static esp_err_t obtain_time(void)
  37. {
  38. // wait for time to be set
  39. int retry = 0;
  40. const int retry_count = 10;
  41. while (esp_netif_sntp_sync_wait(pdMS_TO_TICKS(2000)) != ESP_OK && ++retry < retry_count) {
  42. ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count);
  43. }
  44. if (retry == retry_count) {
  45. return ESP_FAIL;
  46. }
  47. return ESP_OK;
  48. }
  49. esp_err_t fetch_and_store_time_in_nvs(void *args)
  50. {
  51. nvs_handle_t my_handle = 0;
  52. esp_err_t err;
  53. initialize_sntp();
  54. if (obtain_time() != ESP_OK) {
  55. err = ESP_FAIL;
  56. goto exit;
  57. }
  58. time_t now;
  59. time(&now);
  60. //Open
  61. err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle);
  62. if (err != ESP_OK) {
  63. goto exit;
  64. }
  65. //Write
  66. err = nvs_set_i64(my_handle, "timestamp", now);
  67. if (err != ESP_OK) {
  68. goto exit;
  69. }
  70. err = nvs_commit(my_handle);
  71. if (err != ESP_OK) {
  72. goto exit;
  73. }
  74. exit:
  75. if (my_handle != 0) {
  76. nvs_close(my_handle);
  77. }
  78. esp_netif_sntp_deinit();
  79. if (err != ESP_OK) {
  80. ESP_LOGE(TAG, "Error updating time in nvs");
  81. } else {
  82. ESP_LOGI(TAG, "Updated time in NVS");
  83. }
  84. return err;
  85. }
  86. esp_err_t update_time_from_nvs(void)
  87. {
  88. nvs_handle_t my_handle = 0;
  89. esp_err_t err;
  90. err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle);
  91. if (err != ESP_OK) {
  92. ESP_LOGE(TAG, "Error opening NVS");
  93. goto exit;
  94. }
  95. int64_t timestamp = 0;
  96. err = nvs_get_i64(my_handle, "timestamp", &timestamp);
  97. if (err == ESP_ERR_NVS_NOT_FOUND) {
  98. ESP_LOGI(TAG, "Time not found in NVS. Syncing time from SNTP server.");
  99. if (fetch_and_store_time_in_nvs(NULL) != ESP_OK) {
  100. err = ESP_FAIL;
  101. } else {
  102. err = ESP_OK;
  103. }
  104. } else if (err == ESP_OK) {
  105. struct timeval get_nvs_time;
  106. get_nvs_time.tv_sec = timestamp;
  107. settimeofday(&get_nvs_time, NULL);
  108. }
  109. exit:
  110. if (my_handle != 0) {
  111. nvs_close(my_handle);
  112. }
  113. return err;
  114. }