2ba8e281ac155799b9caa663d0fba6d775acf3f6
[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,
124                                     &signchar, &chan->bits_used,
125                                     &padint, &chan->shift);
126                     chan->bytes = padint / 8;
127                     if (curr_bytes % chan->bytes == 0) {
128                         chan->location = curr_bytes;
129                     } else {
130                         chan->location = curr_bytes - curr_bytes%chan->bytes + chan->bytes;
131                     }
132                     curr_bytes = chan->location + chan->bytes;
133                     // probably should be 5?
134                     if (ret < 0) {
135                         // cleanup
136                         free(str);
137                         close(fd);
138                         return MRAA_IO_SETUP_FAILURE;
139                     }
140                     chan->signedd = (signchar == 's');
141                     chan->lendian = (shortbuf == 'l');
142                     if (chan->bits_used == 64) {
143                         chan->mask = ~0;
144                     } else {
145                         chan->mask = (1 << chan->bits_used) - 1;
146                     }
147                     close(fd);
148                 }
149                 // grab the enable flag of channel
150                 snprintf(buf, MAX_SIZE, "%sen", str);
151                 fd = open(buf, O_RDONLY);
152                 if (fd > 0) {
153                     if (read(fd, readbuf, 2 * sizeof(char)) != 2) {
154                         syslog(LOG_ERR, "iio: Failed to read a sensible value from sysfs");
155                         return -1;
156                     }
157                     chan->enabled = (int) strtol(readbuf, NULL, 10);
158                     //only calculate enable buffer size for trigger buffer extract data
159                     if (chan->enabled) {
160                         dev->datasize += chan->bytes;
161                     }
162                     close(fd);
163                 }
164                 // clean up str var
165                 free(str);
166             }
167         }
168     }
169
170     return MRAA_SUCCESS;
171 }
172
173 const char*
174 mraa_iio_get_device_name(mraa_iio_context dev)
175 {
176     return dev->name;
177 }
178
179 int
180 mraa_iio_get_device_num_by_name(const char* name)
181 {
182     int i;
183
184     if (plat_iio == NULL) {
185         syslog(LOG_ERR, "iio: platform IIO structure is not initialized");
186         return -1;
187     }
188
189     if (name == NULL) {
190         syslog(LOG_ERR, "iio: device name is NULL, unable to find its number");
191         return -1;
192     }
193
194     for (i = 0; i < plat_iio->iio_device_count; i++) {
195         struct _iio* device;
196         device = &plat_iio->iio_devices[i];
197         // we want to check for exact match
198         if (strncmp(device->name, name, strlen(device->name)+1) == 0) {
199             return device->num;
200         }
201     }
202
203     return -1;
204 }
205
206 mraa_result_t
207 mraa_iio_read(mraa_iio_context dev, const char* attr_chan, float* data)
208 {
209     char buf[MAX_SIZE];
210     snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/%s", dev->num, attr_chan);
211     int fd = open(buf, O_RDONLY);
212     if (fd != -1) {
213         int len = read(fd, &buf, MAX_SIZE);
214         *data = strtol(buf, NULL, 10);
215         return MRAA_SUCCESS;
216     }
217     return MRAA_ERROR_UNSPECIFIED;
218 }
219
220 mraa_result_t
221 mraa_iio_write(mraa_iio_context dev, const char* attr_chan, const char* data)
222 {
223     char buf[128];
224     snprintf(buf, 128, IIO_SYSFS_DEVICE "%d/%s", dev->num, attr_chan);
225     int fd = open(buf, O_WRONLY);
226     if (fd != -1) {
227         write(fd, data, (strlen(data)+1));
228         return MRAA_SUCCESS;
229     }
230     return MRAA_ERROR_UNSPECIFIED;
231 }
232
233 static mraa_result_t
234 mraa_iio_wait_event(int fd, char* data, int *read_size)
235 {
236     struct pollfd pfd;
237
238     if (fd < 0) {
239         return MRAA_ERROR_INVALID_RESOURCE;
240     }
241
242     pfd.fd = fd;
243     pfd.events = POLLIN;
244
245     // Wait for it forever or until pthread_cancel
246     // poll is a cancelable point like sleep()
247     int x = poll(&pfd, 1, -1);
248
249     memset(data, 0, 100);
250     *read_size = read(fd, data, 100);
251
252     return MRAA_SUCCESS;
253 }
254
255 static void*
256 mraa_iio_trigger_handler(void* arg)
257 {
258     mraa_iio_context dev = (mraa_iio_context) arg;
259     int i;
260     char data[MAX_SIZE*100];
261     int read_size;
262
263     for (;;) {
264         if (mraa_iio_wait_event(dev->fp, &data[0], &read_size) == MRAA_SUCCESS) {
265             pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
266             //only can process if readsize >= enabled channel's datasize
267             for (i=0; i<(read_size/dev->datasize); i++) {
268                 dev->isr(&data);
269             }
270             pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
271         } else {
272             // we must have got an error code so die nicely
273             pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
274             return NULL;
275         }
276     }
277 }
278
279 mraa_result_t
280 mraa_iio_trigger_buffer(mraa_iio_context dev, void (*fptr)(char* data), void* args)
281 {
282     char bu[MAX_SIZE];
283     if (dev->thread_id != 0) {
284         return MRAA_ERROR_NO_RESOURCES;
285     }
286
287     sprintf(bu, IIO_SLASH_DEV "%d", dev->num);
288     dev->fp = open(bu, O_RDONLY | O_NONBLOCK);
289     if (dev->fp == -1) {
290         return MRAA_ERROR_INVALID_RESOURCE;
291     }
292
293     dev->isr = fptr;
294     pthread_create(&dev->thread_id, NULL, mraa_iio_trigger_handler, (void*) dev);
295
296     return MRAA_SUCCESS;
297 }
298
299 mraa_result_t
300 mraa_iio_get_event_data(mraa_iio_context dev)
301 {
302     const struct dirent *ent;
303     DIR* dir;
304     int event_num = 0;
305     char buf[MAX_SIZE];
306     char readbuf[32];
307     int fd;
308     int ret = 0;
309     int padint = 0;
310     int curr_bytes = 0;
311     char shortbuf, signchar;
312     memset(buf, 0, MAX_SIZE);
313     snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_EVENTS, dev->num);
314     dir = opendir(buf);
315     if (dir != NULL) {
316         while ((ent = readdir(dir)) != NULL) {
317             if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), "_en") == 0) {
318                 event_num++;
319             }
320         }
321         dev->event_num = event_num;
322                 //no need proceed if no event found
323                 if (event_num == 0)
324                         return MRAA_SUCCESS;
325         mraa_iio_event* event;
326                 dev->events = calloc(event_num, sizeof(mraa_iio_event));
327                 if ( dev->events == NULL)
328                 {
329                         closedir(dir);
330                         return MRAA_ERROR_UNSPECIFIED;
331                 }
332                 rewinddir(dir);
333                 event_num = 0;
334                 while ((ent = readdir(dir)) != NULL)
335                 {
336             if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), "_en") == 0) {
337                                 event = &dev->events[event_num];
338                 event->name = strdup(ent->d_name);
339                                 snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_EVENTS "/%s", dev->num, ent->d_name);
340                                 fd = open(buf, O_RDONLY);
341                                 if (fd > 0) {
342                                         if (read(fd, readbuf, 2 * sizeof(char)) != 2) {
343                                                 break;
344                                         }
345                                         close(fd);
346                                 }
347                 event->enabled = ((int) strtol(readbuf, NULL, 10));
348                                 //Todo, read other event info.
349                                 event_num++;
350             }
351         }
352                 closedir(dir);
353     }
354     return MRAA_SUCCESS;
355 }
356
357 mraa_result_t
358 mraa_iio_event_read(mraa_iio_context dev, const char* attribute, float* data)
359 {
360     char buf[MAX_SIZE];
361     snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_EVENTS "/%s", dev->num, attribute);
362     int fd = open(buf, O_RDONLY);
363     if (fd != -1) {
364         int len = read(fd, &buf, MAX_SIZE);
365         *data = strtol(buf, NULL, 10);
366         return MRAA_SUCCESS;
367     }
368     return MRAA_ERROR_UNSPECIFIED;
369 }
370
371 mraa_result_t
372 mraa_iio_event_write(mraa_iio_context dev, const char* attribute,  const char* data)
373 {
374         int len;
375     char buf[MAX_SIZE];
376     snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_EVENTS "/%s", dev->num, attribute);
377     int fd = open(buf, O_WRONLY);
378     if (fd != -1) {
379                 int len = write(fd, data, ( strlen(data) +1 ));
380         return MRAA_SUCCESS;
381     }
382     return MRAA_ERROR_UNSPECIFIED;
383 }
384
385 static mraa_result_t
386 mraa_iio_event_poll_nonblock(int fd, struct iio_event_data* data)
387 {
388     struct pollfd pfd;
389
390     if (fd < 0) {
391         return MRAA_ERROR_INVALID_RESOURCE;
392     }
393
394     pfd.fd = fd;
395     pfd.events = POLLIN;
396
397     // Wait for it forever or until pthread_cancel
398     // poll is a cancelable point like sleep()
399     int x = poll(&pfd, 1, -1);
400
401     read(fd, data, sizeof(struct iio_event_data));
402
403     return MRAA_SUCCESS;
404 }
405
406 mraa_result_t
407 mraa_iio_event_poll(mraa_iio_context dev, struct iio_event_data* data)
408 {
409     char bu[MAX_SIZE];
410     int ret;
411         int event_fd;
412         int fd;
413
414         sprintf(bu, IIO_SLASH_DEV "%d", dev->num);
415     fd = open(bu, 0);
416         ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd);
417         close(fd);
418
419         if (ret == -1 || event_fd == -1)
420                 return MRAA_ERROR_UNSPECIFIED;
421
422         ret = read(event_fd, data, sizeof(struct iio_event_data));
423
424         close(event_fd);
425     return MRAA_SUCCESS;
426 }
427
428 static void*
429 mraa_iio_event_handler(void* arg)
430 {
431         struct iio_event_data data;
432     mraa_iio_context dev = (mraa_iio_context) arg;
433
434     for (;;) {
435         if (mraa_iio_event_poll_nonblock(dev->fp_event, &data) == MRAA_SUCCESS) {
436             pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
437             dev->isr_event(&data);
438             pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
439         } else {
440             // we must have got an error code so die nicely
441             pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
442             return NULL;
443         }
444     }
445 }
446
447 mraa_result_t
448 mraa_iio_event_setup_callback(mraa_iio_context dev, void (*fptr)(struct iio_event_data* data), void* args)
449 {
450         int ret;
451     char bu[MAX_SIZE];
452     if (dev->thread_id != 0) {
453         return MRAA_ERROR_NO_RESOURCES;
454     }
455
456     sprintf(bu, IIO_SLASH_DEV "%d", dev->num);
457     dev->fp = open(bu, O_RDONLY | O_NONBLOCK);
458     if (dev->fp == -1) {
459         return MRAA_ERROR_INVALID_RESOURCE;
460     }
461         ret = ioctl(dev->fp, IIO_GET_EVENT_FD_IOCTL, &dev->fp_event);
462         close(dev->fp);
463
464         if (ret == -1 || dev->fp_event == -1)
465         {
466                 return MRAA_ERROR_UNSPECIFIED;
467         }
468
469     dev->isr_event = fptr;
470     pthread_create(&dev->thread_id, NULL, mraa_iio_event_handler, (void*) dev);
471
472     return MRAA_SUCCESS;
473 }
474
475 mraa_result_t
476 mraa_iio_event_extract_event(struct iio_event_data* event, int* chan_type, int* modifier, int* type, int* direction, int* channel, int* channel2, int* different)
477 {
478         *chan_type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
479         *modifier= IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
480         *type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
481         *direction = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
482         *channel = IIO_EVENT_CODE_EXTRACT_CHAN(event->id);
483         *channel2 = IIO_EVENT_CODE_EXTRACT_CHAN2(event->id);
484         *different = IIO_EVENT_CODE_EXTRACT_DIFF(event->id);
485     return MRAA_SUCCESS;
486 }
487 #if 0
488 // does stop make any sense on iio devices?
489 mraa_result_t
490 mraa_iio_stop(mraa_iio_context dev)
491 {
492     return MRAA_SUCCESS;
493 }
494 #endif
495