ucmd_settime.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /* settime: Date & Time editor.
  2. *
  3. * A small ngui canvas window that reads the CMOS RTC through SYS_RTC_GET and
  4. * writes it through SYS_RTC_SET. The "Sync NTP" button fetches UTC from an
  5. * SNTP server through SYS_NTP_SYNC and writes the returned time to the RTC.
  6. * The RTC stores a two-digit year, so the kernel expands it with a 1970-2069
  7. * window; this app accepts the same range so a dead-battery default such as
  8. * 1980 or 1990 can be corrected. */
  9. #include "ucmd_lib.h"
  10. #include "ngui.h"
  11. #define ID_DATE 1
  12. #define ID_TIME 2
  13. #define ID_SET 3
  14. #define ID_NOW 4
  15. #define ID_NTP 5
  16. #define ID_SERVER 6
  17. #define ACT_SET (1u << 0)
  18. #define ACT_NOW (1u << 1)
  19. #define ACT_NTP (1u << 2)
  20. static struct ngui ui;
  21. static struct ngui_input date_in;
  22. static struct ngui_input time_in;
  23. static struct ngui_input ntp_in;
  24. static struct sys_datetime current;
  25. static char status[64];
  26. static int status_error;
  27. static unsigned int status_until;
  28. static unsigned int last_second;
  29. static unsigned int s_len(const char *s)
  30. {
  31. unsigned int n = 0;
  32. while (s && s[n])
  33. n++;
  34. return n;
  35. }
  36. static unsigned int days_in_month(unsigned int y, unsigned int m)
  37. {
  38. static const unsigned char dm[12] = {
  39. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  40. };
  41. if (m < 1 || m > 12)
  42. return 0;
  43. if (m == 2 && ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0))
  44. return 29;
  45. return dm[m - 1];
  46. }
  47. static char *put_2(char *p, unsigned int v)
  48. {
  49. *p++ = (char)('0' + (v / 10) % 10);
  50. *p++ = (char)('0' + v % 10);
  51. return p;
  52. }
  53. static char *put_date(char *p, const struct sys_datetime *dt)
  54. {
  55. *p++ = (char)('0' + (dt->year / 1000) % 10);
  56. *p++ = (char)('0' + (dt->year / 100) % 10);
  57. *p++ = (char)('0' + (dt->year / 10) % 10);
  58. *p++ = (char)('0' + dt->year % 10);
  59. *p++ = '-';
  60. p = put_2(p, dt->month);
  61. *p++ = '-';
  62. p = put_2(p, dt->day);
  63. return p;
  64. }
  65. static char *put_time(char *p, const struct sys_datetime *dt)
  66. {
  67. p = put_2(p, dt->hour);
  68. *p++ = ':';
  69. p = put_2(p, dt->minute);
  70. *p++ = ':';
  71. p = put_2(p, dt->second);
  72. return p;
  73. }
  74. static void set_status(const char *msg, int err)
  75. {
  76. unsigned int i;
  77. for (i = 0; msg[i] && i + 1 < sizeof(status); i++)
  78. status[i] = msg[i];
  79. status[i] = '\0';
  80. status_error = err;
  81. status_until = u_ticks_ms() + 4000;
  82. ngui_invalidate(&ui);
  83. }
  84. static void set_fields(const struct sys_datetime *dt)
  85. {
  86. char b[24];
  87. char *p;
  88. p = put_date(b, dt);
  89. *p = '\0';
  90. ngui_input_set(&date_in, b);
  91. p = put_time(b, dt);
  92. *p = '\0';
  93. ngui_input_set(&time_in, b);
  94. }
  95. static void load_current(int fill)
  96. {
  97. struct sys_datetime dt;
  98. if (u_rtc_get(&dt) != 0) {
  99. set_status("Cannot read the RTC", 1);
  100. return;
  101. }
  102. current = dt;
  103. if (fill)
  104. set_fields(&dt);
  105. }
  106. static int parse_num(const char *s, unsigned int n, unsigned int *out)
  107. {
  108. unsigned int v = 0, i;
  109. if (!n)
  110. return -1;
  111. for (i = 0; i < n; i++) {
  112. if (s[i] < '0' || s[i] > '9')
  113. return -1;
  114. v = v * 10 + (unsigned int)(s[i] - '0');
  115. }
  116. *out = v;
  117. return 0;
  118. }
  119. static int parse_date(const char *s, unsigned int *y, unsigned int *mo,
  120. unsigned int *d)
  121. {
  122. if (s_len(s) != 10)
  123. return -1;
  124. if ((s[4] != '-' && s[4] != '/') ||
  125. (s[7] != '-' && s[7] != '/'))
  126. return -1;
  127. if (parse_num(s, 4, y) != 0 ||
  128. parse_num(s + 5, 2, mo) != 0 ||
  129. parse_num(s + 8, 2, d) != 0)
  130. return -1;
  131. return 0;
  132. }
  133. static int parse_time(const char *s, unsigned int *h, unsigned int *mi,
  134. unsigned int *sec)
  135. {
  136. unsigned int len = s_len(s);
  137. if (len == 5) {
  138. *sec = 0;
  139. } else if (len == 8) {
  140. if (s[5] != ':' || parse_num(s + 6, 2, sec) != 0)
  141. return -1;
  142. } else {
  143. return -1;
  144. }
  145. if (s[2] != ':' ||
  146. parse_num(s, 2, h) != 0 ||
  147. parse_num(s + 3, 2, mi) != 0)
  148. return -1;
  149. return 0;
  150. }
  151. static void apply_now(void)
  152. {
  153. struct sys_datetime dt;
  154. unsigned int y, mo, d, h, mi, s;
  155. if (parse_date(date_in.text, &y, &mo, &d) != 0) {
  156. set_status("Date must be YYYY-MM-DD", 1);
  157. return;
  158. }
  159. if (parse_time(time_in.text, &h, &mi, &s) != 0) {
  160. set_status("Time must be HH:MM:SS", 1);
  161. return;
  162. }
  163. if (y < 1970 || y > 2069) {
  164. set_status("Year must be 1970-2069", 1);
  165. return;
  166. }
  167. if (mo < 1 || mo > 12) {
  168. set_status("Month must be 1-12", 1);
  169. return;
  170. }
  171. if (d < 1 || d > days_in_month(y, mo)) {
  172. set_status("Day is not valid for that month", 1);
  173. return;
  174. }
  175. if (h > 23 || mi > 59 || s > 59) {
  176. set_status("Time is out of range", 1);
  177. return;
  178. }
  179. dt.year = y;
  180. dt.month = mo;
  181. dt.day = d;
  182. dt.hour = h;
  183. dt.minute = mi;
  184. dt.second = s;
  185. dt.wday = 0; /* the kernel recomputes the weekday */
  186. if (u_rtc_set(&dt) != 0) {
  187. set_status("RTC write failed", 1);
  188. return;
  189. }
  190. load_current(1);
  191. set_status("Date and time updated", 0);
  192. }
  193. static const char *ntp_error(int r)
  194. {
  195. switch (r) {
  196. case SYS_NTP_ENONIC:
  197. return "NTP: no network adapter";
  198. case SYS_NTP_ENAME:
  199. return "NTP: cannot resolve the server";
  200. case SYS_NTP_EARP:
  201. return "NTP: server is unreachable (ARP)";
  202. case SYS_NTP_ETX:
  203. return "NTP: transmit failed";
  204. case SYS_NTP_ETIMEOUT:
  205. return "NTP: server did not respond";
  206. case SYS_NTP_EREPLY:
  207. return "NTP: invalid server reply";
  208. case SYS_NTP_EBUSY:
  209. return "NTP: network is busy";
  210. default:
  211. return "NTP: sync failed";
  212. }
  213. }
  214. static unsigned int draw(void)
  215. {
  216. struct ngui_rect body, r, date_r, time_r, server_r, set_r, now_r, ntp_r;
  217. unsigned int action = 0;
  218. char line[64];
  219. char *p;
  220. body.x = 0;
  221. body.y = 0;
  222. body.w = ui.w;
  223. body.h = ui.h;
  224. ngui_fill_rect(&ui, body, ui.theme.bg);
  225. ngui_inset(&body, 16);
  226. r = ngui_cut_top(&body, 24);
  227. ngui_text(&ui, r.x, r.y, "System date and time", ui.theme.text);
  228. p = line;
  229. {
  230. static const char prefix[] = "Current: ";
  231. unsigned int i;
  232. for (i = 0; prefix[i]; i++)
  233. *p++ = prefix[i];
  234. }
  235. p = put_date(p, &current);
  236. *p++ = ' ';
  237. p = put_time(p, &current);
  238. *p = '\0';
  239. r = ngui_cut_top(&body, 24);
  240. ngui_text(&ui, r.x, r.y, line, ui.theme.text_dim);
  241. r = ngui_cut_top(&body, 18);
  242. ngui_text(&ui, r.x, r.y, "Date (YYYY-MM-DD)", ui.theme.text_dim);
  243. date_r = ngui_cut_top(&body, 32);
  244. ngui_cut_top(&body, 8);
  245. r = ngui_cut_top(&body, 18);
  246. ngui_text(&ui, r.x, r.y, "Time (HH:MM:SS)", ui.theme.text_dim);
  247. time_r = ngui_cut_top(&body, 32);
  248. ngui_cut_top(&body, 8);
  249. r = ngui_cut_top(&body, 18);
  250. ngui_text(&ui, r.x, r.y, "NTP server (UTC)", ui.theme.text_dim);
  251. server_r = ngui_cut_top(&body, 32);
  252. ngui_cut_top(&body, 8);
  253. if (ngui_text_input(&ui, ID_DATE, date_r, &date_in) &
  254. NGUI_INPUT_SUBMIT)
  255. action |= ACT_SET;
  256. if (ngui_text_input(&ui, ID_TIME, time_r, &time_in) &
  257. NGUI_INPUT_SUBMIT)
  258. action |= ACT_SET;
  259. if (ngui_text_input(&ui, ID_SERVER, server_r, &ntp_in) &
  260. NGUI_INPUT_SUBMIT)
  261. action |= ACT_NTP;
  262. r = ngui_cut_top(&body, 40);
  263. set_r = r;
  264. set_r.w = 88;
  265. now_r = r;
  266. now_r.x = r.x + 96;
  267. now_r.w = 88;
  268. ntp_r = r;
  269. ntp_r.x = r.x + 192;
  270. ntp_r.w = 112;
  271. if (ngui_button(&ui, ID_SET, set_r, "Set"))
  272. action |= ACT_SET;
  273. if (ngui_button(&ui, ID_NOW, now_r, "Now"))
  274. action |= ACT_NOW;
  275. if (ngui_button(&ui, ID_NTP, ntp_r, "Sync NTP"))
  276. action |= ACT_NTP;
  277. r = ngui_cut_top(&body, 24);
  278. if (status[0] && status_until &&
  279. (int)(u_ticks_ms() - status_until) < 0)
  280. ngui_text(&ui, r.x, r.y, status,
  281. status_error ? ui.theme.error : ui.theme.accent);
  282. r = ngui_cut_top(&body, 20);
  283. ngui_text(&ui, r.x, r.y,
  284. "Sync NTP fetches UTC and writes the RTC.",
  285. ui.theme.text_dim);
  286. r = ngui_cut_top(&body, 20);
  287. ngui_text(&ui, r.x, r.y,
  288. "RTC year range: 1970-2069. Enter applies the change.",
  289. ui.theme.text_dim);
  290. return action;
  291. }
  292. static void set_contact_status(const char *server)
  293. {
  294. static const char prefix[] = "Contacting ";
  295. unsigned int i, n;
  296. for (i = 0; prefix[i] && i + 1 < sizeof(status); i++)
  297. status[i] = prefix[i];
  298. for (n = 0; server[n] && i + 3 < sizeof(status); n++)
  299. status[i++] = server[n];
  300. status[i++] = '.';
  301. status[i++] = '.';
  302. status[i++] = '.';
  303. status[i] = '\0';
  304. status_error = 0;
  305. status_until = u_ticks_ms() + 30000;
  306. ngui_invalidate(&ui);
  307. }
  308. /* Publish the contact status before the blocking syscall. */
  309. static void paint_frame(void)
  310. {
  311. if (!ngui_begin(&ui))
  312. return;
  313. draw();
  314. ngui_end(&ui);
  315. }
  316. static void sync_ntp(void)
  317. {
  318. struct sys_datetime dt;
  319. const char *server = ntp_in.text[0] ? ntp_in.text : "pool.ntp.org";
  320. int r;
  321. set_contact_status(server);
  322. paint_frame();
  323. r = u_ntp_sync(ntp_in.text, 5000, &dt);
  324. if (r != 0) {
  325. set_status(ntp_error(r), 1);
  326. return;
  327. }
  328. if (dt.year < 1970 || dt.year > 2069) {
  329. set_status("NTP time is outside the RTC range", 1);
  330. return;
  331. }
  332. if (u_rtc_set(&dt) != 0) {
  333. set_status("NTP time received, RTC write failed", 1);
  334. return;
  335. }
  336. current = dt;
  337. set_fields(&dt);
  338. set_status("NTP time applied (UTC)", 0);
  339. }
  340. void _start(const char *arg)
  341. {
  342. struct win_info wi;
  343. int quit = 0;
  344. (void)arg;
  345. if (u_win_create(460, 400, "Date & Time", &wi) != 0) {
  346. u_puts("settime: no graphics window\n");
  347. u_exit(1);
  348. }
  349. ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
  350. ngui_font_attach(&ui);
  351. date_in.placeholder = "YYYY-MM-DD";
  352. time_in.placeholder = "HH:MM:SS";
  353. ntp_in.placeholder = "pool.ntp.org";
  354. ngui_input_set(&ntp_in, "pool.ntp.org");
  355. if (u_rtc_get(&current) == 0) {
  356. set_fields(&current);
  357. } else {
  358. set_status("Cannot read the RTC", 1);
  359. }
  360. for (;;) {
  361. unsigned int now = u_ticks_ms();
  362. unsigned int action = 0;
  363. if (status_until && (int)(now - status_until) >= 0) {
  364. status[0] = '\0';
  365. status_until = 0;
  366. status_error = 0;
  367. ngui_invalidate(&ui);
  368. }
  369. if (now - last_second >= 1000) {
  370. last_second = now;
  371. load_current(0);
  372. ngui_invalidate(&ui);
  373. }
  374. if (!ngui_begin(&ui)) {
  375. u_sleep_ms(10);
  376. continue;
  377. }
  378. action = draw();
  379. if (ui.focus == 0 && ngui_key_peek(&ui) == NGUI_KEY_ESC) {
  380. ngui_key(&ui);
  381. quit = 1;
  382. }
  383. ngui_end(&ui);
  384. if (action & ACT_SET)
  385. apply_now();
  386. if (action & ACT_NOW)
  387. load_current(1);
  388. if (action & ACT_NTP)
  389. sync_ntp();
  390. if (quit)
  391. break;
  392. u_sleep_ms(10);
  393. }
  394. u_win_close();
  395. u_exit(0);
  396. }