OSDN Git Service

Rename MAX_REAL_DEP to MAX_BASE_SENSORS and base_idx to base
[android-x86/hardware-intel-libsensors.git] / utils.c
diff --git a/utils.c b/utils.c
index d5ec492..c41931f 100644 (file)
--- a/utils.c
+++ b/utils.c
@@ -6,6 +6,9 @@
 #include <fcntl.h>
 #include <utils/Log.h>
 #include <hardware/sensors.h>
+#include <utils/Atomic.h>
+#include <linux/android_alarm.h>
+
 #include "common.h"
 #include "utils.h"
 
@@ -153,6 +156,40 @@ int sysfs_read_str(const char path[PATH_MAX], char *buf, int buf_len)
 }
 
 
+int sysfs_write_float(const char path[PATH_MAX], float value)
+{
+       int ret;
+       int fd;
+       int len;
+       char buf[20];
+
+       len = snprintf(buf, sizeof(buf), "%g", value);
+
+       if (!path[0] || len <= 0) {
+               ALOGE("Unexpected condition in sysfs_write_float\n");
+               return -1;
+       }
+
+       fd = open(path, O_WRONLY);
+
+       if (fd == -1) {
+               ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
+               return -1;
+       }
+
+       ret = write(fd, buf, len);
+
+       if (ret != len) {
+               ALOGW("Cannot write %s (%d bytes) to %s (%s)\n", buf, len, path,
+                     strerror(errno));
+       }
+
+       close(fd);
+
+       return ret;
+}
+
+
 int sysfs_read_float(const char path[PATH_MAX], float *value)
 {
        int fd;
@@ -182,50 +219,42 @@ int sysfs_read_float(const char path[PATH_MAX], float *value)
 
        *value = (float) strtod(buf, NULL);
 
-       ALOGV("Read %f from %s\n", *value, path);
+       ALOGV("Read %g from %s\n", *value, path);
 
        return 0;
 }
 
 
-int decode_type_spec(  const char type_buf[MAX_TYPE_SPEC_LEN],
-                       struct datum_info_t *type_info)
+int64_t get_timestamp_realtime (void)
 {
-       /* Return size in bytes for this type specification, or -1 in error */
-       char sign;
-       char endianness;
-       unsigned int realbits, storagebits, shift;
-       int tokens;
-
-       /* Valid specs: "le:u10/16>>0", "le:s16/32>>0" or "le:s32/32>>0" */
-
-       tokens = sscanf(type_buf, "%ce:%c%u/%u>>%u",
-                       &endianness, &sign, &realbits, &storagebits, &shift);
-
-       if     (tokens != 5 ||
-               (endianness != 'b' && endianness != 'l') ||
-               (sign != 'u' && sign != 's') ||
-               realbits > storagebits ||
-               (storagebits != 16 && storagebits != 32 && storagebits != 64)) {
-                       ALOGE("Invalid iio channel type spec: %s\n", type_buf);
-                       return -1;
-               }
-
-       type_info->endianness   =               endianness;
-       type_info->sign         =               sign;
-       type_info->realbits     =       (short) realbits;
-       type_info->storagebits  =       (short) storagebits;
-       type_info->shift        =       (short) shift;
-
-       return storagebits / 8;
+       struct timespec ts = {0};
+       clock_gettime(CLOCK_REALTIME, &ts);
+
+       return 1000000000LL * ts.tv_sec + ts.tv_nsec;
 }
 
 
-int64_t get_timestamp(void)
+int64_t get_timestamp_boot (void)
 {
        struct timespec ts = {0};
+       clock_gettime(CLOCK_BOOTTIME, &ts);
 
+       return 1000000000LL * ts.tv_sec + ts.tv_nsec;
+}
+
+
+int64_t get_timestamp_monotonic (void)
+{
+       struct timespec ts = {0};
        clock_gettime(CLOCK_MONOTONIC, &ts);
 
        return 1000000000LL * ts.tv_sec + ts.tv_nsec;
 }
+
+
+void set_timestamp (struct timespec *out, int64_t target_ns)
+{
+       out->tv_sec  = target_ns / 1000000000LL;
+       out->tv_nsec = target_ns % 1000000000LL;
+}
+