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