]>
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_") && |
6df1dc05 MR |
54 | (check_postfix(ent->d_name, "_raw") || |
55 | check_postfix(ent->d_name, "_input"))) | |
49d916ec | 56 | printf(" %-10s\n", ent->d_name); |
49d916ec | 57 | |
f96d055e | 58 | return (closedir(dp) == -1) ? -errno : 0; |
49d916ec MS |
59 | } |
60 | ||
61 | static int dump_one_device(const char *dev_dir_name) | |
62 | { | |
63 | char name[IIO_MAX_NAME_LENGTH]; | |
64 | int dev_idx; | |
a9d7acc8 | 65 | int ret; |
49d916ec | 66 | |
a9d7acc8 HK |
67 | ret = sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_device), "%i", |
68 | &dev_idx); | |
69 | if (ret != 1) | |
d0e68ce1 | 70 | return -EINVAL; |
7663a4aa | 71 | |
a9d7acc8 | 72 | ret = read_sysfs_string("name", dev_dir_name, name); |
af255cd5 | 73 | if (ret < 0) |
a9d7acc8 | 74 | return ret; |
acf50b35 | 75 | |
49d916ec MS |
76 | printf("Device %03d: %s\n", dev_idx, name); |
77 | ||
edead9b1 HS |
78 | if (verblevel >= VERBLEVEL_SENSORS) |
79 | return dump_channels(dev_dir_name); | |
7663a4aa | 80 | |
49d916ec MS |
81 | return 0; |
82 | } | |
83 | ||
84 | static int dump_one_trigger(const char *dev_dir_name) | |
85 | { | |
86 | char name[IIO_MAX_NAME_LENGTH]; | |
87 | int dev_idx; | |
a9d7acc8 | 88 | int ret; |
49d916ec | 89 | |
a9d7acc8 HK |
90 | ret = sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_trigger), |
91 | "%i", &dev_idx); | |
92 | if (ret != 1) | |
d0e68ce1 | 93 | return -EINVAL; |
7663a4aa | 94 | |
a9d7acc8 | 95 | ret = read_sysfs_string("name", dev_dir_name, name); |
af255cd5 | 96 | if (ret < 0) |
a9d7acc8 | 97 | return ret; |
acf50b35 | 98 | |
49d916ec | 99 | printf("Trigger %03d: %s\n", dev_idx, name); |
7663a4aa | 100 | |
49d916ec MS |
101 | return 0; |
102 | } | |
103 | ||
acf50b35 | 104 | static int dump_devices(void) |
49d916ec MS |
105 | { |
106 | const struct dirent *ent; | |
acf50b35 | 107 | int ret; |
49d916ec | 108 | DIR *dp; |
49d916ec MS |
109 | |
110 | dp = opendir(iio_dir); | |
ff1ac639 | 111 | if (!dp) { |
d9abc615 | 112 | fprintf(stderr, "No industrial I/O devices available\n"); |
acf50b35 | 113 | return -ENODEV; |
49d916ec MS |
114 | } |
115 | ||
ff1ac639 | 116 | while (ent = readdir(dp), ent) { |
49d916ec MS |
117 | if (check_prefix(ent->d_name, type_device)) { |
118 | char *dev_dir_name; | |
d1e50413 | 119 | |
e9e45b43 HK |
120 | if (asprintf(&dev_dir_name, "%s%s", iio_dir, |
121 | ent->d_name) < 0) { | |
acf50b35 HK |
122 | ret = -ENOMEM; |
123 | goto error_close_dir; | |
124 | } | |
125 | ||
126 | ret = dump_one_device(dev_dir_name); | |
127 | if (ret) { | |
128 | free(dev_dir_name); | |
e9e45b43 HK |
129 | goto error_close_dir; |
130 | } | |
131 | ||
49d916ec MS |
132 | free(dev_dir_name); |
133 | if (verblevel >= VERBLEVEL_SENSORS) | |
134 | printf("\n"); | |
135 | } | |
136 | } | |
137 | rewinddir(dp); | |
ff1ac639 | 138 | while (ent = readdir(dp), ent) { |
49d916ec MS |
139 | if (check_prefix(ent->d_name, type_trigger)) { |
140 | char *dev_dir_name; | |
d1e50413 | 141 | |
e9e45b43 HK |
142 | if (asprintf(&dev_dir_name, "%s%s", iio_dir, |
143 | ent->d_name) < 0) { | |
acf50b35 HK |
144 | ret = -ENOMEM; |
145 | goto error_close_dir; | |
146 | } | |
147 | ||
148 | ret = dump_one_trigger(dev_dir_name); | |
149 | if (ret) { | |
150 | free(dev_dir_name); | |
e9e45b43 HK |
151 | goto error_close_dir; |
152 | } | |
153 | ||
49d916ec MS |
154 | free(dev_dir_name); |
155 | } | |
156 | } | |
7663a4aa | 157 | |
acf50b35 HK |
158 | return (closedir(dp) == -1) ? -errno : 0; |
159 | ||
e9e45b43 | 160 | error_close_dir: |
acf50b35 HK |
161 | if (closedir(dp) == -1) |
162 | perror("dump_devices(): Failed to close directory"); | |
163 | ||
164 | return ret; | |
49d916ec MS |
165 | } |
166 | ||
167 | int main(int argc, char **argv) | |
168 | { | |
169 | int c, err = 0; | |
170 | ||
e06e3d71 | 171 | while ((c = getopt(argc, argv, "v")) != EOF) { |
49d916ec MS |
172 | switch (c) { |
173 | case 'v': | |
174 | verblevel++; | |
175 | break; | |
176 | ||
177 | case '?': | |
178 | default: | |
179 | err++; | |
180 | break; | |
181 | } | |
182 | } | |
183 | if (err || argc > optind) { | |
184 | fprintf(stderr, "Usage: lsiio [options]...\n" | |
185 | "List industrial I/O devices\n" | |
e06e3d71 | 186 | " -v Increase verbosity (may be given multiple times)\n"); |
49d916ec MS |
187 | exit(1); |
188 | } | |
189 | ||
acf50b35 | 190 | return dump_devices(); |
49d916ec | 191 | } |