blob: 2429c78de94061b0867b9281aeb75fd6bea2553a [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>
Jonathan Camerone58537c2010-10-08 12:14:14 +010035#include "iio_utils.h"
36
Jonathan Camerone58537c2010-10-08 12:14:14 +010037/**
Linus Walleij5f991a92016-04-14 10:26:47 +020038 * enum autochan - state for the automatic channel enabling mechanism
39 */
40enum autochan {
41 AUTOCHANNELS_DISABLED,
42 AUTOCHANNELS_ENABLED,
43 AUTOCHANNELS_ACTIVE,
44};
45
46/**
Jonathan Camerone58537c2010-10-08 12:14:14 +010047 * size_from_channelarray() - calculate the storage size of a scan
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +020048 * @channels: the channel info array
49 * @num_channels: number of channels
Jonathan Camerone58537c2010-10-08 12:14:14 +010050 *
51 * Has the side effect of filling the channels[i].location values used
52 * in processing the buffer output.
53 **/
54int size_from_channelarray(struct iio_channel_info *channels, int num_channels)
55{
56 int bytes = 0;
57 int i = 0;
Melike Yurtogluff39a252014-10-03 23:34:50 +030058
Jonathan Camerone58537c2010-10-08 12:14:14 +010059 while (i < num_channels) {
60 if (bytes % channels[i].bytes == 0)
61 channels[i].location = bytes;
62 else
Hartmut Knaack7663a4a2015-06-10 21:51:20 +020063 channels[i].location = bytes - bytes % channels[i].bytes
64 + channels[i].bytes;
65
Jonathan Camerone58537c2010-10-08 12:14:14 +010066 bytes = channels[i].location + channels[i].bytes;
67 i++;
68 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +020069
Jonathan Camerone58537c2010-10-08 12:14:14 +010070 return bytes;
71}
72
Tiberiu Breanae8d09272015-07-03 12:57:36 +030073void print1byte(uint8_t input, struct iio_channel_info *info)
74{
75 /*
76 * Shift before conversion to avoid sign extension
77 * of left aligned data
78 */
79 input >>= info->shift;
80 input &= info->mask;
81 if (info->is_signed) {
82 int8_t val = (int8_t)(input << (8 - info->bits_used)) >>
83 (8 - info->bits_used);
84 printf("%05f ", ((float)val + info->offset) * info->scale);
85 } else {
86 printf("%05f ", ((float)input + info->offset) * info->scale);
87 }
88}
89
Hartmut Knaack8e926132015-05-31 14:39:58 +020090void print2byte(uint16_t input, struct iio_channel_info *info)
Jonathan Cameron52615d42011-05-18 14:41:19 +010091{
Jonathan Cameron117cf8b2011-12-04 19:10:59 +000092 /* First swap if incorrect endian */
Jonathan Cameron117cf8b2011-12-04 19:10:59 +000093 if (info->be)
Hartmut Knaack8e926132015-05-31 14:39:58 +020094 input = be16toh(input);
Jonathan Cameron117cf8b2011-12-04 19:10:59 +000095 else
Hartmut Knaack8e926132015-05-31 14:39:58 +020096 input = le16toh(input);
Jonathan Cameron117cf8b2011-12-04 19:10:59 +000097
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +020098 /*
99 * Shift before conversion to avoid sign extension
100 * of left aligned data
101 */
Aya Mahfouz1525ecf2015-02-26 11:45:26 +0200102 input >>= info->shift;
Hartmut Knaack8e926132015-05-31 14:39:58 +0200103 input &= info->mask;
Jonathan Cameron52615d42011-05-18 14:41:19 +0100104 if (info->is_signed) {
Hartmut Knaack8e926132015-05-31 14:39:58 +0200105 int16_t val = (int16_t)(input << (16 - info->bits_used)) >>
106 (16 - info->bits_used);
107 printf("%05f ", ((float)val + info->offset) * info->scale);
Jonathan Cameron52615d42011-05-18 14:41:19 +0100108 } else {
Hartmut Knaack8e926132015-05-31 14:39:58 +0200109 printf("%05f ", ((float)input + info->offset) * info->scale);
Jonathan Cameron52615d42011-05-18 14:41:19 +0100110 }
111}
Hartmut Knaack8e926132015-05-31 14:39:58 +0200112
113void print4byte(uint32_t input, struct iio_channel_info *info)
114{
115 /* First swap if incorrect endian */
116 if (info->be)
117 input = be32toh(input);
118 else
119 input = le32toh(input);
120
121 /*
122 * Shift before conversion to avoid sign extension
123 * of left aligned data
124 */
125 input >>= info->shift;
126 input &= info->mask;
127 if (info->is_signed) {
128 int32_t val = (int32_t)(input << (32 - info->bits_used)) >>
129 (32 - info->bits_used);
130 printf("%05f ", ((float)val + info->offset) * info->scale);
131 } else {
132 printf("%05f ", ((float)input + info->offset) * info->scale);
133 }
134}
135
136void print8byte(uint64_t input, struct iio_channel_info *info)
137{
138 /* First swap if incorrect endian */
139 if (info->be)
140 input = be64toh(input);
141 else
142 input = le64toh(input);
143
144 /*
145 * Shift before conversion to avoid sign extension
146 * of left aligned data
147 */
148 input >>= info->shift;
149 input &= info->mask;
150 if (info->is_signed) {
151 int64_t val = (int64_t)(input << (64 - info->bits_used)) >>
152 (64 - info->bits_used);
153 /* special case for timestamp */
154 if (info->scale == 1.0f && info->offset == 0.0f)
155 printf("%" PRId64 " ", val);
156 else
157 printf("%05f ",
158 ((float)val + info->offset) * info->scale);
159 } else {
160 printf("%05f ", ((float)input + info->offset) * info->scale);
161 }
162}
163
Jonathan Camerone58537c2010-10-08 12:14:14 +0100164/**
165 * process_scan() - print out the values in SI units
166 * @data: pointer to the start of the scan
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200167 * @channels: information about the channels.
168 * Note: size_from_channelarray must have been called first
169 * to fill the location offsets.
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +0200170 * @num_channels: number of channels
Jonathan Camerone58537c2010-10-08 12:14:14 +0100171 **/
172void process_scan(char *data,
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +0200173 struct iio_channel_info *channels,
Jonathan Camerone58537c2010-10-08 12:14:14 +0100174 int num_channels)
175{
176 int k;
Melike Yurtogluff39a252014-10-03 23:34:50 +0300177
Jonathan Camerone58537c2010-10-08 12:14:14 +0100178 for (k = 0; k < num_channels; k++)
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +0200179 switch (channels[k].bytes) {
Jonathan Camerone58537c2010-10-08 12:14:14 +0100180 /* only a few cases implemented so far */
Tiberiu Breanae8d09272015-07-03 12:57:36 +0300181 case 1:
182 print1byte(*(uint8_t *)(data + channels[k].location),
183 &channels[k]);
184 break;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100185 case 2:
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +0200186 print2byte(*(uint16_t *)(data + channels[k].location),
187 &channels[k]);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100188 break;
Marek Vasut6cffc1f2012-08-12 16:21:00 +0100189 case 4:
Hartmut Knaack8e926132015-05-31 14:39:58 +0200190 print4byte(*(uint32_t *)(data + channels[k].location),
191 &channels[k]);
Marek Vasut6cffc1f2012-08-12 16:21:00 +0100192 break;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100193 case 8:
Hartmut Knaack8e926132015-05-31 14:39:58 +0200194 print8byte(*(uint64_t *)(data + channels[k].location),
195 &channels[k]);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100196 break;
197 default:
198 break;
199 }
200 printf("\n");
201}
202
Linus Walleij5f991a92016-04-14 10:26:47 +0200203static int enable_disable_all_channels(char *dev_dir_name, int enable)
204{
205 const struct dirent *ent;
206 char scanelemdir[256];
207 DIR *dp;
208 int ret;
209
210 snprintf(scanelemdir, sizeof(scanelemdir),
211 FORMAT_SCAN_ELEMENTS_DIR, dev_dir_name);
212 scanelemdir[sizeof(scanelemdir)-1] = '\0';
213
214 dp = opendir(scanelemdir);
215 if (!dp) {
216 fprintf(stderr, "Enabling/disabling channels: can't open %s\n",
217 scanelemdir);
218 return -EIO;
219 }
220
221 ret = -ENOENT;
222 while (ent = readdir(dp), ent) {
223 if (iioutils_check_suffix(ent->d_name, "_en")) {
224 printf("%sabling: %s\n",
225 enable ? "En" : "Dis",
226 ent->d_name);
227 ret = write_sysfs_int(ent->d_name, scanelemdir,
228 enable);
229 if (ret < 0)
230 fprintf(stderr, "Failed to enable/disable %s\n",
231 ent->d_name);
232 }
233 }
234
235 if (closedir(dp) == -1) {
236 perror("Enabling/disabling channels: "
237 "Failed to close directory");
238 return -errno;
239 }
240 return 0;
241}
242
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200243void print_usage(void)
244{
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300245 fprintf(stderr, "Usage: generic_buffer [options]...\n"
246 "Capture, convert and output data from IIO device buffer\n"
Linus Walleij5f991a92016-04-14 10:26:47 +0200247 " -a Auto-activate all available channels\n"
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300248 " -c <n> Do n conversions\n"
249 " -e Disable wait for event (new data)\n"
250 " -g Use trigger-less mode\n"
251 " -l <n> Set buffer length to n samples\n"
252 " -n <name> Set device name (mandatory)\n"
253 " -t <name> Set trigger name\n"
254 " -w <n> Set delay between reads in us (event-less mode)\n");
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200255}
256
Jonathan Camerone58537c2010-10-08 12:14:14 +0100257int main(int argc, char **argv)
258{
Jonathan Cameron96df9792011-02-11 13:09:13 +0000259 unsigned long num_loops = 2;
260 unsigned long timedelay = 1000000;
261 unsigned long buf_len = 128;
262
Jonathan Camerone58537c2010-10-08 12:14:14 +0100263 int ret, c, i, j, toread;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100264 int fp;
265
266 int num_channels;
267 char *trigger_name = NULL, *device_name = NULL;
268 char *dev_dir_name, *buf_dir_name;
269
270 int datardytrigger = 1;
271 char *data;
Jonathan Cameronc77b3812011-04-15 18:56:00 +0100272 ssize_t read_size;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100273 int dev_num, trig_num;
Jonathan Cameron52615d42011-05-18 14:41:19 +0100274 char *buffer_access;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100275 int scan_size;
Jonathan Cameron30268a32011-02-11 13:09:12 +0000276 int noevents = 0;
Karol Wronab6d5be572014-11-04 15:29:39 +0100277 int notrigger = 0;
Linus Walleij5f991a92016-04-14 10:26:47 +0200278 enum autochan autochannels = AUTOCHANNELS_DISABLED;
Jonathan Cameron96df9792011-02-11 13:09:13 +0000279 char *dummy;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100280
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +0200281 struct iio_channel_info *channels;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100282
Linus Walleij5f991a92016-04-14 10:26:47 +0200283 while ((c = getopt(argc, argv, "ac:egl:n:t:w:")) != -1) {
Jonathan Camerone58537c2010-10-08 12:14:14 +0100284 switch (c) {
Linus Walleij5f991a92016-04-14 10:26:47 +0200285 case 'a':
286 autochannels = AUTOCHANNELS_ENABLED;
287 break;
Jonathan Cameron96df9792011-02-11 13:09:13 +0000288 case 'c':
Hartmut Knaackc8ce9902015-05-31 14:40:03 +0200289 errno = 0;
Jonathan Cameron96df9792011-02-11 13:09:13 +0000290 num_loops = strtoul(optarg, &dummy, 10);
Hartmut Knaackc8ce9902015-05-31 14:40:03 +0200291 if (errno)
292 return -errno;
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200293
Jonathan Cameron96df9792011-02-11 13:09:13 +0000294 break;
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200295 case 'e':
296 noevents = 1;
297 break;
298 case 'g':
299 notrigger = 1;
Jonathan Cameron96df9792011-02-11 13:09:13 +0000300 break;
301 case 'l':
Hartmut Knaackc8ce9902015-05-31 14:40:03 +0200302 errno = 0;
Jonathan Cameron96df9792011-02-11 13:09:13 +0000303 buf_len = strtoul(optarg, &dummy, 10);
Hartmut Knaackc8ce9902015-05-31 14:40:03 +0200304 if (errno)
305 return -errno;
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200306
Jonathan Cameron96df9792011-02-11 13:09:13 +0000307 break;
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200308 case 'n':
309 device_name = optarg;
310 break;
311 case 't':
312 trigger_name = optarg;
313 datardytrigger = 0;
314 break;
315 case 'w':
316 errno = 0;
317 timedelay = strtoul(optarg, &dummy, 10);
318 if (errno)
319 return -errno;
Karol Wronab6d5be572014-11-04 15:29:39 +0100320 break;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100321 case '?':
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200322 print_usage();
Jonathan Camerone58537c2010-10-08 12:14:14 +0100323 return -1;
324 }
325 }
326
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300327 if (!device_name) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300328 fprintf(stderr, "Device name not set\n");
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200329 print_usage();
Michael Hennerich065896e2011-02-24 16:34:50 +0100330 return -1;
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200331 }
Michael Hennerich065896e2011-02-24 16:34:50 +0100332
Jonathan Camerone58537c2010-10-08 12:14:14 +0100333 /* Find the device requested */
Jonathan Cameron1aa04272011-08-30 12:32:47 +0100334 dev_num = find_type_by_name(device_name, "iio:device");
Jonathan Camerone58537c2010-10-08 12:14:14 +0100335 if (dev_num < 0) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300336 fprintf(stderr, "Failed to find the %s\n", device_name);
Hartmut Knaack0e799872015-05-31 14:40:17 +0200337 return dev_num;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100338 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200339
Jonathan Camerone58537c2010-10-08 12:14:14 +0100340 printf("iio device number being used is %d\n", dev_num);
341
Hartmut Knaacke9e45b42015-05-31 14:40:02 +0200342 ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num);
343 if (ret < 0)
344 return -ENOMEM;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100345
Karol Wronab6d5be572014-11-04 15:29:39 +0100346 if (!notrigger) {
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300347 if (!trigger_name) {
Karol Wronab6d5be572014-11-04 15:29:39 +0100348 /*
349 * Build the trigger name. If it is device associated
350 * its name is <device_name>_dev[n] where n matches
351 * the device number found above.
352 */
353 ret = asprintf(&trigger_name,
354 "%s-dev%d", device_name, dev_num);
355 if (ret < 0) {
356 ret = -ENOMEM;
Hartmut Knaackd3ccfc42015-05-31 14:39:42 +0200357 goto error_free_dev_dir_name;
Karol Wronab6d5be572014-11-04 15:29:39 +0100358 }
359 }
360
Linus Walleij793d6b52016-03-24 09:39:14 +0100361 /* Look for this "-devN" trigger */
362 trig_num = find_type_by_name(trigger_name, "trigger");
363 if (trig_num < 0) {
364 /* OK try the simpler "-trigger" suffix instead */
365 free(trigger_name);
366 ret = asprintf(&trigger_name,
367 "%s-trigger", device_name);
368 if (ret < 0) {
369 ret = -ENOMEM;
370 goto error_free_dev_dir_name;
371 }
372 }
373
Karol Wronab6d5be572014-11-04 15:29:39 +0100374 trig_num = find_type_by_name(trigger_name, "trigger");
375 if (trig_num < 0) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300376 fprintf(stderr, "Failed to find the trigger %s\n",
377 trigger_name);
Hartmut Knaacke83a47c2015-05-31 14:39:57 +0200378 ret = trig_num;
Karol Wronab6d5be572014-11-04 15:29:39 +0100379 goto error_free_triggername;
380 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200381
Karol Wronab6d5be572014-11-04 15:29:39 +0100382 printf("iio trigger number being used is %d\n", trig_num);
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200383 } else {
Karol Wronab6d5be572014-11-04 15:29:39 +0100384 printf("trigger-less mode selected\n");
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200385 }
Jonathan Camerone58537c2010-10-08 12:14:14 +0100386
387 /*
388 * Parse the files in scan_elements to identify what channels are
389 * present
390 */
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +0200391 ret = build_channel_array(dev_dir_name, &channels, &num_channels);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100392 if (ret) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300393 fprintf(stderr, "Problem reading scan element information\n"
394 "diag %s\n", dev_dir_name);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100395 goto error_free_triggername;
396 }
Linus Walleij5f991a92016-04-14 10:26:47 +0200397 if (num_channels && autochannels == AUTOCHANNELS_ENABLED) {
398 fprintf(stderr, "Auto-channels selected but some channels "
399 "are already activated in sysfs\n");
400 fprintf(stderr, "Proceeding without activating any channels\n");
401 }
402
403 if (!num_channels && autochannels == AUTOCHANNELS_ENABLED) {
404 fprintf(stderr,
405 "No channels are enabled, enabling all channels\n");
406
407 ret = enable_disable_all_channels(dev_dir_name, 1);
408 if (ret) {
409 fprintf(stderr, "Failed to enable all channels\n");
410 goto error_free_triggername;
411 }
412
413 /* This flags that we need to disable the channels again */
414 autochannels = AUTOCHANNELS_ACTIVE;
415
416 ret = build_channel_array(dev_dir_name, &channels,
417 &num_channels);
418 if (ret) {
419 fprintf(stderr, "Problem reading scan element "
420 "information\n"
421 "diag %s\n", dev_dir_name);
422 goto error_disable_channels;
423 }
424 if (!num_channels) {
425 fprintf(stderr, "Still no channels after "
426 "auto-enabling, giving up\n");
427 goto error_disable_channels;
428 }
429 }
430
431 if (!num_channels && autochannels == AUTOCHANNELS_DISABLED) {
Linus Walleij53dabaf2015-08-10 10:55:08 +0200432 fprintf(stderr,
433 "No channels are enabled, we have nothing to scan.\n");
434 fprintf(stderr, "Enable channels manually in "
435 FORMAT_SCAN_ELEMENTS_DIR
Linus Walleij5f991a92016-04-14 10:26:47 +0200436 "/*_en or pass -a to autoenable channels and "
437 "try again.\n", dev_dir_name);
Linus Walleij53dabaf2015-08-10 10:55:08 +0200438 ret = -ENOENT;
439 goto error_free_triggername;
440 }
Jonathan Camerone58537c2010-10-08 12:14:14 +0100441
442 /*
443 * Construct the directory name for the associated buffer.
444 * As we know that the lis3l02dq has only one buffer this may
445 * be built rather than found.
446 */
Jonathan Cameron1aa04272011-08-30 12:32:47 +0100447 ret = asprintf(&buf_dir_name,
448 "%siio:device%d/buffer", iio_dir, dev_num);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100449 if (ret < 0) {
450 ret = -ENOMEM;
Hartmut Knaack63f05c82015-05-31 14:39:44 +0200451 goto error_free_channels;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100452 }
Karol Wronab6d5be572014-11-04 15:29:39 +0100453
454 if (!notrigger) {
455 printf("%s %s\n", dev_dir_name, trigger_name);
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200456 /*
457 * Set the device trigger to be the data ready trigger found
458 * above
459 */
Karol Wronab6d5be572014-11-04 15:29:39 +0100460 ret = write_sysfs_string_and_verify("trigger/current_trigger",
461 dev_dir_name,
462 trigger_name);
463 if (ret < 0) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300464 fprintf(stderr,
465 "Failed to write current_trigger file\n");
Karol Wronab6d5be572014-11-04 15:29:39 +0100466 goto error_free_buf_dir_name;
467 }
Jonathan Camerone58537c2010-10-08 12:14:14 +0100468 }
469
470 /* Setup ring buffer parameters */
471 ret = write_sysfs_int("length", buf_dir_name, buf_len);
472 if (ret < 0)
473 goto error_free_buf_dir_name;
474
475 /* Enable the buffer */
476 ret = write_sysfs_int("enable", buf_dir_name, 1);
Irina Tirdeae7231492015-07-24 16:28:06 +0300477 if (ret < 0) {
478 fprintf(stderr,
479 "Failed to enable buffer: %s\n", strerror(-ret));
Jonathan Camerone58537c2010-10-08 12:14:14 +0100480 goto error_free_buf_dir_name;
Irina Tirdeae7231492015-07-24 16:28:06 +0300481 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200482
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +0200483 scan_size = size_from_channelarray(channels, num_channels);
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200484 data = malloc(scan_size * buf_len);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100485 if (!data) {
486 ret = -ENOMEM;
487 goto error_free_buf_dir_name;
488 }
489
Jonathan Cameron1aa04272011-08-30 12:32:47 +0100490 ret = asprintf(&buffer_access, "/dev/iio:device%d", dev_num);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100491 if (ret < 0) {
492 ret = -ENOMEM;
493 goto error_free_data;
494 }
495
Jonathan Camerone58537c2010-10-08 12:14:14 +0100496 /* Attempt to open non blocking the access dev */
497 fp = open(buffer_access, O_RDONLY | O_NONBLOCK);
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200498 if (fp == -1) { /* TODO: If it isn't there make the node */
Jonathan Camerone58537c2010-10-08 12:14:14 +0100499 ret = -errno;
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300500 fprintf(stderr, "Failed to open %s\n", buffer_access);
Jonathan Cameron52615d42011-05-18 14:41:19 +0100501 goto error_free_buffer_access;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100502 }
503
Jonathan Camerone58537c2010-10-08 12:14:14 +0100504 for (j = 0; j < num_loops; j++) {
Jonathan Cameron30268a32011-02-11 13:09:12 +0000505 if (!noevents) {
Jonathan Cameron52615d42011-05-18 14:41:19 +0100506 struct pollfd pfd = {
507 .fd = fp,
508 .events = POLLIN,
509 };
510
Hartmut Knaack6bb7cac2015-05-31 14:40:04 +0200511 ret = poll(&pfd, 1, -1);
512 if (ret < 0) {
513 ret = -errno;
514 goto error_close_buffer_access;
515 } else if (ret == 0) {
516 continue;
517 }
518
Jonathan Cameron52615d42011-05-18 14:41:19 +0100519 toread = buf_len;
Jonathan Cameron30268a32011-02-11 13:09:12 +0000520 } else {
Jonathan Cameron96df9792011-02-11 13:09:13 +0000521 usleep(timedelay);
Jonathan Cameron30268a32011-02-11 13:09:12 +0000522 toread = 64;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100523 }
Jonathan Cameron30268a32011-02-11 13:09:12 +0000524
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200525 read_size = read(fp, data, toread * scan_size);
Peter Meerwald97b603a2014-12-06 06:00:00 +0000526 if (read_size < 0) {
Hartmut Knaack87499482015-05-31 14:39:56 +0200527 if (errno == EAGAIN) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300528 fprintf(stderr, "nothing available\n");
Peter Meerwald97b603a2014-12-06 06:00:00 +0000529 continue;
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200530 } else {
Peter Meerwald97b603a2014-12-06 06:00:00 +0000531 break;
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200532 }
Jonathan Camerone58537c2010-10-08 12:14:14 +0100533 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200534 for (i = 0; i < read_size / scan_size; i++)
535 process_scan(data + scan_size * i, channels,
Jonathan Camerone58537c2010-10-08 12:14:14 +0100536 num_channels);
537 }
538
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +0200539 /* Stop the buffer */
Jonathan Camerone58537c2010-10-08 12:14:14 +0100540 ret = write_sysfs_int("enable", buf_dir_name, 0);
541 if (ret < 0)
Jonathan Cameron52615d42011-05-18 14:41:19 +0100542 goto error_close_buffer_access;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100543
Karol Wronab6d5be572014-11-04 15:29:39 +0100544 if (!notrigger)
545 /* Disconnect the trigger - just write a dummy name. */
Hartmut Knaack6bb7cac2015-05-31 14:40:04 +0200546 ret = write_sysfs_string("trigger/current_trigger",
547 dev_dir_name, "NULL");
548 if (ret < 0)
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300549 fprintf(stderr, "Failed to write to %s\n",
550 dev_dir_name);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100551
Jonathan Camerone58537c2010-10-08 12:14:14 +0100552error_close_buffer_access:
Hartmut Knaack6bb7cac2015-05-31 14:40:04 +0200553 if (close(fp) == -1)
554 perror("Failed to close buffer");
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200555
Jonathan Camerone58537c2010-10-08 12:14:14 +0100556error_free_buffer_access:
557 free(buffer_access);
Hartmut Knaacka71bfb42015-05-31 14:39:41 +0200558error_free_data:
559 free(data);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100560error_free_buf_dir_name:
561 free(buf_dir_name);
Hartmut Knaack63f05c82015-05-31 14:39:44 +0200562error_free_channels:
563 for (i = num_channels - 1; i >= 0; i--) {
564 free(channels[i].name);
565 free(channels[i].generic_name);
566 }
567 free(channels);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100568error_free_triggername:
569 if (datardytrigger)
570 free(trigger_name);
Linus Walleij5f991a92016-04-14 10:26:47 +0200571error_disable_channels:
572 if (autochannels == AUTOCHANNELS_ACTIVE) {
573 ret = enable_disable_all_channels(dev_dir_name, 0);
574 if (ret)
575 fprintf(stderr, "Failed to disable all channels\n");
576 }
Hartmut Knaackd3ccfc42015-05-31 14:39:42 +0200577error_free_dev_dir_name:
578 free(dev_dir_name);
Hartmut Knaack0e799872015-05-31 14:40:17 +0200579
Jonathan Camerone58537c2010-10-08 12:14:14 +0100580 return ret;
581}