1 // SPDX-License-Identifier: GPL-2.0-only
3 * Industrial I/O utilities - lsiio.c
5 * Copyright (c) 2010 Manuel Stahl <manuel.stahl@iis.fraunhofer.de>
15 #include <sys/types.h>
18 #include "iio_utils.h"
20 static enum verbosity
{
21 VERBLEVEL_DEFAULT
, /* 0 gives lspci behaviour */
22 VERBLEVEL_SENSORS
, /* 1 lists sensors */
23 } verblevel
= VERBLEVEL_DEFAULT
;
25 const char *type_device
= "iio:device";
26 const char *type_trigger
= "trigger";
28 static inline int check_prefix(const char *str
, const char *prefix
)
30 return strlen(str
) > strlen(prefix
) &&
31 strncmp(str
, prefix
, strlen(prefix
)) == 0;
34 static inline int check_postfix(const char *str
, const char *postfix
)
36 return strlen(str
) > strlen(postfix
) &&
37 strcmp(str
+ strlen(str
) - strlen(postfix
), postfix
) == 0;
40 static int dump_channels(const char *dev_dir_name
)
43 const struct dirent
*ent
;
45 dp
= opendir(dev_dir_name
);
49 while (ent
= readdir(dp
), ent
)
50 if (check_prefix(ent
->d_name
, "in_") &&
51 (check_postfix(ent
->d_name
, "_raw") ||
52 check_postfix(ent
->d_name
, "_input")))
53 printf(" %-10s\n", ent
->d_name
);
55 return (closedir(dp
) == -1) ? -errno
: 0;
58 static int dump_one_device(const char *dev_dir_name
)
60 char name
[IIO_MAX_NAME_LENGTH
];
64 ret
= sscanf(dev_dir_name
+ strlen(iio_dir
) + strlen(type_device
), "%i",
69 ret
= read_sysfs_string("name", dev_dir_name
, name
);
73 printf("Device %03d: %s\n", dev_idx
, name
);
75 if (verblevel
>= VERBLEVEL_SENSORS
)
76 return dump_channels(dev_dir_name
);
81 static int dump_one_trigger(const char *dev_dir_name
)
83 char name
[IIO_MAX_NAME_LENGTH
];
87 ret
= sscanf(dev_dir_name
+ strlen(iio_dir
) + strlen(type_trigger
),
92 ret
= read_sysfs_string("name", dev_dir_name
, name
);
96 printf("Trigger %03d: %s\n", dev_idx
, name
);
101 static int dump_devices(void)
103 const struct dirent
*ent
;
107 dp
= opendir(iio_dir
);
109 fprintf(stderr
, "No industrial I/O devices available\n");
113 while (ent
= readdir(dp
), ent
) {
114 if (check_prefix(ent
->d_name
, type_device
)) {
117 if (asprintf(&dev_dir_name
, "%s%s", iio_dir
,
120 goto error_close_dir
;
123 ret
= dump_one_device(dev_dir_name
);
126 goto error_close_dir
;
130 if (verblevel
>= VERBLEVEL_SENSORS
)
135 while (ent
= readdir(dp
), ent
) {
136 if (check_prefix(ent
->d_name
, type_trigger
)) {
139 if (asprintf(&dev_dir_name
, "%s%s", iio_dir
,
142 goto error_close_dir
;
145 ret
= dump_one_trigger(dev_dir_name
);
148 goto error_close_dir
;
155 return (closedir(dp
) == -1) ? -errno
: 0;
158 if (closedir(dp
) == -1)
159 perror("dump_devices(): Failed to close directory");
164 int main(int argc
, char **argv
)
168 while ((c
= getopt(argc
, argv
, "v")) != EOF
) {
180 if (err
|| argc
> optind
) {
181 fprintf(stderr
, "Usage: lsiio [options]...\n"
182 "List industrial I/O devices\n"
183 " -v Increase verbosity (may be given multiple times)\n");
187 return dump_devices();