staging: iio: Add tool to list IIO devices and triggers
[deliverable/linux.git] / drivers / staging / iio / Documentation / lsiio.c
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
23
24 static enum verbosity {
25 VERBLEVEL_DEFAULT, /* 0 gives lspci behaviour */
26 VERBLEVEL_SENSORS, /* 1 lists sensors */
27 } verblevel = VERBLEVEL_DEFAULT;
28
29 const char *type_device = "iio:device";
30 const char *type_trigger = "trigger";
31
32
33 static inline int check_prefix(const char *str, const char *prefix)
34 {
35 return strlen(str) > strlen(prefix) &&
36 strncmp(str, prefix, strlen(prefix)) == 0;
37 }
38
39 static inline int check_postfix(const char *str, const char *postfix)
40 {
41 return strlen(str) > strlen(postfix) &&
42 strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
43 }
44
45 static int dump_channels(const char *dev_dir_name)
46 {
47 DIR *dp;
48 const struct dirent *ent;
49 dp = opendir(dev_dir_name);
50 if (dp == NULL)
51 return -errno;
52 while (ent = readdir(dp), ent != NULL)
53 if (check_prefix(ent->d_name, "in_") &&
54 check_postfix(ent->d_name, "_raw")) {
55 printf(" %-10s\n", ent->d_name);
56 }
57
58 return 0;
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;
65
66 sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_device),
67 "%i", &dev_idx);
68 read_sysfs_string("name", dev_dir_name, name);
69 printf("Device %03d: %s\n", dev_idx, name);
70
71 if (verblevel >= VERBLEVEL_SENSORS) {
72 int ret = dump_channels(dev_dir_name);
73 if (ret)
74 return ret;
75 }
76 return 0;
77 }
78
79 static int dump_one_trigger(const char *dev_dir_name)
80 {
81 char name[IIO_MAX_NAME_LENGTH];
82 int dev_idx;
83
84 sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_trigger),
85 "%i", &dev_idx);
86 read_sysfs_string("name", dev_dir_name, name);
87 printf("Trigger %03d: %s\n", dev_idx, name);
88 return 0;
89 }
90
91 static void dump_devices(void)
92 {
93 const struct dirent *ent;
94 int number, numstrlen;
95
96 FILE *nameFile;
97 DIR *dp;
98 char thisname[IIO_MAX_NAME_LENGTH];
99 char *filename;
100
101 dp = opendir(iio_dir);
102 if (dp == NULL) {
103 printf("No industrial I/O devices available\n");
104 return;
105 }
106
107 while (ent = readdir(dp), ent != NULL) {
108 if (check_prefix(ent->d_name, type_device)) {
109 char *dev_dir_name;
110 asprintf(&dev_dir_name, "%s%s", iio_dir, ent->d_name);
111 dump_one_device(dev_dir_name);
112 free(dev_dir_name);
113 if (verblevel >= VERBLEVEL_SENSORS)
114 printf("\n");
115 }
116 }
117 rewinddir(dp);
118 while (ent = readdir(dp), ent != NULL) {
119 if (check_prefix(ent->d_name, type_trigger)) {
120 char *dev_dir_name;
121 asprintf(&dev_dir_name, "%s%s", iio_dir, ent->d_name);
122 dump_one_trigger(dev_dir_name);
123 free(dev_dir_name);
124 }
125 }
126 closedir(dp);
127 }
128
129 int main(int argc, char **argv)
130 {
131 int c, err = 0;
132
133 while ((c = getopt(argc, argv, "d:D:v")) != EOF) {
134 switch (c) {
135 case 'v':
136 verblevel++;
137 break;
138
139 case '?':
140 default:
141 err++;
142 break;
143 }
144 }
145 if (err || argc > optind) {
146 fprintf(stderr, "Usage: lsiio [options]...\n"
147 "List industrial I/O devices\n"
148 " -v, --verbose\n"
149 " Increase verbosity (may be given multiple times)\n"
150 );
151 exit(1);
152 }
153
154 dump_devices();
155
156 return 0;
157 }
This page took 0.049658 seconds and 5 git commands to generate.