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