1/*
2 * Copyright 2002-2009, Axel D��rfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
5
6
7#include <errno.h>
8#include <stdio.h>
9#include <string.h>
10#include <syslog.h>
11
12#include <FindDirectory.h>
13#include <OS.h>
14
15#include <commpage_defs.h>
16#include <errno_private.h>
17#include <libroot_private.h>
18#include <real_time_data.h>
19#include <user_timer_defs.h>
20#include <syscalls.h>
21
22
23static struct real_time_data* sRealTimeData;
24
25
26void
27__init_time(void)
28{
29	sRealTimeData = (struct real_time_data*)
30		USER_COMMPAGE_TABLE[COMMPAGE_ENTRY_REAL_TIME_DATA];
31
32	__arch_init_time(sRealTimeData, false);
33}
34
35
36bigtime_t
37__get_system_time_offset()
38{
39	return __arch_get_system_time_offset(sRealTimeData);
40}
41
42
43//	#pragma mark - public API
44
45
46uint32
47real_time_clock(void)
48{
49	return (__arch_get_system_time_offset(sRealTimeData) + system_time())
50		/ 1000000;
51}
52
53
54bigtime_t
55real_time_clock_usecs(void)
56{
57	return __arch_get_system_time_offset(sRealTimeData) + system_time();
58}
59
60
61void
62set_real_time_clock(uint32 secs)
63{
64	_kern_set_real_time_clock((bigtime_t)secs * 1000000);
65}
66
67
68status_t
69set_timezone(const char* /*timezone*/)
70{
71	/* There's nothing we can do here, since we no longer support named
72	 * timezones.
73	 *
74	 * TODO: should we keep this around for compatibility or get rid of it?
75	 */
76	return B_OK;
77}
78
79
80bigtime_t
81set_alarm(bigtime_t when, uint32 mode)
82{
83	// prepare the values to be passed to the kernel
84	bigtime_t interval = 0;
85	uint32 flags = B_RELATIVE_TIMEOUT;
86
87	if (when == B_INFINITE_TIMEOUT) {
88		when = B_INFINITE_TIMEOUT;
89	} else {
90		switch (mode) {
91			case B_PERIODIC_ALARM:
92				interval = when;
93				break;
94			case B_ONE_SHOT_ABSOLUTE_ALARM:
95				flags = B_ABSOLUTE_TIMEOUT;
96				break;
97			case B_ONE_SHOT_RELATIVE_ALARM:
98			default:
99				break;
100		}
101	}
102
103	// set the timer
104	user_timer_info oldInfo;
105	status_t error = _kern_set_timer(USER_TIMER_REAL_TIME_ID, find_thread(NULL),
106		when, interval, flags, &oldInfo);
107	if (error != B_OK)
108		return 0;
109
110	// A remaining time of B_INFINITE_TIMEOUT means not scheduled.
111	return oldInfo.remaining_time != B_INFINITE_TIMEOUT
112		? oldInfo.remaining_time : 0;
113}
114