blob: 2cf56fb2449ba0c3f67493561b25e9fc6d24e66c [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Manuel Stahl49d916e2014-05-02 13:23:00 +01002/*
3 * Industrial I/O utilities - lsiio.c
4 *
5 * Copyright (c) 2010 Manuel Stahl <manuel.stahl@iis.fraunhofer.de>
Manuel Stahl49d916e2014-05-02 13:23:00 +01006 */
7
8#include <string.h>
9#include <dirent.h>
10#include <stdio.h>
11#include <errno.h>
12#include <stdint.h>
13#include <stdlib.h>
14#include <unistd.h>
15#include <sys/types.h>
16#include <sys/stat.h>
17#include <sys/dir.h>
18#include "iio_utils.h"
19
Manuel Stahl49d916e2014-05-02 13:23:00 +010020static enum verbosity {
21 VERBLEVEL_DEFAULT, /* 0 gives lspci behaviour */
22 VERBLEVEL_SENSORS, /* 1 lists sensors */
23} verblevel = VERBLEVEL_DEFAULT;
24
25const char *type_device = "iio:device";
26const char *type_trigger = "trigger";
27
Manuel Stahl49d916e2014-05-02 13:23:00 +010028static inline int check_prefix(const char *str, const char *prefix)
29{
30 return strlen(str) > strlen(prefix) &&
Hartmut Knaack7663a4a2015-06-10 21:51:20 +020031 strncmp(str, prefix, strlen(prefix)) == 0;
Manuel Stahl49d916e2014-05-02 13:23:00 +010032}
33
34static inline int check_postfix(const char *str, const char *postfix)
35{
36 return strlen(str) > strlen(postfix) &&
Hartmut Knaack7663a4a2015-06-10 21:51:20 +020037 strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
Manuel Stahl49d916e2014-05-02 13:23:00 +010038}
39
40static int dump_channels(const char *dev_dir_name)
41{
42 DIR *dp;
43 const struct dirent *ent;
Heena Sirwanid1e50412014-10-06 17:03:31 +053044
Manuel Stahl49d916e2014-05-02 13:23:00 +010045 dp = opendir(dev_dir_name);
Cristina Opriceanaff1ac632015-07-13 16:15:56 +030046 if (!dp)
Manuel Stahl49d916e2014-05-02 13:23:00 +010047 return -errno;
Hartmut Knaack7663a4a2015-06-10 21:51:20 +020048
Cristina Opriceanaff1ac632015-07-13 16:15:56 +030049 while (ent = readdir(dp), ent)
Manuel Stahl49d916e2014-05-02 13:23:00 +010050 if (check_prefix(ent->d_name, "in_") &&
Matt Ranostay6df1dc02016-08-01 19:39:51 -070051 (check_postfix(ent->d_name, "_raw") ||
52 check_postfix(ent->d_name, "_input")))
Manuel Stahl49d916e2014-05-02 13:23:00 +010053 printf(" %-10s\n", ent->d_name);
Manuel Stahl49d916e2014-05-02 13:23:00 +010054
Hartmut Knaackf96d0552015-05-31 14:39:47 +020055 return (closedir(dp) == -1) ? -errno : 0;
Manuel Stahl49d916e2014-05-02 13:23:00 +010056}
57
58static int dump_one_device(const char *dev_dir_name)
59{
60 char name[IIO_MAX_NAME_LENGTH];
61 int dev_idx;
Hartmut Knaacka9d7acc2015-06-10 21:51:21 +020062 int ret;
Manuel Stahl49d916e2014-05-02 13:23:00 +010063
Hartmut Knaacka9d7acc2015-06-10 21:51:21 +020064 ret = sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_device), "%i",
65 &dev_idx);
66 if (ret != 1)
Heena Sirwanid0e68ce12014-10-06 17:04:43 +053067 return -EINVAL;
Hartmut Knaack7663a4a2015-06-10 21:51:20 +020068
Hartmut Knaacka9d7acc2015-06-10 21:51:21 +020069 ret = read_sysfs_string("name", dev_dir_name, name);
Linus Walleijaf255cd2015-08-04 16:21:49 +020070 if (ret < 0)
Hartmut Knaacka9d7acc2015-06-10 21:51:21 +020071 return ret;
Hartmut Knaackacf50b32015-05-31 14:40:15 +020072
Manuel Stahl49d916e2014-05-02 13:23:00 +010073 printf("Device %03d: %s\n", dev_idx, name);
74
Heena Sirwaniedead9b2014-10-06 17:04:10 +053075 if (verblevel >= VERBLEVEL_SENSORS)
76 return dump_channels(dev_dir_name);
Hartmut Knaack7663a4a2015-06-10 21:51:20 +020077
Manuel Stahl49d916e2014-05-02 13:23:00 +010078 return 0;
79}
80
81static int dump_one_trigger(const char *dev_dir_name)
82{
83 char name[IIO_MAX_NAME_LENGTH];
84 int dev_idx;
Hartmut Knaacka9d7acc2015-06-10 21:51:21 +020085 int ret;
Manuel Stahl49d916e2014-05-02 13:23:00 +010086
Hartmut Knaacka9d7acc2015-06-10 21:51:21 +020087 ret = sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_trigger),
88 "%i", &dev_idx);
89 if (ret != 1)
Heena Sirwanid0e68ce12014-10-06 17:04:43 +053090 return -EINVAL;
Hartmut Knaack7663a4a2015-06-10 21:51:20 +020091
Hartmut Knaacka9d7acc2015-06-10 21:51:21 +020092 ret = read_sysfs_string("name", dev_dir_name, name);
Linus Walleijaf255cd2015-08-04 16:21:49 +020093 if (ret < 0)
Hartmut Knaacka9d7acc2015-06-10 21:51:21 +020094 return ret;
Hartmut Knaackacf50b32015-05-31 14:40:15 +020095
Manuel Stahl49d916e2014-05-02 13:23:00 +010096 printf("Trigger %03d: %s\n", dev_idx, name);
Hartmut Knaack7663a4a2015-06-10 21:51:20 +020097
Manuel Stahl49d916e2014-05-02 13:23:00 +010098 return 0;
99}
100
Hartmut Knaackacf50b32015-05-31 14:40:15 +0200101static int dump_devices(void)
Manuel Stahl49d916e2014-05-02 13:23:00 +0100102{
103 const struct dirent *ent;
Hartmut Knaackacf50b32015-05-31 14:40:15 +0200104 int ret;
Manuel Stahl49d916e2014-05-02 13:23:00 +0100105 DIR *dp;
Manuel Stahl49d916e2014-05-02 13:23:00 +0100106
107 dp = opendir(iio_dir);
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300108 if (!dp) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300109 fprintf(stderr, "No industrial I/O devices available\n");
Hartmut Knaackacf50b32015-05-31 14:40:15 +0200110 return -ENODEV;
Manuel Stahl49d916e2014-05-02 13:23:00 +0100111 }
112
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300113 while (ent = readdir(dp), ent) {
Manuel Stahl49d916e2014-05-02 13:23:00 +0100114 if (check_prefix(ent->d_name, type_device)) {
115 char *dev_dir_name;
Heena Sirwanid1e50412014-10-06 17:03:31 +0530116
Hartmut Knaacke9e45b42015-05-31 14:40:02 +0200117 if (asprintf(&dev_dir_name, "%s%s", iio_dir,
118 ent->d_name) < 0) {
Hartmut Knaackacf50b32015-05-31 14:40:15 +0200119 ret = -ENOMEM;
Hartmut Knaacke9e45b42015-05-31 14:40:02 +0200120 goto error_close_dir;
121 }
122
Hartmut Knaackacf50b32015-05-31 14:40:15 +0200123 ret = dump_one_device(dev_dir_name);
124 if (ret) {
125 free(dev_dir_name);
126 goto error_close_dir;
127 }
128
Manuel Stahl49d916e2014-05-02 13:23:00 +0100129 free(dev_dir_name);
130 if (verblevel >= VERBLEVEL_SENSORS)
131 printf("\n");
132 }
133 }
134 rewinddir(dp);
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300135 while (ent = readdir(dp), ent) {
Manuel Stahl49d916e2014-05-02 13:23:00 +0100136 if (check_prefix(ent->d_name, type_trigger)) {
137 char *dev_dir_name;
Heena Sirwanid1e50412014-10-06 17:03:31 +0530138
Hartmut Knaacke9e45b42015-05-31 14:40:02 +0200139 if (asprintf(&dev_dir_name, "%s%s", iio_dir,
140 ent->d_name) < 0) {
Hartmut Knaackacf50b32015-05-31 14:40:15 +0200141 ret = -ENOMEM;
Hartmut Knaacke9e45b42015-05-31 14:40:02 +0200142 goto error_close_dir;
143 }
144
Hartmut Knaackacf50b32015-05-31 14:40:15 +0200145 ret = dump_one_trigger(dev_dir_name);
146 if (ret) {
147 free(dev_dir_name);
148 goto error_close_dir;
149 }
150
Manuel Stahl49d916e2014-05-02 13:23:00 +0100151 free(dev_dir_name);
152 }
153 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200154
Hartmut Knaackacf50b32015-05-31 14:40:15 +0200155 return (closedir(dp) == -1) ? -errno : 0;
156
Hartmut Knaacke9e45b42015-05-31 14:40:02 +0200157error_close_dir:
Hartmut Knaackacf50b32015-05-31 14:40:15 +0200158 if (closedir(dp) == -1)
159 perror("dump_devices(): Failed to close directory");
160
161 return ret;
Manuel Stahl49d916e2014-05-02 13:23:00 +0100162}
163
164int main(int argc, char **argv)
165{
166 int c, err = 0;
167
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200168 while ((c = getopt(argc, argv, "v")) != EOF) {
Manuel Stahl49d916e2014-05-02 13:23:00 +0100169 switch (c) {
170 case 'v':
171 verblevel++;
172 break;
173
174 case '?':
175 default:
176 err++;
177 break;
178 }
179 }
180 if (err || argc > optind) {
181 fprintf(stderr, "Usage: lsiio [options]...\n"
182 "List industrial I/O devices\n"
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200183 " -v Increase verbosity (may be given multiple times)\n");
Manuel Stahl49d916e2014-05-02 13:23:00 +0100184 exit(1);
185 }
186
Hartmut Knaackacf50b32015-05-31 14:40:15 +0200187 return dump_devices();
Manuel Stahl49d916e2014-05-02 13:23:00 +0100188}