From ff1ac639b395d58bbd99ff4a36b10eebb81544a3 Mon Sep 17 00:00:00 2001 From: Cristina Opriceana Date: Mon, 13 Jul 2015 16:15:56 +0300 Subject: tools: iio: Remove explicit NULL comparison Remove explicit NULL comparison and write it in its simpler form as recommended by checkpatch.pl. Signed-off-by: Cristina Opriceana Reviewed-by: Hartmut Knaack Signed-off-by: Jonathan Cameron --- tools/iio/generic_buffer.c | 4 ++-- tools/iio/iio_utils.c | 56 +++++++++++++++++++++++----------------------- tools/iio/lsiio.c | 10 ++++----- 3 files changed, 35 insertions(+), 35 deletions(-) (limited to 'tools') diff --git a/tools/iio/generic_buffer.c b/tools/iio/generic_buffer.c index 0e737238ca74..9535c2dc25af 100644 --- a/tools/iio/generic_buffer.c +++ b/tools/iio/generic_buffer.c @@ -270,7 +270,7 @@ int main(int argc, char **argv) } } - if (device_name == NULL) { + if (!device_name) { printf("Device name not set\n"); print_usage(); return -1; @@ -290,7 +290,7 @@ int main(int argc, char **argv) return -ENOMEM; if (!notrigger) { - if (trigger_name == NULL) { + if (!trigger_name) { /* * Build the trigger name. If it is device associated * its name is _dev[n] where n matches diff --git a/tools/iio/iio_utils.c b/tools/iio/iio_utils.c index 8fb3214c70f2..4a7e4801cebf 100644 --- a/tools/iio/iio_utils.c +++ b/tools/iio/iio_utils.c @@ -117,13 +117,13 @@ int iioutils_get_type(unsigned *is_signed, unsigned *bytes, unsigned *bits_used, } dp = opendir(scan_el_dir); - if (dp == NULL) { + if (!dp) { ret = -errno; goto error_free_builtname_generic; } ret = -ENOENT; - while (ent = readdir(dp), ent != NULL) + while (ent = readdir(dp), ent) /* * Do we allow devices to override a generic name with * a specific one? @@ -138,7 +138,7 @@ int iioutils_get_type(unsigned *is_signed, unsigned *bytes, unsigned *bits_used, } sysfsfp = fopen(filename, "r"); - if (sysfsfp == NULL) { + if (!sysfsfp) { ret = -errno; printf("failed to open %s\n", filename); goto error_free_filename; @@ -235,13 +235,13 @@ int iioutils_get_param_float(float *output, const char *param_name, } dp = opendir(device_dir); - if (dp == NULL) { + if (!dp) { ret = -errno; goto error_free_builtname_generic; } ret = -ENOENT; - while (ent = readdir(dp), ent != NULL) + while (ent = readdir(dp), ent) if ((strcmp(builtname, ent->d_name) == 0) || (strcmp(builtname_generic, ent->d_name) == 0)) { ret = asprintf(&filename, @@ -325,12 +325,12 @@ int build_channel_array(const char *device_dir, return -ENOMEM; dp = opendir(scan_el_dir); - if (dp == NULL) { + if (!dp) { ret = -errno; goto error_free_name; } - while (ent = readdir(dp), ent != NULL) + while (ent = readdir(dp), ent) if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), "_en") == 0) { ret = asprintf(&filename, @@ -341,7 +341,7 @@ int build_channel_array(const char *device_dir, } sysfsfp = fopen(filename, "r"); - if (sysfsfp == NULL) { + if (!sysfsfp) { ret = -errno; free(filename); goto error_close_dir; @@ -369,13 +369,13 @@ int build_channel_array(const char *device_dir, } *ci_array = malloc(sizeof(**ci_array) * (*counter)); - if (*ci_array == NULL) { + if (!*ci_array) { ret = -ENOMEM; goto error_close_dir; } seekdir(dp, 0); - while (ent = readdir(dp), ent != NULL) { + while (ent = readdir(dp), ent) { if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), "_en") == 0) { int current_enabled = 0; @@ -391,7 +391,7 @@ int build_channel_array(const char *device_dir, } sysfsfp = fopen(filename, "r"); - if (sysfsfp == NULL) { + if (!sysfsfp) { ret = -errno; free(filename); count--; @@ -424,7 +424,7 @@ int build_channel_array(const char *device_dir, current->name = strndup(ent->d_name, strlen(ent->d_name) - strlen("_en")); - if (current->name == NULL) { + if (!current->name) { free(filename); ret = -ENOMEM; count--; @@ -452,7 +452,7 @@ int build_channel_array(const char *device_dir, } sysfsfp = fopen(filename, "r"); - if (sysfsfp == NULL) { + if (!sysfsfp) { ret = -errno; printf("failed to open %s\n", filename); free(filename); @@ -567,12 +567,12 @@ int find_type_by_name(const char *name, const char *type) char *filename; dp = opendir(iio_dir); - if (dp == NULL) { + if (!dp) { printf("No industrialio devices available\n"); return -ENODEV; } - while (ent = readdir(dp), ent != NULL) { + while (ent = readdir(dp), ent) { if (strcmp(ent->d_name, ".") != 0 && strcmp(ent->d_name, "..") != 0 && strlen(ent->d_name) > strlen(type) && @@ -595,7 +595,7 @@ int find_type_by_name(const char *name, const char *type) ":", 1) != 0) { filename = malloc(strlen(iio_dir) + strlen(type) + numstrlen + 6); - if (filename == NULL) { + if (!filename) { ret = -ENOMEM; goto error_close_dir; } @@ -654,7 +654,7 @@ static int _write_sysfs_int(const char *filename, const char *basedir, int val, int test; char *temp = malloc(strlen(basedir) + strlen(filename) + 2); - if (temp == NULL) + if (!temp) return -ENOMEM; ret = sprintf(temp, "%s/%s", basedir, filename); @@ -662,7 +662,7 @@ static int _write_sysfs_int(const char *filename, const char *basedir, int val, goto error_free; sysfsfp = fopen(temp, "w"); - if (sysfsfp == NULL) { + if (!sysfsfp) { ret = -errno; printf("failed to open %s\n", temp); goto error_free; @@ -683,7 +683,7 @@ static int _write_sysfs_int(const char *filename, const char *basedir, int val, if (verify) { sysfsfp = fopen(temp, "r"); - if (sysfsfp == NULL) { + if (!sysfsfp) { ret = -errno; printf("failed to open %s\n", temp); goto error_free; @@ -749,7 +749,7 @@ static int _write_sysfs_string(const char *filename, const char *basedir, FILE *sysfsfp; char *temp = malloc(strlen(basedir) + strlen(filename) + 2); - if (temp == NULL) { + if (!temp) { printf("Memory allocation failed\n"); return -ENOMEM; } @@ -759,7 +759,7 @@ static int _write_sysfs_string(const char *filename, const char *basedir, goto error_free; sysfsfp = fopen(temp, "w"); - if (sysfsfp == NULL) { + if (!sysfsfp) { ret = -errno; printf("Could not open %s\n", temp); goto error_free; @@ -780,7 +780,7 @@ static int _write_sysfs_string(const char *filename, const char *basedir, if (verify) { sysfsfp = fopen(temp, "r"); - if (sysfsfp == NULL) { + if (!sysfsfp) { ret = -errno; printf("Could not open file to verify\n"); goto error_free; @@ -855,7 +855,7 @@ int read_sysfs_posint(const char *filename, const char *basedir) FILE *sysfsfp; char *temp = malloc(strlen(basedir) + strlen(filename) + 2); - if (temp == NULL) { + if (!temp) { printf("Memory allocation failed"); return -ENOMEM; } @@ -865,7 +865,7 @@ int read_sysfs_posint(const char *filename, const char *basedir) goto error_free; sysfsfp = fopen(temp, "r"); - if (sysfsfp == NULL) { + if (!sysfsfp) { ret = -errno; goto error_free; } @@ -902,7 +902,7 @@ int read_sysfs_float(const char *filename, const char *basedir, float *val) FILE *sysfsfp; char *temp = malloc(strlen(basedir) + strlen(filename) + 2); - if (temp == NULL) { + if (!temp) { printf("Memory allocation failed"); return -ENOMEM; } @@ -912,7 +912,7 @@ int read_sysfs_float(const char *filename, const char *basedir, float *val) goto error_free; sysfsfp = fopen(temp, "r"); - if (sysfsfp == NULL) { + if (!sysfsfp) { ret = -errno; goto error_free; } @@ -949,7 +949,7 @@ int read_sysfs_string(const char *filename, const char *basedir, char *str) FILE *sysfsfp; char *temp = malloc(strlen(basedir) + strlen(filename) + 2); - if (temp == NULL) { + if (!temp) { printf("Memory allocation failed"); return -ENOMEM; } @@ -959,7 +959,7 @@ int read_sysfs_string(const char *filename, const char *basedir, char *str) goto error_free; sysfsfp = fopen(temp, "r"); - if (sysfsfp == NULL) { + if (!sysfsfp) { ret = -errno; goto error_free; } diff --git a/tools/iio/lsiio.c b/tools/iio/lsiio.c index 7f432a55a6c4..4f8172fe7881 100644 --- a/tools/iio/lsiio.c +++ b/tools/iio/lsiio.c @@ -46,10 +46,10 @@ static int dump_channels(const char *dev_dir_name) const struct dirent *ent; dp = opendir(dev_dir_name); - if (dp == NULL) + if (!dp) return -errno; - while (ent = readdir(dp), ent != NULL) + while (ent = readdir(dp), ent) if (check_prefix(ent->d_name, "in_") && check_postfix(ent->d_name, "_raw")) printf(" %-10s\n", ent->d_name); @@ -107,12 +107,12 @@ static int dump_devices(void) DIR *dp; dp = opendir(iio_dir); - if (dp == NULL) { + if (!dp) { printf("No industrial I/O devices available\n"); return -ENODEV; } - while (ent = readdir(dp), ent != NULL) { + while (ent = readdir(dp), ent) { if (check_prefix(ent->d_name, type_device)) { char *dev_dir_name; @@ -134,7 +134,7 @@ static int dump_devices(void) } } rewinddir(dp); - while (ent = readdir(dp), ent != NULL) { + while (ent = readdir(dp), ent) { if (check_prefix(ent->d_name, type_trigger)) { char *dev_dir_name; -- cgit v1.2.3 From 37d38e2bcce055c3988977429dc4336c866df73d Mon Sep 17 00:00:00 2001 From: Cristina Opriceana Date: Mon, 13 Jul 2015 16:17:47 +0300 Subject: tools: iio: Remove unnecessary braces MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Single statement blocks don’t need braces. Found with checkpatch.pl. Signed-off-by: Cristina Opriceana Acked-by: Hartmut Knaack Signed-off-by: Jonathan Cameron --- tools/iio/iio_event_monitor.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'tools') diff --git a/tools/iio/iio_event_monitor.c b/tools/iio/iio_event_monitor.c index 703f4cb0e8e9..9ee195949a4a 100644 --- a/tools/iio/iio_event_monitor.c +++ b/tools/iio/iio_event_monitor.c @@ -262,9 +262,8 @@ int main(int argc, char **argv) printf("Found IIO device with name %s with device number %d\n", device_name, dev_num); ret = asprintf(&chrdev_name, "/dev/iio:device%d", dev_num); - if (ret < 0) { + if (ret < 0) return -ENOMEM; - } } else { /* * If we can't find an IIO device by name assume device_name is -- cgit v1.2.3 From 34cbea1908fb686b037e01e5b1ab8e0e67b09ed3 Mon Sep 17 00:00:00 2001 From: Cristina Opriceana Date: Mon, 13 Jul 2015 16:20:11 +0300 Subject: tools: iio: Add ARRAY_SIZE macro Calculation of the length of an array can be done with the ARRAY_SIZE macro to make code more abstract and remove the associated checkpatch.pl warning. Signed-off-by: Cristina Opriceana Acked-by: Hartmut Knaack Signed-off-by: Jonathan Cameron --- tools/iio/iio_utils.c | 2 +- tools/iio/iio_utils.h | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) (limited to 'tools') diff --git a/tools/iio/iio_utils.c b/tools/iio/iio_utils.c index 4a7e4801cebf..e177f4022d40 100644 --- a/tools/iio/iio_utils.c +++ b/tools/iio/iio_utils.c @@ -39,7 +39,7 @@ int iioutils_break_up_name(const char *full_name, char **generic_name) char *working, *prefix = ""; int i, ret; - for (i = 0; i < sizeof(iio_direction) / sizeof(iio_direction[0]); i++) + for (i = 0; i < ARRAY_SIZE(iio_direction); i++) if (!strncmp(full_name, iio_direction[i], strlen(iio_direction[i]))) { prefix = iio_direction[i]; diff --git a/tools/iio/iio_utils.h b/tools/iio/iio_utils.h index 086610139ade..f30a1091b53e 100644 --- a/tools/iio/iio_utils.h +++ b/tools/iio/iio_utils.h @@ -18,6 +18,8 @@ #define FORMAT_SCAN_ELEMENTS_DIR "%s/scan_elements" #define FORMAT_TYPE_FILE "%s_type" +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) + extern const char *iio_dir; /** -- cgit v1.2.3 From d9abc615ea1659b6967a00e95b1b3a7fd4079b80 Mon Sep 17 00:00:00 2001 From: Cristina Opriceana Date: Fri, 17 Jul 2015 18:43:42 +0300 Subject: tools: iio: Send error messages to stderr This patch indends to make some cleanup and send printf error messages to stderr. The changes were performed with coccinelle for failure messages and manual for other cases, such as wrong usage messages. Signed-off-by: Cristina Opriceana Reviewed-by: Hartmut Knaack Signed-off-by: Jonathan Cameron --- tools/iio/generic_buffer.c | 39 ++++++++++++++++++--------------- tools/iio/iio_event_monitor.c | 14 ++++++------ tools/iio/iio_utils.c | 51 +++++++++++++++++++++++++------------------ tools/iio/lsiio.c | 2 +- 4 files changed, 59 insertions(+), 47 deletions(-) (limited to 'tools') diff --git a/tools/iio/generic_buffer.c b/tools/iio/generic_buffer.c index 9535c2dc25af..32f389ebb94d 100644 --- a/tools/iio/generic_buffer.c +++ b/tools/iio/generic_buffer.c @@ -193,15 +193,15 @@ void process_scan(char *data, void print_usage(void) { - printf("Usage: generic_buffer [options]...\n" - "Capture, convert and output data from IIO device buffer\n" - " -c Do n conversions\n" - " -e Disable wait for event (new data)\n" - " -g Use trigger-less mode\n" - " -l Set buffer length to n samples\n" - " -n Set device name (mandatory)\n" - " -t Set trigger name\n" - " -w Set delay between reads in us (event-less mode)\n"); + fprintf(stderr, "Usage: generic_buffer [options]...\n" + "Capture, convert and output data from IIO device buffer\n" + " -c Do n conversions\n" + " -e Disable wait for event (new data)\n" + " -g Use trigger-less mode\n" + " -l Set buffer length to n samples\n" + " -n Set device name (mandatory)\n" + " -t Set trigger name\n" + " -w Set delay between reads in us (event-less mode)\n"); } int main(int argc, char **argv) @@ -271,7 +271,7 @@ int main(int argc, char **argv) } if (!device_name) { - printf("Device name not set\n"); + fprintf(stderr, "Device name not set\n"); print_usage(); return -1; } @@ -279,7 +279,7 @@ int main(int argc, char **argv) /* Find the device requested */ dev_num = find_type_by_name(device_name, "iio:device"); if (dev_num < 0) { - printf("Failed to find the %s\n", device_name); + fprintf(stderr, "Failed to find the %s\n", device_name); return dev_num; } @@ -307,7 +307,8 @@ int main(int argc, char **argv) /* Verify the trigger exists */ trig_num = find_type_by_name(trigger_name, "trigger"); if (trig_num < 0) { - printf("Failed to find the trigger %s\n", trigger_name); + fprintf(stderr, "Failed to find the trigger %s\n", + trigger_name); ret = trig_num; goto error_free_triggername; } @@ -323,8 +324,8 @@ int main(int argc, char **argv) */ ret = build_channel_array(dev_dir_name, &channels, &num_channels); if (ret) { - printf("Problem reading scan element information\n"); - printf("diag %s\n", dev_dir_name); + fprintf(stderr, "Problem reading scan element information\n" + "diag %s\n", dev_dir_name); goto error_free_triggername; } @@ -350,7 +351,8 @@ int main(int argc, char **argv) dev_dir_name, trigger_name); if (ret < 0) { - printf("Failed to write current_trigger file\n"); + fprintf(stderr, + "Failed to write current_trigger file\n"); goto error_free_buf_dir_name; } } @@ -382,7 +384,7 @@ int main(int argc, char **argv) fp = open(buffer_access, O_RDONLY | O_NONBLOCK); if (fp == -1) { /* TODO: If it isn't there make the node */ ret = -errno; - printf("Failed to open %s\n", buffer_access); + fprintf(stderr, "Failed to open %s\n", buffer_access); goto error_free_buffer_access; } @@ -410,7 +412,7 @@ int main(int argc, char **argv) read_size = read(fp, data, toread * scan_size); if (read_size < 0) { if (errno == EAGAIN) { - printf("nothing available\n"); + fprintf(stderr, "nothing available\n"); continue; } else { break; @@ -431,7 +433,8 @@ int main(int argc, char **argv) ret = write_sysfs_string("trigger/current_trigger", dev_dir_name, "NULL"); if (ret < 0) - printf("Failed to write to %s\n", dev_dir_name); + fprintf(stderr, "Failed to write to %s\n", + dev_dir_name); error_close_buffer_access: if (close(fp) == -1) diff --git a/tools/iio/iio_event_monitor.c b/tools/iio/iio_event_monitor.c index 9ee195949a4a..cd3fd41b481d 100644 --- a/tools/iio/iio_event_monitor.c +++ b/tools/iio/iio_event_monitor.c @@ -215,8 +215,8 @@ static void print_event(struct iio_event_data *event) bool diff = IIO_EVENT_CODE_EXTRACT_DIFF(event->id); if (!event_is_known(event)) { - printf("Unknown event: time: %lld, id: %llx\n", - event->timestamp, event->id); + fprintf(stderr, "Unknown event: time: %lld, id: %llx\n", + event->timestamp, event->id); return; } @@ -251,7 +251,7 @@ int main(int argc, char **argv) int fd, event_fd; if (argc <= 1) { - printf("Usage: %s \n", argv[0]); + fprintf(stderr, "Usage: %s \n", argv[0]); return -1; } @@ -277,14 +277,14 @@ int main(int argc, char **argv) fd = open(chrdev_name, 0); if (fd == -1) { ret = -errno; - fprintf(stdout, "Failed to open %s\n", chrdev_name); + fprintf(stderr, "Failed to open %s\n", chrdev_name); goto error_free_chrdev_name; } ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd); if (ret == -1 || event_fd == -1) { ret = -errno; - fprintf(stdout, "Failed to retrieve event fd\n"); + fprintf(stderr, "Failed to retrieve event fd\n"); if (close(fd) == -1) perror("Failed to close character device file"); @@ -300,7 +300,7 @@ int main(int argc, char **argv) ret = read(event_fd, &event, sizeof(event)); if (ret == -1) { if (errno == EAGAIN) { - printf("nothing available\n"); + fprintf(stderr, "nothing available\n"); continue; } else { ret = -errno; @@ -310,7 +310,7 @@ int main(int argc, char **argv) } if (ret != sizeof(event)) { - printf("Reading event failed!\n"); + fprintf(stderr, "Reading event failed!\n"); ret = -EIO; break; } diff --git a/tools/iio/iio_utils.c b/tools/iio/iio_utils.c index e177f4022d40..c3f9e37333e7 100644 --- a/tools/iio/iio_utils.c +++ b/tools/iio/iio_utils.c @@ -140,7 +140,8 @@ int iioutils_get_type(unsigned *is_signed, unsigned *bytes, unsigned *bits_used, sysfsfp = fopen(filename, "r"); if (!sysfsfp) { ret = -errno; - printf("failed to open %s\n", filename); + fprintf(stderr, "failed to open %s\n", + filename); goto error_free_filename; } @@ -152,11 +153,13 @@ int iioutils_get_type(unsigned *is_signed, unsigned *bytes, unsigned *bits_used, &padint, shift); if (ret < 0) { ret = -errno; - printf("failed to pass scan type description\n"); + fprintf(stderr, + "failed to pass scan type description\n"); goto error_close_sysfsfp; } else if (ret != 5) { ret = -EIO; - printf("scan type description didn't match\n"); + fprintf(stderr, + "scan type description didn't match\n"); goto error_close_sysfsfp; } @@ -170,7 +173,8 @@ int iioutils_get_type(unsigned *is_signed, unsigned *bytes, unsigned *bits_used, *is_signed = (signchar == 's'); if (fclose(sysfsfp)) { ret = -errno; - printf("Failed to close %s\n", filename); + fprintf(stderr, "Failed to close %s\n", + filename); goto error_free_filename; } @@ -454,7 +458,8 @@ int build_channel_array(const char *device_dir, sysfsfp = fopen(filename, "r"); if (!sysfsfp) { ret = -errno; - printf("failed to open %s\n", filename); + fprintf(stderr, "failed to open %s\n", + filename); free(filename); goto error_cleanup_array; } @@ -568,7 +573,7 @@ int find_type_by_name(const char *name, const char *type) dp = opendir(iio_dir); if (!dp) { - printf("No industrialio devices available\n"); + fprintf(stderr, "No industrialio devices available\n"); return -ENODEV; } @@ -581,11 +586,13 @@ int find_type_by_name(const char *name, const char *type) ret = sscanf(ent->d_name + strlen(type), "%d", &number); if (ret < 0) { ret = -errno; - printf("failed to read element number\n"); + fprintf(stderr, + "failed to read element number\n"); goto error_close_dir; } else if (ret != 1) { ret = -EIO; - printf("failed to match element number\n"); + fprintf(stderr, + "failed to match element number\n"); goto error_close_dir; } @@ -664,7 +671,7 @@ static int _write_sysfs_int(const char *filename, const char *basedir, int val, sysfsfp = fopen(temp, "w"); if (!sysfsfp) { ret = -errno; - printf("failed to open %s\n", temp); + fprintf(stderr, "failed to open %s\n", temp); goto error_free; } @@ -685,7 +692,7 @@ static int _write_sysfs_int(const char *filename, const char *basedir, int val, sysfsfp = fopen(temp, "r"); if (!sysfsfp) { ret = -errno; - printf("failed to open %s\n", temp); + fprintf(stderr, "failed to open %s\n", temp); goto error_free; } @@ -703,8 +710,9 @@ static int _write_sysfs_int(const char *filename, const char *basedir, int val, } if (test != val) { - printf("Possible failure in int write %d to %s/%s\n", - val, basedir, filename); + fprintf(stderr, + "Possible failure in int write %d to %s/%s\n", + val, basedir, filename); ret = -1; } } @@ -750,7 +758,7 @@ static int _write_sysfs_string(const char *filename, const char *basedir, char *temp = malloc(strlen(basedir) + strlen(filename) + 2); if (!temp) { - printf("Memory allocation failed\n"); + fprintf(stderr, "Memory allocation failed\n"); return -ENOMEM; } @@ -761,7 +769,7 @@ static int _write_sysfs_string(const char *filename, const char *basedir, sysfsfp = fopen(temp, "w"); if (!sysfsfp) { ret = -errno; - printf("Could not open %s\n", temp); + fprintf(stderr, "Could not open %s\n", temp); goto error_free; } @@ -782,7 +790,7 @@ static int _write_sysfs_string(const char *filename, const char *basedir, sysfsfp = fopen(temp, "r"); if (!sysfsfp) { ret = -errno; - printf("Could not open file to verify\n"); + fprintf(stderr, "Could not open file to verify\n"); goto error_free; } @@ -800,9 +808,10 @@ static int _write_sysfs_string(const char *filename, const char *basedir, } if (strcmp(temp, val) != 0) { - printf("Possible failure in string write of %s " - "Should be %s written to %s/%s\n", temp, val, - basedir, filename); + fprintf(stderr, + "Possible failure in string write of %s " + "Should be %s written to %s/%s\n", temp, val, + basedir, filename); ret = -1; } } @@ -856,7 +865,7 @@ int read_sysfs_posint(const char *filename, const char *basedir) char *temp = malloc(strlen(basedir) + strlen(filename) + 2); if (!temp) { - printf("Memory allocation failed"); + fprintf(stderr, "Memory allocation failed"); return -ENOMEM; } @@ -903,7 +912,7 @@ int read_sysfs_float(const char *filename, const char *basedir, float *val) char *temp = malloc(strlen(basedir) + strlen(filename) + 2); if (!temp) { - printf("Memory allocation failed"); + fprintf(stderr, "Memory allocation failed"); return -ENOMEM; } @@ -950,7 +959,7 @@ int read_sysfs_string(const char *filename, const char *basedir, char *str) char *temp = malloc(strlen(basedir) + strlen(filename) + 2); if (!temp) { - printf("Memory allocation failed"); + fprintf(stderr, "Memory allocation failed"); return -ENOMEM; } diff --git a/tools/iio/lsiio.c b/tools/iio/lsiio.c index 4f8172fe7881..b271a9a796d2 100644 --- a/tools/iio/lsiio.c +++ b/tools/iio/lsiio.c @@ -108,7 +108,7 @@ static int dump_devices(void) dp = opendir(iio_dir); if (!dp) { - printf("No industrial I/O devices available\n"); + fprintf(stderr, "No industrial I/O devices available\n"); return -ENODEV; } -- cgit v1.2.3 From 7868dfd216074fc5f902e7befacda2a0ec76e403 Mon Sep 17 00:00:00 2001 From: Joo Aun Saw Date: Thu, 23 Jul 2015 12:53:47 +1000 Subject: tools: iio: make scale and offset files optional Make scale and offset optional by adding -ENOENT check as not all drivers implement them. Signed-off-by: Joo Aun Saw Acked-by: Hartmut Knaack Signed-off-by: Jonathan Cameron --- tools/iio/iio_utils.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tools') diff --git a/tools/iio/iio_utils.c b/tools/iio/iio_utils.c index c3f9e37333e7..1dcdf03955cb 100644 --- a/tools/iio/iio_utils.c +++ b/tools/iio/iio_utils.c @@ -487,7 +487,7 @@ int build_channel_array(const char *device_dir, device_dir, current->name, current->generic_name); - if (ret < 0) + if ((ret < 0) && (ret != -ENOENT)) goto error_cleanup_array; ret = iioutils_get_param_float(¤t->offset, @@ -495,7 +495,7 @@ int build_channel_array(const char *device_dir, device_dir, current->name, current->generic_name); - if (ret < 0) + if ((ret < 0) && (ret != -ENOENT)) goto error_cleanup_array; ret = iioutils_get_type(¤t->is_signed, -- cgit v1.2.3 From ae067cb68d2ef42d4a84eeaf07411fe2936f48c4 Mon Sep 17 00:00:00 2001 From: Irina Tirdea Date: Fri, 24 Jul 2015 16:28:05 +0300 Subject: tools: iio: fix mask for 32 bit sensor data When the the sensor data uses 32 bits out of 32, generic_buffer prints the value 0 for all data read. In this case, the mask is shifted 32 bits, which is beyond the size of an integer. This will lead to the mask always being 0. Before printing, the mask is applied to the raw value, thus generating a final value of 0. Fix the mask by shifting a 64 bit value instead of an integer. Signed-off-by: Irina Tirdea Acked-by: Hartmut Knaack Signed-off-by: Jonathan Cameron --- tools/iio/iio_utils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tools') diff --git a/tools/iio/iio_utils.c b/tools/iio/iio_utils.c index 1dcdf03955cb..a95270f33353 100644 --- a/tools/iio/iio_utils.c +++ b/tools/iio/iio_utils.c @@ -168,7 +168,7 @@ int iioutils_get_type(unsigned *is_signed, unsigned *bytes, unsigned *bits_used, if (*bits_used == 64) *mask = ~0; else - *mask = (1 << *bits_used) - 1; + *mask = (1ULL << *bits_used) - 1; *is_signed = (signchar == 's'); if (fclose(sysfsfp)) { -- cgit v1.2.3 From e7231491ce6ae2053569166f60835a5594b3c0af Mon Sep 17 00:00:00 2001 From: Irina Tirdea Date: Fri, 24 Jul 2015 16:28:06 +0300 Subject: tools: iio: print error message when buffer enable fails Running generic_buffer without enabling any channel of the sensor will fail without printing any error message. Add an error message that indicates buffer enable failed. Signed-off-by: Irina Tirdea Acked-by: Hartmut Knaack Signed-off-by: Jonathan Cameron --- tools/iio/generic_buffer.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'tools') diff --git a/tools/iio/generic_buffer.c b/tools/iio/generic_buffer.c index 32f389ebb94d..9f7b85bf6ada 100644 --- a/tools/iio/generic_buffer.c +++ b/tools/iio/generic_buffer.c @@ -364,8 +364,11 @@ int main(int argc, char **argv) /* Enable the buffer */ ret = write_sysfs_int("enable", buf_dir_name, 1); - if (ret < 0) + if (ret < 0) { + fprintf(stderr, + "Failed to enable buffer: %s\n", strerror(-ret)); goto error_free_buf_dir_name; + } scan_size = size_from_channelarray(channels, num_channels); data = malloc(scan_size * buf_len); -- cgit v1.2.3 From 280f09256ab6f6180132da193b9f2e0b2fa61c51 Mon Sep 17 00:00:00 2001 From: Joo Aun Saw Date: Wed, 29 Jul 2015 01:12:24 +1000 Subject: tools: iio: iio_utils: remove unnecessary define guard Signed-off-by: Joo Aun Saw Acked-by: Daniel Baluta Signed-off-by: Jonathan Cameron --- tools/iio/iio_utils.c | 5 ----- 1 file changed, 5 deletions(-) (limited to 'tools') diff --git a/tools/iio/iio_utils.c b/tools/iio/iio_utils.c index a95270f33353..31aca9ab078b 100644 --- a/tools/iio/iio_utils.c +++ b/tools/iio/iio_utils.c @@ -6,9 +6,6 @@ * under the terms of the GNU General Public License version 2 as published by * the Free Software Foundation. */ -#ifndef _IIO_UTILS_H -#define _IIO_UTILS_H - #include #include #include @@ -990,5 +987,3 @@ error_free: return ret; } - -#endif /* _IIO_UTILS_H */ -- cgit v1.2.3 From 5e37c523921480093323c06529ab854f0635c59d Mon Sep 17 00:00:00 2001 From: Joo Aun Saw Date: Tue, 28 Jul 2015 11:46:13 +1000 Subject: tools: iio: iio_utils: Make calc_digits static Signed-off-by: Joo Aun Saw Acked-by: Daniel Baluta Signed-off-by: Jonathan Cameron --- tools/iio/iio_utils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tools') diff --git a/tools/iio/iio_utils.c b/tools/iio/iio_utils.c index 31aca9ab078b..4bbd6e7959cd 100644 --- a/tools/iio/iio_utils.c +++ b/tools/iio/iio_utils.c @@ -537,7 +537,7 @@ error_free_name: return ret; } -int calc_digits(int num) +static int calc_digits(int num) { int count = 0; -- cgit v1.2.3 From 6b20f40679108e3c04e9bdb3d719e364ec29289e Mon Sep 17 00:00:00 2001 From: Joo Aun Saw Date: Sat, 25 Jul 2015 01:23:28 +1000 Subject: tools: iio: Set caller's ci_array pointer to NULL after free On error, caller's ci_array is freed and set to NULL to avoid potential double free if some other user of this code is not sufficiently careful. Counter is reset to zero for consistency. Signed-off-by: Joo Aun Saw Signed-off-by: Jonathan Cameron --- tools/iio/iio_utils.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'tools') diff --git a/tools/iio/iio_utils.c b/tools/iio/iio_utils.c index 4bbd6e7959cd..8475b832ee39 100644 --- a/tools/iio/iio_utils.c +++ b/tools/iio/iio_utils.c @@ -526,6 +526,8 @@ error_cleanup_array: free((*ci_array)[i].generic_name); } free(*ci_array); + *ci_array = NULL; + *counter = 0; error_close_dir: if (dp) if (closedir(dp) == -1) -- cgit v1.2.3 From 95ddd3f4b17e1df20b5e23d7b81614e7c8a643da Mon Sep 17 00:00:00 2001 From: Joo Aun Saw Date: Sat, 25 Jul 2015 01:23:29 +1000 Subject: tools: iio: remove unnecessary double pointer Remove unnecessary double pointer from channel sorting function. Signed-off-by: Joo Aun Saw Signed-off-by: Jonathan Cameron --- tools/iio/iio_utils.c | 12 ++++++------ tools/iio/iio_utils.h | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) (limited to 'tools') diff --git a/tools/iio/iio_utils.c b/tools/iio/iio_utils.c index 8475b832ee39..5eb6793f3972 100644 --- a/tools/iio/iio_utils.c +++ b/tools/iio/iio_utils.c @@ -286,17 +286,17 @@ error_free_builtname: * @cnt: the amount of array elements **/ -void bsort_channel_array_by_index(struct iio_channel_info **ci_array, int cnt) +void bsort_channel_array_by_index(struct iio_channel_info *ci_array, int cnt) { struct iio_channel_info temp; int x, y; for (x = 0; x < cnt; x++) for (y = 0; y < (cnt - 1); y++) - if ((*ci_array)[y].index > (*ci_array)[y + 1].index) { - temp = (*ci_array)[y + 1]; - (*ci_array)[y + 1] = (*ci_array)[y]; - (*ci_array)[y] = temp; + if (ci_array[y].index > ci_array[y + 1].index) { + temp = ci_array[y + 1]; + ci_array[y + 1] = ci_array[y]; + ci_array[y] = temp; } } @@ -516,7 +516,7 @@ int build_channel_array(const char *device_dir, free(scan_el_dir); /* reorder so that the array is in index order */ - bsort_channel_array_by_index(ci_array, *counter); + bsort_channel_array_by_index(*ci_array, *counter); return 0; diff --git a/tools/iio/iio_utils.h b/tools/iio/iio_utils.h index f30a1091b53e..e3503bfe538b 100644 --- a/tools/iio/iio_utils.h +++ b/tools/iio/iio_utils.h @@ -60,7 +60,7 @@ int iioutils_get_type(unsigned *is_signed, unsigned *bytes, unsigned *bits_used, int iioutils_get_param_float(float *output, const char *param_name, const char *device_dir, const char *name, const char *generic_name); -void bsort_channel_array_by_index(struct iio_channel_info **ci_array, int cnt); +void bsort_channel_array_by_index(struct iio_channel_info *ci_array, int cnt); int build_channel_array(const char *device_dir, struct iio_channel_info **ci_array, int *counter); int find_type_by_name(const char *name, const char *type); -- cgit v1.2.3 From af255cd562aaa72455f9022a26afacd68f3fbf73 Mon Sep 17 00:00:00 2001 From: Linus Walleij Date: Tue, 4 Aug 2015 16:21:49 +0200 Subject: iio: lsiio: fix error code handling error commit acf50b3586f8d8a7530b905e111dda41876d38f4 "tools:iio:lsiio: add error handling" introduced error handling of errors returned from read_sysfs_string(), but with a simple if (retval), missing the fact that these functions return a positive value if the read was successful. As a result lsiio regresses and does not show any devices on my filesystem. Fix this by checking for only negative error codes. Cc: Hartmut Knaack Signed-off-by: Linus Walleij Acked-by: Hartmut Knaack Signed-off-by: Jonathan Cameron --- tools/iio/lsiio.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tools') diff --git a/tools/iio/lsiio.c b/tools/iio/lsiio.c index b271a9a796d2..3d650e668252 100644 --- a/tools/iio/lsiio.c +++ b/tools/iio/lsiio.c @@ -69,7 +69,7 @@ static int dump_one_device(const char *dev_dir_name) return -EINVAL; ret = read_sysfs_string("name", dev_dir_name, name); - if (ret) + if (ret < 0) return ret; printf("Device %03d: %s\n", dev_idx, name); @@ -92,7 +92,7 @@ static int dump_one_trigger(const char *dev_dir_name) return -EINVAL; ret = read_sysfs_string("name", dev_dir_name, name); - if (ret) + if (ret < 0) return ret; printf("Trigger %03d: %s\n", dev_idx, name); -- cgit v1.2.3