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>
31 * Some of these calls are going to fail, because not all sensors expose all
32 * possible sysfs attributes. As an optimization we may want to cache which
33 * ones are valid and immediately return in error for inexistent entries.
35 int sysfs_read(const char path[PATH_MAX], void *buf, int buf_len)
39 if (!path[0] || !buf || buf_len < 1)
42 fd = open(path, O_RDONLY);
45 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
49 len = read(fd, buf, buf_len);
54 ALOGW("Cannot read from %s (%s)\n", path, strerror(errno));
56 ALOGV("Read %d bytes from %s\n", len, path);
62 int sysfs_write(const char path[PATH_MAX], const void *buf, const int buf_len)
66 if (!path[0] || !buf || buf_len < 1)
69 fd = open(path, O_WRONLY);
72 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
76 len = write(fd, buf, buf_len);
81 ALOGW("Cannot write to %s (%s)\n", path, strerror(errno));
82 else if (len != buf_len)
83 ALOGW("Cannot write %d bytes to %s (%d)\n", buf_len, path, len);
85 ALOGV("Wrote %d bytes to %s\n", buf_len, path);
91 static void str2int(const char* buf, void *v)
97 static void str2float(const char* buf, void *v)
99 *(float*)v = strtof(buf, NULL);
103 static void str2uint64(const char* buf, void *v)
105 *(uint64_t*)v = atoll(buf);
109 int sysfs_read_num(const char path[PATH_MAX], void *v,
110 void (*str2num)(const char* buf, void *v))
113 int len = sysfs_read_str(path, buf, sizeof(buf));
116 ALOGW("Cannot read number from %s (%s)\n", path,
126 int sysfs_read_int(const char path[PATH_MAX], int *value)
128 return sysfs_read_num(path, value, str2int);
132 int sysfs_read_float(const char path[PATH_MAX], float *value)
134 return sysfs_read_num(path, value, str2float);
138 int sysfs_read_uint64(const char path[PATH_MAX], uint64_t *value)
140 return sysfs_read_num(path, value, str2uint64);
144 int sysfs_write_int(const char path[PATH_MAX], int value)
147 int len = snprintf(buf, sizeof(buf), "%d", value);
150 ALOGE("Unexpected condition in sysfs_write_int\n");
154 return sysfs_write(path, buf, len);
157 int sysfs_write_float(const char path[PATH_MAX], float value)
160 int len = snprintf(buf, sizeof(buf), "%g", value);
163 ALOGE("Unexpected condition in sysfs_write_float\n");
167 return sysfs_write(path, buf, len);
171 int sysfs_write_str(const char path[PATH_MAX], const char *str)
176 return sysfs_write(path, str, strlen(str));
180 int sysfs_read_str(const char path[PATH_MAX], char *buf, int buf_len)
184 if (!buf || buf_len < 1)
187 len = sysfs_read(path, buf, buf_len);
190 ALOGW("Cannot read string from %s (%s)\n", path,
195 buf[len == 0 ? 0 : len - 1] = '\0';
197 ALOGV("Read %s from %s\n", buf, path);
203 int64_t get_timestamp (clockid_t clock_id)
205 struct timespec ts = {0, 0};
207 if (!clock_gettime(clock_id, &ts))
208 return 1000000000LL * ts.tv_sec + ts.tv_nsec;
209 else /* in this case errno is set appropriately */
213 int64_t get_timestamp_realtime (void)
215 return get_timestamp(CLOCK_REALTIME);
218 int64_t get_timestamp_boot (void)
220 return get_timestamp(CLOCK_BOOTTIME);
223 int64_t get_timestamp_thread (void)
225 return get_timestamp(CLOCK_THREAD_CPUTIME_ID);
228 int64_t get_timestamp_monotonic (void)
230 return get_timestamp(CLOCK_MONOTONIC);
233 void set_timestamp (struct timespec *out, int64_t target_ns)
235 out->tv_sec = target_ns / 1000000000LL;
236 out->tv_nsec = target_ns % 1000000000LL;