f8a32755c2f434bec14a1c698dc9398cfc65fef4
[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_float(mraa_iio_context dev, const char* filename, float* data)
207 {
208     char buf[MAX_SIZE];
209     snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/%s", dev->num, filename);
210     FILE* fp = fopen(buf, "r");
211     if (fp != NULL) {
212         fscanf(fp, "%f\n", data);
213         fclose(fp);
214         return MRAA_SUCCESS;
215     }
216     return MRAA_ERROR_UNSPECIFIED;
217 }
218
219 mraa_result_t
220 mraa_iio_read_integer(mraa_iio_context dev, const char* filename, int* data)
221 {
222     char buf[MAX_SIZE];
223     snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/%s", dev->num, filename);
224     FILE* fp = fopen(buf, "r");
225     if (fp != NULL) {
226         fscanf(fp, "%d\n", data);
227         fclose(fp);
228         return MRAA_SUCCESS;
229     }
230     return MRAA_ERROR_UNSPECIFIED;
231 }
232
233 mraa_result_t
234 mraa_iio_read_string(mraa_iio_context dev, const char* filename, char* data)
235 {
236     char buf[MAX_SIZE];
237     snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/%s", dev->num, filename);
238     FILE* fp = fopen(buf, "r");
239     if (fp != NULL) {
240         fscanf(fp, "%s\n", data);
241         fclose(fp);
242         return MRAA_SUCCESS;
243     }
244     return MRAA_ERROR_UNSPECIFIED;
245 }
246
247 mraa_result_t
248 mraa_iio_write_float(mraa_iio_context dev, const char* filename, const float data)
249 {
250     char buf[MAX_SIZE];
251     snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/%s", dev->num, filename);
252     FILE* fp = fopen(buf, "w");
253     if (fp != NULL) {
254         fprintf(fp, "%f", data);
255         fclose(fp);
256         return MRAA_SUCCESS;
257     }
258     return MRAA_ERROR_UNSPECIFIED;
259 }
260
261 mraa_result_t
262 mraa_iio_write_integer(mraa_iio_context dev, const char* filename, const int data)
263 {
264     char buf[MAX_SIZE];
265     snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/%s", dev->num, filename);
266     FILE* fp = fopen(buf, "w");
267     if (fp != NULL) {
268         fprintf(fp, "%d", data);
269         fclose(fp);
270         return MRAA_SUCCESS;
271     }
272     return MRAA_ERROR_UNSPECIFIED;
273 }
274
275 mraa_result_t
276 mraa_iio_write_string(mraa_iio_context dev, const char* filename, const char* data)
277 {
278     char buf[MAX_SIZE];
279     snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/%s", dev->num, filename);
280     FILE* fp = fopen(buf, "w");
281     if (fp != NULL) {
282         fprintf(fp, "%s", data);
283         fclose(fp);
284         return MRAA_SUCCESS;
285     }
286     return MRAA_ERROR_UNSPECIFIED;
287 }
288
289 static mraa_result_t
290 mraa_iio_wait_event(int fd, char* data, int* read_size)
291 {
292     struct pollfd pfd;
293
294     if (fd < 0) {
295         return MRAA_ERROR_INVALID_RESOURCE;
296     }
297
298     pfd.fd = fd;
299     pfd.events = POLLIN;
300
301     // Wait for it forever or until pthread_cancel
302     // poll is a cancelable point like sleep()
303     int x = poll(&pfd, 1, -1);
304
305     memset(data, 0, 100);
306     *read_size = read(fd, data, 100);
307
308     return MRAA_SUCCESS;
309 }
310
311 static void*
312 mraa_iio_trigger_handler(void* arg)
313 {
314     mraa_iio_context dev = (mraa_iio_context) arg;
315     int i;
316     char data[MAX_SIZE * 100];
317     int read_size;
318
319     for (;;) {
320         if (mraa_iio_wait_event(dev->fp, &data[0], &read_size) == MRAA_SUCCESS) {
321             pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
322             // only can process if readsize >= enabled channel's datasize
323             for (i = 0; i < (read_size / dev->datasize); i++) {
324                 dev->isr(&data);
325             }
326             pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
327         } else {
328             // we must have got an error code so die nicely
329             pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
330             return NULL;
331         }
332     }
333 }
334
335 mraa_result_t
336 mraa_iio_trigger_buffer(mraa_iio_context dev, void (*fptr)(char* data), void* args)
337 {
338     char bu[MAX_SIZE];
339     if (dev->thread_id != 0) {
340         return MRAA_ERROR_NO_RESOURCES;
341     }
342
343     sprintf(bu, IIO_SLASH_DEV "%d", dev->num);
344     dev->fp = open(bu, O_RDONLY | O_NONBLOCK);
345     if (dev->fp == -1) {
346         return MRAA_ERROR_INVALID_RESOURCE;
347     }
348
349     dev->isr = fptr;
350     pthread_create(&dev->thread_id, NULL, mraa_iio_trigger_handler, (void*) dev);
351
352     return MRAA_SUCCESS;
353 }
354
355 mraa_result_t
356 mraa_iio_get_event_data(mraa_iio_context dev)
357 {
358     const struct dirent* ent;
359     DIR* dir;
360     int event_num = 0;
361     char buf[MAX_SIZE];
362     char readbuf[32];
363     int fd;
364     int ret = 0;
365     int padint = 0;
366     int curr_bytes = 0;
367     char shortbuf, signchar;
368     memset(buf, 0, MAX_SIZE);
369     snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_EVENTS, dev->num);
370     dir = opendir(buf);
371     if (dir != NULL) {
372         while ((ent = readdir(dir)) != NULL) {
373             if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), "_en") == 0) {
374                 event_num++;
375             }
376         }
377         dev->event_num = event_num;
378         // no need proceed if no event found
379         if (event_num == 0)
380             return MRAA_SUCCESS;
381         mraa_iio_event* event;
382         dev->events = calloc(event_num, sizeof(mraa_iio_event));
383         if (dev->events == NULL) {
384             closedir(dir);
385             return MRAA_ERROR_UNSPECIFIED;
386         }
387         rewinddir(dir);
388         event_num = 0;
389         while ((ent = readdir(dir)) != NULL) {
390             if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), "_en") == 0) {
391                 event = &dev->events[event_num];
392                 event->name = strdup(ent->d_name);
393                 snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_EVENTS "/%s", dev->num, ent->d_name);
394                 fd = open(buf, O_RDONLY);
395                 if (fd > 0) {
396                     if (read(fd, readbuf, 2 * sizeof(char)) != 2) {
397                         break;
398                     }
399                     close(fd);
400                 }
401                 event->enabled = ((int) strtol(readbuf, NULL, 10));
402                 // Todo, read other event info.
403                 event_num++;
404             }
405         }
406         closedir(dir);
407     }
408     return MRAA_SUCCESS;
409 }
410
411 static mraa_result_t
412 mraa_iio_event_poll_nonblock(int fd, struct iio_event_data* data)
413 {
414     struct pollfd pfd;
415
416     if (fd < 0) {
417         return MRAA_ERROR_INVALID_RESOURCE;
418     }
419
420     pfd.fd = fd;
421     pfd.events = POLLIN;
422
423     // Wait for it forever or until pthread_cancel
424     // poll is a cancelable point like sleep()
425     int x = poll(&pfd, 1, -1);
426
427     read(fd, data, sizeof(struct iio_event_data));
428
429     return MRAA_SUCCESS;
430 }
431
432 mraa_result_t
433 mraa_iio_event_poll(mraa_iio_context dev, struct iio_event_data* data)
434 {
435     char bu[MAX_SIZE];
436     int ret;
437     int event_fd;
438     int fd;
439
440     sprintf(bu, IIO_SLASH_DEV "%d", dev->num);
441     fd = open(bu, 0);
442     ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd);
443     close(fd);
444
445     if (ret == -1 || event_fd == -1)
446         return MRAA_ERROR_UNSPECIFIED;
447
448     ret = read(event_fd, data, sizeof(struct iio_event_data));
449
450     close(event_fd);
451     return MRAA_SUCCESS;
452 }
453
454 static void*
455 mraa_iio_event_handler(void* arg)
456 {
457     struct iio_event_data data;
458     mraa_iio_context dev = (mraa_iio_context) arg;
459
460     for (;;) {
461         if (mraa_iio_event_poll_nonblock(dev->fp_event, &data) == MRAA_SUCCESS) {
462             pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
463             dev->isr_event(&data);
464             pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
465         } else {
466             // we must have got an error code so die nicely
467             pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
468             return NULL;
469         }
470     }
471 }
472
473 mraa_result_t
474 mraa_iio_event_setup_callback(mraa_iio_context dev, void (*fptr)(struct iio_event_data* data), void* args)
475 {
476     int ret;
477     char bu[MAX_SIZE];
478     if (dev->thread_id != 0) {
479         return MRAA_ERROR_NO_RESOURCES;
480     }
481
482     sprintf(bu, IIO_SLASH_DEV "%d", dev->num);
483     dev->fp = open(bu, O_RDONLY | O_NONBLOCK);
484     if (dev->fp == -1) {
485         return MRAA_ERROR_INVALID_RESOURCE;
486     }
487     ret = ioctl(dev->fp, IIO_GET_EVENT_FD_IOCTL, &dev->fp_event);
488     close(dev->fp);
489
490     if (ret == -1 || dev->fp_event == -1) {
491         return MRAA_ERROR_UNSPECIFIED;
492     }
493
494     dev->isr_event = fptr;
495     pthread_create(&dev->thread_id, NULL, mraa_iio_event_handler, (void*) dev);
496
497     return MRAA_SUCCESS;
498 }
499
500 mraa_result_t
501 mraa_iio_event_extract_event(struct iio_event_data* event,
502                              int* chan_type,
503                              int* modifier,
504                              int* type,
505                              int* direction,
506                              int* channel,
507                              int* channel2,
508                              int* different)
509 {
510     *chan_type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
511     *modifier = IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
512     *type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
513     *direction = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
514     *channel = IIO_EVENT_CODE_EXTRACT_CHAN(event->id);
515     *channel2 = IIO_EVENT_CODE_EXTRACT_CHAN2(event->id);
516     *different = IIO_EVENT_CODE_EXTRACT_DIFF(event->id);
517     return MRAA_SUCCESS;
518 }
519 #if 0
520 // does stop make any sense on iio devices?
521 mraa_result_t
522 mraa_iio_stop(mraa_iio_context dev)
523 {
524     return MRAA_SUCCESS;
525 }
526 #endif