mraa.c: iio detect tidy up.
[contrib/mraa.git] / src / mraa.c
index 5a1772f..28a2c4a 100644 (file)
 #include "gpio.h"
 #include "version.h"
 
+#define MAX_PLATFORM_NAME_LENGTH 128
+#define IIO_DEVICE_WILDCARD "iio:device*"
 mraa_board_t* plat = NULL;
-static mraa_platform_t platform_type = MRAA_UNKNOWN_PLATFORM;
-mraa_adv_func_t* advance_func;
+mraa_iio_info_t* plat_iio = NULL;
+
+static char platform_name[MAX_PLATFORM_NAME_LENGTH];
+
+static int num_i2c_devices = 0;
+static int num_iio_devices = 0;
 
 const char*
 mraa_get_version()
@@ -67,6 +73,7 @@ mraa_set_log_level(int level)
     return MRAA_ERROR_INVALID_PARAMETER;
 }
 
+
 #if (defined SWIGPYTHON) || (defined SWIG)
 mraa_result_t
 #else
@@ -97,9 +104,8 @@ mraa_init()
     Py_InitializeEx(0);
     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
     platform_type = mraa_x86_platform();
@@ -110,12 +116,38 @@ mraa_init()
 #error mraa_ARCH NOTHING
 #endif
 
+    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 = "Unknown platform";
+        }
+    }
+    // Now detect sub platform
+    if (plat != NULL) {
+        mraa_platform_t usb_platform_type = mraa_usb_platform_extender(plat);
+        if (plat->platform_type == MRAA_UNKNOWN_PLATFORM && usb_platform_type != MRAA_UNKNOWN_PLATFORM) {
+            plat->platform_type = usb_platform_type;
+        } else {
+            return MRAA_ERROR_PLATFORM_NOT_INITIALISED;
+        }
+    }
     if (plat == NULL) {
         printf("mraa: FATAL error, failed to initialise platform\n");
         return MRAA_ERROR_PLATFORM_NOT_INITIALISED;
     }
+#endif
+
+    mraa_result_t iio_result = mraa_iio_detect();
+    if (iio_result != MRAA_SUCCESS)
+        return iio_result;
 
-    syslog(LOG_INFO, "libmraa initialised for platform '%s' of type %d", mraa_get_platform_name(), platform_type);
+    syslog(LOG_NOTICE, "libmraa initialised for platform '%s' of type %d", mraa_get_platform_name(), mraa_get_platform_type());
     return MRAA_SUCCESS;
 }
 
@@ -126,7 +158,18 @@ mraa_deinit()
         if (plat->pins != NULL) {
             free(plat->pins);
         }
+        mraa_board_t* sub_plat = plat->sub_platform;
+        if (sub_plat != NULL) {
+            if (sub_plat->pins != NULL) {
+                free(sub_plat->pins);
+            }
+            free(sub_plat);
+        }
         free(plat);
+
+    }
+    if (plat_iio != NULL) {
+        free(plat_iio);
     }
     closelog();
 }
@@ -146,6 +189,55 @@ mraa_set_priority(const unsigned int priority)
     return sched_setscheduler(0, SCHED_RR, &sched_s);
 }
 
+static int
+mraa_count_iio_devices(const char* path, const struct stat* sb, int flag, struct FTW* ftwb)
+{
+    // we are only interested in files with specific names
+    if (fnmatch(IIO_DEVICE_WILDCARD, basename(path), 0) == 0) {
+        num_iio_devices++;
+    }
+    return 0;
+}
+
+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)
 {
@@ -224,46 +316,66 @@ mraa_result_print(mraa_result_t result)
     }
 }
 
