OSDN Git Service

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