cmake: add -DFTDID2xx and use FindFtd2xx.cmake
[contrib/mraa.git] / src / mraa.c
index 739f371..09713ae 100644 (file)
@@ -60,8 +60,10 @@ mraa_set_log_level(int level)
 {
     if (level <= 7 && level >= 0) {
         setlogmask(LOG_UPTO(level));
+        syslog(LOG_DEBUG, "Loglevel %d is set", level);
         return MRAA_SUCCESS;
     }
+    syslog(LOG_NOTICE, "Invalid loglevel %d requested", level);
     return MRAA_ERROR_INVALID_PARAMETER;
 }
 
@@ -95,25 +97,39 @@ 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));
 
-#ifdef X86PLAT
+#if defined(X86PLAT)
     // Use runtime x86 platform detection
     platform_type = mraa_x86_platform();
-#elif ARMPLAT
+#elif defined(ARMPLAT)
     // Use runtime ARM platform detection
     platform_type = mraa_arm_platform();
 #else
 #error mraa_ARCH NOTHING
 #endif
 
+#ifdef 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));
+        plat->platform_name = "Unknown platform";
+        if (plat != NULL) {
+            int usb_platform_type = mraa_usb_platform_extender(plat);
+            if (platform_type == MRAA_UNKNOWN_PLATFORM) {
+                platform_type = usb_platform_type;
+            }
+        }
+    }
     if (plat == NULL) {
         printf("mraa: FATAL error, failed to initialise platform\n");
         return MRAA_ERROR_PLATFORM_NOT_INITIALISED;
     }
+#endif
 
-    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(), platform_type);
     return MRAA_SUCCESS;
 }
 
@@ -310,6 +326,29 @@ mraa_get_platform_name()
     return (char*) plat->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
 mraa_get_pin_count()
 {
@@ -330,6 +369,17 @@ mraa_get_pin_name(int pin)
     return (char*) plat->pins[pin].name;
 }
 
+int
+mraa_get_default_i2c_bus()
+{
+    if (plat == NULL) {
+        return -1;
+    } else
+        return plat->def_i2c_bus;
+}
+
+
+
 mraa_boolean_t
 mraa_file_exist(const char* filename)
 {
@@ -353,7 +403,16 @@ mraa_file_contains(const char* filename, const char* content)
     if (file != NULL) {
         size_t len = 1024;
         char* line = malloc(len);
+        if (line == NULL) {
+            free(file);
+            return 0;
+        }
         FILE* fh = fopen(file, "r");
+        if (fh == NULL) {
+            free(file);
+            free(line);
+            return 0;
+        }
         while ((getline(&line, &len, fh) != -1) && (found == 0)) {
             if (strstr(line, content)) {
                 found = 1;
@@ -379,7 +438,16 @@ mraa_file_contains_both(const char* filename, const char* content, const char* c
     if (file != NULL) {
         size_t len = 1024;
         char* line = malloc(len);
+        if (line == NULL) {
+            free(file);
+            return 0;
+        }
         FILE* fh = fopen(file, "r");
+        if (fh == NULL) {
+            free(file);
+            free(line);
+            return 0;
+        }
         while ((getline(&line, &len, fh) != -1) && (found == 0)) {
             if (strstr(line, content) && strstr(line, content2)) {
                 found = 1;