Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
e58537cc JC |
2 | /* Industrialio buffer test code. |
3 | * | |
4 | * Copyright (c) 2008 Jonathan Cameron | |
5 | * | |
e58537cc JC |
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. | |
e58537cc JC |
15 | */ |
16 | ||
17 | #include <unistd.h> | |
bdcb31d0 | 18 | #include <stdlib.h> |
e58537cc JC |
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> | |
30268a3d | 26 | #include <string.h> |
52615d47 | 27 | #include <poll.h> |
117cf8b7 | 28 | #include <endian.h> |
bb23378c | 29 | #include <getopt.h> |
1bcdfbcf | 30 | #include <inttypes.h> |
4f20d592 CDL |
31 | #include <stdbool.h> |
32 | #include <signal.h> | |
8827faab AA |
33 | #include <sys/ioctl.h> |
34 | #include <linux/iio/buffer.h> | |
e58537cc JC |
35 | #include "iio_utils.h" |
36 | ||
5f991a92 LW |
37 | /** |
38 | * enum autochan - state for the automatic channel enabling mechanism | |
39 | */ | |
40 | enum autochan { | |
41 | AUTOCHANNELS_DISABLED, | |
42 | AUTOCHANNELS_ENABLED, | |
43 | AUTOCHANNELS_ACTIVE, | |
44 | }; | |
45 | ||
e58537cc JC |
46 | /** |
47 | * size_from_channelarray() - calculate the storage size of a scan | |
d8da0eee PM |
48 | * @channels: the channel info array |
49 | * @num_channels: number of channels | |
e58537cc JC |
50 | * |
51 | * Has the side effect of filling the channels[i].location values used | |
52 | * in processing the buffer output. | |
53 | **/ | |
49d73631 | 54 | static unsigned int size_from_channelarray(struct iio_channel_info *channels, int num_channels) |
e58537cc | 55 | { |
49d73631 | 56 | unsigned int bytes = 0; |
2d3dff57 MV |
57 | int i = 0, max = 0; |
58 | unsigned int misalignment; | |
ff39a252 | 59 | |
e58537cc | 60 | while (i < num_channels) { |
2d3dff57 MV |
61 | if (channels[i].bytes > max) |
62 | max = channels[i].bytes; | |
e58537cc JC |
63 | if (bytes % channels[i].bytes == 0) |
64 | channels[i].location = bytes; | |
65 | else | |
7663a4aa HK |
66 | channels[i].location = bytes - bytes % channels[i].bytes |
67 | + channels[i].bytes; | |
68 | ||
e58537cc JC |
69 | bytes = channels[i].location + channels[i].bytes; |
70 | i++; | |
71 | } | |
2d3dff57 MV |
72 | /* |
73 | * We want the data in next sample to also be properly aligned so | |
74 | * we'll add padding at the end if needed. Adding padding only | |
75 | * works for channel data which size is 2^n bytes. | |
76 | */ | |
77 | misalignment = bytes % max; | |
78 | if (misalignment) | |
79 | bytes += max - misalignment; | |
7663a4aa | 80 | |
e58537cc JC |
81 | return bytes; |
82 | } | |
83 | ||
ebe51125 | 84 | static void print1byte(uint8_t input, struct iio_channel_info *info) |
e8d0927a TB |
85 | { |
86 | /* | |
87 | * Shift before conversion to avoid sign extension | |
88 | * of left aligned data | |
89 | */ | |
90 | input >>= info->shift; | |
91 | input &= info->mask; | |
92 | if (info->is_signed) { | |
93 | int8_t val = (int8_t)(input << (8 - info->bits_used)) >> | |
94 | (8 - info->bits_used); | |
95 | printf("%05f ", ((float)val + info->offset) * info->scale); | |
96 | } else { | |
97 | printf("%05f ", ((float)input + info->offset) * info->scale); | |
98 | } | |
99 | } | |
100 | ||
ebe51125 | 101 | static void print2byte(uint16_t input, struct iio_channel_info *info) |
52615d47 | 102 | { |
117cf8b7 | 103 | /* First swap if incorrect endian */ |
117cf8b7 | 104 | if (info->be) |
8e926134 | 105 | input = be16toh(input); |
117cf8b7 | 106 | else |
8e926134 | 107 | input = le16toh(input); |
117cf8b7 | 108 | |
d8da0eee PM |
109 | /* |
110 | * Shift before conversion to avoid sign extension | |
111 | * of left aligned data | |
112 | */ | |
1525ecfc | 113 | input >>= info->shift; |
8e926134 | 114 | input &= info->mask; |
52615d47 | 115 | if (info->is_signed) { |
8e926134 HK |
116 | int16_t val = (int16_t)(input << (16 - info->bits_used)) >> |
117 | (16 - info->bits_used); | |
118 | printf("%05f ", ((float)val + info->offset) * info->scale); | |
119 | } else { | |
120 | printf("%05f ", ((float)input + info->offset) * info->scale); | |
121 | } | |
122 | } | |
ff39a252 | 123 | |
ebe51125 | 124 | static void print4byte(uint32_t input, struct iio_channel_info *info) |
8e926134 HK |
125 | { |
126 | /* First swap if incorrect endian */ | |
127 | if (info->be) | |
128 | input = be32toh(input); | |
129 | else | |
130 | input = le32toh(input); | |
131 | ||
132 | /* | |
133 | * Shift before conversion to avoid sign extension | |
134 | * of left aligned data | |
135 | */ | |
136 | input >>= info->shift; | |
137 | input &= info->mask; | |
138 | if (info->is_signed) { | |
139 | int32_t val = (int32_t)(input << (32 - info->bits_used)) >> | |
140 | (32 - info->bits_used); | |
141 | printf("%05f ", ((float)val + info->offset) * info->scale); | |
52615d47 | 142 | } else { |
8e926134 HK |
143 | printf("%05f ", ((float)input + info->offset) * info->scale); |
144 | } | |
145 | } | |
ff39a252 | 146 | |
ebe51125 | 147 | static void print8byte(uint64_t input, struct iio_channel_info *info) |
8e926134 HK |
148 | { |
149 | /* First swap if incorrect endian */ | |
150 | if (info->be) | |
151 | input = be64toh(input); | |
152 | else | |
153 | input = le64toh(input); | |
154 | ||
155 | /* | |
156 | * Shift before conversion to avoid sign extension | |
157 | * of left aligned data | |
158 | */ | |
159 | input >>= info->shift; | |
160 | input &= info->mask; | |
161 | if (info->is_signed) { | |
162 | int64_t val = (int64_t)(input << (64 - info->bits_used)) >> | |
163 | (64 - info->bits_used); | |
164 | /* special case for timestamp */ | |
165 | if (info->scale == 1.0f && info->offset == 0.0f) | |
166 | printf("%" PRId64 " ", val); | |
167 | else | |
168 | printf("%05f ", | |
169 | ((float)val + info->offset) * info->scale); | |
170 | } else { | |
171 | printf("%05f ", ((float)input + info->offset) * info->scale); | |
52615d47 JC |
172 | } |
173 | } | |
8e926134 | 174 | |
e58537cc JC |
175 | /** |
176 | * process_scan() - print out the values in SI units | |
177 | * @data: pointer to the start of the scan | |
7663a4aa HK |
178 | * @channels: information about the channels. |
179 | * Note: size_from_channelarray must have been called first | |
180 | * to fill the location offsets. | |
d8da0eee | 181 | * @num_channels: number of channels |
e58537cc | 182 | **/ |
ebe51125 AA |
183 | static void process_scan(char *data, struct iio_channel_info *channels, |
184 | int num_channels) | |
e58537cc JC |
185 | { |
186 | int k; | |
ff39a252 | 187 | |
e58537cc | 188 | for (k = 0; k < num_channels; k++) |
d8da0eee | 189 | switch (channels[k].bytes) { |
e58537cc | 190 | /* only a few cases implemented so far */ |
e8d0927a TB |
191 | case 1: |
192 | print1byte(*(uint8_t *)(data + channels[k].location), | |
193 | &channels[k]); | |
194 | break; | |
e58537cc | 195 | case 2: |
d8da0eee PM |
196 | print2byte(*(uint16_t *)(data + channels[k].location), |
197 | &channels[k]); | |
e58537cc | 198 | break; |
6cffc1f8 | 199 | case 4: |
8e926134 HK |
200 | print4byte(*(uint32_t *)(data + channels[k].location), |
201 | &channels[k]); | |
6cffc1f8 | 202 | break; |
e58537cc | 203 | case 8: |
8e926134 HK |
204 | print8byte(*(uint64_t *)(data + channels[k].location), |
205 | &channels[k]); | |
e58537cc JC |
206 | break; |
207 | default: | |
208 | break; | |
209 | } | |
210 | printf("\n"); | |
211 | } | |
212 | ||
8827faab | 213 | static int enable_disable_all_channels(char *dev_dir_name, int buffer_idx, int enable) |
5f991a92 LW |
214 | { |
215 | const struct dirent *ent; | |
216 | char scanelemdir[256]; | |
217 | DIR *dp; | |
218 | int ret; | |
219 | ||
220 | snprintf(scanelemdir, sizeof(scanelemdir), | |
8827faab | 221 | FORMAT_SCAN_ELEMENTS_DIR, dev_dir_name, buffer_idx); |
5f991a92 LW |
222 | scanelemdir[sizeof(scanelemdir)-1] = '\0'; |
223 | ||
224 | dp = opendir(scanelemdir); | |
225 | if (!dp) { | |
226 | fprintf(stderr, "Enabling/disabling channels: can't open %s\n", | |
227 | scanelemdir); | |
228 | return -EIO; | |
229 | } | |
230 | ||
231 | ret = -ENOENT; | |
232 | while (ent = readdir(dp), ent) { | |
233 | if (iioutils_check_suffix(ent->d_name, "_en")) { | |
234 | printf("%sabling: %s\n", | |
235 | enable ? "En" : "Dis", | |
236 | ent->d_name); | |
237 | ret = write_sysfs_int(ent->d_name, scanelemdir, | |
238 | enable); | |
239 | if (ret < 0) | |
240 | fprintf(stderr, "Failed to enable/disable %s\n", | |
241 | ent->d_name); | |
242 | } | |
243 | } | |
244 | ||
245 | if (closedir(dp) == -1) { | |
246 | perror("Enabling/disabling channels: " | |
247 | "Failed to close directory"); | |
248 | return -errno; | |
249 | } | |
250 | return 0; | |
251 | } | |
252 | ||
ebe51125 | 253 | static void print_usage(void) |
e06e3d71 | 254 | { |
d9abc615 CO |
255 | fprintf(stderr, "Usage: generic_buffer [options]...\n" |
256 | "Capture, convert and output data from IIO device buffer\n" | |
5f991a92 | 257 | " -a Auto-activate all available channels\n" |
4e2cf0e2 | 258 | " -A Force-activate ALL channels\n" |
8827faab | 259 | " -b <n> The buffer which to open (by index), default 0\n" |
55dda0ab | 260 | " -c <n> Do n conversions, or loop forever if n < 0\n" |
d9abc615 CO |
261 | " -e Disable wait for event (new data)\n" |
262 | " -g Use trigger-less mode\n" | |
263 | " -l <n> Set buffer length to n samples\n" | |
de397db8 CDL |
264 | " --device-name -n <name>\n" |
265 | " --device-num -N <num>\n" | |
266 | " Set device by name or number (mandatory)\n" | |
7c7e9dad CDL |
267 | " --trigger-name -t <name>\n" |
268 | " --trigger-num -T <num>\n" | |
269 | " Set trigger by name or number\n" | |
d9abc615 | 270 | " -w <n> Set delay between reads in us (event-less mode)\n"); |
e06e3d71 HK |
271 | } |
272 | ||
ebe51125 AA |
273 | static enum autochan autochannels = AUTOCHANNELS_DISABLED; |
274 | static char *dev_dir_name = NULL; | |
275 | static char *buf_dir_name = NULL; | |
8827faab | 276 | static int buffer_idx = 0; |
ebe51125 | 277 | static bool current_trigger_set = false; |
4f20d592 | 278 | |
ebe51125 | 279 | static void cleanup(void) |
4f20d592 CDL |
280 | { |
281 | int ret; | |
282 | ||
283 | /* Disable trigger */ | |
284 | if (dev_dir_name && current_trigger_set) { | |
285 | /* Disconnect the trigger - just write a dummy name. */ | |
286 | ret = write_sysfs_string("trigger/current_trigger", | |
287 | dev_dir_name, "NULL"); | |
288 | if (ret < 0) | |
289 | fprintf(stderr, "Failed to disable trigger: %s\n", | |
290 | strerror(-ret)); | |
291 | current_trigger_set = false; | |
292 | } | |
293 | ||
294 | /* Disable buffer */ | |
295 | if (buf_dir_name) { | |
296 | ret = write_sysfs_int("enable", buf_dir_name, 0); | |
297 | if (ret < 0) | |
298 | fprintf(stderr, "Failed to disable buffer: %s\n", | |
299 | strerror(-ret)); | |
300 | } | |
301 | ||
302 | /* Disable channels if auto-enabled */ | |
303 | if (dev_dir_name && autochannels == AUTOCHANNELS_ACTIVE) { | |
8827faab | 304 | ret = enable_disable_all_channels(dev_dir_name, buffer_idx, 0); |
4f20d592 CDL |
305 | if (ret) |
306 | fprintf(stderr, "Failed to disable all channels\n"); | |
307 | autochannels = AUTOCHANNELS_DISABLED; | |
308 | } | |
309 | } | |
310 | ||
ebe51125 | 311 | static void sig_handler(int signum) |
4f20d592 CDL |
312 | { |
313 | fprintf(stderr, "Caught signal %d\n", signum); | |
314 | cleanup(); | |
315 | exit(-signum); | |
316 | } | |
317 | ||
ebe51125 | 318 | static void register_cleanup(void) |
4f20d592 CDL |
319 | { |
320 | struct sigaction sa = { .sa_handler = sig_handler }; | |
321 | const int signums[] = { SIGINT, SIGTERM, SIGABRT }; | |
322 | int ret, i; | |
323 | ||
324 | for (i = 0; i < ARRAY_SIZE(signums); ++i) { | |
325 | ret = sigaction(signums[i], &sa, NULL); | |
326 | if (ret) { | |
327 | perror("Failed to register signal handler"); | |
328 | exit(-1); | |
329 | } | |
330 | } | |
331 | } | |
332 | ||
de397db8 CDL |
333 | static const struct option longopts[] = { |
334 | { "device-name", 1, 0, 'n' }, | |
335 | { "device-num", 1, 0, 'N' }, | |
7c7e9dad CDL |
336 | { "trigger-name", 1, 0, 't' }, |
337 | { "trigger-num", 1, 0, 'T' }, | |
de397db8 CDL |
338 | { }, |
339 | }; | |
340 | ||
e58537cc JC |
341 | int main(int argc, char **argv) |
342 | { | |
b119d3bc | 343 | long long num_loops = 2; |
96df9799 JC |
344 | unsigned long timedelay = 1000000; |
345 | unsigned long buf_len = 128; | |
346 | ||
71b52d2c | 347 | ssize_t i; |
55dda0ab | 348 | unsigned long long j; |
71b52d2c MK |
349 | unsigned long toread; |
350 | int ret, c; | |
8827faab AA |
351 | struct stat st; |
352 | int fd = -1; | |
353 | int buf_fd = -1; | |
e58537cc | 354 | |
4f20d592 | 355 | int num_channels = 0; |
e58537cc | 356 | char *trigger_name = NULL, *device_name = NULL; |
e58537cc | 357 | |
4f20d592 | 358 | char *data = NULL; |
c77b3810 | 359 | ssize_t read_size; |
deb4d1fd | 360 | int dev_num = -1, trig_num = -1; |
4f20d592 | 361 | char *buffer_access = NULL; |
49d73631 | 362 | unsigned int scan_size; |
30268a3d | 363 | int noevents = 0; |
b6d5be57 | 364 | int notrigger = 0; |
96df9799 | 365 | char *dummy; |
71ccbe5d | 366 | bool force_autochannels = false; |
e58537cc | 367 | |
ddbc719f | 368 | struct iio_channel_info *channels = NULL; |
e58537cc | 369 | |
4f20d592 CDL |
370 | register_cleanup(); |
371 | ||
8827faab | 372 | while ((c = getopt_long(argc, argv, "aAb:c:egl:n:N:t:T:w:?", longopts, |
4e2cf0e2 | 373 | NULL)) != -1) { |
e58537cc | 374 | switch (c) { |
5f991a92 LW |
375 | case 'a': |
376 | autochannels = AUTOCHANNELS_ENABLED; | |
377 | break; | |
4e2cf0e2 ERR |
378 | case 'A': |
379 | autochannels = AUTOCHANNELS_ENABLED; | |
71ccbe5d | 380 | force_autochannels = true; |
8827faab AA |
381 | break; |
382 | case 'b': | |
383 | errno = 0; | |
384 | buffer_idx = strtoll(optarg, &dummy, 10); | |
385 | if (errno) { | |
386 | ret = -errno; | |
387 | goto error; | |
388 | } | |
389 | if (buffer_idx < 0) { | |
390 | ret = -ERANGE; | |
391 | goto error; | |
392 | } | |
393 | ||
394 | break; | |
96df9799 | 395 | case 'c': |
c8ce9903 | 396 | errno = 0; |
55dda0ab | 397 | num_loops = strtoll(optarg, &dummy, 10); |
4f20d592 CDL |
398 | if (errno) { |
399 | ret = -errno; | |
400 | goto error; | |
401 | } | |
7663a4aa | 402 | |
96df9799 | 403 | break; |
e06e3d71 HK |
404 | case 'e': |
405 | noevents = 1; | |
406 | break; | |
407 | case 'g': | |
408 | notrigger = 1; | |
96df9799 JC |
409 | break; |
410 | case 'l': | |
c8ce9903 | 411 | errno = 0; |
96df9799 | 412 | buf_len = strtoul(optarg, &dummy, 10); |
4f20d592 CDL |
413 | if (errno) { |
414 | ret = -errno; | |
415 | goto error; | |
416 | } | |
7663a4aa | 417 | |
96df9799 | 418 | break; |
e06e3d71 | 419 | case 'n': |
de397db8 CDL |
420 | device_name = strdup(optarg); |
421 | break; | |
422 | case 'N': | |
423 | errno = 0; | |
424 | dev_num = strtoul(optarg, &dummy, 10); | |
425 | if (errno) { | |
426 | ret = -errno; | |
427 | goto error; | |
428 | } | |
e06e3d71 HK |
429 | break; |
430 | case 't': | |
4f20d592 | 431 | trigger_name = strdup(optarg); |
e06e3d71 | 432 | break; |
7c7e9dad CDL |
433 | case 'T': |
434 | errno = 0; | |
435 | trig_num = strtoul(optarg, &dummy, 10); | |
436 | if (errno) | |
437 | return -errno; | |
438 | break; | |
e06e3d71 HK |
439 | case 'w': |
440 | errno = 0; | |
441 | timedelay = strtoul(optarg, &dummy, 10); | |
4f20d592 CDL |
442 | if (errno) { |
443 | ret = -errno; | |
444 | goto error; | |
445 | } | |
b6d5be57 | 446 | break; |
e58537cc | 447 | case '?': |
e06e3d71 | 448 | print_usage(); |
4f20d592 CDL |
449 | ret = -1; |
450 | goto error; | |
e58537cc JC |
451 | } |
452 | } | |
453 | ||
454 | /* Find the device requested */ | |
de397db8 CDL |
455 | if (dev_num < 0 && !device_name) { |
456 | fprintf(stderr, "Device not set\n"); | |
457 | print_usage(); | |
458 | ret = -1; | |
459 | goto error; | |
460 | } else if (dev_num >= 0 && device_name) { | |
461 | fprintf(stderr, "Only one of --device-num or --device-name needs to be set\n"); | |
462 | print_usage(); | |
463 | ret = -1; | |
4f20d592 | 464 | goto error; |
de397db8 CDL |
465 | } else if (dev_num < 0) { |
466 | dev_num = find_type_by_name(device_name, "iio:device"); | |
467 | if (dev_num < 0) { | |
468 | fprintf(stderr, "Failed to find the %s\n", device_name); | |
469 | ret = dev_num; | |
470 | goto error; | |
471 | } | |
e58537cc JC |
472 | } |
473 | printf("iio device number being used is %d\n", dev_num); | |
474 | ||
e9e45b43 | 475 | ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num); |
de397db8 CDL |
476 | if (ret < 0) |
477 | return -ENOMEM; | |
478 | /* Fetch device_name if specified by number */ | |
479 | if (!device_name) { | |
480 | device_name = malloc(IIO_MAX_NAME_LENGTH); | |
481 | if (!device_name) { | |
482 | ret = -ENOMEM; | |
483 | goto error; | |
484 | } | |
485 | ret = read_sysfs_string("name", dev_dir_name, device_name); | |
486 | if (ret < 0) { | |
487 | fprintf(stderr, "Failed to read name of device %d\n", dev_num); | |
488 | goto error; | |
489 | } | |
4f20d592 | 490 | } |
b6d5be57 | 491 | |
7c7e9dad CDL |
492 | if (notrigger) { |
493 | printf("trigger-less mode selected\n"); | |
f8e81d7e | 494 | } else if (trig_num >= 0) { |
7c7e9dad CDL |
495 | char *trig_dev_name; |
496 | ret = asprintf(&trig_dev_name, "%strigger%d", iio_dir, trig_num); | |
497 | if (ret < 0) { | |
498 | return -ENOMEM; | |
499 | } | |
500 | trigger_name = malloc(IIO_MAX_NAME_LENGTH); | |
3c6b818b ZJ |
501 | if (!trigger_name) { |
502 | ret = -ENOMEM; | |
503 | goto error; | |
504 | } | |
7c7e9dad CDL |
505 | ret = read_sysfs_string("name", trig_dev_name, trigger_name); |
506 | free(trig_dev_name); | |
507 | if (ret < 0) { | |
508 | fprintf(stderr, "Failed to read trigger%d name from\n", trig_num); | |
509 | return ret; | |
510 | } | |
511 | printf("iio trigger number being used is %d\n", trig_num); | |
512 | } else { | |
ff1ac639 | 513 | if (!trigger_name) { |
b6d5be57 KW |
514 | /* |
515 | * Build the trigger name. If it is device associated | |
516 | * its name is <device_name>_dev[n] where n matches | |
517 | * the device number found above. | |
518 | */ | |
519 | ret = asprintf(&trigger_name, | |
520 | "%s-dev%d", device_name, dev_num); | |
521 | if (ret < 0) { | |
522 | ret = -ENOMEM; | |
4f20d592 | 523 | goto error; |
b6d5be57 | 524 | } |
e58537cc | 525 | } |
e58537cc | 526 | |
793d6b5e LW |
527 | /* Look for this "-devN" trigger */ |
528 | trig_num = find_type_by_name(trigger_name, "trigger"); | |
529 | if (trig_num < 0) { | |
530 | /* OK try the simpler "-trigger" suffix instead */ | |
531 | free(trigger_name); | |
532 | ret = asprintf(&trigger_name, | |
533 | "%s-trigger", device_name); | |
534 | if (ret < 0) { | |
535 | ret = -ENOMEM; | |
4f20d592 | 536 | goto error; |
793d6b5e LW |
537 | } |
538 | } | |
539 | ||
b6d5be57 KW |
540 | trig_num = find_type_by_name(trigger_name, "trigger"); |
541 | if (trig_num < 0) { | |
d9abc615 CO |
542 | fprintf(stderr, "Failed to find the trigger %s\n", |
543 | trigger_name); | |
e83a47cf | 544 | ret = trig_num; |
4f20d592 | 545 | goto error; |
b6d5be57 | 546 | } |
7663a4aa | 547 | |
b6d5be57 | 548 | printf("iio trigger number being used is %d\n", trig_num); |
7663a4aa | 549 | } |
e58537cc JC |
550 | |
551 | /* | |
552 | * Parse the files in scan_elements to identify what channels are | |
553 | * present | |
554 | */ | |
8827faab | 555 | ret = build_channel_array(dev_dir_name, buffer_idx, &channels, &num_channels); |
e58537cc | 556 | if (ret) { |
d9abc615 CO |
557 | fprintf(stderr, "Problem reading scan element information\n" |
558 | "diag %s\n", dev_dir_name); | |
4f20d592 | 559 | goto error; |
e58537cc | 560 | } |
7c5fe411 | 561 | if (num_channels && autochannels == AUTOCHANNELS_ENABLED && |
71ccbe5d | 562 | !force_autochannels) { |
5f991a92 LW |
563 | fprintf(stderr, "Auto-channels selected but some channels " |
564 | "are already activated in sysfs\n"); | |
565 | fprintf(stderr, "Proceeding without activating any channels\n"); | |
566 | } | |
567 | ||
4e2cf0e2 | 568 | if ((!num_channels && autochannels == AUTOCHANNELS_ENABLED) || |
7c5fe411 | 569 | (autochannels == AUTOCHANNELS_ENABLED && force_autochannels)) { |
4e2cf0e2 | 570 | fprintf(stderr, "Enabling all channels\n"); |
5f991a92 | 571 | |
8827faab | 572 | ret = enable_disable_all_channels(dev_dir_name, buffer_idx, 1); |
5f991a92 LW |
573 | if (ret) { |
574 | fprintf(stderr, "Failed to enable all channels\n"); | |
4f20d592 | 575 | goto error; |
5f991a92 LW |
576 | } |
577 | ||
578 | /* This flags that we need to disable the channels again */ | |
579 | autochannels = AUTOCHANNELS_ACTIVE; | |
580 | ||
8827faab | 581 | ret = build_channel_array(dev_dir_name, buffer_idx, &channels, |
5f991a92 LW |
582 | &num_channels); |
583 | if (ret) { | |
584 | fprintf(stderr, "Problem reading scan element " | |
585 | "information\n" | |
586 | "diag %s\n", dev_dir_name); | |
4f20d592 | 587 | goto error; |
5f991a92 LW |
588 | } |
589 | if (!num_channels) { | |
590 | fprintf(stderr, "Still no channels after " | |
591 | "auto-enabling, giving up\n"); | |
4f20d592 | 592 | goto error; |
5f991a92 LW |
593 | } |
594 | } | |
595 | ||
596 | if (!num_channels && autochannels == AUTOCHANNELS_DISABLED) { | |
53dabafe LW |
597 | fprintf(stderr, |
598 | "No channels are enabled, we have nothing to scan.\n"); | |
599 | fprintf(stderr, "Enable channels manually in " | |
600 | FORMAT_SCAN_ELEMENTS_DIR | |
5f991a92 | 601 | "/*_en or pass -a to autoenable channels and " |
8827faab | 602 | "try again.\n", dev_dir_name, buffer_idx); |
53dabafe | 603 | ret = -ENOENT; |
4f20d592 | 604 | goto error; |
53dabafe | 605 | } |
e58537cc JC |
606 | |
607 | /* | |
608 | * Construct the directory name for the associated buffer. | |
609 | * As we know that the lis3l02dq has only one buffer this may | |
610 | * be built rather than found. | |
611 | */ | |
1aa04278 | 612 | ret = asprintf(&buf_dir_name, |
8827faab | 613 | "%siio:device%d/buffer%d", iio_dir, dev_num, buffer_idx); |
e58537cc JC |
614 | if (ret < 0) { |
615 | ret = -ENOMEM; | |
4f20d592 | 616 | goto error; |
e58537cc | 617 | } |
b6d5be57 | 618 | |
8827faab AA |
619 | if (stat(buf_dir_name, &st)) { |
620 | fprintf(stderr, "Could not stat() '%s', got error %d: %s\n", | |
621 | buf_dir_name, errno, strerror(errno)); | |
622 | ret = -errno; | |
623 | goto error; | |
624 | } | |
625 | ||
626 | if (!S_ISDIR(st.st_mode)) { | |
627 | fprintf(stderr, "File '%s' is not a directory\n", buf_dir_name); | |
628 | ret = -EFAULT; | |
629 | goto error; | |
630 | } | |
631 | ||
b6d5be57 KW |
632 | if (!notrigger) { |
633 | printf("%s %s\n", dev_dir_name, trigger_name); | |
7663a4aa HK |
634 | /* |
635 | * Set the device trigger to be the data ready trigger found | |
636 | * above | |
637 | */ | |
b6d5be57 KW |
638 | ret = write_sysfs_string_and_verify("trigger/current_trigger", |
639 | dev_dir_name, | |
640 | trigger_name); | |
641 | if (ret < 0) { | |
d9abc615 CO |
642 | fprintf(stderr, |
643 | "Failed to write current_trigger file\n"); | |
4f20d592 | 644 | goto error; |
b6d5be57 | 645 | } |
e58537cc JC |
646 | } |
647 | ||
8827faab AA |
648 | ret = asprintf(&buffer_access, "/dev/iio:device%d", dev_num); |
649 | if (ret < 0) { | |
650 | ret = -ENOMEM; | |
651 | goto error; | |
652 | } | |
653 | ||
654 | /* Attempt to open non blocking the access dev */ | |
655 | fd = open(buffer_access, O_RDONLY | O_NONBLOCK); | |
656 | if (fd == -1) { /* TODO: If it isn't there make the node */ | |
657 | ret = -errno; | |
658 | fprintf(stderr, "Failed to open %s\n", buffer_access); | |
659 | goto error; | |
660 | } | |
661 | ||
662 | /* specify for which buffer index we want an FD */ | |
663 | buf_fd = buffer_idx; | |
664 | ||
665 | ret = ioctl(fd, IIO_BUFFER_GET_FD_IOCTL, &buf_fd); | |
666 | if (ret == -1 || buf_fd == -1) { | |
667 | ret = -errno; | |
668 | if (ret == -ENODEV || ret == -EINVAL) | |
669 | fprintf(stderr, | |
670 | "Device does not have this many buffers\n"); | |
671 | else | |
672 | fprintf(stderr, "Failed to retrieve buffer fd\n"); | |
673 | ||
674 | goto error; | |
675 | } | |
676 | ||
e58537cc JC |
677 | /* Setup ring buffer parameters */ |
678 | ret = write_sysfs_int("length", buf_dir_name, buf_len); | |
679 | if (ret < 0) | |
4f20d592 | 680 | goto error; |
e58537cc JC |
681 | |
682 | /* Enable the buffer */ | |
683 | ret = write_sysfs_int("enable", buf_dir_name, 1); | |
e7231491 IT |
684 | if (ret < 0) { |
685 | fprintf(stderr, | |
8827faab AA |
686 | "Failed to enable buffer '%s': %s\n", |
687 | buf_dir_name, strerror(-ret)); | |
4f20d592 | 688 | goto error; |
e7231491 | 689 | } |
7663a4aa | 690 | |
d8da0eee | 691 | scan_size = size_from_channelarray(channels, num_channels); |
49d73631 CM |
692 | |
693 | size_t total_buf_len = scan_size * buf_len; | |
694 | ||
695 | if (scan_size > 0 && total_buf_len / scan_size != buf_len) { | |
696 | ret = -EFAULT; | |
697 | perror("Integer overflow happened when calculate scan_size * buf_len"); | |
698 | goto error; | |
699 | } | |
700 | ||
701 | data = malloc(total_buf_len); | |
e58537cc JC |
702 | if (!data) { |
703 | ret = -ENOMEM; | |
4f20d592 | 704 | goto error; |
e58537cc JC |
705 | } |
706 | ||
8827faab AA |
707 | /** |
708 | * This check is being done here for sanity reasons, however it | |
709 | * should be omitted under normal operation. | |
710 | * If this is buffer0, we check that we get EBUSY after this point. | |
711 | */ | |
712 | if (buffer_idx == 0) { | |
713 | errno = 0; | |
714 | read_size = read(fd, data, 1); | |
715 | if (read_size > -1 || errno != EBUSY) { | |
716 | ret = -EFAULT; | |
717 | perror("Reading from '%s' should not be possible after ioctl()"); | |
718 | goto error; | |
719 | } | |
e58537cc JC |
720 | } |
721 | ||
8827faab AA |
722 | /* close now the main chardev FD and let the buffer FD work */ |
723 | if (close(fd) == -1) | |
724 | perror("Failed to close character device file"); | |
725 | fd = -1; | |
e58537cc | 726 | |
55dda0ab | 727 | for (j = 0; j < num_loops || num_loops < 0; j++) { |
30268a3d | 728 | if (!noevents) { |
52615d47 | 729 | struct pollfd pfd = { |
8827faab | 730 | .fd = buf_fd, |
52615d47 JC |
731 | .events = POLLIN, |
732 | }; | |
733 | ||
6bb7cac8 HK |
734 | ret = poll(&pfd, 1, -1); |
735 | if (ret < 0) { | |
736 | ret = -errno; | |
4f20d592 | 737 | goto error; |
6bb7cac8 HK |
738 | } else if (ret == 0) { |
739 | continue; | |
740 | } | |
741 | ||
30268a3d | 742 | } else { |
96df9799 | 743 | usleep(timedelay); |
e58537cc | 744 | } |
30268a3d | 745 | |
7c919b61 MV |
746 | toread = buf_len; |
747 | ||
8827faab | 748 | read_size = read(buf_fd, data, toread * scan_size); |
97b603a4 | 749 | if (read_size < 0) { |
8749948a | 750 | if (errno == EAGAIN) { |
d9abc615 | 751 | fprintf(stderr, "nothing available\n"); |
97b603a4 | 752 | continue; |
7663a4aa | 753 | } else { |
97b603a4 | 754 | break; |
7663a4aa | 755 | } |
e58537cc | 756 | } |
7663a4aa HK |
757 | for (i = 0; i < read_size / scan_size; i++) |
758 | process_scan(data + scan_size * i, channels, | |
e58537cc JC |
759 | num_channels); |
760 | } | |
761 | ||
4f20d592 CDL |
762 | error: |
763 | cleanup(); | |
e58537cc | 764 | |
8827faab AA |
765 | if (fd >= 0 && close(fd) == -1) |
766 | perror("Failed to close character device"); | |
767 | if (buf_fd >= 0 && close(buf_fd) == -1) | |
6bb7cac8 | 768 | perror("Failed to close buffer"); |
e58537cc | 769 | free(buffer_access); |
a71bfb4a | 770 | free(data); |
e58537cc | 771 | free(buf_dir_name); |
63f05c85 HK |
772 | for (i = num_channels - 1; i >= 0; i--) { |
773 | free(channels[i].name); | |
774 | free(channels[i].generic_name); | |
775 | } | |
776 | free(channels); | |
4f20d592 | 777 | free(trigger_name); |
de397db8 | 778 | free(device_name); |
d3ccfc41 | 779 | free(dev_dir_name); |
0e799878 | 780 | |
e58537cc JC |
781 | return ret; |
782 | } |