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