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)
153 return sysfs_write(path, str, strlen(str));
157 int sysfs_read_str(const char path[PATH_MAX], char *buf, int buf_len)
161 if (!buf || buf_len < 1)
164 len = sysfs_read(path, buf, buf_len);
167 ALOGW("Cannot read string from %s (%s)\n", path,
172 buf[len == 0 ? 0 : len - 1] = '\0';
174 ALOGV("Read %s from %s\n", buf, path);
180 int sysfs_write_float(const char path[PATH_MAX], float value)
187 len = snprintf(buf, sizeof(buf), "%g", value);
189 if (!path[0] || len <= 0) {
190 ALOGE("Unexpected condition in sysfs_write_float\n");
194 fd = open(path, O_WRONLY);
197 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
201 ret = write(fd, buf, len);
204 ALOGW("Cannot write %s (%d bytes) to %s (%s)\n", buf, len, path,
214 int sysfs_read_float(const char path[PATH_MAX], float *value)
220 if (!path[0] || !value) {
224 fd = open(path, O_RDONLY);
227 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
231 len = read(fd, buf, sizeof(buf));
236 ALOGW("Cannot read float from %s (%s)\n", path,
241 *value = (float) strtod(buf, NULL);
243 ALOGV("Read %g from %s\n", *value, path);
248 int sysfs_read_uint64(const char path[PATH_MAX], uint64_t *value)
254 if (!path[0] || !value) {
258 fd = open(path, O_RDONLY);
261 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
265 len = read(fd, buf, sizeof(buf));
270 ALOGW("Cannot read uint64 from %s (%s)\n", path,
277 ALOGV("Read %llu from %s\n", *value, path);
284 int64_t get_timestamp_realtime (void)
286 struct timespec ts = {0};
287 clock_gettime(CLOCK_REALTIME, &ts);
289 return 1000000000LL * ts.tv_sec + ts.tv_nsec;
293 int64_t get_timestamp_boot (void)
295 struct timespec ts = {0};
296 clock_gettime(CLOCK_BOOTTIME, &ts);
298 return 1000000000LL * ts.tv_sec + ts.tv_nsec;
302 int64_t get_timestamp_monotonic (void)
304 struct timespec ts = {0};
305 clock_gettime(CLOCK_MONOTONIC, &ts);
307 return 1000000000LL * ts.tv_sec + ts.tv_nsec;
311 void set_timestamp (struct timespec *out, int64_t target_ns)
313 out->tv_sec = target_ns / 1000000000LL;
314 out->tv_nsec = target_ns % 1000000000LL;