+
+mraa_boolean_t
+mraa_has_sub_platform()
+{
+    return (plat != NULL) && (plat->sub_platform != NULL);
+}
+
 mraa_boolean_t
 mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
 {
-    if (plat == NULL) {
+    if (plat == NULL)
+        return 0;
+
+    mraa_board_t* current_plat = plat;
+    if (mraa_is_sub_platform_id(pin)) {
+        current_plat = plat->sub_platform;
+        if (current_plat == NULL) {
+            syslog(LOG_ERR, "mraa_pin_mode_test: Sub platform Not Initialised");
+            return 0;
+        }
+        pin = mraa_get_sub_platform_index(pin);
+    }
+
+    if (current_plat == NULL || current_plat->platform_type == MRAA_UNKNOWN_PLATFORM) {
         return 0;
     }
-    if (pin > (plat->phy_pin_count - 1) || pin < 0)
+    if (pin > (current_plat->phy_pin_count - 1) || pin < 0)
         return 0;
 
     switch (mode) {
         case MRAA_PIN_VALID:
-            if (plat->pins[pin].capabilites.valid == 1)
+            if (current_plat->pins[pin].capabilites.valid == 1)
                 return 1;
             break;
         case MRAA_PIN_GPIO:
-            if (plat->pins[pin].capabilites.gpio == 1)
+            if (current_plat->pins[pin].capabilites.gpio == 1)
                 return 1;
             break;
         case MRAA_PIN_PWM:
-            if (plat->pins[pin].capabilites.pwm == 1)
+            if (current_plat->pins[pin].capabilites.pwm == 1)
                 return 1;
             break;
         case MRAA_PIN_FAST_GPIO:
-            if (plat->pins[pin].capabilites.fast_gpio == 1)
+            if (current_plat->pins[pin].capabilites.fast_gpio == 1)
                 return 1;
             break;
         case MRAA_PIN_SPI:
-            if (plat->pins[pin].capabilites.spi == 1)
+            if (current_plat->pins[pin].capabilites.spi == 1)
                 return 1;
             break;
         case MRAA_PIN_I2C:
-            if (plat->pins[pin].capabilites.i2c == 1)
+            if (current_plat->pins[pin].capabilites.i2c == 1)
                 return 1;
             break;
         case MRAA_PIN_AIO:
-            if (plat->pins[pin].capabilites.aio == 1)
+            if (current_plat->pins[pin].capabilites.aio == 1)
                 return 1;
             break;
         case MRAA_PIN_UART:
-            if (plat->pins[pin].capabilites.uart == 1)
+            if (current_plat->pins[pin].capabilites.uart == 1)
                 return 1;
             break;
         default:
@@ -276,7 +388,17 @@ mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
 mraa_platform_t
 mraa_get_platform_type()
 {
-    return platform_type;
+    if (plat == NULL)
+        return MRAA_UNKNOWN_PLATFORM;
+    return plat->platform_type;
+}
+
+int
+mraa_get_platform_combined_type()
+{
+    int type = mraa_get_platform_type();
+    int sub_type = mraa_has_sub_platform() ? plat->sub_platform->platform_type : MRAA_UNKNOWN_PLATFORM;
+    return type | (sub_type << 8);
 }
 
 unsigned int
@@ -292,6 +414,23 @@ mraa_adc_raw_bits()
 }
 
 unsigned int
+mraa_get_platform_adc_raw_bits(uint8_t platform_offset)
+{
+    if (platform_offset == MRAA_MAIN_PLATFORM_OFFSET)
+        return mraa_adc_raw_bits();
+    else {
+        if (!mraa_has_sub_platform())
+            return 0;
+
+        if (plat->sub_platform->aio_count == 0)
+            return 0;
+
+        return plat->sub_platform->adc_raw;
+    }
+}
+
+
+unsigned int
 mraa_adc_supported_bits()
 {
     if (plat == NULL)
@@ -303,13 +442,59 @@ mraa_adc_supported_bits()
     return plat->adc_supported;
 }
 
+unsigned int
+mraa_get_platform_adc_supported_bits(int platform_offset)
+{
+    if (platform_offset == MRAA_MAIN_PLATFORM_OFFSET)
+        return mraa_adc_supported_bits();
+    else {
+        if (!mraa_has_sub_platform())
+            return 0;
+
+        if (plat->sub_platform->aio_count == 0)
+            return 0;
+
+        return plat->sub_platform->adc_supported;
+    }
+}
+
+
 char*
 mraa_get_platform_name()
 {
     if (plat == NULL) {
         return NULL;
     }
-    return (char*) plat->platform_name;
+    if (mraa_has_sub_platform()) {
+        snprintf(platform_name, MAX_PLATFORM_NAME_LENGTH, "%s + %s", plat->platform_name, plat->sub_platform->platform_name);
+    } else {
+        strncpy(platform_name, plat->platform_name, MAX_PLATFORM_NAME_LENGTH-1);
+    }
+
+    return platform_name;
+}
+
+int
+mraa_get_i2c_bus_count()
+{
+    if (plat == NULL) {
+        return -1;
+    }
+    return plat->i2c_bus_count;
+}
+
+int
+mraa_get_i2c_bus_id(unsigned i2c_bus)
+{
+    if (plat == NULL) {
+        return -1;
+    }
+
+    if (i2c_bus >= plat->i2c_bus_count) {
+        return -1;
+    }
+
+    return plat->i2c_bus[i2c_bus].bus_id;
 }
 
 unsigned int
@@ -321,17 +506,57 @@ mraa_get_pin_count()
     return plat->phy_pin_count;
 }
 
+unsigned int
+mraa_get_platform_pin_count(uint8_t platform_offset)
+{
+    if (platform_offset == MRAA_MAIN_PLATFORM_OFFSET)
+        return mraa_get_pin_count();
+    else {
+        if (mraa_has_sub_platform())
+           return plat->sub_platform->phy_pin_count;
+        else
+           return 0;
+    }
+}
+
+
 char*
 mraa_get_pin_name(int pin)
 {
-    if (plat == NULL) {
-        return NULL;
+    if (plat == NULL)
+        return 0;
+
+    mraa_board_t* current_plat = plat;
+    if (mraa_is_sub_platform_id(pin)) {
+        current_plat = plat->sub_platform;
+        if (current_plat == NULL) {
+            syslog(LOG_ERR, "mraa_get_pin_name: Sub platform Not Initialised");
+            return 0;
+        }
+        pin = mraa_get_sub_platform_index(pin);
     }
-    if (pin > (plat->phy_pin_count - 1) || pin < 0)
+
+    if (pin > (current_plat->phy_pin_count - 1) || pin < 0)
         return NULL;
-    return (char*) plat->pins[pin].name;
+    return (char*) current_plat->pins[pin].name;
 }
 
+int
+mraa_get_default_i2c_bus(uint8_t platform_offset)
+{
+    if (plat == NULL)
+        return -1;
+    if (platform_offset == MRAA_MAIN_PLATFORM_OFFSET) {
+        return plat->def_i2c_bus;
+    } else {
+        if (mraa_has_sub_platform())
+           return plat->sub_platform->def_i2c_bus;
+        else
+           return -1;
+    }
+}
+
+
 mraa_boolean_t
 mraa_file_exist(const char* filename)
 {
@@ -356,12 +581,12 @@ mraa_file_contains(const char* filename, const char* content)
         size_t len = 1024;
         char* line = malloc(len);
         if (line == NULL) {
-            close(file);
+            free(file);
             return 0;
         }
         FILE* fh = fopen(file, "r");
         if (fh == NULL) {
-            close(file);
+            free(file);
             free(line);
             return 0;
         }
@@ -391,12 +616,12 @@ mraa_file_contains_both(const char* filename, const char* content, const char* c
         size_t len = 1024;
         char* line = malloc(len);
         if (line == NULL) {
-            close(file);
+            free(file);
             return 0;
         }
         FILE* fh = fopen(file, "r");
         if (fh == NULL) {
-            close(file);
+            free(file);
             free(line);
             return 0;
         }
@@ -457,10 +682,8 @@ mraa_link_targets(const char* filename, const char* targetname)
     }
 }
 
-static int num_i2c_devices = 0;
-
 static int
-mraa_count_files(const char* path, const struct stat* sb, int flag, struct FTW* ftwb)
+mraa_count_i2c_files(const char* path, const struct stat* sb, int flag, struct FTW* ftwb)
 {
     switch (sb->st_mode & S_IFMT) {
         case S_IFLNK:
@@ -486,7 +709,7 @@ mraa_find_i2c_bus(const char* devname, int startfrom)
 
     // find how many i2c buses we have if we haven't already
     if (num_i2c_devices == 0) {
-        if (nftw("/sys/class/i2c-dev/", &mraa_count_files, 20, FTW_PHYS) == -1) {
+        if (nftw("/sys/class/i2c-dev/", &mraa_count_i2c_files, 20, FTW_PHYS) == -1) {
             return -1;
         }
     }
@@ -538,3 +761,41 @@ mraa_find_i2c_bus(const char* devname, int startfrom)
 
     return ret;
 }
+
+mraa_boolean_t
+mraa_is_sub_platform_id(int pin_or_bus)
+{
+    return (pin_or_bus & MRAA_SUB_PLATFORM_MASK) != 0;
+}
+
+int
+mraa_get_sub_platform_id(int pin_or_bus)
+{
+    return pin_or_bus | MRAA_SUB_PLATFORM_MASK;
+}
+
+int
+mraa_get_sub_platform_index(int pin_or_bus)
+{
+    return pin_or_bus & (~MRAA_SUB_PLATFORM_MASK);
+}
+
+int
+mraa_get_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->iio_device_count; i++) {
+#if 0
+        // compare with devices array
+        if (!strcmp() {
+        }
+#endif
+    }
+    return 0;
+}