time_sync.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. initialize_sntp();
  52. if (obtain_time() != ESP_OK) {
  53. return ESP_FAIL;
  54. }
  55. nvs_handle_t my_handle;
  56. esp_err_t err;
  57. time_t now;
  58. time(&now);
  59. //Open
  60. err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle);
  61. if (err != ESP_OK) {
  62. goto exit;
  63. }
  64. //Write
  65. err = nvs_set_i64(my_handle, "timestamp", now);
  66. if (err != ESP_OK) {
  67. goto exit;
  68. }
  69. err = nvs_commit(my_handle);
  70. if (err != ESP_OK) {
  71. goto exit;
  72. }
  73. nvs_close(my_handle);
  74. esp_netif_deinit();
  75. exit:
  76. if (err != ESP_OK) {
  77. ESP_LOGE(TAG, "Error updating time in nvs");
  78. } else {
  79. ESP_LOGI(TAG, "Updated time in NVS");
  80. }
  81. return err;
  82. }
  83. esp_err_t update_time_from_nvs(void)
  84. {
  85. nvs_handle_t my_handle;
  86. esp_err_t err;
  87. err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle);
  88. if (err != ESP_OK) {
  89. ESP_LOGE(TAG, "Error opening NVS");
  90. goto exit;
  91. }
  92. int64_t timestamp = 0;
  93. err = nvs_get_i64(my_handle, "timestamp", &timestamp);
  94. if (err == ESP_ERR_NVS_NOT_FOUND) {
  95. ESP_LOGI(TAG, "Time not found in NVS. Syncing time from SNTP server.");
  96. if (fetch_and_store_time_in_nvs(NULL) != ESP_OK) {
  97. err = ESP_FAIL;
  98. } else {
  99. err = ESP_OK;
  100. }
  101. } else if (err == ESP_OK) {
  102. struct timeval get_nvs_time;
  103. get_nvs_time.tv_sec = timestamp;
  104. settimeofday(&get_nvs_time, NULL);
  105. }
  106. exit:
  107. nvs_close(my_handle);
  108. return err;
  109. }