366c4ed671039bb5e5b134ad01dd2654cb3a909e
[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->iio_device_count) {
41         return NULL;
42     }
43
44     mraa_iio_get_channel_data(&plat_iio->iio_devices[device]);
45     return &plat_iio->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 int
167 mraa_iio_get_device_num_by_name(const char* name)
168 {
169     int i;
170
171     if (plat_iio == NULL) {
172         syslog(LOG_ERR, "iio: platform IIO structure is not initialized");
173         return -1;
174     }
175
176     if (name == NULL) {
177         syslog(LOG_ERR, "iio: device name is NULL, unable to find its number");
178         return -1;
179     }
180
181     for (i = 0; i < plat_iio->iio_device_count; i++) {
182         struct _iio* device;
183         device = &plat_iio->iio_devices[i];
184         // we want to check for exact match
185         if (strncmp(device->name, name, strlen(device->name)+1) == 0) {
186             return device->num;
187         }
188     }
189
190     return -1;
191 }
192
193 mraa_result_t
194 mraa_iio_read(mraa_iio_context dev, const char* attr_chan, float* data)
195 {
196     char buf[64];
197     snprintf(buf, 64, IIO_SYSFS_DEVICE "%d/%s", dev->num, attr_chan);
198     int fd = open(buf, O_RDONLY);
199     if (fd != -1) {
200         int len = read(fd, &buf, 64);
201         *data = strtol(buf, NULL, 10);
202         return MRAA_SUCCESS;
203     }
204     return MRAA_ERROR_UNSPECIFIED;
205 }
206
207 mraa_result_t
208 mraa_iio_write(mraa_iio_context dev, const char* attr_chan)
209 {
210     return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED;
211 }
212
213 static mraa_result_t
214 mraa_iio_wait_event(int fd, char* data)
215 {
216     struct pollfd pfd;
217
218     if (fd < 0) {
219         return MRAA_ERROR_INVALID_RESOURCE;
220     }
221
222     pfd.fd = fd;
223     pfd.events = POLLIN;
224
225     // Wait for it forever or until pthread_cancel
226     // poll is a cancelable point like sleep()
227     int x = poll(&pfd, 1, -1);
228
229     memset(data, 0, 100);
230     read(fd, data, 100);
231
232     return MRAA_SUCCESS;
233 }
234
235 static void*
236 mraa_iio_trigger_handler(void* arg)
237 {
238     mraa_iio_context dev = (mraa_iio_context) arg;
239     char data[MAX_SIZE*100];
240
241     for (;;) {
242         if (mraa_iio_wait_event(dev->fp, &data[0]) == MRAA_SUCCESS) {
243             pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
244             dev->isr(&data);
245             pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
246         } else {
247             // we must have got an error code so die nicely
248             pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
249             return NULL;
250         }
251     }
252 }
253
254 mraa_result_t
255 mraa_iio_trigger_buffer(mraa_iio_context dev, void (*fptr)(char* data), void* args)
256 {
257     char bu[MAX_SIZE];
258     if (dev->thread_id != 0) {
259         return MRAA_ERROR_NO_RESOURCES;
260     }
261
262     sprintf(bu, IIO_SLASH_DEV "%d", dev->num);
263     dev->fp = open(bu, O_RDONLY | O_NONBLOCK);
264     if (dev->fp == -1) {
265         return MRAA_ERROR_INVALID_RESOURCE;
266     }
267
268     dev->isr = fptr;
269     pthread_create(&dev->thread_id, NULL, mraa_iio_trigger_handler, (void*) dev);
270
271     return MRAA_SUCCESS;
272 }
273
274 #if 0
275 // does stop make any sense on iio devices?
276 mraa_result_t
277 mraa_iio_stop(mraa_iio_context dev)
278 {
279     return MRAA_SUCCESS;
280 }
281 #endif
282