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)
135 int len = snprintf(buf, sizeof(buf), "%d", value);
138 ALOGE("Unexpected condition in sysfs_write_int\n");
142 return sysfs_write(path, buf, len);
145 int sysfs_write_float(const char path[PATH_MAX], float value)
148 int len = snprintf(buf, sizeof(buf), "%g", value);
151 ALOGE("Unexpected condition in sysfs_write_float\n");
155 return sysfs_write(path, buf, len);
159 int sysfs_write_str(const char path[PATH_MAX], const char *str)
164 return sysfs_write(path, str, strlen(str));
168 int sysfs_read_str(const char path[PATH_MAX], char *buf, int buf_len)
172 if (!buf || buf_len < 1)
175 len = sysfs_read(path, buf, buf_len);
178 ALOGW("Cannot read string from %s (%s)\n", path,
183 buf[len == 0 ? 0 : len - 1] = '\0';
185 ALOGV("Read %s from %s\n", buf, path);
191 int64_t get_timestamp_realtime (void)
193 struct timespec ts = {0};
194 clock_gettime(CLOCK_REALTIME, &ts);
196 return 1000000000LL * ts.tv_sec + ts.tv_nsec;
200 int64_t get_timestamp_boot (void)
202 struct timespec ts = {0};
203 clock_gettime(CLOCK_BOOTTIME, &ts);
205 return 1000000000LL * ts.tv_sec + ts.tv_nsec;
209 int64_t get_timestamp_monotonic (void)
211 struct timespec ts = {0};
212 clock_gettime(CLOCK_MONOTONIC, &ts);
214 return 1000000000LL * ts.tv_sec + ts.tv_nsec;
218 void set_timestamp (struct timespec *out, int64_t target_ns)
220 out->tv_sec = target_ns / 1000000000LL;
221 out->tv_nsec = target_ns % 1000000000LL;