dcf80d43252f891d69eed87ab18b6f7fa15c5154
[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
31 #define MAX_SIZE 128
32 #define IIO_DEVICE "iio:device"
33 #define IIO_SCAN_ELEM "scan_elements"
34 #define IIO_SLASH_DEV "/dev/"IIO_DEVICE
35 #define IIO_SYSFS_DEVICE "/sys/bus/iio/devices/"IIO_DEVICE
36
37 mraa_iio_context
38 mraa_iio_init(int device)
39 {
40     if (device > plat->iio_device_count) {
41         return NULL;
42     }
43
44     mraa_iio_get_channel_data(&plat->iio_devices[device]);
45     return &plat->iio_devices[device];
46 }
47
48 int
49 mraa_iio_read_size(mraa_iio_context dev)
50 {
51     return dev->datasize;
52 }
53
54 mraa_iio_channel*
55 mraa_iio_get_channels(mraa_iio_context dev)
56 {
57     return dev->channels;
58 }
59
60 int
61 mraa_iio_get_channel_count(mraa_iio_context dev)
62 {
63     return dev->chan_num;
64 }
65
66 mraa_result_t
67 mraa_iio_get_channel_data(mraa_iio_context dev)
68 {
69     const struct dirent *ent;
70     DIR* dir;
71     int chan_num = 0;
72     char buf[MAX_SIZE];
73     char readbuf[32];
74     int fd;
75     int ret = 0;
76     int padint = 0;
77     int curr_bytes = 0;
78     char shortbuf, signchar;
79     memset(buf, 0, MAX_SIZE);
80     snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_SCAN_ELEM, dev->num);
81     dir = opendir(buf);
82     if (dir != NULL) {
83         while ((ent = readdir(dir)) != NULL) {
84             if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), "_en") == 0) {
85                     chan_num++;
86             }
87         }
88     }
89     dev->chan_num = chan_num;
90     mraa_iio_channel* chan;
91     dev->channels = calloc(chan_num, sizeof(mraa_iio_channel));
92     seekdir(dir, 0);
93     while ((ent = readdir(dir)) != NULL) {
94         if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_index"), "_index") == 0) {
95             snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_SCAN_ELEM "/%s", dev->num, ent->d_name);
96             fd = open(buf, O_RDONLY);
97             if (fd > 0) {
98                 if (read(fd, readbuf, 2 * sizeof(char)) != 2) {
99                     break;
100                 }
101                 chan_num = ((int) strtol(readbuf, NULL, 10));
102                 chan = &dev->channels[chan_num];
103                 chan->index = chan_num;
104                 close(fd);
105
106                 buf[(strlen(buf)-5)] = '\0';
107                 char* str = strdup(buf);
108                 // grab the type of the buffer
109                 snprintf(buf, MAX_SIZE, "%stype", str);
110                 fd = open(buf, O_RDONLY);
111                 if (fd > 0) {
112                     read(fd, readbuf, 31 * sizeof(char));
113                     ret = sscanf(readbuf, "%ce:%c%u/%u>>%u", &shortbuf,
114                                     &signchar, &chan->bits_used,
115                                     &padint, &chan->shift);
116                     chan->bytes = padint / 8;
117                     if (curr_bytes % chan->bytes == 0) {
118                         chan->location = curr_bytes;
119                     } else {
120                         chan->location = curr_bytes - curr_bytes%chan->bytes + chan->bytes;
121                     }
122                     curr_bytes = chan->location + chan->bytes;
123                     // probably should be 5?
124                     if (ret < 0) {
125                         // cleanup
126                         free(str);
127                         close(fd);
128                         return MRAA_IO_SETUP_FAILURE;
129                     }
130                     chan->signedd = (signchar == 's');
131                     chan->lendian = (shortbuf == 'l');
132                     if (chan->bits_used == 64) {
133                         chan->mask = ~0;
134                     } else {
135                         chan->mask = (1 << chan->bits_used) - 1;
136                     }
137                     close(fd);
138                 }
139                 // grab the enable flag of channel
140                 snprintf(buf, MAX_SIZE, "%sen", str);
141                 fd = open(buf, O_RDONLY);
142                 if (fd > 0) {
143                     if (read(fd, readbuf, 2 * sizeof(char)) != 2) {
144                         syslog(LOG_ERR, "iio: Failed to read a sensible value from sysfs");
145                         return -1;
146                     }
147                     chan->enabled = (int) strtol(readbuf, NULL, 10);
148                     close(fd);
149                 }
150                 // clean up str var
151                 free(str);
152             }
153         }
154     }
155     dev->datasize = curr_bytes;
156
157     return MRAA_SUCCESS;
158 }
159
160 const char*
161 mraa_iio_get_device_name(mraa_iio_context dev)
162 {
163     return dev->name;
164 }
165
166 mraa_result_t
167 mraa_iio_read(mraa_iio_context dev, const char* attr_chan, float* data)
168 {
169     char buf[64];
170     snprintf(buf, 64, IIO_SYSFS_DEVICE "%d/%s", dev->num, attr_chan);
171     int fd = open(buf, O_RDONLY);
172     if (fd != -1) {
173         int len = read(fd, &buf, 64);
174         *data = strtol(buf, NULL, 10);
175         return MRAA_SUCCESS;
176     }
177     return MRAA_ERROR_UNSPECIFIED;
178 }
179
180 mraa_result_t
181 mraa_iio_write(mraa_iio_context dev, const char* attr_chan)
182 {
183     return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED;
184 }
185
186 static mraa_result_t
187 mraa_iio_wait_event(int fd, char* data)
188 {
189     struct pollfd pfd;
190
191     if (fd < 0) {
192         return MRAA_ERROR_INVALID_RESOURCE;
193     }
194
195     pfd.fd = fd;
196     pfd.events = POLLIN;
197
198     // Wait for it forever or until pthread_cancel
199     // poll is a cancelable point like sleep()
200     int x = poll(&pfd, 1, -1);
201
202     memset(data, 0, 100);
203     read(fd, data, 100);
204
205     return MRAA_SUCCESS;
206 }
207
208 static void*
209 mraa_iio_trigger_handler(void* arg)
210 {
211     mraa_iio_context dev = (mraa_iio_context) arg;
212     char data[MAX_SIZE*100];
213
214     for (;;) {
215         if (mraa_iio_wait_event(dev->fp, &data[0]) == MRAA_SUCCESS) {
216             pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
217             dev->isr(&data);
218             pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
219         } else {
220             // we must have got an error code so die nicely
221             pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
222             return NULL;
223         }
224     }
225 }
226
227 mraa_result_t
228 mraa_iio_trigger_buffer(mraa_iio_context dev, void (*fptr)(char* data), void* args)
229 {
230     char bu[MAX_SIZE];
231     if (dev->thread_id != 0) {
232         return MRAA_ERROR_NO_RESOURCES;
233     }
234
235     sprintf(bu, IIO_SLASH_DEV "%d", dev->num);
236     dev->fp = open(bu, O_RDONLY | O_NONBLOCK);
237     if (dev->fp == -1) {
238         return MRAA_ERROR_INVALID_RESOURCE;
239     }
240
241     dev->isr = fptr;
242     pthread_create(&dev->thread_id, NULL, mraa_iio_trigger_handler, (void*) dev);
243
244     return MRAA_SUCCESS;
245 }
246
247 #if 0
248 // does stop make any sense on iio devices?
249 mraa_result_t
250 mraa_iio_stop(mraa_iio_context dev)
251 {
252     return MRAA_SUCCESS;
253 }
254 #endif
255