2 * Copyright (C) 2014 Intel Corporation.
8 #include <hardware/sensors.h>
9 #include <utils/Atomic.h>
10 #include <linux/android_alarm.h>
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.
23 int sysfs_write_int(const char path[PATH_MAX], int value)
30 len = sprintf(buf, "%d", value);
32 if (!path[0] || len <= 0) {
33 ALOGE("Unexpected condition in sysfs_write_int\n");
37 fd = open(path, O_WRONLY);
40 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
44 ret = write(fd, buf, len);
47 ALOGW("Cannot write %s (%d bytes) to %s (%s)\n", buf, len, path,
57 int sysfs_read_int(const char path[PATH_MAX], int *value)
63 if (!path[0] || !value) {
67 fd = open(path, O_RDONLY);
70 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
74 len = read(fd, buf, sizeof(buf));
79 ALOGW("Cannot read integer from %s (%s)\n", path,
86 ALOGV("Read %d from %s\n", *value, path);
92 int sysfs_write_str(const char path[PATH_MAX], const char *str)
98 if (!path[0] || !str || !str[0]) {
102 fd = open(path, O_WRONLY);
105 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
111 ret = write(fd, str, len);
114 ALOGW("Cannot write %s (%d bytes) to %s (%s)\n", str, len, path,
118 ALOGV("Wrote %s to %s\n", str, path);
126 int sysfs_read_str(const char path[PATH_MAX], char *buf, int buf_len)
131 if (!path[0] || !buf || buf_len < 1)
134 fd = open(path, O_RDONLY);
137 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
141 len = read(fd, buf, buf_len);
146 ALOGW("Cannot read string from %s (%s)\n", path,
151 buf[len == 0 ? 0 : len-1] = '\0';
153 ALOGV("Read %s from %s\n", buf, path);
159 int sysfs_write_float(const char path[PATH_MAX], float value)
166 len = snprintf(buf, sizeof(buf), "%g", value);
168 if (!path[0] || len <= 0) {
169 ALOGE("Unexpected condition in sysfs_write_float\n");
173 fd = open(path, O_WRONLY);
176 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
180 ret = write(fd, buf, len);
183 ALOGW("Cannot write %s (%d bytes) to %s (%s)\n", buf, len, path,
193 int sysfs_read_float(const char path[PATH_MAX], float *value)
199 if (!path[0] || !value) {
203 fd = open(path, O_RDONLY);
206 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
210 len = read(fd, buf, sizeof(buf));
215 ALOGW("Cannot read float from %s (%s)\n", path,
220 *value = (float) strtod(buf, NULL);
222 ALOGV("Read %g from %s\n", *value, path);
228 int64_t get_timestamp_realtime (void)
230 struct timespec ts = {0};
231 clock_gettime(CLOCK_REALTIME, &ts);
233 return 1000000000LL * ts.tv_sec + ts.tv_nsec;
237 int64_t get_timestamp_boot (void)
239 struct timespec ts = {0};
240 clock_gettime(CLOCK_BOOTTIME, &ts);
242 return 1000000000LL * ts.tv_sec + ts.tv_nsec;
246 int64_t get_timestamp_monotonic (void)
248 struct timespec ts = {0};
249 clock_gettime(CLOCK_MONOTONIC, &ts);
251 return 1000000000LL * ts.tv_sec + ts.tv_nsec;
255 void set_timestamp (struct timespec *out, int64_t target_ns)
257 out->tv_sec = target_ns / 1000000000LL;
258 out->tv_nsec = target_ns % 1000000000LL;