Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 2 | /* Industrialio buffer test code. |
| 3 | * |
| 4 | * Copyright (c) 2008 Jonathan Cameron |
| 5 | * |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 6 | * This program is primarily intended as an example application. |
| 7 | * Reads the current buffer setup from sysfs and starts a short capture |
| 8 | * from the specified device, pretty printing the result after appropriate |
| 9 | * conversion. |
| 10 | * |
| 11 | * Command line parameters |
| 12 | * generic_buffer -n <device_name> -t <trigger_name> |
| 13 | * If trigger name is not specified the program assumes you want a dataready |
| 14 | * trigger associated with the device and goes looking for it. |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <unistd.h> |
Roberta Dobrescu | bdcb31d | 2015-02-26 10:49:24 +0200 | [diff] [blame] | 18 | #include <stdlib.h> |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 19 | #include <dirent.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <stdio.h> |
| 22 | #include <errno.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/dir.h> |
| 25 | #include <linux/types.h> |
Jonathan Cameron | 30268a3 | 2011-02-11 13:09:12 +0000 | [diff] [blame] | 26 | #include <string.h> |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 27 | #include <poll.h> |
Jonathan Cameron | 117cf8b | 2011-12-04 19:10:59 +0000 | [diff] [blame] | 28 | #include <endian.h> |
Peter Meerwald | bb23378 | 2012-06-25 23:12:17 +0200 | [diff] [blame] | 29 | #include <getopt.h> |
Peter Meerwald | 1bcdfbc | 2012-06-25 23:12:16 +0200 | [diff] [blame] | 30 | #include <inttypes.h> |
Crestez Dan Leonard | 4f20d59 | 2016-05-23 21:39:56 +0300 | [diff] [blame] | 31 | #include <stdbool.h> |
| 32 | #include <signal.h> |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 33 | #include "iio_utils.h" |
| 34 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 35 | /** |
Linus Walleij | 5f991a9 | 2016-04-14 10:26:47 +0200 | [diff] [blame] | 36 | * enum autochan - state for the automatic channel enabling mechanism |
| 37 | */ |
| 38 | enum autochan { |
| 39 | AUTOCHANNELS_DISABLED, |
| 40 | AUTOCHANNELS_ENABLED, |
| 41 | AUTOCHANNELS_ACTIVE, |
| 42 | }; |
| 43 | |
| 44 | /** |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 45 | * size_from_channelarray() - calculate the storage size of a scan |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 46 | * @channels: the channel info array |
| 47 | * @num_channels: number of channels |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 48 | * |
| 49 | * Has the side effect of filling the channels[i].location values used |
| 50 | * in processing the buffer output. |
| 51 | **/ |
| 52 | int size_from_channelarray(struct iio_channel_info *channels, int num_channels) |
| 53 | { |
| 54 | int bytes = 0; |
| 55 | int i = 0; |
Melike Yurtoglu | ff39a25 | 2014-10-03 23:34:50 +0300 | [diff] [blame] | 56 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 57 | while (i < num_channels) { |
| 58 | if (bytes % channels[i].bytes == 0) |
| 59 | channels[i].location = bytes; |
| 60 | else |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 61 | channels[i].location = bytes - bytes % channels[i].bytes |
| 62 | + channels[i].bytes; |
| 63 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 64 | bytes = channels[i].location + channels[i].bytes; |
| 65 | i++; |
| 66 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 67 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 68 | return bytes; |
| 69 | } |
| 70 | |
Tiberiu Breana | e8d0927 | 2015-07-03 12:57:36 +0300 | [diff] [blame] | 71 | void print1byte(uint8_t input, struct iio_channel_info *info) |
| 72 | { |
| 73 | /* |
| 74 | * Shift before conversion to avoid sign extension |
| 75 | * of left aligned data |
| 76 | */ |
| 77 | input >>= info->shift; |
| 78 | input &= info->mask; |
| 79 | if (info->is_signed) { |
| 80 | int8_t val = (int8_t)(input << (8 - info->bits_used)) >> |
| 81 | (8 - info->bits_used); |
| 82 | printf("%05f ", ((float)val + info->offset) * info->scale); |
| 83 | } else { |
| 84 | printf("%05f ", ((float)input + info->offset) * info->scale); |
| 85 | } |
| 86 | } |
| 87 | |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 88 | void print2byte(uint16_t input, struct iio_channel_info *info) |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 89 | { |
Jonathan Cameron | 117cf8b | 2011-12-04 19:10:59 +0000 | [diff] [blame] | 90 | /* First swap if incorrect endian */ |
Jonathan Cameron | 117cf8b | 2011-12-04 19:10:59 +0000 | [diff] [blame] | 91 | if (info->be) |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 92 | input = be16toh(input); |
Jonathan Cameron | 117cf8b | 2011-12-04 19:10:59 +0000 | [diff] [blame] | 93 | else |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 94 | input = le16toh(input); |
Jonathan Cameron | 117cf8b | 2011-12-04 19:10:59 +0000 | [diff] [blame] | 95 | |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 96 | /* |
| 97 | * Shift before conversion to avoid sign extension |
| 98 | * of left aligned data |
| 99 | */ |
Aya Mahfouz | 1525ecf | 2015-02-26 11:45:26 +0200 | [diff] [blame] | 100 | input >>= info->shift; |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 101 | input &= info->mask; |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 102 | if (info->is_signed) { |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 103 | int16_t val = (int16_t)(input << (16 - info->bits_used)) >> |
| 104 | (16 - info->bits_used); |
| 105 | printf("%05f ", ((float)val + info->offset) * info->scale); |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 106 | } else { |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 107 | printf("%05f ", ((float)input + info->offset) * info->scale); |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 108 | } |
| 109 | } |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 110 | |
| 111 | void print4byte(uint32_t input, struct iio_channel_info *info) |
| 112 | { |
| 113 | /* First swap if incorrect endian */ |
| 114 | if (info->be) |
| 115 | input = be32toh(input); |
| 116 | else |
| 117 | input = le32toh(input); |
| 118 | |
| 119 | /* |
| 120 | * Shift before conversion to avoid sign extension |
| 121 | * of left aligned data |
| 122 | */ |
| 123 | input >>= info->shift; |
| 124 | input &= info->mask; |
| 125 | if (info->is_signed) { |
| 126 | int32_t val = (int32_t)(input << (32 - info->bits_used)) >> |
| 127 | (32 - info->bits_used); |
| 128 | printf("%05f ", ((float)val + info->offset) * info->scale); |
| 129 | } else { |
| 130 | printf("%05f ", ((float)input + info->offset) * info->scale); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | void print8byte(uint64_t input, struct iio_channel_info *info) |
| 135 | { |
| 136 | /* First swap if incorrect endian */ |
| 137 | if (info->be) |
| 138 | input = be64toh(input); |
| 139 | else |
| 140 | input = le64toh(input); |
| 141 | |
| 142 | /* |
| 143 | * Shift before conversion to avoid sign extension |
| 144 | * of left aligned data |
| 145 | */ |
| 146 | input >>= info->shift; |
| 147 | input &= info->mask; |
| 148 | if (info->is_signed) { |
| 149 | int64_t val = (int64_t)(input << (64 - info->bits_used)) >> |
| 150 | (64 - info->bits_used); |
| 151 | /* special case for timestamp */ |
| 152 | if (info->scale == 1.0f && info->offset == 0.0f) |
| 153 | printf("%" PRId64 " ", val); |
| 154 | else |
| 155 | printf("%05f ", |
| 156 | ((float)val + info->offset) * info->scale); |
| 157 | } else { |
| 158 | printf("%05f ", ((float)input + info->offset) * info->scale); |
| 159 | } |
| 160 | } |
| 161 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 162 | /** |
| 163 | * process_scan() - print out the values in SI units |
| 164 | * @data: pointer to the start of the scan |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 165 | * @channels: information about the channels. |
| 166 | * Note: size_from_channelarray must have been called first |
| 167 | * to fill the location offsets. |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 168 | * @num_channels: number of channels |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 169 | **/ |
| 170 | void process_scan(char *data, |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 171 | struct iio_channel_info *channels, |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 172 | int num_channels) |
| 173 | { |
| 174 | int k; |
Melike Yurtoglu | ff39a25 | 2014-10-03 23:34:50 +0300 | [diff] [blame] | 175 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 176 | for (k = 0; k < num_channels; k++) |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 177 | switch (channels[k].bytes) { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 178 | /* only a few cases implemented so far */ |
Tiberiu Breana | e8d0927 | 2015-07-03 12:57:36 +0300 | [diff] [blame] | 179 | case 1: |
| 180 | print1byte(*(uint8_t *)(data + channels[k].location), |
| 181 | &channels[k]); |
| 182 | break; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 183 | case 2: |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 184 | print2byte(*(uint16_t *)(data + channels[k].location), |
| 185 | &channels[k]); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 186 | break; |
Marek Vasut | 6cffc1f | 2012-08-12 16:21:00 +0100 | [diff] [blame] | 187 | case 4: |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 188 | print4byte(*(uint32_t *)(data + channels[k].location), |
| 189 | &channels[k]); |
Marek Vasut | 6cffc1f | 2012-08-12 16:21:00 +0100 | [diff] [blame] | 190 | break; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 191 | case 8: |
Hartmut Knaack | 8e92613 | 2015-05-31 14:39:58 +0200 | [diff] [blame] | 192 | print8byte(*(uint64_t *)(data + channels[k].location), |
| 193 | &channels[k]); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 194 | break; |
| 195 | default: |
| 196 | break; |
| 197 | } |
| 198 | printf("\n"); |
| 199 | } |
| 200 | |
Linus Walleij | 5f991a9 | 2016-04-14 10:26:47 +0200 | [diff] [blame] | 201 | static int enable_disable_all_channels(char *dev_dir_name, int enable) |
| 202 | { |
| 203 | const struct dirent *ent; |
| 204 | char scanelemdir[256]; |
| 205 | DIR *dp; |
| 206 | int ret; |
| 207 | |
| 208 | snprintf(scanelemdir, sizeof(scanelemdir), |
| 209 | FORMAT_SCAN_ELEMENTS_DIR, dev_dir_name); |
| 210 | scanelemdir[sizeof(scanelemdir)-1] = '\0'; |
| 211 | |
| 212 | dp = opendir(scanelemdir); |
| 213 | if (!dp) { |
| 214 | fprintf(stderr, "Enabling/disabling channels: can't open %s\n", |
| 215 | scanelemdir); |
| 216 | return -EIO; |
| 217 | } |
| 218 | |
| 219 | ret = -ENOENT; |
| 220 | while (ent = readdir(dp), ent) { |
| 221 | if (iioutils_check_suffix(ent->d_name, "_en")) { |
| 222 | printf("%sabling: %s\n", |
| 223 | enable ? "En" : "Dis", |
| 224 | ent->d_name); |
| 225 | ret = write_sysfs_int(ent->d_name, scanelemdir, |
| 226 | enable); |
| 227 | if (ret < 0) |
| 228 | fprintf(stderr, "Failed to enable/disable %s\n", |
| 229 | ent->d_name); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | if (closedir(dp) == -1) { |
| 234 | perror("Enabling/disabling channels: " |
| 235 | "Failed to close directory"); |
| 236 | return -errno; |
| 237 | } |
| 238 | return 0; |
| 239 | } |
| 240 | |
Hartmut Knaack | e06e3d7 | 2015-05-31 14:40:22 +0200 | [diff] [blame] | 241 | void print_usage(void) |
| 242 | { |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 243 | fprintf(stderr, "Usage: generic_buffer [options]...\n" |
| 244 | "Capture, convert and output data from IIO device buffer\n" |
Linus Walleij | 5f991a9 | 2016-04-14 10:26:47 +0200 | [diff] [blame] | 245 | " -a Auto-activate all available channels\n" |
Eva Rachel Retuya | 4e2cf0e | 2016-10-14 18:18:39 +0800 | [diff] [blame] | 246 | " -A Force-activate ALL channels\n" |
Martin Kelly | 55dda0a | 2018-05-17 17:14:46 -0700 | [diff] [blame] | 247 | " -c <n> Do n conversions, or loop forever if n < 0\n" |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 248 | " -e Disable wait for event (new data)\n" |
| 249 | " -g Use trigger-less mode\n" |
| 250 | " -l <n> Set buffer length to n samples\n" |
Crestez Dan Leonard | de397db | 2016-05-23 21:39:57 +0300 | [diff] [blame] | 251 | " --device-name -n <name>\n" |
| 252 | " --device-num -N <num>\n" |
| 253 | " Set device by name or number (mandatory)\n" |
Crestez Dan Leonard | 7c7e9da | 2016-05-23 21:39:58 +0300 | [diff] [blame] | 254 | " --trigger-name -t <name>\n" |
| 255 | " --trigger-num -T <num>\n" |
| 256 | " Set trigger by name or number\n" |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 257 | " -w <n> Set delay between reads in us (event-less mode)\n"); |
Hartmut Knaack | e06e3d7 | 2015-05-31 14:40:22 +0200 | [diff] [blame] | 258 | } |
| 259 | |
Crestez Dan Leonard | 4f20d59 | 2016-05-23 21:39:56 +0300 | [diff] [blame] | 260 | enum autochan autochannels = AUTOCHANNELS_DISABLED; |
| 261 | char *dev_dir_name = NULL; |
| 262 | char *buf_dir_name = NULL; |
| 263 | bool current_trigger_set = false; |
| 264 | |
| 265 | void cleanup(void) |
| 266 | { |
| 267 | int ret; |
| 268 | |
| 269 | /* Disable trigger */ |
| 270 | if (dev_dir_name && current_trigger_set) { |
| 271 | /* Disconnect the trigger - just write a dummy name. */ |
| 272 | ret = write_sysfs_string("trigger/current_trigger", |
| 273 | dev_dir_name, "NULL"); |
| 274 | if (ret < 0) |
| 275 | fprintf(stderr, "Failed to disable trigger: %s\n", |
| 276 | strerror(-ret)); |
| 277 | current_trigger_set = false; |
| 278 | } |
| 279 | |
| 280 | /* Disable buffer */ |
| 281 | if (buf_dir_name) { |
| 282 | ret = write_sysfs_int("enable", buf_dir_name, 0); |
| 283 | if (ret < 0) |
| 284 | fprintf(stderr, "Failed to disable buffer: %s\n", |
| 285 | strerror(-ret)); |
| 286 | } |
| 287 | |
| 288 | /* Disable channels if auto-enabled */ |
| 289 | if (dev_dir_name && autochannels == AUTOCHANNELS_ACTIVE) { |
| 290 | ret = enable_disable_all_channels(dev_dir_name, 0); |
| 291 | if (ret) |
| 292 | fprintf(stderr, "Failed to disable all channels\n"); |
| 293 | autochannels = AUTOCHANNELS_DISABLED; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | void sig_handler(int signum) |
| 298 | { |
| 299 | fprintf(stderr, "Caught signal %d\n", signum); |
| 300 | cleanup(); |
| 301 | exit(-signum); |
| 302 | } |
| 303 | |
| 304 | void register_cleanup(void) |
| 305 | { |
| 306 | struct sigaction sa = { .sa_handler = sig_handler }; |
| 307 | const int signums[] = { SIGINT, SIGTERM, SIGABRT }; |
| 308 | int ret, i; |
| 309 | |
| 310 | for (i = 0; i < ARRAY_SIZE(signums); ++i) { |
| 311 | ret = sigaction(signums[i], &sa, NULL); |
| 312 | if (ret) { |
| 313 | perror("Failed to register signal handler"); |
| 314 | exit(-1); |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
Crestez Dan Leonard | de397db | 2016-05-23 21:39:57 +0300 | [diff] [blame] | 319 | static const struct option longopts[] = { |
| 320 | { "device-name", 1, 0, 'n' }, |
| 321 | { "device-num", 1, 0, 'N' }, |
Crestez Dan Leonard | 7c7e9da | 2016-05-23 21:39:58 +0300 | [diff] [blame] | 322 | { "trigger-name", 1, 0, 't' }, |
| 323 | { "trigger-num", 1, 0, 'T' }, |
Crestez Dan Leonard | de397db | 2016-05-23 21:39:57 +0300 | [diff] [blame] | 324 | { }, |
| 325 | }; |
| 326 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 327 | int main(int argc, char **argv) |
| 328 | { |
Martin Kelly | b119d3b | 2019-01-11 23:13:09 +0000 | [diff] [blame] | 329 | long long num_loops = 2; |
Jonathan Cameron | 96df979 | 2011-02-11 13:09:13 +0000 | [diff] [blame] | 330 | unsigned long timedelay = 1000000; |
| 331 | unsigned long buf_len = 128; |
| 332 | |
Martin Kelly | 71b52d2 | 2018-05-17 17:14:45 -0700 | [diff] [blame] | 333 | ssize_t i; |
Martin Kelly | 55dda0a | 2018-05-17 17:14:46 -0700 | [diff] [blame] | 334 | unsigned long long j; |
Martin Kelly | 71b52d2 | 2018-05-17 17:14:45 -0700 | [diff] [blame] | 335 | unsigned long toread; |
| 336 | int ret, c; |
Crestez Dan Leonard | 4f20d59 | 2016-05-23 21:39:56 +0300 | [diff] [blame] | 337 | int fp = -1; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 338 | |
Crestez Dan Leonard | 4f20d59 | 2016-05-23 21:39:56 +0300 | [diff] [blame] | 339 | int num_channels = 0; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 340 | char *trigger_name = NULL, *device_name = NULL; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 341 | |
Crestez Dan Leonard | 4f20d59 | 2016-05-23 21:39:56 +0300 | [diff] [blame] | 342 | char *data = NULL; |
Jonathan Cameron | c77b381 | 2011-04-15 18:56:00 +0100 | [diff] [blame] | 343 | ssize_t read_size; |
Crestez Dan Leonard | deb4d1f | 2016-06-03 21:56:29 +0300 | [diff] [blame] | 344 | int dev_num = -1, trig_num = -1; |
Crestez Dan Leonard | 4f20d59 | 2016-05-23 21:39:56 +0300 | [diff] [blame] | 345 | char *buffer_access = NULL; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 346 | int scan_size; |
Jonathan Cameron | 30268a3 | 2011-02-11 13:09:12 +0000 | [diff] [blame] | 347 | int noevents = 0; |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 348 | int notrigger = 0; |
Jonathan Cameron | 96df979 | 2011-02-11 13:09:13 +0000 | [diff] [blame] | 349 | char *dummy; |
Eva Rachel Retuya | 71ccbe5 | 2016-10-20 17:19:23 +0800 | [diff] [blame] | 350 | bool force_autochannels = false; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 351 | |
Alison Schofield | ddbc719 | 2016-07-26 20:17:43 -0700 | [diff] [blame] | 352 | struct iio_channel_info *channels = NULL; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 353 | |
Crestez Dan Leonard | 4f20d59 | 2016-05-23 21:39:56 +0300 | [diff] [blame] | 354 | register_cleanup(); |
| 355 | |
Eva Rachel Retuya | 4e2cf0e | 2016-10-14 18:18:39 +0800 | [diff] [blame] | 356 | while ((c = getopt_long(argc, argv, "aAc:egl:n:N:t:T:w:?", longopts, |
| 357 | NULL)) != -1) { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 358 | switch (c) { |
Linus Walleij | 5f991a9 | 2016-04-14 10:26:47 +0200 | [diff] [blame] | 359 | case 'a': |
| 360 | autochannels = AUTOCHANNELS_ENABLED; |
| 361 | break; |
Eva Rachel Retuya | 4e2cf0e | 2016-10-14 18:18:39 +0800 | [diff] [blame] | 362 | case 'A': |
| 363 | autochannels = AUTOCHANNELS_ENABLED; |
Eva Rachel Retuya | 71ccbe5 | 2016-10-20 17:19:23 +0800 | [diff] [blame] | 364 | force_autochannels = true; |
Eva Rachel Retuya | 4e2cf0e | 2016-10-14 18:18:39 +0800 | [diff] [blame] | 365 | break; |
Jonathan Cameron | 96df979 | 2011-02-11 13:09:13 +0000 | [diff] [blame] | 366 | case 'c': |
Hartmut Knaack | c8ce990 | 2015-05-31 14:40:03 +0200 | [diff] [blame] | 367 | errno = 0; |
Martin Kelly | 55dda0a | 2018-05-17 17:14:46 -0700 | [diff] [blame] | 368 | num_loops = strtoll(optarg, &dummy, 10); |
Crestez Dan Leonard | 4f20d59 | 2016-05-23 21:39:56 +0300 | [diff] [blame] | 369 | if (errno) { |
| 370 | ret = -errno; |
| 371 | goto error; |
| 372 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 373 | |
Jonathan Cameron | 96df979 | 2011-02-11 13:09:13 +0000 | [diff] [blame] | 374 | break; |
Hartmut Knaack | e06e3d7 | 2015-05-31 14:40:22 +0200 | [diff] [blame] | 375 | case 'e': |
| 376 | noevents = 1; |
| 377 | break; |
| 378 | case 'g': |
| 379 | notrigger = 1; |
Jonathan Cameron | 96df979 | 2011-02-11 13:09:13 +0000 | [diff] [blame] | 380 | break; |
| 381 | case 'l': |
Hartmut Knaack | c8ce990 | 2015-05-31 14:40:03 +0200 | [diff] [blame] | 382 | errno = 0; |
Jonathan Cameron | 96df979 | 2011-02-11 13:09:13 +0000 | [diff] [blame] | 383 | buf_len = strtoul(optarg, &dummy, 10); |
Crestez Dan Leonard | 4f20d59 | 2016-05-23 21:39:56 +0300 | [diff] [blame] | 384 | if (errno) { |
| 385 | ret = -errno; |
| 386 | goto error; |
| 387 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 388 | |
Jonathan Cameron | 96df979 | 2011-02-11 13:09:13 +0000 | [diff] [blame] | 389 | break; |
Hartmut Knaack | e06e3d7 | 2015-05-31 14:40:22 +0200 | [diff] [blame] | 390 | case 'n': |
Crestez Dan Leonard | de397db | 2016-05-23 21:39:57 +0300 | [diff] [blame] | 391 | device_name = strdup(optarg); |
| 392 | break; |
| 393 | case 'N': |
| 394 | errno = 0; |
| 395 | dev_num = strtoul(optarg, &dummy, 10); |
| 396 | if (errno) { |
| 397 | ret = -errno; |
| 398 | goto error; |
| 399 | } |
Hartmut Knaack | e06e3d7 | 2015-05-31 14:40:22 +0200 | [diff] [blame] | 400 | break; |
| 401 | case 't': |
Crestez Dan Leonard | 4f20d59 | 2016-05-23 21:39:56 +0300 | [diff] [blame] | 402 | trigger_name = strdup(optarg); |
Hartmut Knaack | e06e3d7 | 2015-05-31 14:40:22 +0200 | [diff] [blame] | 403 | break; |
Crestez Dan Leonard | 7c7e9da | 2016-05-23 21:39:58 +0300 | [diff] [blame] | 404 | case 'T': |
| 405 | errno = 0; |
| 406 | trig_num = strtoul(optarg, &dummy, 10); |
| 407 | if (errno) |
| 408 | return -errno; |
| 409 | break; |
Hartmut Knaack | e06e3d7 | 2015-05-31 14:40:22 +0200 | [diff] [blame] | 410 | case 'w': |
| 411 | errno = 0; |
| 412 | timedelay = strtoul(optarg, &dummy, 10); |
Crestez Dan Leonard | 4f20d59 | 2016-05-23 21:39:56 +0300 | [diff] [blame] | 413 | if (errno) { |
| 414 | ret = -errno; |
| 415 | goto error; |
| 416 | } |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 417 | break; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 418 | case '?': |
Hartmut Knaack | e06e3d7 | 2015-05-31 14:40:22 +0200 | [diff] [blame] | 419 | print_usage(); |
Crestez Dan Leonard | 4f20d59 | 2016-05-23 21:39:56 +0300 | [diff] [blame] | 420 | ret = -1; |
| 421 | goto error; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | |
| 425 | /* Find the device requested */ |
Crestez Dan Leonard | de397db | 2016-05-23 21:39:57 +0300 | [diff] [blame] | 426 | if (dev_num < 0 && !device_name) { |
| 427 | fprintf(stderr, "Device not set\n"); |
| 428 | print_usage(); |
| 429 | ret = -1; |
Crestez Dan Leonard | 4f20d59 | 2016-05-23 21:39:56 +0300 | [diff] [blame] | 430 | goto error; |
Crestez Dan Leonard | de397db | 2016-05-23 21:39:57 +0300 | [diff] [blame] | 431 | } else if (dev_num >= 0 && device_name) { |
| 432 | fprintf(stderr, "Only one of --device-num or --device-name needs to be set\n"); |
| 433 | print_usage(); |
| 434 | ret = -1; |
| 435 | goto error; |
| 436 | } else if (dev_num < 0) { |
| 437 | dev_num = find_type_by_name(device_name, "iio:device"); |
| 438 | if (dev_num < 0) { |
| 439 | fprintf(stderr, "Failed to find the %s\n", device_name); |
| 440 | ret = dev_num; |
| 441 | goto error; |
| 442 | } |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 443 | } |
| 444 | printf("iio device number being used is %d\n", dev_num); |
| 445 | |
Hartmut Knaack | e9e45b4 | 2015-05-31 14:40:02 +0200 | [diff] [blame] | 446 | ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num); |
Crestez Dan Leonard | de397db | 2016-05-23 21:39:57 +0300 | [diff] [blame] | 447 | if (ret < 0) |
| 448 | return -ENOMEM; |
| 449 | /* Fetch device_name if specified by number */ |
| 450 | if (!device_name) { |
| 451 | device_name = malloc(IIO_MAX_NAME_LENGTH); |
| 452 | if (!device_name) { |
| 453 | ret = -ENOMEM; |
| 454 | goto error; |
| 455 | } |
| 456 | ret = read_sysfs_string("name", dev_dir_name, device_name); |
| 457 | if (ret < 0) { |
| 458 | fprintf(stderr, "Failed to read name of device %d\n", dev_num); |
| 459 | goto error; |
| 460 | } |
Crestez Dan Leonard | 4f20d59 | 2016-05-23 21:39:56 +0300 | [diff] [blame] | 461 | } |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 462 | |
Crestez Dan Leonard | 7c7e9da | 2016-05-23 21:39:58 +0300 | [diff] [blame] | 463 | if (notrigger) { |
| 464 | printf("trigger-less mode selected\n"); |
Gregor Boirie | f8e81d7 | 2016-08-24 15:28:27 +0200 | [diff] [blame] | 465 | } else if (trig_num >= 0) { |
Crestez Dan Leonard | 7c7e9da | 2016-05-23 21:39:58 +0300 | [diff] [blame] | 466 | char *trig_dev_name; |
| 467 | ret = asprintf(&trig_dev_name, "%strigger%d", iio_dir, trig_num); |
| 468 | if (ret < 0) { |
| 469 | return -ENOMEM; |
| 470 | } |
| 471 | trigger_name = malloc(IIO_MAX_NAME_LENGTH); |
| 472 | ret = read_sysfs_string("name", trig_dev_name, trigger_name); |
| 473 | free(trig_dev_name); |
| 474 | if (ret < 0) { |
| 475 | fprintf(stderr, "Failed to read trigger%d name from\n", trig_num); |
| 476 | return ret; |
| 477 | } |
| 478 | printf("iio trigger number being used is %d\n", trig_num); |
| 479 | } else { |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 480 | if (!trigger_name) { |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 481 | /* |
| 482 | * Build the trigger name. If it is device associated |
| 483 | * its name is <device_name>_dev[n] where n matches |
| 484 | * the device number found above. |
| 485 | */ |
| 486 | ret = asprintf(&trigger_name, |
| 487 | "%s-dev%d", device_name, dev_num); |
| 488 | if (ret < 0) { |
| 489 | ret = -ENOMEM; |
Crestez Dan Leonard | 4f20d59 | 2016-05-23 21:39:56 +0300 | [diff] [blame] | 490 | goto error; |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
Linus Walleij | 793d6b5 | 2016-03-24 09:39:14 +0100 | [diff] [blame] | 494 | /* Look for this "-devN" trigger */ |
| 495 | trig_num = find_type_by_name(trigger_name, "trigger"); |
| 496 | if (trig_num < 0) { |
| 497 | /* OK try the simpler "-trigger" suffix instead */ |
| 498 | free(trigger_name); |
| 499 | ret = asprintf(&trigger_name, |
| 500 | "%s-trigger", device_name); |
| 501 | if (ret < 0) { |
| 502 | ret = -ENOMEM; |
Crestez Dan Leonard | 4f20d59 | 2016-05-23 21:39:56 +0300 | [diff] [blame] | 503 | goto error; |
Linus Walleij | 793d6b5 | 2016-03-24 09:39:14 +0100 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 507 | trig_num = find_type_by_name(trigger_name, "trigger"); |
| 508 | if (trig_num < 0) { |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 509 | fprintf(stderr, "Failed to find the trigger %s\n", |
| 510 | trigger_name); |
Hartmut Knaack | e83a47c | 2015-05-31 14:39:57 +0200 | [diff] [blame] | 511 | ret = trig_num; |
Crestez Dan Leonard | 4f20d59 | 2016-05-23 21:39:56 +0300 | [diff] [blame] | 512 | goto error; |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 513 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 514 | |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 515 | printf("iio trigger number being used is %d\n", trig_num); |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 516 | } |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 517 | |
| 518 | /* |
| 519 | * Parse the files in scan_elements to identify what channels are |
| 520 | * present |
| 521 | */ |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 522 | ret = build_channel_array(dev_dir_name, &channels, &num_channels); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 523 | if (ret) { |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 524 | fprintf(stderr, "Problem reading scan element information\n" |
| 525 | "diag %s\n", dev_dir_name); |
Crestez Dan Leonard | 4f20d59 | 2016-05-23 21:39:56 +0300 | [diff] [blame] | 526 | goto error; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 527 | } |
Eva Rachel Retuya | 7c5fe41 | 2016-10-20 17:19:24 +0800 | [diff] [blame] | 528 | if (num_channels && autochannels == AUTOCHANNELS_ENABLED && |
Eva Rachel Retuya | 71ccbe5 | 2016-10-20 17:19:23 +0800 | [diff] [blame] | 529 | !force_autochannels) { |
Linus Walleij | 5f991a9 | 2016-04-14 10:26:47 +0200 | [diff] [blame] | 530 | fprintf(stderr, "Auto-channels selected but some channels " |
| 531 | "are already activated in sysfs\n"); |
| 532 | fprintf(stderr, "Proceeding without activating any channels\n"); |
| 533 | } |
| 534 | |
Eva Rachel Retuya | 4e2cf0e | 2016-10-14 18:18:39 +0800 | [diff] [blame] | 535 | if ((!num_channels && autochannels == AUTOCHANNELS_ENABLED) || |
Eva Rachel Retuya | 7c5fe41 | 2016-10-20 17:19:24 +0800 | [diff] [blame] | 536 | (autochannels == AUTOCHANNELS_ENABLED && force_autochannels)) { |
Eva Rachel Retuya | 4e2cf0e | 2016-10-14 18:18:39 +0800 | [diff] [blame] | 537 | fprintf(stderr, "Enabling all channels\n"); |
Linus Walleij | 5f991a9 | 2016-04-14 10:26:47 +0200 | [diff] [blame] | 538 | |
| 539 | ret = enable_disable_all_channels(dev_dir_name, 1); |
| 540 | if (ret) { |
| 541 | fprintf(stderr, "Failed to enable all channels\n"); |
Crestez Dan Leonard | 4f20d59 | 2016-05-23 21:39:56 +0300 | [diff] [blame] | 542 | goto error; |
Linus Walleij | 5f991a9 | 2016-04-14 10:26:47 +0200 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | /* This flags that we need to disable the channels again */ |
| 546 | autochannels = AUTOCHANNELS_ACTIVE; |
| 547 | |
| 548 | ret = build_channel_array(dev_dir_name, &channels, |
| 549 | &num_channels); |
| 550 | if (ret) { |
| 551 | fprintf(stderr, "Problem reading scan element " |
| 552 | "information\n" |
| 553 | "diag %s\n", dev_dir_name); |
Crestez Dan Leonard | 4f20d59 | 2016-05-23 21:39:56 +0300 | [diff] [blame] | 554 | goto error; |
Linus Walleij | 5f991a9 | 2016-04-14 10:26:47 +0200 | [diff] [blame] | 555 | } |
| 556 | if (!num_channels) { |
| 557 | fprintf(stderr, "Still no channels after " |
| 558 | "auto-enabling, giving up\n"); |
Crestez Dan Leonard | 4f20d59 | 2016-05-23 21:39:56 +0300 | [diff] [blame] | 559 | goto error; |
Linus Walleij | 5f991a9 | 2016-04-14 10:26:47 +0200 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | |
| 563 | if (!num_channels && autochannels == AUTOCHANNELS_DISABLED) { |
Linus Walleij | 53dabaf | 2015-08-10 10:55:08 +0200 | [diff] [blame] | 564 | fprintf(stderr, |
| 565 | "No channels are enabled, we have nothing to scan.\n"); |
| 566 | fprintf(stderr, "Enable channels manually in " |
| 567 | FORMAT_SCAN_ELEMENTS_DIR |
Linus Walleij | 5f991a9 | 2016-04-14 10:26:47 +0200 | [diff] [blame] | 568 | "/*_en or pass -a to autoenable channels and " |
| 569 | "try again.\n", dev_dir_name); |
Linus Walleij | 53dabaf | 2015-08-10 10:55:08 +0200 | [diff] [blame] | 570 | ret = -ENOENT; |
Crestez Dan Leonard | 4f20d59 | 2016-05-23 21:39:56 +0300 | [diff] [blame] | 571 | goto error; |
Linus Walleij | 53dabaf | 2015-08-10 10:55:08 +0200 | [diff] [blame] | 572 | } |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 573 | |
| 574 | /* |
| 575 | * Construct the directory name for the associated buffer. |
| 576 | * As we know that the lis3l02dq has only one buffer this may |
| 577 | * be built rather than found. |
| 578 | */ |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 579 | ret = asprintf(&buf_dir_name, |
| 580 | "%siio:device%d/buffer", iio_dir, dev_num); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 581 | if (ret < 0) { |
| 582 | ret = -ENOMEM; |
Crestez Dan Leonard | 4f20d59 | 2016-05-23 21:39:56 +0300 | [diff] [blame] | 583 | goto error; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 584 | } |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 585 | |
| 586 | if (!notrigger) { |
| 587 | printf("%s %s\n", dev_dir_name, trigger_name); |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 588 | /* |
| 589 | * Set the device trigger to be the data ready trigger found |
| 590 | * above |
| 591 | */ |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 592 | ret = write_sysfs_string_and_verify("trigger/current_trigger", |
| 593 | dev_dir_name, |
| 594 | trigger_name); |
| 595 | if (ret < 0) { |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 596 | fprintf(stderr, |
| 597 | "Failed to write current_trigger file\n"); |
Crestez Dan Leonard | 4f20d59 | 2016-05-23 21:39:56 +0300 | [diff] [blame] | 598 | goto error; |
Karol Wrona | b6d5be57 | 2014-11-04 15:29:39 +0100 | [diff] [blame] | 599 | } |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | /* Setup ring buffer parameters */ |
| 603 | ret = write_sysfs_int("length", buf_dir_name, buf_len); |
| 604 | if (ret < 0) |
Crestez Dan Leonard | 4f20d59 | 2016-05-23 21:39:56 +0300 | [diff] [blame] | 605 | goto error; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 606 | |
| 607 | /* Enable the buffer */ |
| 608 | ret = write_sysfs_int("enable", buf_dir_name, 1); |
Irina Tirdea | e723149 | 2015-07-24 16:28:06 +0300 | [diff] [blame] | 609 | if (ret < 0) { |
| 610 | fprintf(stderr, |
| 611 | "Failed to enable buffer: %s\n", strerror(-ret)); |
Crestez Dan Leonard | 4f20d59 | 2016-05-23 21:39:56 +0300 | [diff] [blame] | 612 | goto error; |
Irina Tirdea | e723149 | 2015-07-24 16:28:06 +0300 | [diff] [blame] | 613 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 614 | |
Peter Meerwald | d8da0ee | 2012-06-25 23:12:14 +0200 | [diff] [blame] | 615 | scan_size = size_from_channelarray(channels, num_channels); |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 616 | data = malloc(scan_size * buf_len); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 617 | if (!data) { |
| 618 | ret = -ENOMEM; |
Crestez Dan Leonard | 4f20d59 | 2016-05-23 21:39:56 +0300 | [diff] [blame] | 619 | goto error; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 620 | } |
| 621 | |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 622 | ret = asprintf(&buffer_access, "/dev/iio:device%d", dev_num); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 623 | if (ret < 0) { |
| 624 | ret = -ENOMEM; |
Crestez Dan Leonard | 4f20d59 | 2016-05-23 21:39:56 +0300 | [diff] [blame] | 625 | goto error; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 626 | } |
| 627 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 628 | /* Attempt to open non blocking the access dev */ |
| 629 | fp = open(buffer_access, O_RDONLY | O_NONBLOCK); |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 630 | if (fp == -1) { /* TODO: If it isn't there make the node */ |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 631 | ret = -errno; |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 632 | fprintf(stderr, "Failed to open %s\n", buffer_access); |
Crestez Dan Leonard | 4f20d59 | 2016-05-23 21:39:56 +0300 | [diff] [blame] | 633 | goto error; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 634 | } |
| 635 | |
Martin Kelly | 55dda0a | 2018-05-17 17:14:46 -0700 | [diff] [blame] | 636 | for (j = 0; j < num_loops || num_loops < 0; j++) { |
Jonathan Cameron | 30268a3 | 2011-02-11 13:09:12 +0000 | [diff] [blame] | 637 | if (!noevents) { |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 638 | struct pollfd pfd = { |
| 639 | .fd = fp, |
| 640 | .events = POLLIN, |
| 641 | }; |
| 642 | |
Hartmut Knaack | 6bb7cac | 2015-05-31 14:40:04 +0200 | [diff] [blame] | 643 | ret = poll(&pfd, 1, -1); |
| 644 | if (ret < 0) { |
| 645 | ret = -errno; |
Crestez Dan Leonard | 4f20d59 | 2016-05-23 21:39:56 +0300 | [diff] [blame] | 646 | goto error; |
Hartmut Knaack | 6bb7cac | 2015-05-31 14:40:04 +0200 | [diff] [blame] | 647 | } else if (ret == 0) { |
| 648 | continue; |
| 649 | } |
| 650 | |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 651 | toread = buf_len; |
Jonathan Cameron | 30268a3 | 2011-02-11 13:09:12 +0000 | [diff] [blame] | 652 | } else { |
Jonathan Cameron | 96df979 | 2011-02-11 13:09:13 +0000 | [diff] [blame] | 653 | usleep(timedelay); |
Jonathan Cameron | 30268a3 | 2011-02-11 13:09:12 +0000 | [diff] [blame] | 654 | toread = 64; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 655 | } |
Jonathan Cameron | 30268a3 | 2011-02-11 13:09:12 +0000 | [diff] [blame] | 656 | |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 657 | read_size = read(fp, data, toread * scan_size); |
Peter Meerwald | 97b603a | 2014-12-06 06:00:00 +0000 | [diff] [blame] | 658 | if (read_size < 0) { |
Hartmut Knaack | 8749948 | 2015-05-31 14:39:56 +0200 | [diff] [blame] | 659 | if (errno == EAGAIN) { |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 660 | fprintf(stderr, "nothing available\n"); |
Peter Meerwald | 97b603a | 2014-12-06 06:00:00 +0000 | [diff] [blame] | 661 | continue; |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 662 | } else { |
Peter Meerwald | 97b603a | 2014-12-06 06:00:00 +0000 | [diff] [blame] | 663 | break; |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 664 | } |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 665 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 666 | for (i = 0; i < read_size / scan_size; i++) |
| 667 | process_scan(data + scan_size * i, channels, |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 668 | num_channels); |
| 669 | } |
| 670 | |
Crestez Dan Leonard | 4f20d59 | 2016-05-23 21:39:56 +0300 | [diff] [blame] | 671 | error: |
| 672 | cleanup(); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 673 | |
Crestez Dan Leonard | 4f20d59 | 2016-05-23 21:39:56 +0300 | [diff] [blame] | 674 | if (fp >= 0 && close(fp) == -1) |
Hartmut Knaack | 6bb7cac | 2015-05-31 14:40:04 +0200 | [diff] [blame] | 675 | perror("Failed to close buffer"); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 676 | free(buffer_access); |
Hartmut Knaack | a71bfb4 | 2015-05-31 14:39:41 +0200 | [diff] [blame] | 677 | free(data); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 678 | free(buf_dir_name); |
Hartmut Knaack | 63f05c8 | 2015-05-31 14:39:44 +0200 | [diff] [blame] | 679 | for (i = num_channels - 1; i >= 0; i--) { |
| 680 | free(channels[i].name); |
| 681 | free(channels[i].generic_name); |
| 682 | } |
| 683 | free(channels); |
Crestez Dan Leonard | 4f20d59 | 2016-05-23 21:39:56 +0300 | [diff] [blame] | 684 | free(trigger_name); |
Crestez Dan Leonard | de397db | 2016-05-23 21:39:57 +0300 | [diff] [blame] | 685 | free(device_name); |
Hartmut Knaack | d3ccfc4 | 2015-05-31 14:39:42 +0200 | [diff] [blame] | 686 | free(dev_dir_name); |
Hartmut Knaack | 0e79987 | 2015-05-31 14:40:17 +0200 | [diff] [blame] | 687 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 688 | return ret; |
| 689 | } |