Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 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 | |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 23 | static enum verbosity { |
| 24 | VERBLEVEL_DEFAULT, /* 0 gives lspci behaviour */ |
| 25 | VERBLEVEL_SENSORS, /* 1 lists sensors */ |
| 26 | } verblevel = VERBLEVEL_DEFAULT; |
| 27 | |
| 28 | const char *type_device = "iio:device"; |
| 29 | const char *type_trigger = "trigger"; |
| 30 | |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 31 | static inline int check_prefix(const char *str, const char *prefix) |
| 32 | { |
| 33 | return strlen(str) > strlen(prefix) && |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 34 | strncmp(str, prefix, strlen(prefix)) == 0; |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | static inline int check_postfix(const char *str, const char *postfix) |
| 38 | { |
| 39 | return strlen(str) > strlen(postfix) && |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 40 | strcmp(str + strlen(str) - strlen(postfix), postfix) == 0; |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | static int dump_channels(const char *dev_dir_name) |
| 44 | { |
| 45 | DIR *dp; |
| 46 | const struct dirent *ent; |
Heena Sirwani | d1e5041 | 2014-10-06 17:03:31 +0530 | [diff] [blame] | 47 | |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 48 | dp = opendir(dev_dir_name); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 49 | if (!dp) |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 50 | return -errno; |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 51 | |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 52 | while (ent = readdir(dp), ent) |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 53 | if (check_prefix(ent->d_name, "in_") && |
Matt Ranostay | 6df1dc0 | 2016-08-01 19:39:51 -0700 | [diff] [blame] | 54 | (check_postfix(ent->d_name, "_raw") || |
| 55 | check_postfix(ent->d_name, "_input"))) |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 56 | printf(" %-10s\n", ent->d_name); |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 57 | |
Hartmut Knaack | f96d055 | 2015-05-31 14:39:47 +0200 | [diff] [blame] | 58 | return (closedir(dp) == -1) ? -errno : 0; |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 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; |
Hartmut Knaack | a9d7acc | 2015-06-10 21:51:21 +0200 | [diff] [blame] | 65 | int ret; |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 66 | |
Hartmut Knaack | a9d7acc | 2015-06-10 21:51:21 +0200 | [diff] [blame] | 67 | ret = sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_device), "%i", |
| 68 | &dev_idx); |
| 69 | if (ret != 1) |
Heena Sirwani | d0e68ce | 2014-10-06 17:04:43 +0530 | [diff] [blame] | 70 | return -EINVAL; |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 71 | |
Hartmut Knaack | a9d7acc | 2015-06-10 21:51:21 +0200 | [diff] [blame] | 72 | ret = read_sysfs_string("name", dev_dir_name, name); |
Linus Walleij | af255cd | 2015-08-04 16:21:49 +0200 | [diff] [blame] | 73 | if (ret < 0) |
Hartmut Knaack | a9d7acc | 2015-06-10 21:51:21 +0200 | [diff] [blame] | 74 | return ret; |
Hartmut Knaack | acf50b3 | 2015-05-31 14:40:15 +0200 | [diff] [blame] | 75 | |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 76 | printf("Device %03d: %s\n", dev_idx, name); |
| 77 | |
Heena Sirwani | edead9b | 2014-10-06 17:04:10 +0530 | [diff] [blame] | 78 | if (verblevel >= VERBLEVEL_SENSORS) |
| 79 | return dump_channels(dev_dir_name); |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 80 | |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static int dump_one_trigger(const char *dev_dir_name) |
| 85 | { |
| 86 | char name[IIO_MAX_NAME_LENGTH]; |
| 87 | int dev_idx; |
Hartmut Knaack | a9d7acc | 2015-06-10 21:51:21 +0200 | [diff] [blame] | 88 | int ret; |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 89 | |
Hartmut Knaack | a9d7acc | 2015-06-10 21:51:21 +0200 | [diff] [blame] | 90 | ret = sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_trigger), |
| 91 | "%i", &dev_idx); |
| 92 | if (ret != 1) |
Heena Sirwani | d0e68ce | 2014-10-06 17:04:43 +0530 | [diff] [blame] | 93 | return -EINVAL; |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 94 | |
Hartmut Knaack | a9d7acc | 2015-06-10 21:51:21 +0200 | [diff] [blame] | 95 | ret = read_sysfs_string("name", dev_dir_name, name); |
Linus Walleij | af255cd | 2015-08-04 16:21:49 +0200 | [diff] [blame] | 96 | if (ret < 0) |
Hartmut Knaack | a9d7acc | 2015-06-10 21:51:21 +0200 | [diff] [blame] | 97 | return ret; |
Hartmut Knaack | acf50b3 | 2015-05-31 14:40:15 +0200 | [diff] [blame] | 98 | |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 99 | printf("Trigger %03d: %s\n", dev_idx, name); |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 100 | |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 101 | return 0; |
| 102 | } |
| 103 | |
Hartmut Knaack | acf50b3 | 2015-05-31 14:40:15 +0200 | [diff] [blame] | 104 | static int dump_devices(void) |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 105 | { |
| 106 | const struct dirent *ent; |
Hartmut Knaack | acf50b3 | 2015-05-31 14:40:15 +0200 | [diff] [blame] | 107 | int ret; |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 108 | DIR *dp; |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 109 | |
| 110 | dp = opendir(iio_dir); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 111 | if (!dp) { |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 112 | fprintf(stderr, "No industrial I/O devices available\n"); |
Hartmut Knaack | acf50b3 | 2015-05-31 14:40:15 +0200 | [diff] [blame] | 113 | return -ENODEV; |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 114 | } |
| 115 | |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 116 | while (ent = readdir(dp), ent) { |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 117 | if (check_prefix(ent->d_name, type_device)) { |
| 118 | char *dev_dir_name; |
Heena Sirwani | d1e5041 | 2014-10-06 17:03:31 +0530 | [diff] [blame] | 119 | |
Hartmut Knaack | e9e45b4 | 2015-05-31 14:40:02 +0200 | [diff] [blame] | 120 | if (asprintf(&dev_dir_name, "%s%s", iio_dir, |
| 121 | ent->d_name) < 0) { |
Hartmut Knaack | acf50b3 | 2015-05-31 14:40:15 +0200 | [diff] [blame] | 122 | ret = -ENOMEM; |
Hartmut Knaack | e9e45b4 | 2015-05-31 14:40:02 +0200 | [diff] [blame] | 123 | goto error_close_dir; |
| 124 | } |
| 125 | |
Hartmut Knaack | acf50b3 | 2015-05-31 14:40:15 +0200 | [diff] [blame] | 126 | ret = dump_one_device(dev_dir_name); |
| 127 | if (ret) { |
| 128 | free(dev_dir_name); |
| 129 | goto error_close_dir; |
| 130 | } |
| 131 | |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 132 | free(dev_dir_name); |
| 133 | if (verblevel >= VERBLEVEL_SENSORS) |
| 134 | printf("\n"); |
| 135 | } |
| 136 | } |
| 137 | rewinddir(dp); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 138 | while (ent = readdir(dp), ent) { |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 139 | if (check_prefix(ent->d_name, type_trigger)) { |
| 140 | char *dev_dir_name; |
Heena Sirwani | d1e5041 | 2014-10-06 17:03:31 +0530 | [diff] [blame] | 141 | |
Hartmut Knaack | e9e45b4 | 2015-05-31 14:40:02 +0200 | [diff] [blame] | 142 | if (asprintf(&dev_dir_name, "%s%s", iio_dir, |
| 143 | ent->d_name) < 0) { |
Hartmut Knaack | acf50b3 | 2015-05-31 14:40:15 +0200 | [diff] [blame] | 144 | ret = -ENOMEM; |
Hartmut Knaack | e9e45b4 | 2015-05-31 14:40:02 +0200 | [diff] [blame] | 145 | goto error_close_dir; |
| 146 | } |
| 147 | |
Hartmut Knaack | acf50b3 | 2015-05-31 14:40:15 +0200 | [diff] [blame] | 148 | ret = dump_one_trigger(dev_dir_name); |
| 149 | if (ret) { |
| 150 | free(dev_dir_name); |
| 151 | goto error_close_dir; |
| 152 | } |
| 153 | |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 154 | free(dev_dir_name); |
| 155 | } |
| 156 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 157 | |
Hartmut Knaack | acf50b3 | 2015-05-31 14:40:15 +0200 | [diff] [blame] | 158 | return (closedir(dp) == -1) ? -errno : 0; |
| 159 | |
Hartmut Knaack | e9e45b4 | 2015-05-31 14:40:02 +0200 | [diff] [blame] | 160 | error_close_dir: |
Hartmut Knaack | acf50b3 | 2015-05-31 14:40:15 +0200 | [diff] [blame] | 161 | if (closedir(dp) == -1) |
| 162 | perror("dump_devices(): Failed to close directory"); |
| 163 | |
| 164 | return ret; |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | int main(int argc, char **argv) |
| 168 | { |
| 169 | int c, err = 0; |
| 170 | |
Hartmut Knaack | e06e3d7 | 2015-05-31 14:40:22 +0200 | [diff] [blame] | 171 | while ((c = getopt(argc, argv, "v")) != EOF) { |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 172 | 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 Knaack | e06e3d7 | 2015-05-31 14:40:22 +0200 | [diff] [blame] | 186 | " -v Increase verbosity (may be given multiple times)\n"); |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 187 | exit(1); |
| 188 | } |
| 189 | |
Hartmut Knaack | acf50b3 | 2015-05-31 14:40:15 +0200 | [diff] [blame] | 190 | return dump_devices(); |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 191 | } |