refactor: decouple i386 specific instruction invocation
[lunaix-os.git] / lunaix-os / kernel / time / clock.c
index 15a6a32aaf5fbf67527457578d30ebf52f46f18c..e91be19ef18fd61ba2538079e1376247b2f1a397 100644 (file)
@@ -1,13 +1,62 @@
 #include <hal/rtc.h>
 #include <lunaix/clock.h>
+#include <lunaix/fs/twifs.h>
 #include <lunaix/spike.h>
 #include <lunaix/timer.h>
 
+#include <klibc/string.h>
+
 static volatile time_t sys_time;
 
 void
 clock_systime_counter(void* arg);
 
+void
+__clock_read_systime(struct twimap* map)
+{
+    time_t save = sys_time;
+    twimap_printf(map, "%u", save);
+}
+
+void
+__clock_read_datetime(struct twimap* map)
+{
+    datetime_t dt;
+    clock_walltime(&dt);
+    twimap_printf(map,
+                  "%.4d-%.2d-%.2d %.2d:%.2d:%.2d",
+                  dt.year,
+                  dt.month,
+                  dt.day,
+                  dt.hour,
+                  dt.minute,
+                  dt.second);
+}
+
+void
+__clock_read_unix(struct twimap* map)
+{
+    datetime_t dt;
+    clock_walltime(&dt);
+    twimap_printf(map, "%u", clock_tounixtime(&dt));
+}
+
+void
+clock_build_mapping()
+{
+    struct twifs_node* root = twifs_dir_node(NULL, "clock");
+    struct twimap* map;
+
+    map = twifs_mapping(root, NULL, "systime");
+    map->read = __clock_read_systime;
+
+    map = twifs_mapping(root, NULL, "unix");
+    map->read = __clock_read_unix;
+
+    map = twifs_mapping(root, NULL, "datetime");
+    map->read = __clock_read_datetime;
+}
+
 void
 clock_init()
 {
@@ -17,6 +66,8 @@ clock_init()
 
     // 系统计时器每毫秒累加。
     timer_run_ms(1, clock_systime_counter, NULL, TIMER_MODE_PERIODIC);
+
+    clock_build_mapping();
 }
 
 void
@@ -52,7 +103,7 @@ clock_walltime(datetime_t* datetime)
         datetime->second = rtc_read_reg(RTC_REG_SEC);
     } while (!clock_datatime_eq(datetime, &current));
 
-    uint8_t regbv = rtc_read_reg(RTC_REG_B);
+    u8_t regbv = rtc_read_reg(RTC_REG_B);
 
     // Convert from bcd to binary when needed
     if (!RTC_BIN_ENCODED(regbv)) {
@@ -66,7 +117,7 @@ clock_walltime(datetime_t* datetime)
 
     // To 24 hour format
     if (!RTC_24HRS_ENCODED(regbv) && (datetime->hour >> 7)) {
-        datetime->hour = (12 + datetime->hour & 0x80);
+        datetime->hour = 12 + (datetime->hour & 0x80);
     }
 
     datetime->year += RTC_CURRENT_CENTRY * 100;