Multiuser, Capabilities and Access Controls (#54)
[lunaix-os.git] / lunaix-os / kernel / time / clock.c
index be6b21ba3c61ed3a442345b7e32233f828d81498..93354bbbf645737ab63b8061c7528b7bb7dab1b4 100644 (file)
@@ -1,62 +1,34 @@
-#include <hal/hwrtc.h>
-#include <hal/hwtimer.h>
 #include <lunaix/clock.h>
+#include <lunaix/device.h>
 #include <lunaix/fs/twifs.h>
 #include <lunaix/spike.h>
 
 #include <klibc/string.h>
 
-void
-__clock_read_systime(struct twimap* map)
+static void
+__twimap_read_systime(struct twimap* map)
 {
     ticks_t sys_time = clock_systime();
     twimap_printf(map, "%u", sys_time);
 }
 
-void
-__clock_read_datetime(struct twimap* map)
+static void
+__twimap_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);
+                  dt.year, dt.month, dt.day,
+                  dt.hour, dt.minute, dt.second);
 }
 
-void
-__clock_read_unix(struct twimap* map)
+static void
+__twimap_read_unixtime(struct twimap* map)
 {
     twimap_printf(map, "%u", clock_unixtime());
 }
 
-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;
-}
-EXPORT_TWIFS_PLUGIN(sys_clock, clock_build_mapping);
-
-void
-clock_init()
-{
-    hwrtc_init();
-}
-
 time_t
 clock_unixtime()
 {
@@ -68,12 +40,36 @@ clock_unixtime()
 time_t
 clock_systime()
 {
+    if (!systimer) {
+        return 0;
+    }
+
     ticks_t t = hwtimer_current_systicks();
-    return t / current_timer->running_freq;
+    ticks_t tu = systimer->running_freq / 1000;
+
+    if (unlikely(!tu)) {
+        return t;
+    }
+
+    return t / (tu);
 }
 
 void
 clock_walltime(datetime_t* datetime)
 {
-    primary_rtc->get_walltime(primary_rtc, datetime);
-}
\ No newline at end of file
+    sysrtc->ops->get_walltime(sysrtc, datetime);
+}
+
+static void
+clock_build_mapping()
+{
+    struct twifs_node* root;
+    struct twimap* map;
+
+    root = twifs_dir_node(NULL, "clock");
+    
+    twimap_export_value(root, systime, FSACL_ugR, NULL);
+    twimap_export_value(root, unixtime, FSACL_ugR, NULL);
+    twimap_export_value(root, datetime, FSACL_ugR, NULL);
+}
+EXPORT_TWIFS_PLUGIN(sys_clock, clock_build_mapping);