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