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