mraa.c: we now remove trailing CR/LF from IIO dev names
[contrib/mraa.git] / src / mraa.c
1 /*
2  * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3  * Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
4  * Copyright (c) 2014 Intel Corporation.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25
26 #define _GNU_SOURCE
27 #if !defined(_XOPEN_SOURCE) || _XOPEN_SOURCE < 600
28 #define _XOPEN_SOURCE 600 /* Get nftw() and S_IFSOCK declarations */
29 #endif
30
31 #include <stddef.h>
32 #include <stdlib.h>
33 #include <sched.h>
34 #include <string.h>
35 #include <pwd.h>
36 #include <glob.h>
37 #include <ftw.h>
38 #include <dirent.h>
39 #include <sys/stat.h>
40 #include <fcntl.h>
41 #include <string.h>
42 #include <stdio.h>
43
44 #include "mraa_internal.h"
45 #include "gpio.h"
46 #include "version.h"
47
48 #define MAX_PLATFORM_NAME_LENGTH 128
49 #define IIO_DEVICE_WILDCARD "iio:device*"
50 mraa_board_t* plat = NULL;
51 mraa_iio_info_t* plat_iio = NULL;
52
53 static char platform_name[MAX_PLATFORM_NAME_LENGTH];
54
55 static int num_i2c_devices = 0;
56 static int num_iio_devices = 0;
57
58 const char*
59 mraa_get_version()
60 {
61     return gVERSION;
62 }
63
64 mraa_result_t
65 mraa_set_log_level(int level)
66 {
67     if (level <= 7 && level >= 0) {
68         setlogmask(LOG_UPTO(level));
69         syslog(LOG_DEBUG, "Loglevel %d is set", level);
70         return MRAA_SUCCESS;
71     }
72     syslog(LOG_NOTICE, "Invalid loglevel %d requested", level);
73     return MRAA_ERROR_INVALID_PARAMETER;
74 }
75
76 static int
77 mraa_count_iio_devices(const char* path, const struct stat* sb, int flag, struct FTW* ftwb)
78 {
79     // we are only interested in files with specific names
80     if (fnmatch(IIO_DEVICE_WILDCARD, basename(path), 0) == 0) {
81         num_iio_devices++;
82     }
83     return 0;
84 }
85
86 #if (defined SWIGPYTHON) || (defined SWIG)
87 mraa_result_t
88 #else
89 mraa_result_t __attribute__((constructor))
90 #endif
91 mraa_init()
92 {
93     if (plat != NULL) {
94         return MRAA_ERROR_PLATFORM_ALREADY_INITIALISED;
95     }
96
97     uid_t proc_euid = geteuid();
98     struct passwd* proc_user = getpwuid(proc_euid);
99
100 #ifdef DEBUG
101     setlogmask(LOG_UPTO(LOG_DEBUG));
102 #else
103     setlogmask(LOG_UPTO(LOG_NOTICE));
104 #endif
105
106     openlog("libmraa", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
107     syslog(LOG_NOTICE, "libmraa version %s initialised by user '%s' with EUID %d",
108            mraa_get_version(), (proc_user != NULL) ? proc_user->pw_name : "<unknown>", proc_euid);
109
110 #ifdef SWIGPYTHON
111     // Initialise python threads, this allows use to grab the GIL when we are
112     // required to do so
113     Py_InitializeEx(0);
114     PyEval_InitThreads();
115 #endif
116
117     mraa_platform_t platform_type;
118 #if defined(X86PLAT)
119     // Use runtime x86 platform detection
120     platform_type = mraa_x86_platform();
121 #elif defined(ARMPLAT)
122     // Use runtime ARM platform detection
123     platform_type = mraa_arm_platform();
124 #else
125 #error mraa_ARCH NOTHING
126 #endif
127
128     if (plat != NULL)
129         plat->platform_type = platform_type;
130
131 #if defined(USBPLAT)
132     // This is a platform extender so create null base platform if one doesn't already exist
133     if (plat == NULL) {
134         plat = (mraa_board_t*) calloc(1, sizeof(mraa_board_t));
135         if (plat != NULL) {
136             plat->platform_type = MRAA_NULL_PLATFORM;
137             plat->platform_name = "Unknown platform";
138         }
139     }
140     // Now detect sub platform
141     if (plat != NULL) {
142         mraa_platform_t usb_platform_type = mraa_usb_platform_extender(plat);
143         if (plat->platform_type == MRAA_UNKNOWN_PLATFORM && usb_platform_type != MRAA_UNKNOWN_PLATFORM) {
144             plat->platform_type = usb_platform_type;
145         } else {
146             return MRAA_ERROR_PLATFORM_NOT_INITIALISED;
147         }
148     }
149     if (plat == NULL) {
150         printf("mraa: FATAL error, failed to initialise platform\n");
151         return MRAA_ERROR_PLATFORM_NOT_INITIALISED;
152     }
153 #endif
154
155     plat_iio = (mraa_iio_info_t*) calloc(1, sizeof(mraa_iio_info_t));
156     // Now detect IIO devices, linux only
157     // find how many iio devices we have if we haven't already
158     if (num_iio_devices == 0) {
159         if (nftw("/sys/bus/iio/devices", &mraa_count_iio_devices, 20, FTW_PHYS) == -1) {
160             return MRAA_ERROR_UNSPECIFIED;
161         }
162     }
163     char name[64], filepath[64];
164     int fd, len, i;
165     plat_iio->iio_device_count = num_iio_devices;
166     plat_iio->iio_devices = calloc(num_iio_devices, sizeof(struct _iio));
167     struct _iio* device;
168     for (i=0; i < num_iio_devices; i++) {
169         device = &plat_iio->iio_devices[i];
170         device->num = i;
171         snprintf(filepath, 64, "/sys/bus/iio/devices/iio:device%d/name", i);
172         fd = open(filepath, O_RDONLY);
173         if (fd > 0) {
174             len = read(fd, &name, 64);
175             if (len > 1) {
176                 // remove any trailing CR/LF symbols
177                 name[strcspn(name, "\r\n")] = '\0';
178                 len = strlen(name);
179                 // use strndup
180                 device->name = malloc((sizeof(char) * len) + sizeof(char));
181                 strncpy(device->name, name, len+1);
182             }
183             close(fd);
184         }
185     }
186
187     syslog(LOG_NOTICE, "libmraa initialised for platform '%s' of type %d", mraa_get_platform_name(), mraa_get_platform_type());
188     return MRAA_SUCCESS;
189 }
190
191 void
192 mraa_deinit()
193 {
194     if (plat != NULL) {
195         if (plat->pins != NULL) {
196             free(plat->pins);
197         }
198         mraa_board_t* sub_plat = plat->sub_platform;
199         if (sub_plat != NULL) {
200             if (sub_plat->pins != NULL) {
201                 free(sub_plat->pins);
202             }
203             free(sub_plat);
204         }
205         free(plat);
206
207     }
208     if (plat_iio != NULL) {
209         free(plat_iio);
210     }
211     closelog();
212 }
213
214 int
215 mraa_set_priority(const unsigned int priority)
216 {
217     struct sched_param sched_s;
218
219     memset(&sched_s, 0, sizeof(struct sched_param));
220     if (priority > sched_get_priority_max(SCHED_RR)) {
221         sched_s.sched_priority = sched_get_priority_max(SCHED_RR);
222     } else {
223         sched_s.sched_priority = priority;
224     }
225
226     return sched_setscheduler(0, SCHED_RR, &sched_s);
227 }
228
229 mraa_result_t
230 mraa_setup_mux_mapped(mraa_pin_t meta)
231 {
232     int mi;
233
234     for (mi = 0; mi < meta.mux_total; mi++) {
235         mraa_gpio_context mux_i;
236         mux_i = mraa_gpio_init_raw(meta.mux[mi].pin);
237         if (mux_i == NULL) {
238             return MRAA_ERROR_INVALID_HANDLE;
239         }
240         // this function will sometimes fail, however this is not critical as
241         // long as the write succeeds - Test case galileo gen2 pin2
242         mraa_gpio_dir(mux_i, MRAA_GPIO_OUT);
243         mraa_gpio_owner(mux_i, 0);
244
245         if (mraa_gpio_write(mux_i, meta.mux[mi].value) != MRAA_SUCCESS) {
246             mraa_gpio_close(mux_i);
247             return MRAA_ERROR_INVALID_RESOURCE;
248         }
249         mraa_gpio_close(mux_i);
250     }
251
252     return MRAA_SUCCESS;
253 }
254
255 void
256 mraa_result_print(mraa_result_t result)
257 {
258     switch (result) {
259         case MRAA_SUCCESS:
260             fprintf(stdout, "MRAA: SUCCESS\n");
261             break;
262         case MRAA_ERROR_FEATURE_NOT_IMPLEMENTED:
263             fprintf(stdout, "MRAA: Feature not implemented.\n");
264             break;
265         case MRAA_ERROR_FEATURE_NOT_SUPPORTED:
266             fprintf(stdout, "MRAA: Feature not supported by Hardware.\n");
267             break;
268         case MRAA_ERROR_INVALID_VERBOSITY_LEVEL:
269             fprintf(stdout, "MRAA: Invalid verbosity level.\n");
270             break;
271         case MRAA_ERROR_INVALID_PARAMETER:
272             fprintf(stdout, "MRAA: Invalid parameter.\n");
273             break;
274         case MRAA_ERROR_INVALID_HANDLE:
275             fprintf(stdout, "MRAA: Invalid Handle.\n");
276             break;
277         case MRAA_ERROR_NO_RESOURCES:
278             fprintf(stdout, "MRAA: No resources.\n");
279             break;
280         case MRAA_ERROR_INVALID_RESOURCE:
281             fprintf(stdout, "MRAA: Invalid resource.\n");
282             break;
283         case MRAA_ERROR_INVALID_QUEUE_TYPE:
284             fprintf(stdout, "MRAA: Invalid Queue Type.\n");
285             break;
286         case MRAA_ERROR_NO_DATA_AVAILABLE:
287             fprintf(stdout, "MRAA: No Data available.\n");
288             break;
289         case MRAA_ERROR_INVALID_PLATFORM:
290             fprintf(stdout, "MRAA: Platform not recognised.\n");
291             break;
292         case MRAA_ERROR_PLATFORM_NOT_INITIALISED:
293             fprintf(stdout, "MRAA: Platform not initialised.\n");
294             break;
295         case MRAA_ERROR_PLATFORM_ALREADY_INITIALISED:
296             fprintf(stdout, "MRAA: Platform already initialised.\n");
297             break;
298         case MRAA_ERROR_UNSPECIFIED:
299             fprintf(stdout, "MRAA: Unspecified Error.\n");
300             break;
301         default:
302             fprintf(stdout, "MRAA: Unrecognised error.\n");
303             break;
304     }
305 }
306
307
308 mraa_boolean_t
309 mraa_has_sub_platform()
310 {
311     return (plat != NULL) && (plat->sub_platform != NULL);
312 }
313
314 mraa_boolean_t
315 mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
316 {
317     if (plat == NULL)
318         return 0;
319
320     mraa_board_t* current_plat = plat;
321     if (mraa_is_sub_platform_id(pin)) {
322         current_plat = plat->sub_platform;
323         if (current_plat == NULL) {
324             syslog(LOG_ERR, "mraa_pin_mode_test: Sub platform Not Initialised");
325             return 0;
326         }
327         pin = mraa_get_sub_platform_index(pin);
328     }
329
330     if (current_plat == NULL || current_plat->platform_type == MRAA_UNKNOWN_PLATFORM) {
331         return 0;
332     }
333     if (pin > (current_plat->phy_pin_count - 1) || pin < 0)
334         return 0;
335
336     switch (mode) {
337         case MRAA_PIN_VALID:
338             if (current_plat->pins[pin].capabilites.valid == 1)
339                 return 1;
340             break;
341         case MRAA_PIN_GPIO:
342             if (current_plat->pins[pin].capabilites.gpio == 1)
343                 return 1;
344             break;
345         case MRAA_PIN_PWM:
346             if (current_plat->pins[pin].capabilites.pwm == 1)
347                 return 1;
348             break;
349         case MRAA_PIN_FAST_GPIO:
350             if (current_plat->pins[pin].capabilites.fast_gpio == 1)
351                 return 1;
352             break;
353         case MRAA_PIN_SPI:
354             if (current_plat->pins[pin].capabilites.spi == 1)
355                 return 1;
356             break;
357         case MRAA_PIN_I2C:
358             if (current_plat->pins[pin].capabilites.i2c == 1)
359                 return 1;
360             break;
361         case MRAA_PIN_AIO:
362             if (current_plat->pins[pin].capabilites.aio == 1)
363                 return 1;
364             break;
365         case MRAA_PIN_UART:
366             if (current_plat->pins[pin].capabilites.uart == 1)
367                 return 1;
368             break;
369         default:
370             syslog(LOG_NOTICE, "requested pinmode invalid");
371             break;
372     }
373     return 0;
374 }
375
376 mraa_platform_t
377 mraa_get_platform_type()
378 {
379     if (plat == NULL)
380         return MRAA_UNKNOWN_PLATFORM;
381     return plat->platform_type;
382 }
383
384 int
385 mraa_get_platform_combined_type()
386 {
387     int type = mraa_get_platform_type();
388     int sub_type = mraa_has_sub_platform() ? plat->sub_platform->platform_type : MRAA_UNKNOWN_PLATFORM;
389     return type | (sub_type << 8);
390 }
391
392 unsigned int
393 mraa_adc_raw_bits()
394 {
395     if (plat == NULL)
396         return 0;
397
398     if (plat->aio_count == 0)
399         return 0;
400
401     return plat->adc_raw;
402 }
403
404 unsigned int
405 mraa_get_platform_adc_raw_bits(uint8_t platform_offset)
406 {
407     if (platform_offset == MRAA_MAIN_PLATFORM_OFFSET)
408         return mraa_adc_raw_bits();
409     else {
410         if (!mraa_has_sub_platform())
411             return 0;
412
413         if (plat->sub_platform->aio_count == 0)
414             return 0;
415
416         return plat->sub_platform->adc_raw;
417     }
418 }
419
420
421 unsigned int
422 mraa_adc_supported_bits()
423 {
424     if (plat == NULL)
425         return 0;
426
427     if (plat->aio_count == 0)
428         return 0;
429
430     return plat->adc_supported;
431 }
432
433 unsigned int
434 mraa_get_platform_adc_supported_bits(int platform_offset)
435 {
436     if (platform_offset == MRAA_MAIN_PLATFORM_OFFSET)
437         return mraa_adc_supported_bits();
438     else {
439         if (!mraa_has_sub_platform())
440             return 0;
441
442         if (plat->sub_platform->aio_count == 0)
443             return 0;
444
445         return plat->sub_platform->adc_supported;
446     }
447 }
448
449
450 char*
451 mraa_get_platform_name()
452 {
453     if (plat == NULL) {
454         return NULL;
455     }
456     if (mraa_has_sub_platform()) {
457         snprintf(platform_name, MAX_PLATFORM_NAME_LENGTH, "%s + %s", plat->platform_name, plat->sub_platform->platform_name);
458     } else {
459         strncpy(platform_name, plat->platform_name, MAX_PLATFORM_NAME_LENGTH-1);
460     }
461
462     return platform_name;
463 }
464
465 int
466 mraa_get_i2c_bus_count()
467 {
468     if (plat == NULL) {
469         return -1;
470     }
471     return plat->i2c_bus_count;
472 }
473
474 int
475 mraa_get_i2c_bus_id(unsigned i2c_bus)
476 {
477     if (plat == NULL) {
478         return -1;
479     }
480
481     if (i2c_bus >= plat->i2c_bus_count) {
482         return -1;
483     }
484
485     return plat->i2c_bus[i2c_bus].bus_id;
486 }
487
488 unsigned int
489 mraa_get_pin_count()
490 {
491     if (plat == NULL) {
492         return 0;
493     }
494     return plat->phy_pin_count;
495 }
496
497 unsigned int
498 mraa_get_platform_pin_count(uint8_t platform_offset)
499 {
500     if (platform_offset == MRAA_MAIN_PLATFORM_OFFSET)
501         return mraa_get_pin_count();
502     else {
503         if (mraa_has_sub_platform())
504            return plat->sub_platform->phy_pin_count;
505         else
506            return 0;
507     }
508 }
509
510
511 char*
512 mraa_get_pin_name(int pin)
513 {
514     if (plat == NULL)
515         return 0;
516
517     mraa_board_t* current_plat = plat;
518     if (mraa_is_sub_platform_id(pin)) {
519         current_plat = plat->sub_platform;
520         if (current_plat == NULL) {
521             syslog(LOG_ERR, "mraa_get_pin_name: Sub platform Not Initialised");
522             return 0;
523         }
524         pin = mraa_get_sub_platform_index(pin);
525     }
526
527     if (pin > (current_plat->phy_pin_count - 1) || pin < 0)
528         return NULL;
529     return (char*) current_plat->pins[pin].name;
530 }
531
532 int
533 mraa_get_default_i2c_bus(uint8_t platform_offset)
534 {
535     if (plat == NULL)
536         return -1;
537     if (platform_offset == MRAA_MAIN_PLATFORM_OFFSET) {
538         return plat->def_i2c_bus;
539     } else {
540         if (mraa_has_sub_platform())
541            return plat->sub_platform->def_i2c_bus;
542         else
543            return -1;
544     }
545 }
546
547
548 mraa_boolean_t
549 mraa_file_exist(const char* filename)
550 {
551     glob_t results;
552     results.gl_pathc = 0;
553     glob(filename, 0, NULL, &results);
554     int file_found = results.gl_pathc == 1;
555     globfree(&results);
556     return file_found;
557 }
558
559 mraa_boolean_t
560 mraa_file_contains(const char* filename, const char* content)
561 {
562     mraa_boolean_t found = 0;
563     if ((filename == NULL) || (content == NULL)) {
564         return 0;
565     }
566
567     char* file = mraa_file_unglob(filename);
568     if (file != NULL) {
569         size_t len = 1024;
570         char* line = malloc(len);
571         if (line == NULL) {
572             free(file);
573             return 0;
574         }
575         FILE* fh = fopen(file, "r");
576         if (fh == NULL) {
577             free(file);
578             free(line);
579             return 0;
580         }
581         while ((getline(&line, &len, fh) != -1) && (found == 0)) {
582             if (strstr(line, content)) {
583                 found = 1;
584                 break;
585             }
586         }
587         fclose(fh);
588         free(file);
589         free(line);
590     }
591     return found;
592 }
593
594 mraa_boolean_t
595 mraa_file_contains_both(const char* filename, const char* content, const char* content2)
596 {
597     mraa_boolean_t found = 0;
598     if ((filename == NULL) || (content == NULL)) {
599         return 0;
600     }
601
602     char* file = mraa_file_unglob(filename);
603     if (file != NULL) {
604         size_t len = 1024;
605         char* line = malloc(len);
606         if (line == NULL) {
607             free(file);
608             return 0;
609         }
610         FILE* fh = fopen(file, "r");
611         if (fh == NULL) {
612             free(file);
613             free(line);
614             return 0;
615         }
616         while ((getline(&line, &len, fh) != -1) && (found == 0)) {
617             if (strstr(line, content) && strstr(line, content2)) {
618                 found = 1;
619                 break;
620             }
621         }
622         fclose(fh);
623         free(file);
624         free(line);
625     }
626     return found;
627 }
628
629 char*
630 mraa_file_unglob(const char* filename)
631 {
632     glob_t results;
633     char* res = NULL;
634     results.gl_pathc = 0;
635     glob(filename, 0, NULL, &results);
636     if (results.gl_pathc == 1)
637         res = strdup(results.gl_pathv[0]);
638     globfree(&results);
639     return res;
640 }
641
642 mraa_boolean_t
643 mraa_link_targets(const char* filename, const char* targetname)
644 {
645     int size = 100;
646     int nchars = 0;
647     char* buffer = NULL;
648     while (nchars == 0) {
649         buffer = (char*) realloc(buffer, size);
650         if (buffer == NULL)
651             return 0;
652         nchars = readlink(filename, buffer, size);
653         if (nchars < 0) {
654             free(buffer);
655             return 0;
656         } else {
657             buffer[nchars] = '\0';
658         }
659         if (nchars >= size) {
660             size *= 2;
661             nchars = 0;
662         }
663     }
664     if (strstr(buffer, targetname)) {
665         free(buffer);
666         return 1;
667     } else {
668         free(buffer);
669         return 0;
670     }
671 }
672
673 static int
674 mraa_count_i2c_files(const char* path, const struct stat* sb, int flag, struct FTW* ftwb)
675 {
676     switch (sb->st_mode & S_IFMT) {
677         case S_IFLNK:
678             num_i2c_devices++;
679             break;
680     }
681     return 0;
682 }
683
684 int
685 mraa_find_i2c_bus(const char* devname, int startfrom)
686 {
687     char path[64];
688     int fd;
689     int i = startfrom;
690     int ret = -1;
691
692     // because feeding mraa_find_i2c_bus result back into the function is
693     // useful treat -1 as 0
694     if (startfrom < 0) {
695         startfrom = 0;
696     }
697
698     // find how many i2c buses we have if we haven't already
699     if (num_i2c_devices == 0) {
700         if (nftw("/sys/class/i2c-dev/", &mraa_count_i2c_files, 20, FTW_PHYS) == -1) {
701             return -1;
702         }
703     }
704
705     // i2c devices are numbered numerically so 0 must exist otherwise there is
706     // no i2c-dev loaded
707     if (mraa_file_exist("/sys/class/i2c-dev/i2c-0")) {
708         for (i; i < num_i2c_devices; i++) {
709             off_t size, err;
710             snprintf(path, 64, "/sys/class/i2c-dev/i2c-%u/name", i);
711             fd = open(path, O_RDONLY);
712             if (fd < 0) {
713                 break;
714             }
715             size = lseek(fd, 0, SEEK_END);
716             if (size < 0) {
717                 syslog(LOG_WARNING, "mraa: failed to seek i2c filename file");
718                 close(fd);
719                 break;
720             }
721             err = lseek(fd, 0, SEEK_SET);
722             if (err < 0) {
723                 syslog(LOG_WARNING, "mraa: failed to seek i2c filename file");
724                 close(fd);
725                 break;
726             }
727             char* value = malloc(size);
728             if (value == NULL) {
729                 syslog(LOG_ERR, "mraa: failed to allocate memory for i2c file");
730                 close(fd);
731                 break;
732             }
733             ssize_t r = read(fd, value, size);
734             if (r > 0) {
735                 if (strcasestr(value, devname) != NULL) {
736                     free(value);
737                     close(fd);
738                     return i;
739                 }
740             } else {
741                 syslog(LOG_ERR, "mraa: sysfs i2cdev failed");
742             }
743             free(value);
744             close(fd);
745         }
746     } else {
747         syslog(LOG_WARNING, "mraa: no i2c-dev detected, load i2c-dev");
748     }
749
750     return ret;
751 }
752
753 mraa_boolean_t
754 mraa_is_sub_platform_id(int pin_or_bus)
755 {
756     return (pin_or_bus & MRAA_SUB_PLATFORM_MASK) != 0;
757 }
758
759 int
760 mraa_get_sub_platform_id(int pin_or_bus)
761 {
762     return pin_or_bus | MRAA_SUB_PLATFORM_MASK;
763 }
764
765 int
766 mraa_get_sub_platform_index(int pin_or_bus)
767 {
768     return pin_or_bus & (~MRAA_SUB_PLATFORM_MASK);
769 }
770
771 int
772 mraa_get_iio_device_count()
773 {
774     return plat_iio->iio_device_count;
775 }
776
777 int
778 mraa_find_iio_device(const char* devicename)
779 {
780     int i = 0;
781     for (i; i < plat_iio->iio_device_count; i++) {
782 #if 0
783         // compare with devices array
784         if (!strcmp() {
785         }
786 #endif
787     }
788     return 0;
789 }