tools:iio:generic_buffer: catch errors for arguments conversion
[platform/kernel/linux-starfive.git] / tools / iio / generic_buffer.c
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>
22 #include <stdlib.h>
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>
30 #include <string.h>
31 #include <poll.h>
32 #include <endian.h>
33 #include <getopt.h>
34 #include <inttypes.h>
35 #include "iio_utils.h"
36
37 /**
38  * size_from_channelarray() - calculate the storage size of a scan
39  * @channels:           the channel info array
40  * @num_channels:       number of channels
41  *
42  * Has the side effect of filling the channels[i].location values used
43  * in processing the buffer output.
44  **/
45 int size_from_channelarray(struct iio_channel_info *channels, int num_channels)
46 {
47         int bytes = 0;
48         int i = 0;
49
50         while (i < num_channels) {
51                 if (bytes % channels[i].bytes == 0)
52                         channels[i].location = bytes;
53                 else
54                         channels[i].location = bytes - bytes%channels[i].bytes
55                                 + channels[i].bytes;
56                 bytes = channels[i].location + channels[i].bytes;
57                 i++;
58         }
59         return bytes;
60 }
61
62 void print2byte(uint16_t input, struct iio_channel_info *info)
63 {
64         /* First swap if incorrect endian */
65         if (info->be)
66                 input = be16toh(input);
67         else
68                 input = le16toh(input);
69
70         /*
71          * Shift before conversion to avoid sign extension
72          * of left aligned data
73          */
74         input >>= info->shift;
75         input &= info->mask;
76         if (info->is_signed) {
77                 int16_t val = (int16_t)(input << (16 - info->bits_used)) >>
78                               (16 - info->bits_used);
79                 printf("%05f ", ((float)val + info->offset) * info->scale);
80         } else {
81                 printf("%05f ", ((float)input + info->offset) * info->scale);
82         }
83 }
84
85 void print4byte(uint32_t input, struct iio_channel_info *info)
86 {
87         /* First swap if incorrect endian */
88         if (info->be)
89                 input = be32toh(input);
90         else
91                 input = le32toh(input);
92
93         /*
94          * Shift before conversion to avoid sign extension
95          * of left aligned data
96          */
97         input >>= info->shift;
98         input &= info->mask;
99         if (info->is_signed) {
100                 int32_t val = (int32_t)(input << (32 - info->bits_used)) >>
101                               (32 - info->bits_used);
102                 printf("%05f ", ((float)val + info->offset) * info->scale);
103         } else {
104                 printf("%05f ", ((float)input + info->offset) * info->scale);
105         }
106 }
107
108 void print8byte(uint64_t input, struct iio_channel_info *info)
109 {
110         /* First swap if incorrect endian */
111         if (info->be)
112                 input = be64toh(input);
113         else
114                 input = le64toh(input);
115
116         /*
117          * Shift before conversion to avoid sign extension
118          * of left aligned data
119          */
120         input >>= info->shift;
121         input &= info->mask;
122         if (info->is_signed) {
123                 int64_t val = (int64_t)(input << (64 - info->bits_used)) >>
124                               (64 - info->bits_used);
125                 /* special case for timestamp */
126                 if (info->scale == 1.0f && info->offset == 0.0f)
127                         printf("%" PRId64 " ", val);
128                 else
129                         printf("%05f ",
130                                ((float)val + info->offset) * info->scale);
131         } else {
132                 printf("%05f ", ((float)input + info->offset) * info->scale);
133         }
134 }
135
136 /**
137  * process_scan() - print out the values in SI units
138  * @data:               pointer to the start of the scan
139  * @channels:           information about the channels. Note
140  *  size_from_channelarray must have been called first to fill the
141  *  location offsets.
142  * @num_channels:       number of channels
143  **/
144 void process_scan(char *data,
145                   struct iio_channel_info *channels,
146                   int num_channels)
147 {
148         int k;
149
150         for (k = 0; k < num_channels; k++)
151                 switch (channels[k].bytes) {
152                         /* only a few cases implemented so far */
153                 case 2:
154                         print2byte(*(uint16_t *)(data + channels[k].location),
155                                    &channels[k]);
156                         break;
157                 case 4:
158                         print4byte(*(uint32_t *)(data + channels[k].location),
159                                    &channels[k]);
160                         break;
161                 case 8:
162                         print8byte(*(uint64_t *)(data + channels[k].location),
163                                    &channels[k]);
164                         break;
165                 default:
166                         break;
167                 }
168         printf("\n");
169 }
170
171 int main(int argc, char **argv)
172 {
173         unsigned long num_loops = 2;
174         unsigned long timedelay = 1000000;
175         unsigned long buf_len = 128;
176
177         int ret, c, i, j, toread;
178         int fp;
179
180         int num_channels;
181         char *trigger_name = NULL, *device_name = NULL;
182         char *dev_dir_name, *buf_dir_name;
183
184         int datardytrigger = 1;
185         char *data;
186         ssize_t read_size;
187         int dev_num, trig_num;
188         char *buffer_access;
189         int scan_size;
190         int noevents = 0;
191         int notrigger = 0;
192         char *dummy;
193
194         struct iio_channel_info *channels;
195
196         while ((c = getopt(argc, argv, "l:w:c:et:n:g")) != -1) {
197                 switch (c) {
198                 case 'n':
199                         device_name = optarg;
200                         break;
201                 case 't':
202                         trigger_name = optarg;
203                         datardytrigger = 0;
204                         break;
205                 case 'e':
206                         noevents = 1;
207                         break;
208                 case 'c':
209                         errno = 0;
210                         num_loops = strtoul(optarg, &dummy, 10);
211                         if (errno)
212                                 return -errno;
213                         break;
214                 case 'w':
215                         errno = 0;
216                         timedelay = strtoul(optarg, &dummy, 10);
217                         if (errno)
218                                 return -errno;
219                         break;
220                 case 'l':
221                         errno = 0;
222                         buf_len = strtoul(optarg, &dummy, 10);
223                         if (errno)
224                                 return -errno;
225                         break;
226                 case 'g':
227                         notrigger = 1;
228                         break;
229                 case '?':
230                         return -1;
231                 }
232         }
233
234         if (device_name == NULL)
235                 return -1;
236
237         /* Find the device requested */
238         dev_num = find_type_by_name(device_name, "iio:device");
239         if (dev_num < 0) {
240                 printf("Failed to find the %s\n", device_name);
241                 ret = dev_num;
242                 goto error_ret;
243         }
244         printf("iio device number being used is %d\n", dev_num);
245
246         ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num);
247         if (ret < 0)
248                 return -ENOMEM;
249
250         if (!notrigger) {
251                 if (trigger_name == NULL) {
252                         /*
253                          * Build the trigger name. If it is device associated
254                          * its name is <device_name>_dev[n] where n matches
255                          * the device number found above.
256                          */
257                         ret = asprintf(&trigger_name,
258                                        "%s-dev%d", device_name, dev_num);
259                         if (ret < 0) {
260                                 ret = -ENOMEM;
261                                 goto error_free_dev_dir_name;
262                         }
263                 }
264
265                 /* Verify the trigger exists */
266                 trig_num = find_type_by_name(trigger_name, "trigger");
267                 if (trig_num < 0) {
268                         printf("Failed to find the trigger %s\n", trigger_name);
269                         ret = trig_num;
270                         goto error_free_triggername;
271                 }
272                 printf("iio trigger number being used is %d\n", trig_num);
273         } else
274                 printf("trigger-less mode selected\n");
275
276         /*
277          * Parse the files in scan_elements to identify what channels are
278          * present
279          */
280         ret = build_channel_array(dev_dir_name, &channels, &num_channels);
281         if (ret) {
282                 printf("Problem reading scan element information\n");
283                 printf("diag %s\n", dev_dir_name);
284                 goto error_free_triggername;
285         }
286
287         /*
288          * Construct the directory name for the associated buffer.
289          * As we know that the lis3l02dq has only one buffer this may
290          * be built rather than found.
291          */
292         ret = asprintf(&buf_dir_name,
293                        "%siio:device%d/buffer", iio_dir, dev_num);
294         if (ret < 0) {
295                 ret = -ENOMEM;
296                 goto error_free_channels;
297         }
298
299         if (!notrigger) {
300                 printf("%s %s\n", dev_dir_name, trigger_name);
301                 /* Set the device trigger to be the data ready trigger found
302                  * above */
303                 ret = write_sysfs_string_and_verify("trigger/current_trigger",
304                                                     dev_dir_name,
305                                                     trigger_name);
306                 if (ret < 0) {
307                         printf("Failed to write current_trigger file\n");
308                         goto error_free_buf_dir_name;
309                 }
310         }
311
312         /* Setup ring buffer parameters */
313         ret = write_sysfs_int("length", buf_dir_name, buf_len);
314         if (ret < 0)
315                 goto error_free_buf_dir_name;
316
317         /* Enable the buffer */
318         ret = write_sysfs_int("enable", buf_dir_name, 1);
319         if (ret < 0)
320                 goto error_free_buf_dir_name;
321         scan_size = size_from_channelarray(channels, num_channels);
322         data = malloc(scan_size*buf_len);
323         if (!data) {
324                 ret = -ENOMEM;
325                 goto error_free_buf_dir_name;
326         }
327
328         ret = asprintf(&buffer_access, "/dev/iio:device%d", dev_num);
329         if (ret < 0) {
330                 ret = -ENOMEM;
331                 goto error_free_data;
332         }
333
334         /* Attempt to open non blocking the access dev */
335         fp = open(buffer_access, O_RDONLY | O_NONBLOCK);
336         if (fp == -1) { /* If it isn't there make the node */
337                 ret = -errno;
338                 printf("Failed to open %s\n", buffer_access);
339                 goto error_free_buffer_access;
340         }
341
342         /* Wait for events 10 times */
343         for (j = 0; j < num_loops; j++) {
344                 if (!noevents) {
345                         struct pollfd pfd = {
346                                 .fd = fp,
347                                 .events = POLLIN,
348                         };
349
350                         poll(&pfd, 1, -1);
351                         toread = buf_len;
352
353                 } else {
354                         usleep(timedelay);
355                         toread = 64;
356                 }
357
358                 read_size = read(fp,
359                                  data,
360                                  toread*scan_size);
361                 if (read_size < 0) {
362                         if (errno == EAGAIN) {
363                                 printf("nothing available\n");
364                                 continue;
365                         } else
366                                 break;
367                 }
368                 for (i = 0; i < read_size/scan_size; i++)
369                         process_scan(data + scan_size*i,
370                                      channels,
371                                      num_channels);
372         }
373
374         /* Stop the buffer */
375         ret = write_sysfs_int("enable", buf_dir_name, 0);
376         if (ret < 0)
377                 goto error_close_buffer_access;
378
379         if (!notrigger)
380                 /* Disconnect the trigger - just write a dummy name. */
381                 write_sysfs_string("trigger/current_trigger",
382                                    dev_dir_name, "NULL");
383
384 error_close_buffer_access:
385         close(fp);
386 error_free_buffer_access:
387         free(buffer_access);
388 error_free_data:
389         free(data);
390 error_free_buf_dir_name:
391         free(buf_dir_name);
392 error_free_channels:
393         for (i = num_channels - 1; i >= 0; i--) {
394                 free(channels[i].name);
395                 free(channels[i].generic_name);
396         }
397         free(channels);
398 error_free_triggername:
399         if (datardytrigger)
400                 free(trigger_name);
401 error_free_dev_dir_name:
402         free(dev_dir_name);
403 error_ret:
404         return ret;
405 }