iio: iio_generic_buffer: Add --device-num option
[linux-2.6-block.git] / tools / iio / iio_generic_buffer.c
CommitLineData
e58537cc
JC
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>
bdcb31d0 22#include <stdlib.h>
e58537cc
JC
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>
30268a3d 30#include <string.h>
52615d47 31#include <poll.h>
117cf8b7 32#include <endian.h>
bb23378c 33#include <getopt.h>
1bcdfbcf 34#include <inttypes.h>
4f20d592
CDL
35#include <stdbool.h>
36#include <signal.h>
e58537cc
JC
37#include "iio_utils.h"
38
5f991a92
LW
39/**
40 * enum autochan - state for the automatic channel enabling mechanism
41 */
42enum autochan {
43 AUTOCHANNELS_DISABLED,
44 AUTOCHANNELS_ENABLED,
45 AUTOCHANNELS_ACTIVE,
46};
47
e58537cc
JC
48/**
49 * size_from_channelarray() - calculate the storage size of a scan
d8da0eee
PM
50 * @channels: the channel info array
51 * @num_channels: number of channels
e58537cc
JC
52 *
53 * Has the side effect of filling the channels[i].location values used
54 * in processing the buffer output.
55 **/
56int size_from_channelarray(struct iio_channel_info *channels, int num_channels)
57{
58 int bytes = 0;
59 int i = 0;
ff39a252 60
e58537cc
JC
61 while (i < num_channels) {
62 if (bytes % channels[i].bytes == 0)
63 channels[i].location = bytes;
64 else
7663a4aa
HK
65 channels[i].location = bytes - bytes % channels[i].bytes
66 + channels[i].bytes;
67
e58537cc
JC
68 bytes = channels[i].location + channels[i].bytes;
69 i++;
70 }
7663a4aa 71
e58537cc
JC
72 return bytes;
73}
74
e8d0927a
TB
75void 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
8e926134 92void print2byte(uint16_t input, struct iio_channel_info *info)
52615d47 93{
117cf8b7 94 /* First swap if incorrect endian */
117cf8b7 95 if (info->be)
8e926134 96 input = be16toh(input);
117cf8b7 97 else
8e926134 98 input = le16toh(input);
117cf8b7 99
d8da0eee
PM
100 /*
101 * Shift before conversion to avoid sign extension
102 * of left aligned data
103 */
1525ecfc 104 input >>= info->shift;
8e926134 105 input &= info->mask;
52615d47 106 if (info->is_signed) {
8e926134
HK
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);
110 } else {
111 printf("%05f ", ((float)input + info->offset) * info->scale);
112 }
113}
ff39a252 114
8e926134
HK
115void 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);
52615d47 133 } else {
8e926134
HK
134 printf("%05f ", ((float)input + info->offset) * info->scale);
135 }
136}
ff39a252 137
8e926134
HK
138void 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);
52615d47
JC
163 }
164}
8e926134 165
e58537cc
JC
166/**
167 * process_scan() - print out the values in SI units
168 * @data: pointer to the start of the scan
7663a4aa
HK
169 * @channels: information about the channels.
170 * Note: size_from_channelarray must have been called first
171 * to fill the location offsets.
d8da0eee 172 * @num_channels: number of channels
e58537cc
JC
173 **/
174void process_scan(char *data,
d8da0eee 175 struct iio_channel_info *channels,
e58537cc
JC
176 int num_channels)
177{
178 int k;
ff39a252 179
e58537cc 180 for (k = 0; k < num_channels; k++)
d8da0eee 181 switch (channels[k].bytes) {
e58537cc 182 /* only a few cases implemented so far */
e8d0927a
TB
183 case 1:
184 print1byte(*(uint8_t *)(data + channels[k].location),
185 &channels[k]);
186 break;
e58537cc 187 case 2:
d8da0eee
PM
188 print2byte(*(uint16_t *)(data + channels[k].location),
189 &channels[k]);
e58537cc 190 break;
6cffc1f8 191 case 4:
8e926134
HK
192 print4byte(*(uint32_t *)(data + channels[k].location),
193 &channels[k]);
6cffc1f8 194 break;
e58537cc 195 case 8:
8e926134
HK
196 print8byte(*(uint64_t *)(data + channels[k].location),
197 &channels[k]);
e58537cc
JC
198 break;
199 default:
200 break;
201 }
202 printf("\n");
203}
204
5f991a92
LW
205static 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
e06e3d71
HK
245void print_usage(void)
246{
d9abc615
CO
247 fprintf(stderr, "Usage: generic_buffer [options]...\n"
248 "Capture, convert and output data from IIO device buffer\n"
5f991a92 249 " -a Auto-activate all available channels\n"
d9abc615
CO
250 " -c <n> Do n conversions\n"
251 " -e Disable wait for event (new data)\n"
252 " -g Use trigger-less mode\n"
253 " -l <n> Set buffer length to n samples\n"
de397db8
CDL
254 " --device-name -n <name>\n"
255 " --device-num -N <num>\n"
256 " Set device by name or number (mandatory)\n"
d9abc615
CO
257 " -t <name> Set trigger name\n"
258 " -w <n> Set delay between reads in us (event-less mode)\n");
e06e3d71
HK
259}
260
4f20d592
CDL
261enum autochan autochannels = AUTOCHANNELS_DISABLED;
262char *dev_dir_name = NULL;
263char *buf_dir_name = NULL;
264bool current_trigger_set = false;
265
266void cleanup(void)
267{
268 int ret;
269
270 /* Disable trigger */
271 if (dev_dir_name && current_trigger_set) {
272 /* Disconnect the trigger - just write a dummy name. */
273 ret = write_sysfs_string("trigger/current_trigger",
274 dev_dir_name, "NULL");
275 if (ret < 0)
276 fprintf(stderr, "Failed to disable trigger: %s\n",
277 strerror(-ret));
278 current_trigger_set = false;
279 }
280
281 /* Disable buffer */
282 if (buf_dir_name) {
283 ret = write_sysfs_int("enable", buf_dir_name, 0);
284 if (ret < 0)
285 fprintf(stderr, "Failed to disable buffer: %s\n",
286 strerror(-ret));
287 }
288
289 /* Disable channels if auto-enabled */
290 if (dev_dir_name && autochannels == AUTOCHANNELS_ACTIVE) {
291 ret = enable_disable_all_channels(dev_dir_name, 0);
292 if (ret)
293 fprintf(stderr, "Failed to disable all channels\n");
294 autochannels = AUTOCHANNELS_DISABLED;
295 }
296}
297
298void sig_handler(int signum)
299{
300 fprintf(stderr, "Caught signal %d\n", signum);
301 cleanup();
302 exit(-signum);
303}
304
305void register_cleanup(void)
306{
307 struct sigaction sa = { .sa_handler = sig_handler };
308 const int signums[] = { SIGINT, SIGTERM, SIGABRT };
309 int ret, i;
310
311 for (i = 0; i < ARRAY_SIZE(signums); ++i) {
312 ret = sigaction(signums[i], &sa, NULL);
313 if (ret) {
314 perror("Failed to register signal handler");
315 exit(-1);
316 }
317 }
318}
319
de397db8
CDL
320static const struct option longopts[] = {
321 { "device-name", 1, 0, 'n' },
322 { "device-num", 1, 0, 'N' },
323 { },
324};
325
e58537cc
JC
326int main(int argc, char **argv)
327{
96df9799
JC
328 unsigned long num_loops = 2;
329 unsigned long timedelay = 1000000;
330 unsigned long buf_len = 128;
331
e58537cc 332 int ret, c, i, j, toread;
4f20d592 333 int fp = -1;
e58537cc 334
4f20d592 335 int num_channels = 0;
e58537cc 336 char *trigger_name = NULL, *device_name = NULL;
e58537cc 337
4f20d592 338 char *data = NULL;
c77b3810 339 ssize_t read_size;
de397db8 340 int dev_num = -1, trig_num;
4f20d592 341 char *buffer_access = NULL;
e58537cc 342 int scan_size;
30268a3d 343 int noevents = 0;
b6d5be57 344 int notrigger = 0;
96df9799 345 char *dummy;
e58537cc 346
d8da0eee 347 struct iio_channel_info *channels;
e58537cc 348
4f20d592
CDL
349 register_cleanup();
350
de397db8 351 while ((c = getopt_long(argc, argv, "ac:egl:n:N:t:w:", longopts, NULL)) != -1) {
e58537cc 352 switch (c) {
5f991a92
LW
353 case 'a':
354 autochannels = AUTOCHANNELS_ENABLED;
355 break;
96df9799 356 case 'c':
c8ce9903 357 errno = 0;
96df9799 358 num_loops = strtoul(optarg, &dummy, 10);
4f20d592
CDL
359 if (errno) {
360 ret = -errno;
361 goto error;
362 }
7663a4aa 363
96df9799 364 break;
e06e3d71
HK
365 case 'e':
366 noevents = 1;
367 break;
368 case 'g':
369 notrigger = 1;
96df9799
JC
370 break;
371 case 'l':
c8ce9903 372 errno = 0;
96df9799 373 buf_len = strtoul(optarg, &dummy, 10);
4f20d592
CDL
374 if (errno) {
375 ret = -errno;
376 goto error;
377 }
7663a4aa 378
96df9799 379 break;
e06e3d71 380 case 'n':
de397db8
CDL
381 device_name = strdup(optarg);
382 break;
383 case 'N':
384 errno = 0;
385 dev_num = strtoul(optarg, &dummy, 10);
386 if (errno) {
387 ret = -errno;
388 goto error;
389 }
e06e3d71
HK
390 break;
391 case 't':
4f20d592 392 trigger_name = strdup(optarg);
e06e3d71
HK
393 break;
394 case 'w':
395 errno = 0;
396 timedelay = strtoul(optarg, &dummy, 10);
4f20d592
CDL
397 if (errno) {
398 ret = -errno;
399 goto error;
400 }
b6d5be57 401 break;
e58537cc 402 case '?':
e06e3d71 403 print_usage();
4f20d592
CDL
404 ret = -1;
405 goto error;
e58537cc
JC
406 }
407 }
408
409 /* Find the device requested */
de397db8
CDL
410 if (dev_num < 0 && !device_name) {
411 fprintf(stderr, "Device not set\n");
412 print_usage();
413 ret = -1;
414 goto error;
415 } else if (dev_num >= 0 && device_name) {
416 fprintf(stderr, "Only one of --device-num or --device-name needs to be set\n");
417 print_usage();
418 ret = -1;
4f20d592 419 goto error;
de397db8
CDL
420 } else if (dev_num < 0) {
421 dev_num = find_type_by_name(device_name, "iio:device");
422 if (dev_num < 0) {
423 fprintf(stderr, "Failed to find the %s\n", device_name);
424 ret = dev_num;
425 goto error;
426 }
e58537cc
JC
427 }
428 printf("iio device number being used is %d\n", dev_num);
429
e9e45b43 430 ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num);
de397db8
CDL
431 if (ret < 0)
432 return -ENOMEM;
433 /* Fetch device_name if specified by number */
434 if (!device_name) {
435 device_name = malloc(IIO_MAX_NAME_LENGTH);
436 if (!device_name) {
437 ret = -ENOMEM;
438 goto error;
439 }
440 ret = read_sysfs_string("name", dev_dir_name, device_name);
441 if (ret < 0) {
442 fprintf(stderr, "Failed to read name of device %d\n", dev_num);
443 goto error;
444 }
4f20d592 445 }
b6d5be57
KW
446
447 if (!notrigger) {
ff1ac639 448 if (!trigger_name) {
b6d5be57
KW
449 /*
450 * Build the trigger name. If it is device associated
451 * its name is <device_name>_dev[n] where n matches
452 * the device number found above.
453 */
454 ret = asprintf(&trigger_name,
455 "%s-dev%d", device_name, dev_num);
456 if (ret < 0) {
457 ret = -ENOMEM;
4f20d592 458 goto error;
b6d5be57 459 }
e58537cc 460 }
e58537cc 461
793d6b5e
LW
462 /* Look for this "-devN" trigger */
463 trig_num = find_type_by_name(trigger_name, "trigger");
464 if (trig_num < 0) {
465 /* OK try the simpler "-trigger" suffix instead */
466 free(trigger_name);
467 ret = asprintf(&trigger_name,
468 "%s-trigger", device_name);
469 if (ret < 0) {
470 ret = -ENOMEM;
4f20d592 471 goto error;
793d6b5e
LW
472 }
473 }
474
b6d5be57
KW
475 trig_num = find_type_by_name(trigger_name, "trigger");
476 if (trig_num < 0) {
d9abc615
CO
477 fprintf(stderr, "Failed to find the trigger %s\n",
478 trigger_name);
e83a47cf 479 ret = trig_num;
4f20d592 480 goto error;
b6d5be57 481 }
7663a4aa 482
b6d5be57 483 printf("iio trigger number being used is %d\n", trig_num);
7663a4aa 484 } else {
b6d5be57 485 printf("trigger-less mode selected\n");
7663a4aa 486 }
e58537cc
JC
487
488 /*
489 * Parse the files in scan_elements to identify what channels are
490 * present
491 */
d8da0eee 492 ret = build_channel_array(dev_dir_name, &channels, &num_channels);
e58537cc 493 if (ret) {
d9abc615
CO
494 fprintf(stderr, "Problem reading scan element information\n"
495 "diag %s\n", dev_dir_name);
4f20d592 496 goto error;
e58537cc 497 }
5f991a92
LW
498 if (num_channels && autochannels == AUTOCHANNELS_ENABLED) {
499 fprintf(stderr, "Auto-channels selected but some channels "
500 "are already activated in sysfs\n");
501 fprintf(stderr, "Proceeding without activating any channels\n");
502 }
503
504 if (!num_channels && autochannels == AUTOCHANNELS_ENABLED) {
505 fprintf(stderr,
506 "No channels are enabled, enabling all channels\n");
507
508 ret = enable_disable_all_channels(dev_dir_name, 1);
509 if (ret) {
510 fprintf(stderr, "Failed to enable all channels\n");
4f20d592 511 goto error;
5f991a92
LW
512 }
513
514 /* This flags that we need to disable the channels again */
515 autochannels = AUTOCHANNELS_ACTIVE;
516
517 ret = build_channel_array(dev_dir_name, &channels,
518 &num_channels);
519 if (ret) {
520 fprintf(stderr, "Problem reading scan element "
521 "information\n"
522 "diag %s\n", dev_dir_name);
4f20d592 523 goto error;
5f991a92
LW
524 }
525 if (!num_channels) {
526 fprintf(stderr, "Still no channels after "
527 "auto-enabling, giving up\n");
4f20d592 528 goto error;
5f991a92
LW
529 }
530 }
531
532 if (!num_channels && autochannels == AUTOCHANNELS_DISABLED) {
53dabafe
LW
533 fprintf(stderr,
534 "No channels are enabled, we have nothing to scan.\n");
535 fprintf(stderr, "Enable channels manually in "
536 FORMAT_SCAN_ELEMENTS_DIR
5f991a92
LW
537 "/*_en or pass -a to autoenable channels and "
538 "try again.\n", dev_dir_name);
53dabafe 539 ret = -ENOENT;
4f20d592 540 goto error;
53dabafe 541 }
e58537cc
JC
542
543 /*
544 * Construct the directory name for the associated buffer.
545 * As we know that the lis3l02dq has only one buffer this may
546 * be built rather than found.
547 */
1aa04278
JC
548 ret = asprintf(&buf_dir_name,
549 "%siio:device%d/buffer", iio_dir, dev_num);
e58537cc
JC
550 if (ret < 0) {
551 ret = -ENOMEM;
4f20d592 552 goto error;
e58537cc 553 }
b6d5be57
KW
554
555 if (!notrigger) {
556 printf("%s %s\n", dev_dir_name, trigger_name);
7663a4aa
HK
557 /*
558 * Set the device trigger to be the data ready trigger found
559 * above
560 */
b6d5be57
KW
561 ret = write_sysfs_string_and_verify("trigger/current_trigger",
562 dev_dir_name,
563 trigger_name);
564 if (ret < 0) {
d9abc615
CO
565 fprintf(stderr,
566 "Failed to write current_trigger file\n");
4f20d592 567 goto error;
b6d5be57 568 }
e58537cc
JC
569 }
570
571 /* Setup ring buffer parameters */
572 ret = write_sysfs_int("length", buf_dir_name, buf_len);
573 if (ret < 0)
4f20d592 574 goto error;
e58537cc
JC
575
576 /* Enable the buffer */
577 ret = write_sysfs_int("enable", buf_dir_name, 1);
e7231491
IT
578 if (ret < 0) {
579 fprintf(stderr,
580 "Failed to enable buffer: %s\n", strerror(-ret));
4f20d592 581 goto error;
e7231491 582 }
7663a4aa 583
d8da0eee 584 scan_size = size_from_channelarray(channels, num_channels);
7663a4aa 585 data = malloc(scan_size * buf_len);
e58537cc
JC
586 if (!data) {
587 ret = -ENOMEM;
4f20d592 588 goto error;
e58537cc
JC
589 }
590
1aa04278 591 ret = asprintf(&buffer_access, "/dev/iio:device%d", dev_num);
e58537cc
JC
592 if (ret < 0) {
593 ret = -ENOMEM;
4f20d592 594 goto error;
e58537cc
JC
595 }
596
e58537cc
JC
597 /* Attempt to open non blocking the access dev */
598 fp = open(buffer_access, O_RDONLY | O_NONBLOCK);
7663a4aa 599 if (fp == -1) { /* TODO: If it isn't there make the node */
e58537cc 600 ret = -errno;
d9abc615 601 fprintf(stderr, "Failed to open %s\n", buffer_access);
4f20d592 602 goto error;
e58537cc
JC
603 }
604
e58537cc 605 for (j = 0; j < num_loops; j++) {
30268a3d 606 if (!noevents) {
52615d47
JC
607 struct pollfd pfd = {
608 .fd = fp,
609 .events = POLLIN,
610 };
611
6bb7cac8
HK
612 ret = poll(&pfd, 1, -1);
613 if (ret < 0) {
614 ret = -errno;
4f20d592 615 goto error;
6bb7cac8
HK
616 } else if (ret == 0) {
617 continue;
618 }
619
52615d47 620 toread = buf_len;
30268a3d 621 } else {
96df9799 622 usleep(timedelay);
30268a3d 623 toread = 64;
e58537cc 624 }
30268a3d 625
7663a4aa 626 read_size = read(fp, data, toread * scan_size);
97b603a4 627 if (read_size < 0) {
8749948a 628 if (errno == EAGAIN) {
d9abc615 629 fprintf(stderr, "nothing available\n");
97b603a4 630 continue;
7663a4aa 631 } else {
97b603a4 632 break;
7663a4aa 633 }
e58537cc 634 }
7663a4aa
HK
635 for (i = 0; i < read_size / scan_size; i++)
636 process_scan(data + scan_size * i, channels,
e58537cc
JC
637 num_channels);
638 }
639
4f20d592
CDL
640error:
641 cleanup();
e58537cc 642
4f20d592 643 if (fp >= 0 && close(fp) == -1)
6bb7cac8 644 perror("Failed to close buffer");
e58537cc 645 free(buffer_access);
a71bfb4a 646 free(data);
e58537cc 647 free(buf_dir_name);
63f05c85
HK
648 for (i = num_channels - 1; i >= 0; i--) {
649 free(channels[i].name);
650 free(channels[i].generic_name);
651 }
652 free(channels);
4f20d592 653 free(trigger_name);
de397db8 654 free(device_name);
d3ccfc41 655 free(dev_dir_name);
0e799878 656
e58537cc
JC
657 return ret;
658}