time_sync.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * SPDX-FileCopyrightText: 2021 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, "pool.ntp.org");
  34. #ifdef CONFIG_SNTP_TIME_SYNC_METHOD_SMOOTH
  35. sntp_set_sync_mode(SNTP_SYNC_MODE_SMOOTH);
  36. #endif
  37. sntp_init();
  38. }
  39. static void obtain_time(void)
  40. {
  41. /**
  42. * NTP server address could be aquired via DHCP,
  43. * see LWIP_DHCP_GET_NTP_SRV menuconfig option
  44. */
  45. #ifdef LWIP_DHCP_GET_NTP_SRV
  46. sntp_servermode_dhcp(1);
  47. #endif
  48. // wait for time to be set
  49. int retry = 0;
  50. const int retry_count = 10;
  51. while (sntp_get_sync_status() == SNTP_SYNC_STATUS_RESET && ++retry < retry_count) {
  52. ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count);
  53. vTaskDelay(2000 / portTICK_PERIOD_MS);
  54. }
  55. }
  56. void fetch_and_store_time_in_nvs(void *args)
  57. {
  58. initialize_sntp();
  59. obtain_time();
  60. nvs_handle_t my_handle;
  61. esp_err_t err;
  62. time_t now;
  63. time(&now);
  64. //Open
  65. err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle);
  66. if (err != ESP_OK) {
  67. goto exit;
  68. }
  69. //Write
  70. err = nvs_set_i64(my_handle, "timestamp", now);
  71. if (err != ESP_OK) {
  72. goto exit;
  73. }
  74. err = nvs_commit(my_handle);
  75. if (err != ESP_OK) {
  76. goto exit;
  77. }
  78. nvs_close(my_handle);
  79. sntp_stop();
  80. exit:
  81. if (err != ESP_OK) {
  82. ESP_LOGE(TAG, "Error updating time in nvs");
  83. } else {
  84. ESP_LOGI(TAG, "Updated time in NVS");
  85. }
  86. }
  87. esp_err_t update_time_from_nvs(void)
  88. {
  89. nvs_handle_t my_handle;
  90. esp_err_t err;
  91. err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle);
  92. if (err != ESP_OK) {
  93. ESP_LOGE(TAG, "Error opening NVS");
  94. goto exit;
  95. }
  96. int64_t timestamp = 0;
  97. err = nvs_get_i64(my_handle, "timestamp", &timestamp);
  98. if (err == ESP_ERR_NVS_NOT_FOUND) {
  99. fetch_and_store_time_in_nvs(NULL);
  100. err = ESP_OK;
  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. }