api: Added API calls that support both main and sub platforms
authorHenry Bruce <henry.bruce@intel.com>
Thu, 20 Aug 2015 19:08:57 +0000 (12:08 -0700)
committerBrendan Le Foll <brendan.le.foll@intel.com>
Tue, 8 Sep 2015 13:42:04 +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 7c5dc49..58afb82 100644 (file)
@@ -24,6 +24,7 @@
 
 #pragma once
 
+#include <stdint.h>
 #include "types.h"
 
 #define MRAA_PLATFORM_NAME_MAX_SIZE 64
@@ -32,6 +33,8 @@
 #define MRAA_SUB_PLATFORM_BIT_SHIFT 9
 #define MRAA_SUB_PLATFORM_MASK (1<<MRAA_SUB_PLATFORM_BIT_SHIFT)
 
+#define MRAA_MAIN_PLATFORM_OFFSET 0
+#define MRAA_SUB_PLATFORM_OFFSET 1
 
 
 /** @file
@@ -95,6 +98,14 @@ mraa_boolean_t mraa_pin_mode_test(int pin, mraa_pinmodes_t mode);
 unsigned int mraa_adc_raw_bits();
 
 /**
+ * Check the specified board's bit size when reading the value
+ *
+ * @param specified platform offset; 0 for main platform, 1 foor sub platform
+ * @return raw bits being read from kernel module. zero if no ADC
+ */
+unsigned int mraa_get_platform_adc_raw_bits(uint8_t platform_offset);
+
+/**
  * Return value that the raw value should be shifted to. Zero if no ADC
  *
  * @return return actual bit size the adc value should be understood as.
@@ -102,6 +113,14 @@ unsigned int mraa_adc_raw_bits();
 unsigned int mraa_adc_supported_bits();
 
 /**
+ * Return value that the raw value should be shifted to. Zero if no ADC
+ *
+ * @param specified platform offset; 0 for main platform, 1 foor sub platform
+ * @return return actual bit size the adc value should be understood as.
+ */
+unsigned int mraa_get_platform_adc_supported_bits(int platform_offset);
+
+/**
  * Sets the log level to use from 0-7 where 7 is very verbose. These are the
  * syslog log levels, see syslog(3) for more information on the levels.
  *
@@ -151,6 +170,15 @@ void mraa_result_print(mraa_result_t result);
 mraa_platform_t mraa_get_platform_type();
 
 /**
+ * Get combined platform type, board must be initialised.
+ * The combined type is represented as
+ * (sub_platform_type << 8) | main_platform_type
+ *
+ * @return int combined platform type
+ */
+int mraa_get_platform_combined_type();
+
+/**
  * Get platform pincount, board must be initialised.
  *
  * @return uint of physical pin count on the in-use platform
@@ -174,6 +202,14 @@ int mraa_get_i2c_bus_count();
 int mraa_get_i2c_bus_id(unsigned int i2c_bus);
 
 /**
+ * Get specified platform pincount, board must be initialised.
+ *
+ * @param specified platform offset; 0 for main platform, 1 foor sub platform
+ * @return uint of physical pin count on the in-use platform
+ */
+unsigned int mraa_get_platform_pin_count(uint8_t platform_offset);
+
+/**
 * Get name of pin, board must be initialised.
 *
 * @param pin number
@@ -186,7 +222,7 @@ char* mraa_get_pin_name(int pin);
  *
  * @return int default i2c bus index
  */
