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)
31 fd = open(path, O_WRONLY);
34 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
38 len = sprintf(buf, "%d", value) + 1;
40 ret = write(fd, buf, len);
43 ALOGW("Cannot write %s (%d bytes) to %s (%s)\n", buf, len, path,
53 int sysfs_read_int(const char path[PATH_MAX], int *value)
59 if (!path[0] || !value) {
63 fd = open(path, O_RDONLY);
66 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
70 len = read(fd, buf, sizeof(buf));
75 ALOGW("Cannot read integer from %s (%s)\n", path,
82 ALOGV("Read %d from %s\n", *value, path);
88 int sysfs_write_str(const char path[PATH_MAX], const char *str)
94 if (!path[0] || !str || !str[0]) {
98 fd = open(path, O_WRONLY);
101 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
107 ret = write(fd, str, len);
110 ALOGW("Cannot write %s (%d bytes) to %s (%s)\n", str, len, path,
114 ALOGV("Wrote %s to %s\n", str, path);
122 int sysfs_read_str(const char path[PATH_MAX], char *buf, int buf_len)
127 if (!path[0] || !buf || buf_len < 1)
130 fd = open(path, O_RDONLY);
133 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
137 len = read(fd, buf, buf_len);
142 ALOGW("Cannot read string from %s (%s)\n", path,
147 buf[len == 0 ? 0 : len-1] = '\0';
149 ALOGV("Read %s from %s\n", buf, path);
155 int sysfs_read_float(const char path[PATH_MAX], float *value)
161 if (!path[0] || !value) {
165 fd = open(path, O_RDONLY);
168 ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
172 len = read(fd, buf, sizeof(buf));
177 ALOGW("Cannot read float from %s (%s)\n", path,
182 *value = (float) strtod(buf, NULL);
184 ALOGV("Read %f from %s\n", *value, path);
190 int decode_type_spec( const char type_buf[MAX_TYPE_SPEC_LEN],
191 struct datum_info_t *type_info)
193 /* Return size in bytes for this type specification, or -1 in error */
196 unsigned int realbits, storagebits, shift;
199 /* Valid specs: "le:u10/16>>0", "le:s16/32>>0" or "le:s32/32>>0" */
201 tokens = sscanf(type_buf, "%ce:%c%u/%u>>%u",
202 &endianness, &sign, &realbits, &storagebits, &shift);
205 (endianness != 'b' && endianness != 'l') ||
206 (sign != 'u' && sign != 's') ||
207 realbits > storagebits ||
208 (storagebits != 16 && storagebits != 32 && storagebits != 64)) {
209 ALOGE("Invalid iio channel type spec: %s\n", type_buf);
213 type_info->endianness = endianness;
214 type_info->sign = sign;
215 type_info->realbits = (short) realbits;
216 type_info->storagebits = (short) storagebits;
217 type_info->shift = (short) shift;
219 return storagebits / 8;
223 int64_t get_timestamp(void)
225 struct timespec ts = {0};
227 clock_gettime(CLOCK_MONOTONIC, &ts);
229 return 1000000000LL * ts.tv_sec + ts.tv_nsec;