Skip to content

Commit

Permalink
tests: check return values on all calls
Browse files Browse the repository at this point in the history
This makes the code check all the missing return values that
were skipped in the past, to try to report as many errors as
possible.

Signed-off-by: Robin Getz <[email protected]>
  • Loading branch information
rgetz committed Feb 21, 2020
1 parent d4b4072 commit 9929ca0
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 24 deletions.
14 changes: 11 additions & 3 deletions tests/iio_adi_xflow_check.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ static void *monitor_thread_fn(void *data)
sleep(1);

/* Clear all status bits */
iio_device_reg_write(dev, 0x80000088, 0x6);
ret = iio_device_reg_write(dev, 0x80000088, 0x6);
if (ret) {
fprintf(stderr, "Failed to clearn DMA status register: %s\n",
strerror(-ret));
}

while (app_running) {
ret = iio_device_reg_read(dev, 0x80000088, &val);
Expand All @@ -145,8 +149,12 @@ static void *monitor_thread_fn(void *data)
}

/* Clear bits */
if (val)
iio_device_reg_write(dev, 0x80000088, val);
if (val) {
ret = iio_device_reg_write(dev, 0x80000088, val);
if (ret)
fprintf(stderr, "Failed to clearn DMA status register: %s\n",
strerror(-ret));
}
sleep(1);
}

Expand Down
17 changes: 13 additions & 4 deletions tests/iio_attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ int main(int argc, char **argv)
scan_only = false, quiet = false, gen_code = false;
unsigned int i;
char *wbuf = NULL;
ssize_t ret;

while ((c = getopt_long(argc, argv, "+hau:g:CdcBDiosIq",
options, &option_index)) != -1) {
Expand Down Expand Up @@ -651,10 +652,18 @@ int main(int argc, char **argv)
for (i = 0; i < nb_ctx_attrs; i++) {
const char *key, *value;

iio_context_get_attr(ctx, i, &key, &value);
if (!attr_index || str_match(key, argv[attr_index], ignore_case)) {
printf("%s: %s\n", key, value);
gen_context_attr(key);
ret = iio_context_get_attr(ctx, i, &key, &value);
if (!ret) {
if (!attr_index || str_match(key, argv[attr_index], ignore_case)) {
printf("%s: %s\n", key, value);
gen_context_attr(key);
}
} else {
char *buf = xmalloc(BUF_SIZE);
iio_strerror(errno, buf, BUF_SIZE);
fprintf(stderr, "Unable to get context attributes: %s (%zd)\n",
buf, ret);
free(buf);
}
}
}
Expand Down
27 changes: 22 additions & 5 deletions tests/iio_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ static void scan(void)

ret = iio_scan_context_get_info_list(ctx, &info);
if (ret < 0) {
fprintf(stderr, "Unable to scan: %li\n", (long) ret);
char err_str[1024];
iio_strerror(-ret, err_str, sizeof(err_str));
fprintf(stderr, "Unable to scan: %s (%zd)\n", err_str, ret);
goto err_free_ctx;
}

Expand Down Expand Up @@ -295,8 +297,11 @@ int main(int argc, char **argv)
if (!ret)
printf("Backend version: %u.%u (git tag: %s)\n",
major, minor, git_tag);
else
fprintf(stderr, "Unable to get backend version: %i\n", ret);
else {
char err_str[1024];
iio_strerror(-ret, err_str, sizeof(err_str));
fprintf(stderr, "Unable to get backend version: %s (%i)\n", err_str, ret);
}

printf("Backend description string: %s\n",
iio_context_get_description(ctx));
Expand All @@ -308,8 +313,15 @@ int main(int argc, char **argv)
for (i = 0; i < nb_ctx_attrs; i++) {
const char *key, *value;

iio_context_get_attr(ctx, i, &key, &value);
printf("\t%s: %s\n", key, value);
ret = iio_context_get_attr(ctx, i, &key, &value);
if (ret == 0)
printf("\t%s: %s\n", key, value);
else {
char err_str[1024];
iio_strerror(-ret, err_str, sizeof(err_str));
fprintf(stderr, "\tUnable to read IIO context attributes: %s (%i)\n",
err_str, ret);
}
}

unsigned int nb_devices = iio_context_get_devices_count(ctx);
Expand Down Expand Up @@ -467,6 +479,11 @@ int main(int argc, char **argv)
iio_device_get_id(trig),
name ? name : "");
}
} else if (ret == -ENOENT) {
printf("\t\tNo trigger on this device\n");
} else if (ret < 0) {
iio_strerror(-ret, buf, BUF_SIZE);
printf("ERROR: checking for trigger : %s (%i)\n", buf, ret);
}
}

Expand Down
39 changes: 33 additions & 6 deletions tests/iio_readdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ int main(int argc, char **argv)
size_t sample_size;
int timeout = -1;
bool scan_for_context = false;
ssize_t ret;

