2 // Copyright (c) 2015 Intel Corporation
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
19 #include <sys/ioctl.h>
20 #include <utils/Log.h>
21 #include <hardware/sensors.h>
22 #include <utils/Atomic.h>
23 #include <linux/android_alarm.h>
32 * Some of these calls are going to fail, because not all sensors expose all
33 * possible sysfs attributes. As an optimization we may want to cache which
34 * ones are valid and immediately return in error for inexistent entries.
36 int sysfs_read(const char path[PATH_MAX], void *buf, int buf_len)
40 if (!path[0] || !buf || buf_len < 1)
43 fd = open(path, O_RDONLY);
46 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
50 len = read(fd, buf, buf_len);
55 ALOGW("Cannot read from %s (%s)\n", path, strerror(errno));
57 ALOGV("Read %d bytes from %s\n", len, path);
63 int sysfs_write(const char path[PATH_MAX], const void *buf, const int buf_len)
67 if (!path[0] || !buf || buf_len < 1)
70 fd = open(path, O_WRONLY);
73 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
77 len = write(fd, buf, buf_len);
82 ALOGW("Cannot write to %s (%s)\n", path, strerror(errno));
83 else if (len != buf_len)
84 ALOGW("Cannot write %d bytes to %s (%d)\n", buf_len, path, len);
86 ALOGV("Wrote %d bytes to %s\n", buf_len, path);
92 static void str2int(const char* buf, void *v)
98 static void str2float(const char* buf, void *v)
100 *(float*)v = strtof(buf, NULL);
104 static void str2uint64(const char* buf, void *v)
106 *(uint64_t*)v = atoll(buf);
110 int sysfs_read_num(const char path[PATH_MAX], void *v,
111 void (*str2num)(const char* buf, void *v))
114 int len = sysfs_read_str(path, buf, sizeof(buf));
117 ALOGW("Cannot read number from %s (%s)\n", path,
127 int sysfs_read_int(const char path[PATH_MAX], int *value)
129 return sysfs_read_num(path, value, str2int);
133 int sysfs_read_float(const char path[PATH_MAX], float *value)
135 return sysfs_read_num(path, value, str2float);
139 int sysfs_read_uint64(const char path[PATH_MAX], uint64_t *value)
141 return sysfs_read_num(path, value, str2uint64);
145 int sysfs_write_int(const char path[PATH_MAX], int value)
148 int len = snprintf(buf, sizeof(buf), "%d", value);
151 ALOGE("Unexpected condition in sysfs_write_int\n");
155 return sysfs_write(path, buf, len);
158 int sysfs_write_float(const char path[PATH_MAX], float value)
161 int len = snprintf(buf, sizeof(buf), "%g", value);
164 ALOGE("Unexpected condition in sysfs_write_float\n");
168 return sysfs_write(path, buf, len);
172 int sysfs_write_str(const char path[PATH_MAX], const char *str)
177 return sysfs_write(path, str, strlen(str));
181 int sysfs_read_str(const char path[PATH_MAX], char *buf, int buf_len)
185 if (!buf || buf_len < 1)
188 len = sysfs_read(path, buf, buf_len);
191 ALOGW("Cannot read string from %s (%s)\n", path,
196 buf[len == 0 ? 0 : len - 1] = '\0';
198 ALOGV("Read %s from %s\n", buf, path);
204 int64_t get_timestamp (clockid_t clock_id)
206 struct timespec ts = {0};
208 if (!clock_gettime(clock_id, &ts))
209 return 1000000000LL * ts.tv_sec + ts.tv_nsec;
210 else /* in this case errno is set appropriately */
214 int64_t get_timestamp_realtime (void)
216 return get_timestamp(CLOCK_REALTIME);
219 int64_t get_timestamp_boot (void)
221 return get_timestamp(CLOCK_BOOTTIME);
224 int64_t get_timestamp_thread (void)
226 return get_timestamp(CLOCK_THREAD_CPUTIME_ID);
229 int64_t get_timestamp_monotonic (void)
231 return get_timestamp(CLOCK_MONOTONIC);
234 void set_timestamp (struct timespec *out, int64_t target_ns)
236 out->tv_sec = target_ns / 1000000000LL;
237 out->tv_nsec = target_ns % 1000000000LL;