+int
+mraa_iio_read_size(mraa_iio_context dev)
+{
+ return dev->datasize;
+}
+
+mraa_iio_channel*
+mraa_iio_get_channels(mraa_iio_context dev)
+{
+ return dev->channels;
+}
+
+int
+mraa_iio_get_channel_count(mraa_iio_context dev)
+{
+ return dev->chan_num;
+}
+
+mraa_result_t
+mraa_iio_get_channel_data(mraa_iio_context dev)
+{
+ const struct dirent *ent;
+ DIR* dir;
+ int chan_num = 0;
+ char buf[MAX_SIZE];
+ char readbuf[32];
+ int fd;
+ int ret = 0;
+ int padint = 0;
+ int curr_bytes = 0;
+ char shortbuf, signchar;
+ memset(buf, 0, MAX_SIZE);
+ snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_SCAN_ELEM, dev->num);
+ dir = opendir(buf);
+ if (dir != NULL) {
+ while ((ent = readdir(dir)) != NULL) {
+ if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), "_en") == 0) {
+ chan_num++;
+ }
+ }
+ }
+ dev->chan_num = chan_num;
+ mraa_iio_channel* chan;
+ dev->channels = calloc(chan_num, sizeof(mraa_iio_channel));
+ seekdir(dir, 0);
+ while ((ent = readdir(dir)) != NULL) {
+ if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_index"), "_index") == 0) {
+ snprintf(buf, MAX_SIZE, IIO_SYSFS_DEVICE "%d/" IIO_SCAN_ELEM "/%s", dev->num, ent->d_name);
+ fd = open(buf, O_RDONLY);
+ if (fd > 0) {
+ if (read(fd, readbuf, 2 * sizeof(char)) != 2) {
+ break;
+ }
+ chan_num = ((int) strtol(readbuf, NULL, 10));
+ chan = &dev->channels[chan_num];
+ chan->index = chan_num;
+ close(fd);
+
+ buf[(strlen(buf)-5)] = '\0';
+ char* str = strdup(buf);
+ snprintf(buf, MAX_SIZE, "%stype", str);
+ fd = open(buf, O_RDONLY);
+ if (fd > 0) {
+ read(fd, readbuf, 31 * sizeof(char));
+ ret = sscanf(readbuf, "%ce:%c%u/%u>>%u", &shortbuf,
+ &signchar, &chan->bits_used,
+ &padint, &chan->shift);
+ chan->bytes = padint / 8;
+ if (curr_bytes % chan->bytes == 0) {
+ chan->location = curr_bytes;
+ } else {
+ chan->location = curr_bytes - curr_bytes%chan->bytes + chan->bytes;
+ }
+ curr_bytes = chan->location + chan->bytes;
+ if (ret < 0) {
+ // probably should be 5?
+ return MRAA_IO_SETUP_FAILURE;
+ }
+ chan->signedd = (signchar == 's');
+ chan->lendian = (shortbuf == 'l');
+ if (chan->bits_used == 64) {
+ chan->mask = ~0;
+ } else {
+ chan->mask = (1 << chan->bits_used) - 1;
+ }
+
+ close(fd);
+ }
+ // todo - read scale & _en attributes
+ }
+ }
+ }
+ dev->datasize = curr_bytes;
+
+ return MRAA_SUCCESS;
+}
+