2 * Copyright (C) 2014-2015 Intel Corporation.
9 #include <hardware/sensors.h>
10 #include <utils/Atomic.h>
11 #include <linux/android_alarm.h>
20 * Some of these calls are going to fail, because not all sensors expose all
21 * possible sysfs attributes. As an optimization we may want to cache which
22 * ones are valid and immediately return in error for inexistent entries.
24 int sysfs_read(const char path[PATH_MAX], void *buf, int buf_len)
28 if (!path[0] || !buf || buf_len < 1)
31 fd = open(path, O_RDONLY);
34 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
38 len = read(fd, buf, buf_len);
43 ALOGW("Cannot read from %s (%s)\n", path, strerror(errno));
45 ALOGV("Read %d bytes from %s\n", len, path);
51 int sysfs_write(const char path[PATH_MAX], const void *buf, const int buf_len)
55 if (!path[0] || !buf || buf_len < 1)
58 fd = open(path, O_WRONLY);
61 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
65 len = write(fd, buf, buf_len);
70 ALOGW("Cannot write to %s (%s)\n", path, strerror(errno));
71 else if (len != buf_len)
72 ALOGW("Cannot write %d bytes to %s (%d)\n", buf_len, path, len);
74 ALOGV("Wrote %d bytes to %s\n", buf_len, path);
80 static void str2int(const char* buf, void *v)
86 static void str2float(const char* buf, void *v)
88 *(float*)v = strtof(buf, NULL);
92 static void str2uint64(const char* buf, void *v)
94 *(uint64_t*)v = atoll(buf);
98 int sysfs_read_num(const char path[PATH_MAX], void *v,
99 void (*str2num)(const char* buf, void *v))
102 int len = sysfs_read_str(path, buf, sizeof(buf));
105 ALOGW("Cannot read number from %s (%s)\n", path,
115 int sysfs_read_int(const char path[PATH_MAX], int *value)
117 return sysfs_read_num(path, value, str2int);
121 int sysfs_read_float(const char path[PATH_MAX], float *value)
123 return sysfs_read_num(path, value, str2float);
127 int sysfs_read_uint64(const char path[PATH_MAX], uint64_t *value)
129 return sysfs_read_num(path, value, str2uint64);
133 int sysfs_write_int(const char path[PATH_MAX], int value)
136 int len = snprintf(buf, sizeof(buf), "%d", value);
139 ALOGE("Unexpected condition in sysfs_write_int\n");
143 return sysfs_write(path, buf, len);
146 int sysfs_write_float(const char path[PATH_MAX], float value)
149 int len = snprintf(buf, sizeof(buf), "%g", value);
152 ALOGE("Unexpected condition in sysfs_write_float\n");
156 return sysfs_write(path, buf, len);
160 int sysfs_write_str(const char path[PATH_MAX], const char *str)
165 return sysfs_write(path, str, strlen(str));
169 int sysfs_read_str(const char path[PATH_MAX], char *buf, int buf_len)
173 if (!buf || buf_len < 1)
176 len = sysfs_read(path, buf, buf_len);
179 ALOGW("Cannot read string from %s (%s)\n", path,
184 buf[len == 0 ? 0 : len - 1] = '\0';
186 ALOGV("Read %s from %s\n", buf, path);
192 int64_t get_timestamp_realtime (void)
194 struct timespec ts = {0};
195 clock_gettime(CLOCK_REALTIME, &ts);
197 return 1000000000LL * ts.tv_sec + ts.tv_nsec;
201 int64_t get_timestamp_boot (void)
203 struct timespec ts = {0};
204 clock_gettime(CLOCK_BOOTTIME, &ts);
206 return 1000000000LL * ts.tv_sec + ts.tv_nsec;
210 int64_t get_timestamp_monotonic (void)
212 struct timespec ts = {0};
213 clock_gettime(CLOCK_MONOTONIC, &ts);
215 return 1000000000LL * ts.tv_sec + ts.tv_nsec;
219 void set_timestamp (struct timespec *out, int64_t target_ns)
221 out->tv_sec = target_ns / 1000000000LL;
222 out->tv_nsec = target_ns % 1000000000LL;