Skip to content

Commit

Permalink
read|write_dev : qualify channel names and err if bad name
Browse files Browse the repository at this point in the history
in the past, you could put giberish channel names, and read|write_dev
would accept them (ignoring them). This sometimes lead to typos causing
issues with "wrong" or "incorrect" data.

This resolves that by checking for a match, and erroring if there is
something provided that doesn't match.

Signed-off-by: Robin Getz <[email protected]>
  • Loading branch information
rgetz authored and pcercuei committed Mar 3, 2022
1 parent 590d1a2 commit e2ce2d3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 20 deletions.
15 changes: 15 additions & 0 deletions tests/iio_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 126,21 @@ struct iio_context * autodetect_context(bool rtn, const char * name, const char
return ctx;
}

int iio_device_enable_channel(const struct iio_device *dev, const char * channel, bool type)
{
struct iio_channel *ch;

ch = iio_device_find_channel(dev, channel, type);
if (!ch)
return -ENXIO;

if (iio_channel_is_enabled(ch))
return -EBUSY;

iio_channel_enable(ch);
return 0;
}

unsigned long int sanitize_clamp(const char *name, const char *argv,
uint64_t min, uint64_t max)
{
Expand Down
1 change: 1 addition & 0 deletions tests/iio_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 47,7 @@ char *cmn_strndup(const char *str, size_t n);
struct iio_context * autodetect_context(bool rtn, const char *name, const char *scan);
unsigned long int sanitize_clamp(const char *name, const char *argv,
uint64_t min, uint64_t max);
int iio_device_enable_channel(const struct iio_device *dev, const char * channel, bool type);

/* optstring is a string containing the legitimate option characters.
* If such a character is followed by a colon, the option requires an argument.
Expand Down
19 changes: 9 additions & 10 deletions tests/iio_readdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -373,17 373,16 @@ int main(int argc, char **argv)
}
}
} else {
for (i = 0; i < nb_channels; i ) {
struct iio_channel *ch = iio_device_get_channel(dev, i);
for (j = optind 1; j < (unsigned int) argc; j ) {
const char *n = iio_channel_get_name(ch);
if ((!strcmp(argw[j], iio_channel_get_id(ch)) ||
(n && !strcmp(n, argw[j]))) &&
!iio_channel_is_output(ch)) {
iio_channel_enable(ch);
nb_active_channels ;
}
for (j = optind 1; j < (unsigned int) argc; j ) {
ret = iio_device_enable_channel(dev, argw[j], false);
if (ret < 0) {
char buf[256];
iio_strerror(-(int) ret, buf, sizeof(buf));
fprintf(stderr, "Bad channel name \"%s\" : %s\n", argw[j], buf);
iio_context_destroy(ctx);
return EXIT_FAILURE;
}
nb_active_channels ;
}
}

Expand Down
19 changes: 9 additions & 10 deletions tests/iio_writedev.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,17 392,16 @@ int main(int argc, char **argv)
}
}
} else {
for (i = 0; i < nb_channels; i ) {
struct iio_channel *ch = iio_device_get_channel(dev, i);
for (j = optind 1; j < (unsigned int) argc; j ) {
const char *n = iio_channel_get_name(ch);
if ((!strcmp(argw[j], iio_channel_get_id(ch)) ||
(n && !strcmp(n, argw[j]))) &&
iio_channel_is_output(ch)) {
iio_channel_enable(ch);
nb_active_channels ;
}
for (j = optind 1; j < (unsigned int) argc; j ) {
ret = iio_device_enable_channel(dev, argw[j], true);
if (ret < 0) {
char buf[256];
iio_strerror(-(int) ret, buf, sizeof(buf));
fprintf(stderr, "Bad channel name \"%s\" : %s\n", argw[j], buf);
iio_context_destroy(ctx);
return EXIT_FAILURE;
}
nb_active_channels ;
}
}

Expand Down

0 comments on commit e2ce2d3

Please sign in to comment.