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 static void str2int(const char* buf, void *v)
85 static void str2float(const char* buf, void *v)
87 *(float*)v = strtof(buf, NULL);
91 static void str2uint64(const char* buf, void *v)
93 *(uint64_t*)v = atoll(buf);
97 int sysfs_read_num(const char path[PATH_MAX], void *v,
98 void (*str2num)(const char* buf, void *v))
101 int len = sysfs_read_str(path, buf, sizeof(buf));
104 ALOGW("Cannot read number from %s (%s)\n", path,
114 int sysfs_read_int(const char path[PATH_MAX], int *value)
116 return sysfs_read_num(path, value, str2int);
120 int sysfs_read_float(const char path[PATH_MAX], float *value)
122 return sysfs_read_num(path, value, str2float);
126 int sysfs_read_uint64(const char path[PATH_MAX], uint64_t *value)
128 return sysfs_read_num(path, value, str2uint64);
132 int sysfs_write_int(const char path[PATH_MAX], int value)
139 len = sprintf(buf, "%d", value);
141 if (!path[0] || len <= 0) {
142 ALOGE("Unexpected condition in sysfs_write_int\n");
146 fd = open(path, O_WRONLY);
149 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
153 ret = write(fd, buf, len);
156 ALOGW("Cannot write %s (%d bytes) to %s (%s)\n", buf, len, path,
166 int sysfs_write_str(const char path[PATH_MAX], const char *str)
171 return sysfs_write(path, str, strlen(str));
175 int sysfs_read_str(const char path[PATH_MAX], char *buf, int buf_len)
179 if (!buf || buf_len < 1)
182 len = sysfs_read(path, buf, buf_len);
185 ALOGW("Cannot read string from %s (%s)\n", path,
190 buf[len == 0 ? 0 : len - 1] = '\0';
192 ALOGV("Read %s from %s\n", buf, path);
198 int sysfs_write_float(const char path[PATH_MAX], float value)
205 len = snprintf(buf, sizeof(buf), "%g", value);
207 if (!path[0] || len <= 0) {
208 ALOGE("Unexpected condition in sysfs_write_float\n");
212 fd = open(path, O_WRONLY);
215 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
219 ret = write(fd, buf, len);
222 ALOGW("Cannot write %s (%d bytes) to %s (%s)\n", buf, len, path,
232 int64_t get_timestamp_realtime (void)
234 struct timespec ts = {0};
235 clock_gettime(CLOCK_REALTIME, &ts);
237 return 1000000000LL * ts.tv_sec + ts.tv_nsec;
241 int64_t get_timestamp_boot (void)
243 struct timespec ts = {0};
244 clock_gettime(CLOCK_BOOTTIME, &ts);
246 return 1000000000LL * ts.tv_sec + ts.tv_nsec;
250 int64_t get_timestamp_monotonic (void)
252 struct timespec ts = {0};
253 clock_gettime(CLOCK_MONOTONIC, &ts);
255 return 1000000000LL * ts.tv_sec + ts.tv_nsec;
259 void set_timestamp (struct timespec *out, int64_t target_ns)
261 out->tv_sec = target_ns / 1000000000LL;
262 out->tv_nsec = target_ns % 1000000000LL;