-int mraa_get_default_2c_bus();
+int mraa_get_default_i2c_bus(uint8_t platform_offset);
 
 /**
  * Detect presence of sub platform.
@@ -197,27 +233,6 @@ mraa_boolean_t mraa_has_sub_platform();
 
 
 /**
- * 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 fdb1373..0121895 100644 (file)
 #include "gpio.h"
 #include "version.h"
 
+#define MAX_PLATFORM_NAME_LENGTH 128
 mraa_board_t* plat = NULL;
-static mraa_board_t* current_plat = NULL;
+// static mraa_board_t* current_plat = NULL;
+
+static char platform_name[MAX_PLATFORM_NAME_LENGTH];
 mraa_adv_func_t* advance_func;
 
 const char*
@@ -124,8 +127,8 @@ mraa_init()
     if (plat == NULL) {
         plat = (mraa_board_t*) calloc(1, sizeof(mraa_board_t));
         if (plat != NULL) {
-            plat->platform_type = MRAA_UNKNOWN_PLATFORM;
-            plat->platform_name = "Unknown platform";
+            plat->platform_type = MRAA_NULL_PLATFORM;
+            plat->platform_name = "Null platform";
         }
     }
     // Now detect sub platform
@@ -143,13 +146,7 @@ mraa_init()
     }
 #endif
 
-    current_plat = plat;
     syslog(LOG_NOTICE, "libmraa initialised for platform '%s' of type %d", mraa_get_platform_name(), mraa_get_platform_type());
-    if (mraa_has_sub_platform()) {
-        mraa_select_sub_platform();
-        syslog(LOG_NOTICE, "libmraa initialised for sub-platform '%s' of type %d", mraa_get_platform_name(), mraa_get_platform_type());    
-        mraa_select_main_platform();
-    }
     return MRAA_SUCCESS;
 }
 
@@ -275,34 +272,21 @@ mraa_has_sub_platform()
 
 
 mraa_boolean_t
-mraa_select_main_platform()
+mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
 {
-    if (plat != NULL) {
-        current_plat = plat;
-        return 1;
-    } else
+    if (plat == NULL)
         return 0;
-}
 
-mraa_boolean_t
-mraa_select_sub_platform()
-{
-    if (plat->sub_platform != NULL) {
+    mraa_board_t* current_plat = plat;
+    if (mraa_is_sub_platform_id(pin)) {
         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);
-}
+        if (current_plat == NULL) {
+            syslog(LOG_ERR, "mraa_pin_mode_test: Sub platform Not Initialised");
+            return 0;
+        }
+        pin = mraa_get_sub_platform_index(pin);
+    }
 
-mraa_boolean_t
-mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
-{
     if (current_plat == NULL || current_plat->platform_type == MRAA_UNKNOWN_PLATFORM) {
         return 0;
     }
@@ -352,44 +336,89 @@ mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
 mraa_platform_t
 mraa_get_platform_type()
 {
-    if (current_plat == NULL)
+    if (plat == NULL)
         return MRAA_UNKNOWN_PLATFORM;
-    return current_plat->platform_type;
+    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
 mraa_adc_raw_bits()
 {
-    if (current_plat == NULL)
+    if (plat == NULL)
         return 0;
 
-    if (current_plat->aio_count == 0)
+    if (plat->aio_count == 0)
         return 0;
 
-    return current_plat->adc_raw;
+    return plat->adc_raw;
+}
+
+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 (current_plat == NULL)
+    if (plat == NULL)
         return 0;
 
-    if (current_plat->aio_count == 0)
+    if (plat->aio_count == 0)
         return 0;
 
-    return current_plat->adc_supported;
+    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 (current_plat == NULL) {
+    if (plat == NULL) {
         return NULL;
     }
-    return (char*) current_plat->platform_name;
+    strncpy(platform_name, plat->platform_name, MAX_PLATFORM_NAME_LENGTH);
+    if (mraa_has_sub_platform()) {
+        strncat(platform_name, " + ", MAX_PLATFORM_NAME_LENGTH);
+        strncat(platform_name, plat->sub_platform->platform_name, MAX_PLATFORM_NAME_LENGTH);        
+    }
+    return platform_name;
 }
 
 int
@@ -418,34 +447,63 @@ mraa_get_i2c_bus_id(unsigned i2c_bus)
 unsigned int
 mraa_get_pin_count()
 {
-    if (current_plat == NULL) {
+    if (plat == NULL) {
         return 0;
     }
-    return current_plat->phy_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 (current_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 > (current_plat->phy_pin_count - 1) || pin < 0)
         return NULL;
     return (char*) current_plat->pins[pin].name;
 }
 
 int
-mraa_get_default_i2c_bus()
+mraa_get_default_i2c_bus(uint8_t platform_offset)
 {
-    if (current_plat == NULL) {
+    if (plat == NULL) 
         return -1;
-    } else
-        return current_plat->def_i2c_bus;
+    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)
 {