Skip to content

Commit

Permalink
tests: put common/replicated functions into a single file
Browse files Browse the repository at this point in the history
The scan function was copied/pasted in each utility, this moves
it to a a seperate c file, which is built as a library, which then
every utility statically links to, saving maintainance, and build time.

Signed-off-by: Robin Getz <[email protected]>
  • Loading branch information
rgetz committed Apr 17, 2020
1 parent 2d7fad4 commit 329352a
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 392 deletions.
14 changes: 8 additions & 6 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,22 @@ endif()
if (CMAKE_COMPILER_IS_GNUCC)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DIIO_CHECK_RET")
endif()
add_library(iio_tests_helper STATIC iio_common.c gen_code.c)
target_link_libraries(iio_tests_helper iio)

add_executable(iio_genxml iio_genxml.c ${GETOPT_C_FILE} ${LIBIIO_RC})
add_executable(iio_info iio_info.c ${GETOPT_C_FILE} ${LIBIIO_RC})
add_executable(iio_attr iio_attr.c gen_code.c ${GETOPT_C_FILE} ${LIBIIO_RC})
add_executable(iio_attr iio_attr.c ${GETOPT_C_FILE} ${LIBIIO_RC})
add_executable(iio_readdev iio_readdev.c ${GETOPT_C_FILE} ${LIBIIO_RC})
add_executable(iio_reg iio_reg.c ${GETOPT_C_FILE} ${LIBIIO_RC})
add_executable(iio_writedev iio_writedev.c ${GETOPT_C_FILE} ${LIBIIO_RC})

target_link_libraries(iio_genxml iio)
target_link_libraries(iio_info iio)
target_link_libraries(iio_attr iio)
target_link_libraries(iio_readdev iio)
target_link_libraries(iio_info iio iio_tests_helper)
target_link_libraries(iio_attr iio iio_tests_helper)
target_link_libraries(iio_readdev iio iio_tests_helper)
target_link_libraries(iio_reg iio)
target_link_libraries(iio_writedev iio)
target_link_libraries(iio_writedev iio iio_tests_helper)

set(IIO_TESTS_TARGETS iio_genxml iio_info iio_attr iio_readdev iio_reg iio_writedev)

Expand All @@ -48,7 +50,7 @@ if(PTHREAD_LIBRARIES)
project(iio_stresstest C)
add_executable(iio_adi_xflow_check iio_adi_xflow_check.c)
add_executable(iio_stresstest iio_stresstest.c)
target_link_libraries(iio_adi_xflow_check iio ${PTHREAD_LIBRARIES})
target_link_libraries(iio_adi_xflow_check iio iio_tests_helper ${PTHREAD_LIBRARIES})
target_link_libraries(iio_stresstest iio ${PTHREAD_LIBRARIES})
set(IIO_TESTS_TARGETS ${IIO_TESTS_TARGETS} iio_adi_xflow_check iio_stresstest)

Expand Down
54 changes: 6 additions & 48 deletions tests/iio_adi_xflow_check.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
#include <string.h>
#include <unistd.h>

#include "iio_common.h"


#define MY_NAME "iio_adi_xflow_check"

struct xflow_pthread_data {
struct iio_context *ctx;
const char *device_name;
Expand Down Expand Up @@ -161,53 +166,6 @@ static void *monitor_thread_fn(void *data)
return (void *)0;
}

static struct iio_context *scan(void)
{
struct iio_scan_context *scan_ctx;
struct iio_context_info **info;
struct iio_context *ctx = NULL;
unsigned int i;
ssize_t ret;

scan_ctx = iio_create_scan_context(NULL, 0);
if (!scan_ctx) {
fprintf(stderr, "Unable to create scan context\n");
return NULL;
}

ret = iio_scan_context_get_info_list(scan_ctx, &info);
if (ret < 0) {
char err_str[1024];
iio_strerror(-ret, err_str, sizeof(err_str));
fprintf(stderr, "Scanning for IIO contexts failed: %s\n", err_str);
goto err_free_ctx;
}

if (ret == 0) {
printf("No IIO context found.\n");
goto err_free_info_list;
}

if (ret == 1) {
ctx = iio_create_context_from_uri(iio_context_info_get_uri(info[0]));
} else {
fprintf(stderr, "Multiple contexts found. Please select one using --uri:\n");

for (i = 0; i < (size_t) ret; i++) {
fprintf(stderr, "\t%u: %s [%s]\n", i,
iio_context_info_get_description(info[i]),
iio_context_info_get_uri(info[i]));
}
}

err_free_info_list:
iio_context_info_list_free(info);
err_free_ctx:
iio_scan_context_destroy(scan_ctx);

return ctx;
}

