2 * Industrial I/O utilities - lsiio.c
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"
24 static enum verbosity {
25 VERBLEVEL_DEFAULT, /* 0 gives lspci behaviour */
26 VERBLEVEL_SENSORS, /* 1 lists sensors */
27 } verblevel = VERBLEVEL_DEFAULT;
29 const char *type_device = "iio:device";
30 const char *type_trigger = "trigger";
33 static inline int check_prefix(const char *str, const char *prefix)
35 return strlen(str) > strlen(prefix) &&
36 strncmp(str, prefix, strlen(prefix)) == 0;
39 static inline int check_postfix(const char *str, const char *postfix)
41 return strlen(str) > strlen(postfix) &&
42 strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
45 static int dump_channels(const char *dev_dir_name)
48 const struct dirent *ent;
50 dp = opendir(dev_dir_name);
53 while (ent = readdir(dp), ent != NULL)
54 if (check_prefix(ent->d_name, "in_") &&
55 check_postfix(ent->d_name, "_raw")) {
56 printf(" %-10s\n", ent->d_name);
62 static int dump_one_device(const char *dev_dir_name)
64 char name[IIO_MAX_NAME_LENGTH];
68 retval = sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_device),
72 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);
80 static int dump_one_trigger(const char *dev_dir_name)
82 char name[IIO_MAX_NAME_LENGTH];
86 retval = sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_trigger),
90 read_sysfs_string("name", dev_dir_name, name);
91 printf("Trigger %03d: %s\n", dev_idx, name);
95 static void dump_devices(void)
97 const struct dirent *ent;
98 int number, numstrlen;
102 char thisname[IIO_MAX_NAME_LENGTH];
105 dp = opendir(iio_dir);
107 printf("No industrial I/O devices available\n");
111 while (ent = readdir(dp), ent != NULL) {
112 if (check_prefix(ent->d_name, type_device)) {
115 asprintf(&dev_dir_name, "%s%s", iio_dir, ent->d_name);
116 dump_one_device(dev_dir_name);
118 if (verblevel >= VERBLEVEL_SENSORS)
123 while (ent = readdir(dp), ent != NULL) {
124 if (check_prefix(ent->d_name, type_trigger)) {
127 asprintf(&dev_dir_name, "%s%s", iio_dir, ent->d_name);
128 dump_one_trigger(dev_dir_name);
135 int main(int argc, char **argv)
139 while ((c = getopt(argc, argv, "d:D:v")) != EOF) {
151 if (err || argc > optind) {
152 fprintf(stderr, "Usage: lsiio [options]...\n"
153 "List industrial I/O devices\n"
155 " Increase verbosity (may be given multiple times)\n"