Skip to content

Commit

Permalink
examples: ensure all examples compile with new return checking option
Browse files Browse the repository at this point in the history
This sets the-DIIO_CHECK_RET flag in the Makefile, and fixes the
issues that were pointed out.

Signed-off-by: Robin Getz <[email protected]>
  • Loading branch information
rgetz committed Mar 13, 2020
1 parent 3037476 commit 34f3a99
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
2 changes: 1 addition & 1 deletion examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

TARGETS := ad9361-iiostream ad9371-iiostream adrv9009-iiostream dummy-iiostream iio-monitor

CFLAGS = -Wall
CFLAGS = -Wall -DIIO_CHECK_RET

UNAME_S := $(shell uname -s)

Expand Down
23 changes: 19 additions & 4 deletions examples/dummy-iiostream.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,22 @@ static bool has_repeat;
/* cleanup and exit */
static void shutdown()
{
int ret;

if (channels) { free(channels); }

printf("* Destroying buffers\n");
if (rxbuf) { iio_buffer_destroy(rxbuf); }

printf("* Disassociate trigger\n");
if (dev) { iio_device_set_trigger(dev, NULL); }
if (dev) {
ret = iio_device_set_trigger(dev, NULL);
if (ret < 0) {
char buf[256];
iio_strerror(-ret, buf, sizeof(buf));
fprintf(stderr, "%s (%d) while Disassociate trigger\n", buf, ret);
}
}

printf("* Destroying context\n");
if (ctx) { iio_context_destroy(ctx); }
Expand Down Expand Up @@ -333,11 +342,17 @@ int main (int argc, char **argv)
printf("\n");
break;

case SAMPLE_CALLBACK:
iio_buffer_foreach_sample(rxbuf, sample_cb, NULL);
case SAMPLE_CALLBACK: {
int ret;
ret = iio_buffer_foreach_sample(rxbuf, sample_cb, NULL);
if (ret < 0) {
char buf[256];
iio_strerror(-ret, buf, sizeof(buf));
fprintf(stderr, "%s (%d) while processing buffer\n", buf, ret);
}
printf("\n");
break;

}
case CHANNEL_READ_RAW:
case CHANNEL_READ:
for (int i = 0; i < channel_count; ++i) {
Expand Down
38 changes: 30 additions & 8 deletions examples/iio-monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,30 +63,52 @@ static bool is_valid_channel(struct iio_channel *chn)
channel_has_attr(chn, "input"));
}

static void err_str(int ret)
{
char buf[256];
iio_strerror(-ret, buf, sizeof(buf));
fprintf(stderr, "Error during read: %s (%d)\n", buf, ret);
}

static double get_channel_value(struct iio_channel *chn)
{
char *old_locale;
char buf[1024];
double val;
int ret;

old_locale = strdup(setlocale(LC_NUMERIC, NULL));
setlocale(LC_NUMERIC, "C");

if (channel_has_attr(chn, "input")) {
iio_channel_attr_read(chn, "input", buf, sizeof(buf));
val = strtod(buf, NULL);
ret = iio_channel_attr_read(chn, "input", buf, sizeof(buf));
if (ret < 0) {
err_str(ret);
val = 0;
} else
val = strtod(buf, NULL);
} else {
iio_channel_attr_read(chn, "raw", buf, sizeof(buf));
val = strtod(buf, NULL);
ret = iio_channel_attr_read(chn, "raw", buf, sizeof(buf));
if (ret < 0) {
err_str(ret);
val = 0;
} else
val = strtod(buf, NULL);

if (channel_has_attr(chn, "offset")) {
iio_channel_attr_read(chn, "offset", buf, sizeof(buf));
val += strtod(buf, NULL);
ret = iio_channel_attr_read(chn, "offset", buf, sizeof(buf));
if (ret < 0)
err_str(ret);
else
val += strtod(buf, NULL);
}

if (channel_has_attr(chn, "scale")) {
iio_channel_attr_read(chn, "scale", buf, sizeof(buf));
val *= strtod(buf, NULL);
ret = iio_channel_attr_read(chn, "scale", buf, sizeof(buf));
if (ret < 0)
err_str(ret);
else
val *= strtod(buf, NULL);
}
}

Expand Down

0 comments on commit 34f3a99

Please sign in to comment.