OSDN Git Service

Move module path to vendor
[android-x86/hardware-intel-libsensors.git] / utils.c
diff --git a/utils.c b/utils.c
index 1ef11b7..488b70a 100644 (file)
--- a/utils.c
+++ b/utils.c
@@ -76,273 +76,148 @@ int sysfs_write(const char path[PATH_MAX], const void *buf, const int buf_len)
 }
 
 
-int sysfs_write_int(const char path[PATH_MAX], int value)
+static void str2int(const char* buf, void *v)
 {
-       int ret;
-       int fd;
-       int len;
-       char buf[20];
-
-       len = sprintf(buf, "%d", value);
-
-       if (!path[0] || len <= 0) {
-               ALOGE("Unexpected condition in sysfs_write_int\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*)v = atoi(buf);
 }
 
 
-int sysfs_read_int(const char path[PATH_MAX], int *value)
+static void str2float(const char* buf, void *v)
 {
-       int fd;
-       int len;
-       char buf[20] = {0};
-
-       if (!path[0] || !value) {
-               return -1;
-       }
+       *(float*)v = strtof(buf, NULL);
+}
 
-       fd = open(path, O_RDONLY);
 
-       if (fd == -1) {
-               ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
-               return -1;
-       }
+static void str2uint64(const char* buf, void *v)
+{
+       *(uint64_t*)v = atoll(buf);
+}
 
-       len = read(fd, buf, sizeof(buf));
 
-       close(fd);
+int sysfs_read_num(const char path[PATH_MAX], void *v,
+               void (*str2num)(const char* buf, void *v))
+{
+       char buf[20];
+       int len = sysfs_read_str(path, buf, sizeof(buf));
 
        if (len <= 0) {
-               ALOGW("Cannot read integer from %s (%s)\n", path,
+               ALOGW("Cannot read number from %s (%s)\n", path,
                      strerror(errno));
                return -1;
        }
 
-       *value = atoi(buf);
-
-       ALOGV("Read %d from %s\n", *value, path);
-
+       str2num(buf, v);
        return 0;
 }
 
 
-int sysfs_write_str(const char path[PATH_MAX], const char *str)
+int sysfs_read_int(const char path[PATH_MAX], int *value)
 {
-       int ret;
-       int fd;
-       int len;
-
-       if (!path[0] || !str || !str[0]) {
-               return -1;
-       }
-
-       fd = open(path, O_WRONLY);
-
-       if (fd == -1) {
-               ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
-               return -1;
-       }
-
-       len = strlen(str);
-
-       ret = write(fd, str, len);
-
-       if (ret != len) {
-               ALOGW("Cannot write %s (%d bytes) to %s (%s)\n", str, len, path,
-                     strerror(errno));
-       }
-       else
-               ALOGV("Wrote %s to %s\n", str, path);
-
-       close(fd);
-
-       return ret;
+       return sysfs_read_num(path, value, str2int);
 }
 
 
-int sysfs_read_str(const char path[PATH_MAX], char *buf, int buf_len)
+int sysfs_read_float(const char path[PATH_MAX], float *value)
 {
-       int fd;
-       int len;
-
-       if (!path[0] || !buf || buf_len < 1)
-               return -1;
+       return sysfs_read_num(path, value, str2float);
+}
 
-       fd = open(path, O_RDONLY);
 
-       if (fd == -1) {
-               ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
-               return -1;
-       }
+int sysfs_read_uint64(const char path[PATH_MAX], uint64_t *value)
+{
+       return sysfs_read_num(path, value, str2uint64);
+}
 
-       len = read(fd, buf, buf_len);
 
-       close(fd);
+int sysfs_write_int(const char path[PATH_MAX], int value)
+{
+       char buf[20];
+       int len = snprintf(buf, sizeof(buf), "%d", value);
 
-       if (len == -1) {
-               ALOGW("Cannot read string from %s (%s)\n", path,
-                     strerror(errno));
+       if (len <= 0) {
+               ALOGE("Unexpected condition in sysfs_write_int\n");
                return -1;
        }
 
-       buf[len == 0 ? 0 : len-1] = '\0';
-
-       ALOGV("Read %s from %s\n", buf, path);
-
-       return len;
+       return sysfs_write(path, buf, len);
 }
 
-
 int sysfs_write_float(const char path[PATH_MAX], float value)
 {
-       int ret;
-       int fd;
-       int len;
        char buf[20];
+       int len = snprintf(buf, sizeof(buf), "%g", value);
 
-       len = snprintf(buf, sizeof(buf), "%g", value);
-
-       if (!path[0] || len <= 0) {
+       if (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;
+       return sysfs_write(path, buf, len);
 }
 
 
-int sysfs_read_float(const char path[PATH_MAX], float *value)
+int sysfs_write_str(const char path[PATH_MAX], const char *str)
 {
-       int fd;
-       int len;
-       char buf[20] = {0};
-
-       if (!path[0] || !value) {
-               return -1;
-       }
-
-       fd = open(path, O_RDONLY);
-
-       if (fd == -1) {
-               ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
+       if (!str || !str[0])
                return -1;
-       }
-
-       len = read(fd, buf, sizeof(buf));
-
-       close(fd);
 
-       if (len <= 0) {
-               ALOGW("Cannot read float from %s (%s)\n", path,
-                     strerror(errno));
-               return -1;
-       }
-
-       *value = (float) strtod(buf, NULL);
-
-       ALOGV("Read %g from %s\n", *value, path);
-
-       return 0;
+       return sysfs_write(path, str, strlen(str));
 }
 
-int sysfs_read_uint64(const char path[PATH_MAX], uint64_t *value)
+
+int sysfs_read_str(const char path[PATH_MAX], char *buf, int buf_len)
 {
-       int fd;
        int len;
-       char buf[20] = {0};
-
-       if (!path[0] || !value) {
-               return -1;
-       }
-
-       fd = open(path, O_RDONLY);
 
-       if (fd == -1) {
-               ALOGV("Cannot open %s (%s)\n", path, strerror(errno));
+       if (!buf || buf_len < 1)
                return -1;
-       }
-
-       len = read(fd, buf, sizeof(buf));
 
-       close(fd);
+       len = sysfs_read(path, buf, buf_len);
 
-       if (len <= 0) {
-               ALOGW("Cannot read uint64 from %s (%s)\n", path,
+       if (len == -1) {
+               ALOGW("Cannot read string from %s (%s)\n", path,
                      strerror(errno));
                return -1;
        }
 
-       *value =  atoll(buf);
+       buf[len == 0 ? 0 : len - 1] = '\0';
 
-       ALOGV("Read %llu from %s\n", *value, path);
+       ALOGV("Read %s from %s\n", buf, path);
 
-       return 0;
+       return len;
 }
 
 
-
-int64_t get_timestamp_realtime (void)
+int64_t get_timestamp (clockid_t clock_id)
 {
        struct timespec ts = {0};
-       clock_gettime(CLOCK_REALTIME, &ts);
 
-       return 1000000000LL * ts.tv_sec + ts.tv_nsec;
+       if (!clock_gettime(clock_id, &ts))
+               return 1000000000LL * ts.tv_sec + ts.tv_nsec;
+       else    /* in this case errno is set appropriately */
+               return -1;
 }
 
+int64_t get_timestamp_realtime (void)
+{
+       return get_timestamp(CLOCK_REALTIME);
+}
 
 int64_t get_timestamp_boot (void)
 {
-       struct timespec ts = {0};
-       clock_gettime(CLOCK_BOOTTIME, &ts);
-
-       return 1000000000LL * ts.tv_sec + ts.tv_nsec;
+       return get_timestamp(CLOCK_BOOTTIME);
 }
 
+int64_t get_timestamp_thread (void)
+{
+       return get_timestamp(CLOCK_THREAD_CPUTIME_ID);
+}
 
 int64_t get_timestamp_monotonic (void)
 {
-       struct timespec ts = {0};
-       clock_gettime(CLOCK_MONOTONIC, &ts);
-
-       return 1000000000LL * ts.tv_sec + ts.tv_nsec;
+       return get_timestamp(CLOCK_MONOTONIC);
 }
 
-
 void set_timestamp (struct timespec *out, int64_t target_ns)
 {
        out->tv_sec  = target_ns / 1000000000LL;