Multiuser, Capabilities and Access Controls (#54)
[lunaix-os.git] / lunaix-os / kernel / time / clock.c
1 #include <lunaix/clock.h>
2 #include <lunaix/device.h>
3 #include <lunaix/fs/twifs.h>
4 #include <lunaix/spike.h>
5
6 #include <klibc/string.h>
7
8 static void
9 __twimap_read_systime(struct twimap* map)
10 {
11     ticks_t sys_time = clock_systime();
12     twimap_printf(map, "%u", sys_time);
13 }
14
15 static void
16 __twimap_read_datetime(struct twimap* map)
17 {
18     datetime_t dt;
19     clock_walltime(&dt);
20     twimap_printf(map,
21                   "%.4d-%.2d-%.2d %.2d:%.2d:%.2d",
22                   dt.year, dt.month, dt.day,
23                   dt.hour, dt.minute, dt.second);
24 }
25
26 static void
27 __twimap_read_unixtime(struct twimap* map)
28 {
29     twimap_printf(map, "%u", clock_unixtime());
30 }
31
32 time_t
33 clock_unixtime()
34 {
35     datetime_t dt;
36     hwrtc_walltime(&dt);
37     return datetime_tounix(&dt);
38 }
39
40 time_t
41 clock_systime()
42 {
43     if (!systimer) {
44         return 0;
45     }
46
47     ticks_t t = hwtimer_current_systicks();
48     ticks_t tu = systimer->running_freq / 1000;
49
50     if (unlikely(!tu)) {
51         return t;
52     }
53
54     return t / (tu);
55 }
56
57 void
58 clock_walltime(datetime_t* datetime)
59 {
60     sysrtc->ops->get_walltime(sysrtc, datetime);
61 }
62
63 static void
64 clock_build_mapping()
65 {
66     struct twifs_node* root;
67     struct twimap* map;
68
69     root = twifs_dir_node(NULL, "clock");
70     
71     twimap_export_value(root, systime, FSACL_ugR, NULL);
72     twimap_export_value(root, unixtime, FSACL_ugR, NULL);
73     twimap_export_value(root, datetime, FSACL_ugR, NULL);
74 }
75 EXPORT_TWIFS_PLUGIN(sys_clock, clock_build_mapping);