| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446 |
- /* settime: Date & Time editor.
- *
- * A small ngui canvas window that reads the CMOS RTC through SYS_RTC_GET and
- * writes it through SYS_RTC_SET. The "Sync NTP" button fetches UTC from an
- * SNTP server through SYS_NTP_SYNC and writes the returned time to the RTC.
- * The RTC stores a two-digit year, so the kernel expands it with a 1970-2069
- * window; this app accepts the same range so a dead-battery default such as
- * 1980 or 1990 can be corrected. */
- #include "ucmd_lib.h"
- #include "ngui.h"
- #define ID_DATE 1
- #define ID_TIME 2
- #define ID_SET 3
- #define ID_NOW 4
- #define ID_NTP 5
- #define ID_SERVER 6
- #define ACT_SET (1u << 0)
- #define ACT_NOW (1u << 1)
- #define ACT_NTP (1u << 2)
- static struct ngui ui;
- static struct ngui_input date_in;
- static struct ngui_input time_in;
- static struct ngui_input ntp_in;
- static struct sys_datetime current;
- static char status[64];
- static int status_error;
- static unsigned int status_until;
- static unsigned int last_second;
- static unsigned int s_len(const char *s)
- {
- unsigned int n = 0;
- while (s && s[n])
- n++;
- return n;
- }
- static unsigned int days_in_month(unsigned int y, unsigned int m)
- {
- static const unsigned char dm[12] = {
- 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
- };
- if (m < 1 || m > 12)
- return 0;
- if (m == 2 && ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0))
- return 29;
- return dm[m - 1];
- }
- static char *put_2(char *p, unsigned int v)
- {
- *p++ = (char)('0' + (v / 10) % 10);
- *p++ = (char)('0' + v % 10);
- return p;
- }
- static char *put_date(char *p, const struct sys_datetime *dt)
- {
- *p++ = (char)('0' + (dt->year / 1000) % 10);
- *p++ = (char)('0' + (dt->year / 100) % 10);
- *p++ = (char)('0' + (dt->year / 10) % 10);
- *p++ = (char)('0' + dt->year % 10);
- *p++ = '-';
- p = put_2(p, dt->month);
- *p++ = '-';
- p = put_2(p, dt->day);
- return p;
- }
- static char *put_time(char *p, const struct sys_datetime *dt)
- {
- p = put_2(p, dt->hour);
- *p++ = ':';
- p = put_2(p, dt->minute);
- *p++ = ':';
- p = put_2(p, dt->second);
- return p;
- }
- static void set_status(const char *msg, int err)
- {
- unsigned int i;
- for (i = 0; msg[i] && i + 1 < sizeof(status); i++)
- status[i] = msg[i];
- status[i] = '\0';
- status_error = err;
- status_until = u_ticks_ms() + 4000;
- ngui_invalidate(&ui);
- }
- static void set_fields(const struct sys_datetime *dt)
- {
- char b[24];
- char *p;
- p = put_date(b, dt);
- *p = '\0';
- ngui_input_set(&date_in, b);
- p = put_time(b, dt);
- *p = '\0';
- ngui_input_set(&time_in, b);
- }
- static void load_current(int fill)
- {
- struct sys_datetime dt;
- if (u_rtc_get(&dt) != 0) {
- set_status("Cannot read the RTC", 1);
- return;
- }
- current = dt;
- if (fill)
- set_fields(&dt);
- }
- static int parse_num(const char *s, unsigned int n, unsigned int *out)
- {
- unsigned int v = 0, i;
- if (!n)
- return -1;
- for (i = 0; i < n; i++) {
- if (s[i] < '0' || s[i] > '9')
- return -1;
- v = v * 10 + (unsigned int)(s[i] - '0');
- }
- *out = v;
- return 0;
- }
- static int parse_date(const char *s, unsigned int *y, unsigned int *mo,
- unsigned int *d)
- {
- if (s_len(s) != 10)
- return -1;
- if ((s[4] != '-' && s[4] != '/') ||
- (s[7] != '-' && s[7] != '/'))
- return -1;
- if (parse_num(s, 4, y) != 0 ||
- parse_num(s + 5, 2, mo) != 0 ||
- parse_num(s + 8, 2, d) != 0)
- return -1;
- return 0;
- }
- static int parse_time(const char *s, unsigned int *h, unsigned int *mi,
- unsigned int *sec)
- {
- unsigned int len = s_len(s);
- if (len == 5) {
- *sec = 0;
- } else if (len == 8) {
- if (s[5] != ':' || parse_num(s + 6, 2, sec) != 0)
- return -1;
- } else {
- return -1;
- }
- if (s[2] != ':' ||
- parse_num(s, 2, h) != 0 ||
- parse_num(s + 3, 2, mi) != 0)
- return -1;
- return 0;
- }
- static void apply_now(void)
- {
- struct sys_datetime dt;
- unsigned int y, mo, d, h, mi, s;
- if (parse_date(date_in.text, &y, &mo, &d) != 0) {
- set_status("Date must be YYYY-MM-DD", 1);
- return;
- }
- if (parse_time(time_in.text, &h, &mi, &s) != 0) {
- set_status("Time must be HH:MM:SS", 1);
- return;
- }
- if (y < 1970 || y > 2069) {
- set_status("Year must be 1970-2069", 1);
- return;
- }
- if (mo < 1 || mo > 12) {
- set_status("Month must be 1-12", 1);
- return;
- }
- if (d < 1 || d > days_in_month(y, mo)) {
- set_status("Day is not valid for that month", 1);
- return;
- }
- if (h > 23 || mi > 59 || s > 59) {
- set_status("Time is out of range", 1);
- return;
- }
- dt.year = y;
- dt.month = mo;
- dt.day = d;
- dt.hour = h;
- dt.minute = mi;
- dt.second = s;
- dt.wday = 0; /* the kernel recomputes the weekday */
- if (u_rtc_set(&dt) != 0) {
- set_status("RTC write failed", 1);
- return;
- }
- load_current(1);
- set_status("Date and time updated", 0);
- }
- static const char *ntp_error(int r)
- {
- switch (r) {
- case SYS_NTP_ENONIC:
- return "NTP: no network adapter";
- case SYS_NTP_ENAME:
- return "NTP: cannot resolve the server";
- case SYS_NTP_EARP:
- return "NTP: server is unreachable (ARP)";
- case SYS_NTP_ETX:
- return "NTP: transmit failed";
- case SYS_NTP_ETIMEOUT:
- return "NTP: server did not respond";
- case SYS_NTP_EREPLY:
- return "NTP: invalid server reply";
- case SYS_NTP_EBUSY:
- return "NTP: network is busy";
- default:
- return "NTP: sync failed";
- }
- }
- static unsigned int draw(void)
- {
- struct ngui_rect body, r, date_r, time_r, server_r, set_r, now_r, ntp_r;
- unsigned int action = 0;
- char line[64];
- char *p;
- body.x = 0;
- body.y = 0;
- body.w = ui.w;
- body.h = ui.h;
- ngui_fill_rect(&ui, body, ui.theme.bg);
- ngui_inset(&body, 16);
- r = ngui_cut_top(&body, 24);
- ngui_text(&ui, r.x, r.y, "System date and time", ui.theme.text);
- p = line;
- {
- static const char prefix[] = "Current: ";
- unsigned int i;
- for (i = 0; prefix[i]; i++)
- *p++ = prefix[i];
- }
- p = put_date(p, ¤t);
- *p++ = ' ';
- p = put_time(p, ¤t);
- *p = '\0';
- r = ngui_cut_top(&body, 24);
- ngui_text(&ui, r.x, r.y, line, ui.theme.text_dim);
- r = ngui_cut_top(&body, 18);
- ngui_text(&ui, r.x, r.y, "Date (YYYY-MM-DD)", ui.theme.text_dim);
- date_r = ngui_cut_top(&body, 32);
- ngui_cut_top(&body, 8);
- r = ngui_cut_top(&body, 18);
- ngui_text(&ui, r.x, r.y, "Time (HH:MM:SS)", ui.theme.text_dim);
- time_r = ngui_cut_top(&body, 32);
- ngui_cut_top(&body, 8);
- r = ngui_cut_top(&body, 18);
- ngui_text(&ui, r.x, r.y, "NTP server (UTC)", ui.theme.text_dim);
- server_r = ngui_cut_top(&body, 32);
- ngui_cut_top(&body, 8);
- if (ngui_text_input(&ui, ID_DATE, date_r, &date_in) &
- NGUI_INPUT_SUBMIT)
- action |= ACT_SET;
- if (ngui_text_input(&ui, ID_TIME, time_r, &time_in) &
- NGUI_INPUT_SUBMIT)
- action |= ACT_SET;
- if (ngui_text_input(&ui, ID_SERVER, server_r, &ntp_in) &
- NGUI_INPUT_SUBMIT)
- action |= ACT_NTP;
- r = ngui_cut_top(&body, 40);
- set_r = r;
- set_r.w = 88;
- now_r = r;
- now_r.x = r.x + 96;
- now_r.w = 88;
- ntp_r = r;
- ntp_r.x = r.x + 192;
- ntp_r.w = 112;
- if (ngui_button(&ui, ID_SET, set_r, "Set"))
- action |= ACT_SET;
- if (ngui_button(&ui, ID_NOW, now_r, "Now"))
- action |= ACT_NOW;
- if (ngui_button(&ui, ID_NTP, ntp_r, "Sync NTP"))
- action |= ACT_NTP;
- r = ngui_cut_top(&body, 24);
- if (status[0] && status_until &&
- (int)(u_ticks_ms() - status_until) < 0)
- ngui_text(&ui, r.x, r.y, status,
- status_error ? ui.theme.error : ui.theme.accent);
- r = ngui_cut_top(&body, 20);
- ngui_text(&ui, r.x, r.y,
- "Sync NTP fetches UTC and writes the RTC.",
- ui.theme.text_dim);
- r = ngui_cut_top(&body, 20);
- ngui_text(&ui, r.x, r.y,
- "RTC year range: 1970-2069. Enter applies the change.",
- ui.theme.text_dim);
- return action;
- }
- static void set_contact_status(const char *server)
- {
- static const char prefix[] = "Contacting ";
- unsigned int i, n;
- for (i = 0; prefix[i] && i + 1 < sizeof(status); i++)
- status[i] = prefix[i];
- for (n = 0; server[n] && i + 3 < sizeof(status); n++)
- status[i++] = server[n];
- status[i++] = '.';
- status[i++] = '.';
- status[i++] = '.';
- status[i] = '\0';
- status_error = 0;
- status_until = u_ticks_ms() + 30000;
- ngui_invalidate(&ui);
- }
- /* Publish the contact status before the blocking syscall. */
- static void paint_frame(void)
- {
- if (!ngui_begin(&ui))
- return;
- draw();
- ngui_end(&ui);
- }
- static void sync_ntp(void)
- {
- struct sys_datetime dt;
- const char *server = ntp_in.text[0] ? ntp_in.text : "pool.ntp.org";
- int r;
- set_contact_status(server);
- paint_frame();
- r = u_ntp_sync(ntp_in.text, 5000, &dt);
- if (r != 0) {
- set_status(ntp_error(r), 1);
- return;
- }
- if (dt.year < 1970 || dt.year > 2069) {
- set_status("NTP time is outside the RTC range", 1);
- return;
- }
- if (u_rtc_set(&dt) != 0) {
- set_status("NTP time received, RTC write failed", 1);
- return;
- }
- current = dt;
- set_fields(&dt);
- set_status("NTP time applied (UTC)", 0);
- }
- void _start(const char *arg)
- {
- struct win_info wi;
- int quit = 0;
- (void)arg;
- if (u_win_create(460, 400, "Date & Time", &wi) != 0) {
- u_puts("settime: no graphics window\n");
- u_exit(1);
- }
- ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
- ngui_font_attach(&ui);
- date_in.placeholder = "YYYY-MM-DD";
- time_in.placeholder = "HH:MM:SS";
- ntp_in.placeholder = "pool.ntp.org";
- ngui_input_set(&ntp_in, "pool.ntp.org");
- if (u_rtc_get(¤t) == 0) {
- set_fields(¤t);
- } else {
- set_status("Cannot read the RTC", 1);
- }
- for (;;) {
- unsigned int now = u_ticks_ms();
- unsigned int action = 0;
- if (status_until && (int)(now - status_until) >= 0) {
- status[0] = '\0';
- status_until = 0;
- status_error = 0;
- ngui_invalidate(&ui);
- }
- if (now - last_second >= 1000) {
- last_second = now;
- load_current(0);
- ngui_invalidate(&ui);
- }
- if (!ngui_begin(&ui)) {
- u_sleep_ms(10);
- continue;
- }
- action = draw();
- if (ui.focus == 0 && ngui_key_peek(&ui) == NGUI_KEY_ESC) {
- ngui_key(&ui);
- quit = 1;
- }
- ngui_end(&ui);
- if (action & ACT_SET)
- apply_now();
- if (action & ACT_NOW)
- load_current(1);
- if (action & ACT_NTP)
- sync_ntp();
- if (quit)
- break;
- u_sleep_ms(10);
- }
- u_win_close();
- u_exit(0);
- }
|