ucmd_settime.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 RTC stores a two-digit year, so the
  5. * kernel expands it with a 1970-2069 window; this app accepts the same range
  6. * so a dead-battery default such as 1980 or 1990 can be corrected. */
  7. #include "ucmd_lib.h"
  8. #include "ngui.h"
  9. #define ID_DATE 1
  10. #define ID_TIME 2
  11. #define ID_SET 3
  12. #define ID_NOW 4
  13. #define ACT_SET (1u << 0)
  14. #define ACT_NOW (1u << 1)
  15. static struct ngui ui;
  16. static struct ngui_input date_in;
  17. static struct ngui_input time_in;
  18. static struct sys_datetime current;
  19. static char status[64];
  20. static int status_error;
  21. static unsigned int status_until;
  22. static unsigned int last_second;
  23. static unsigned int s_len(const char *s)
  24. {
  25. unsigned int n = 0;
  26. while (s && s[n])
  27. n++;
  28. return n;
  29. }
  30. static unsigned int days_in_month(unsigned int y, unsigned int m)
  31. {
  32. static const unsigned char dm[12] = {
  33. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  34. };
  35. if (m < 1 || m > 12)
  36. return 0;
  37. if (m == 2 && ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0))
  38. return 29;
  39. return dm[m - 1];
  40. }
  41. static char *put_2(char *p, unsigned int v)
  42. {
  43. *p++ = (char)('0' + (v / 10) % 10);
  44. *p++ = (char)('0' + v % 10);
  45. return p;
  46. }
  47. static char *put_date(char *p, const struct sys_datetime *dt)
  48. {
  49. *p++ = (char)('0' + (dt->year / 1000) % 10);
  50. *p++ = (char)('0' + (dt->year / 100) % 10);
  51. *p++ = (char)('0' + (dt->year / 10) % 10);
  52. *p++ = (char)('0' + dt->year % 10);
  53. *p++ = '-';
  54. p = put_2(p, dt->month);
  55. *p++ = '-';
  56. p = put_2(p, dt->day);
  57. return p;
  58. }
  59. static char *put_time(char *p, const struct sys_datetime *dt)
  60. {
  61. p = put_2(p, dt->hour);
  62. *p++ = ':';
  63. p = put_2(p, dt->minute);
  64. *p++ = ':';
  65. p = put_2(p, dt->second);
  66. return p;
  67. }
  68. static void set_status(const char *msg, int err)
  69. {
  70. unsigned int i;
  71. for (i = 0; msg[i] && i + 1 < sizeof(status); i++)
  72. status[i] = msg[i];
  73. status[i] = '\0';
  74. status_error = err;
  75. status_until = u_ticks_ms() + 4000;
  76. ngui_invalidate(&ui);
  77. }
  78. static void load_current(int fill_fields)
  79. {
  80. struct sys_datetime dt;
  81. char b[24];
  82. char *p;
  83. if (u_rtc_get(&dt) != 0) {
  84. set_status("Cannot read the RTC", 1);
  85. return;
  86. }
  87. current = dt;
  88. if (!fill_fields)
  89. return;
  90. p = put_date(b, &dt);
  91. *p = '\0';
  92. ngui_input_set(&date_in, b);
  93. p = put_time(b, &dt);
  94. *p = '\0';
  95. ngui_input_set(&time_in, b);
  96. }
  97. static int parse_num(const char *s, unsigned int n, unsigned int *out)
  98. {
  99. unsigned int v = 0, i;
  100. if (!n)
  101. return -1;
  102. for (i = 0; i < n; i++) {
  103. if (s[i] < '0' || s[i] > '9')
  104. return -1;
  105. v = v * 10 + (unsigned int)(s[i] - '0');
  106. }
  107. *out = v;
  108. return 0;
  109. }
  110. static int parse_date(const char *s, unsigned int *y, unsigned int *mo,
  111. unsigned int *d)
  112. {
  113. if (s_len(s) != 10)
  114. return -1;
  115. if ((s[4] != '-' && s[4] != '/') ||
  116. (s[7] != '-' && s[7] != '/'))
  117. return -1;
  118. if (parse_num(s, 4, y) != 0 ||
  119. parse_num(s + 5, 2, mo) != 0 ||
  120. parse_num(s + 8, 2, d) != 0)
  121. return -1;
  122. return 0;
  123. }
  124. static int parse_time(const char *s, unsigned int *h, unsigned int *mi,
  125. unsigned int *sec)
  126. {
  127. unsigned int len = s_len(s);
  128. if (len == 5) {
  129. *sec = 0;
  130. } else if (len == 8) {
  131. if (s[5] != ':' || parse_num(s + 6, 2, sec) != 0)
  132. return -1;
  133. } else {
  134. return -1;
  135. }
  136. if (s[2] != ':' ||
  137. parse_num(s, 2, h) != 0 ||
  138. parse_num(s + 3, 2, mi) != 0)
  139. return -1;
  140. return 0;
  141. }
  142. static void apply_now(void)
  143. {
  144. struct sys_datetime dt;
  145. unsigned int y, mo, d, h, mi, s;
  146. if (parse_date(date_in.text, &y, &mo, &d) != 0) {
  147. set_status("Date must be YYYY-MM-DD", 1);
  148. return;
  149. }
  150. if (parse_time(time_in.text, &h, &mi, &s) != 0) {
  151. set_status("Time must be HH:MM:SS", 1);
  152. return;
  153. }
  154. if (y < 1970 || y > 2069) {
  155. set_status("Year must be 1970-2069", 1);
  156. return;
  157. }
  158. if (mo < 1 || mo > 12) {
  159. set_status("Month must be 1-12", 1);
  160. return;
  161. }
  162. if (d < 1 || d > days_in_month(y, mo)) {
  163. set_status("Day is not valid for that month", 1);
  164. return;
  165. }
  166. if (h > 23 || mi > 59 || s > 59) {
  167. set_status("Time is out of range", 1);
  168. return;
  169. }
  170. dt.year = y;
  171. dt.month = mo;
  172. dt.day = d;
  173. dt.hour = h;
  174. dt.minute = mi;
  175. dt.second = s;
  176. dt.wday = 0; /* the kernel recomputes the weekday */
  177. if (u_rtc_set(&dt) != 0) {
  178. set_status("RTC write failed", 1);
  179. return;
  180. }
  181. load_current(1);
  182. set_status("Date and time updated", 0);
  183. }
  184. static unsigned int draw(void)
  185. {
  186. struct ngui_rect body, r, date_r, time_r, set_r, now_r;
  187. unsigned int action = 0;
  188. char line[64];
  189. char *p;
  190. body.x = 0;
  191. body.y = 0;
  192. body.w = ui.w;
  193. body.h = ui.h;
  194. ngui_fill_rect(&ui, body, ui.theme.bg);
  195. ngui_inset(&body, 16);
  196. r = ngui_cut_top(&body, 24);
  197. ngui_text(&ui, r.x, r.y, "System date and time", ui.theme.text);
  198. p = line;
  199. {
  200. static const char prefix[] = "Current: ";
  201. unsigned int i;
  202. for (i = 0; prefix[i]; i++)
  203. *p++ = prefix[i];
  204. }
  205. p = put_date(p, &current);
  206. *p++ = ' ';
  207. p = put_time(p, &current);
  208. *p = '\0';
  209. r = ngui_cut_top(&body, 24);
  210. ngui_text(&ui, r.x, r.y, line, ui.theme.text_dim);
  211. r = ngui_cut_top(&body, 18);
  212. ngui_text(&ui, r.x, r.y, "Date (YYYY-MM-DD)", ui.theme.text_dim);
  213. date_r = ngui_cut_top(&body, 32);
  214. ngui_cut_top(&body, 8);
  215. r = ngui_cut_top(&body, 18);
  216. ngui_text(&ui, r.x, r.y, "Time (HH:MM:SS)", ui.theme.text_dim);
  217. time_r = ngui_cut_top(&body, 32);
  218. ngui_cut_top(&body, 8);
  219. if (ngui_text_input(&ui, ID_DATE, date_r, &date_in) &
  220. NGUI_INPUT_SUBMIT)
  221. action |= ACT_SET;
  222. if (ngui_text_input(&ui, ID_TIME, time_r, &time_in) &
  223. NGUI_INPUT_SUBMIT)
  224. action |= ACT_SET;
  225. r = ngui_cut_top(&body, 40);
  226. set_r = r;
  227. set_r.w = 96;
  228. now_r = r;
  229. now_r.x = r.x + 104;
  230. now_r.w = 96;
  231. if (ngui_button(&ui, ID_SET, set_r, "Set"))
  232. action |= ACT_SET;
  233. if (ngui_button(&ui, ID_NOW, now_r, "Now"))
  234. action |= ACT_NOW;
  235. r = ngui_cut_top(&body, 24);
  236. if (status[0] && status_until &&
  237. (int)(u_ticks_ms() - status_until) < 0)
  238. ngui_text(&ui, r.x, r.y, status,
  239. status_error ? ui.theme.error : ui.theme.accent);
  240. r = ngui_cut_top(&body, 20);
  241. ngui_text(&ui, r.x, r.y,
  242. "RTC year range: 1970-2069. Enter applies the change.",
  243. ui.theme.text_dim);
  244. return action;
  245. }
  246. void _start(const char *arg)
  247. {
  248. struct win_info wi;
  249. int quit = 0;
  250. (void)arg;
  251. if (u_win_create(460, 320, "Date & Time", &wi) != 0) {
  252. u_puts("settime: no graphics window\n");
  253. u_exit(1);
  254. }
  255. ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
  256. ngui_font_attach(&ui);
  257. date_in.placeholder = "YYYY-MM-DD";
  258. time_in.placeholder = "HH:MM:SS";
  259. if (u_rtc_get(&current) == 0) {
  260. char b[24];
  261. char *p;
  262. p = put_date(b, &current);
  263. *p = '\0';
  264. ngui_input_set(&date_in, b);
  265. p = put_time(b, &current);
  266. *p = '\0';
  267. ngui_input_set(&time_in, b);
  268. } else {
  269. set_status("Cannot read the RTC", 1);
  270. }
  271. for (;;) {
  272. unsigned int now = u_ticks_ms();
  273. unsigned int action = 0;
  274. if (status_until && (int)(now - status_until) >= 0) {
  275. status[0] = '\0';
  276. status_until = 0;
  277. status_error = 0;
  278. ngui_invalidate(&ui);
  279. }
  280. if (now - last_second >= 1000) {
  281. last_second = now;
  282. load_current(0);
  283. ngui_invalidate(&ui);
  284. }
  285. if (!ngui_begin(&ui)) {
  286. u_sleep_ms(10);
  287. continue;
  288. }
  289. action = draw();
  290. if (ui.focus == 0 && ngui_key_peek(&ui) == NGUI_KEY_ESC) {
  291. ngui_key(&ui);
  292. quit = 1;
  293. }
  294. ngui_end(&ui);
  295. if (action & ACT_SET)
  296. apply_now();
  297. if (action & ACT_NOW)
  298. load_current(1);
  299. if (quit)
  300. break;
  301. u_sleep_ms(10);
  302. }
  303. u_win_close();
  304. u_exit(0);
  305. }