time_helper.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* time_helper.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #include <string.h>
  22. #include <lwip/apps/sntp.h>
  23. #include "sdkconfig.h"
  24. #include "esp_log.h"
  25. #include "time_helper.h"
  26. const static char* TAG = "Time Helper";
  27. #define TIME_ZONE "PST-8"
  28. /* NELEMS(x) number of elements
  29. * To determine the number of elements in the array, we can divide the total size of
  30. * the array by the size of the array element
  31. * See https://stackoverflow.com/questions/37538/how-do-i-determine-the-size-of-my-array-in-c
  32. **/
  33. #define NELEMS(x) ( (int)(sizeof(x) / sizeof((x)[0])) )
  34. #define NTP_SERVER_LIST ( (char*[]) { \
  35. "pool.ntp.org", \
  36. "time.nist.gov", \
  37. "utcnist.colorado.edu" \
  38. } \
  39. )
  40. /* #define NTP_SERVER_COUNT using NELEMS:
  41. *
  42. * (int)(sizeof(NTP_SERVER_LIST) / sizeof(NTP_SERVER_LIST[0]))
  43. */
  44. #define NTP_SERVER_COUNT NELEMS(NTP_SERVER_LIST)
  45. char* ntpServerList[NTP_SERVER_COUNT] = NTP_SERVER_LIST;
  46. /* our NTP server list is global info */
  47. extern char* ntpServerList[NTP_SERVER_COUNT];
  48. int set_time(void)
  49. {
  50. /* we'll also return a result code of zero */
  51. int res = 0;
  52. int i = 0; /* counter for time servers */
  53. time_t interim_time;
  54. /* ideally, we'd like to set time from network,
  55. * but let's set a default time, just in case */
  56. struct tm timeinfo = {
  57. .tm_year = 2022 - 1900,
  58. .tm_mon = 11,
  59. .tm_mday = 15,
  60. .tm_hour = 3,
  61. .tm_min = 25,
  62. .tm_sec = 0
  63. };
  64. struct timeval now;
  65. #ifndef NTP_SERVER_COUNT
  66. #define NTP_SERVER_COUNT 0
  67. char* ntpServerList[NTP_SERVER_COUNT];
  68. #endif /* not defined: NTP_SERVER_COUNT */
  69. #ifndef TIME_ZONE
  70. #define TIME_ZONE "PST-8"
  71. #endif /* not defined: TIME_ZONE */
  72. /* set interim static time */
  73. interim_time = mktime(&timeinfo);
  74. now = (struct timeval){ .tv_sec = interim_time };
  75. settimeofday(&now, NULL);
  76. /* set timezone */
  77. setenv("TZ", TIME_ZONE, 1);
  78. tzset();
  79. if (NTP_SERVER_COUNT) {
  80. /* next, let's setup NTP time servers
  81. *
  82. * see https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/system_time.html#sntp-time-synchronization
  83. */
  84. sntp_setoperatingmode(SNTP_OPMODE_POLL);
  85. ESP_LOGI(TAG, "sntp_setservername:");
  86. for (i = 0; i < NTP_SERVER_COUNT; i++) {
  87. const char* thisServer = ntpServerList[i];
  88. if (strncmp(thisServer, "\x00", 1) == 0) {
  89. /* just in case we run out of NTP servers */
  90. break;
  91. }
  92. ESP_LOGI(TAG, "%s", thisServer);
  93. sntp_setservername(i, thisServer);
  94. }
  95. sntp_init();
  96. ESP_LOGI(TAG, "sntp_init done.");
  97. }
  98. else {
  99. ESP_LOGI(TAG, "No sntp time servers found.");
  100. }
  101. return res;
  102. }