From 8c85d6351d1de0b83c87fce172dcfc3de5c9aa0a Mon Sep 17 00:00:00 2001 From: Viorel Suman Date: Fri, 6 Feb 2015 15:47:51 +0200 Subject: [PATCH] Utils review - numerical read functions All numerical read functions decomposed into two phases - "read" phase and "convert" phase. The "read" phase is same for all types, the only difference is the type specific conversion function. Change-Id: I6358d593e3804c2a00a0252f0c3bd0e18c67b9b9 Signed-off-by: Viorel Suman --- utils.c | 156 ++++++++++++++++++++++------------------------------------------ 1 file changed, 52 insertions(+), 104 deletions(-) diff --git a/utils.c b/utils.c index 7016cda..6ee1f47 100644 --- a/utils.c +++ b/utils.c @@ -76,72 +76,90 @@ 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]; + *(int*)v = atoi(buf); +} - len = sprintf(buf, "%d", value); - if (!path[0] || len <= 0) { - ALOGE("Unexpected condition in sysfs_write_int\n"); - return -1; - } +static void str2float(const char* buf, void *v) +{ + *(float*)v = strtof(buf, NULL); +} - fd = open(path, O_WRONLY); - 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); +} - ret = write(fd, buf, len); - if (ret != len) { - ALOGW("Cannot write %s (%d bytes) to %s (%s)\n", buf, len, path, +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 number from %s (%s)\n", path, strerror(errno)); + return -1; } - close(fd); - - return ret; + str2num(buf, v); + return 0; } int sysfs_read_int(const char path[PATH_MAX], int *value) { + return sysfs_read_num(path, value, str2int); +} + + +int sysfs_read_float(const char path[PATH_MAX], float *value) +{ + return sysfs_read_num(path, value, str2float); +} + + +int sysfs_read_uint64(const char path[PATH_MAX], uint64_t *value) +{ + return sysfs_read_num(path, value, str2uint64); +} + + +int sysfs_write_int(const char path[PATH_MAX], int value) +{ + int ret; int fd; int len; - char buf[20] = {0}; + char buf[20]; + + len = sprintf(buf, "%d", value); - if (!path[0] || !value) { + if (!path[0] || len <= 0) { + ALOGE("Unexpected condition in sysfs_write_int\n"); return -1; } - fd = open(path, O_RDONLY); + fd = open(path, O_WRONLY); if (fd == -1) { ALOGV("Cannot open %s (%s)\n", path, strerror(errno)); return -1; } - len = read(fd, buf, sizeof(buf)); - - close(fd); + ret = write(fd, buf, len); - if (len <= 0) { - ALOGW("Cannot read integer from %s (%s)\n", path, + if (ret != len) { + ALOGW("Cannot write %s (%d bytes) to %s (%s)\n", buf, len, path, strerror(errno)); - return -1; } - *value = atoi(buf); - - ALOGV("Read %d from %s\n", *value, path); + close(fd); - return 0; + return ret; } @@ -211,76 +229,6 @@ int sysfs_write_float(const char path[PATH_MAX], float value) } -int sysfs_read_float(const char path[PATH_MAX], float *value) -{ - 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)); - 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; -} - -int sysfs_read_uint64(const char path[PATH_MAX], uint64_t *value) -{ - 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)); - return -1; - } - - len = read(fd, buf, sizeof(buf)); - - close(fd); - - if (len <= 0) { - ALOGW("Cannot read uint64 from %s (%s)\n", path, - strerror(errno)); - return -1; - } - - *value = atoll(buf); - - ALOGV("Read %llu from %s\n", *value, path); - - return 0; -} - - - int64_t get_timestamp_realtime (void) { struct timespec ts = {0}; -- 2.11.0