Commit | Line | Data |
---|---|---|
49d916ec MS |
1 | /* |
2 | * Industrial I/O utilities - lsiio.c | |
3 | * | |
4 | * Copyright (c) 2010 Manuel Stahl <manuel.stahl@iis.fraunhofer.de> | |
5 | * | |
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. | |
9 | */ | |
10 | ||
11 | #include <string.h> | |
12 | #include <dirent.h> | |
13 | #include <stdio.h> | |
14 | #include <errno.h> | |
15 | #include <stdint.h> | |
16 | #include <stdlib.h> | |
17 | #include <unistd.h> | |
18 | #include <sys/types.h> | |
19 | #include <sys/stat.h> | |
20 | #include <sys/dir.h> | |
21 | #include "iio_utils.h" | |
22 | ||
49d916ec MS |
23 | static enum verbosity { |
24 | VERBLEVEL_DEFAULT, /* 0 gives lspci behaviour */ | |
25 | VERBLEVEL_SENSORS, /* 1 lists sensors */ | |
26 | } verblevel = VERBLEVEL_DEFAULT; | |
27 | ||
28 | const char *type_device = "iio:device"; | |
29 | const char *type_trigger = "trigger"; | |
30 | ||
49d916ec MS |
31 | static inline int check_prefix(const char *str, const char *prefix) |
32 | { | |
33 | return strlen(str) > strlen(prefix) && | |
7663a4aa | 34 | strncmp(str, prefix, strlen(prefix)) == 0; |
49d916ec MS |
35 | } |
36 | ||
37 | static inline int check_postfix(const char *str, const char *postfix) | |
38 | { | |
39 | return strlen(str) > strlen(postfix) && | |
7663a4aa | 40 | strcmp(str + strlen(str) - strlen(postfix), postfix) == 0; |
49d916ec MS |
41 | } |
42 | ||
43 | static int dump_channels(const char *dev_dir_name) | |
44 | { | |
45 | DIR *dp; | |
46 | const struct dirent *ent; | |
d1e50413 | 47 | |
49d916ec | 48 | dp = opendir(dev_dir_name); |
ff1ac639 | 49 | if (!dp) |
49d916ec | 50 | return -errno; |
7663a4aa | 51 | |
ff1ac639 | 52 | while (ent = readdir(dp), ent) |
49d916ec | 53 | if (check_prefix(ent->d_name, "in_") && |
7663a4aa | 54 | check_postfix(ent->d_name, "_raw")) |
49d916ec | 55 | printf(" %-10s\n", ent->d_name); |
49d916ec | 56 | |
f96d055e | 57 | return (closedir(dp) == -1) ? -errno : 0; |
49d916ec MS |
58 | } |
59 | ||
60 | static int dump_one_device(const char *dev_dir_name) | |
61 | { | |
62 | char name[IIO_MAX_NAME_LENGTH]; | |
63 | int dev_idx; | |
a9d7acc8 | 64 | int ret; |
49d916ec | 65 | |
a9d7acc8 HK |
66 | ret = sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_device), "%i", |
67 | &dev_idx); | |
68 | if (ret != 1) | |
d0e68ce1 | 69 | return -EINVAL; |
7663a4aa | 70 | |
a9d7acc8 | 71 | ret = read_sysfs_string("name", dev_dir_name, name); |
af255cd5 | 72 | if (ret < 0) |
a9d7acc8 | 73 | return ret; |
acf50b35 | 74 | |
49d916ec MS |
75 | printf("Device %03d: %s\n", dev_idx, name); |
76 | ||
edead9b1 HS |
77 | if (verblevel >= VERBLEVEL_SENSORS) |
78 | return dump_channels(dev_dir_name); | |
7663a4aa | 79 | |
49d916ec MS |
80 | return 0; |
81 | } | |
82 | ||
83 | static int dump_one_trigger(const char *dev_dir_name) | |
84 | { | |
85 | char name[IIO_MAX_NAME_LENGTH]; | |
86 | int dev_idx; | |
a9d7acc8 | 87 | int ret; |
49d916ec | 88 | |
a9d7acc8 HK |
89 | ret = sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_trigger), |
90 | "%i", &dev_idx); | |
91 | if (ret != 1) | |
d0e68ce1 | 92 | return -EINVAL; |
7663a4aa | 93 | |
a9d7acc8 | 94 | ret = read_sysfs_string("name", dev_dir_name, name); |
af255cd5 | 95 | if (ret < 0) |
a9d7acc8 | 96 | return ret; |
acf50b35 | 97 | |
49d916ec | 98 | printf("Trigger %03d: %s\n", dev_idx, name); |
7663a4aa | 99 | |
49d916ec MS |
100 | return 0; |
101 | } | |
102 | ||
acf50b35 | 103 | static int dump_devices(void) |
49d916ec MS |
104 | { |
105 | const struct dirent *ent; | |
acf50b35 | 106 | int ret; |
49d916ec | 107 | DIR *dp; |
49d916ec MS |
108 | |
109 | dp = opendir(iio_dir); | |
ff1ac639 | 110 | if (!dp) { |
d9abc615 | 111 | fprintf(stderr, "No industrial I/O devices available\n"); |
acf50b35 | 112 | return -ENODEV; |
49d916ec MS |
113 | } |
114 | ||
ff1ac639 | 115 | while (ent = readdir(dp), ent) { |
49d916ec MS |
116 | if (check_prefix(ent->d_name, type_device)) { |
117 | char *dev_dir_name; | |
d1e50413 | 118 | |
e9e45b43 HK |
119 | if (asprintf(&dev_dir_name, "%s%s", iio_dir, |
120 | ent->d_name) < 0) { | |
acf50b35 HK |
121 | ret = -ENOMEM; |
122 | goto error_close_dir; | |
123 | } | |
124 | ||
125 | ret = dump_one_device(dev_dir_name); | |
126 | if (ret) { | |
127 | free(dev_dir_name); | |
e9e45b43 HK |
128 | goto error_close_dir; |
129 | } | |
130 | ||
49d916ec MS |
131 | free(dev_dir_name); |
132 | if (verblevel >= VERBLEVEL_SENSORS) | |
133 | printf("\n"); | |
134 | } | |
135 | } | |
136 | rewinddir(dp); | |
ff1ac639 | 137 | while (ent = readdir(dp), ent) { |
49d916ec MS |
138 | if (check_prefix(ent->d_name, type_trigger)) { |
139 | char *dev_dir_name; | |
d1e50413 | 140 | |
e9e45b43 HK |
141 | if (asprintf(&dev_dir_name, "%s%s", iio_dir, |
142 | ent->d_name) < 0) { | |
acf50b35 HK |
143 | ret = -ENOMEM; |
144 | goto error_close_dir; | |
145 | } | |
146 | ||
147 | ret = dump_one_trigger(dev_dir_name); | |
148 | if (ret) { | |
149 | free(dev_dir_name); | |
e9e45b43 HK |
150 | goto error_close_dir; |
151 | } | |
152 | ||
49d916ec MS |
153 | free(dev_dir_name); |
154 | } | |
155 | } | |
7663a4aa | 156 | |
acf50b35 HK |
157 | return (closedir(dp) == -1) ? -errno : 0; |
158 | ||
e9e45b43 | 159 | error_close_dir: |
acf50b35 HK |
160 | if (closedir(dp) == -1) |
161 | perror("dump_devices(): Failed to close directory"); | |
162 | ||
163 | return ret; | |
49d916ec MS |
164 | } |
165 | ||
166 | int main(int argc, char **argv) | |
167 | { | |
168 | int c, err = 0; | |
169 | ||
e06e3d71 | 170 | while ((c = getopt(argc, argv, "v")) != EOF) { |
49d916ec MS |
171 | switch (c) { |
172 | case 'v': | |
173 | verblevel++; | |
174 | break; | |
175 | ||
176 | case '?': | |
177 | default: | |
178 | err++; | |
179 | break; | |
180 | } | |
181 | } | |
182 | if (err || argc > optind) { | |
183 | fprintf(stderr, "Usage: lsiio [options]...\n" | |
184 | "List industrial I/O devices\n" | |
e06e3d71 | 185 | " -v Increase verbosity (may be given multiple times)\n"); |
49d916ec MS |
186 | exit(1); |
187 | } | |
188 | ||
acf50b35 | 189 | return dump_devices(); |
49d916ec | 190 | } |