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 printf(" %-10s\n", ent
->d_name
);
57 return (closedir(dp
) == -1) ? -errno
: 0;
60 static int dump_one_device(const char *dev_dir_name
)
62 char name
[IIO_MAX_NAME_LENGTH
];
66 ret
= sscanf(dev_dir_name
+ strlen(iio_dir
) + strlen(type_device
), "%i",
71 ret
= read_sysfs_string("name", dev_dir_name
, name
);
75 printf("Device %03d: %s\n", dev_idx
, name
);
77 if (verblevel
>= VERBLEVEL_SENSORS
)
78 return dump_channels(dev_dir_name
);
83 static int dump_one_trigger(const char *dev_dir_name
)
85 char name
[IIO_MAX_NAME_LENGTH
];
89 ret
= sscanf(dev_dir_name
+ strlen(iio_dir
) + strlen(type_trigger
),
94 ret
= read_sysfs_string("name", dev_dir_name
, name
);
98 printf("Trigger %03d: %s\n", dev_idx
, name
);
103 static int dump_devices(void)
105 const struct dirent
*ent
;
109 dp
= opendir(iio_dir
);
111 fprintf(stderr
, "No industrial I/O devices available\n");
115 while (ent
= readdir(dp
), ent
) {
116 if (check_prefix(ent
->d_name
, type_device
)) {
119 if (asprintf(&dev_dir_name
, "%s%s", iio_dir
,
122 goto error_close_dir
;
125 ret
= dump_one_device(dev_dir_name
);
128 goto error_close_dir
;
132 if (verblevel
>= VERBLEVEL_SENSORS
)
137 while (ent
= readdir(dp
), ent
) {
138 if (check_prefix(ent
->d_name
, type_trigger
)) {
141 if (asprintf(&dev_dir_name
, "%s%s", iio_dir
,
144 goto error_close_dir
;
147 ret
= dump_one_trigger(dev_dir_name
);
150 goto error_close_dir
;
157 return (closedir(dp
) == -1) ? -errno
: 0;
160 if (closedir(dp
) == -1)
161 perror("dump_devices(): Failed to close directory");
166 int main(int argc
, char **argv
)
170 while ((c
= getopt(argc
, argv
, "v")) != EOF
) {
182 if (err
|| argc
> optind
) {
183 fprintf(stderr
, "Usage: lsiio [options]...\n"
184 "List industrial I/O devices\n"
185 " -v Increase verbosity (may be given multiple times)\n");
189 return dump_devices();