iio.c: run clang format on iio.c
[contrib/mraa.git] / src / iio / iio.c
1 /*
2  * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3  * Copyright (c) 2015 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24
25 #include "iio.h"
26 #include "mraa_internal.h"
27 #include "dirent.h"
28 #include <string.h>
29 #include <poll.h>
30 #include <stropts.h>
31
32 #define MAX_SIZE 128
33 #define IIO_DEVICE "iio:device"
34 #define IIO_SCAN_ELEM "scan_elements"
35 #define IIO_SLASH_DEV "/dev/" IIO_DEVICE
36 #define IIO_SYSFS_DEVICE "/sys/bus/iio/devices/" IIO_DEVICE
37 #define IIO_EVENTS "events"
38
39 mraa_iio_context
40 mraa_iio_init(int device)
41 {
42     if (device > plat_iio->iio_device_count) {
43         return NULL;
44     }
45
46     mraa_iio_get_channel_data(&plat_iio->iio_devices[device]);
47     mraa_iio_get_event_data(&plat_iio->iio_devices[device]);
48
49     return &plat_iio->iio_devices[device];
50 }
51
52 int
53 mraa_iio_read_size(mraa_iio_context dev)
54 {
55     return dev->datasize;
56 }
57
58 mraa_iio_channel*
59 mraa_iio_get_channels(mraa_iio_context dev)
60 {
61     return dev->channels;
62 }
63
64 int
65 mraa_iio_get_channel_count(mraa_iio_context dev)
66 {
67     return dev->chan_num;
68 }
69
70 mraa_result_t
71 mraa_iio_get_channel_data(mraa_iio_context dev)
72 {
73     const struct dirent* ent;
74     DIR* dir;
75     int chan_num = 0;
76     char buf[MAX_SIZE];
77     char readbuf[32];
78     int fd;
79     int ret = 0;
80     int padint = 0;
81     int curr_bytes = 0;
82     char shortbuf, signchar;
83
84     dev->datasize = 0;
85
86     memset(buf, 0, MAX_SIZE);
87     snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_SCAN_ELEM, dev->num);
88     dir = opendir(buf);
89     if (dir != NULL) {
90         while ((ent = readdir(dir)) != NULL) {
91             if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), "_en") == 0) {
92                 chan_num++;
93             }
94         }
95     }
96     dev->chan_num = chan_num;
97     // no need proceed if no channel found
98     if (chan_num == 0)
99         return MRAA_SUCCESS;
100     mraa_iio_channel* chan;
101     dev->channels = calloc(chan_num, sizeof(mraa_iio_channel));
102     seekdir(dir, 0);
103     while ((ent = readdir(dir)) != NULL) {
104         if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_index"), "_index") == 0) {
105             snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_SCAN_ELEM "/%s", dev->num, ent->d_name);
106             fd = open(buf, O_RDONLY);
107             if (fd > 0) {
108                 if (read(fd, readbuf, 2 * sizeof(char)) != 2) {
109                     break;
110                 }
111                 chan_num = ((int) strtol(readbuf, NULL, 10));
112                 chan = &dev->channels[chan_num];
113                 chan->index = chan_num;
114                 close(fd);
115
116                 buf[(strlen(buf) - 5)] = '\0';
117                 char* str = strdup(buf);
118                 // grab the type of the buffer
119                 snprintf(buf, MAX_SIZE, "%stype", str);
120                 fd = open(buf, O_RDONLY);
121                 if (fd > 0) {
122                     read(fd, readbuf, 31 * sizeof(char));
123                     ret = sscanf(readbuf, "%ce:%c%u/%u>>%u", &shortbuf, &signchar, &chan->bits_used,
124                                  &padint, &chan->shift);
125                     chan->bytes = padint / 8;
126                     if (curr_bytes % chan->bytes == 0) {
127                         chan->location = curr_bytes;
128                     } else {
129                         chan->location = curr_bytes - curr_bytes % chan->bytes + chan->bytes;
130                     }
131                     curr_bytes = chan->location + chan->bytes;
132                     // probably should be 5?
133                     if (ret < 0) {
134                         // cleanup
135                         free(str);
136                         close(fd);
137                         return MRAA_IO_SETUP_FAILURE;
138                     }
139                     chan->signedd = (signchar == 's');
140                     chan->lendian = (shortbuf == 'l');
141                     if (chan->bits_used == 64) {
142                         chan->mask = ~0;
143                     } else {
144                         chan->mask = (1 << chan->bits_used) - 1;
145                     }
146                     close(fd);
147                 }
148                 // grab the enable flag of channel
149                 snprintf(buf, MAX_SIZE, "%sen", str);
150                 fd = open(buf, O_RDONLY);
151                 if (fd > 0) {
152                     if (read(fd, readbuf, 2 * sizeof(char)) != 2) {
153                         syslog(LOG_ERR, "iio: Failed to read a sensible value from sysfs");
154                         return -1;
155                     }
156                     chan->enabled = (int) strtol(readbuf, NULL, 10);
157                     // only calculate enable buffer size for trigger buffer extract data
158                     if (chan->enabled) {
159                         dev->datasize += chan->bytes;
160                     }
161                     close(fd);
162                 }
163                 // clean up str var
164                 free(str);
165             }
166         }
167     }
168
169     return MRAA_SUCCESS;
170 }
171
172 const char*
173 mraa_iio_get_device_name(mraa_iio_context dev)
174 {
175     return dev->name;
176 }
177
178 int
179 mraa_iio_get_device_num_by_name(const char* name)
180 {
181     int i;
182
183     if (plat_iio == NULL) {
184         syslog(LOG_ERR, "iio: platform IIO structure is not initialized");
185         return -1;
186     }
187
188     if (name == NULL) {
189         syslog(LOG_ERR, "iio: device name is NULL, unable to find its number");
190         return -1;
191     }
192
193     for (i = 0; i < plat_iio->iio_device_count; i++) {
194         struct _iio* device;
195         device = &plat_iio->iio_devices[i];
196         // we want to check for exact match
197         if (strncmp(device->name, name, strlen(device->name) + 1) == 0) {
198             return device->num;
199         }
200     }
201
202     return -1;
203 }
204
205 mraa_result_t
206 mraa_iio_read(mraa_iio_context dev, const char* attr_chan, float* data)
207 {
208     char buf[MAX_SIZE];
209     snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/%s", dev->num, attr_chan);
210     int fd = open(buf, O_RDONLY);
211     if (fd != -1) {
212         int len = read(fd, &buf, MAX_SIZE);
213         *data = strtol(buf, NULL, 10);
214         return MRAA_SUCCESS;
215     }
216     return MRAA_ERROR_UNSPECIFIED;
217 }
218
219 mraa_result_t
220 mraa_iio_write(mraa_iio_context dev, const char* attr_chan, const char* data)
221 {
222     char buf[128];
223     snprintf(buf, 128, IIO_SYSFS_DEVICE "%d/%s", dev->num, attr_chan);
224     int fd = open(buf, O_WRONLY);
225     if (fd != -1) {
226         write(fd, data, (strlen(data) + 1));
227         return MRAA_SUCCESS;
228     }
229     return MRAA_ERROR_UNSPECIFIED;
230 }
231
232 static mraa_result_t
233 mraa_iio_wait_event(int fd, char* data, int* read_size)
234 {
235     struct pollfd pfd;
236
237     if (fd < 0) {
238         return MRAA_ERROR_INVALID_RESOURCE;
239     }
240
241     pfd.fd = fd;
242     pfd.events = POLLIN;
243
244     // Wait for it forever or until pthread_cancel
245     // poll is a cancelable point like sleep()
246     int x = poll(&pfd, 1, -1);
247
248     memset(data, 0, 100);
249     *read_size = read(fd, data, 100);
250
251     return MRAA_SUCCESS;
252 }
253
254 static void*
255 mraa_iio_trigger_handler(void* arg)
256 {
257     mraa_iio_context dev = (mraa_iio_context) arg;
258     int i;
259     char data[MAX_SIZE * 100];
260     int read_size;
261
262     for (;;) {
263         if (mraa_iio_wait_event(dev->fp, &data[0], &read_size) == MRAA_SUCCESS) {
264             pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
265             // only can process if readsize >= enabled channel's datasize
266             for (i = 0; i < (read_size / dev->datasize); i++) {
267                 dev->isr(&data);
268             }
269             pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
270         } else {
271             // we must have got an error code so die nicely
272             pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
273             return NULL;
274         }
275     }
276 }
277
278 mraa_result_t
279 mraa_iio_trigger_buffer(mraa_iio_context dev, void (*fptr)(char* data), void* args)
280 {
281     char bu[MAX_SIZE];
282     if (dev->thread_id != 0) {
283         return MRAA_ERROR_NO_RESOURCES;
284     }
285
286     sprintf(bu, IIO_SLASH_DEV "%d", dev->num);
287     dev->fp = open(bu, O_RDONLY | O_NONBLOCK);
288     if (dev->fp == -1) {
289         return MRAA_ERROR_INVALID_RESOURCE;
290     }
291
292     dev->isr = fptr;
293     pthread_create(&dev->thread_id, NULL, mraa_iio_trigger_handler, (void*) dev);
294
295     return MRAA_SUCCESS;
296 }
297
298 mraa_result_t
299 mraa_iio_get_event_data(mraa_iio_context dev)
300 {
301     const struct dirent* ent;
302     DIR* dir;
303     int event_num = 0;
304     char buf[MAX_SIZE];
305     char readbuf[32];
306     int fd;
307     int ret = 0;
308     int padint = 0;
309     int curr_bytes = 0;
310     char shortbuf, signchar;
311     memset(buf, 0, MAX_SIZE);
312     snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_EVENTS, dev->num);
313     dir = opendir(buf);
314     if (dir != NULL) {
315         while ((ent = readdir(dir)) != NULL) {
316             if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), "_en") == 0) {
317                 event_num++;
318             }
319         }
320         dev->event_num = event_num;
321         // no need proceed if no event found
322         if (event_num == 0)
323             return MRAA_SUCCESS;
324         mraa_iio_event* event;
325         dev->events = calloc(event_num, sizeof(mraa_iio_event));
326         if (dev->events == NULL) {
327             closedir(dir);
328             return MRAA_ERROR_UNSPECIFIED;
329         }
330         rewinddir(dir);
331         event_num = 0;
332         while ((ent = readdir(dir)) != NULL) {
333             if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), "_en") == 0) {
334                 event = &dev->events[event_num];
335                 event->name = strdup(ent->d_name);
336                 snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_EVENTS "/%s", dev->num, ent->d_name);
337                 fd = open(buf, O_RDONLY);
338                 if (fd > 0) {
339                     if (read(fd, readbuf, 2 * sizeof(char)) != 2) {
340                         break;
341                     }
342                     close(fd);
343                 }
344                 event->enabled = ((int) strtol(readbuf, NULL, 10));
345                 // Todo, read other event info.
346                 event_num++;
347             }
348         }
349         closedir(dir);
350     }
351     return MRAA_SUCCESS;
352 }
353
354 mraa_result_t
355 mraa_iio_event_read(mraa_iio_context dev, const char* attribute, float* data)
356 {
357     char buf[MAX_SIZE];
358     snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_EVENTS "/%s", dev->num, attribute);
359     int fd = open(buf, O_RDONLY);
360     if (fd != -1) {
361         int len = read(fd, &buf, MAX_SIZE);
362         *data = strtol(buf, NULL, 10);
363         return MRAA_SUCCESS;
364     }
365     return MRAA_ERROR_UNSPECIFIED;
366 }
367
368 mraa_result_t
369 mraa_iio_event_write(mraa_iio_context dev, const char* attribute, const char* data)
370 {
371     int len;
372     char buf[MAX_SIZE];
373     snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_EVENTS "/%s", dev->num, attribute);
374     int fd = open(buf, O_WRONLY);
375     if (fd != -1) {
376         int len = write(fd, data, (strlen(data) + 1));
377         return MRAA_SUCCESS;
378     }
379     return MRAA_ERROR_UNSPECIFIED;
380 }
381
382 static mraa_result_t
383 mraa_iio_event_poll_nonblock(int fd, struct iio_event_data* data)
384 {
385     struct pollfd pfd;
386
387     if (fd < 0) {
388         return MRAA_ERROR_INVALID_RESOURCE;
389     }
390
391     pfd.fd = fd;
392     pfd.events = POLLIN;
393
394     // Wait for it forever or until pthread_cancel
395     // poll is a cancelable point like sleep()
396     int x = poll(&pfd, 1, -1);
397
398     read(fd, data, sizeof(struct iio_event_data));
399
400     return MRAA_SUCCESS;
401 }
402
403 mraa_result_t
404 mraa_iio_event_poll(mraa_iio_context dev, struct iio_event_data* data)
405 {
406     char bu[MAX_SIZE];
407     int ret;
408     int event_fd;
409     int fd;
410
411     sprintf(bu, IIO_SLASH_DEV "%d", dev->num);
412     fd = open(bu, 0);
413     ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd);
414     close(fd);
415
416     if (ret == -1 || event_fd == -1)
417         return MRAA_ERROR_UNSPECIFIED;
418
419     ret = read(event_fd, data, sizeof(struct iio_event_data));
420
421     close(event_fd);
422     return MRAA_SUCCESS;
423 }
424
425 static void*
426 mraa_iio_event_handler(void* arg)
427 {
428     struct iio_event_data data;
429     mraa_iio_context dev = (mraa_iio_context) arg;
430
431     for (;;) {
432         if (mraa_iio_event_poll_nonblock(dev->fp_event, &data) == MRAA_SUCCESS) {
433             pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
434             dev->isr_event(&data);
435             pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
436         } else {
437             // we must have got an error code so die nicely
438             pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
439             return NULL;
440         }
441     }
442 }
443
444 mraa_result_t
445 mraa_iio_event_setup_callback(mraa_iio_context dev, void (*fptr)(struct iio_event_data* data), void* args)
446 {
447     int ret;
448     char bu[MAX_SIZE];
449     if (dev->thread_id != 0) {
450         return MRAA_ERROR_NO_RESOURCES;
451     }
452
453     sprintf(bu, IIO_SLASH_DEV "%d", dev->num);
454     dev->fp = open(bu, O_RDONLY | O_NONBLOCK);
455     if (dev->fp == -1) {
456         return MRAA_ERROR_INVALID_RESOURCE;
457     }
458     ret = ioctl(dev->fp, IIO_GET_EVENT_FD_IOCTL, &dev->fp_event);
459     close(dev->fp);
460
461     if (ret == -1 || dev->fp_event == -1) {
462         return MRAA_ERROR_UNSPECIFIED;
463     }
464
465     dev->isr_event = fptr;
466     pthread_create(&dev->thread_id, NULL, mraa_iio_event_handler, (void*) dev);
467
468     return MRAA_SUCCESS;
469 }
470
471 mraa_result_t
472 mraa_iio_event_extract_event(struct iio_event_data* event,
473                              int* chan_type,
474                              int* modifier,
475                              int* type,
476                              int* direction,
477                              int* channel,
478                              int* channel2,
479                              int* different)
480 {
481     *chan_type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
482     *modifier = IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
483     *type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
484     *direction = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
485     *channel = IIO_EVENT_CODE_EXTRACT_CHAN(event->id);
486     *channel2 = IIO_EVENT_CODE_EXTRACT_CHAN2(event->id);
487     *different = IIO_EVENT_CODE_EXTRACT_DIFF(event->id);
488     return MRAA_SUCCESS;
489 }
490 #if 0
491 // does stop make any sense on iio devices?
492 mraa_result_t
493 mraa_iio_stop(mraa_iio_context dev)
494 {
495     return MRAA_SUCCESS;
496 }
497 #endif