summaryrefslogtreecommitdiffstats
path: root/kernel/tools/iio
diff options
context:
space:
mode:
authorJosé Pekkarinen <jose.pekkarinen@nokia.com>2016-04-11 10:41:07 +0300
committerJosé Pekkarinen <jose.pekkarinen@nokia.com>2016-04-13 08:17:18 +0300
commite09b41010ba33a20a87472ee821fa407a5b8da36 (patch)
treed10dc367189862e7ca5c592f033dc3726e1df4e3 /kernel/tools/iio
parentf93b97fd65072de626c074dbe099a1fff05ce060 (diff)
These changes are the raw update to linux-4.4.6-rt14. Kernel sources
are taken from kernel.org, and rt patch from the rt wiki download page. During the rebasing, the following patch collided: Force tick interrupt and get rid of softirq magic(I70131fb85). Collisions have been removed because its logic was found on the source already. Change-Id: I7f57a4081d9deaa0d9ccfc41a6c8daccdee3b769 Signed-off-by: José Pekkarinen <jose.pekkarinen@nokia.com>
Diffstat (limited to 'kernel/tools/iio')
-rw-r--r--kernel/tools/iio/Makefile4
-rw-r--r--kernel/tools/iio/generic_buffer.c298
-rw-r--r--kernel/tools/iio/iio_event_monitor.c90
-rw-r--r--kernel/tools/iio/iio_utils.c688
-rw-r--r--kernel/tools/iio/iio_utils.h39
-rw-r--r--kernel/tools/iio/lsiio.c106
6 files changed, 863 insertions, 362 deletions
diff --git a/kernel/tools/iio/Makefile b/kernel/tools/iio/Makefile
index bf7ae6d66..3a7a54f59 100644
--- a/kernel/tools/iio/Makefile
+++ b/kernel/tools/iio/Makefile
@@ -1,5 +1,5 @@
-CC = gcc
-CFLAGS = -Wall -g -D_GNU_SOURCE
+CC = $(CROSS_COMPILE)gcc
+CFLAGS += -Wall -g -D_GNU_SOURCE
all: iio_event_monitor lsiio generic_buffer
diff --git a/kernel/tools/iio/generic_buffer.c b/kernel/tools/iio/generic_buffer.c
index f805493be..01c4f6780 100644
--- a/kernel/tools/iio/generic_buffer.c
+++ b/kernel/tools/iio/generic_buffer.c
@@ -51,47 +51,113 @@ int size_from_channelarray(struct iio_channel_info *channels, int num_channels)
if (bytes % channels[i].bytes == 0)
channels[i].location = bytes;
else
- channels[i].location = bytes - bytes%channels[i].bytes
- + channels[i].bytes;
+ channels[i].location = bytes - bytes % channels[i].bytes
+ + channels[i].bytes;
+
bytes = channels[i].location + channels[i].bytes;
i++;
}
+
return bytes;
}
-void print2byte(int input, struct iio_channel_info *info)
+void print1byte(uint8_t input, struct iio_channel_info *info)
+{
+ /*
+ * Shift before conversion to avoid sign extension
+ * of left aligned data
+ */
+ input >>= info->shift;
+ input &= info->mask;
+ if (info->is_signed) {
+ int8_t val = (int8_t)(input << (8 - info->bits_used)) >>
+ (8 - info->bits_used);
+ printf("%05f ", ((float)val + info->offset) * info->scale);
+ } else {
+ printf("%05f ", ((float)input + info->offset) * info->scale);
+ }
+}
+
+void print2byte(uint16_t input, struct iio_channel_info *info)
{
/* First swap if incorrect endian */
if (info->be)
- input = be16toh((uint16_t)input);
+ input = be16toh(input);
else
- input = le16toh((uint16_t)input);
+ input = le16toh(input);
/*
* Shift before conversion to avoid sign extension
* of left aligned data
*/
input >>= info->shift;
+ input &= info->mask;
if (info->is_signed) {
- int16_t val = input;
+ int16_t val = (int16_t)(input << (16 - info->bits_used)) >>
+ (16 - info->bits_used);
+ printf("%05f ", ((float)val + info->offset) * info->scale);
+ } else {
+ printf("%05f ", ((float)input + info->offset) * info->scale);
+ }
+}
+
+void print4byte(uint32_t input, struct iio_channel_info *info)
+{
+ /* First swap if incorrect endian */
+ if (info->be)
+ input = be32toh(input);
+ else
+ input = le32toh(input);
- val &= (1 << info->bits_used) - 1;
- val = (int16_t)(val << (16 - info->bits_used)) >>
- (16 - info->bits_used);
- printf("%05f ", ((float)val + info->offset)*info->scale);
+ /*
+ * Shift before conversion to avoid sign extension
+ * of left aligned data
+ */
+ input >>= info->shift;
+ input &= info->mask;
+ if (info->is_signed) {
+ int32_t val = (int32_t)(input << (32 - info->bits_used)) >>
+ (32 - info->bits_used);
+ printf("%05f ", ((float)val + info->offset) * info->scale);
} else {
- uint16_t val = input;
+ printf("%05f ", ((float)input + info->offset) * info->scale);
+ }
+}
+
+void print8byte(uint64_t input, struct iio_channel_info *info)
+{
+ /* First swap if incorrect endian */
+ if (info->be)
+ input = be64toh(input);
+ else
+ input = le64toh(input);
- val &= (1 << info->bits_used) - 1;
- printf("%05f ", ((float)val + info->offset)*info->scale);
+ /*
+ * Shift before conversion to avoid sign extension
+ * of left aligned data
+ */
+ input >>= info->shift;
+ input &= info->mask;
+ if (info->is_signed) {
+ int64_t val = (int64_t)(input << (64 - info->bits_used)) >>
+ (64 - info->bits_used);
+ /* special case for timestamp */
+ if (info->scale == 1.0f && info->offset == 0.0f)
+ printf("%" PRId64 " ", val);
+ else
+ printf("%05f ",
+ ((float)val + info->offset) * info->scale);
+ } else {
+ printf("%05f ", ((float)input + info->offset) * info->scale);
}
}
+
/**
* process_scan() - print out the values in SI units
* @data: pointer to the start of the scan
- * @channels: information about the channels. Note
- * size_from_channelarray must have been called first to fill the
- * location offsets.
+ * @channels: information about the channels.
+ * Note: size_from_channelarray must have been called first
+ * to fill the location offsets.
* @num_channels: number of channels
**/
void process_scan(char *data,
@@ -103,37 +169,21 @@ void process_scan(char *data,
for (k = 0; k < num_channels; k++)
switch (channels[k].bytes) {
/* only a few cases implemented so far */
+ case 1:
+ print1byte(*(uint8_t *)(data + channels[k].location),
+ &channels[k]);
+ break;
case 2:
print2byte(*(uint16_t *)(data + channels[k].location),
&channels[k]);
break;
case 4:
- if (!channels[k].is_signed) {
- uint32_t val = *(uint32_t *)
- (data + channels[k].location);
- printf("%05f ", ((float)val +
- channels[k].offset)*
- channels[k].scale);
-
- }
+ print4byte(*(uint32_t *)(data + channels[k].location),
+ &channels[k]);
break;
case 8:
- if (channels[k].is_signed) {
- int64_t val = *(int64_t *)
- (data +
- channels[k].location);
- if ((val >> channels[k].bits_used) & 1)
- val = (val & channels[k].mask) |
- ~channels[k].mask;
- /* special case for timestamp */
- if (channels[k].scale == 1.0f &&
- channels[k].offset == 0.0f)
- printf("%" PRId64 " ", val);
- else
- printf("%05f ", ((float)val +
- channels[k].offset)*
- channels[k].scale);
- }
+ print8byte(*(uint64_t *)(data + channels[k].location),
+ &channels[k]);
break;
default:
break;
@@ -141,6 +191,19 @@ void process_scan(char *data,
printf("\n");
}
+void print_usage(void)
+{
+ fprintf(stderr, "Usage: generic_buffer [options]...\n"
+ "Capture, convert and output data from IIO device buffer\n"
+ " -c <n> Do n conversions\n"
+ " -e Disable wait for event (new data)\n"
+ " -g Use trigger-less mode\n"
+ " -l <n> Set buffer length to n samples\n"
+ " -n <name> Set device name (mandatory)\n"
+ " -t <name> Set trigger name\n"
+ " -w <n> Set delay between reads in us (event-less mode)\n");
+}
+
int main(int argc, char **argv)
{
unsigned long num_loops = 2;
@@ -166,8 +229,28 @@ int main(int argc, char **argv)
struct iio_channel_info *channels;
- while ((c = getopt(argc, argv, "l:w:c:et:n:g")) != -1) {
+ while ((c = getopt(argc, argv, "c:egl:n:t:w:")) != -1) {
switch (c) {
+ case 'c':
+ errno = 0;
+ num_loops = strtoul(optarg, &dummy, 10);
+ if (errno)
+ return -errno;
+
+ break;
+ case 'e':
+ noevents = 1;
+ break;
+ case 'g':
+ notrigger = 1;
+ break;
+ case 'l':
+ errno = 0;
+ buf_len = strtoul(optarg, &dummy, 10);
+ if (errno)
+ return -errno;
+
+ break;
case 'n':
device_name = optarg;
break;
@@ -175,42 +258,39 @@ int main(int argc, char **argv)
trigger_name = optarg;
datardytrigger = 0;
break;
- case 'e':
- noevents = 1;
- break;
- case 'c':
- num_loops = strtoul(optarg, &dummy, 10);
- break;
case 'w':
+ errno = 0;
timedelay = strtoul(optarg, &dummy, 10);
- break;
- case 'l':
- buf_len = strtoul(optarg, &dummy, 10);
- break;
- case 'g':
- notrigger = 1;
+ if (errno)
+ return -errno;
break;
case '?':
+ print_usage();
return -1;
}
}
- if (device_name == NULL)
+ if (!device_name) {
+ fprintf(stderr, "Device name not set\n");
+ print_usage();
return -1;
+ }
/* 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);
- ret = -ENODEV;
- goto error_ret;
+ fprintf(stderr, "Failed to find the %s\n", device_name);
+ return dev_num;
}
+
printf("iio device number being used is %d\n", dev_num);
- asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num);
+ ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num);
+ if (ret < 0)
+ return -ENOMEM;
if (!notrigger) {
- if (trigger_name == NULL) {
+ if (!trigger_name) {
/*
* Build the trigger name. If it is device associated
* its name is <device_name>_dev[n] where n matches
@@ -220,20 +300,23 @@ int main(int argc, char **argv)
"%s-dev%d", device_name, dev_num);
if (ret < 0) {
ret = -ENOMEM;
- goto error_ret;
+ goto error_free_dev_dir_name;
}
}
/* 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);
- ret = -ENODEV;
+ fprintf(stderr, "Failed to find the trigger %s\n",
+ trigger_name);
+ ret = trig_num;
goto error_free_triggername;
}
+
printf("iio trigger number being used is %d\n", trig_num);
- } else
+ } else {
printf("trigger-less mode selected\n");
+ }
/*
* Parse the files in scan_elements to identify what channels are
@@ -241,8 +324,17 @@ 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;
+ }
+ if (!num_channels) {
+ fprintf(stderr,
+ "No channels are enabled, we have nothing to scan.\n");
+ fprintf(stderr, "Enable channels manually in "
+ FORMAT_SCAN_ELEMENTS_DIR
+ "/*_en and try again.\n", dev_dir_name);
+ ret = -ENOENT;
goto error_free_triggername;
}
@@ -255,18 +347,21 @@ int main(int argc, char **argv)
"%siio:device%d/buffer", iio_dir, dev_num);
if (ret < 0) {
ret = -ENOMEM;
- goto error_free_triggername;
+ goto error_free_channels;
}
if (!notrigger) {
printf("%s %s\n", dev_dir_name, trigger_name);
- /* Set the device trigger to be the data ready trigger found
- * above */
+ /*
+ * Set the device trigger to be the data ready trigger found
+ * above
+ */
ret = write_sysfs_string_and_verify("trigger/current_trigger",
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;
}
}
@@ -278,10 +373,14 @@ 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);
+ data = malloc(scan_size * buf_len);
if (!data) {
ret = -ENOMEM;
goto error_free_buf_dir_name;
@@ -295,13 +394,12 @@ int main(int argc, char **argv)
/* Attempt to open non blocking the access dev */
fp = open(buffer_access, O_RDONLY | O_NONBLOCK);
- if (fp == -1) { /* If it isn't there make the node */
- printf("Failed to open %s\n", buffer_access);
+ if (fp == -1) { /* TODO: If it isn't there make the node */
ret = -errno;
+ fprintf(stderr, "Failed to open %s\n", buffer_access);
goto error_free_buffer_access;
}
- /* Wait for events 10 times */
for (j = 0; j < num_loops; j++) {
if (!noevents) {
struct pollfd pfd = {
@@ -309,27 +407,31 @@ int main(int argc, char **argv)
.events = POLLIN,
};
- poll(&pfd, 1, -1);
- toread = buf_len;
+ ret = poll(&pfd, 1, -1);
+ if (ret < 0) {
+ ret = -errno;
+ goto error_close_buffer_access;
+ } else if (ret == 0) {
+ continue;
+ }
+ toread = buf_len;
} else {
usleep(timedelay);
toread = 64;
}
- read_size = read(fp,
- data,
- toread*scan_size);
+ read_size = read(fp, data, toread * scan_size);
if (read_size < 0) {
- if (errno == -EAGAIN) {
- printf("nothing available\n");
+ if (errno == EAGAIN) {
+ fprintf(stderr, "nothing available\n");
continue;
- } else
+ } else {
break;
+ }
}
- for (i = 0; i < read_size/scan_size; i++)
- process_scan(data + scan_size*i,
- channels,
+ for (i = 0; i < read_size / scan_size; i++)
+ process_scan(data + scan_size * i, channels,
num_channels);
}
@@ -340,20 +442,34 @@ int main(int argc, char **argv)
if (!notrigger)
/* Disconnect the trigger - just write a dummy name. */
- write_sysfs_string("trigger/current_trigger",
- dev_dir_name, "NULL");
+ ret = write_sysfs_string("trigger/current_trigger",
+ dev_dir_name, "NULL");
+ if (ret < 0)
+ fprintf(stderr, "Failed to write to %s\n",
+ dev_dir_name);
error_close_buffer_access:
- close(fp);
-error_free_data:
- free(data);
+ if (close(fp) == -1)
+ perror("Failed to close buffer");
+
error_free_buffer_access:
free(buffer_access);
+error_free_data:
+ free(data);
error_free_buf_dir_name:
free(buf_dir_name);
+error_free_channels:
+ for (i = num_channels - 1; i >= 0; i--) {
+ free(channels[i].name);
+ free(channels[i].generic_name);
+ }
+ free(channels);
error_free_triggername:
if (datardytrigger)
free(trigger_name);
-error_ret:
+
+error_free_dev_dir_name:
+ free(dev_dir_name);
+
return ret;
}
diff --git a/kernel/tools/iio/iio_event_monitor.c b/kernel/tools/iio/iio_event_monitor.c
index 427c271ac..d51eb0420 100644
--- a/kernel/tools/iio/iio_event_monitor.c
+++ b/kernel/tools/iio/iio_event_monitor.c
@@ -13,7 +13,6 @@
*
* Usage:
* iio_event_monitor <device_name>
- *
*/
#include <unistd.h>
@@ -51,6 +50,9 @@ static const char * const iio_chan_type_name_spec[] = {
[IIO_HUMIDITYRELATIVE] = "humidityrelative",
[IIO_ACTIVITY] = "activity",
[IIO_STEPS] = "steps",
+ [IIO_ENERGY] = "energy",
+ [IIO_DISTANCE] = "distance",
+ [IIO_VELOCITY] = "velocity",
};
static const char * const iio_ev_type_text[] = {
@@ -99,6 +101,7 @@ static const char * const iio_modifier_names[] = {
[IIO_MOD_JOGGING] = "jogging",
[IIO_MOD_WALKING] = "walking",
[IIO_MOD_STILL] = "still",
+ [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)",
};
static bool event_is_known(struct iio_event_data *event)
@@ -130,6 +133,9 @@ static bool event_is_known(struct iio_event_data *event)
case IIO_HUMIDITYRELATIVE:
case IIO_ACTIVITY:
case IIO_STEPS:
+ case IIO_ENERGY:
+ case IIO_DISTANCE:
+ case IIO_VELOCITY:
break;
default:
return false;
@@ -167,6 +173,7 @@ static bool event_is_known(struct iio_event_data *event)
case IIO_MOD_JOGGING:
case IIO_MOD_WALKING:
case IIO_MOD_STILL:
+ case IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z:
break;
default:
return false;
@@ -208,31 +215,29 @@ 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;
}
- printf("Event: time: %lld, ", event->timestamp);
+ printf("Event: time: %lld, type: %s", event->timestamp,
+ iio_chan_type_name_spec[type]);
- if (mod != IIO_NO_MOD) {
- printf("type: %s(%s), ",
- iio_chan_type_name_spec[type],
- iio_modifier_names[mod]);
- } else {
- printf("type: %s, ",
- iio_chan_type_name_spec[type]);
- }
+ if (mod != IIO_NO_MOD)
+ printf("(%s)", iio_modifier_names[mod]);
- if (diff && chan >= 0 && chan2 >= 0)
- printf("channel: %d-%d, ", chan, chan2);
- else if (chan >= 0)
- printf("channel: %d, ", chan);
+ if (chan >= 0) {
+ printf(", channel: %d", chan);
+ if (diff && chan2 >= 0)
+ printf("-%d", chan2);
+ }
- printf("evtype: %s", iio_ev_type_text[ev_type]);
+ printf(", evtype: %s", iio_ev_type_text[ev_type]);
if (dir != IIO_EV_DIR_NONE)
printf(", direction: %s", iio_ev_dir_text[dir]);
+
printf("\n");
}
@@ -246,7 +251,7 @@ int main(int argc, char **argv)
int fd, event_fd;
if (argc <= 1) {
- printf("Usage: %s <device_name>\n", argv[0]);
+ fprintf(stderr, "Usage: %s <device_name>\n", argv[0]);
return -1;
}
@@ -255,31 +260,42 @@ int main(int argc, char **argv)
dev_num = find_type_by_name(device_name, "iio:device");
if (dev_num >= 0) {
printf("Found IIO device with name %s with device number %d\n",
- device_name, dev_num);
+ device_name, dev_num);
ret = asprintf(&chrdev_name, "/dev/iio:device%d", dev_num);
- if (ret < 0) {
- ret = -ENOMEM;
- goto error_ret;
- }
+ if (ret < 0)
+ return -ENOMEM;
} else {
- /* If we can't find a IIO device by name assume device_name is a
- IIO chrdev */
+ /*
+ * If we can't find an IIO device by name assume device_name is
+ * an IIO chrdev
+ */
chrdev_name = strdup(device_name);
+ if (!chrdev_name)
+ return -ENOMEM;
}
fd = open(chrdev_name, 0);
if (fd == -1) {
- fprintf(stdout, "Failed to open %s\n", chrdev_name);
ret = -errno;
+ 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;
+ if (ret == -ENODEV)
+ fprintf(stderr,
+ "This device does not support events\n");
+ else
+ fprintf(stderr, "Failed to retrieve event fd\n");
+ if (close(fd) == -1)
+ perror("Failed to close character device file");
- close(fd);
+ goto error_free_chrdev_name;
+ }
- if (ret == -1 || event_fd == -1) {
- fprintf(stdout, "Failed to retrieve event fd\n");
+ if (close(fd) == -1) {
ret = -errno;
goto error_free_chrdev_name;
}
@@ -288,21 +304,29 @@ 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 {
- perror("Failed to read event from device");
ret = -errno;
+ perror("Failed to read event from device");
break;
}
}
+ if (ret != sizeof(event)) {
+ fprintf(stderr, "Reading event failed!\n");
+ ret = -EIO;
+ break;
+ }
+
print_event(&event);
}
- close(event_fd);
+ if (close(event_fd) == -1)
+ perror("Failed to close event file");
+
error_free_chrdev_name:
free(chrdev_name);
-error_ret:
+
return ret;
}
diff --git a/kernel/tools/iio/iio_utils.c b/kernel/tools/iio/iio_utils.c
index 6f6452167..5eb6793f3 100644
--- a/kernel/tools/iio/iio_utils.c
+++ b/kernel/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 <string.h>
#include <stdlib.h>
#include <stdio.h>
@@ -29,16 +26,17 @@ static char * const iio_direction[] = {
* iioutils_break_up_name() - extract generic name from full channel name
* @full_name: the full channel name
* @generic_name: the output generic channel name
+ *
+ * Returns 0 on success, or a negative error code if string extraction failed.
**/
-int iioutils_break_up_name(const char *full_name,
- char **generic_name)
+int iioutils_break_up_name(const char *full_name, char **generic_name)
{
char *current;
char *w, *r;
char *working, *prefix = "";
- int i;
+ 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];
@@ -46,7 +44,14 @@ int iioutils_break_up_name(const char *full_name,
}
current = strdup(full_name + strlen(prefix) + 1);
+ if (!current)
+ return -ENOMEM;
+
working = strtok(current, "_\0");
+ if (!working) {
+ free(current);
+ return -EINVAL;
+ }
w = working;
r = working;
@@ -56,34 +61,34 @@ int iioutils_break_up_name(const char *full_name,
*w = *r;
w++;
}
+
r++;
}
*w = '\0';
- asprintf(generic_name, "%s_%s", prefix, working);
+ ret = asprintf(generic_name, "%s_%s", prefix, working);
free(current);
- return 0;
+ return (ret == -1) ? -ENOMEM : 0;
}
/**
* iioutils_get_type() - find and process _type attribute data
* @is_signed: output whether channel is signed
* @bytes: output how many bytes the channel storage occupies
+ * @bits_used: output number of valid bits of data
+ * @shift: output amount of bits to shift right data before applying bit mask
* @mask: output a bit mask for the raw data
- * @be: big endian
- * @device_dir: the iio device directory
+ * @be: output if data in big endian
+ * @device_dir: the IIO device directory
* @name: the channel name
* @generic_name: the channel type name
+ *
+ * Returns a value >= 0 on success, otherwise a negative error code.
**/
-int iioutils_get_type(unsigned *is_signed,
- unsigned *bytes,
- unsigned *bits_used,
- unsigned *shift,
- uint64_t *mask,
- unsigned *be,
- const char *device_dir,
- const char *name,
- const char *generic_name)
+int iioutils_get_type(unsigned *is_signed, unsigned *bytes, unsigned *bits_used,
+ unsigned *shift, uint64_t *mask, unsigned *be,
+ const char *device_dir, const char *name,
+ const char *generic_name)
{
FILE *sysfsfp;
int ret;
@@ -94,10 +99,9 @@ int iioutils_get_type(unsigned *is_signed,
const struct dirent *ent;
ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
- if (ret < 0) {
- ret = -ENOMEM;
- goto error_ret;
- }
+ if (ret < 0)
+ return -ENOMEM;
+
ret = asprintf(&builtname, FORMAT_TYPE_FILE, name);
if (ret < 0) {
ret = -ENOMEM;
@@ -110,11 +114,13 @@ int iioutils_get_type(unsigned *is_signed,
}
dp = opendir(scan_el_dir);
- if (dp == NULL) {
+ if (!dp) {
ret = -errno;
goto error_free_builtname_generic;
}
- while (ent = readdir(dp), ent != NULL)
+
+ ret = -ENOENT;
+ while (ent = readdir(dp), ent)
/*
* Do we allow devices to override a generic name with
* a specific one?
@@ -127,10 +133,12 @@ int iioutils_get_type(unsigned *is_signed,
ret = -ENOMEM;
goto error_closedir;
}
+
sysfsfp = fopen(filename, "r");
- if (sysfsfp == NULL) {
- printf("failed to open %s\n", filename);
+ if (!sysfsfp) {
ret = -errno;
+ fprintf(stderr, "failed to open %s\n",
+ filename);
goto error_free_filename;
}
@@ -141,49 +149,73 @@ int iioutils_get_type(unsigned *is_signed,
bits_used,
&padint, shift);
if (ret < 0) {
- printf("failed to pass scan type description\n");
ret = -errno;
+ fprintf(stderr,
+ "failed to pass scan type description\n");
+ goto error_close_sysfsfp;
+ } else if (ret != 5) {
+ ret = -EIO;
+ fprintf(stderr,
+ "scan type description didn't match\n");
goto error_close_sysfsfp;
}
+
*be = (endianchar == 'b');
*bytes = padint / 8;
if (*bits_used == 64)
*mask = ~0;
else
- *mask = (1 << *bits_used) - 1;
- if (signchar == 's')
- *is_signed = 1;
- else
- *is_signed = 0;
- fclose(sysfsfp);
- free(filename);
+ *mask = (1ULL << *bits_used) - 1;
+
+ *is_signed = (signchar == 's');
+ if (fclose(sysfsfp)) {
+ ret = -errno;
+ fprintf(stderr, "Failed to close %s\n",
+ filename);
+ goto error_free_filename;
+ }
- filename = 0;
sysfsfp = 0;
+ free(filename);
+ filename = 0;
}
+
error_close_sysfsfp:
if (sysfsfp)
- fclose(sysfsfp);
+ if (fclose(sysfsfp))
+ perror("iioutils_get_type(): Failed to close file");
+
error_free_filename:
if (filename)
free(filename);
+
error_closedir:
- closedir(dp);
+ if (closedir(dp) == -1)
+ perror("iioutils_get_type(): Failed to close directory");
+
error_free_builtname_generic:
free(builtname_generic);
error_free_builtname:
free(builtname);
error_free_scan_el_dir:
free(scan_el_dir);
-error_ret:
+
return ret;
}
-int iioutils_get_param_float(float *output,
- const char *param_name,
- const char *device_dir,
- const char *name,
- const char *generic_name)
+/**
+ * iioutils_get_param_float() - read a float value from a channel parameter
+ * @output: output the float value
+ * @param_name: the parameter name to read
+ * @device_dir: the IIO device directory in sysfs
+ * @name: the channel name
+ * @generic_name: the channel type name
+ *
+ * Returns a value >= 0 on success, otherwise a negative error code.
+ **/
+int iioutils_get_param_float(float *output, const char *param_name,
+ const char *device_dir, const char *name,
+ const char *generic_name)
{
FILE *sysfsfp;
int ret;
@@ -193,22 +225,24 @@ int iioutils_get_param_float(float *output,
const struct dirent *ent;
ret = asprintf(&builtname, "%s_%s", name, param_name);
- if (ret < 0) {
- ret = -ENOMEM;
- goto error_ret;
- }
+ if (ret < 0)
+ return -ENOMEM;
+
ret = asprintf(&builtname_generic,
"%s_%s", generic_name, param_name);
if (ret < 0) {
ret = -ENOMEM;
goto error_free_builtname;
}
+
dp = opendir(device_dir);
- if (dp == NULL) {
+ if (!dp) {
ret = -errno;
goto error_free_builtname_generic;
}
- while (ent = readdir(dp), ent != NULL)
+
+ ret = -ENOENT;
+ while (ent = readdir(dp), ent)
if ((strcmp(builtname, ent->d_name) == 0) ||
(strcmp(builtname_generic, ent->d_name) == 0)) {
ret = asprintf(&filename,
@@ -217,60 +251,69 @@ int iioutils_get_param_float(float *output,
ret = -ENOMEM;
goto error_closedir;
}
+
sysfsfp = fopen(filename, "r");
if (!sysfsfp) {
ret = -errno;
goto error_free_filename;
}
- fscanf(sysfsfp, "%f", output);
+
+ errno = 0;
+ if (fscanf(sysfsfp, "%f", output) != 1)
+ ret = errno ? -errno : -ENODATA;
+
break;
}
error_free_filename:
if (filename)
free(filename);
+
error_closedir:
- closedir(dp);
+ if (closedir(dp) == -1)
+ perror("iioutils_get_param_float(): Failed to close directory");
+
error_free_builtname_generic:
free(builtname_generic);
error_free_builtname:
free(builtname);
-error_ret:
+
return ret;
}
/**
- * bsort_channel_array_by_index() - reorder so that the array is in index order
- *
+ * bsort_channel_array_by_index() - sort the array in index order
+ * @ci_array: the iio_channel_info array to be sorted
+ * @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;
}
}
/**
* build_channel_array() - function to figure out what channels are present
* @device_dir: the IIO device directory in sysfs
- * @
+ * @ci_array: output the resulting array of iio_channel_info
+ * @counter: output the amount of array elements
+ *
+ * Returns 0 on success, otherwise a negative error code.
**/
int build_channel_array(const char *device_dir,
- struct iio_channel_info **ci_array,
- int *counter)
+ struct iio_channel_info **ci_array, int *counter)
{
DIR *dp;
FILE *sysfsfp;
- int count, i;
+ int count = 0, i;
struct iio_channel_info *current;
int ret;
const struct dirent *ent;
@@ -279,16 +322,16 @@ int build_channel_array(const char *device_dir,
*counter = 0;
ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
- if (ret < 0) {
- ret = -ENOMEM;
- goto error_ret;
- }
+ if (ret < 0)
+ 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,
@@ -297,26 +340,43 @@ int build_channel_array(const char *device_dir,
ret = -ENOMEM;
goto error_close_dir;
}
+
sysfsfp = fopen(filename, "r");
- if (sysfsfp == NULL) {
+ if (!sysfsfp) {
ret = -errno;
free(filename);
goto error_close_dir;
}
- fscanf(sysfsfp, "%i", &ret);
+
+ errno = 0;
+ if (fscanf(sysfsfp, "%i", &ret) != 1) {
+ ret = errno ? -errno : -ENODATA;
+ if (fclose(sysfsfp))
+ perror("build_channel_array(): Failed to close file");
+
+ free(filename);
+ goto error_close_dir;
+ }
if (ret == 1)
(*counter)++;
- fclose(sysfsfp);
+
+ if (fclose(sysfsfp)) {
+ ret = -errno;
+ free(filename);
+ goto error_close_dir;
+ }
+
free(filename);
}
+
*ci_array = malloc(sizeof(**ci_array) * (*counter));
- if (*ci_array == NULL) {
+ if (!*ci_array) {
ret = -ENOMEM;
goto error_close_dir;
}
+
seekdir(dp, 0);
- count = 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;
@@ -330,14 +390,29 @@ int build_channel_array(const char *device_dir,
count--;
goto error_cleanup_array;
}
+
sysfsfp = fopen(filename, "r");
- if (sysfsfp == NULL) {
+ if (!sysfsfp) {
+ ret = -errno;
free(filename);
+ count--;
+ goto error_cleanup_array;
+ }
+
+ errno = 0;
+ if (fscanf(sysfsfp, "%i", &current_enabled) != 1) {
+ ret = errno ? -errno : -ENODATA;
+ free(filename);
+ count--;
+ goto error_cleanup_array;
+ }
+
+ if (fclose(sysfsfp)) {
ret = -errno;
+ free(filename);
+ count--;
goto error_cleanup_array;
}
- fscanf(sysfsfp, "%i", &current_enabled);
- fclose(sysfsfp);
if (!current_enabled) {
free(filename);
@@ -350,18 +425,23 @@ 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--;
goto error_cleanup_array;
}
+
/* Get the generic and specific name elements */
ret = iioutils_break_up_name(current->name,
&current->generic_name);
if (ret) {
free(filename);
+ free(current->name);
+ count--;
goto error_cleanup_array;
}
+
ret = asprintf(&filename,
"%s/%s_index",
scan_el_dir,
@@ -371,9 +451,32 @@ int build_channel_array(const char *device_dir,
ret = -ENOMEM;
goto error_cleanup_array;
}
+
sysfsfp = fopen(filename, "r");
- fscanf(sysfsfp, "%u", &current->index);
- fclose(sysfsfp);
+ if (!sysfsfp) {
+ ret = -errno;
+ fprintf(stderr, "failed to open %s\n",
+ filename);
+ free(filename);
+ goto error_cleanup_array;
+ }
+
+ errno = 0;
+ if (fscanf(sysfsfp, "%u", &current->index) != 1) {
+ ret = errno ? -errno : -ENODATA;
+ if (fclose(sysfsfp))
+ perror("build_channel_array(): Failed to close file");
+
+ free(filename);
+ goto error_cleanup_array;
+ }
+
+ if (fclose(sysfsfp)) {
+ ret = -errno;
+ free(filename);
+ goto error_cleanup_array;
+ }
+
free(filename);
/* Find the scale */
ret = iioutils_get_param_float(&current->scale,
@@ -381,15 +484,17 @@ 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(&current->offset,
"offset",
device_dir,
current->name,
current->generic_name);
- if (ret < 0)
+ if ((ret < 0) && (ret != -ENOENT))
goto error_cleanup_array;
+
ret = iioutils_get_type(&current->is_signed,
&current->bytes,
&current->bits_used,
@@ -399,182 +504,317 @@ int build_channel_array(const char *device_dir,
device_dir,
current->name,
current->generic_name);
+ if (ret < 0)
+ goto error_cleanup_array;
}
}
- closedir(dp);
+ if (closedir(dp) == -1) {
+ ret = -errno;
+ goto error_cleanup_array;
+ }
+
+ 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;
error_cleanup_array:
- for (i = count - 1; i >= 0; i--)
+ for (i = count - 1; i >= 0; i--) {
free((*ci_array)[i].name);
+ free((*ci_array)[i].generic_name);
+ }
free(*ci_array);
+ *ci_array = NULL;
+ *counter = 0;
error_close_dir:
- closedir(dp);
+ if (dp)
+ if (closedir(dp) == -1)
+ perror("build_channel_array(): Failed to close dir");
+
error_free_name:
free(scan_el_dir);
-error_ret:
+
return ret;
}
+static int calc_digits(int num)
+{
+ int count = 0;
+
+ while (num != 0) {
+ num /= 10;
+ count++;
+ }
+
+ return count;
+}
+
/**
* find_type_by_name() - function to match top level types by name
* @name: top level type instance name
- * @type: the type of top level instance being sort
+ * @type: the type of top level instance being searched
*
+ * Returns the device number of a matched IIO device on success, otherwise a
+ * negative error code.
* Typical types this is used for are device and trigger.
**/
int find_type_by_name(const char *name, const char *type)
{
const struct dirent *ent;
- int number, numstrlen;
+ int number, numstrlen, ret;
- FILE *nameFile;
+ FILE *namefp;
DIR *dp;
char thisname[IIO_MAX_NAME_LENGTH];
char *filename;
dp = opendir(iio_dir);
- if (dp == NULL) {
- printf("No industrialio devices available\n");
+ if (!dp) {
+ fprintf(stderr, "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) &&
- strncmp(ent->d_name, type, strlen(type)) == 0) {
- numstrlen = sscanf(ent->d_name + strlen(type),
- "%d",
- &number);
+ strcmp(ent->d_name, "..") != 0 &&
+ strlen(ent->d_name) > strlen(type) &&
+ strncmp(ent->d_name, type, strlen(type)) == 0) {
+ errno = 0;
+ ret = sscanf(ent->d_name + strlen(type), "%d", &number);
+ if (ret < 0) {
+ ret = -errno;
+ fprintf(stderr,
+ "failed to read element number\n");
+ goto error_close_dir;
+ } else if (ret != 1) {
+ ret = -EIO;
+ fprintf(stderr,
+ "failed to match element number\n");
+ goto error_close_dir;
+ }
+
+ numstrlen = calc_digits(number);
/* verify the next character is not a colon */
if (strncmp(ent->d_name + strlen(type) + numstrlen,
- ":",
- 1) != 0) {
- filename = malloc(strlen(iio_dir)
- + strlen(type)
- + numstrlen
- + 6);
- if (filename == NULL) {
- closedir(dp);
- return -ENOMEM;
+ ":", 1) != 0) {
+ filename = malloc(strlen(iio_dir) + strlen(type)
+ + numstrlen + 6);
+ if (!filename) {
+ ret = -ENOMEM;
+ goto error_close_dir;
}
- sprintf(filename, "%s%s%d/name",
- iio_dir,
- type,
- number);
- nameFile = fopen(filename, "r");
- if (!nameFile) {
+
+ ret = sprintf(filename, "%s%s%d/name", iio_dir,
+ type, number);
+ if (ret < 0) {
+ free(filename);
+ goto error_close_dir;
+ }
+
+ namefp = fopen(filename, "r");
+ if (!namefp) {
free(filename);
continue;
}
+
free(filename);
- fscanf(nameFile, "%s", thisname);
- fclose(nameFile);
+ errno = 0;
+ if (fscanf(namefp, "%s", thisname) != 1) {
+ ret = errno ? -errno : -ENODATA;
+ goto error_close_dir;
+ }
+
+ if (fclose(namefp)) {
+ ret = -errno;
+ goto error_close_dir;
+ }
+
if (strcmp(name, thisname) == 0) {
- closedir(dp);
+ if (closedir(dp) == -1)
+ return -errno;
+
return number;
}
}
}
}
- closedir(dp);
+ if (closedir(dp) == -1)
+ return -errno;
+
return -ENODEV;
+
+error_close_dir:
+ if (closedir(dp) == -1)
+ perror("find_type_by_name(): Failed to close directory");
+
+ return ret;
}
-int _write_sysfs_int(char *filename, char *basedir, int val, int verify)
+static int _write_sysfs_int(const char *filename, const char *basedir, int val,
+ int verify)
{
int ret = 0;
FILE *sysfsfp;
int test;
char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
- if (temp == NULL)
+ if (!temp)
return -ENOMEM;
- sprintf(temp, "%s/%s", basedir, filename);
+
+ ret = sprintf(temp, "%s/%s", basedir, filename);
+ if (ret < 0)
+ goto error_free;
+
sysfsfp = fopen(temp, "w");
- if (sysfsfp == NULL) {
- printf("failed to open %s\n", temp);
+ if (!sysfsfp) {
ret = -errno;
+ fprintf(stderr, "failed to open %s\n", temp);
+ goto error_free;
+ }
+
+ ret = fprintf(sysfsfp, "%d", val);
+ if (ret < 0) {
+ if (fclose(sysfsfp))
+ perror("_write_sysfs_int(): Failed to close dir");
+
goto error_free;
}
- fprintf(sysfsfp, "%d", val);
- fclose(sysfsfp);
+
+ if (fclose(sysfsfp)) {
+ ret = -errno;
+ goto error_free;
+ }
+
if (verify) {
sysfsfp = fopen(temp, "r");
- if (sysfsfp == NULL) {
- printf("failed to open %s\n", temp);
+ if (!sysfsfp) {
+ ret = -errno;
+ fprintf(stderr, "failed to open %s\n", temp);
+ goto error_free;
+ }
+
+ if (fscanf(sysfsfp, "%d", &test) != 1) {
+ ret = errno ? -errno : -ENODATA;
+ if (fclose(sysfsfp))
+ perror("_write_sysfs_int(): Failed to close dir");
+
+ goto error_free;
+ }
+
+ if (fclose(sysfsfp)) {
ret = -errno;
goto error_free;
}
- fscanf(sysfsfp, "%d", &test);
- fclose(sysfsfp);
+
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;
}
}
+
error_free:
free(temp);
return ret;
}
-int write_sysfs_int(char *filename, char *basedir, int val)
+/**
+ * write_sysfs_int() - write an integer value to a sysfs file
+ * @filename: name of the file to write to
+ * @basedir: the sysfs directory in which the file is to be found
+ * @val: integer value to write to file
+ *
+ * Returns a value >= 0 on success, otherwise a negative error code.
+ **/
+int write_sysfs_int(const char *filename, const char *basedir, int val)
{
return _write_sysfs_int(filename, basedir, val, 0);
}
-int write_sysfs_int_and_verify(char *filename, char *basedir, int val)
+/**
+ * write_sysfs_int_and_verify() - write an integer value to a sysfs file
+ * and verify
+ * @filename: name of the file to write to
+ * @basedir: the sysfs directory in which the file is to be found
+ * @val: integer value to write to file
+ *
+ * Returns a value >= 0 on success, otherwise a negative error code.
+ **/
+int write_sysfs_int_and_verify(const char *filename, const char *basedir,
+ int val)
{
return _write_sysfs_int(filename, basedir, val, 1);
}
-int _write_sysfs_string(char *filename, char *basedir, char *val, int verify)
+static int _write_sysfs_string(const char *filename, const char *basedir,
+ const char *val, int verify)
{
int ret = 0;
FILE *sysfsfp;
char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
- if (temp == NULL) {
- printf("Memory allocation failed\n");
+ if (!temp) {
+ fprintf(stderr, "Memory allocation failed\n");
return -ENOMEM;
}
- sprintf(temp, "%s/%s", basedir, filename);
+
+ ret = sprintf(temp, "%s/%s", basedir, filename);
+ if (ret < 0)
+ goto error_free;
+
sysfsfp = fopen(temp, "w");
- if (sysfsfp == NULL) {
- printf("Could not open %s\n", temp);
+ if (!sysfsfp) {
+ ret = -errno;
+ fprintf(stderr, "Could not open %s\n", temp);
+ goto error_free;
+ }
+
+ ret = fprintf(sysfsfp, "%s", val);
+ if (ret < 0) {
+ if (fclose(sysfsfp))
+ perror("_write_sysfs_string(): Failed to close dir");
+
+ goto error_free;
+ }
+
+ if (fclose(sysfsfp)) {
ret = -errno;
goto error_free;
}
- fprintf(sysfsfp, "%s", val);
- fclose(sysfsfp);
+
if (verify) {
sysfsfp = fopen(temp, "r");
- if (sysfsfp == NULL) {
- printf("could not open file to verify\n");
+ if (!sysfsfp) {
ret = -errno;
+ fprintf(stderr, "Could not open file to verify\n");
goto error_free;
}
- fscanf(sysfsfp, "%s", temp);
- fclose(sysfsfp);
+
+ if (fscanf(sysfsfp, "%s", temp) != 1) {
+ ret = errno ? -errno : -ENODATA;
+ if (fclose(sysfsfp))
+ perror("_write_sysfs_string(): Failed to close dir");
+
+ goto error_free;
+ }
+
+ if (fclose(sysfsfp)) {
+ ret = -errno;
+ goto error_free;
+ }
+
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;
}
}
+
error_free:
free(temp);
@@ -586,84 +826,166 @@ error_free:
* @filename: name of file to write to
* @basedir: the sysfs directory in which the file is to be found
* @val: the string to write
+ *
+ * Returns a value >= 0 on success, otherwise a negative error code.
**/
-int write_sysfs_string_and_verify(char *filename, char *basedir, char *val)
+int write_sysfs_string_and_verify(const char *filename, const char *basedir,
+ const char *val)
{
return _write_sysfs_string(filename, basedir, val, 1);
}
-int write_sysfs_string(char *filename, char *basedir, char *val)
+/**
+ * write_sysfs_string() - write string to a sysfs file
+ * @filename: name of file to write to
+ * @basedir: the sysfs directory in which the file is to be found
+ * @val: the string to write
+ *
+ * Returns a value >= 0 on success, otherwise a negative error code.
+ **/
+int write_sysfs_string(const char *filename, const char *basedir,
+ const char *val)
{
return _write_sysfs_string(filename, basedir, val, 0);
}
-int read_sysfs_posint(char *filename, char *basedir)
+/**
+ * read_sysfs_posint() - read an integer value from file
+ * @filename: name of file to read from
+ * @basedir: the sysfs directory in which the file is to be found
+ *
+ * Returns the read integer value >= 0 on success, otherwise a negative error
+ * code.
+ **/
+int read_sysfs_posint(const char *filename, const char *basedir)
{
int ret;
FILE *sysfsfp;
char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
- if (temp == NULL) {
- printf("Memory allocation failed");
+ if (!temp) {
+ fprintf(stderr, "Memory allocation failed");
return -ENOMEM;
}
- sprintf(temp, "%s/%s", basedir, filename);
+
+ ret = sprintf(temp, "%s/%s", basedir, filename);
+ if (ret < 0)
+ goto error_free;
+
sysfsfp = fopen(temp, "r");
- if (sysfsfp == NULL) {
+ if (!sysfsfp) {
ret = -errno;
goto error_free;
}
- fscanf(sysfsfp, "%d\n", &ret);
- fclose(sysfsfp);
+
+ errno = 0;
+ if (fscanf(sysfsfp, "%d\n", &ret) != 1) {
+ ret = errno ? -errno : -ENODATA;
+ if (fclose(sysfsfp))
+ perror("read_sysfs_posint(): Failed to close dir");
+
+ goto error_free;
+ }
+
+ if (fclose(sysfsfp))
+ ret = -errno;
+
error_free:
free(temp);
+
return ret;
}
-int read_sysfs_float(char *filename, char *basedir, float *val)
+/**
+ * read_sysfs_float() - read a float value from file
+ * @filename: name of file to read from
+ * @basedir: the sysfs directory in which the file is to be found
+ * @val: output the read float value
+ *
+ * Returns a value >= 0 on success, otherwise a negative error code.
+ **/
+int read_sysfs_float(const char *filename, const char *basedir, float *val)
{
int ret = 0;
FILE *sysfsfp;
char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
- if (temp == NULL) {
- printf("Memory allocation failed");
+ if (!temp) {
+ fprintf(stderr, "Memory allocation failed");
return -ENOMEM;
}
- sprintf(temp, "%s/%s", basedir, filename);
+
+ ret = sprintf(temp, "%s/%s", basedir, filename);
+ if (ret < 0)
+ goto error_free;
+
sysfsfp = fopen(temp, "r");
- if (sysfsfp == NULL) {
+ if (!sysfsfp) {
ret = -errno;
goto error_free;
}
- fscanf(sysfsfp, "%f\n", val);
- fclose(sysfsfp);
+
+ errno = 0;
+ if (fscanf(sysfsfp, "%f\n", val) != 1) {
+ ret = errno ? -errno : -ENODATA;
+ if (fclose(sysfsfp))
+ perror("read_sysfs_float(): Failed to close dir");
+
+ goto error_free;
+ }
+
+ if (fclose(sysfsfp))
+ ret = -errno;
+
error_free:
free(temp);
+
return ret;
}
+/**
+ * read_sysfs_string() - read a string from file
+ * @filename: name of file to read from
+ * @basedir: the sysfs directory in which the file is to be found
+ * @str: output the read string
+ *
+ * Returns a value >= 0 on success, otherwise a negative error code.
+ **/
int read_sysfs_string(const char *filename, const char *basedir, char *str)
{
int ret = 0;
FILE *sysfsfp;
char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
- if (temp == NULL) {
- printf("Memory allocation failed");
+ if (!temp) {
+ fprintf(stderr, "Memory allocation failed");
return -ENOMEM;
}
- sprintf(temp, "%s/%s", basedir, filename);
+
+ ret = sprintf(temp, "%s/%s", basedir, filename);
+ if (ret < 0)
+ goto error_free;
+
sysfsfp = fopen(temp, "r");
- if (sysfsfp == NULL) {
+ if (!sysfsfp) {
ret = -errno;
goto error_free;
}
- fscanf(sysfsfp, "%s\n", str);
- fclose(sysfsfp);
+
+ errno = 0;
+ if (fscanf(sysfsfp, "%s\n", str) != 1) {
+ ret = errno ? -errno : -ENODATA;
+ if (fclose(sysfsfp))
+ perror("read_sysfs_string(): Failed to close dir");
+
+ goto error_free;
+ }
+
+ if (fclose(sysfsfp))
+ ret = -errno;
+
error_free:
free(temp);
+
return ret;
}
-
-#endif /* _IIO_UTILS_H */
diff --git a/kernel/tools/iio/iio_utils.h b/kernel/tools/iio/iio_utils.h
index 1bc837b2d..e3503bfe5 100644
--- a/kernel/tools/iio/iio_utils.h
+++ b/kernel/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;
/**
@@ -28,9 +30,12 @@ extern const char *iio_dir;
* @offset: offset to be applied for conversion to si units
* @index: the channel index in the buffer output
* @bytes: number of bytes occupied in buffer output
+ * @bits_used: number of valid bits of data
+ * @shift: amount of bits to shift right data before applying bit mask
* @mask: a bit mask for the raw output
+ * @be: flag if data is big endian
* @is_signed: is the raw value stored signed
- * @enabled: is this channel enabled
+ * @location: data offset for this channel inside the buffer (in bytes)
**/
struct iio_channel_info {
char *name;
@@ -48,24 +53,26 @@ struct iio_channel_info {
};
int iioutils_break_up_name(const char *full_name, char **generic_name);
-int iioutils_get_type(unsigned *is_signed, unsigned *bytes,
- unsigned *bits_used, unsigned *shift,
- uint64_t *mask, unsigned *be,
- const char *device_dir, const char *name,
- const char *generic_name);
+int iioutils_get_type(unsigned *is_signed, unsigned *bytes, unsigned *bits_used,
+ unsigned *shift, uint64_t *mask, unsigned *be,
+ const char *device_dir, const char *name,
+ const char *generic_name);
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);
+ 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);
int build_channel_array(const char *device_dir,
- struct iio_channel_info **ci_array, int *counter);
+ struct iio_channel_info **ci_array, int *counter);
int find_type_by_name(const char *name, const char *type);
-int write_sysfs_int(char *filename, char *basedir, int val);
-int write_sysfs_int_and_verify(char *filename, char *basedir, int val);
-int write_sysfs_string_and_verify(char *filename, char *basedir, char *val);
-int write_sysfs_string(char *filename, char *basedir, char *val);
-int read_sysfs_posint(char *filename, char *basedir);
-int read_sysfs_float(char *filename, char *basedir, float *val);
+int write_sysfs_int(const char *filename, const char *basedir, int val);
+int write_sysfs_int_and_verify(const char *filename, const char *basedir,
+ int val);
+int write_sysfs_string_and_verify(const char *filename, const char *basedir,
+ const char *val);
+int write_sysfs_string(const char *filename, const char *basedir,
+ const char *val);
+int read_sysfs_posint(const char *filename, const char *basedir);
+int read_sysfs_float(const char *filename, const char *basedir, float *val);
int read_sysfs_string(const char *filename, const char *basedir, char *str);
#endif /* _IIO_UTILS_H_ */
diff --git a/kernel/tools/iio/lsiio.c b/kernel/tools/iio/lsiio.c
index c585440f8..3d650e668 100644
--- a/kernel/tools/iio/lsiio.c
+++ b/kernel/tools/iio/lsiio.c
@@ -20,7 +20,6 @@
#include <sys/dir.h>
#include "iio_utils.h"
-
static enum verbosity {
VERBLEVEL_DEFAULT, /* 0 gives lspci behaviour */
VERBLEVEL_SENSORS, /* 1 lists sensors */
@@ -29,17 +28,16 @@ static enum verbosity {
const char *type_device = "iio:device";
const char *type_trigger = "trigger";
-
static inline int check_prefix(const char *str, const char *prefix)
{
return strlen(str) > strlen(prefix) &&
- strncmp(str, prefix, strlen(prefix)) == 0;
+ strncmp(str, prefix, strlen(prefix)) == 0;
}
static inline int check_postfix(const char *str, const char *postfix)
{
return strlen(str) > strlen(postfix) &&
- strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
+ strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
}
static int dump_channels(const char *dev_dir_name)
@@ -48,32 +46,37 @@ 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")) {
+ check_postfix(ent->d_name, "_raw"))
printf(" %-10s\n", ent->d_name);
- }
- return 0;
+ return (closedir(dp) == -1) ? -errno : 0;
}
static int dump_one_device(const char *dev_dir_name)
{
char name[IIO_MAX_NAME_LENGTH];
int dev_idx;
- int retval;
+ int ret;
- retval = sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_device),
- "%i", &dev_idx);
- if (retval != 1)
+ ret = sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_device), "%i",
+ &dev_idx);
+ if (ret != 1)
return -EINVAL;
- read_sysfs_string("name", dev_dir_name, name);
+
+ ret = read_sysfs_string("name", dev_dir_name, name);
+ if (ret < 0)
+ return ret;
+
printf("Device %03d: %s\n", dev_idx, name);
if (verblevel >= VERBLEVEL_SENSORS)
return dump_channels(dev_dir_name);
+
return 0;
}
@@ -81,57 +84,90 @@ static int dump_one_trigger(const char *dev_dir_name)
{
char name[IIO_MAX_NAME_LENGTH];
int dev_idx;
- int retval;
+ int ret;
- retval = sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_trigger),
- "%i", &dev_idx);
- if (retval != 1)
+ ret = sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_trigger),
+ "%i", &dev_idx);
+ if (ret != 1)
return -EINVAL;
- read_sysfs_string("name", dev_dir_name, name);
+
+ ret = read_sysfs_string("name", dev_dir_name, name);
+ if (ret < 0)
+ return ret;
+
printf("Trigger %03d: %s\n", dev_idx, name);
+
return 0;
}
-static void dump_devices(void)
+static int dump_devices(void)
{
const struct dirent *ent;
+ int ret;
DIR *dp;
dp = opendir(iio_dir);
- if (dp == NULL) {
- printf("No industrial I/O devices available\n");
- return;
+ if (!dp) {
+ fprintf(stderr, "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;
- asprintf(&dev_dir_name, "%s%s", iio_dir, ent->d_name);
- dump_one_device(dev_dir_name);
+ if (asprintf(&dev_dir_name, "%s%s", iio_dir,
+ ent->d_name) < 0) {
+ ret = -ENOMEM;
+ goto error_close_dir;
+ }
+
+ ret = dump_one_device(dev_dir_name);
+ if (ret) {
+ free(dev_dir_name);
+ goto error_close_dir;
+ }
+
free(dev_dir_name);
if (verblevel >= VERBLEVEL_SENSORS)
printf("\n");
}
}
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;
- asprintf(&dev_dir_name, "%s%s", iio_dir, ent->d_name);
- dump_one_trigger(dev_dir_name);
+ if (asprintf(&dev_dir_name, "%s%s", iio_dir,
+ ent->d_name) < 0) {
+ ret = -ENOMEM;
+ goto error_close_dir;
+ }
+
+ ret = dump_one_trigger(dev_dir_name);
+ if (ret) {
+ free(dev_dir_name);
+ goto error_close_dir;
+ }
+
free(dev_dir_name);
}
}
- closedir(dp);
+
+ return (closedir(dp) == -1) ? -errno : 0;
+
+error_close_dir:
+ if (closedir(dp) == -1)
+ perror("dump_devices(): Failed to close directory");
+
+ return ret;
}
int main(int argc, char **argv)
{
int c, err = 0;
- while ((c = getopt(argc, argv, "d:D:v")) != EOF) {
+ while ((c = getopt(argc, argv, "v")) != EOF) {
switch (c) {
case 'v':
verblevel++;
@@ -146,13 +182,9 @@ int main(int argc, char **argv)
if (err || argc > optind) {
fprintf(stderr, "Usage: lsiio [options]...\n"
"List industrial I/O devices\n"
- " -v, --verbose\n"
- " Increase verbosity (may be given multiple times)\n"
- );
+ " -v Increase verbosity (may be given multiple times)\n");
exit(1);
}
- dump_devices();
-
- return 0;
+ return dump_devices();
}