OSDN Git Service

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