Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

403 lines
10 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
8 years ago
  1. /*
  2. NTP MODULE
  3. Based on esp8266 / esp32 configTime and C date and time functions:
  4. - https://github.com/esp8266/Arduino/blob/master/libraries/esp8266/examples/NTP-TZ-DST/NTP-TZ-DST.ino
  5. - https://www.nongnu.org/lwip/2_1_x/group__sntp.html
  6. - man 3 ctime
  7. Copyright (C) 2019 by Maxim Prokhorov <prokhorov dot max at outlook dot com>
  8. */
  9. #include "ntp.h"
  10. #if NTP_SUPPORT && !NTP_LEGACY_SUPPORT
  11. #include <Arduino.h>
  12. #include <coredecls.h>
  13. #include <Ticker.h>
  14. static_assert(
  15. (SNTP_SERVER_DNS == 1),
  16. "lwip must be configured with SNTP_SERVER_DNS"
  17. );
  18. #include "config/buildtime.h"
  19. #include "debug.h"
  20. #include "broker.h"
  21. #include "ws.h"
  22. BrokerBind(NtpBroker);
  23. // Arduino/esp8266 lwip2 custom functions that can be redefined
  24. // Must return time in milliseconds, legacy settings are in seconds.
  25. String _ntp_server;
  26. uint32_t _ntp_startup_delay = (NTP_START_DELAY * 1000);
  27. uint32_t _ntp_update_delay = (NTP_UPDATE_INTERVAL * 1000);
  28. uint32_t sntp_startup_delay_MS_rfc_not_less_than_60000() {
  29. return _ntp_startup_delay;
  30. }
  31. uint32_t sntp_update_delay_MS_rfc_not_less_than_15000() {
  32. return _ntp_update_delay;
  33. }
  34. // We also must shim TimeLib functions until everything else is ported.
  35. // We can't sometimes avoid TimeLib as dependancy though, which would be really bad
  36. static Ticker _ntp_broker_timer;
  37. static bool _ntp_synced = false;
  38. static time_t _ntp_last = 0;
  39. static time_t _ntp_ts = 0;
  40. static tm _ntp_tm_local;
  41. static tm _ntp_tm_utc;
  42. void _ntpTmCache(time_t ts) {
  43. if (_ntp_ts != ts) {
  44. _ntp_ts = ts;
  45. localtime_r(&_ntp_ts, &_ntp_tm_local);
  46. gmtime_r(&_ntp_ts, &_ntp_tm_utc);
  47. }
  48. }
  49. int hour(time_t ts) {
  50. _ntpTmCache(ts);
  51. return _ntp_tm_local.tm_hour;
  52. }
  53. int minute(time_t ts) {
  54. _ntpTmCache(ts);
  55. return _ntp_tm_local.tm_min;
  56. }
  57. int second(time_t ts) {
  58. _ntpTmCache(ts);
  59. return _ntp_tm_local.tm_sec;
  60. }
  61. int day(time_t ts) {
  62. _ntpTmCache(ts);
  63. return _ntp_tm_local.tm_mday;
  64. }
  65. // `tm.tm_wday` range is 0..6, TimeLib is 1..7
  66. int weekday(time_t ts) {
  67. _ntpTmCache(ts);
  68. return _ntp_tm_local.tm_wday + 1;
  69. }
  70. // `tm.tm_mon` range is 0..11, TimeLib range is 1..12
  71. int month(time_t ts) {
  72. _ntpTmCache(ts);
  73. return _ntp_tm_local.tm_mon + 1;
  74. }
  75. int year(time_t ts) {
  76. _ntpTmCache(ts);
  77. return _ntp_tm_local.tm_year + 1900;
  78. }
  79. int utc_hour(time_t ts) {
  80. _ntpTmCache(ts);
  81. return _ntp_tm_utc.tm_hour;
  82. }
  83. int utc_minute(time_t ts) {
  84. _ntpTmCache(ts);
  85. return _ntp_tm_utc.tm_min;
  86. }
  87. int utc_second(time_t ts) {
  88. _ntpTmCache(ts);
  89. return _ntp_tm_utc.tm_sec;
  90. }
  91. int utc_day(time_t ts) {
  92. _ntpTmCache(ts);
  93. return _ntp_tm_utc.tm_mday;
  94. }
  95. int utc_weekday(time_t ts) {
  96. _ntpTmCache(ts);
  97. return _ntp_tm_utc.tm_wday + 1;
  98. }
  99. int utc_month(time_t ts) {
  100. _ntpTmCache(ts);
  101. return _ntp_tm_utc.tm_mon + 1;
  102. }
  103. int utc_year(time_t ts) {
  104. _ntpTmCache(ts);
  105. return _ntp_tm_utc.tm_year + 1900;
  106. }
  107. time_t now() {
  108. return time(nullptr);
  109. }
  110. // -----------------------------------------------------------------------------
  111. #if WEB_SUPPORT
  112. bool _ntpWebSocketOnKeyCheck(const char * key, JsonVariant& value) {
  113. return (strncmp(key, "ntp", 3) == 0);
  114. }
  115. void _ntpWebSocketOnVisible(JsonObject& root) {
  116. root["ntpVisible"] = 1;
  117. root["ntplwipVisible"] = 1;
  118. }
  119. void _ntpWebSocketOnData(JsonObject& root) {
  120. root["ntpStatus"] = ntpSynced();
  121. }
  122. void _ntpWebSocketOnConnected(JsonObject& root) {
  123. root["ntpServer"] = getSetting("ntpServer", F(NTP_SERVER));
  124. root["ntpTZ"] = getSetting("ntpTZ", NTP_TIMEZONE);
  125. }
  126. #endif
  127. // TODO: mention possibility of multiple servers
  128. String _ntpGetServer() {
  129. String server;
  130. server = sntp_getservername(0);
  131. if (!server.length()) {
  132. server = IPAddress(sntp_getserver(0)).toString();
  133. }
  134. return server;
  135. }
  136. void _ntpReport() {
  137. if (!ntpSynced()) {
  138. DEBUG_MSG_P(PSTR("[NTP] Not synced\n"));
  139. return;
  140. }
  141. tm utc_tm;
  142. tm sync_tm;
  143. auto ts = now();
  144. gmtime_r(&ts, &utc_tm);
  145. gmtime_r(&_ntp_last, &sync_tm);
  146. DEBUG_MSG_P(PSTR("[NTP] Server : %s\n"), _ntp_server.c_str());
  147. DEBUG_MSG_P(PSTR("[NTP] Sync Time : %s (UTC)\n"), ntpDateTime(&sync_tm).c_str());
  148. DEBUG_MSG_P(PSTR("[NTP] UTC Time : %s\n"), ntpDateTime(&utc_tm).c_str());
  149. const char* cfg_tz = getenv("TZ");
  150. if ((cfg_tz != nullptr) && (strcmp(cfg_tz, "UTC0") != 0)) {
  151. tm local_tm;
  152. localtime_r(&ts, &local_tm);
  153. DEBUG_MSG_P(PSTR("[NTP] Local Time : %s (%s)\n"),
  154. ntpDateTime(&local_tm).c_str(), cfg_tz
  155. );
  156. }
  157. }
  158. void _ntpConfigure() {
  159. // Note: TZ_... provided by the Core are already wrapped with PSTR(...)
  160. const auto cfg_tz = getSetting("ntpTZ", NTP_TIMEZONE);
  161. const char* active_tz = getenv("TZ");
  162. if (cfg_tz != active_tz) {
  163. setenv("TZ", cfg_tz.c_str(), 1);
  164. tzset();
  165. }
  166. const auto cfg_server = getSetting("ntpServer", F(NTP_SERVER));
  167. const auto active_server = _ntpGetServer();
  168. if (cfg_tz != active_tz) {
  169. _ntp_server = cfg_server;
  170. configTime(cfg_tz.c_str(), _ntp_server.c_str());
  171. DEBUG_MSG_P(PSTR("[NTP] Server: %s, TZ: %s\n"), cfg_server.c_str(), cfg_tz.length() ? cfg_tz.c_str() : "UTC0");
  172. }
  173. }
  174. // -----------------------------------------------------------------------------
  175. bool ntpSynced() {
  176. return _ntp_synced;
  177. }
  178. String ntpDateTime(tm* timestruct) {
  179. char buffer[20];
  180. snprintf_P(buffer, sizeof(buffer),
  181. PSTR("%04d-%02d-%02d %02d:%02d:%02d"),
  182. timestruct->tm_year + 1900,
  183. timestruct->tm_mon + 1,
  184. timestruct->tm_mday,
  185. timestruct->tm_hour,
  186. timestruct->tm_min,
  187. timestruct->tm_sec
  188. );
  189. return String(buffer);
  190. }
  191. String ntpDateTime(time_t ts) {
  192. tm timestruct;
  193. localtime_r(&ts, &timestruct);
  194. return ntpDateTime(&timestruct);
  195. }
  196. String ntpDateTime() {
  197. if (ntpSynced()) {
  198. return ntpDateTime(now());
  199. }
  200. return String();
  201. }
  202. // -----------------------------------------------------------------------------
  203. #if BROKER_SUPPORT
  204. void _ntpBrokerSchedule(int offset);
  205. void _ntpBrokerCallback() {
  206. if (!ntpSynced()) {
  207. _ntpBrokerSchedule(60);
  208. return;
  209. }
  210. const auto ts = now();
  211. // current time and formatter string is in local TZ
  212. tm local_tm;
  213. localtime_r(&ts, &local_tm);
  214. int now_hour = local_tm.tm_hour;
  215. int now_minute = local_tm.tm_min;
  216. static int last_hour = -1;
  217. static int last_minute = -1;
  218. String datetime;
  219. if ((last_minute != now_minute) || (last_hour != now_hour)) {
  220. datetime = ntpDateTime(&local_tm);
  221. }
  222. // notify subscribers about each tick interval (note that both can happen simultaneously)
  223. if (last_hour != now_hour) {
  224. last_hour = now_hour;
  225. NtpBroker::Publish(NtpTick::EveryHour, ts, datetime.c_str());
  226. }
  227. if (last_minute != now_minute) {
  228. last_minute = now_minute;
  229. NtpBroker::Publish(NtpTick::EveryMinute, ts, datetime.c_str());
  230. }
  231. // try to autocorrect each invocation
  232. _ntpBrokerSchedule(60 - local_tm.tm_sec);
  233. }
  234. // XXX: Nonos docs for some reason mention 100 micros as minimum time. Schedule next second in case this is 0
  235. void _ntpBrokerSchedule(int offset) {
  236. _ntp_broker_timer.once_scheduled(offset ?: 1, _ntpBrokerCallback);
  237. }
  238. #endif
  239. void _ntpSetTimeOfDayCallback() {
  240. _ntp_synced = true;
  241. _ntp_last = time(nullptr);
  242. #if BROKER_SUPPORT
  243. static bool once = true;
  244. if (once) {
  245. schedule_function(_ntpBrokerCallback);
  246. once = false;
  247. }
  248. #endif
  249. #if WEB_SUPPORT
  250. wsPost(_ntpWebSocketOnData);
  251. #endif
  252. schedule_function(_ntpReport);
  253. }
  254. void _ntpSetTimestamp(time_t ts) {
  255. timeval tv { ts, 0 };
  256. timezone tz { 0, 0 };
  257. settimeofday(&tv, &tz);
  258. }
  259. // -----------------------------------------------------------------------------
  260. void ntpSetup() {
  261. // Randomized in time to avoid clogging the server with simultaneous requests from multiple devices
  262. // (for example, when multiple devices start up at the same time)
  263. const uint32_t startup_delay = getSetting("ntpStartDelay", NTP_START_DELAY);
  264. const uint32_t update_delay = getSetting("ntpUpdateIntvl", NTP_UPDATE_INTERVAL);
  265. _ntp_startup_delay = secureRandom(startup_delay, startup_delay * 2);
  266. _ntp_update_delay = secureRandom(update_delay, update_delay * 2);
  267. DEBUG_MSG_P(PSTR("[NTP] Startup delay: %us, Update delay: %us\n"),
  268. _ntp_startup_delay, _ntp_update_delay
  269. );
  270. _ntp_startup_delay = _ntp_startup_delay * 1000;
  271. _ntp_update_delay = _ntp_update_delay * 1000;
  272. // start up with some reasonable timestamp already available
  273. _ntpSetTimestamp(__UNIX_TIMESTAMP__);
  274. // will be called every time after ntp syncs AND loop() finishes
  275. settimeofday_cb(_ntpSetTimeOfDayCallback);
  276. // generic configuration, always handled
  277. espurnaRegisterReload(_ntpConfigure);
  278. _ntpConfigure();
  279. // make sure our logic does know about the actual server
  280. // in case dhcp sends out ntp settings
  281. static WiFiEventHandler on_sta = WiFi.onStationModeGotIP([](WiFiEventStationModeGotIP) {
  282. const auto server = _ntpGetServer();
  283. if (sntp_enabled() && (!_ntp_server.length() || (server != _ntp_server))) {
  284. DEBUG_MSG_P(PSTR("[NTP] Updating `ntpServer` setting from DHCP: %s\n"), server.c_str());
  285. _ntp_server = server;
  286. setSetting("ntpServer", server);
  287. }
  288. });
  289. // optional functionality
  290. #if WEB_SUPPORT
  291. wsRegister()
  292. .onVisible(_ntpWebSocketOnVisible)
  293. .onConnected(_ntpWebSocketOnConnected)
  294. .onData(_ntpWebSocketOnData)
  295. .onKeyCheck(_ntpWebSocketOnKeyCheck);
  296. #endif
  297. #if TERMINAL_SUPPORT
  298. terminalRegisterCommand(F("NTP"), [](Embedis* e) {
  299. _ntpReport();
  300. terminalOK();
  301. });
  302. terminalRegisterCommand(F("NTP.SETTIME"), [](Embedis* e) {
  303. if (e->argc != 2) return;
  304. _ntp_synced = true;
  305. _ntpSetTimestamp(String(e->argv[1]).toInt());
  306. terminalOK();
  307. });
  308. // TODO:
  309. // terminalRegisterCommand(F("NTP.SYNC"), [](Embedis* e) { ... }
  310. //
  311. #endif
  312. }
  313. #endif // NTP_SUPPORT && !NTP_LEGACY_SUPPORT