OSDN Git Service

Fix 64bit builds on master
[android-x86/hardware-intel-libsensors.git] / utils.c
1 /*
2  * Copyright (C) 2014-2015 Intel Corporation.
3  */
4
5 #include <stdlib.h>
6 #include <fcntl.h>
7 #include <sys/ioctl.h>
8 #include <utils/Log.h>
9 #include <hardware/sensors.h>
10 #include <utils/Atomic.h>
11 #include <linux/android_alarm.h>
12
13 #include "common.h"
14 #include "utils.h"
15
16 #include <errno.h>
17
18 /* Note:
19  *
20  * Some of these calls are going to fail, because not all sensors expose all
21  * possible sysfs attributes. As an optimization we may want to cache which
22  * ones are valid and immediately return in error for inexistent entries.
23  */
24 int sysfs_read(const char path[PATH_MAX], void *buf, int buf_len)
25 {
26         int fd, len;
27
28         if (!path[0] || !buf || buf_len < 1)
29                 return -1;
30
31         fd = open(path, O_RDONLY);
32
33         if (fd == -1) {
34                 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
35                 return -1;
36         }
37
38         len = read(fd, buf, buf_len);
39
40         close(fd);
41
42         if (len == -1)
43                 ALOGW("Cannot read from %s (%s)\n", path, strerror(errno));
44         else
45                 ALOGV("Read %d bytes from %s\n", len, path);
46
47         return len;
48 }
49
50
51 int sysfs_write(const char path[PATH_MAX], const void *buf, const int buf_len)
52 {
53         int fd, len;
54
55         if (!path[0] || !buf || buf_len < 1)
56                 return -1;
57
58         fd = open(path, O_WRONLY);
59
60         if (fd == -1) {
61                 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
62                 return -1;
63         }
64
65         len = write(fd, buf, buf_len);
66
67         close(fd);
68
69         if (len == -1)
70                 ALOGW("Cannot write to %s (%s)\n", path, strerror(errno));
71         else if (len != buf_len)
72                 ALOGW("Cannot write %d bytes to %s (%d)\n", buf_len, path, len);
73         else
74                 ALOGV("Wrote %d bytes to %s\n", buf_len, path);
75
76         return len;
77 }
78
79
80 static void str2int(const char* buf, void *v)
81 {
82         *(int*)v = atoi(buf);
83 }
84
85
86 static void str2float(const char* buf, void *v)
87 {
88         *(float*)v = strtof(buf, NULL);
89 }
90
91
92 static void str2uint64(const char* buf, void *v)
93 {
94         *(uint64_t*)v = atoll(buf);
95 }
96
97
98 int sysfs_read_num(const char path[PATH_MAX], void *v,
99                 void (*str2num)(const char* buf, void *v))
100 {
101         char buf[20];
102         int len = sysfs_read_str(path, buf, sizeof(buf));
103
104         if (len <= 0) {
105                 ALOGW("Cannot read number from %s (%s)\n", path,
106                       strerror(errno));
107                 return -1;
108         }
109
110         str2num(buf, v);
111         return 0;
112 }
113
114
115 int sysfs_read_int(const char path[PATH_MAX], int *value)
116 {
117         return sysfs_read_num(path, value, str2int);
118 }
119
120
121 int sysfs_read_float(const char path[PATH_MAX], float *value)
122 {
123         return sysfs_read_num(path, value, str2float);
124 }
125
126
127 int sysfs_read_uint64(const char path[PATH_MAX], uint64_t *value)
128 {
129         return sysfs_read_num(path, value, str2uint64);
130 }
131
132
133 int sysfs_write_int(const char path[PATH_MAX], int value)
134 {
135         char buf[20];
136         int len = snprintf(buf, sizeof(buf), "%d", value);
137
138         if (len <= 0) {
139                 ALOGE("Unexpected condition in sysfs_write_int\n");
140                 return -1;
141         }
142
143         return sysfs_write(path, buf, len);
144 }
145
146 int sysfs_write_float(const char path[PATH_MAX], float value)
147 {
148         char buf[20];
149         int len = snprintf(buf, sizeof(buf), "%g", value);
150
151         if (len <= 0) {
152                 ALOGE("Unexpected condition in sysfs_write_float\n");
153                 return -1;
154         }
155
156         return sysfs_write(path, buf, len);
157 }
158
159
160 int sysfs_write_str(const char path[PATH_MAX], const char *str)
161 {
162         if (!str || !str[0])
163                 return -1;
164
165         return sysfs_write(path, str, strlen(str));
166 }
167
168
169 int sysfs_read_str(const char path[PATH_MAX], char *buf, int buf_len)
170 {
171         int len;
172
173         if (!buf || buf_len < 1)
174                 return -1;
175
176         len = sysfs_read(path, buf, buf_len);
177
178         if (len == -1) {
179                 ALOGW("Cannot read string from %s (%s)\n", path,
180                       strerror(errno));
181                 return -1;
182         }
183
184         buf[len == 0 ? 0 : len - 1] = '\0';
185
186         ALOGV("Read %s from %s\n", buf, path);
187
188         return len;
189 }
190
191
192 int64_t get_timestamp_realtime (void)
193 {
194         struct timespec ts = {0};
195         clock_gettime(CLOCK_REALTIME, &ts);
196
197         return 1000000000LL * ts.tv_sec + ts.tv_nsec;
198 }
199
200
201 int64_t get_timestamp_boot (void)
202 {
203         struct timespec ts = {0};
204         clock_gettime(CLOCK_BOOTTIME, &ts);
205
206         return 1000000000LL * ts.tv_sec + ts.tv_nsec;
207 }
208
209
210 int64_t get_timestamp_monotonic (void)
211 {
212         struct timespec ts = {0};
213         clock_gettime(CLOCK_MONOTONIC, &ts);
214
215         return 1000000000LL * ts.tv_sec + ts.tv_nsec;
216 }
217
218
219 void set_timestamp (struct timespec *out, int64_t target_ns)
220 {
221         out->tv_sec  = target_ns / 1000000000LL;
222         out->tv_nsec = target_ns % 1000000000LL;
223 }
224