int main(int argc, char **argv)
{
unsigned int buffer_size = 1024 * 1024;
Expand Down Expand Up @@ -272,7 +230,7 @@ int main(int argc, char **argv)


if (scan_for_context)
ctx = scan();
ctx = autodetect_context(true, NULL, MY_NAME);
else if (arg_uri)
ctx = iio_create_context_from_uri(arg_uri);
else if (arg_ip)
Expand Down
98 changes: 11 additions & 87 deletions tests/iio_attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <ctype.h>
#include <sys/types.h>
#include "gen_code.h"
#include "iio_common.h"

#define MY_NAME "iio_attr"

Expand All @@ -38,32 +39,6 @@
#define _strdup strdup
#endif

enum backend {
LOCAL,
XML,
AUTO
};

/*
* internal buffers need to be big enough for attributes
* coming back from the kernel. Because of virtual memory,
* only the amount of ram that is needed is used.
*/
#define BUF_SIZE 16384

static void * xmalloc(size_t n)
{
void *p = malloc(n);

if (!p && n != 0) {
fprintf(stderr, MY_NAME
" fatal error: allocating %zu bytes failed\n",n);
exit(EXIT_FAILURE);
}

return p;
}

static bool str_match(const char * haystack, char * needle, bool ignore)
{
bool ret = false;
Expand Down Expand Up @@ -120,62 +95,11 @@ static bool str_match(const char * haystack, char * needle, bool ignore)
return ret;
}

static struct iio_context * autodetect_context(bool gen_code)
{
struct iio_scan_context *scan_ctx;
struct iio_context_info **info;
struct iio_context *ctx = NULL;
unsigned int i;
ssize_t ret;

scan_ctx = iio_create_scan_context(NULL, 0);
if (!scan_ctx) {
fprintf(stderr, "Unable to create scan context\n");
return NULL;
}

ret = iio_scan_context_get_info_list(scan_ctx, &info);
if (ret < 0) {
char *err_str = xmalloc(BUF_SIZE);
iio_strerror(-ret, err_str, BUF_SIZE);
fprintf(stderr, "Scanning for IIO contexts failed: %s\n", err_str);
free (err_str);
goto err_free_ctx;
}

if (ret == 0) {
printf("No IIO context found.\n");
goto err_free_info_list;
}
if (ret == 1) {
printf("Using auto-detected IIO context at URI \"%s\"\n",
iio_context_info_get_uri(info[0]));
ctx = iio_create_context_from_uri(iio_context_info_get_uri(info[0]));
if (gen_code)
gen_context(iio_context_info_get_uri(info[0]));
} else {
fprintf(stderr, "Multiple contexts found. Please select one using --uri:\n");
for (i = 0; i < (size_t) ret; i++) {
fprintf(stderr, "\t%u: %s [%s]\n",
i, iio_context_info_get_description(info[i]),
iio_context_info_get_uri(info[i]));
}
}

err_free_info_list:
iio_context_info_list_free(info);
err_free_ctx:
iio_scan_context_destroy(scan_ctx);

return ctx;
}


static void dump_device_attributes(const struct iio_device *dev,
const char *attr, const char *wbuf, bool quiet)
{
ssize_t ret;
char *buf = xmalloc(BUF_SIZE);
char *buf = xmalloc(BUF_SIZE, MY_NAME);

if (!wbuf || !quiet) {
if (!quiet)
Expand Down Expand Up @@ -213,7 +137,7 @@ static void dump_buffer_attributes(const struct iio_device *dev,
const char *attr, const char *wbuf, bool quiet)
{
ssize_t ret;
char *buf = xmalloc(BUF_SIZE);
char *buf = xmalloc(BUF_SIZE, MY_NAME);

if (!wbuf || !quiet) {
gen_function("device_buffer", "dev", attr, NULL);
Expand Down Expand Up @@ -255,7 +179,7 @@ static void dump_debug_attributes(const struct iio_device *dev,
const char *attr, const char *wbuf, bool quiet)
{
ssize_t ret;
char *buf = xmalloc(BUF_SIZE);
char *buf = xmalloc(BUF_SIZE, MY_NAME);

if (!wbuf || !quiet) {
gen_function("device_debug", "dev", attr, NULL);
Expand Down Expand Up @@ -297,7 +221,7 @@ static void dump_channel_attributes(const struct iio_device *dev,
struct iio_channel *ch, const char *attr, const char *wbuf, bool quiet)
{
ssize_t ret;
char *buf = xmalloc(BUF_SIZE);
char *buf = xmalloc(BUF_SIZE, MY_NAME);
const char *type_name;

if (!wbuf || !quiet) {
Expand Down Expand Up @@ -422,7 +346,7 @@ int main(int argc, char **argv)
int c, option_index = 0;
int device_index = 0, channel_index = 0, attr_index = 0;
const char *arg_uri = NULL, *gen_file = NULL;
enum backend backend = LOCAL;
enum backend backend = IIO_LOCAL;
bool detect_context = false, search_device = false, ignore_case = false,
search_channel = false, search_buffer = false, search_debug = false,
search_context = false, input_only = false, output_only = false,
Expand All @@ -442,7 +366,7 @@ int main(int argc, char **argv)
detect_context = true;
break;
case 'u':
backend = AUTO;
backend = IIO_AUTO;
arg_uri = optarg;
break;
/* Attribute type
Expand Down Expand Up @@ -623,16 +547,16 @@ int main(int argc, char **argv)
}

if (detect_context)
ctx = autodetect_context(gen_code);
else if (backend == AUTO) {
ctx = autodetect_context(true, gen_code, MY_NAME);
else if (backend == IIO_AUTO) {
ctx = iio_create_context_from_uri(arg_uri);
gen_context(arg_uri);
} else
ctx = iio_create_default_context();

if (!ctx) {
if (!detect_context) {
char *buf = xmalloc(BUF_SIZE);
char *buf = xmalloc(BUF_SIZE, MY_NAME);

iio_strerror(errno, buf, BUF_SIZE);
fprintf(stderr, "Unable to create IIO context: %s\n",
Expand All @@ -659,7 +583,7 @@ int main(int argc, char **argv)
gen_context_attr(key);
}
} else {
char *buf = xmalloc(BUF_SIZE);
char *buf = xmalloc(BUF_SIZE, MY_NAME);
iio_strerror(errno, buf, BUF_SIZE);
fprintf(stderr, "Unable to get context attributes: %s (%zd)\n",
buf, ret);
Expand Down
97 changes: 97 additions & 0 deletions tests/iio_common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* iio_common - Common functions used in the IIO utilities
*
* Copyright (C) 2014-2020 Analog Devices, Inc.
* Author: Paul Cercueil
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* */

#include <iio.h>
#include <stdio.h>

#include "iio_common.h"
#include "gen_code.h"

void * xmalloc(size_t n, const char * name)
{
void *p = malloc(n);

if (!p && n != 0) {
fprintf(stderr, "%s fatal error: allocating %zu bytes failed\n",
name, n);
exit(EXIT_FAILURE);
}

return p;
}

struct iio_context * autodetect_context(bool rtn, bool gen_code, const char * name)
{
struct iio_scan_context *scan_ctx;
struct iio_context_info **info;
struct iio_context *ctx = NULL;
unsigned int i;
ssize_t ret;
FILE *out;

scan_ctx = iio_create_scan_context(NULL, 0);
if (!scan_ctx) {
fprintf(stderr, "Unable to create scan context\n");
return NULL;
}

ret = iio_scan_context_get_info_list(scan_ctx, &info);
if (ret < 0) {
char *err_str = xmalloc(BUF_SIZE, name);
iio_strerror(-ret, err_str, BUF_SIZE);
fprintf(stderr, "Scanning for IIO contexts failed: %s\n", err_str);
free (err_str);
goto err_free_ctx;
}

if (ret == 0) {
printf("No IIO context found.\n");
goto err_free_info_list;
}
if (!rtn && ret == 1) {
printf("Using auto-detected IIO context at URI \"%s\"\n",
iio_context_info_get_uri(info[0]));
ctx = iio_create_context_from_uri(iio_context_info_get_uri(info[0]));
if (gen_code)
gen_context(iio_context_info_get_uri(info[0]));
} else {
if (rtn) {
out = stderr;
fprintf(out, "Multiple contexts found. Please select one using --uri:\n");
} else {
out = stdout;
fprintf(out, "Available contexts:\n");
}
for (i = 0; i < (size_t) ret; i++) {
fprintf(out, "\t%u: %s [%s]\n",
i, iio_context_info_get_description(info[i]),
iio_context_info_get_uri(info[i]));
}
}

err_free_info_list:
iio_context_info_list_free(info);
err_free_ctx:
iio_scan_context_destroy(scan_ctx);

return ctx;
}

Loading

0 comments on commit 329352a

Please sign in to comment.