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.
24 int sysfs_write_int(const char path[PATH_MAX], int value)
31 len = sprintf(buf, "%d", value);
33 if (!path[0] || len <= 0) {
34 ALOGE("Unexpected condition in sysfs_write_int\n");
38 fd = open(path, O_WRONLY);
41 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
45 ret = write(fd, buf, len);
48 ALOGW("Cannot write %s (%d bytes) to %s (%s)\n", buf, len, path,
58 int sysfs_read_int(const char path[PATH_MAX], int *value)
64 if (!path[0] || !value) {
68 fd = open(path, O_RDONLY);
71 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
75 len = read(fd, buf, sizeof(buf));
80 ALOGW("Cannot read integer from %s (%s)\n", path,
87 ALOGV("Read %d from %s\n", *value, path);
93 int sysfs_write_str(const char path[PATH_MAX], const char *str)
99 if (!path[0] || !str || !str[0]) {
103 fd = open(path, O_WRONLY);
106 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
112 ret = write(fd, str, len);
115 ALOGW("Cannot write %s (%d bytes) to %s (%s)\n", str, len, path,
119 ALOGV("Wrote %s to %s\n", str, path);
127 int sysfs_read_str(const char path[PATH_MAX], char *buf, int buf_len)
132 if (!path[0] || !buf || buf_len < 1)
135 fd = open(path, O_RDONLY);
138 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
142 len = read(fd, buf, buf_len);
147 ALOGW("Cannot read string from %s (%s)\n", path,
152 buf[len == 0 ? 0 : len-1] = '\0';
154 ALOGV("Read %s from %s\n", buf, path);
160 int sysfs_write_float(const char path[PATH_MAX], float value)
167 len = snprintf(buf, sizeof(buf), "%g", value);
169 if (!path[0] || len <= 0) {
170 ALOGE("Unexpected condition in sysfs_write_float\n");
174 fd = open(path, O_WRONLY);
177 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
181 ret = write(fd, buf, len);
184 ALOGW("Cannot write %s (%d bytes) to %s (%s)\n", buf, len, path,
194 int sysfs_read_float(const char path[PATH_MAX], float *value)
200 if (!path[0] || !value) {
204 fd = open(path, O_RDONLY);
207 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
211 len = read(fd, buf, sizeof(buf));
216 ALOGW("Cannot read float from %s (%s)\n", path,
221 *value = (float) strtod(buf, NULL);
223 ALOGV("Read %g from %s\n", *value, path);
228 int sysfs_read_uint64(const char path[PATH_MAX], uint64_t *value)
234 if (!path[0] || !value) {
238 fd = open(path, O_RDONLY);
241 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
245 len = read(fd, buf, sizeof(buf));
250 ALOGW("Cannot read uint64 from %s (%s)\n", path,
257 ALOGV("Read %llu from %s\n", *value, path);
264 int64_t get_timestamp_realtime (void)
266 struct timespec ts = {0};
267 clock_gettime(CLOCK_REALTIME, &ts);
269 return 1000000000LL * ts.tv_sec + ts.tv_nsec;
273 int64_t get_timestamp_boot (void)
275 struct timespec ts = {0};
276 clock_gettime(CLOCK_BOOTTIME, &ts);
278 return 1000000000LL * ts.tv_sec + ts.tv_nsec;
282 int64_t get_timestamp_monotonic (void)
284 struct timespec ts = {0};
285 clock_gettime(CLOCK_MONOTONIC, &ts);
287 return 1000000000LL * ts.tv_sec + ts.tv_nsec;
291 void set_timestamp (struct timespec *out, int64_t target_ns)
293 out->tv_sec = target_ns / 1000000000LL;
294 out->tv_nsec = target_ns % 1000000000LL;