OSDN Git Service

7016cdac377c8dea13ba52618844c7ca09fcfa67
[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
17 /* Note:
18  *
19  * Some of these calls are going to fail, because not all sensors expose all
20  * possible sysfs attributes. As an optimization we may want to cache which
21  * ones are valid and immediately return in error for inexistent entries.
22  */
23 int sysfs_read(const char path[PATH_MAX], void *buf, int buf_len)
24 {
25         int fd, len;
26
27         if (!path[0] || !buf || buf_len < 1)
28                 return -1;
29
30         fd = open(path, O_RDONLY);
31
32         if (fd == -1) {
33                 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
34                 return -1;
35         }
36
37         len = read(fd, buf, buf_len);
38
39         close(fd);
40
41         if (len == -1)
42                 ALOGW("Cannot read from %s (%s)\n", path, strerror(errno));
43         else
44                 ALOGV("Read %d bytes from %s\n", len, path);
45
46         return len;
47 }
48
49
50 int sysfs_write(const char path[PATH_MAX], const void *buf, const int buf_len)
51 {
52         int fd, len;
53
54         if (!path[0] || !buf || buf_len < 1)
55                 return -1;
56
57         fd = open(path, O_WRONLY);
58
59         if (fd == -1) {
60                 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
61                 return -1;
62         }
63
64         len = write(fd, buf, buf_len);
65
66         close(fd);
67
68         if (len == -1)
69                 ALOGW("Cannot write to %s (%s)\n", path, strerror(errno));
70         else if (len != buf_len)
71                 ALOGW("Cannot write %d bytes to %s (%d)\n", buf_len, path, len);
72         else
73                 ALOGV("Wrote %d bytes to %s\n", buf_len, path);
74
75         return len;
76 }
77
78
79 int sysfs_write_int(const char path[PATH_MAX], int value)
80 {
81         int ret;
82         int fd;
83         int len;
84         char buf[20];
85
86         len = sprintf(buf, "%d", value);
87
88         if (!path[0] || len <= 0) {
89                 ALOGE("Unexpected condition in sysfs_write_int\n");
90                 return -1;
91         }
92
93         fd = open(path, O_WRONLY);
94
95         if (fd == -1) {
96                 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
97                 return -1;
98         }
99
100         ret = write(fd, buf, len);
101
102         if (ret != len) {
103                 ALOGW("Cannot write %s (%d bytes) to %s (%s)\n", buf, len, path,
104                       strerror(errno));
105         }
106
107         close(fd);
108
109         return ret;
110 }
111
112
113 int sysfs_read_int(const char path[PATH_MAX], int *value)
114 {
115         int fd;
116         int len;
117         char buf[20] = {0};
118
119         if (!path[0] || !value) {
120                 return -1;
121         }
122
123         fd = open(path, O_RDONLY);
124
125         if (fd == -1) {
126                 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
127                 return -1;
128         }
129
130         len = read(fd, buf, sizeof(buf));
131
132         close(fd);
133
134         if (len <= 0) {
135                 ALOGW("Cannot read integer from %s (%s)\n", path,
136                       strerror(errno));
137                 return -1;
138         }
139
140         *value = atoi(buf);
141
142         ALOGV("Read %d from %s\n", *value, path);
143
144         return 0;
145 }
146
147
148 int sysfs_write_str(const char path[PATH_MAX], const char *str)
149 {
150         if (!str || !str[0])
151                 return -1;
152
153         return sysfs_write(path, str, strlen(str));
154 }
155
156
157 int sysfs_read_str(const char path[PATH_MAX], char *buf, int buf_len)
158 {
159         int len;
160
161         if (!buf || buf_len < 1)
162                 return -1;
163
164         len = sysfs_read(path, buf, buf_len);
165
166         if (len == -1) {
167                 ALOGW("Cannot read string from %s (%s)\n", path,
168                       strerror(errno));
169                 return -1;
170         }
171
172         buf[len == 0 ? 0 : len - 1] = '\0';
173
174         ALOGV("Read %s from %s\n", buf, path);
175
176         return len;
177 }
178
179
180 int sysfs_write_float(const char path[PATH_MAX], float value)
181 {
182         int ret;
183         int fd;
184         int len;
185         char buf[20];
186
187         len = snprintf(buf, sizeof(buf), "%g", value);
188
189         if (!path[0] || len <= 0) {
190                 ALOGE("Unexpected condition in sysfs_write_float\n");
191                 return -1;
192         }
193
194         fd = open(path, O_WRONLY);
195
196         if (fd == -1) {
197                 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
198                 return -1;
199         }
200
201         ret = write(fd, buf, len);
202
203         if (ret != len) {
204                 ALOGW("Cannot write %s (%d bytes) to %s (%s)\n", buf, len, path,
205                       strerror(errno));
206         }
207
208         close(fd);
209
210         return ret;
211 }
212
213
214 int sysfs_read_float(const char path[PATH_MAX], float *value)
215 {
216         int fd;
217         int len;
218         char buf[20] = {0};
219
220         if (!path[0] || !value) {
221                 return -1;
222         }
223
224         fd = open(path, O_RDONLY);
225
226         if (fd == -1) {
227                 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
228                 return -1;
229         }
230
231         len = read(fd, buf, sizeof(buf));
232
233         close(fd);
234
235         if (len <= 0) {
236                 ALOGW("Cannot read float from %s (%s)\n", path,
237                       strerror(errno));
238                 return -1;
239         }
240
241         *value = (float) strtod(buf, NULL);
242
243         ALOGV("Read %g from %s\n", *value, path);
244
245         return 0;
246 }
247
248 int sysfs_read_uint64(const char path[PATH_MAX], uint64_t *value)
249 {
250         int fd;
251         int len;
252         char buf[20] = {0};
253
254         if (!path[0] || !value) {
255                 return -1;
256         }
257
258         fd = open(path, O_RDONLY);
259
260         if (fd == -1) {
261                 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
262                 return -1;
263         }
264
265         len = read(fd, buf, sizeof(buf));
266
267         close(fd);
268
269         if (len <= 0) {
270                 ALOGW("Cannot read uint64 from %s (%s)\n", path,
271                       strerror(errno));
272                 return -1;
273         }
274
275         *value =  atoll(buf);
276
277         ALOGV("Read %llu from %s\n", *value, path);
278
279         return 0;
280 }
281
282
283
284 int64_t get_timestamp_realtime (void)
285 {
286         struct timespec ts = {0};
287         clock_gettime(CLOCK_REALTIME, &ts);
288
289         return 1000000000LL * ts.tv_sec + ts.tv_nsec;
290 }
291
292
293 int64_t get_timestamp_boot (void)
294 {
295         struct timespec ts = {0};
296         clock_gettime(CLOCK_BOOTTIME, &ts);
297
298         return 1000000000LL * ts.tv_sec + ts.tv_nsec;
299 }
300
301
302 int64_t get_timestamp_monotonic (void)
303 {
304         struct timespec ts = {0};
305         clock_gettime(CLOCK_MONOTONIC, &ts);
306
307         return 1000000000LL * ts.tv_sec + ts.tv_nsec;
308 }
309
310
311 void set_timestamp (struct timespec *out, int64_t target_ns)
312 {
313         out->tv_sec  = target_ns / 1000000000LL;
314         out->tv_nsec = target_ns % 1000000000LL;
315 }
316