ipv6/ila: fix nlsize calculation for lwtunnel
[deliverable/linux.git] / tools / iio / generic_buffer.c
CommitLineData
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>
e58537cc
JC
35#include "iio_utils.h"
36
e58537cc
JC
37/**
38 * size_from_channelarray() - calculate the storage size of a scan
d8da0eee
PM
39 * @channels: the channel info array
40 * @num_channels: number of channels
e58537cc
JC
41 *
42 * Has the side effect of filling the channels[i].location values used
43 * in processing the buffer output.
44 **/
45int size_from_channelarray(struct iio_channel_info *channels, int num_channels)
46{
47 int bytes = 0;
48 int i = 0;
ff39a252 49
e58537cc
JC
50 while (i < num_channels) {
51 if (bytes % channels[i].bytes == 0)
52 channels[i].location = bytes;
53 else
7663a4aa
HK
54 channels[i].location = bytes - bytes % channels[i].bytes
55 + channels[i].bytes;
56
e58537cc
JC
57 bytes = channels[i].location + channels[i].bytes;
58 i++;
59 }
7663a4aa 60
e58537cc
JC
61 return bytes;
62}
63
e8d0927a
TB
64void print1byte(uint8_t input, struct iio_channel_info *info)
65{
66 /*
67 * Shift before conversion to avoid sign extension
68 * of left aligned data
69 */
70 input >>= info->shift;
71 input &= info->mask;
72 if (info->is_signed) {
73 int8_t val = (int8_t)(input << (8 - info->bits_used)) >>
74 (8 - info->bits_used);
75 printf("%05f ", ((float)val + info->offset) * info->scale);
76 } else {
77 printf("%05f ", ((float)input + info->offset) * info->scale);
78 }
79}
80
8e926134 81void print2byte(uint16_t input, struct iio_channel_info *info)
52615d47 82{
117cf8b7 83 /* First swap if incorrect endian */
117cf8b7 84 if (info->be)
8e926134 85 input = be16toh(input);
117cf8b7 86 else
8e926134 87 input = le16toh(input);
117cf8b7 88
d8da0eee
PM
89 /*
90 * Shift before conversion to avoid sign extension
91 * of left aligned data
92 */
1525ecfc 93 input >>= info->shift;
8e926134 94 input &= info->mask;
52615d47 95 if (info->is_signed) {
8e926134
HK
96 int16_t val = (int16_t)(input << (16 - info->bits_used)) >>
97 (16 - info->bits_used);
98 printf("%05f ", ((float)val + info->offset) * info->scale);
99 } else {
100 printf("%05f ", ((float)input + info->offset) * info->scale);
101 }
102}
ff39a252 103
8e926134
HK
104void print4byte(uint32_t input, struct iio_channel_info *info)
105{
106 /* First swap if incorrect endian */
107 if (info->be)
108 input = be32toh(input);
109 else
110 input = le32toh(input);
111
112 /*
113 * Shift before conversion to avoid sign extension
114 * of left aligned data
115 */
116 input >>= info->shift;
117 input &= info->mask;
118 if (info->is_signed) {
119 int32_t val = (int32_t)(input << (32 - info->bits_used)) >>
120 (32 - info->bits_used);
121 printf("%05f ", ((float)val + info->offset) * info->scale);
52615d47 122 } else {
8e926134
HK
123 printf("%05f ", ((float)input + info->offset) * info->scale);
124 }
125}
ff39a252 126
8e926134
HK
127void print8byte(uint64_t input, struct iio_channel_info *info)
128{
129 /* First swap if incorrect endian */
130 if (info->be)
131 input = be64toh(input);
132 else
133 input = le64toh(input);
134
135 /*
136 * Shift before conversion to avoid sign extension
137 * of left aligned data
138 */
139 input >>= info->shift;
140 input &= info->mask;
141 if (info->is_signed) {
142 int64_t val = (int64_t)(input << (64 - info->bits_used)) >>
143 (64 - info->bits_used);
144 /* special case for timestamp */
145 if (info->scale == 1.0f && info->offset == 0.0f)
146 printf("%" PRId64 " ", val);
147 else
148 printf("%05f ",
149 ((float)val + info->offset) * info->scale);
150 } else {
151 printf("%05f ", ((float)input + info->offset) * info->scale);
52615d47
JC
152 }
153}
8e926134 154
e58537cc
JC
155/**
156 * process_scan() - print out the values in SI units
157 * @data: pointer to the start of the scan
7663a4aa
HK
158 * @channels: information about the channels.
159 * Note: size_from_channelarray must have been called first
160 * to fill the location offsets.
d8da0eee 161 * @num_channels: number of channels
e58537cc
JC
162 **/
163void process_scan(char *data,
d8da0eee 164 struct iio_channel_info *channels,
e58537cc
JC
165 int num_channels)
166{
167 int k;
ff39a252 168
e58537cc 169 for (k = 0; k < num_channels; k++)
d8da0eee 170 switch (channels[k].bytes) {
e58537cc 171 /* only a few cases implemented so far */
e8d0927a
TB
172 case 1:
173 print1byte(*(uint8_t *)(data + channels[k].location),
174 &channels[k]);
175 break;
e58537cc 176 case 2:
d8da0eee
PM
177 print2byte(*(uint16_t *)(data + channels[k].location),
178 &channels[k]);
e58537cc 179 break;
6cffc1f8 180 case 4:
8e926134
HK
181 print4byte(*(uint32_t *)(data + channels[k].location),
182 &channels[k]);
6cffc1f8 183 break;
e58537cc 184 case 8:
8e926134
HK
185 print8byte(*(uint64_t *)(data + channels[k].location),
186 &channels[k]);
e58537cc
JC
187 break;
188 default:
189 break;
190 }
191 printf("\n");
192}
193
e06e3d71
HK
194void print_usage(void)
195{
d9abc615
CO
196 fprintf(stderr, "Usage: generic_buffer [options]...\n"
197 "Capture, convert and output data from IIO device buffer\n"
198 " -c <n> Do n conversions\n"
199 " -e Disable wait for event (new data)\n"
200 " -g Use trigger-less mode\n"
201 " -l <n> Set buffer length to n samples\n"
202 " -n <name> Set device name (mandatory)\n"
203 " -t <name> Set trigger name\n"
204 " -w <n> Set delay between reads in us (event-less mode)\n");
e06e3d71
HK
205}
206
e58537cc
JC
207int main(int argc, char **argv)
208{
96df9799
JC
209 unsigned long num_loops = 2;
210 unsigned long timedelay = 1000000;
211 unsigned long buf_len = 128;
212
e58537cc 213 int ret, c, i, j, toread;
e58537cc
JC
214 int fp;
215
216 int num_channels;
217 char *trigger_name = NULL, *device_name = NULL;
218 char *dev_dir_name, *buf_dir_name;
219
220 int datardytrigger = 1;
221 char *data;
c77b3810 222 ssize_t read_size;
e58537cc 223 int dev_num, trig_num;
52615d47 224 char *buffer_access;
e58537cc 225 int scan_size;
30268a3d 226 int noevents = 0;
b6d5be57 227 int notrigger = 0;
96df9799 228 char *dummy;
e58537cc 229
d8da0eee 230 struct iio_channel_info *channels;
e58537cc 231
e06e3d71 232 while ((c = getopt(argc, argv, "c:egl:n:t:w:")) != -1) {
e58537cc 233 switch (c) {
96df9799 234 case 'c':
c8ce9903 235 errno = 0;
96df9799 236 num_loops = strtoul(optarg, &dummy, 10);
c8ce9903
HK
237 if (errno)
238 return -errno;
7663a4aa 239
96df9799 240 break;
e06e3d71
HK
241 case 'e':
242 noevents = 1;
243 break;
244 case 'g':
245 notrigger = 1;
96df9799
JC
246 break;
247 case 'l':
c8ce9903 248 errno = 0;
96df9799 249 buf_len = strtoul(optarg, &dummy, 10);
c8ce9903
HK
250 if (errno)
251 return -errno;
7663a4aa 252
96df9799 253 break;
e06e3d71
HK
254 case 'n':
255 device_name = optarg;
256 break;
257 case 't':
258 trigger_name = optarg;
259 datardytrigger = 0;
260 break;
261 case 'w':
262 errno = 0;
263 timedelay = strtoul(optarg, &dummy, 10);
264 if (errno)
265 return -errno;
b6d5be57 266 break;
e58537cc 267 case '?':
e06e3d71 268 print_usage();
e58537cc
JC
269 return -1;
270 }
271 }
272
ff1ac639 273 if (!device_name) {
d9abc615 274 fprintf(stderr, "Device name not set\n");
e06e3d71 275 print_usage();
065896e9 276 return -1;
e06e3d71 277 }
065896e9 278
e58537cc 279 /* Find the device requested */
1aa04278 280 dev_num = find_type_by_name(device_name, "iio:device");
e58537cc 281 if (dev_num < 0) {
d9abc615 282 fprintf(stderr, "Failed to find the %s\n", device_name);
0e799878 283 return dev_num;
e58537cc 284 }
7663a4aa 285
e58537cc
JC
286 printf("iio device number being used is %d\n", dev_num);
287
e9e45b43
HK
288 ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num);
289 if (ret < 0)
290 return -ENOMEM;
b6d5be57
KW
291
292 if (!notrigger) {
ff1ac639 293 if (!trigger_name) {
b6d5be57
KW
294 /*
295 * Build the trigger name. If it is device associated
296 * its name is <device_name>_dev[n] where n matches
297 * the device number found above.
298 */
299 ret = asprintf(&trigger_name,
300 "%s-dev%d", device_name, dev_num);
301 if (ret < 0) {
302 ret = -ENOMEM;
d3ccfc41 303 goto error_free_dev_dir_name;
b6d5be57 304 }
e58537cc 305 }
e58537cc 306
b6d5be57
KW
307 /* Verify the trigger exists */
308 trig_num = find_type_by_name(trigger_name, "trigger");
309 if (trig_num < 0) {
d9abc615
CO
310 fprintf(stderr, "Failed to find the trigger %s\n",
311 trigger_name);
e83a47cf 312 ret = trig_num;
b6d5be57
KW
313 goto error_free_triggername;
314 }
7663a4aa 315
b6d5be57 316 printf("iio trigger number being used is %d\n", trig_num);
7663a4aa 317 } else {
b6d5be57 318 printf("trigger-less mode selected\n");
7663a4aa 319 }
e58537cc
JC
320
321 /*
322 * Parse the files in scan_elements to identify what channels are
323 * present
324 */
d8da0eee 325 ret = build_channel_array(dev_dir_name, &channels, &num_channels);
e58537cc 326 if (ret) {
d9abc615
CO
327 fprintf(stderr, "Problem reading scan element information\n"
328 "diag %s\n", dev_dir_name);
e58537cc
JC
329 goto error_free_triggername;
330 }
53dabafe
LW
331 if (!num_channels) {
332 fprintf(stderr,
333 "No channels are enabled, we have nothing to scan.\n");
334 fprintf(stderr, "Enable channels manually in "
335 FORMAT_SCAN_ELEMENTS_DIR
336 "/*_en and try again.\n", dev_dir_name);
337 ret = -ENOENT;
338 goto error_free_triggername;
339 }
e58537cc
JC
340
341 /*
342 * Construct the directory name for the associated buffer.
343 * As we know that the lis3l02dq has only one buffer this may
344 * be built rather than found.
345 */
1aa04278
JC
346 ret = asprintf(&buf_dir_name,
347 "%siio:device%d/buffer", iio_dir, dev_num);
e58537cc
JC
348 if (ret < 0) {
349 ret = -ENOMEM;
63f05c85 350 goto error_free_channels;
e58537cc 351 }
b6d5be57
KW
352
353 if (!notrigger) {
354 printf("%s %s\n", dev_dir_name, trigger_name);
7663a4aa
HK
355 /*
356 * Set the device trigger to be the data ready trigger found
357 * above
358 */
b6d5be57
KW
359 ret = write_sysfs_string_and_verify("trigger/current_trigger",
360 dev_dir_name,
361 trigger_name);
362 if (ret < 0) {
d9abc615
CO
363 fprintf(stderr,
364 "Failed to write current_trigger file\n");
b6d5be57
KW
365 goto error_free_buf_dir_name;
366 }
e58537cc
JC
367 }
368
369 /* Setup ring buffer parameters */
370 ret = write_sysfs_int("length", buf_dir_name, buf_len);
371 if (ret < 0)
372 goto error_free_buf_dir_name;
373
374 /* Enable the buffer */
375 ret = write_sysfs_int("enable", buf_dir_name, 1);
e7231491
IT
376 if (ret < 0) {
377 fprintf(stderr,
378 "Failed to enable buffer: %s\n", strerror(-ret));
e58537cc 379 goto error_free_buf_dir_name;
e7231491 380 }
7663a4aa 381
d8da0eee 382 scan_size = size_from_channelarray(channels, num_channels);
7663a4aa 383 data = malloc(scan_size * buf_len);
e58537cc
JC
384 if (!data) {
385 ret = -ENOMEM;
386 goto error_free_buf_dir_name;
387 }
388
1aa04278 389 ret = asprintf(&buffer_access, "/dev/iio:device%d", dev_num);
e58537cc
JC
390 if (ret < 0) {
391 ret = -ENOMEM;
392 goto error_free_data;
393 }
394
e58537cc
JC
395 /* Attempt to open non blocking the access dev */
396 fp = open(buffer_access, O_RDONLY | O_NONBLOCK);
7663a4aa 397 if (fp == -1) { /* TODO: If it isn't there make the node */
e58537cc 398 ret = -errno;
d9abc615 399 fprintf(stderr, "Failed to open %s\n", buffer_access);
52615d47 400 goto error_free_buffer_access;
e58537cc
JC
401 }
402
e58537cc 403 for (j = 0; j < num_loops; j++) {
30268a3d 404 if (!noevents) {
52615d47
JC
405 struct pollfd pfd = {
406 .fd = fp,
407 .events = POLLIN,
408 };
409
6bb7cac8
HK
410 ret = poll(&pfd, 1, -1);
411 if (ret < 0) {
412 ret = -errno;
413 goto error_close_buffer_access;
414 } else if (ret == 0) {
415 continue;
416 }
417
52615d47 418 toread = buf_len;
30268a3d 419 } else {
96df9799 420 usleep(timedelay);
30268a3d 421 toread = 64;
e58537cc 422 }
30268a3d 423
7663a4aa 424 read_size = read(fp, data, toread * scan_size);
97b603a4 425 if (read_size < 0) {
8749948a 426 if (errno == EAGAIN) {
d9abc615 427 fprintf(stderr, "nothing available\n");
97b603a4 428 continue;
7663a4aa 429 } else {
97b603a4 430 break;
7663a4aa 431 }
e58537cc 432 }
7663a4aa
HK
433 for (i = 0; i < read_size / scan_size; i++)
434 process_scan(data + scan_size * i, channels,
e58537cc
JC
435 num_channels);
436 }
437
d8da0eee 438 /* Stop the buffer */
e58537cc
JC
439 ret = write_sysfs_int("enable", buf_dir_name, 0);
440 if (ret < 0)
52615d47 441 goto error_close_buffer_access;
e58537cc 442
b6d5be57
KW
443 if (!notrigger)
444 /* Disconnect the trigger - just write a dummy name. */
6bb7cac8
HK
445 ret = write_sysfs_string("trigger/current_trigger",
446 dev_dir_name, "NULL");
447 if (ret < 0)
d9abc615
CO
448 fprintf(stderr, "Failed to write to %s\n",
449 dev_dir_name);
e58537cc 450
e58537cc 451error_close_buffer_access:
6bb7cac8
HK
452 if (close(fp) == -1)
453 perror("Failed to close buffer");
7663a4aa 454
e58537cc
JC
455error_free_buffer_access:
456 free(buffer_access);
a71bfb4a
HK
457error_free_data:
458 free(data);
e58537cc
JC
459error_free_buf_dir_name:
460 free(buf_dir_name);
63f05c85
HK
461error_free_channels:
462 for (i = num_channels - 1; i >= 0; i--) {
463 free(channels[i].name);
464 free(channels[i].generic_name);
465 }
466 free(channels);
e58537cc
JC
467error_free_triggername:
468 if (datardytrigger)
469 free(trigger_name);
7663a4aa 470
d3ccfc41
HK
471error_free_dev_dir_name:
472 free(dev_dir_name);
0e799878 473
e58537cc
JC
474 return ret;
475}
This page took 0.492734 seconds and 5 git commands to generate.