mraa.c: Moved iio detection code into a function
[contrib/mraa.git] / src / mraa.c
index bdc91bc..01d6d1d 100644 (file)
 #include "version.h"
 
 #define MAX_PLATFORM_NAME_LENGTH 128
+#define IIO_DEVICE_WILDCARD "iio:device*"
 mraa_board_t* plat = NULL;
-// static mraa_board_t* current_plat = NULL;
+mraa_iio_info_t* plat_iio = NULL;
 
 static char platform_name[MAX_PLATFORM_NAME_LENGTH];
-mraa_adv_func_t* advance_func;
 
 static int num_i2c_devices = 0;
 static int num_iio_devices = 0;
@@ -76,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;
 }
@@ -115,9 +114,6 @@ mraa_init()
     PyEval_InitThreads();
 #endif
 
-    advance_func = (mraa_adv_func_t*) malloc(sizeof(mraa_adv_func_t));
-    memset(advance_func, 0, sizeof(mraa_adv_func_t));
-
     mraa_platform_t platform_type;
 #if defined(X86PLAT)
     // Use runtime x86 platform detection
@@ -132,15 +128,15 @@ mraa_init()
     if (plat != NULL)
         plat->platform_type = platform_type;
 
+#if defined(USBPLAT)
     // This is a platform extender so create null base platform if one doesn't already exist
     if (plat == NULL) {
         plat = (mraa_board_t*) calloc(1, sizeof(mraa_board_t));
         if (plat != NULL) {
             plat->platform_type = MRAA_NULL_PLATFORM;
-            plat->platform_name = "Null platform";
+            plat->platform_name = "Unknown platform";
         }
     }
-#if defined(USBPLAT)
     // Now detect sub platform
     if (plat != NULL) {
         mraa_platform_t usb_platform_type = mraa_usb_platform_extender(plat);
@@ -156,33 +152,10 @@ mraa_init()
     }
 #endif
 
-    // Now detect IIO devices, linux only
-    // find how many i2c buses 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;
-        }
-    }
-    char name[64], filepath[64];
-    int fd, len, i;
-    plat->iio_device_count = num_iio_devices;
-    plat->iio_devices = calloc(num_iio_devices, sizeof(struct _iio));
-    struct _iio* device;
-    for (i=0; i < num_iio_devices; i++) {
-        device = &plat->iio_devices[i];
-        device->num = i;
-        snprintf(filepath, 64, "/sys/bus/iio/devices/iio:device%d/name", i);
-        fd = open(filepath, O_RDONLY);
-        if (fd != -1) {
-            len = read(fd, &name, 64);
-            if (len > 1) {
-                // use strndup
-                device->name = malloc((sizeof(char) * len) + sizeof(char));
-                strncpy(device->name, name, len);
-            }
-        }
-    }
-
+    mraa_result_t iio_result = mraa_iio_detect();
+    if (iio_result != MRAA_SUCCESS)
+        return iio_result;
+    
     syslog(LOG_NOTICE, "libmraa initialised for platform '%s' of type %d", mraa_get_platform_name(), mraa_get_platform_type());
     return MRAA_SUCCESS;
 }
@@ -204,6 +177,9 @@ mraa_deinit()
         free(plat);
 
     }
+    if (plat_iio != NULL) {
+        free(plat_iio);
+    }
     closelog();
 }
 
@@ -222,6 +198,46 @@ mraa_set_priority(const unsigned int priority)
     return sched_setscheduler(0, SCHED_RR, &sched_s);
 }
 
+
+mraa_result_t
+mraa_iio_detect()
+{
+    plat_iio = (mraa_iio_info_t*) calloc(1, sizeof(mraa_iio_info_t));
+    plat_iio->iio_device_count = num_iio_devices;    
+    // Now detect IIO devices, linux only
+    // 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;
+        }
+    }
+    char name[64], filepath[64];
+    int fd, len, i;
+    plat_iio->iio_device_count = num_iio_devices;
+    plat_iio->iio_devices = calloc(num_iio_devices, sizeof(struct _iio));
+    struct _iio* device;
+    for (i=0; i < num_iio_devices; i++) {
+        device = &plat_iio->iio_devices[i];
+        device->num = i;
+        snprintf(filepath, 64, "/sys/bus/iio/devices/iio:device%d/name", i);
+        fd = open(filepath, O_RDONLY);
+        if (fd > 0) {
+            len = read(fd, &name, 64);
+            if (len > 1) {
+                // remove any trailing CR/LF symbols
+                name[strcspn(name, "\r\n")] = '\0';
+                len = strlen(name);
+                // use strndup
+                device->name = malloc((sizeof(char) * len) + sizeof(char));
+                strncpy(device->name, name, len+1);
+            }
+            close(fd);
+        }
+    }
+    return MRAA_SUCCESS;
+}
+
+
 mraa_result_t
 mraa_setup_mux_mapped(mraa_pin_t meta)
 {
@@ -767,14 +783,14 @@ mraa_get_sub_platform_index(int pin_or_bus)
 int
 mraa_get_iio_device_count()
 {
-    return plat->iio_device_count;
+    return plat_iio->iio_device_count;
 }
 
 int
 mraa_find_iio_device(const char* devicename)
 {
     int i = 0;
-    for (i; i < plat->iio_device_count; i++) {
+    for (i; i < plat_iio->iio_device_count; i++) {
 #if 0
         // compare with devices array
         if (!strcmp() {