mraa.c: Added mainn and sub-platform select API calls.
authorHenry Bruce <henry.bruce@intel.com>
Mon, 27 Jul 2015 16:32:39 +0000 (09:32 -0700)
committerBrendan Le Foll <brendan.le.foll@intel.com>
Tue, 8 Sep 2015 13:42:03 +0000 (14:42 +0100)
Signed-off-by: Henry Bruce <henry.bruce@intel.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
api/mraa/common.h
src/mraa.c

index a8cf4f1..eb048d9 100644 (file)
@@ -151,13 +151,6 @@ void mraa_result_print(mraa_result_t result);
 mraa_platform_t mraa_get_platform_type();
 
 /**
- * Get sub platform type, board must be initialised.
- *
- * @return mraa_platform_t Platform type enum
- */
-mraa_platform_t mraa_get_sub_platform_type();
-
-/**
  * Get platform pincount, board must be initialised.
  *
  * @return uint of physical pin count on the in-use platform
@@ -196,6 +189,27 @@ char* mraa_get_pin_name(int pin);
 int mraa_get_default_2c_bus();
 
 /**
+ * Select main platform for platform info calls.
+ *
+ * @return mraa_boolean_t 1 if main platform is available, 0 otherwise
+ */
+mraa_boolean_t mraa_select_main_platform();
+
+/**
+ * Select sub platform for platform info calls.
+ *
+ * @return mraa_boolean_t 1 if sub platform is available, 0 otherwise
+ */
+mraa_boolean_t mraa_select_sub_platform();
+       
+/**
+ * Check if sub platform is currently available and selected for platform info calls.
+ *
+ * @return mraa_boolean_t 1 if sub platform is selected, 0 otherwise
+ */
+mraa_boolean_t mraa_is_sub_platform_selected();
+
+/**
  * Check if pin or bus id includes sub platform mask.
  *
  * @param int pin or bus number
index 03e9f46..073fd30 100644 (file)
@@ -46,6 +46,7 @@
 #include "version.h"
 
 mraa_board_t* plat = NULL;
+static mraa_board_t* current_plat = NULL;
 mraa_adv_func_t* advance_func;
 
 const char*
@@ -104,7 +105,7 @@ mraa_init()
 #if defined(X86PLAT)
     // Use runtime x86 platform detection
     platform_type = mraa_x86_platform();
-    // x86 platforms have advanced_func stable in board config structure
+    // x86 platforms have advanced_func table in board config structure
     free(advance_func);
     if (plat != NULL)
         advance_func = plat->adv_func;
@@ -141,6 +142,7 @@ mraa_init()
 #endif
 
     syslog(LOG_NOTICE, "libmraa initialised for platform '%s' of type %d", mraa_get_platform_name(), mraa_get_platform_type());
+    current_plat = plat;
     return MRAA_SUCCESS;
 }
 
@@ -151,7 +153,15 @@ 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);
+        
     }
     closelog();
 }
@@ -249,46 +259,75 @@ mraa_result_print(mraa_result_t result)
     }
 }
 
+
+mraa_boolean_t
+mraa_select_main_platform()
+{
+    if (plat != NULL) {
+        current_plat = plat;
+        return 1;
+    } else
+        return 0;
+}
+
+mraa_boolean_t
+mraa_select_sub_platform()
+{
+    if (plat->sub_platform != NULL) {
+        current_plat = plat->sub_platform;
+        return 1;
+    } else
+        return 0;
+}
+
+mraa_boolean_t
+mraa_is_sub_platform_selected()
+{
+    return (plat->sub_platform != NULL) &&  (current_plat == plat->sub_platform);
+}
+
+
+
 mraa_boolean_t
 mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
 {
-    if (plat == NULL) {
+    if (current_plat == NULL) {
         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:
@@ -301,52 +340,44 @@ mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
 mraa_platform_t
 mraa_get_platform_type()
 {
-    if (plat == NULL)
+    if (current_plat == NULL)
         return MRAA_UNKNOWN_PLATFORM;
-    return plat->platform_type;
+    return current_plat->platform_type;
 }
 
 
-mraa_platform_t
-mraa_get_sub_platform_type()
-{
-    if (plat == NULL || plat->sub_platform == NULL)
-        return MRAA_UNKNOWN_PLATFORM;
-    return plat->sub_platform->platform_type;
-}
-
 
 unsigned int
 mraa_adc_raw_bits()
 {
-    if (plat == NULL)
+    if (current_plat == NULL)
         return 0;
 
-    if (plat->aio_count == 0)
+    if (current_plat->aio_count == 0)
         return 0;
 
-    return plat->adc_raw;
+    return current_plat->adc_raw;
 }
 
 unsigned int
 mraa_adc_supported_bits()
 {
-    if (plat == NULL)
+    if (current_plat == NULL)
         return 0;
 
-    if (plat->aio_count == 0)
+    if (current_plat->aio_count == 0)
         return 0;
 
-    return plat->adc_supported;
+    return current_plat->adc_supported;
 }
 
 char*
 mraa_get_platform_name()
 {
-    if (plat == NULL) {
+    if (current_plat == NULL) {
         return NULL;
     }
-    return (char*) plat->platform_name;
+    return (char*) current_plat->platform_name;
 }
 
 int
@@ -375,30 +406,30 @@ mraa_get_i2c_bus_id(unsigned i2c_bus)
 unsigned int
 mraa_get_pin_count()
 {
-    if (plat == NULL) {
+    if (current_plat == NULL) {
         return 0;
     }
-    return plat->phy_pin_count;
+    return current_plat->phy_pin_count;
 }
 
 char*
 mraa_get_pin_name(int pin)
 {
-    if (plat == NULL) {
+    if (current_plat == NULL) {
         return NULL;
     }
-    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()
 {
-    if (plat == NULL) {
+    if (current_plat == NULL) {
         return -1;
     } else
-        return plat->def_i2c_bus;
+        return current_plat->def_i2c_bus;
 }