blob: 8b1983d79c165bcb59120a5b648a1827f56dbeff [file] [log] [blame]
Mike Lockwood5ffbf262010-04-09 15:27:16 -04001/*
2 * Copyright (C) 2010 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#include <sys/time.h>
18#include <linux/ioctl.h>
19#include <linux/rtc.h>
20#include <linux/android_alarm.h>
21#include <fcntl.h>
22#include <stdio.h>
23
24
25static void format_time(int time, char* buffer) {
26 int seconds, minutes, hours, days;
27
28 seconds = time % 60;
29 time /= 60;
30 minutes = time % 60;
31 time /= 60;
32 hours = time % 24;
33 days = time / 24;
34
35 if (days > 0)
36 sprintf(buffer, "%d days, %02d:%02d:%02d", days, hours, minutes, seconds);
37 else
38 sprintf(buffer, "%02d:%02d:%02d", hours, minutes, seconds);
39}
40
41int64_t elapsedRealtime()
42{
43 struct timespec ts;
44 int fd, result;
45
46 fd = open("/dev/alarm", O_RDONLY);
47 if (fd < 0)
48 return fd;
49
50 result = ioctl(fd, ANDROID_ALARM_GET_TIME(ANDROID_ALARM_ELAPSED_REALTIME), &ts);
51 close(fd);
52
53 if (result == 0)
54 return ts.tv_sec;
55 return -1;
56}
57
58int uptime_main(int argc, char *argv[])
59{
60 float up_time, idle_time;
61 char up_string[100], idle_string[100], sleep_string[100];
62 int elapsed;
63
64 FILE* file = fopen("/proc/uptime", "r");
65 if (!file) {
66 fprintf(stderr, "Could not open /proc/uptime\n");
67 return -1;
68 }
69 if (fscanf(file, "%f %f", &up_time, &idle_time) != 2) {
70 fprintf(stderr, "Could not parse /proc/uptime\n");
71 fclose(file);
72 return -1;
73 }
74 fclose(file);
75
76 elapsed = elapsedRealtime();
77 if (elapsed < 0) {
78 fprintf(stderr, "elapsedRealtime failed\n");
79 return -1;
80 }
81
82 format_time(elapsed, up_string);
83 format_time((int)idle_time, idle_string);
84 format_time((int)(elapsed - up_time), sleep_string);
85 printf("up time: %s, idle time: %s, sleep time: %s\n", up_string, idle_string, sleep_string);
86
87 return 0;
88}