Commit | Line | Data |
---|---|---|
e58537cc JC |
1 | /* 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> | |
bdcb31d0 | 22 | #include <stdlib.h> |
e58537cc JC |
23 | #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> | |
30268a3d | 30 | #include <string.h> |
52615d47 | 31 | #include <poll.h> |
117cf8b7 | 32 | #include <endian.h> |
bb23378c | 33 | #include <getopt.h> |
1bcdfbcf | 34 | #include <inttypes.h> |
4f20d592 CDL |
35 | #include <stdbool.h> |
36 | #include <signal.h> | |
e58537cc JC |
37 | #include "iio_utils.h" |
38 | ||
5f991a92 LW |
39 | /** |
40 | * enum autochan - state for the automatic channel enabling mechanism | |
41 | */ | |
42 | enum autochan { | |
43 | AUTOCHANNELS_DISABLED, | |
44 | AUTOCHANNELS_ENABLED, | |
45 | AUTOCHANNELS_ACTIVE, | |
46 | }; | |
47 | ||
e58537cc JC |
48 | /** |
49 | * size_from_channelarray() - calculate the storage size of a scan | |
d8da0eee PM |
50 | * @channels: the channel info array |
51 | * @num_channels: number of channels | |
e58537cc JC |
52 | * |
53 | * Has the side effect of filling the channels[i].location values used | |
54 | * in processing the buffer output. | |
55 | **/ | |
56 | int size_from_channelarray(struct iio_channel_info *channels, int num_channels) | |
57 | { | |
58 | int bytes = 0; | |
59 | int i = 0; | |
ff39a252 | 60 | |
e58537cc JC |
61 | while (i < num_channels) { |
62 | if (bytes % channels[i].bytes == 0) | |
63 | channels[i].location = bytes; | |
64 | else | |
7663a4aa HK |
65 | channels[i].location = bytes - bytes % channels[i].bytes |
66 | + channels[i].bytes; | |
67 | ||
e58537cc JC |
68 | bytes = channels[i].location + channels[i].bytes; |
69 | i++; | |
70 | } | |
7663a4aa | 71 | |
e58537cc JC |
72 | return bytes; |
73 | } | |
74 | ||
e8d0927a TB |
75 | void 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 | ||
8e926134 | 92 | void print2byte(uint16_t input, struct iio_channel_info *info) |
52615d47 | 93 | { |
117cf8b7 | 94 | /* First swap if incorrect endian */ |
117cf8b7 | 95 | if (info->be) |
8e926134 | 96 | input = be16toh(input); |
117cf8b7 | 97 | else |
8e926134 | 98 | input = le16toh(input); |
117cf8b7 | 99 | |
d8da0eee PM |
100 | /* |
101 | * Shift before conversion to avoid sign extension | |
102 | * of left aligned data | |
103 | */ | |
1525ecfc | 104 | input >>= info->shift; |
8e926134 | 105 | input &= info->mask; |
52615d47 | 106 | if (info->is_signed) { |
8e926134 HK |
107 | int16_t val = (int16_t)(input << (16 - info->bits_used)) >> |
108 | (16 - info->bits_used); | |
109 | printf("%05f ", ((float)val + info->offset) * info->scale); | |
110 | } else { | |
111 | printf("%05f ", ((float)input + info->offset) * info->scale); | |
112 | } | |
113 | } | |
ff39a252 | 114 | |
8e926134 HK |
115 | void 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); | |
52615d47 | 133 | } else { |
8e926134 HK |
134 | printf("%05f ", ((float)input + info->offset) * info->scale); |
135 | } | |
136 | } | |
ff39a252 | 137 | |
8e926134 HK |
138 | void 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); | |
52615d47 JC |
163 | } |
164 | } | |
8e926134 | 165 | |
e58537cc JC |
166 | /** |
167 | * process_scan() - print out the values in SI units | |
168 | * @data: pointer to the start of the scan | |
7663a4aa HK |
169 | * @channels: information about the channels. |
170 | * Note: size_from_channelarray must have been called first | |
171 | * to fill the location offsets. | |
d8da0eee | 172 | * @num_channels: number of channels |
e58537cc JC |
173 | **/ |
174 | void process_scan(char *data, | |
d8da0eee | 175 | struct iio_channel_info *channels, |
e58537cc JC |
176 | int num_channels) |
177 | { | |
178 | int k; | |
ff39a252 | 179 | |
e58537cc | 180 | for (k = 0; k < num_channels; k++) |
d8da0eee | 181 | switch (channels[k].bytes) { |
e58537cc | 182 | /* only a few cases implemented so far */ |
e8d0927a TB |
183 | case 1: |
184 | print1byte(*(uint8_t *)(data + channels[k].location), | |
185 | &channels[k]); | |
186 | break; | |
e58537cc | 187 | case 2: |
d8da0eee PM |
188 | print2byte(*(uint16_t *)(data + channels[k].location), |
189 | &channels[k]); | |
e58537cc | 190 | break; |
6cffc1f8 | 191 | case 4: |
8e926134 HK |
192 | print4byte(*(uint32_t *)(data + channels[k].location), |
193 | &channels[k]); | |
6cffc1f8 | 194 | break; |
e58537cc | 195 | case 8: |
8e926134 HK |
196 | print8byte(*(uint64_t *)(data + channels[k].location), |
197 | &channels[k]); | |
e58537cc JC |
198 | break; |
199 | default: | |
200 | break; | |
201 | } | |
202 | printf("\n"); | |
203 | } | |
204 | ||
5f991a92 LW |
205 | static 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 | ||
e06e3d71 HK |
245 | void print_usage(void) |
246 | { | |
d9abc615 CO |
247 | fprintf(stderr, "Usage: generic_buffer [options]...\n" |
248 | "Capture, convert and output data from IIO device buffer\n" | |
5f991a92 | 249 | " -a Auto-activate all available channels\n" |
d9abc615 CO |
250 | " -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" | |
de397db8 CDL |
254 | " --device-name -n <name>\n" |
255 | " --device-num -N <num>\n" | |
256 | " Set device by name or number (mandatory)\n" | |
7c7e9dad CDL |
257 | " --trigger-name -t <name>\n" |
258 | " --trigger-num -T <num>\n" | |
259 | " Set trigger by name or number\n" | |
d9abc615 | 260 | " -w <n> Set delay between reads in us (event-less mode)\n"); |
e06e3d71 HK |
261 | } |
262 | ||
4f20d592 CDL |
263 | enum autochan autochannels = AUTOCHANNELS_DISABLED; |
264 | char *dev_dir_name = NULL; | |
265 | char *buf_dir_name = NULL; | |
266 | bool current_trigger_set = false; | |
267 | ||
268 | void 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 | ||
300 | void sig_handler(int signum) | |
301 | { | |
302 | fprintf(stderr, "Caught signal %d\n", signum); | |
303 | cleanup(); | |
304 | exit(-signum); | |
305 | } | |
306 | ||
307 | void 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 | ||
de397db8 CDL |
322 | static const struct option longopts[] = { |
323 | { "device-name", 1, 0, 'n' }, | |
324 | { "device-num", 1, 0, 'N' }, | |
7c7e9dad CDL |
325 | { "trigger-name", 1, 0, 't' }, |
326 | { "trigger-num", 1, 0, 'T' }, | |
de397db8 CDL |
327 | { }, |
328 | }; | |
329 | ||
e58537cc JC |
330 | int main(int argc, char **argv) |
331 | { | |
96df9799 JC |
332 | unsigned long num_loops = 2; |
333 | unsigned long timedelay = 1000000; | |
334 | unsigned long buf_len = 128; | |
335 | ||
e58537cc | 336 | int ret, c, i, j, toread; |
4f20d592 | 337 | int fp = -1; |
e58537cc | 338 | |
4f20d592 | 339 | int num_channels = 0; |
e58537cc | 340 | char *trigger_name = NULL, *device_name = NULL; |
e58537cc | 341 | |
4f20d592 | 342 | char *data = NULL; |
c77b3810 | 343 | ssize_t read_size; |
deb4d1fd | 344 | int dev_num = -1, trig_num = -1; |
4f20d592 | 345 | char *buffer_access = NULL; |
e58537cc | 346 | int scan_size; |
30268a3d | 347 | int noevents = 0; |
b6d5be57 | 348 | int notrigger = 0; |
96df9799 | 349 | char *dummy; |
e58537cc | 350 | |
d8da0eee | 351 | struct iio_channel_info *channels; |
e58537cc | 352 | |
4f20d592 CDL |
353 | register_cleanup(); |
354 | ||
7c7e9dad | 355 | while ((c = getopt_long(argc, argv, "ac:egl:n:N:t:T:w:", longopts, NULL)) != -1) { |
e58537cc | 356 | switch (c) { |
5f991a92 LW |
357 | case 'a': |
358 | autochannels = AUTOCHANNELS_ENABLED; | |
359 | break; | |
96df9799 | 360 | case 'c': |
c8ce9903 | 361 | errno = 0; |
96df9799 | 362 | num_loops = strtoul(optarg, &dummy, 10); |
4f20d592 CDL |
363 | if (errno) { |
364 | ret = -errno; | |
365 | goto error; | |
366 | } | |
7663a4aa | 367 | |
96df9799 | 368 | break; |
e06e3d71 HK |
369 | case 'e': |
370 | noevents = 1; | |
371 | break; | |
372 | case 'g': | |
373 | notrigger = 1; | |
96df9799 JC |
374 | break; |
375 | case 'l': | |
c8ce9903 | 376 | errno = 0; |
96df9799 | 377 | buf_len = strtoul(optarg, &dummy, 10); |
4f20d592 CDL |
378 | if (errno) { |
379 | ret = -errno; | |
380 | goto error; | |
381 | } | |
7663a4aa | 382 | |
96df9799 | 383 | break; |
e06e3d71 | 384 | case 'n': |
de397db8 CDL |
385 | 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 | } | |
e06e3d71 HK |
394 | break; |
395 | case 't': | |
4f20d592 | 396 | trigger_name = strdup(optarg); |
e06e3d71 | 397 | break; |
7c7e9dad CDL |
398 | case 'T': |
399 | errno = 0; | |
400 | trig_num = strtoul(optarg, &dummy, 10); | |
401 | if (errno) | |
402 | return -errno; | |
403 | break; | |
e06e3d71 HK |
404 | case 'w': |
405 | errno = 0; | |
406 | timedelay = strtoul(optarg, &dummy, 10); | |
4f20d592 CDL |
407 | if (errno) { |
408 | ret = -errno; | |
409 | goto error; | |
410 | } | |
b6d5be57 | 411 | break; |
e58537cc | 412 | case '?': |
e06e3d71 | 413 | print_usage(); |
4f20d592 CDL |
414 | ret = -1; |
415 | goto error; | |
e58537cc JC |
416 | } |
417 | } | |
418 | ||
419 | /* Find the device requested */ | |
de397db8 CDL |
420 | if (dev_num < 0 && !device_name) { |
421 | fprintf(stderr, "Device not set\n"); | |
422 | print_usage(); | |
423 | ret = -1; | |
424 | goto error; | |
425 | } 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; | |
4f20d592 | 429 | goto error; |
de397db8 CDL |
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 | } | |
e58537cc JC |
437 | } |
438 | printf("iio device number being used is %d\n", dev_num); | |
439 | ||
e9e45b43 | 440 | ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num); |
de397db8 CDL |
441 | 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 | } | |
4f20d592 | 455 | } |
b6d5be57 | 456 | |
7c7e9dad CDL |
457 | if (notrigger) { |
458 | printf("trigger-less mode selected\n"); | |
deb4d1fd | 459 | } if (trig_num >= 0) { |
7c7e9dad CDL |
460 | 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 { | |
ff1ac639 | 474 | if (!trigger_name) { |
b6d5be57 KW |
475 | /* |
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; | |
4f20d592 | 484 | goto error; |
b6d5be57 | 485 | } |
e58537cc | 486 | } |
e58537cc | 487 | |
793d6b5e LW |
488 | /* 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; | |
4f20d592 | 497 | goto error; |
793d6b5e LW |
498 | } |
499 | } | |
500 | ||
b6d5be57 KW |
501 | trig_num = find_type_by_name(trigger_name, "trigger"); |
502 | if (trig_num < 0) { | |
d9abc615 CO |
503 | fprintf(stderr, "Failed to find the trigger %s\n", |
504 | trigger_name); | |
e83a47cf | 505 | ret = trig_num; |
4f20d592 | 506 | goto error; |
b6d5be57 | 507 | } |
7663a4aa | 508 | |
b6d5be57 | 509 | printf("iio trigger number being used is %d\n", trig_num); |
7663a4aa | 510 | } |
e58537cc JC |
511 | |
512 | /* | |
513 | * Parse the files in scan_elements to identify what channels are | |
514 | * present | |
515 | */ | |
d8da0eee | 516 | ret = build_channel_array(dev_dir_name, &channels, &num_channels); |
e58537cc | 517 | if (ret) { |
d9abc615 CO |
518 | fprintf(stderr, "Problem reading scan element information\n" |
519 | "diag %s\n", dev_dir_name); | |
4f20d592 | 520 | goto error; |
e58537cc | 521 | } |
5f991a92 LW |
522 | 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"); | |
4f20d592 | 535 | goto error; |
5f991a92 LW |
536 | } |
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); | |
4f20d592 | 547 | goto error; |
5f991a92 LW |
548 | } |
549 | if (!num_channels) { | |
550 | fprintf(stderr, "Still no channels after " | |
551 | "auto-enabling, giving up\n"); | |
4f20d592 | 552 | goto error; |
5f991a92 LW |
553 | } |
554 | } | |
555 | ||
556 | if (!num_channels && autochannels == AUTOCHANNELS_DISABLED) { | |
53dabafe LW |
557 | 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 | |
5f991a92 LW |
561 | "/*_en or pass -a to autoenable channels and " |
562 | "try again.\n", dev_dir_name); | |
53dabafe | 563 | ret = -ENOENT; |
4f20d592 | 564 | goto error; |
53dabafe | 565 | } |
e58537cc JC |
566 | |
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 | */ | |
1aa04278 JC |
572 | ret = asprintf(&buf_dir_name, |
573 | "%siio:device%d/buffer", iio_dir, dev_num); | |
e58537cc JC |
574 | if (ret < 0) { |
575 | ret = -ENOMEM; | |
4f20d592 | 576 | goto error; |
e58537cc | 577 | } |
b6d5be57 KW |
578 | |
579 | if (!notrigger) { | |
580 | printf("%s %s\n", dev_dir_name, trigger_name); | |
7663a4aa HK |
581 | /* |
582 | * Set the device trigger to be the data ready trigger found | |
583 | * above | |
584 | */ | |
b6d5be57 KW |
585 | ret = write_sysfs_string_and_verify("trigger/current_trigger", |
586 | dev_dir_name, | |
587 | trigger_name); | |
588 | if (ret < 0) { | |
d9abc615 CO |
589 | fprintf(stderr, |
590 | "Failed to write current_trigger file\n"); | |
4f20d592 | 591 | goto error; |
b6d5be57 | 592 | } |
e58537cc JC |
593 | } |
594 | ||
595 | /* Setup ring buffer parameters */ | |
596 | ret = write_sysfs_int("length", buf_dir_name, buf_len); | |
597 | if (ret < 0) | |
4f20d592 | 598 | goto error; |
e58537cc JC |
599 | |
600 | /* Enable the buffer */ | |
601 | ret = write_sysfs_int("enable", buf_dir_name, 1); | |
e7231491 IT |
602 | if (ret < 0) { |
603 | fprintf(stderr, | |
604 | "Failed to enable buffer: %s\n", strerror(-ret)); | |
4f20d592 | 605 | goto error; |
e7231491 | 606 | } |
7663a4aa | 607 | |
d8da0eee | 608 | scan_size = size_from_channelarray(channels, num_channels); |
7663a4aa | 609 | data = malloc(scan_size * buf_len); |
e58537cc JC |
610 | if (!data) { |
611 | ret = -ENOMEM; | |
4f20d592 | 612 | goto error; |
e58537cc JC |
613 | } |
614 | ||
1aa04278 | 615 | ret = asprintf(&buffer_access, "/dev/iio:device%d", dev_num); |
e58537cc JC |
616 | if (ret < 0) { |
617 | ret = -ENOMEM; | |
4f20d592 | 618 | goto error; |
e58537cc JC |
619 | } |
620 | ||
e58537cc JC |
621 | /* Attempt to open non blocking the access dev */ |
622 | fp = open(buffer_access, O_RDONLY | O_NONBLOCK); | |
7663a4aa | 623 | if (fp == -1) { /* TODO: If it isn't there make the node */ |
e58537cc | 624 | ret = -errno; |
d9abc615 | 625 | fprintf(stderr, "Failed to open %s\n", buffer_access); |
4f20d592 | 626 | goto error; |
e58537cc JC |
627 | } |
628 | ||
e58537cc | 629 | for (j = 0; j < num_loops; j++) { |
30268a3d | 630 | if (!noevents) { |
52615d47 JC |
631 | struct pollfd pfd = { |
632 | .fd = fp, | |
633 | .events = POLLIN, | |
634 | }; | |
635 | ||
6bb7cac8 HK |
636 | ret = poll(&pfd, 1, -1); |
637 | if (ret < 0) { | |
638 | ret = -errno; | |
4f20d592 | 639 | goto error; |
6bb7cac8 HK |
640 | } else if (ret == 0) { |
641 | continue; | |
642 | } | |
643 | ||
52615d47 | 644 | toread = buf_len; |
30268a3d | 645 | } else { |
96df9799 | 646 | usleep(timedelay); |
30268a3d | 647 | toread = 64; |
e58537cc | 648 | } |
30268a3d | 649 | |
7663a4aa | 650 | read_size = read(fp, data, toread * scan_size); |
97b603a4 | 651 | if (read_size < 0) { |
8749948a | 652 | if (errno == EAGAIN) { |
d9abc615 | 653 | fprintf(stderr, "nothing available\n"); |
97b603a4 | 654 | continue; |
7663a4aa | 655 | } else { |
97b603a4 | 656 | break; |
7663a4aa | 657 | } |
e58537cc | 658 | } |
7663a4aa HK |
659 | for (i = 0; i < read_size / scan_size; i++) |
660 | process_scan(data + scan_size * i, channels, | |
e58537cc JC |
661 | num_channels); |
662 | } | |
663 | ||
4f20d592 CDL |
664 | error: |
665 | cleanup(); | |
e58537cc | 666 | |
4f20d592 | 667 | if (fp >= 0 && close(fp) == -1) |
6bb7cac8 | 668 | perror("Failed to close buffer"); |
e58537cc | 669 | free(buffer_access); |
a71bfb4a | 670 | free(data); |
e58537cc | 671 | free(buf_dir_name); |
63f05c85 HK |
672 | for (i = num_channels - 1; i >= 0; i--) { |
673 | free(channels[i].name); | |
674 | free(channels[i].generic_name); | |
675 | } | |
676 | free(channels); | |
4f20d592 | 677 | free(trigger_name); |
de397db8 | 678 | free(device_name); |
d3ccfc41 | 679 | free(dev_dir_name); |
0e799878 | 680 | |
e58537cc JC |
681 | return ret; |
682 | } |