* fix an issue that execve attempts to parse directory as elf file.
[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,
23                   dt.month,
24                   dt.day,
25                   dt.hour,
26                   dt.minute,
27                   dt.second);
28 }
29
30 void
31 __clock_read_unix(struct twimap* map)
32 {
33     twimap_printf(map, "%u", clock_unixtime());
34 }
35
36 void
37 clock_build_mapping()
38 {
39     struct twifs_node* root = twifs_dir_node(NULL, "clock");
40     struct twimap* map;
41
42     map = twifs_mapping(root, NULL, "systime");
43     map->read = __clock_read_systime;
44
45     map = twifs_mapping(root, NULL, "unix");
46     map->read = __clock_read_unix;
47
48     map = twifs_mapping(root, NULL, "datetime");
49     map->read = __clock_read_datetime;
50 }
51 EXPORT_TWIFS_PLUGIN(sys_clock, clock_build_mapping);
52
53 time_t
54 clock_unixtime()
55 {
56     datetime_t dt;
57     hwrtc_walltime(&dt);
58     return datetime_tounix(&dt);
59 }
60
61 time_t
62 clock_systime()
63 {
64     if (!systimer) {
65         return 0;
66     }
67
68     ticks_t t = hwtimer_current_systicks();
69     ticks_t tu = systimer->running_freq / 1000;
70
71     if (unlikely(!tu)) {
72         return t;
73     }
74
75     return t / (tu);
76 }
77
78 void
79 clock_walltime(datetime_t* datetime)
80 {
81     sysrtc->get_walltime(sysrtc, datetime);
82 }
83
84 void
85 clock_init()
86 {
87     int idx = 0;
88     struct device_def* pos;
89     foreach_exported_device_of(load_timedev, idx, pos)
90     {
91         if (pos->class.device != DEV_RTC) {
92             continue;
93         }
94
95         pos->init(pos);
96     }
97 }