blob: 53e06264a8a6acaf659b140070fd9190c3e5bc6a [file] [log] [blame]
The Android Open Source Projectcbb10112009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18/*
19 * System clock functions.
20 */
21
Kenny Rootdafff0b2011-02-16 10:13:53 -080022#ifdef HAVE_ANDROID_OS
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080023#include <linux/ioctl.h>
24#include <linux/rtc.h>
25#include <utils/Atomic.h>
26#include <linux/android_alarm.h>
27#endif
28
29#include <sys/time.h>
30#include <limits.h>
31#include <fcntl.h>
32#include <errno.h>
33#include <string.h>
34
35#include <utils/SystemClock.h>
36#include <utils/Timers.h>
37
38#define LOG_TAG "SystemClock"
39#include "utils/Log.h"
40
41namespace android {
42
43/*
44 * Set the current time. This only works when running as root.
45 */
46int setCurrentTimeMillis(int64_t millis)
47{
48#if WIN32
49 // not implemented
50 return -1;
51#else
52 struct timeval tv;
Kenny Rootdafff0b2011-02-16 10:13:53 -080053#ifdef HAVE_ANDROID_OS
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080054 struct timespec ts;
55 int fd;
56 int res;
57#endif
58 int ret = 0;
59
60 if (millis <= 0 || millis / 1000LL >= INT_MAX) {
61 return -1;
62 }
63
64 tv.tv_sec = (time_t) (millis / 1000LL);
65 tv.tv_usec = (suseconds_t) ((millis % 1000LL) * 1000LL);
66
Steve Blockeb095332011-12-20 16:23:08 +000067 ALOGD("Setting time of day to sec=%d\n", (int) tv.tv_sec);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080068
Kenny Rootdafff0b2011-02-16 10:13:53 -080069#ifdef HAVE_ANDROID_OS
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080070 fd = open("/dev/alarm", O_RDWR);
71 if(fd < 0) {
Steve Block61d341b2012-01-05 23:22:43 +000072 ALOGW("Unable to open alarm driver: %s\n", strerror(errno));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080073 return -1;
74 }
75 ts.tv_sec = tv.tv_sec;
76 ts.tv_nsec = tv.tv_usec * 1000;
77 res = ioctl(fd, ANDROID_ALARM_SET_RTC, &ts);
78 if(res < 0) {
Steve Block61d341b2012-01-05 23:22:43 +000079 ALOGW("Unable to set rtc to %ld: %s\n", tv.tv_sec, strerror(errno));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080080 ret = -1;
81 }
82 close(fd);
83#else
84 if (settimeofday(&tv, NULL) != 0) {
Steve Block61d341b2012-01-05 23:22:43 +000085 ALOGW("Unable to set clock to %d.%d: %s\n",
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080086 (int) tv.tv_sec, (int) tv.tv_usec, strerror(errno));
87 ret = -1;
88 }
89#endif
90
91 return ret;
92#endif // WIN32
93}
94
95/*
96 * native public static long uptimeMillis();
97 */
98int64_t uptimeMillis()
99{
100 int64_t when = systemTime(SYSTEM_TIME_MONOTONIC);
101 return (int64_t) nanoseconds_to_milliseconds(when);
102}
103
104/*
105 * native public static long elapsedRealtime();
106 */
107int64_t elapsedRealtime()
108{
Nick Pellyaf1e7b72012-07-19 09:17:24 -0700109 return nanoseconds_to_milliseconds(elapsedRealtimeNano());
110}
111
112/*
113 * native public static long elapsedRealtimeNano();
114 */
115int64_t elapsedRealtimeNano()
116{
Kenny Rootdafff0b2011-02-16 10:13:53 -0800117#ifdef HAVE_ANDROID_OS
Nick Pellyaf1e7b72012-07-19 09:17:24 -0700118 struct timespec ts;
119 int result = clock_gettime(CLOCK_BOOTTIME, &ts);
120 if (result == 0) {
121 return seconds_to_nanoseconds(ts.tv_sec) + ts.tv_nsec;
122 }
123
124 // CLOCK_BOOTTIME doesn't exist, fallback to /dev/alarm
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800125 static int s_fd = -1;
126
127 if (s_fd == -1) {
128 int fd = open("/dev/alarm", O_RDONLY);
129 if (android_atomic_cmpxchg(-1, fd, &s_fd)) {
130 close(fd);
131 }
Nick Pellyaf1e7b72012-07-19 09:17:24 -0700132 result = ioctl(s_fd,
133 ANDROID_ALARM_GET_TIME(ANDROID_ALARM_ELAPSED_REALTIME), &ts);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800134 }
135
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800136 if (result == 0) {
Nick Pellyaf1e7b72012-07-19 09:17:24 -0700137 return seconds_to_nanoseconds(ts.tv_sec) + ts.tv_nsec;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800138 }
Nick Pellyaf1e7b72012-07-19 09:17:24 -0700139
140 // XXX: there was an error, probably because the driver didn't
141 // exist ... this should return
142 // a real error, like an exception!
143 return systemTime(SYSTEM_TIME_MONOTONIC);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800144#else
Nick Pellyaf1e7b72012-07-19 09:17:24 -0700145 return systemTime(SYSTEM_TIME_MONOTONIC);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800146#endif
147}
148
149}; // namespace android