OSDN Git Service

Utils review - introducing base read and write functions
[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         int ret;
151         int fd;
152         int len;
153
154         if (!path[0] || !str || !str[0]) {
155                 return -1;
156         }
157
158         fd = open(path, O_WRONLY);
159
160         if (fd == -1) {
161                 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
162                 return -1;
163         }
164
165         len = strlen(str);
166
167         ret = write(fd, str, len);
168
169         if (ret != len) {
170                 ALOGW("Cannot write %s (%d bytes) to %s (%s)\n", str, len, path,
171                       strerror(errno));
172         }
173         else
174                 ALOGV("Wrote %s to %s\n", str, path);
175
176         close(fd);
177
178         return ret;
179 }
180
181
182 int sysfs_read_str(const char path[PATH_MAX], char *buf, int buf_len)
183 {
184         int fd;
185         int len;
186
187         if (!path[0] || !buf || buf_len < 1)
188                 return -1;
189
190         fd = open(path, O_RDONLY);
191
192         if (fd == -1) {
193                 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
194                 return -1;
195         }
196
197         len = read(fd, buf, buf_len);
198
199         close(fd);
200
201         if (len == -1) {
202                 ALOGW("Cannot read string from %s (%s)\n", path,
203                       strerror(errno));
204                 return -1;
205         }
206
207         buf[len == 0 ? 0 : len-1] = '\0';
208
209         ALOGV("Read %s from %s\n", buf, path);
210
211         return len;
212 }
213
214
215 int sysfs_write_float(const char path[PATH_MAX], float value)
216 {
217         int ret;
218         int fd;
219         int len;
220         char buf[20];
221
222         len = snprintf(buf, sizeof(buf), "%g", value);
223
224         if (!path[0] || len <= 0) {
225                 ALOGE("Unexpected condition in sysfs_write_float\n");
226                 return -1;
227         }
228
229         fd = open(path, O_WRONLY);
230
231         if (fd == -1) {
232                 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
233                 return -1;
234         }
235
236         ret = write(fd, buf, len);
237
238         if (ret != len) {
239                 ALOGW("Cannot write %s (%d bytes) to %s (%s)\n", buf, len, path,
240                       strerror(errno));
241         }
242
243         close(fd);
244
245         return ret;
246 }
247
248
249 int sysfs_read_float(const char path[PATH_MAX], float *value)
250 {
251         int fd;
252         int len;
253         char buf[20] = {0};
254
255         if (!path[0] || !value) {
256                 return -1;
257         }
258
259         fd = open(path, O_RDONLY);
260
261         if (fd == -1) {
262                 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
263                 return -1;
264         }
265
266         len = read(fd, buf, sizeof(buf));
267
268         close(fd);
269
270         if (len <= 0) {
271                 ALOGW("Cannot read float from %s (%s)\n", path,
272                       strerror(errno));
273                 return -1;
274         }
275
276         *value = (float) strtod(buf, NULL);
277
278         ALOGV("Read %g from %s\n", *value, path);
279
280         return 0;
281 }
282
283 int sysfs_read_uint64(const char path[PATH_MAX], uint64_t *value)
284 {
285         int fd;
286         int len;
287         char buf[20] = {0};
288
289         if (!path[0] || !value) {
290                 return -1;
291         }
292
293         fd = open(path, O_RDONLY);
294
295         if (fd == -1) {
296                 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
297                 return -1;
298         }
299
300         len = read(fd, buf, sizeof(buf));
301
302         close(fd);
303
304         if (len <= 0) {
305                 ALOGW("Cannot read uint64 from %s (%s)\n", path,
306                       strerror(errno));
307                 return -1;
308         }
309
310         *value =  atoll(buf);
311
312         ALOGV("Read %llu from %s\n", *value, path);
313
314         return 0;
315 }
316
317
318
319 int64_t get_timestamp_realtime (void)
320 {
321         struct timespec ts = {0};
322         clock_gettime(CLOCK_REALTIME, &ts);
323
324         return 1000000000LL * ts.tv_sec + ts.tv_nsec;
325 }
326
327
328 int64_t get_timestamp_boot (void)
329 {
330         struct timespec ts = {0};
331         clock_gettime(CLOCK_BOOTTIME, &ts);
332
333         return 1000000000LL * ts.tv_sec + ts.tv_nsec;
334 }
335
336
337 int64_t get_timestamp_monotonic (void)
338 {
339         struct timespec ts = {0};
340         clock_gettime(CLOCK_MONOTONIC, &ts);
341
342         return 1000000000LL * ts.tv_sec + ts.tv_nsec;
343 }
344
345
346 void set_timestamp (struct timespec *out, int64_t target_ns)
347 {
348         out->tv_sec  = target_ns / 1000000000LL;
349         out->tv_nsec = target_ns % 1000000000LL;
350 }
351