]>
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" |
4e2cf0e2 | 250 | " -A Force-activate ALL channels\n" |
55dda0ab | 251 | " -c <n> Do n conversions, or loop forever if n < 0\n" |
d9abc615 CO |
252 | " -e Disable wait for event (new data)\n" |
253 | " -g Use trigger-less mode\n" | |
254 | " -l <n> Set buffer length to n samples\n" | |
de397db8 CDL |
255 | " --device-name -n <name>\n" |
256 | " --device-num -N <num>\n" | |
257 | " Set device by name or number (mandatory)\n" | |
7c7e9dad CDL |
258 | " --trigger-name -t <name>\n" |
259 | " --trigger-num -T <num>\n" | |
260 | " Set trigger by name or number\n" | |
d9abc615 | 261 | " -w <n> Set delay between reads in us (event-less mode)\n"); |
e06e3d71 HK |
262 | } |
263 | ||
4f20d592 CDL |
264 | enum autochan autochannels = AUTOCHANNELS_DISABLED; |
265 | char *dev_dir_name = NULL; | |
266 | char *buf_dir_name = NULL; | |
267 | bool current_trigger_set = false; | |
268 | ||
269 | void cleanup(void) | |
270 | { | |
271 | int ret; | |
272 | ||
273 | /* Disable trigger */ | |
274 | if (dev_dir_name && current_trigger_set) { | |
275 | /* Disconnect the trigger - just write a dummy name. */ | |
276 | ret = write_sysfs_string("trigger/current_trigger", | |
277 | dev_dir_name, "NULL"); | |
278 | if (ret < 0) | |
279 | fprintf(stderr, "Failed to disable trigger: %s\n", | |
280 | strerror(-ret)); | |
281 | current_trigger_set = false; | |
282 | } | |
283 | ||
284 | /* Disable buffer */ | |
285 | if (buf_dir_name) { | |
286 | ret = write_sysfs_int("enable", buf_dir_name, 0); | |
287 | if (ret < 0) | |
288 | fprintf(stderr, "Failed to disable buffer: %s\n", | |
289 | strerror(-ret)); | |
290 | } | |
291 | ||
292 | /* Disable channels if auto-enabled */ | |
293 | if (dev_dir_name && autochannels == AUTOCHANNELS_ACTIVE) { | |
294 | ret = enable_disable_all_channels(dev_dir_name, 0); | |
295 | if (ret) | |
296 | fprintf(stderr, "Failed to disable all channels\n"); | |
297 | autochannels = AUTOCHANNELS_DISABLED; | |
298 | } | |
299 | } | |
300 | ||
301 | void sig_handler(int signum) | |
302 | { | |
303 | fprintf(stderr, "Caught signal %d\n", signum); | |
304 | cleanup(); | |
305 | exit(-signum); | |
306 | } | |
307 | ||
308 | void register_cleanup(void) | |
309 | { | |
310 | struct sigaction sa = { .sa_handler = sig_handler }; | |
311 | const int signums[] = { SIGINT, SIGTERM, SIGABRT }; | |
312 | int ret, i; | |
313 | ||
314 | for (i = 0; i < ARRAY_SIZE(signums); ++i) { | |
315 | ret = sigaction(signums[i], &sa, NULL); | |
316 | if (ret) { | |
317 | perror("Failed to register signal handler"); | |
318 | exit(-1); | |
319 | } | |
320 | } | |
321 | } | |
322 | ||
de397db8 CDL |
323 | static const struct option longopts[] = { |
324 | { "device-name", 1, 0, 'n' }, | |
325 | { "device-num", 1, 0, 'N' }, | |
7c7e9dad CDL |
326 | { "trigger-name", 1, 0, 't' }, |
327 | { "trigger-num", 1, 0, 'T' }, | |
de397db8 CDL |
328 | { }, |
329 | }; | |
330 | ||
e58537cc JC |
331 | int main(int argc, char **argv) |
332 | { | |
b119d3bc | 333 | long long num_loops = 2; |
96df9799 JC |
334 | unsigned long timedelay = 1000000; |
335 | unsigned long buf_len = 128; | |
336 | ||
71b52d2c | 337 | ssize_t i; |
55dda0ab | 338 | unsigned long long j; |
71b52d2c MK |
339 | unsigned long toread; |
340 | int ret, c; | |
4f20d592 | 341 | int fp = -1; |
e58537cc | 342 | |
4f20d592 | 343 | int num_channels = 0; |
e58537cc | 344 | char *trigger_name = NULL, *device_name = NULL; |
e58537cc | 345 | |
4f20d592 | 346 | char *data = NULL; |
c77b3810 | 347 | ssize_t read_size; |
deb4d1fd | 348 | int dev_num = -1, trig_num = -1; |
4f20d592 | 349 | char *buffer_access = NULL; |
e58537cc | 350 | int scan_size; |
30268a3d | 351 | int noevents = 0; |
b6d5be57 | 352 | int notrigger = 0; |
96df9799 | 353 | char *dummy; |
71ccbe5d | 354 | bool force_autochannels = false; |
e58537cc | 355 | |
ddbc719f | 356 | struct iio_channel_info *channels = NULL; |
e58537cc | 357 | |
4f20d592 CDL |
358 | register_cleanup(); |
359 | ||
4e2cf0e2 ERR |
360 | while ((c = getopt_long(argc, argv, "aAc:egl:n:N:t:T:w:?", longopts, |
361 | NULL)) != -1) { | |
e58537cc | 362 | switch (c) { |
5f991a92 LW |
363 | case 'a': |
364 | autochannels = AUTOCHANNELS_ENABLED; | |
365 | break; | |
4e2cf0e2 ERR |
366 | case 'A': |
367 | autochannels = AUTOCHANNELS_ENABLED; | |
71ccbe5d | 368 | force_autochannels = true; |
4e2cf0e2 | 369 | break; |
96df9799 | 370 | case 'c': |
c8ce9903 | 371 | errno = 0; |
55dda0ab | 372 | num_loops = strtoll(optarg, &dummy, 10); |
4f20d592 CDL |
373 | if (errno) { |
374 | ret = -errno; | |
375 | goto error; | |
376 | } | |
7663a4aa | 377 | |
96df9799 | 378 | break; |
e06e3d71 HK |
379 | case 'e': |
380 | noevents = 1; | |
381 | break; | |
382 | case 'g': | |
383 | notrigger = 1; | |
96df9799 JC |
384 | break; |
385 | case 'l': | |
c8ce9903 | 386 | errno = 0; |
96df9799 | 387 | buf_len = strtoul(optarg, &dummy, 10); |
4f20d592 CDL |
388 | if (errno) { |
389 | ret = -errno; | |
390 | goto error; | |
391 | } | |
7663a4aa | 392 | |
96df9799 | 393 | break; |
e06e3d71 | 394 | case 'n': |
de397db8 CDL |
395 | device_name = strdup(optarg); |
396 | break; | |
397 | case 'N': | |
398 | errno = 0; | |
399 | dev_num = strtoul(optarg, &dummy, 10); | |
400 | if (errno) { | |
401 | ret = -errno; | |
402 | goto error; | |
403 | } | |
e06e3d71 HK |
404 | break; |
405 | case 't': | |
4f20d592 | 406 | trigger_name = strdup(optarg); |
e06e3d71 | 407 | break; |
7c7e9dad CDL |
408 | case 'T': |
409 | errno = 0; | |
410 | trig_num = strtoul(optarg, &dummy, 10); | |
411 | if (errno) | |
412 | return -errno; | |
413 | break; | |
e06e3d71 HK |
414 | case 'w': |
415 | errno = 0; | |
416 | timedelay = strtoul(optarg, &dummy, 10); | |
4f20d592 CDL |
417 | if (errno) { |
418 | ret = -errno; | |
419 | goto error; | |
420 | } | |
b6d5be57 | 421 | break; |
e58537cc | 422 | case '?': |
e06e3d71 | 423 | print_usage(); |
4f20d592 CDL |
424 | ret = -1; |
425 | goto error; | |
e58537cc JC |
426 | } |
427 | } | |
428 | ||
429 | /* Find the device requested */ | |
de397db8 CDL |
430 | if (dev_num < 0 && !device_name) { |
431 | fprintf(stderr, "Device not set\n"); | |
432 | print_usage(); | |
433 | ret = -1; | |
434 | goto error; | |
435 | } else if (dev_num >= 0 && device_name) { | |
436 | fprintf(stderr, "Only one of --device-num or --device-name needs to be set\n"); | |
437 | print_usage(); | |
438 | ret = -1; | |
4f20d592 | 439 | goto error; |
de397db8 CDL |
440 | } else if (dev_num < 0) { |
441 | dev_num = find_type_by_name(device_name, "iio:device"); | |
442 | if (dev_num < 0) { | |
443 | fprintf(stderr, "Failed to find the %s\n", device_name); | |
444 | ret = dev_num; | |
445 | goto error; | |
446 | } | |
e58537cc JC |
447 | } |
448 | printf("iio device number being used is %d\n", dev_num); | |
449 | ||
e9e45b43 | 450 | ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num); |
de397db8 CDL |
451 | if (ret < 0) |
452 | return -ENOMEM; | |
453 | /* Fetch device_name if specified by number */ | |
454 | if (!device_name) { | |
455 | device_name = malloc(IIO_MAX_NAME_LENGTH); | |
456 | if (!device_name) { | |
457 | ret = -ENOMEM; | |
458 | goto error; | |
459 | } | |
460 | ret = read_sysfs_string("name", dev_dir_name, device_name); | |
461 | if (ret < 0) { | |
462 | fprintf(stderr, "Failed to read name of device %d\n", dev_num); | |
463 | goto error; | |
464 | } | |
4f20d592 | 465 | } |
b6d5be57 | 466 | |
7c7e9dad CDL |
467 | if (notrigger) { |
468 | printf("trigger-less mode selected\n"); | |
f8e81d7e | 469 | } else if (trig_num >= 0) { |
7c7e9dad CDL |
470 | char *trig_dev_name; |
471 | ret = asprintf(&trig_dev_name, "%strigger%d", iio_dir, trig_num); | |
472 | if (ret < 0) { | |
473 | return -ENOMEM; | |
474 | } | |
475 | trigger_name = malloc(IIO_MAX_NAME_LENGTH); | |
476 | ret = read_sysfs_string("name", trig_dev_name, trigger_name); | |
477 | free(trig_dev_name); | |
478 | if (ret < 0) { | |
479 | fprintf(stderr, "Failed to read trigger%d name from\n", trig_num); | |
480 | return ret; | |
481 | } | |
482 | printf("iio trigger number being used is %d\n", trig_num); | |
483 | } else { | |
ff1ac639 | 484 | if (!trigger_name) { |
b6d5be57 KW |
485 | /* |
486 | * Build the trigger name. If it is device associated | |
487 | * its name is <device_name>_dev[n] where n matches | |
488 | * the device number found above. | |
489 | */ | |
490 | ret = asprintf(&trigger_name, | |
491 | "%s-dev%d", device_name, dev_num); | |
492 | if (ret < 0) { | |
493 | ret = -ENOMEM; | |
4f20d592 | 494 | goto error; |
b6d5be57 | 495 | } |
e58537cc | 496 | } |
e58537cc | 497 | |
793d6b5e LW |
498 | /* Look for this "-devN" trigger */ |
499 | trig_num = find_type_by_name(trigger_name, "trigger"); | |
500 | if (trig_num < 0) { | |
501 | /* OK try the simpler "-trigger" suffix instead */ | |
502 | free(trigger_name); | |
503 | ret = asprintf(&trigger_name, | |
504 | "%s-trigger", device_name); | |
505 | if (ret < 0) { | |
506 | ret = -ENOMEM; | |
4f20d592 | 507 | goto error; |
793d6b5e LW |
508 | } |
509 | } | |
510 | ||
b6d5be57 KW |
511 | trig_num = find_type_by_name(trigger_name, "trigger"); |
512 | if (trig_num < 0) { | |
d9abc615 CO |
513 | fprintf(stderr, "Failed to find the trigger %s\n", |
514 | trigger_name); | |
e83a47cf | 515 | ret = trig_num; |
4f20d592 | 516 | goto error; |
b6d5be57 | 517 | } |
7663a4aa | 518 | |
b6d5be57 | 519 | printf("iio trigger number being used is %d\n", trig_num); |
7663a4aa | 520 | } |
e58537cc JC |
521 | |
522 | /* | |
523 | * Parse the files in scan_elements to identify what channels are | |
524 | * present | |
525 | */ | |
d8da0eee | 526 | ret = build_channel_array(dev_dir_name, &channels, &num_channels); |
e58537cc | 527 | if (ret) { |
d9abc615 CO |
528 | fprintf(stderr, "Problem reading scan element information\n" |
529 | "diag %s\n", dev_dir_name); | |
4f20d592 | 530 | goto error; |
e58537cc | 531 | } |
7c5fe411 | 532 | if (num_channels && autochannels == AUTOCHANNELS_ENABLED && |
71ccbe5d | 533 | !force_autochannels) { |
5f991a92 LW |
534 | fprintf(stderr, "Auto-channels selected but some channels " |
535 | "are already activated in sysfs\n"); | |
536 | fprintf(stderr, "Proceeding without activating any channels\n"); | |
537 | } | |
538 | ||
4e2cf0e2 | 539 | if ((!num_channels && autochannels == AUTOCHANNELS_ENABLED) || |
7c5fe411 | 540 | (autochannels == AUTOCHANNELS_ENABLED && force_autochannels)) { |
4e2cf0e2 | 541 | fprintf(stderr, "Enabling all channels\n"); |
5f991a92 LW |
542 | |
543 | ret = enable_disable_all_channels(dev_dir_name, 1); | |
544 | if (ret) { | |
545 | fprintf(stderr, "Failed to enable all channels\n"); | |
4f20d592 | 546 | goto error; |
5f991a92 LW |
547 | } |
548 | ||
549 | /* This flags that we need to disable the channels again */ | |
550 | autochannels = AUTOCHANNELS_ACTIVE; | |
551 | ||
552 | ret = build_channel_array(dev_dir_name, &channels, | |
553 | &num_channels); | |
554 | if (ret) { | |
555 | fprintf(stderr, "Problem reading scan element " | |
556 | "information\n" | |
557 | "diag %s\n", dev_dir_name); | |
4f20d592 | 558 | goto error; |
5f991a92 LW |
559 | } |
560 | if (!num_channels) { | |
561 | fprintf(stderr, "Still no channels after " | |
562 | "auto-enabling, giving up\n"); | |
4f20d592 | 563 | goto error; |
5f991a92 LW |
564 | } |
565 | } | |
566 | ||
567 | if (!num_channels && autochannels == AUTOCHANNELS_DISABLED) { | |
53dabafe LW |
568 | fprintf(stderr, |
569 | "No channels are enabled, we have nothing to scan.\n"); | |
570 | fprintf(stderr, "Enable channels manually in " | |
571 | FORMAT_SCAN_ELEMENTS_DIR | |
5f991a92 LW |
572 | "/*_en or pass -a to autoenable channels and " |
573 | "try again.\n", dev_dir_name); | |
53dabafe | 574 | ret = -ENOENT; |
4f20d592 | 575 | goto error; |
53dabafe | 576 | } |
e58537cc JC |
577 | |
578 | /* | |
579 | * Construct the directory name for the associated buffer. | |
580 | * As we know that the lis3l02dq has only one buffer this may | |
581 | * be built rather than found. | |
582 | */ | |
1aa04278 JC |
583 | ret = asprintf(&buf_dir_name, |
584 | "%siio:device%d/buffer", iio_dir, dev_num); | |
e58537cc JC |
585 | if (ret < 0) { |
586 | ret = -ENOMEM; | |
4f20d592 | 587 | goto error; |
e58537cc | 588 | } |
b6d5be57 KW |
589 | |
590 | if (!notrigger) { | |
591 | printf("%s %s\n", dev_dir_name, trigger_name); | |
7663a4aa HK |
592 | /* |
593 | * Set the device trigger to be the data ready trigger found | |
594 | * above | |
595 | */ | |
b6d5be57 KW |
596 | ret = write_sysfs_string_and_verify("trigger/current_trigger", |
597 | dev_dir_name, | |
598 | trigger_name); | |
599 | if (ret < 0) { | |
d9abc615 CO |
600 | fprintf(stderr, |
601 | "Failed to write current_trigger file\n"); | |
4f20d592 | 602 | goto error; |
b6d5be57 | 603 | } |
e58537cc JC |
604 | } |
605 | ||
606 | /* Setup ring buffer parameters */ | |
607 | ret = write_sysfs_int("length", buf_dir_name, buf_len); | |
608 | if (ret < 0) | |
4f20d592 | 609 | goto error; |
e58537cc JC |
610 | |
611 | /* Enable the buffer */ | |
612 | ret = write_sysfs_int("enable", buf_dir_name, 1); | |
e7231491 IT |
613 | if (ret < 0) { |
614 | fprintf(stderr, | |
615 | "Failed to enable buffer: %s\n", strerror(-ret)); | |
4f20d592 | 616 | goto error; |
e7231491 | 617 | } |
7663a4aa | 618 | |
d8da0eee | 619 | scan_size = size_from_channelarray(channels, num_channels); |
7663a4aa | 620 | data = malloc(scan_size * buf_len); |
e58537cc JC |
621 | if (!data) { |
622 | ret = -ENOMEM; | |
4f20d592 | 623 | goto error; |
e58537cc JC |
624 | } |
625 | ||
1aa04278 | 626 | ret = asprintf(&buffer_access, "/dev/iio:device%d", dev_num); |
e58537cc JC |
627 | if (ret < 0) { |
628 | ret = -ENOMEM; | |
4f20d592 | 629 | goto error; |
e58537cc JC |
630 | } |
631 | ||
e58537cc JC |
632 | /* Attempt to open non blocking the access dev */ |
633 | fp = open(buffer_access, O_RDONLY | O_NONBLOCK); | |
7663a4aa | 634 | if (fp == -1) { /* TODO: If it isn't there make the node */ |
e58537cc | 635 | ret = -errno; |
d9abc615 | 636 | fprintf(stderr, "Failed to open %s\n", buffer_access); |
4f20d592 | 637 | goto error; |
e58537cc JC |
638 | } |
639 | ||
55dda0ab | 640 | for (j = 0; j < num_loops || num_loops < 0; j++) { |
30268a3d | 641 | if (!noevents) { |
52615d47 JC |
642 | struct pollfd pfd = { |
643 | .fd = fp, | |
644 | .events = POLLIN, | |
645 | }; | |
646 | ||
6bb7cac8 HK |
647 | ret = poll(&pfd, 1, -1); |
648 | if (ret < 0) { | |
649 | ret = -errno; | |
4f20d592 | 650 | goto error; |
6bb7cac8 HK |
651 | } else if (ret == 0) { |
652 | continue; | |
653 | } | |
654 | ||
52615d47 | 655 | toread = buf_len; |
30268a3d | 656 | } else { |
96df9799 | 657 | usleep(timedelay); |
30268a3d | 658 | toread = 64; |
e58537cc | 659 | } |
30268a3d | 660 | |
7663a4aa | 661 | read_size = read(fp, data, toread * scan_size); |
97b603a4 | 662 | if (read_size < 0) { |
8749948a | 663 | if (errno == EAGAIN) { |
d9abc615 | 664 | fprintf(stderr, "nothing available\n"); |
97b603a4 | 665 | continue; |
7663a4aa | 666 | } else { |
97b603a4 | 667 | break; |
7663a4aa | 668 | } |
e58537cc | 669 | } |
7663a4aa HK |
670 | for (i = 0; i < read_size / scan_size; i++) |
671 | process_scan(data + scan_size * i, channels, | |
e58537cc JC |
672 | num_channels); |
673 | } | |
674 | ||
4f20d592 CDL |
675 | error: |
676 | cleanup(); | |
e58537cc | 677 | |
4f20d592 | 678 | if (fp >= 0 && close(fp) == -1) |
6bb7cac8 | 679 | perror("Failed to close buffer"); |
e58537cc | 680 | free(buffer_access); |
a71bfb4a | 681 | free(data); |
e58537cc | 682 | free(buf_dir_name); |
63f05c85 HK |
683 | for (i = num_channels - 1; i >= 0; i--) { |
684 | free(channels[i].name); | |
685 | free(channels[i].generic_name); | |
686 | } | |
687 | free(channels); | |
4f20d592 | 688 | free(trigger_name); |
de397db8 | 689 | free(device_name); |
d3ccfc41 | 690 | free(dev_dir_name); |
0e799878 | 691 | |
e58537cc JC |
692 | return ret; |
693 | } |