while ((c = getopt_long(argc, argv, "+hn:u:t:b:s:T:a",
options, &option_index)) != -1) {
Expand Down Expand Up @@ -316,8 +317,15 @@ int main(int argc, char **argv)
return EXIT_FAILURE;
}

if (timeout >= 0)
iio_context_set_timeout(ctx, timeout);
if (timeout >= 0) {
ret = iio_context_set_timeout(ctx, timeout);
if (ret < 0) {
char err_str[1024];
iio_strerror(-ret, err_str, sizeof(err_str));
fprintf(stderr, "IIO contexts set timeout failed : %s (%zd)\n",
err_str, ret);
}
}

dev = iio_context_find_device(ctx, argv[optind]);
if (!dev) {
Expand Down Expand Up @@ -346,11 +354,24 @@ int main(int argc, char **argv)
* fail gracefully to remain compatible.
*/
if (iio_device_attr_write_longlong(trigger,
"sampling_frequency", DEFAULT_FREQ_HZ) < 0)
iio_device_attr_write_longlong(trigger,
"sampling_frequency", DEFAULT_FREQ_HZ) < 0) {
ret = iio_device_attr_write_longlong(trigger,
"frequency", DEFAULT_FREQ_HZ);
if (ret < 0) {
char buf[256];
iio_strerror(-ret, buf, sizeof(buf));
fprintf(stderr, "sample rate not set : %s (%zd)\n",
buf, ret);
}
}

iio_device_set_trigger(dev, trigger);
ret = iio_device_set_trigger(dev, trigger);
if (ret < 0) {
char buf[256];
iio_strerror(-ret, buf, sizeof(buf));
fprintf(stderr, "set triffer failed : %s (%zd)\n",
buf, ret);
}
}

nb_channels = iio_device_get_channels_count(dev);
Expand Down Expand Up @@ -427,7 +448,13 @@ int main(int argc, char **argv)
quit_all(EXIT_SUCCESS);
}
} else {
iio_buffer_foreach_sample(buffer, print_sample, NULL);
ret = iio_buffer_foreach_sample(buffer, print_sample, NULL);
if (ret < 0) {
char buf[256];
iio_strerror(-ret, buf, sizeof(buf));
fprintf(stderr, "buffer processing failed : %s (%d)\n",
buf, ret);
}
}
}

Expand Down
39 changes: 33 additions & 6 deletions tests/iio_writedev.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ int main(int argc, char **argv)
int timeout = -1;
bool scan_for_context = false;
bool cyclic_buffer = false;
ssize_t ret;

while ((c = getopt_long(argc, argv, "+hn:u:t:b:s:T:ac",
options, &option_index)) != -1) {
Expand Down Expand Up @@ -329,8 +330,15 @@ int main(int argc, char **argv)
return EXIT_FAILURE;
}

if (timeout >= 0)
iio_context_set_timeout(ctx, timeout);
if (timeout >= 0) {
ret = iio_context_set_timeout(ctx, timeout);
if (ret < 0) {
char err_str[1024];
iio_strerror(-ret, err_str, sizeof(err_str));
fprintf(stderr, "IIO contexts set timeout failed : %s (%zd)\n",
err_str, ret);
}
}

dev = iio_context_find_device(ctx, argv[optind]);
if (!dev) {
Expand Down Expand Up @@ -359,11 +367,24 @@ int main(int argc, char **argv)
* fail gracefully to remain compatible.
*/
if (iio_device_attr_write_longlong(trigger,
"sampling_frequency", DEFAULT_FREQ_HZ) < 0)
iio_device_attr_write_longlong(trigger,
"sampling_frequency", DEFAULT_FREQ_HZ) < 0) {
ret = iio_device_attr_write_longlong(trigger,
"frequency", DEFAULT_FREQ_HZ);
if (ret < 0) {
char buf[256];
iio_strerror(-ret, buf, sizeof(buf));
fprintf(stderr, "sample rate not set : %s (%zd)\n",
buf, ret);
}
}

iio_device_set_trigger(dev, trigger);
ret = iio_device_set_trigger(dev, trigger);
if (ret < 0) {
char buf[256];
iio_strerror(-ret, buf, sizeof(buf));
fprintf(stderr, "set triffer failed : %s (%zd)\n",
buf, ret);
}
}

nb_channels = iio_device_get_channels_count(dev);
Expand Down Expand Up @@ -426,7 +447,13 @@ int main(int argc, char **argv)
quit_all(EXIT_SUCCESS);
}
} else {
iio_buffer_foreach_sample(buffer, read_sample, NULL);
ret = iio_buffer_foreach_sample(buffer, read_sample, NULL);
if (ret < 0) {
char buf[256];
iio_strerror(-ret, buf, sizeof(buf));
fprintf(stderr, "buffer processing failed : %s (%zd)\n",
buf, ret);
}
}

int ret = iio_buffer_push(buffer);
Expand Down

0 comments on commit 9929ca0

Please sign in to comment.