blob: 3d650e668252f8f195a8e6a14380117d60b329cc [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_") &&
Hartmut Knaack7663a4a2015-06-10 21:51:20 +020054 check_postfix(ent->d_name, "_raw"))
Manuel Stahl49d916e2014-05-02 13:23:00 +010055 printf(" %-10s\n", ent->d_name);
Manuel Stahl49d916e2014-05-02 13:23:00 +010056
Hartmut Knaackf96d0552015-05-31 14:39:47 +020057 return (closedir(dp) == -1) ? -errno : 0;
Manuel Stahl49d916e2014-05-02 13:23:00 +010058}
59
60static int dump_one_device(const char *dev_dir_name)
61{
62 char name[IIO_MAX_NAME_LENGTH];
63 int dev_idx;
Hartmut Knaacka9d7acc2015-06-10 21:51:21 +020064 int ret;
Manuel Stahl49d916e2014-05-02 13:23:00 +010065
Hartmut Knaacka9d7acc2015-06-10 21:51:21 +020066 ret = sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_device), "%i",
67 &dev_idx);
68 if (ret != 1)
Heena Sirwanid0e68ce2014-10-06 17:04:43 +053069 return -EINVAL;
Hartmut Knaack7663a4a2015-06-10 21:51:20 +020070
Hartmut Knaacka9d7acc2015-06-10 21:51:21 +020071 ret = read_sysfs_string("name", dev_dir_name, name);
Linus Walleijaf255cd2015-08-04 16:21:49 +020072 if (ret < 0)
Hartmut Knaacka9d7acc2015-06-10 21:51:21 +020073 return ret;
Hartmut Knaackacf50b32015-05-31 14:40:15 +020074
Manuel Stahl49d916e2014-05-02 13:23:00 +010075 printf("Device %03d: %s\n", dev_idx, name);
76
Heena Sirwaniedead9b2014-10-06 17:04:10 +053077 if (verblevel >= VERBLEVEL_SENSORS)
78 return dump_channels(dev_dir_name);
Hartmut Knaack7663a4a2015-06-10 21:51:20 +020079
Manuel Stahl49d916e2014-05-02 13:23:00 +010080 return 0;
81}
82
83static int dump_one_trigger(const char *dev_dir_name)
84{
85 char name[IIO_MAX_NAME_LENGTH];
86 int dev_idx;
Hartmut Knaacka9d7acc2015-06-10 21:51:21 +020087 int ret;
Manuel Stahl49d916e2014-05-02 13:23:00 +010088
Hartmut Knaacka9d7acc2015-06-10 21:51:21 +020089 ret = sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_trigger),
90 "%i", &dev_idx);
91 if (ret != 1)
Heena Sirwanid0e68ce2014-10-06 17:04:43 +053092 return -EINVAL;
Hartmut Knaack7663a4a2015-06-10 21:51:20 +020093
Hartmut Knaacka9d7acc2015-06-10 21:51:21 +020094 ret = read_sysfs_string("name", dev_dir_name, name);
Linus Walleijaf255cd2015-08-04 16:21:49 +020095 if (ret < 0)
Hartmut Knaacka9d7acc2015-06-10 21:51:21 +020096 return ret;
Hartmut Knaackacf50b32015-05-31 14:40:15 +020097
Manuel Stahl49d916e2014-05-02 13:23:00 +010098 printf("Trigger %03d: %s\n", dev_idx, name);
Hartmut Knaack7663a4a2015-06-10 21:51:20 +020099
Manuel Stahl49d916e2014-05-02 13:23:00 +0100100 return 0;
101}
102
Hartmut Knaackacf50b32015-05-31 14:40:15 +0200103static int dump_devices(void)
Manuel Stahl49d916e2014-05-02 13:23:00 +0100104{
105 const struct dirent *ent;
Hartmut Knaackacf50b32015-05-31 14:40:15 +0200106 int ret;
Manuel Stahl49d916e2014-05-02 13:23:00 +0100107 DIR *dp;
Manuel Stahl49d916e2014-05-02 13:23:00 +0100108
109 dp = opendir(iio_dir);
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300110 if (!dp) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300111 fprintf(stderr, "No industrial I/O devices available\n");
Hartmut Knaackacf50b32015-05-31 14:40:15 +0200112 return -ENODEV;
Manuel Stahl49d916e2014-05-02 13:23:00 +0100113 }
114
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300115 while (ent = readdir(dp), ent) {
Manuel Stahl49d916e2014-05-02 13:23:00 +0100116 if (check_prefix(ent->d_name, type_device)) {
117 char *dev_dir_name;
Heena Sirwanid1e50412014-10-06 17:03:31 +0530118
Hartmut Knaacke9e45b42015-05-31 14:40:02 +0200119 if (asprintf(&dev_dir_name, "%s%s", iio_dir,
120 ent->d_name) < 0) {
Hartmut Knaackacf50b32015-05-31 14:40:15 +0200121 ret = -ENOMEM;
Hartmut Knaacke9e45b42015-05-31 14:40:02 +0200122 goto error_close_dir;
123 }
124
Hartmut Knaackacf50b32015-05-31 14:40:15 +0200125 ret = dump_one_device(dev_dir_name);
126 if (ret) {
127 free(dev_dir_name);
128 goto error_close_dir;
129 }
130
Manuel Stahl49d916e2014-05-02 13:23:00 +0100131 free(dev_dir_name);
132 if (verblevel >= VERBLEVEL_SENSORS)
133 printf("\n");
134 }
135 }
136 rewinddir(dp);
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300137 while (ent = readdir(dp), ent) {
Manuel Stahl49d916e2014-05-02 13:23:00 +0100138 if (check_prefix(ent->d_name, type_trigger)) {
139 char *dev_dir_name;
Heena Sirwanid1e50412014-10-06 17:03:31 +0530140
Hartmut Knaacke9e45b42015-05-31 14:40:02 +0200141 if (asprintf(&dev_dir_name, "%s%s", iio_dir,
142 ent->d_name) < 0) {
Hartmut Knaackacf50b32015-05-31 14:40:15 +0200143 ret = -ENOMEM;
Hartmut Knaacke9e45b42015-05-31 14:40:02 +0200144 goto error_close_dir;
145 }
146
Hartmut Knaackacf50b32015-05-31 14:40:15 +0200147 ret = dump_one_trigger(dev_dir_name);
148 if (ret) {
149 free(dev_dir_name);
150 goto error_close_dir;
151 }
152
Manuel Stahl49d916e2014-05-02 13:23:00 +0100153 free(dev_dir_name);
154 }
155 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200156
Hartmut Knaackacf50b32015-05-31 14:40:15 +0200157 return (closedir(dp) == -1) ? -errno : 0;
158
Hartmut Knaacke9e45b42015-05-31 14:40:02 +0200159error_close_dir:
Hartmut Knaackacf50b32015-05-31 14:40:15 +0200160 if (closedir(dp) == -1)
161 perror("dump_devices(): Failed to close directory");
162
163 return ret;
Manuel Stahl49d916e2014-05-02 13:23:00 +0100164}
165
166int main(int argc, char **argv)
167{
168 int c, err = 0;
169
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200170 while ((c = getopt(argc, argv, "v")) != EOF) {
Manuel Stahl49d916e2014-05-02 13:23:00 +0100171 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"
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200185 " -v Increase verbosity (may be given multiple times)\n");
Manuel Stahl49d916e2014-05-02 13:23:00 +0100186 exit(1);
187 }
188
Hartmut Knaackacf50b32015-05-31 14:40:15 +0200189 return dump_devices();
Manuel Stahl49d916e2014-05-02 13:23:00 +0100190}