blob: f39c0e9c0d5c92205384279fbc2996aaa2ba8619 [file] [log] [blame]
Jonathan Camerone58537c2010-10-08 12:14:14 +01001/* 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>
Roberta Dobrescubdcb31d2015-02-26 10:49:24 +020022#include <stdlib.h>
Jonathan Camerone58537c2010-10-08 12:14:14 +010023#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>
Jonathan Cameron30268a32011-02-11 13:09:12 +000030#include <string.h>
Jonathan Cameron52615d42011-05-18 14:41:19 +010031#include <poll.h>
Jonathan Cameron117cf8b2011-12-04 19:10:59 +000032#include <endian.h>
Peter Meerwaldbb233782012-06-25 23:12:17 +020033#include <getopt.h>
Peter Meerwald1bcdfbc2012-06-25 23:12:16 +020034#include <inttypes.h>
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +030035#include <stdbool.h>
36#include <signal.h>
Jonathan Camerone58537c2010-10-08 12:14:14 +010037#include "iio_utils.h"
38
Jonathan Camerone58537c2010-10-08 12:14:14 +010039/**
Linus Walleij5f991a92016-04-14 10:26:47 +020040 * enum autochan - state for the automatic channel enabling mechanism
41 */
42enum autochan {
43 AUTOCHANNELS_DISABLED,
44 AUTOCHANNELS_ENABLED,
45 AUTOCHANNELS_ACTIVE,
46};
47
48/**
Jonathan Camerone58537c2010-10-08 12:14:14 +010049 * size_from_channelarray() - calculate the storage size of a scan
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +020050 * @channels: the channel info array
51 * @num_channels: number of channels
Jonathan Camerone58537c2010-10-08 12:14:14 +010052 *
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;
Melike Yurtogluff39a252014-10-03 23:34:50 +030060
Jonathan Camerone58537c2010-10-08 12:14:14 +010061 while (i < num_channels) {
62 if (bytes % channels[i].bytes == 0)
63 channels[i].location = bytes;
64 else
Hartmut Knaack7663a4a2015-06-10 21:51:20 +020065 channels[i].location = bytes - bytes % channels[i].bytes
66 + channels[i].bytes;
67
Jonathan Camerone58537c2010-10-08 12:14:14 +010068 bytes = channels[i].location + channels[i].bytes;
69 i++;
70 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +020071
Jonathan Camerone58537c2010-10-08 12:14:14 +010072 return bytes;
73}
74
Tiberiu Breanae8d09272015-07-03 12:57:36 +030075void 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
Hartmut Knaack8e926132015-05-31 14:39:58 +020092void print2byte(uint16_t input, struct iio_channel_info *info)
Jonathan Cameron52615d42011-05-18 14:41:19 +010093{
Jonathan Cameron117cf8b2011-12-04 19:10:59 +000094 /* First swap if incorrect endian */
Jonathan Cameron117cf8b2011-12-04 19:10:59 +000095 if (info->be)
Hartmut Knaack8e926132015-05-31 14:39:58 +020096 input = be16toh(input);
Jonathan Cameron117cf8b2011-12-04 19:10:59 +000097 else
Hartmut Knaack8e926132015-05-31 14:39:58 +020098 input = le16toh(input);
Jonathan Cameron117cf8b2011-12-04 19:10:59 +000099
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +0200100 /*
101 * Shift before conversion to avoid sign extension
102 * of left aligned data
103 */
Aya Mahfouz1525ecf2015-02-26 11:45:26 +0200104 input >>= info->shift;
Hartmut Knaack8e926132015-05-31 14:39:58 +0200105 input &= info->mask;
Jonathan Cameron52615d42011-05-18 14:41:19 +0100106 if (info->is_signed) {
Hartmut Knaack8e926132015-05-31 14:39:58 +0200107 int16_t val = (int16_t)(input << (16 - info->bits_used)) >>
108 (16 - info->bits_used);
109 printf("%05f ", ((float)val + info->offset) * info->scale);
Jonathan Cameron52615d42011-05-18 14:41:19 +0100110 } else {
Hartmut Knaack8e926132015-05-31 14:39:58 +0200111 printf("%05f ", ((float)input + info->offset) * info->scale);
Jonathan Cameron52615d42011-05-18 14:41:19 +0100112 }
113}
Hartmut Knaack8e926132015-05-31 14:39:58 +0200114
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);
133 } else {
134 printf("%05f ", ((float)input + info->offset) * info->scale);
135 }
136}
137
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);
163 }
164}
165
Jonathan Camerone58537c2010-10-08 12:14:14 +0100166/**
167 * process_scan() - print out the values in SI units
168 * @data: pointer to the start of the scan
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200169 * @channels: information about the channels.
170 * Note: size_from_channelarray must have been called first
171 * to fill the location offsets.
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +0200172 * @num_channels: number of channels
Jonathan Camerone58537c2010-10-08 12:14:14 +0100173 **/
174void process_scan(char *data,
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +0200175 struct iio_channel_info *channels,
Jonathan Camerone58537c2010-10-08 12:14:14 +0100176 int num_channels)
177{
178 int k;
Melike Yurtogluff39a252014-10-03 23:34:50 +0300179
Jonathan Camerone58537c2010-10-08 12:14:14 +0100180 for (k = 0; k < num_channels; k++)
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +0200181 switch (channels[k].bytes) {
Jonathan Camerone58537c2010-10-08 12:14:14 +0100182 /* only a few cases implemented so far */
Tiberiu Breanae8d09272015-07-03 12:57:36 +0300183 case 1:
184 print1byte(*(uint8_t *)(data + channels[k].location),
185 &channels[k]);
186 break;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100187 case 2:
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +0200188 print2byte(*(uint16_t *)(data + channels[k].location),
189 &channels[k]);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100190 break;
Marek Vasut6cffc1f2012-08-12 16:21:00 +0100191 case 4:
Hartmut Knaack8e926132015-05-31 14:39:58 +0200192 print4byte(*(uint32_t *)(data + channels[k].location),
193 &channels[k]);
Marek Vasut6cffc1f2012-08-12 16:21:00 +0100194 break;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100195 case 8:
Hartmut Knaack8e926132015-05-31 14:39:58 +0200196 print8byte(*(uint64_t *)(data + channels[k].location),
197 &channels[k]);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100198 break;
199 default:
200 break;
201 }
202 printf("\n");
203}
204
Linus Walleij5f991a92016-04-14 10:26:47 +0200205static 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
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200245void print_usage(void)
246{
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300247 fprintf(stderr, "Usage: generic_buffer [options]...\n"
248 "Capture, convert and output data from IIO device buffer\n"
Linus Walleij5f991a92016-04-14 10:26:47 +0200249 " -a Auto-activate all available channels\n"
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300250 " -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"
Crestez Dan Leonardde397db2016-05-23 21:39:57 +0300254 " --device-name -n <name>\n"
255 " --device-num -N <num>\n"
256 " Set device by name or number (mandatory)\n"
Crestez Dan Leonard7c7e9da2016-05-23 21:39:58 +0300257 " --trigger-name -t <name>\n"
258 " --trigger-num -T <num>\n"
259 " Set trigger by name or number\n"
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300260 " -w <n> Set delay between reads in us (event-less mode)\n");
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200261}
262
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300263enum autochan autochannels = AUTOCHANNELS_DISABLED;
264char *dev_dir_name = NULL;
265char *buf_dir_name = NULL;
266bool current_trigger_set = false;
267
268void cleanup(void)
269{
270 int ret;
271
272 /* Disable trigger */
273 if (dev_dir_name && current_trigger_set) {
274 /* Disconnect the trigger - just write a dummy name. */
275 ret = write_sysfs_string("trigger/current_trigger",
276 dev_dir_name, "NULL");
277 if (ret < 0)
278 fprintf(stderr, "Failed to disable trigger: %s\n",
279 strerror(-ret));
280 current_trigger_set = false;
281 }
282
283 /* Disable buffer */
284 if (buf_dir_name) {
285 ret = write_sysfs_int("enable", buf_dir_name, 0);
286 if (ret < 0)
287 fprintf(stderr, "Failed to disable buffer: %s\n",
288 strerror(-ret));
289 }
290
291 /* Disable channels if auto-enabled */
292 if (dev_dir_name && autochannels == AUTOCHANNELS_ACTIVE) {
293 ret = enable_disable_all_channels(dev_dir_name, 0);
294 if (ret)
295 fprintf(stderr, "Failed to disable all channels\n");
296 autochannels = AUTOCHANNELS_DISABLED;
297 }
298}
299
300void sig_handler(int signum)
301{
302 fprintf(stderr, "Caught signal %d\n", signum);
303 cleanup();
304 exit(-signum);
305}
306
307void register_cleanup(void)
308{
309 struct sigaction sa = { .sa_handler = sig_handler };
310 const int signums[] = { SIGINT, SIGTERM, SIGABRT };
311 int ret, i;
312
313 for (i = 0; i < ARRAY_SIZE(signums); ++i) {
314 ret = sigaction(signums[i], &sa, NULL);
315 if (ret) {
316 perror("Failed to register signal handler");
317 exit(-1);
318 }
319 }
320}
321
Crestez Dan Leonardde397db2016-05-23 21:39:57 +0300322static const struct option longopts[] = {
323 { "device-name", 1, 0, 'n' },
324 { "device-num", 1, 0, 'N' },
Crestez Dan Leonard7c7e9da2016-05-23 21:39:58 +0300325 { "trigger-name", 1, 0, 't' },
326 { "trigger-num", 1, 0, 'T' },
Crestez Dan Leonardde397db2016-05-23 21:39:57 +0300327 { },
328};
329
Jonathan Camerone58537c2010-10-08 12:14:14 +0100330int main(int argc, char **argv)
331{
Jonathan Cameron96df9792011-02-11 13:09:13 +0000332 unsigned long num_loops = 2;
333 unsigned long timedelay = 1000000;
334 unsigned long buf_len = 128;
335
Jonathan Camerone58537c2010-10-08 12:14:14 +0100336 int ret, c, i, j, toread;
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300337 int fp = -1;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100338
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300339 int num_channels = 0;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100340 char *trigger_name = NULL, *device_name = NULL;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100341
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300342 char *data = NULL;
Jonathan Cameronc77b3812011-04-15 18:56:00 +0100343 ssize_t read_size;
Crestez Dan Leonarddeb4d1f2016-06-03 21:56:29 +0300344 int dev_num = -1, trig_num = -1;
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300345 char *buffer_access = NULL;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100346 int scan_size;
Jonathan Cameron30268a32011-02-11 13:09:12 +0000347 int noevents = 0;
Karol Wronab6d5be572014-11-04 15:29:39 +0100348 int notrigger = 0;
Jonathan Cameron96df9792011-02-11 13:09:13 +0000349 char *dummy;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100350
Alison Schofieldddbc7192016-07-26 20:17:43 -0700351 struct iio_channel_info *channels = NULL;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100352
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300353 register_cleanup();
354
Crestez Dan Leonard7c7e9da2016-05-23 21:39:58 +0300355 while ((c = getopt_long(argc, argv, "ac:egl:n:N:t:T:w:", longopts, NULL)) != -1) {
Jonathan Camerone58537c2010-10-08 12:14:14 +0100356 switch (c) {
Linus Walleij5f991a92016-04-14 10:26:47 +0200357 case 'a':
358 autochannels = AUTOCHANNELS_ENABLED;
359 break;
Jonathan Cameron96df9792011-02-11 13:09:13 +0000360 case 'c':
Hartmut Knaackc8ce9902015-05-31 14:40:03 +0200361 errno = 0;
Jonathan Cameron96df9792011-02-11 13:09:13 +0000362 num_loops = strtoul(optarg, &dummy, 10);
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300363 if (errno) {
364 ret = -errno;
365 goto error;
366 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200367
Jonathan Cameron96df9792011-02-11 13:09:13 +0000368 break;
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200369 case 'e':
370 noevents = 1;
371 break;
372 case 'g':
373 notrigger = 1;
Jonathan Cameron96df9792011-02-11 13:09:13 +0000374 break;
375 case 'l':
Hartmut Knaackc8ce9902015-05-31 14:40:03 +0200376 errno = 0;
Jonathan Cameron96df9792011-02-11 13:09:13 +0000377 buf_len = strtoul(optarg, &dummy, 10);
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300378 if (errno) {
379 ret = -errno;
380 goto error;
381 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200382
Jonathan Cameron96df9792011-02-11 13:09:13 +0000383 break;
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200384 case 'n':
Crestez Dan Leonardde397db2016-05-23 21:39:57 +0300385 device_name = strdup(optarg);
386 break;
387 case 'N':
388 errno = 0;
389 dev_num = strtoul(optarg, &dummy, 10);
390 if (errno) {
391 ret = -errno;
392 goto error;
393 }
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200394 break;
395 case 't':
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300396 trigger_name = strdup(optarg);
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200397 break;
Crestez Dan Leonard7c7e9da2016-05-23 21:39:58 +0300398 case 'T':
399 errno = 0;
400 trig_num = strtoul(optarg, &dummy, 10);
401 if (errno)
402 return -errno;
403 break;
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200404 case 'w':
405 errno = 0;
406 timedelay = strtoul(optarg, &dummy, 10);
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300407 if (errno) {
408 ret = -errno;
409 goto error;
410 }
Karol Wronab6d5be572014-11-04 15:29:39 +0100411 break;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100412 case '?':
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200413 print_usage();
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300414 ret = -1;
415 goto error;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100416 }
417 }
418
419 /* Find the device requested */
Crestez Dan Leonardde397db2016-05-23 21:39:57 +0300420 if (dev_num < 0 && !device_name) {
421 fprintf(stderr, "Device not set\n");
422 print_usage();
423 ret = -1;
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300424 goto error;
Crestez Dan Leonardde397db2016-05-23 21:39:57 +0300425 } else if (dev_num >= 0 && device_name) {
426 fprintf(stderr, "Only one of --device-num or --device-name needs to be set\n");
427 print_usage();
428 ret = -1;
429 goto error;
430 } else if (dev_num < 0) {
431 dev_num = find_type_by_name(device_name, "iio:device");
432 if (dev_num < 0) {
433 fprintf(stderr, "Failed to find the %s\n", device_name);
434 ret = dev_num;
435 goto error;
436 }
Jonathan Camerone58537c2010-10-08 12:14:14 +0100437 }
438 printf("iio device number being used is %d\n", dev_num);
439
Hartmut Knaacke9e45b42015-05-31 14:40:02 +0200440 ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num);
Crestez Dan Leonardde397db2016-05-23 21:39:57 +0300441 if (ret < 0)
442 return -ENOMEM;
443 /* Fetch device_name if specified by number */
444 if (!device_name) {
445 device_name = malloc(IIO_MAX_NAME_LENGTH);
446 if (!device_name) {
447 ret = -ENOMEM;
448 goto error;
449 }
450 ret = read_sysfs_string("name", dev_dir_name, device_name);
451 if (ret < 0) {
452 fprintf(stderr, "Failed to read name of device %d\n", dev_num);
453 goto error;
454 }
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300455 }
Jonathan Camerone58537c2010-10-08 12:14:14 +0100456
Crestez Dan Leonard7c7e9da2016-05-23 21:39:58 +0300457 if (notrigger) {
458 printf("trigger-less mode selected\n");
Gregor Boirief8e81d72016-08-24 15:28:27 +0200459 } else if (trig_num >= 0) {
Crestez Dan Leonard7c7e9da2016-05-23 21:39:58 +0300460 char *trig_dev_name;
461 ret = asprintf(&trig_dev_name, "%strigger%d", iio_dir, trig_num);
462 if (ret < 0) {
463 return -ENOMEM;
464 }
465 trigger_name = malloc(IIO_MAX_NAME_LENGTH);
466 ret = read_sysfs_string("name", trig_dev_name, trigger_name);
467 free(trig_dev_name);
468 if (ret < 0) {
469 fprintf(stderr, "Failed to read trigger%d name from\n", trig_num);
470 return ret;
471 }
472 printf("iio trigger number being used is %d\n", trig_num);
473 } else {
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300474 if (!trigger_name) {
Karol Wronab6d5be572014-11-04 15:29:39 +0100475 /*
476 * Build the trigger name. If it is device associated
477 * its name is <device_name>_dev[n] where n matches
478 * the device number found above.
479 */
480 ret = asprintf(&trigger_name,
481 "%s-dev%d", device_name, dev_num);
482 if (ret < 0) {
483 ret = -ENOMEM;
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300484 goto error;
Karol Wronab6d5be572014-11-04 15:29:39 +0100485 }
486 }
487
Linus Walleij793d6b52016-03-24 09:39:14 +0100488 /* Look for this "-devN" trigger */
489 trig_num = find_type_by_name(trigger_name, "trigger");
490 if (trig_num < 0) {
491 /* OK try the simpler "-trigger" suffix instead */
492 free(trigger_name);
493 ret = asprintf(&trigger_name,
494 "%s-trigger", device_name);
495 if (ret < 0) {
496 ret = -ENOMEM;
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300497 goto error;
Linus Walleij793d6b52016-03-24 09:39:14 +0100498 }
499 }
500
Karol Wronab6d5be572014-11-04 15:29:39 +0100501 trig_num = find_type_by_name(trigger_name, "trigger");
502 if (trig_num < 0) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300503 fprintf(stderr, "Failed to find the trigger %s\n",
504 trigger_name);
Hartmut Knaacke83a47c2015-05-31 14:39:57 +0200505 ret = trig_num;
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300506 goto error;
Karol Wronab6d5be572014-11-04 15:29:39 +0100507 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200508
Karol Wronab6d5be572014-11-04 15:29:39 +0100509 printf("iio trigger number being used is %d\n", trig_num);
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200510 }
Jonathan Camerone58537c2010-10-08 12:14:14 +0100511
512 /*
513 * Parse the files in scan_elements to identify what channels are
514 * present
515 */
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +0200516 ret = build_channel_array(dev_dir_name, &channels, &num_channels);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100517 if (ret) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300518 fprintf(stderr, "Problem reading scan element information\n"
519 "diag %s\n", dev_dir_name);
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300520 goto error;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100521 }
Linus Walleij5f991a92016-04-14 10:26:47 +0200522 if (num_channels && autochannels == AUTOCHANNELS_ENABLED) {
523 fprintf(stderr, "Auto-channels selected but some channels "
524 "are already activated in sysfs\n");
525 fprintf(stderr, "Proceeding without activating any channels\n");
526 }
527
528 if (!num_channels && autochannels == AUTOCHANNELS_ENABLED) {
529 fprintf(stderr,
530 "No channels are enabled, enabling all channels\n");
531
532 ret = enable_disable_all_channels(dev_dir_name, 1);
533 if (ret) {
534 fprintf(stderr, "Failed to enable all channels\n");
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300535 goto error;
Linus Walleij5f991a92016-04-14 10:26:47 +0200536 }
537
538 /* This flags that we need to disable the channels again */
539 autochannels = AUTOCHANNELS_ACTIVE;
540
541 ret = build_channel_array(dev_dir_name, &channels,
542 &num_channels);
543 if (ret) {
544 fprintf(stderr, "Problem reading scan element "
545 "information\n"
546 "diag %s\n", dev_dir_name);
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300547 goto error;
Linus Walleij5f991a92016-04-14 10:26:47 +0200548 }
549 if (!num_channels) {
550 fprintf(stderr, "Still no channels after "
551 "auto-enabling, giving up\n");
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300552 goto error;
Linus Walleij5f991a92016-04-14 10:26:47 +0200553 }
554 }
555
556 if (!num_channels && autochannels == AUTOCHANNELS_DISABLED) {
Linus Walleij53dabaf2015-08-10 10:55:08 +0200557 fprintf(stderr,
558 "No channels are enabled, we have nothing to scan.\n");
559 fprintf(stderr, "Enable channels manually in "
560 FORMAT_SCAN_ELEMENTS_DIR
Linus Walleij5f991a92016-04-14 10:26:47 +0200561 "/*_en or pass -a to autoenable channels and "
562 "try again.\n", dev_dir_name);
Linus Walleij53dabaf2015-08-10 10:55:08 +0200563 ret = -ENOENT;
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300564 goto error;
Linus Walleij53dabaf2015-08-10 10:55:08 +0200565 }
Jonathan Camerone58537c2010-10-08 12:14:14 +0100566
567 /*
568 * Construct the directory name for the associated buffer.
569 * As we know that the lis3l02dq has only one buffer this may
570 * be built rather than found.
571 */
Jonathan Cameron1aa04272011-08-30 12:32:47 +0100572 ret = asprintf(&buf_dir_name,
573 "%siio:device%d/buffer", iio_dir, dev_num);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100574 if (ret < 0) {
575 ret = -ENOMEM;
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300576 goto error;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100577 }
Karol Wronab6d5be572014-11-04 15:29:39 +0100578
579 if (!notrigger) {
580 printf("%s %s\n", dev_dir_name, trigger_name);
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200581 /*
582 * Set the device trigger to be the data ready trigger found
583 * above
584 */
Karol Wronab6d5be572014-11-04 15:29:39 +0100585 ret = write_sysfs_string_and_verify("trigger/current_trigger",
586 dev_dir_name,
587 trigger_name);
588 if (ret < 0) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300589 fprintf(stderr,
590 "Failed to write current_trigger file\n");
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300591 goto error;
Karol Wronab6d5be572014-11-04 15:29:39 +0100592 }
Jonathan Camerone58537c2010-10-08 12:14:14 +0100593 }
594
595 /* Setup ring buffer parameters */
596 ret = write_sysfs_int("length", buf_dir_name, buf_len);
597 if (ret < 0)
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300598 goto error;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100599
600 /* Enable the buffer */
601 ret = write_sysfs_int("enable", buf_dir_name, 1);
Irina Tirdeae7231492015-07-24 16:28:06 +0300602 if (ret < 0) {
603 fprintf(stderr,
604 "Failed to enable buffer: %s\n", strerror(-ret));
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300605 goto error;
Irina Tirdeae7231492015-07-24 16:28:06 +0300606 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200607
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +0200608 scan_size = size_from_channelarray(channels, num_channels);
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200609 data = malloc(scan_size * buf_len);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100610 if (!data) {
611 ret = -ENOMEM;
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300612 goto error;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100613 }
614
Jonathan Cameron1aa04272011-08-30 12:32:47 +0100615 ret = asprintf(&buffer_access, "/dev/iio:device%d", dev_num);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100616 if (ret < 0) {
617 ret = -ENOMEM;
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300618 goto error;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100619 }
620
Jonathan Camerone58537c2010-10-08 12:14:14 +0100621 /* Attempt to open non blocking the access dev */
622 fp = open(buffer_access, O_RDONLY | O_NONBLOCK);
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200623 if (fp == -1) { /* TODO: If it isn't there make the node */
Jonathan Camerone58537c2010-10-08 12:14:14 +0100624 ret = -errno;
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300625 fprintf(stderr, "Failed to open %s\n", buffer_access);
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300626 goto error;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100627 }
628
Jonathan Camerone58537c2010-10-08 12:14:14 +0100629 for (j = 0; j < num_loops; j++) {
Jonathan Cameron30268a32011-02-11 13:09:12 +0000630 if (!noevents) {
Jonathan Cameron52615d42011-05-18 14:41:19 +0100631 struct pollfd pfd = {
632 .fd = fp,
633 .events = POLLIN,
634 };
635
Hartmut Knaack6bb7cac2015-05-31 14:40:04 +0200636 ret = poll(&pfd, 1, -1);
637 if (ret < 0) {
638 ret = -errno;
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300639 goto error;
Hartmut Knaack6bb7cac2015-05-31 14:40:04 +0200640 } else if (ret == 0) {
641 continue;
642 }
643
Jonathan Cameron52615d42011-05-18 14:41:19 +0100644 toread = buf_len;
Jonathan Cameron30268a32011-02-11 13:09:12 +0000645 } else {
Jonathan Cameron96df9792011-02-11 13:09:13 +0000646 usleep(timedelay);
Jonathan Cameron30268a32011-02-11 13:09:12 +0000647 toread = 64;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100648 }
Jonathan Cameron30268a32011-02-11 13:09:12 +0000649
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200650 read_size = read(fp, data, toread * scan_size);
Peter Meerwald97b603a2014-12-06 06:00:00 +0000651 if (read_size < 0) {
Hartmut Knaack87499482015-05-31 14:39:56 +0200652 if (errno == EAGAIN) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300653 fprintf(stderr, "nothing available\n");
Peter Meerwald97b603a2014-12-06 06:00:00 +0000654 continue;
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200655 } else {
Peter Meerwald97b603a2014-12-06 06:00:00 +0000656 break;
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200657 }
Jonathan Camerone58537c2010-10-08 12:14:14 +0100658 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200659 for (i = 0; i < read_size / scan_size; i++)
660 process_scan(data + scan_size * i, channels,
Jonathan Camerone58537c2010-10-08 12:14:14 +0100661 num_channels);
662 }
663
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300664error:
665 cleanup();
Jonathan Camerone58537c2010-10-08 12:14:14 +0100666
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300667 if (fp >= 0 && close(fp) == -1)
Hartmut Knaack6bb7cac2015-05-31 14:40:04 +0200668 perror("Failed to close buffer");
Jonathan Camerone58537c2010-10-08 12:14:14 +0100669 free(buffer_access);
Hartmut Knaacka71bfb42015-05-31 14:39:41 +0200670 free(data);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100671 free(buf_dir_name);
Hartmut Knaack63f05c82015-05-31 14:39:44 +0200672 for (i = num_channels - 1; i >= 0; i--) {
673 free(channels[i].name);
674 free(channels[i].generic_name);
675 }
676 free(channels);
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300677 free(trigger_name);
Crestez Dan Leonardde397db2016-05-23 21:39:57 +0300678 free(device_name);
Hartmut Knaackd3ccfc42015-05-31 14:39:42 +0200679 free(dev_dir_name);
Hartmut Knaack0e799872015-05-31 14:40:17 +0200680
Jonathan Camerone58537c2010-10-08 12:14:14 +0100681 return ret;
682}