1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Industrialio buffer test code.
4 * Copyright (c) 2008 Jonathan Cameron
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
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.
25 #include <linux/types.h>
33 #include "iio_utils.h"
36 * enum autochan - state for the automatic channel enabling mechanism
39 AUTOCHANNELS_DISABLED,
45 * size_from_channelarray() - calculate the storage size of a scan
46 * @channels: the channel info array
47 * @num_channels: number of channels
49 * Has the side effect of filling the channels[i].location values used
50 * in processing the buffer output.
52 int size_from_channelarray(struct iio_channel_info *channels, int num_channels)
57 while (i < num_channels) {
58 if (bytes % channels[i].bytes == 0)
59 channels[i].location = bytes;
61 channels[i].location = bytes - bytes % channels[i].bytes
64 bytes = channels[i].location + channels[i].bytes;
71 void print1byte(uint8_t input, struct iio_channel_info *info)
74 * Shift before conversion to avoid sign extension
75 * of left aligned data
77 input >>= info->shift;
79 if (info->is_signed) {
80 int8_t val = (int8_t)(input << (8 - info->bits_used)) >>
81 (8 - info->bits_used);
82 printf("%05f ", ((float)val + info->offset) * info->scale);
84 printf("%05f ", ((float)input + info->offset) * info->scale);
88 void print2byte(uint16_t input, struct iio_channel_info *info)
90 /* First swap if incorrect endian */
92 input = be16toh(input);
94 input = le16toh(input);
97 * Shift before conversion to avoid sign extension
98 * of left aligned data
100 input >>= info->shift;
102 if (info->is_signed) {
103 int16_t val = (int16_t)(input << (16 - info->bits_used)) >>
104 (16 - info->bits_used);
105 printf("%05f ", ((float)val + info->offset) * info->scale);
107 printf("%05f ", ((float)input + info->offset) * info->scale);
111 void print4byte(uint32_t input, struct iio_channel_info *info)
113 /* First swap if incorrect endian */
115 input = be32toh(input);
117 input = le32toh(input);
120 * Shift before conversion to avoid sign extension
121 * of left aligned data
123 input >>= info->shift;
125 if (info->is_signed) {
126 int32_t val = (int32_t)(input << (32 - info->bits_used)) >>
127 (32 - info->bits_used);
128 printf("%05f ", ((float)val + info->offset) * info->scale);
130 printf("%05f ", ((float)input + info->offset) * info->scale);
134 void print8byte(uint64_t input, struct iio_channel_info *info)
136 /* First swap if incorrect endian */
138 input = be64toh(input);
140 input = le64toh(input);
143 * Shift before conversion to avoid sign extension
144 * of left aligned data
146 input >>= info->shift;
148 if (info->is_signed) {
149 int64_t val = (int64_t)(input << (64 - info->bits_used)) >>
150 (64 - info->bits_used);
151 /* special case for timestamp */
152 if (info->scale == 1.0f && info->offset == 0.0f)
153 printf("%" PRId64 " ", val);
156 ((float)val + info->offset) * info->scale);
158 printf("%05f ", ((float)input + info->offset) * info->scale);
163 * process_scan() - print out the values in SI units
164 * @data: pointer to the start of the scan
165 * @channels: information about the channels.
166 * Note: size_from_channelarray must have been called first
167 * to fill the location offsets.
168 * @num_channels: number of channels
170 void process_scan(char *data,
171 struct iio_channel_info *channels,
176 for (k = 0; k < num_channels; k++)
177 switch (channels[k].bytes) {
178 /* only a few cases implemented so far */
180 print1byte(*(uint8_t *)(data + channels[k].location),
184 print2byte(*(uint16_t *)(data + channels[k].location),
188 print4byte(*(uint32_t *)(data + channels[k].location),
192 print8byte(*(uint64_t *)(data + channels[k].location),
201 static int enable_disable_all_channels(char *dev_dir_name, int enable)
203 const struct dirent *ent;
204 char scanelemdir[256];
208 snprintf(scanelemdir, sizeof(scanelemdir),
209 FORMAT_SCAN_ELEMENTS_DIR, dev_dir_name);
210 scanelemdir[sizeof(scanelemdir)-1] = '\0';
212 dp = opendir(scanelemdir);
214 fprintf(stderr, "Enabling/disabling channels: can't open %s\n",
220 while (ent = readdir(dp), ent) {
221 if (iioutils_check_suffix(ent->d_name, "_en")) {
222 printf("%sabling: %s\n",
223 enable ? "En" : "Dis",
225 ret = write_sysfs_int(ent->d_name, scanelemdir,
228 fprintf(stderr, "Failed to enable/disable %s\n",
233 if (closedir(dp) == -1) {
234 perror("Enabling/disabling channels: "
235 "Failed to close directory");
241 void print_usage(void)
243 fprintf(stderr, "Usage: generic_buffer [options]...\n"
244 "Capture, convert and output data from IIO device buffer\n"
245 " -a Auto-activate all available channels\n"
246 " -A Force-activate ALL channels\n"
247 " -c <n> Do n conversions, or loop forever if n < 0\n"
248 " -e Disable wait for event (new data)\n"
249 " -g Use trigger-less mode\n"
250 " -l <n> Set buffer length to n samples\n"
251 " --device-name -n <name>\n"
252 " --device-num -N <num>\n"
253 " Set device by name or number (mandatory)\n"
254 " --trigger-name -t <name>\n"
255 " --trigger-num -T <num>\n"
256 " Set trigger by name or number\n"
257 " -w <n> Set delay between reads in us (event-less mode)\n");
260 enum autochan autochannels = AUTOCHANNELS_DISABLED;
261 char *dev_dir_name = NULL;
262 char *buf_dir_name = NULL;
263 bool current_trigger_set = false;
269 /* Disable trigger */
270 if (dev_dir_name && current_trigger_set) {
271 /* Disconnect the trigger - just write a dummy name. */
272 ret = write_sysfs_string("trigger/current_trigger",
273 dev_dir_name, "NULL");
275 fprintf(stderr, "Failed to disable trigger: %s\n",
277 current_trigger_set = false;
282 ret = write_sysfs_int("enable", buf_dir_name, 0);
284 fprintf(stderr, "Failed to disable buffer: %s\n",
288 /* Disable channels if auto-enabled */
289 if (dev_dir_name && autochannels == AUTOCHANNELS_ACTIVE) {
290 ret = enable_disable_all_channels(dev_dir_name, 0);
292 fprintf(stderr, "Failed to disable all channels\n");
293 autochannels = AUTOCHANNELS_DISABLED;
297 void sig_handler(int signum)
299 fprintf(stderr, "Caught signal %d\n", signum);
304 void register_cleanup(void)
306 struct sigaction sa = { .sa_handler = sig_handler };
307 const int signums[] = { SIGINT, SIGTERM, SIGABRT };
310 for (i = 0; i < ARRAY_SIZE(signums); ++i) {
311 ret = sigaction(signums[i], &sa, NULL);
313 perror("Failed to register signal handler");
319 static const struct option longopts[] = {
320 { "device-name", 1, 0, 'n' },
321 { "device-num", 1, 0, 'N' },
322 { "trigger-name", 1, 0, 't' },
323 { "trigger-num", 1, 0, 'T' },
327 int main(int argc, char **argv)
329 long long num_loops = 2;
330 unsigned long timedelay = 1000000;
331 unsigned long buf_len = 128;
334 unsigned long long j;
335 unsigned long toread;
339 int num_channels = 0;
340 char *trigger_name = NULL, *device_name = NULL;
344 int dev_num = -1, trig_num = -1;
345 char *buffer_access = NULL;
350 bool force_autochannels = false;
352 struct iio_channel_info *channels = NULL;
356 while ((c = getopt_long(argc, argv, "aAc:egl:n:N:t:T:w:?", longopts,
360 autochannels = AUTOCHANNELS_ENABLED;
363 autochannels = AUTOCHANNELS_ENABLED;
364 force_autochannels = true;
368 num_loops = strtoll(optarg, &dummy, 10);
383 buf_len = strtoul(optarg, &dummy, 10);
391 device_name = strdup(optarg);
395 dev_num = strtoul(optarg, &dummy, 10);
402 trigger_name = strdup(optarg);
406 trig_num = strtoul(optarg, &dummy, 10);
412 timedelay = strtoul(optarg, &dummy, 10);
425 /* Find the device requested */
426 if (dev_num < 0 && !device_name) {
427 fprintf(stderr, "Device not set\n");
431 } else if (dev_num >= 0 && device_name) {
432 fprintf(stderr, "Only one of --device-num or --device-name needs to be set\n");
436 } else if (dev_num < 0) {
437 dev_num = find_type_by_name(device_name, "iio:device");
439 fprintf(stderr, "Failed to find the %s\n", device_name);
444 printf("iio device number being used is %d\n", dev_num);
446 ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num);
449 /* Fetch device_name if specified by number */
451 device_name = malloc(IIO_MAX_NAME_LENGTH);
456 ret = read_sysfs_string("name", dev_dir_name, device_name);
458 fprintf(stderr, "Failed to read name of device %d\n", dev_num);
464 printf("trigger-less mode selected\n");
465 } else if (trig_num >= 0) {
467 ret = asprintf(&trig_dev_name, "%strigger%d", iio_dir, trig_num);
471 trigger_name = malloc(IIO_MAX_NAME_LENGTH);
472 ret = read_sysfs_string("name", trig_dev_name, trigger_name);
475 fprintf(stderr, "Failed to read trigger%d name from\n", trig_num);
478 printf("iio trigger number being used is %d\n", trig_num);
482 * Build the trigger name. If it is device associated
483 * its name is <device_name>_dev[n] where n matches
484 * the device number found above.
486 ret = asprintf(&trigger_name,
487 "%s-dev%d", device_name, dev_num);
494 /* Look for this "-devN" trigger */
495 trig_num = find_type_by_name(trigger_name, "trigger");
497 /* OK try the simpler "-trigger" suffix instead */
499 ret = asprintf(&trigger_name,
500 "%s-trigger", device_name);
507 trig_num = find_type_by_name(trigger_name, "trigger");
509 fprintf(stderr, "Failed to find the trigger %s\n",
515 printf("iio trigger number being used is %d\n", trig_num);
519 * Parse the files in scan_elements to identify what channels are
522 ret = build_channel_array(dev_dir_name, &channels, &num_channels);
524 fprintf(stderr, "Problem reading scan element information\n"
525 "diag %s\n", dev_dir_name);
528 if (num_channels && autochannels == AUTOCHANNELS_ENABLED &&
529 !force_autochannels) {
530 fprintf(stderr, "Auto-channels selected but some channels "
531 "are already activated in sysfs\n");
532 fprintf(stderr, "Proceeding without activating any channels\n");
535 if ((!num_channels && autochannels == AUTOCHANNELS_ENABLED) ||
536 (autochannels == AUTOCHANNELS_ENABLED && force_autochannels)) {
537 fprintf(stderr, "Enabling all channels\n");
539 ret = enable_disable_all_channels(dev_dir_name, 1);
541 fprintf(stderr, "Failed to enable all channels\n");
545 /* This flags that we need to disable the channels again */
546 autochannels = AUTOCHANNELS_ACTIVE;
548 ret = build_channel_array(dev_dir_name, &channels,
551 fprintf(stderr, "Problem reading scan element "
553 "diag %s\n", dev_dir_name);
557 fprintf(stderr, "Still no channels after "
558 "auto-enabling, giving up\n");
563 if (!num_channels && autochannels == AUTOCHANNELS_DISABLED) {
565 "No channels are enabled, we have nothing to scan.\n");
566 fprintf(stderr, "Enable channels manually in "
567 FORMAT_SCAN_ELEMENTS_DIR
568 "/*_en or pass -a to autoenable channels and "
569 "try again.\n", dev_dir_name);
575 * Construct the directory name for the associated buffer.
576 * As we know that the lis3l02dq has only one buffer this may
577 * be built rather than found.
579 ret = asprintf(&buf_dir_name,
580 "%siio:device%d/buffer", iio_dir, dev_num);
587 printf("%s %s\n", dev_dir_name, trigger_name);
589 * Set the device trigger to be the data ready trigger found
592 ret = write_sysfs_string_and_verify("trigger/current_trigger",
597 "Failed to write current_trigger file\n");
602 /* Setup ring buffer parameters */
603 ret = write_sysfs_int("length", buf_dir_name, buf_len);
607 /* Enable the buffer */
608 ret = write_sysfs_int("enable", buf_dir_name, 1);
611 "Failed to enable buffer: %s\n", strerror(-ret));
615 scan_size = size_from_channelarray(channels, num_channels);
616 data = malloc(scan_size * buf_len);
622 ret = asprintf(&buffer_access, "/dev/iio:device%d", dev_num);
628 /* Attempt to open non blocking the access dev */
629 fp = open(buffer_access, O_RDONLY | O_NONBLOCK);
630 if (fp == -1) { /* TODO: If it isn't there make the node */
632 fprintf(stderr, "Failed to open %s\n", buffer_access);
636 for (j = 0; j < num_loops || num_loops < 0; j++) {
638 struct pollfd pfd = {
643 ret = poll(&pfd, 1, -1);
647 } else if (ret == 0) {
657 read_size = read(fp, data, toread * scan_size);
659 if (errno == EAGAIN) {
660 fprintf(stderr, "nothing available\n");
666 for (i = 0; i < read_size / scan_size; i++)
667 process_scan(data + scan_size * i, channels,
674 if (fp >= 0 && close(fp) == -1)
675 perror("Failed to close buffer");
679 for (i = num_channels - 1; i >= 0; i--) {
680 free(channels[i].name);
681 free(channels[i].generic_name);