2 * Copyright (C) 2014-2015 Intel Corporation.
9 #include <hardware/sensors.h>
10 #include <utils/Atomic.h>
11 #include <linux/android_alarm.h>
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.
23 int sysfs_read(const char path[PATH_MAX], void *buf, int buf_len)
27 if (!path[0] || !buf || buf_len < 1)
30 fd = open(path, O_RDONLY);
33 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
37 len = read(fd, buf, buf_len);
42 ALOGW("Cannot read from %s (%s)\n", path, strerror(errno));
44 ALOGV("Read %d bytes from %s\n", len, path);
50 int sysfs_write(const char path[PATH_MAX], const void *buf, const int buf_len)
54 if (!path[0] || !buf || buf_len < 1)
57 fd = open(path, O_WRONLY);
60 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
64 len = write(fd, buf, buf_len);
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);
73 ALOGV("Wrote %d bytes to %s\n", buf_len, path);
79 int sysfs_write_int(const char path[PATH_MAX], int value)
86 len = sprintf(buf, "%d", value);
88 if (!path[0] || len <= 0) {
89 ALOGE("Unexpected condition in sysfs_write_int\n");
93 fd = open(path, O_WRONLY);
96 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
100 ret = write(fd, buf, len);
103 ALOGW("Cannot write %s (%d bytes) to %s (%s)\n", buf, len, path,
113 int sysfs_read_int(const char path[PATH_MAX], int *value)
119 if (!path[0] || !value) {
123 fd = open(path, O_RDONLY);
126 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
130 len = read(fd, buf, sizeof(buf));
135 ALOGW("Cannot read integer from %s (%s)\n", path,
142 ALOGV("Read %d from %s\n", *value, path);
148 int sysfs_write_str(const char path[PATH_MAX], const char *str)
154 if (!path[0] || !str || !str[0]) {
158 fd = open(path, O_WRONLY);
161 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
167 ret = write(fd, str, len);
170 ALOGW("Cannot write %s (%d bytes) to %s (%s)\n", str, len, path,
174 ALOGV("Wrote %s to %s\n", str, path);
182 int sysfs_read_str(const char path[PATH_MAX], char *buf, int buf_len)
187 if (!path[0] || !buf || buf_len < 1)
190 fd = open(path, O_RDONLY);
193 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
197 len = read(fd, buf, buf_len);
202 ALOGW("Cannot read string from %s (%s)\n", path,
207 buf[len == 0 ? 0 : len-1] = '\0';
209 ALOGV("Read %s from %s\n", buf, path);
215 int sysfs_write_float(const char path[PATH_MAX], float value)
222 len = snprintf(buf, sizeof(buf), "%g", value);
224 if (!path[0] || len <= 0) {
225 ALOGE("Unexpected condition in sysfs_write_float\n");
229 fd = open(path, O_WRONLY);
232 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
236 ret = write(fd, buf, len);
239 ALOGW("Cannot write %s (%d bytes) to %s (%s)\n", buf, len, path,
249 int sysfs_read_float(const char path[PATH_MAX], float *value)
255 if (!path[0] || !value) {
259 fd = open(path, O_RDONLY);
262 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
266 len = read(fd, buf, sizeof(buf));
271 ALOGW("Cannot read float from %s (%s)\n", path,
276 *value = (float) strtod(buf, NULL);
278 ALOGV("Read %g from %s\n", *value, path);
283 int sysfs_read_uint64(const char path[PATH_MAX], uint64_t *value)
289 if (!path[0] || !value) {
293 fd = open(path, O_RDONLY);
296 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
300 len = read(fd, buf, sizeof(buf));
305 ALOGW("Cannot read uint64 from %s (%s)\n", path,
312 ALOGV("Read %llu from %s\n", *value, path);
319 int64_t get_timestamp_realtime (void)
321 struct timespec ts = {0};
322 clock_gettime(CLOCK_REALTIME, &ts);
324 return 1000000000LL * ts.tv_sec + ts.tv_nsec;
328 int64_t get_timestamp_boot (void)
330 struct timespec ts = {0};
331 clock_gettime(CLOCK_BOOTTIME, &ts);
333 return 1000000000LL * ts.tv_sec + ts.tv_nsec;
337 int64_t get_timestamp_monotonic (void)
339 struct timespec ts = {0};
340 clock_gettime(CLOCK_MONOTONIC, &ts);
342 return 1000000000LL * ts.tv_sec + ts.tv_nsec;
346 void set_timestamp (struct timespec *out, int64_t target_ns)
348 out->tv_sec = target_ns / 1000000000LL;
349 out->tv_nsec = target_ns % 1000000000LL;