time_helper.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /* time_helper.c
  2. *
  3. * Copyright (C) 2006-2024 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. /* common Espressif time_helper */
  22. #include "time_helper.h"
  23. #include "sdkconfig.h"
  24. /* wolfSSL */
  25. /* Always include wolfcrypt/settings.h before any other wolfSSL file. */
  26. /* Reminder: settings.h pulls in user_settings.h; don't include it here. */
  27. #ifdef WOLFSSL_USER_SETTINGS
  28. #include <wolfssl/wolfcrypt/settings.h>
  29. #ifndef WOLFSSL_ESPIDF
  30. #warning "Problem with wolfSSL user_settings."
  31. #warning "Check components/wolfssl/include"
  32. #endif
  33. /* This project not yet using the library */
  34. #undef USE_WOLFSSL_ESP_SDK_WIFI
  35. #include <wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h>
  36. #else
  37. /* Define WOLFSSL_USER_SETTINGS project wide for settings.h to include */
  38. /* wolfSSL user settings in ./components/wolfssl/include/user_settings.h */
  39. #error "Missing WOLFSSL_USER_SETTINGS in CMakeLists or Makefile:\
  40. CFLAGS +=-DWOLFSSL_USER_SETTINGS"
  41. #endif
  42. #include <esp_log.h>
  43. #include <esp_idf_version.h>
  44. #if defined(ESP_IDF_VERSION_MAJOR) && defined(ESP_IDF_VERSION_MINOR)
  45. #if (ESP_IDF_VERSION_MAJOR == 5) && (ESP_IDF_VERSION_MINOR >= 1)
  46. #define HAS_ESP_NETIF_SNTP 1
  47. #include <lwip/apps/sntp.h>
  48. #include <esp_netif_sntp.h>
  49. #else
  50. #include <string.h>
  51. #include <esp_sntp.h>
  52. #endif
  53. #else
  54. /* TODO Consider non ESP-IDF environments */
  55. #endif
  56. /* ESP-IDF uses a 64-bit signed integer to represent time_t starting from
  57. * release v5.0. See: Espressif api-reference/system/system_time
  58. */
  59. /* see https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html */
  60. #ifndef TIME_ZONE
  61. /*
  62. * PST represents Pacific Standard Time.
  63. * +8 specifies the offset from UTC (Coordinated Universal Time), indicating
  64. * that Pacific Time is UTC-8 during standard time.
  65. * PDT represents Pacific Daylight Time.
  66. * M3.2.0 indicates that Daylight Saving Time (DST) starts on the
  67. * second (2) Sunday (0) of March (3).
  68. * M11.1.0 indicates that DST ends on the first (1) Sunday (0) of November (11)
  69. */
  70. #define TIME_ZONE "PST+8PDT,M3.2.0,M11.1.0"
  71. #endif /* not defined: TIME_ZONE, so we are setting our own */
  72. #define NTP_RETRY_COUNT 10
  73. /* NELEMS(x) number of elements
  74. * To determine the number of elements in the array, we can divide the total
  75. * size of the array by the size of the array element.
  76. * See https://stackoverflow.com/questions/37538/how-do-i-determine-the-size-of-my-array-in-c
  77. **/
  78. #define NELEMS(x) ( (int)(sizeof(x) / sizeof((x)[0])) )
  79. /* See also CONFIG_LWIP_SNTP_MAX_SERVERS in sdkconfig */
  80. #define NTP_SERVER_LIST ( (char*[]) { \
  81. "pool.ntp.org", \
  82. "time.nist.gov", \
  83. "utcnist.colorado.edu" \
  84. } \
  85. )
  86. /* #define NTP_SERVER_COUNT using NELEMS:
  87. *
  88. * (int)(sizeof(NTP_SERVER_LIST) / sizeof(NTP_SERVER_LIST[0]))
  89. */
  90. #define NTP_SERVER_COUNT NELEMS(NTP_SERVER_LIST)
  91. #ifndef CONFIG_LWIP_SNTP_MAX_SERVERS
  92. /* We should find max value in sdkconfig, if not set it to our count:*/
  93. #define CONFIG_LWIP_SNTP_MAX_SERVERS NTP_SERVER_COUNT
  94. #endif
  95. char* ntpServerList[NTP_SERVER_COUNT] = NTP_SERVER_LIST;
  96. const static char* TAG = "time_helper";
  97. /* our NTP server list is global info */
  98. extern char* ntpServerList[NTP_SERVER_COUNT];
  99. /* Show the current date and time */
  100. int esp_show_current_datetime(void)
  101. {
  102. time_t now;
  103. char strftime_buf[64];
  104. struct tm timeinfo;
  105. time(&now);
  106. setenv("TZ", TIME_ZONE, 1);
  107. tzset();
  108. localtime_r(&now, &timeinfo);
  109. strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
  110. ESP_LOGI(TAG, "The current date/time is: %s", strftime_buf);
  111. return ESP_OK;
  112. }
  113. /* the worst-case scenario is a hard-coded date/time */
  114. int set_fixed_default_time(void)
  115. {
  116. /* ideally, we'd like to set time from network,
  117. * but let's set a default time, just in case */
  118. struct tm timeinfo = {
  119. .tm_year = 2024 - 1900,
  120. .tm_mon = 3,
  121. .tm_mday = 01,
  122. .tm_hour = 13,
  123. .tm_min = 01,
  124. .tm_sec = 05
  125. };
  126. struct timeval now;
  127. time_t interim_time;
  128. int ret = -1;
  129. /* set interim static time */
  130. interim_time = mktime(&timeinfo);
  131. ESP_LOGI(TAG, "Adjusting time from fixed value");
  132. now = (struct timeval){ .tv_sec = interim_time };
  133. ret = settimeofday(&now, NULL);
  134. ESP_LOGI(TAG, "settimeofday result = %d", ret);
  135. return ret;
  136. }
  137. /* probably_valid_time_string(s)
  138. *
  139. * some sanity checks on time string before calling sscanf()
  140. *
  141. * returns 0 == ESP_OK == Success if str is likely a valid time.
  142. * -1 == ESP_FAIL otherwise
  143. */
  144. int probably_valid_time_string(const char* str)
  145. {
  146. int ret = ESP_OK;
  147. size_t length = 0;
  148. size_t spaces = 0;
  149. size_t colons = 0;
  150. while (str[length] != '\0') {
  151. if (str[length] == ' ') {
  152. spaces++;
  153. }
  154. if (str[length] == ':') {
  155. colons++;
  156. }
  157. length++;
  158. }
  159. if ((length > 32) || (spaces < 4) || (spaces > 5) || (colons > 2)) {
  160. ret = ESP_FAIL;
  161. ESP_LOGE(TAG, "ERROR, failed time sanity check: %s", str);
  162. }
  163. return ret;
  164. }
  165. /* set_time_from_string(s)
  166. *
  167. * returns 0 = success if able to set the time from the provided string
  168. * error for any other value, typically -1 */
  169. int set_time_from_string(const char* time_buffer)
  170. {
  171. /* expecting github default formatting: 'Thu Aug 31 12:41:45 2023 -0700' */
  172. char offset[28]; /* large arrays, just in case there's still bad data */
  173. char day_str[28];
  174. char month_str[28];
  175. const char *format = "%3s %3s %d %d:%d:%d %d %s";
  176. struct tm this_timeinfo;
  177. struct timeval now;
  178. time_t interim_time;
  179. int day, year, hour, minute, second;
  180. int quote_offset = 0;
  181. int ret = 0;
  182. /* perform some basic sanity checks */
  183. ret = probably_valid_time_string(time_buffer);
  184. if (ret == ESP_OK) {
  185. /* we are expecting the string to be encapsulated in single quotes */
  186. if (*time_buffer == 0x27) {
  187. quote_offset = 1;
  188. }
  189. ret = sscanf(time_buffer + quote_offset,
  190. format,
  191. day_str, month_str,
  192. &day, &hour, &minute, &second, &year, &offset);
  193. if (ret == 8) {
  194. /* we found a match for all components */
  195. const char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  196. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  197. };
  198. for (int i = 0; i < 12; i++) {
  199. if (strcmp(month_str, months[i]) == 0) {
  200. this_timeinfo.tm_mon = i;
  201. break;
  202. }
  203. }
  204. this_timeinfo.tm_mday = day;
  205. this_timeinfo.tm_hour = hour;
  206. this_timeinfo.tm_min = minute;
  207. this_timeinfo.tm_sec = second;
  208. this_timeinfo.tm_year = year - 1900; /* Years since 1900 */
  209. interim_time = mktime(&this_timeinfo);
  210. now = (struct timeval){ .tv_sec = interim_time };
  211. ret = settimeofday(&now, NULL);
  212. ESP_LOGI(TAG, "Time updated to %s", time_buffer);
  213. }
  214. else {
  215. ESP_LOGE(TAG, "Failed to convert \"%s\" to a tm date.",
  216. time_buffer);
  217. ESP_LOGI(TAG, "Trying fixed date that was hard-coded....");
  218. set_fixed_default_time();
  219. ret = ESP_FAIL;
  220. }
  221. }
  222. return ret;
  223. }
  224. /* set time; returns 0 if succecssfully configured with NTP */
  225. int set_time(void)
  226. {
  227. #ifndef NTP_SERVER_COUNT
  228. ESP_LOGW(TAG, "Warning: no sntp server names defined. "
  229. "Setting to empty list");
  230. #define NTP_SERVER_COUNT 0
  231. #warning "NTP not properly configured"
  232. #endif /* not defined: NTP_SERVER_COUNT */
  233. #ifdef HAS_ESP_NETIF_SNTP
  234. #if CONFIG_LWIP_SNTP_MAX_SERVERS > 1
  235. esp_sntp_config_t config = ESP_NETIF_SNTP_DEFAULT_CONFIG_MULTIPLE(
  236. NTP_SERVER_COUNT,
  237. ESP_SNTP_SERVER_LIST(ntpServerList[0])
  238. );
  239. #else
  240. esp_sntp_config_t config = ESP_NETIF_SNTP_DEFAULT_CONFIG(ntpServerList[0]);
  241. #endif /* CONFIG_LWIP_SNTP_MAX_SERVERS > 1 */
  242. #endif /* HAS_ESP_NETIF_SNTP */
  243. int ret = 0;
  244. int i = 0; /* counter for time servers */
  245. ESP_LOGI(TAG, "Setting the time. Startup time:");
  246. esp_show_current_datetime();
  247. #ifdef LIBWOLFSSL_VERSION_GIT_HASH_DATE
  248. /* initially set a default approximate time from recent git commit */
  249. ESP_LOGI(TAG, "Found git hash date, attempting to set system date: %s",
  250. LIBWOLFSSL_VERSION_GIT_HASH_DATE);
  251. set_time_from_string(LIBWOLFSSL_VERSION_GIT_HASH_DATE"\0");
  252. esp_show_current_datetime();
  253. ret = -4;
  254. #else
  255. /* otherwise set a fixed time that was hard coded */
  256. set_fixed_default_time();
  257. esp_show_current_datetime();
  258. ret = -3;
  259. #endif
  260. #ifdef CONFIG_SNTP_TIME_SYNC_METHOD_SMOOTH
  261. config.smooth_sync = true;
  262. #endif
  263. if (NTP_SERVER_COUNT) {
  264. /* next, let's setup NTP time servers
  265. *
  266. * see Espressif api-reference/system/system_time
  267. *
  268. * WARNING: do not set operating mode while SNTP client is running!
  269. */
  270. /* TODO Consider esp_sntp_setoperatingmode(SNTP_OPMODE_POLL); */
  271. sntp_setoperatingmode(SNTP_OPMODE_POLL);
  272. if (NTP_SERVER_COUNT > CONFIG_LWIP_SNTP_MAX_SERVERS) {
  273. ESP_LOGW(TAG, "WARNING: %d NTP Servers defined, but "
  274. "CONFIG_LWIP_SNTP_MAX_SERVERS = %d",
  275. NTP_SERVER_COUNT,CONFIG_LWIP_SNTP_MAX_SERVERS);
  276. }
  277. ESP_LOGI(TAG, "sntp_setservername:");
  278. for (i = 0; i < CONFIG_LWIP_SNTP_MAX_SERVERS; i++) {
  279. const char* thisServer = ntpServerList[i];
  280. if (strncmp(thisServer, "\x00", 1) == 0) {
  281. /* just in case we run out of NTP servers */
  282. break;
  283. }
  284. ESP_LOGI(TAG, "%s", thisServer);
  285. sntp_setservername(i, thisServer);
  286. ret = ESP_OK;
  287. }
  288. #ifdef HAS_ESP_NETIF_SNTP
  289. ret = esp_netif_sntp_init(&config);
  290. #else
  291. ESP_LOGW(TAG,"Warning: Consider upgrading ESP-IDF to take advantage "
  292. "of updated SNTP libraries");
  293. #endif
  294. if (ret == ESP_OK) {
  295. ESP_LOGV(TAG, "Successfully called esp_netif_sntp_init");
  296. }
  297. else {
  298. ESP_LOGE(TAG, "ERROR: esp_netif_sntp_init return = %d", ret);
  299. }
  300. sntp_init();
  301. switch (ret) {
  302. case ESP_ERR_INVALID_STATE:
  303. break;
  304. default:
  305. break;
  306. }
  307. ESP_LOGI(TAG, "sntp_init done.");
  308. }
  309. else {
  310. ESP_LOGW(TAG, "No sntp time servers found.");
  311. ret = -1;
  312. }
  313. esp_show_current_datetime();
  314. ESP_LOGI(TAG, "time helper existing with result = %d", ret);
  315. return ret;
  316. }
  317. /* wait for NTP to actually set the time */
  318. int set_time_wait_for_ntp(void)
  319. {
  320. int ret = 0;
  321. #ifdef HAS_ESP_NETIF_SNTP
  322. int ntp_retry = 0;
  323. const int ntp_retry_count = NTP_RETRY_COUNT;
  324. ret = esp_netif_sntp_start();
  325. ret = esp_netif_sntp_sync_wait(500 / portTICK_PERIOD_MS);
  326. #else
  327. ESP_LOGW(TAG, "HAS_ESP_NETIF_SNTP not defined");
  328. #endif /* HAS_ESP_NETIF_SNTP */
  329. esp_show_current_datetime();
  330. #ifdef HAS_ESP_NETIF_SNTP
  331. while (ret == ESP_ERR_TIMEOUT && (ntp_retry++ < ntp_retry_count)) {
  332. ret = esp_netif_sntp_sync_wait(1000 / portTICK_PERIOD_MS);
  333. ESP_LOGI(TAG, "Waiting for NTP to sync time... (%d/%d)",
  334. ntp_retry,
  335. ntp_retry_count);
  336. esp_show_current_datetime();
  337. }
  338. #endif /* HAS_ESP_NETIF_SNTP */
  339. #ifdef TIME_ZONE
  340. setenv("TZ", TIME_ZONE, 1);
  341. tzset();
  342. #endif
  343. if (ret == ESP_OK) {
  344. ESP_LOGI(TAG, "Successfully set time via NTP servers.");
  345. }
  346. else {
  347. ESP_LOGW(TAG, "Warning: Failed to set time with NTP: "
  348. "result = 0x%0x: %s",
  349. ret, esp_err_to_name(ret));
  350. }
  351. return ret;
  352. }