summaryrefslogtreecommitdiff
path: root/tools/iio/lsiio.c
blob: c585440f864e365d015d6701601b9fd0ce2b3176 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * 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"


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";


static inline int check_prefix(const char *str, const char *prefix)
{
	return strlen(str) > strlen(prefix) &&
		strncmp(str, prefix, strlen(prefix)) == 0;
}

static inline int check_postfix(const char *str, const char *postfix)
{
	return strlen(str) > strlen(postfix) &&
		strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
}

static int dump_channels(const char *dev_dir_name)
{
	DIR *dp;
	const struct dirent *ent;

	dp = opendir(dev_dir_name);
	if (dp == NULL)
		return -errno;
	while (ent = readdir(dp), ent != NULL)
		if (check_prefix(ent->d_name, "in_") &&
		    check_postfix(ent->d_name, "_raw")) {
			printf("   %-10s\n", ent->d_name);
		}

	return 0;
}

static int dump_one_device(const char *dev_dir_name)
{
	char name[IIO_MAX_NAME_LENGTH];
	int dev_idx;
	int retval;

	retval = sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_device),
			"%i", &dev_idx);
	if (retval != 1)
		return -EINVAL;
	read_sysfs_string("name", dev_dir_name, name);
	printf("Device %03d: %s\n", dev_idx, name);

	if (verblevel >= VERBLEVEL_SENSORS)
		return dump_channels(dev_dir_name);
	return 0;
}

static int dump_one_trigger(const char *dev_dir_name)
{
	char name[IIO_MAX_NAME_LENGTH];
	int dev_idx;
	int retval;

	retval = sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_trigger),
			"%i", &dev_idx);
	if (retval != 1)
		return -EINVAL;
	read_sysfs_string("name", dev_dir_name, name);
	printf("Trigger %03d: %s\n", dev_idx, name);
	return 0;
}

static void dump_devices(void)
{
	const struct dirent *ent;
	DIR *dp;

	dp = opendir(iio_dir);
	if (dp == NULL) {
		printf("No industrial I/O devices available\n");
		return;
	}

	while (ent = readdir(dp), ent != NULL) {
		if (check_prefix(ent->d_name, type_device)) {
			char *dev_dir_name;

			asprintf(&dev_dir_name, "%s%s", iio_dir, ent->d_name);
			dump_one_device(dev_dir_name);
			free(dev_dir_name);
			if (verblevel >= VERBLEVEL_SENSORS)
				printf("\n");
		}
	}
	rewinddir(dp);
	while (ent = readdir(dp), ent != NULL) {
		if (check_prefix(ent->d_name, type_trigger)) {
			char *dev_dir_name;

			asprintf(&dev_dir_name, "%s%s", iio_dir, ent->d_name);
			dump_one_trigger(dev_dir_name);
			free(dev_dir_name);
		}
	}
	closedir(dp);
}

int main(int argc, char **argv)
{
	int c, err = 0;

	while ((c = getopt(argc, argv, "d:D:v")) != EOF) {
		switch (c) {
		case 'v':
			verblevel++;
			break;

		case '?':
		default:
			err++;
			break;
		}
	}
	if (err || argc > optind) {
		fprintf(stderr, "Usage: lsiio [options]...\n"
			"List industrial I/O devices\n"
			"  -v, --verbose\n"
			"      Increase verbosity (may be given multiple times)\n"
			);
		exit(1);
	}

	dump_devices();

	return 0;
}