blob: ab0f5cf1602592c7b5e8f9846657d6234208851b [file] [log] [blame]
Manuel Stahl49d916e2014-05-02 13:23:00 +01001/*
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
Manuel Stahl49d916e2014-05-02 13:23:00 +010023static enum verbosity {
24 VERBLEVEL_DEFAULT, /* 0 gives lspci behaviour */
25 VERBLEVEL_SENSORS, /* 1 lists sensors */
26} verblevel = VERBLEVEL_DEFAULT;
27
28const char *type_device = "iio:device";
29const char *type_trigger = "trigger";
30
Manuel Stahl49d916e2014-05-02 13:23:00 +010031static inline int check_prefix(const char *str, const char *prefix)
32{
33 return strlen(str) > strlen(prefix) &&
Hartmut Knaack7663a4a2015-06-10 21:51:20 +020034 strncmp(str, prefix, strlen(prefix)) == 0;
Manuel Stahl49d916e2014-05-02 13:23:00 +010035}
36
37static inline int check_postfix(const char *str, const char *postfix)
38{
39 return strlen(str) > strlen(postfix) &&
Hartmut Knaack7663a4a2015-06-10 21:51:20 +020040 strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
Manuel Stahl49d916e2014-05-02 13:23:00 +010041}
42
43static int dump_channels(const char *dev_dir_name)
44{
45 DIR *dp;
46 const struct dirent *ent;
Heena Sirwanid1e50412014-10-06 17:03:31 +053047
Manuel Stahl49d916e2014-05-02 13:23:00 +010048 dp = opendir(dev_dir_name);
Cristina Opriceanaff1ac632015-07-13 16:15:56 +030049 if (!dp)
Manuel Stahl49d916e2014-05-02 13:23:00 +010050 return -errno;
Hartmut Knaack7663a4a2015-06-10 21:51:20 +020051
Cristina Opriceanaff1ac632015-07-13 16:15:56 +030052 while (ent = readdir(dp), ent)
Manuel Stahl49d916e2014-05-02 13:23:00 +010053 if (check_prefix(ent->d_name, "in_") &&
Matt Ranostay6df1dc02016-08-01 19:39:51 -070054 (check_postfix(ent->d_name, "_raw") ||
55 check_postfix(ent->d_name, "_input")))
Manuel Stahl49d916e2014-05-02 13:23:00 +010056 printf(" %-10s\n", ent->d_name);
Manuel Stahl49d916e2014-05-02 13:23:00 +010057
Hartmut Knaackf96d0552015-05-31 14:39:47 +020058 return (closedir(dp) == -1) ? -errno : 0;
Manuel Stahl49d916e2014-05-02 13:23:00 +010059}
60
61static int dump_one_device(const char *dev_dir_name)
62{
63 char name[IIO_MAX_NAME_LENGTH];
64 int dev_idx;
Hartmut Knaacka9d7acc2015-06-10 21:51:21 +020065 int ret;
Manuel Stahl49d916e2014-05-02 13:23:00 +010066
Hartmut Knaacka9d7acc2015-06-10 21:51:21 +020067 ret = sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_device), "%i",
68 &dev_idx);
69 if (ret != 1)
Heena Sirwanid0e68ce2014-10-06 17:04:43 +053070 return -EINVAL;
Hartmut Knaack7663a4a2015-06-10 21:51:20 +020071
Hartmut Knaacka9d7acc2015-06-10 21:51:21 +020072 ret = read_sysfs_string("name", dev_dir_name, name);
Linus Walleijaf255cd2015-08-04 16:21:49 +020073 if (ret < 0)
Hartmut Knaacka9d7acc2015-06-10 21:51:21 +020074 return ret;
Hartmut Knaackacf50b32015-05-31 14:40:15 +020075
Manuel Stahl49d916e2014-05-02 13:23:00 +010076 printf("Device %03d: %s\n", dev_idx, name);
77
Heena Sirwaniedead9b2014-10-06 17:04:10 +053078 if (verblevel >= VERBLEVEL_SENSORS)
79 return dump_channels(dev_dir_name);
Hartmut Knaack7663a4a2015-06-10 21:51:20 +020080
Manuel Stahl49d916e2014-05-02 13:23:00 +010081 return 0;
82}
83
84static int dump_one_trigger(const char *dev_dir_name)
85{
86 char name[IIO_MAX_NAME_LENGTH];
87 int dev_idx;
Hartmut Knaacka9d7acc2015-06-10 21:51:21 +020088 int ret;
Manuel Stahl49d916e2014-05-02 13:23:00 +010089
Hartmut Knaacka9d7acc2015-06-10 21:51:21 +020090 ret = sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_trigger),
91 "%i", &dev_idx);
92 if (ret != 1)
Heena Sirwanid0e68ce2014-10-06 17:04:43 +053093 return -EINVAL;
Hartmut Knaack7663a4a2015-06-10 21:51:20 +020094
Hartmut Knaacka9d7acc2015-06-10 21:51:21 +020095 ret = read_sysfs_string("name", dev_dir_name, name);
Linus Walleijaf255cd2015-08-04 16:21:49 +020096 if (ret < 0)
Hartmut Knaacka9d7acc2015-06-10 21:51:21 +020097 return ret;
Hartmut Knaackacf50b32015-05-31 14:40:15 +020098
Manuel Stahl49d916e2014-05-02 13:23:00 +010099 printf("Trigger %03d: %s\n", dev_idx, name);
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200100
Manuel Stahl49d916e2014-05-02 13:23:00 +0100101 return 0;
102}
103
Hartmut Knaackacf50b32015-05-31 14:40:15 +0200104static int dump_devices(void)
Manuel Stahl49d916e2014-05-02 13:23:00 +0100105{
106 const struct dirent *ent;
Hartmut Knaackacf50b32015-05-31 14:40:15 +0200107 int ret;
Manuel Stahl49d916e2014-05-02 13:23:00 +0100108 DIR *dp;
Manuel Stahl49d916e2014-05-02 13:23:00 +0100109
110 dp = opendir(iio_dir);
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300111 if (!dp) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300112 fprintf(stderr, "No industrial I/O devices available\n");
Hartmut Knaackacf50b32015-05-31 14:40:15 +0200113 return -ENODEV;
Manuel Stahl49d916e2014-05-02 13:23:00 +0100114 }
115
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300116 while (ent = readdir(dp), ent) {
Manuel Stahl49d916e2014-05-02 13:23:00 +0100117 if (check_prefix(ent->d_name, type_device)) {
118 char *dev_dir_name;
Heena Sirwanid1e50412014-10-06 17:03:31 +0530119
Hartmut Knaacke9e45b42015-05-31 14:40:02 +0200120 if (asprintf(&dev_dir_name, "%s%s", iio_dir,
121 ent->d_name) < 0) {
Hartmut Knaackacf50b32015-05-31 14:40:15 +0200122 ret = -ENOMEM;
Hartmut Knaacke9e45b42015-05-31 14:40:02 +0200123 goto error_close_dir;
124 }
125
Hartmut Knaackacf50b32015-05-31 14:40:15 +0200126 ret = dump_one_device(dev_dir_name);
127 if (ret) {
128 free(dev_dir_name);
129 goto error_close_dir;
130 }
131
Manuel Stahl49d916e2014-05-02 13:23:00 +0100132 free(dev_dir_name);
133 if (verblevel >= VERBLEVEL_SENSORS)
134 printf("\n");
135 }
136 }
137 rewinddir(dp);
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300138 while (ent = readdir(dp), ent) {
Manuel Stahl49d916e2014-05-02 13:23:00 +0100139 if (check_prefix(ent->d_name, type_trigger)) {
140 char *dev_dir_name;
Heena Sirwanid1e50412014-10-06 17:03:31 +0530141
Hartmut Knaacke9e45b42015-05-31 14:40:02 +0200142 if (asprintf(&dev_dir_name, "%s%s", iio_dir,
143 ent->d_name) < 0) {
Hartmut Knaackacf50b32015-05-31 14:40:15 +0200144 ret = -ENOMEM;
Hartmut Knaacke9e45b42015-05-31 14:40:02 +0200145 goto error_close_dir;
146 }
147
Hartmut Knaackacf50b32015-05-31 14:40:15 +0200148 ret = dump_one_trigger(dev_dir_name);
149 if (ret) {
150 free(dev_dir_name);
151 goto error_close_dir;
152 }
153
Manuel Stahl49d916e2014-05-02 13:23:00 +0100154 free(dev_dir_name);
155 }
156 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200157
Hartmut Knaackacf50b32015-05-31 14:40:15 +0200158 return (closedir(dp) == -1) ? -errno : 0;
159
Hartmut Knaacke9e45b42015-05-31 14:40:02 +0200160error_close_dir:
Hartmut Knaackacf50b32015-05-31 14:40:15 +0200161 if (closedir(dp) == -1)
162 perror("dump_devices(): Failed to close directory");
163
164 return ret;
Manuel Stahl49d916e2014-05-02 13:23:00 +0100165}
166
167int main(int argc, char **argv)
168{
169 int c, err = 0;
170
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200171 while ((c = getopt(argc, argv, "v")) != EOF) {
Manuel Stahl49d916e2014-05-02 13:23:00 +0100172 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"
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200186 " -v Increase verbosity (may be given multiple times)\n");
Manuel Stahl49d916e2014-05-02 13:23:00 +0100187 exit(1);
188 }
189
Hartmut Knaackacf50b32015-05-31 14:40:15 +0200190 return dump_devices();
Manuel Stahl49d916e2014-05-02 13:23:00 +0100191}