iio: fixed IIO device counting
[contrib/mraa.git] / src / mraa.c
index 9dbffd3..720a7cc 100644 (file)
@@ -46,6 +46,7 @@
 #include "version.h"
 
 #define MAX_PLATFORM_NAME_LENGTH 128
+#define IIO_DEVICE_WILDCARD "iio:device*"
 mraa_board_t* plat = NULL;
 mraa_iio_info_t* plat_iio = NULL;
 
@@ -75,10 +76,9 @@ mraa_set_log_level(int level)
 static int
 mraa_count_iio_devices(const char* path, const struct stat* sb, int flag, struct FTW* ftwb)
 {
-    switch (sb->st_mode & S_IFMT) {
-        case S_IFLNK:
-            num_iio_devices++;
-            break;
+    // we are only interested in files with specific names
+    if (fnmatch(IIO_DEVICE_WILDCARD, basename(path), 0) == 0) {
+        num_iio_devices++;
     }
     return 0;
 }
@@ -154,7 +154,7 @@ mraa_init()
 
     plat_iio = (mraa_iio_info_t*) calloc(1, sizeof(mraa_iio_info_t));
     // Now detect IIO devices, linux only
-    // find how many i2c buses we have if we haven't already
+    // find how many iio devices we have if we haven't already
     if (num_iio_devices == 0) {
         if (nftw("/sys/bus/iio/devices", &mraa_count_iio_devices, 20, FTW_PHYS) == -1) {
             return MRAA_ERROR_UNSPECIFIED;