2 * Copyright (C) 2014 Intel Corporation.
8 #include <hardware/sensors.h>
15 * Some of these calls are going to fail, because not all sensors expose all
16 * possible sysfs attributes. As an optimization we may want to cache which
17 * ones are valid and immediately return in error for inexistent entries.
20 int sysfs_write_int(const char path[PATH_MAX], int value)
27 len = sprintf(buf, "%d", value);
29 if (!path[0] || len <= 0) {
30 ALOGE("Unexpected condition in sysfs_write_int\n");
34 fd = open(path, O_WRONLY);
37 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
41 ret = write(fd, buf, len);
44 ALOGW("Cannot write %s (%d bytes) to %s (%s)\n", buf, len, path,
54 int sysfs_read_int(const char path[PATH_MAX], int *value)
60 if (!path[0] || !value) {
64 fd = open(path, O_RDONLY);
67 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
71 len = read(fd, buf, sizeof(buf));
76 ALOGW("Cannot read integer from %s (%s)\n", path,
83 ALOGV("Read %d from %s\n", *value, path);
89 int sysfs_write_str(const char path[PATH_MAX], const char *str)
95 if (!path[0] || !str || !str[0]) {
99 fd = open(path, O_WRONLY);
102 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
108 ret = write(fd, str, len);
111 ALOGW("Cannot write %s (%d bytes) to %s (%s)\n", str, len, path,
115 ALOGV("Wrote %s to %s\n", str, path);
123 int sysfs_read_str(const char path[PATH_MAX], char *buf, int buf_len)
128 if (!path[0] || !buf || buf_len < 1)
131 fd = open(path, O_RDONLY);
134 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
138 len = read(fd, buf, buf_len);
143 ALOGW("Cannot read string from %s (%s)\n", path,
148 buf[len == 0 ? 0 : len-1] = '\0';
150 ALOGV("Read %s from %s\n", buf, path);
156 int sysfs_write_float(const char path[PATH_MAX], float value)
163 len = snprintf(buf, sizeof(buf), "%g", value);
165 if (!path[0] || len <= 0) {
166 ALOGE("Unexpected condition in sysfs_write_float\n");
170 fd = open(path, O_WRONLY);
173 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
177 ret = write(fd, buf, len);
180 ALOGW("Cannot write %s (%d bytes) to %s (%s)\n", buf, len, path,
190 int sysfs_read_float(const char path[PATH_MAX], float *value)
196 if (!path[0] || !value) {
200 fd = open(path, O_RDONLY);
203 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
207 len = read(fd, buf, sizeof(buf));
212 ALOGW("Cannot read float from %s (%s)\n", path,
217 *value = (float) strtod(buf, NULL);
219 ALOGV("Read %g from %s\n", *value, path);
225 int decode_type_spec( const char type_buf[MAX_TYPE_SPEC_LEN],
226 struct datum_info_t *type_info)
228 /* Return size in bytes for this type specification, or -1 in error */
231 unsigned int realbits, storagebits, shift;
234 /* Valid specs: "le:u10/16>>0", "le:s16/32>>0" or "le:s32/32>>0" */
236 tokens = sscanf(type_buf, "%ce:%c%u/%u>>%u",
237 &endianness, &sign, &realbits, &storagebits, &shift);
240 (endianness != 'b' && endianness != 'l') ||
241 (sign != 'u' && sign != 's') ||
242 realbits > storagebits ||
243 (storagebits != 16 && storagebits != 32 && storagebits != 64)) {
244 ALOGE("Invalid iio channel type spec: %s\n", type_buf);
248 type_info->endianness = endianness;
249 type_info->sign = sign;
250 type_info->realbits = (short) realbits;
251 type_info->storagebits = (short) storagebits;
252 type_info->shift = (short) shift;
254 return storagebits / 8;
258 int64_t get_timestamp(void)
260 struct timespec ts = {0};
262 clock_gettime(CLOCK_MONOTONIC, &ts);
264 return 1000000000LL * ts.tv_sec + ts.tv_nsec;