2 * Industrial I/O utilities - lsiio.c
4 * Copyright (c) 2010 Manuel Stahl <manuel.stahl@iis.fraunhofer.de>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
18 #include <sys/types.h>
21 #include "iio_utils.h"
23 static enum verbosity
{
24 VERBLEVEL_DEFAULT
, /* 0 gives lspci behaviour */
25 VERBLEVEL_SENSORS
, /* 1 lists sensors */
26 } verblevel
= VERBLEVEL_DEFAULT
;
28 const char *type_device
= "iio:device";
29 const char *type_trigger
= "trigger";
31 static inline int check_prefix(const char *str
, const char *prefix
)
33 return strlen(str
) > strlen(prefix
) &&
34 strncmp(str
, prefix
, strlen(prefix
)) == 0;
37 static inline int check_postfix(const char *str
, const char *postfix
)
39 return strlen(str
) > strlen(postfix
) &&
40 strcmp(str
+ strlen(str
) - strlen(postfix
), postfix
) == 0;
43 static int dump_channels(const char *dev_dir_name
)
46 const struct dirent
*ent
;
48 dp
= opendir(dev_dir_name
);
52 while (ent
= readdir(dp
), ent
)
53 if (check_prefix(ent
->d_name
, "in_") &&
54 (check_postfix(ent
->d_name
, "_raw") ||
55 check_postfix(ent
->d_name
, "_input")))
56 printf(" %-10s\n", ent
->d_name
);
58 return (closedir(dp
) == -1) ? -errno
: 0;
61 static int dump_one_device(const char *dev_dir_name
)
63 char name
[IIO_MAX_NAME_LENGTH
];
67 ret
= sscanf(dev_dir_name
+ strlen(iio_dir
) + strlen(type_device
), "%i",
72 ret
= read_sysfs_string("name", dev_dir_name
, name
);
76 printf("Device %03d: %s\n", dev_idx
, name
);
78 if (verblevel
>= VERBLEVEL_SENSORS
)
79 return dump_channels(dev_dir_name
);
84 static int dump_one_trigger(const char *dev_dir_name
)
86 char name
[IIO_MAX_NAME_LENGTH
];
90 ret
= sscanf(dev_dir_name
+ strlen(iio_dir
) + strlen(type_trigger
),
95 ret
= read_sysfs_string("name", dev_dir_name
, name
);
99 printf("Trigger %03d: %s\n", dev_idx
, name
);
104 static int dump_devices(void)
106 const struct dirent
*ent
;
110 dp
= opendir(iio_dir
);
112 fprintf(stderr
, "No industrial I/O devices available\n");
116 while (ent
= readdir(dp
), ent
) {
117 if (check_prefix(ent
->d_name
, type_device
)) {
120 if (asprintf(&dev_dir_name
, "%s%s", iio_dir
,
123 goto error_close_dir
;
126 ret
= dump_one_device(dev_dir_name
);
129 goto error_close_dir
;
133 if (verblevel
>= VERBLEVEL_SENSORS
)
138 while (ent
= readdir(dp
), ent
) {
139 if (check_prefix(ent
->d_name
, type_trigger
)) {
142 if (asprintf(&dev_dir_name
, "%s%s", iio_dir
,
145 goto error_close_dir
;
148 ret
= dump_one_trigger(dev_dir_name
);
151 goto error_close_dir
;
158 return (closedir(dp
) == -1) ? -errno
: 0;
161 if (closedir(dp
) == -1)
162 perror("dump_devices(): Failed to close directory");
167 int main(int argc
, char **argv
)
171 while ((c
= getopt(argc
, argv
, "v")) != EOF
) {
183 if (err
|| argc
> optind
) {
184 fprintf(stderr
, "Usage: lsiio [options]...\n"
185 "List industrial I/O devices\n"
186 " -v Increase verbosity (may be given multiple times)\n");
190 return dump_devices();