0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #include <unistd.h>
0018 #include <stdlib.h>
0019 #include <dirent.h>
0020 #include <fcntl.h>
0021 #include <stdio.h>
0022 #include <errno.h>
0023 #include <sys/stat.h>
0024 #include <sys/dir.h>
0025 #include <linux/types.h>
0026 #include <string.h>
0027 #include <poll.h>
0028 #include <endian.h>
0029 #include <getopt.h>
0030 #include <inttypes.h>
0031 #include <stdbool.h>
0032 #include <signal.h>
0033 #include <sys/ioctl.h>
0034 #include <linux/iio/buffer.h>
0035 #include "iio_utils.h"
0036
0037
0038
0039
0040 enum autochan {
0041 AUTOCHANNELS_DISABLED,
0042 AUTOCHANNELS_ENABLED,
0043 AUTOCHANNELS_ACTIVE,
0044 };
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054 static int size_from_channelarray(struct iio_channel_info *channels, int num_channels)
0055 {
0056 int bytes = 0;
0057 int i = 0;
0058
0059 while (i < num_channels) {
0060 if (bytes % channels[i].bytes == 0)
0061 channels[i].location = bytes;
0062 else
0063 channels[i].location = bytes - bytes % channels[i].bytes
0064 + channels[i].bytes;
0065
0066 bytes = channels[i].location + channels[i].bytes;
0067 i++;
0068 }
0069
0070 return bytes;
0071 }
0072
0073 static void print1byte(uint8_t input, struct iio_channel_info *info)
0074 {
0075
0076
0077
0078
0079 input >>= info->shift;
0080 input &= info->mask;
0081 if (info->is_signed) {
0082 int8_t val = (int8_t)(input << (8 - info->bits_used)) >>
0083 (8 - info->bits_used);
0084 printf("%05f ", ((float)val + info->offset) * info->scale);
0085 } else {
0086 printf("%05f ", ((float)input + info->offset) * info->scale);
0087 }
0088 }
0089
0090 static void print2byte(uint16_t input, struct iio_channel_info *info)
0091 {
0092
0093 if (info->be)
0094 input = be16toh(input);
0095 else
0096 input = le16toh(input);
0097
0098
0099
0100
0101
0102 input >>= info->shift;
0103 input &= info->mask;
0104 if (info->is_signed) {
0105 int16_t val = (int16_t)(input << (16 - info->bits_used)) >>
0106 (16 - info->bits_used);
0107 printf("%05f ", ((float)val + info->offset) * info->scale);
0108 } else {
0109 printf("%05f ", ((float)input + info->offset) * info->scale);
0110 }
0111 }
0112
0113 static void print4byte(uint32_t input, struct iio_channel_info *info)
0114 {
0115
0116 if (info->be)
0117 input = be32toh(input);
0118 else
0119 input = le32toh(input);
0120
0121
0122
0123
0124
0125 input >>= info->shift;
0126 input &= info->mask;
0127 if (info->is_signed) {
0128 int32_t val = (int32_t)(input << (32 - info->bits_used)) >>
0129 (32 - info->bits_used);
0130 printf("%05f ", ((float)val + info->offset) * info->scale);
0131 } else {
0132 printf("%05f ", ((float)input + info->offset) * info->scale);
0133 }
0134 }
0135
0136 static void print8byte(uint64_t input, struct iio_channel_info *info)
0137 {
0138
0139 if (info->be)
0140 input = be64toh(input);
0141 else
0142 input = le64toh(input);
0143
0144
0145
0146
0147
0148 input >>= info->shift;
0149 input &= info->mask;
0150 if (info->is_signed) {
0151 int64_t val = (int64_t)(input << (64 - info->bits_used)) >>
0152 (64 - info->bits_used);
0153
0154 if (info->scale == 1.0f && info->offset == 0.0f)
0155 printf("%" PRId64 " ", val);
0156 else
0157 printf("%05f ",
0158 ((float)val + info->offset) * info->scale);
0159 } else {
0160 printf("%05f ", ((float)input + info->offset) * info->scale);
0161 }
0162 }
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172 static void process_scan(char *data, struct iio_channel_info *channels,
0173 int num_channels)
0174 {
0175 int k;
0176
0177 for (k = 0; k < num_channels; k++)
0178 switch (channels[k].bytes) {
0179
0180 case 1:
0181 print1byte(*(uint8_t *)(data + channels[k].location),
0182 &channels[k]);
0183 break;
0184 case 2:
0185 print2byte(*(uint16_t *)(data + channels[k].location),
0186 &channels[k]);
0187 break;
0188 case 4:
0189 print4byte(*(uint32_t *)(data + channels[k].location),
0190 &channels[k]);
0191 break;
0192 case 8:
0193 print8byte(*(uint64_t *)(data + channels[k].location),
0194 &channels[k]);
0195 break;
0196 default:
0197 break;
0198 }
0199 printf("\n");
0200 }
0201
0202 static int enable_disable_all_channels(char *dev_dir_name, int buffer_idx, int enable)
0203 {
0204 const struct dirent *ent;
0205 char scanelemdir[256];
0206 DIR *dp;
0207 int ret;
0208
0209 snprintf(scanelemdir, sizeof(scanelemdir),
0210 FORMAT_SCAN_ELEMENTS_DIR, dev_dir_name, buffer_idx);
0211 scanelemdir[sizeof(scanelemdir)-1] = '\0';
0212
0213 dp = opendir(scanelemdir);
0214 if (!dp) {
0215 fprintf(stderr, "Enabling/disabling channels: can't open %s\n",
0216 scanelemdir);
0217 return -EIO;
0218 }
0219
0220 ret = -ENOENT;
0221 while (ent = readdir(dp), ent) {
0222 if (iioutils_check_suffix(ent->d_name, "_en")) {
0223 printf("%sabling: %s\n",
0224 enable ? "En" : "Dis",
0225 ent->d_name);
0226 ret = write_sysfs_int(ent->d_name, scanelemdir,
0227 enable);
0228 if (ret < 0)
0229 fprintf(stderr, "Failed to enable/disable %s\n",
0230 ent->d_name);
0231 }
0232 }
0233
0234 if (closedir(dp) == -1) {
0235 perror("Enabling/disabling channels: "
0236 "Failed to close directory");
0237 return -errno;
0238 }
0239 return 0;
0240 }
0241
0242 static void print_usage(void)
0243 {
0244 fprintf(stderr, "Usage: generic_buffer [options]...\n"
0245 "Capture, convert and output data from IIO device buffer\n"
0246 " -a Auto-activate all available channels\n"
0247 " -A Force-activate ALL channels\n"
0248 " -b <n> The buffer which to open (by index), default 0\n"
0249 " -c <n> Do n conversions, or loop forever if n < 0\n"
0250 " -e Disable wait for event (new data)\n"
0251 " -g Use trigger-less mode\n"
0252 " -l <n> Set buffer length to n samples\n"
0253 " --device-name -n <name>\n"
0254 " --device-num -N <num>\n"
0255 " Set device by name or number (mandatory)\n"
0256 " --trigger-name -t <name>\n"
0257 " --trigger-num -T <num>\n"
0258 " Set trigger by name or number\n"
0259 " -w <n> Set delay between reads in us (event-less mode)\n");
0260 }
0261
0262 static enum autochan autochannels = AUTOCHANNELS_DISABLED;
0263 static char *dev_dir_name = NULL;
0264 static char *buf_dir_name = NULL;
0265 static int buffer_idx = 0;
0266 static bool current_trigger_set = false;
0267
0268 static void cleanup(void)
0269 {
0270 int ret;
0271
0272
0273 if (dev_dir_name && current_trigger_set) {
0274
0275 ret = write_sysfs_string("trigger/current_trigger",
0276 dev_dir_name, "NULL");
0277 if (ret < 0)
0278 fprintf(stderr, "Failed to disable trigger: %s\n",
0279 strerror(-ret));
0280 current_trigger_set = false;
0281 }
0282
0283
0284 if (buf_dir_name) {
0285 ret = write_sysfs_int("enable", buf_dir_name, 0);
0286 if (ret < 0)
0287 fprintf(stderr, "Failed to disable buffer: %s\n",
0288 strerror(-ret));
0289 }
0290
0291
0292 if (dev_dir_name && autochannels == AUTOCHANNELS_ACTIVE) {
0293 ret = enable_disable_all_channels(dev_dir_name, buffer_idx, 0);
0294 if (ret)
0295 fprintf(stderr, "Failed to disable all channels\n");
0296 autochannels = AUTOCHANNELS_DISABLED;
0297 }
0298 }
0299
0300 static void sig_handler(int signum)
0301 {
0302 fprintf(stderr, "Caught signal %d\n", signum);
0303 cleanup();
0304 exit(-signum);
0305 }
0306
0307 static void register_cleanup(void)
0308 {
0309 struct sigaction sa = { .sa_handler = sig_handler };
0310 const int signums[] = { SIGINT, SIGTERM, SIGABRT };
0311 int ret, i;
0312
0313 for (i = 0; i < ARRAY_SIZE(signums); ++i) {
0314 ret = sigaction(signums[i], &sa, NULL);
0315 if (ret) {
0316 perror("Failed to register signal handler");
0317 exit(-1);
0318 }
0319 }
0320 }
0321
0322 static const struct option longopts[] = {
0323 { "device-name", 1, 0, 'n' },
0324 { "device-num", 1, 0, 'N' },
0325 { "trigger-name", 1, 0, 't' },
0326 { "trigger-num", 1, 0, 'T' },
0327 { },
0328 };
0329
0330 int main(int argc, char **argv)
0331 {
0332 long long num_loops = 2;
0333 unsigned long timedelay = 1000000;
0334 unsigned long buf_len = 128;
0335
0336 ssize_t i;
0337 unsigned long long j;
0338 unsigned long toread;
0339 int ret, c;
0340 struct stat st;
0341 int fd = -1;
0342 int buf_fd = -1;
0343
0344 int num_channels = 0;
0345 char *trigger_name = NULL, *device_name = NULL;
0346
0347 char *data = NULL;
0348 ssize_t read_size;
0349 int dev_num = -1, trig_num = -1;
0350 char *buffer_access = NULL;
0351 int scan_size;
0352 int noevents = 0;
0353 int notrigger = 0;
0354 char *dummy;
0355 bool force_autochannels = false;
0356
0357 struct iio_channel_info *channels = NULL;
0358
0359 register_cleanup();
0360
0361 while ((c = getopt_long(argc, argv, "aAb:c:egl:n:N:t:T:w:?", longopts,
0362 NULL)) != -1) {
0363 switch (c) {
0364 case 'a':
0365 autochannels = AUTOCHANNELS_ENABLED;
0366 break;
0367 case 'A':
0368 autochannels = AUTOCHANNELS_ENABLED;
0369 force_autochannels = true;
0370 break;
0371 case 'b':
0372 errno = 0;
0373 buffer_idx = strtoll(optarg, &dummy, 10);
0374 if (errno) {
0375 ret = -errno;
0376 goto error;
0377 }
0378 if (buffer_idx < 0) {
0379 ret = -ERANGE;
0380 goto error;
0381 }
0382
0383 break;
0384 case 'c':
0385 errno = 0;
0386 num_loops = strtoll(optarg, &dummy, 10);
0387 if (errno) {
0388 ret = -errno;
0389 goto error;
0390 }
0391
0392 break;
0393 case 'e':
0394 noevents = 1;
0395 break;
0396 case 'g':
0397 notrigger = 1;
0398 break;
0399 case 'l':
0400 errno = 0;
0401 buf_len = strtoul(optarg, &dummy, 10);
0402 if (errno) {
0403 ret = -errno;
0404 goto error;
0405 }
0406
0407 break;
0408 case 'n':
0409 device_name = strdup(optarg);
0410 break;
0411 case 'N':
0412 errno = 0;
0413 dev_num = strtoul(optarg, &dummy, 10);
0414 if (errno) {
0415 ret = -errno;
0416 goto error;
0417 }
0418 break;
0419 case 't':
0420 trigger_name = strdup(optarg);
0421 break;
0422 case 'T':
0423 errno = 0;
0424 trig_num = strtoul(optarg, &dummy, 10);
0425 if (errno)
0426 return -errno;
0427 break;
0428 case 'w':
0429 errno = 0;
0430 timedelay = strtoul(optarg, &dummy, 10);
0431 if (errno) {
0432 ret = -errno;
0433 goto error;
0434 }
0435 break;
0436 case '?':
0437 print_usage();
0438 ret = -1;
0439 goto error;
0440 }
0441 }
0442
0443
0444 if (dev_num < 0 && !device_name) {
0445 fprintf(stderr, "Device not set\n");
0446 print_usage();
0447 ret = -1;
0448 goto error;
0449 } else if (dev_num >= 0 && device_name) {
0450 fprintf(stderr, "Only one of --device-num or --device-name needs to be set\n");
0451 print_usage();
0452 ret = -1;
0453 goto error;
0454 } else if (dev_num < 0) {
0455 dev_num = find_type_by_name(device_name, "iio:device");
0456 if (dev_num < 0) {
0457 fprintf(stderr, "Failed to find the %s\n", device_name);
0458 ret = dev_num;
0459 goto error;
0460 }
0461 }
0462 printf("iio device number being used is %d\n", dev_num);
0463
0464 ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num);
0465 if (ret < 0)
0466 return -ENOMEM;
0467
0468 if (!device_name) {
0469 device_name = malloc(IIO_MAX_NAME_LENGTH);
0470 if (!device_name) {
0471 ret = -ENOMEM;
0472 goto error;
0473 }
0474 ret = read_sysfs_string("name", dev_dir_name, device_name);
0475 if (ret < 0) {
0476 fprintf(stderr, "Failed to read name of device %d\n", dev_num);
0477 goto error;
0478 }
0479 }
0480
0481 if (notrigger) {
0482 printf("trigger-less mode selected\n");
0483 } else if (trig_num >= 0) {
0484 char *trig_dev_name;
0485 ret = asprintf(&trig_dev_name, "%strigger%d", iio_dir, trig_num);
0486 if (ret < 0) {
0487 return -ENOMEM;
0488 }
0489 trigger_name = malloc(IIO_MAX_NAME_LENGTH);
0490 ret = read_sysfs_string("name", trig_dev_name, trigger_name);
0491 free(trig_dev_name);
0492 if (ret < 0) {
0493 fprintf(stderr, "Failed to read trigger%d name from\n", trig_num);
0494 return ret;
0495 }
0496 printf("iio trigger number being used is %d\n", trig_num);
0497 } else {
0498 if (!trigger_name) {
0499
0500
0501
0502
0503
0504 ret = asprintf(&trigger_name,
0505 "%s-dev%d", device_name, dev_num);
0506 if (ret < 0) {
0507 ret = -ENOMEM;
0508 goto error;
0509 }
0510 }
0511
0512
0513 trig_num = find_type_by_name(trigger_name, "trigger");
0514 if (trig_num < 0) {
0515
0516 free(trigger_name);
0517 ret = asprintf(&trigger_name,
0518 "%s-trigger", device_name);
0519 if (ret < 0) {
0520 ret = -ENOMEM;
0521 goto error;
0522 }
0523 }
0524
0525 trig_num = find_type_by_name(trigger_name, "trigger");
0526 if (trig_num < 0) {
0527 fprintf(stderr, "Failed to find the trigger %s\n",
0528 trigger_name);
0529 ret = trig_num;
0530 goto error;
0531 }
0532
0533 printf("iio trigger number being used is %d\n", trig_num);
0534 }
0535
0536
0537
0538
0539
0540 ret = build_channel_array(dev_dir_name, buffer_idx, &channels, &num_channels);
0541 if (ret) {
0542 fprintf(stderr, "Problem reading scan element information\n"
0543 "diag %s\n", dev_dir_name);
0544 goto error;
0545 }
0546 if (num_channels && autochannels == AUTOCHANNELS_ENABLED &&
0547 !force_autochannels) {
0548 fprintf(stderr, "Auto-channels selected but some channels "
0549 "are already activated in sysfs\n");
0550 fprintf(stderr, "Proceeding without activating any channels\n");
0551 }
0552
0553 if ((!num_channels && autochannels == AUTOCHANNELS_ENABLED) ||
0554 (autochannels == AUTOCHANNELS_ENABLED && force_autochannels)) {
0555 fprintf(stderr, "Enabling all channels\n");
0556
0557 ret = enable_disable_all_channels(dev_dir_name, buffer_idx, 1);
0558 if (ret) {
0559 fprintf(stderr, "Failed to enable all channels\n");
0560 goto error;
0561 }
0562
0563
0564 autochannels = AUTOCHANNELS_ACTIVE;
0565
0566 ret = build_channel_array(dev_dir_name, buffer_idx, &channels,
0567 &num_channels);
0568 if (ret) {
0569 fprintf(stderr, "Problem reading scan element "
0570 "information\n"
0571 "diag %s\n", dev_dir_name);
0572 goto error;
0573 }
0574 if (!num_channels) {
0575 fprintf(stderr, "Still no channels after "
0576 "auto-enabling, giving up\n");
0577 goto error;
0578 }
0579 }
0580
0581 if (!num_channels && autochannels == AUTOCHANNELS_DISABLED) {
0582 fprintf(stderr,
0583 "No channels are enabled, we have nothing to scan.\n");
0584 fprintf(stderr, "Enable channels manually in "
0585 FORMAT_SCAN_ELEMENTS_DIR
0586 "/*_en or pass -a to autoenable channels and "
0587 "try again.\n", dev_dir_name, buffer_idx);
0588 ret = -ENOENT;
0589 goto error;
0590 }
0591
0592
0593
0594
0595
0596
0597 ret = asprintf(&buf_dir_name,
0598 "%siio:device%d/buffer%d", iio_dir, dev_num, buffer_idx);
0599 if (ret < 0) {
0600 ret = -ENOMEM;
0601 goto error;
0602 }
0603
0604 if (stat(buf_dir_name, &st)) {
0605 fprintf(stderr, "Could not stat() '%s', got error %d: %s\n",
0606 buf_dir_name, errno, strerror(errno));
0607 ret = -errno;
0608 goto error;
0609 }
0610
0611 if (!S_ISDIR(st.st_mode)) {
0612 fprintf(stderr, "File '%s' is not a directory\n", buf_dir_name);
0613 ret = -EFAULT;
0614 goto error;
0615 }
0616
0617 if (!notrigger) {
0618 printf("%s %s\n", dev_dir_name, trigger_name);
0619
0620
0621
0622
0623 ret = write_sysfs_string_and_verify("trigger/current_trigger",
0624 dev_dir_name,
0625 trigger_name);
0626 if (ret < 0) {
0627 fprintf(stderr,
0628 "Failed to write current_trigger file\n");
0629 goto error;
0630 }
0631 }
0632
0633 ret = asprintf(&buffer_access, "/dev/iio:device%d", dev_num);
0634 if (ret < 0) {
0635 ret = -ENOMEM;
0636 goto error;
0637 }
0638
0639
0640 fd = open(buffer_access, O_RDONLY | O_NONBLOCK);
0641 if (fd == -1) {
0642 ret = -errno;
0643 fprintf(stderr, "Failed to open %s\n", buffer_access);
0644 goto error;
0645 }
0646
0647
0648 buf_fd = buffer_idx;
0649
0650 ret = ioctl(fd, IIO_BUFFER_GET_FD_IOCTL, &buf_fd);
0651 if (ret == -1 || buf_fd == -1) {
0652 ret = -errno;
0653 if (ret == -ENODEV || ret == -EINVAL)
0654 fprintf(stderr,
0655 "Device does not have this many buffers\n");
0656 else
0657 fprintf(stderr, "Failed to retrieve buffer fd\n");
0658
0659 goto error;
0660 }
0661
0662
0663 ret = write_sysfs_int("length", buf_dir_name, buf_len);
0664 if (ret < 0)
0665 goto error;
0666
0667
0668 ret = write_sysfs_int("enable", buf_dir_name, 1);
0669 if (ret < 0) {
0670 fprintf(stderr,
0671 "Failed to enable buffer '%s': %s\n",
0672 buf_dir_name, strerror(-ret));
0673 goto error;
0674 }
0675
0676 scan_size = size_from_channelarray(channels, num_channels);
0677 data = malloc(scan_size * buf_len);
0678 if (!data) {
0679 ret = -ENOMEM;
0680 goto error;
0681 }
0682
0683
0684
0685
0686
0687
0688 if (buffer_idx == 0) {
0689 errno = 0;
0690 read_size = read(fd, data, 1);
0691 if (read_size > -1 || errno != EBUSY) {
0692 ret = -EFAULT;
0693 perror("Reading from '%s' should not be possible after ioctl()");
0694 goto error;
0695 }
0696 }
0697
0698
0699 if (close(fd) == -1)
0700 perror("Failed to close character device file");
0701 fd = -1;
0702
0703 for (j = 0; j < num_loops || num_loops < 0; j++) {
0704 if (!noevents) {
0705 struct pollfd pfd = {
0706 .fd = buf_fd,
0707 .events = POLLIN,
0708 };
0709
0710 ret = poll(&pfd, 1, -1);
0711 if (ret < 0) {
0712 ret = -errno;
0713 goto error;
0714 } else if (ret == 0) {
0715 continue;
0716 }
0717
0718 toread = buf_len;
0719 } else {
0720 usleep(timedelay);
0721 toread = 64;
0722 }
0723
0724 read_size = read(buf_fd, data, toread * scan_size);
0725 if (read_size < 0) {
0726 if (errno == EAGAIN) {
0727 fprintf(stderr, "nothing available\n");
0728 continue;
0729 } else {
0730 break;
0731 }
0732 }
0733 for (i = 0; i < read_size / scan_size; i++)
0734 process_scan(data + scan_size * i, channels,
0735 num_channels);
0736 }
0737
0738 error:
0739 cleanup();
0740
0741 if (fd >= 0 && close(fd) == -1)
0742 perror("Failed to close character device");
0743 if (buf_fd >= 0 && close(buf_fd) == -1)
0744 perror("Failed to close buffer");
0745 free(buffer_access);
0746 free(data);
0747 free(buf_dir_name);
0748 for (i = num_channels - 1; i >= 0; i--) {
0749 free(channels[i].name);
0750 free(channels[i].generic_name);
0751 }
0752 free(channels);
0753 free(trigger_name);
0754 free(device_name);
0755 free(dev_dir_name);
0756
0757 return ret;
0758 }