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