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