a5b9dd838a6a04e119f3ef28a082d0ddec455d5e
[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 void
9 __clock_read_systime(struct twimap* map)
10 {
11     ticks_t sys_time = clock_systime();
12     twimap_printf(map, "%u", sys_time);
13 }
14
15 void
16 __clock_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 void
27 __clock_read_unix(struct twimap* map)
28 {
29     twimap_printf(map, "%u", clock_unixtime());
30 }
31
32 void
33 clock_build_mapping()
34 {
35     struct twifs_node* root = twifs_dir_node(NULL, "clock");
36     struct twimap* map;
37
38     map = twifs_mapping(root, NULL, "systime");
39     map->read = __clock_read_systime;
40
41     map = twifs_mapping(root, NULL, "unix");
42     map->read = __clock_read_unix;
43
44     map = twifs_mapping(root, NULL, "datetime");
45     map->read = __clock_read_datetime;
46 }
47 EXPORT_TWIFS_PLUGIN(sys_clock, clock_build_mapping);
48
49 time_t
50 clock_unixtime()
51 {
52     datetime_t dt;
53     hwrtc_walltime(&dt);
54     return datetime_tounix(&dt);
55 }
56
57 time_t
58 clock_systime()
59 {
60     if (!systimer) {
61         return 0;
62     }
63
64     ticks_t t = hwtimer_current_systicks();
65     ticks_t tu = systimer->running_freq / 1000;
66
67     if (unlikely(!tu)) {
68         return t;
69     }
70
71     return t / (tu);
72 }
73
74 void
75 clock_walltime(datetime_t* datetime)
76 {
77     sysrtc->ops->get_walltime(sysrtc, datetime);
78 }