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_read_float(const char path[PATH_MAX], float *value)
162 if (!path[0] || !value) {
166 fd = open(path, O_RDONLY);
169 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
173 len = read(fd, buf, sizeof(buf));
178 ALOGW("Cannot read float from %s (%s)\n", path,
183 *value = (float) strtod(buf, NULL);
185 ALOGV("Read %f from %s\n", *value, path);
191 int decode_type_spec( const char type_buf[MAX_TYPE_SPEC_LEN],
192 struct datum_info_t *type_info)
194 /* Return size in bytes for this type specification, or -1 in error */
197 unsigned int realbits, storagebits, shift;
200 /* Valid specs: "le:u10/16>>0", "le:s16/32>>0" or "le:s32/32>>0" */
202 tokens = sscanf(type_buf, "%ce:%c%u/%u>>%u",
203 &endianness, &sign, &realbits, &storagebits, &shift);
206 (endianness != 'b' && endianness != 'l') ||
207 (sign != 'u' && sign != 's') ||
208 realbits > storagebits ||
209 (storagebits != 16 && storagebits != 32 && storagebits != 64)) {
210 ALOGE("Invalid iio channel type spec: %s\n", type_buf);
214 type_info->endianness = endianness;
215 type_info->sign = sign;
216 type_info->realbits = (short) realbits;
217 type_info->storagebits = (short) storagebits;
218 type_info->shift = (short) shift;
220 return storagebits / 8;
224 int64_t get_timestamp(void)
226 struct timespec ts = {0};
228 clock_gettime(CLOCK_MONOTONIC, &ts);
230 return 1000000000LL * ts.tv_sec + ts.tv_nsec;