yosemitev2:Add multi-host critical sensor support

This feature adds the host critical sensor support for
Facebook Yosemitev2 platform.

Tested: Critical sensor value displayed in the
OCP debug card with multi-host Yosemitev2 platform.

Signed-off-by: Karthikeyan Pasupathi <p_karthikeya@hcl.com>
Change-Id: Ia77af76ac4e07435e7ed6287be8806efcd3ff86b
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 9c94572..20789ae 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -93,6 +93,6 @@
 install (FILES ${CONFIG_FILES} DESTINATION ${PACKAGE_DIR})
 
 if(BIC)
-set (CONFIG_FILES_YV2 yosemitev2/gpio_desc.json)
+set (CONFIG_FILES_YV2 yosemitev2/gpio_desc.json yosemitev2/cri_sensors.json)
 install (FILES ${CONFIG_FILES_YV2} DESTINATION ${PACKAGE_DIR})
 endif()
diff --git a/include/usb-dbg.hpp b/include/usb-dbg.hpp
index 743829b..608ee41 100644
--- a/include/usb-dbg.hpp
+++ b/include/usb-dbg.hpp
@@ -24,6 +24,8 @@
 #include <nlohmann/json.hpp>
 #include <phosphor-logging/log.hpp>
 #include <sdbusplus/asio/connection.hpp>
+#include <sdbusplus/asio/property.hpp>
+#include <ipmid/utils.hpp>
 
 #include <fstream>
 #include <iomanip>
diff --git a/src/oemcommands.cpp b/src/oemcommands.cpp
index d06bb17..ea5da82 100644
--- a/src/oemcommands.cpp
+++ b/src/oemcommands.cpp
@@ -337,6 +337,20 @@
     return rc;
 }
 
+bool isMultiHostPlatform()
+{
+    bool platform;
+    if (INSTANCES == "0")
+    {
+        platform = false;
+    }
+    else
+    {
+        platform = true;
+    }
+    return platform;
+}
+
 // return code: 0 successful
 int8_t getFruData(std::string& data, std::string& name)
 {
diff --git a/src/usb-dbg.cpp b/src/usb-dbg.cpp
index 472d4ae..55ba20c 100644
--- a/src/usb-dbg.cpp
+++ b/src/usb-dbg.cpp
@@ -22,6 +22,17 @@
 ipmi_ret_t getNetworkData(uint8_t lan_param, char* data);
 int8_t getFruData(std::string& serial, std::string& name);
 
+bool isMultiHostPlatform();
+
+/* Declare Host Selector interface and path */
+namespace selector
+{
+const std::string path = "/xyz/openbmc_project/Chassis/Buttons/HostSelector";
+const std::string interface =
+    "xyz.openbmc_project.Chassis.HostSelector.Selector";
+const std::string propertyName = "Position";
+} // namespace selector
+
 /* Declare storage functions used here */
 namespace storage
 {
@@ -29,14 +40,49 @@
 int getSensorUnit(std::string&, std::string&);
 } // namespace storage
 
+size_t getSelectorPosition()
+{
+    std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
+    std::string service =
+        getService(*dbus, ipmi::selector::interface, ipmi::selector::path);
+    Value variant = getDbusProperty(*dbus, service, ipmi::selector::path,
+                                    ipmi::selector::interface,
+                                    ipmi::selector::propertyName);
+    size_t result = std::get<size_t>(variant);
+    return result;
+}
+
 static int panelNum = (sizeof(panels) / sizeof(struct ctrl_panel)) - 1;
 
 /* Returns the FRU the hand-switch is switched to. If it is switched to BMC
  * it returns FRU_ALL. Note, if in err, it returns FRU_ALL */
-static uint8_t plat_get_fru_sel()
+static size_t plat_get_fru_sel()
 {
-    // For Tiogapass it just return 1, can modify to support more platform
-    return 1;
+    size_t position;
+    bool platform = isMultiHostPlatform();
+
+    if (platform == true)
+    {
+        try
+        {
+            size_t hostPosition = getSelectorPosition();
+            position = hostPosition;
+            if (position == BMC_POSITION)
+            {
+                return FRU_ALL;
+            }
+        }
+        catch (...)
+        {
+            std::cout << "Error reading host selector position" << std::endl;
+        }
+    }
+    else
+    {
+        // For Tiogapass it just return 1, can modify to support more platform
+        position = 1;
+    }
+    return position;
 }
 
 // return 0 on seccuess
@@ -315,7 +361,7 @@
 {
     FILE* fp;
     struct stat file_stat;
-    uint8_t pos = plat_get_fru_sel();
+    size_t pos = plat_get_fru_sel();
     static uint8_t pre_pos = 0xff;
 
     fp = fopen("/mnt/data/cri_sel", "r");
@@ -545,7 +591,7 @@
     const char* ptr;
     FILE* fp;
     struct stat file_stat;
-    uint8_t pos = plat_get_fru_sel();
+    size_t pos = plat_get_fru_sel();
     static uint8_t pre_pos = FRU_ALL;
     bool pos_changed = pre_pos != pos;
 
@@ -628,6 +674,7 @@
 {
     int ret;
     double fvalue;
+    size_t pos = plat_get_fru_sel();
 
     if (page == 1)
     {
@@ -661,6 +708,11 @@
             std::string senName = j.key();
             auto val = j.value();
 
+            if (senName[0] == '_')
+            {
+                senName = std::to_string(pos) + senName;
+            }
+
             if (ipmi::storage::getSensorValue(senName, fvalue) == 0)
             {
                 std::stringstream ss;
@@ -862,7 +914,7 @@
                               uint8_t* count, uint8_t* buffer)
 {
     char line_buff[1000], *pres_dev = line_buff;
-    uint8_t pos = plat_get_fru_sel();
+    size_t pos = plat_get_fru_sel();
     const char* delim = "\n";
     int ret;
     std::string serialName = "BOARD_SERIAL_NUMBER";
@@ -1012,7 +1064,7 @@
 {
     int i;
     unsigned char buff[MAX_VALUE_LEN], pickup, len;
-    uint8_t pos = plat_get_fru_sel();
+    size_t pos = plat_get_fru_sel();
 
     /* To be implemented */
     /*
@@ -1089,7 +1141,7 @@
 {
     uint8_t buff[32] = {0};
     uint8_t res_len;
-    uint8_t pos = plat_get_fru_sel();
+    size_t pos = plat_get_fru_sel();
     uint8_t policy;
     //  uint8_t pwr_policy_item_map[3] = {POWER_CFG_ON, POWER_CFG_LPS,
     //  POWER_CFG_OFF};
diff --git a/yosemitev2/cri_sensors.json b/yosemitev2/cri_sensors.json
new file mode 100644
index 0000000..942426c
--- /dev/null
+++ b/yosemitev2/cri_sensors.json
@@ -0,0 +1,47 @@
+{
+        "_SOC_Temp":
+        {
+                "short_name": "SOC_TEMP:"
+        },
+        "_PVCCIN_VR_Temp":
+        {
+                "short_name": "SOC_VR_TEMP:"
+        },
+        "_PVCCIN_VR_Pout":
+        {
+                "short_name": "SOC_VR_PWR:"
+        },
+        "_SOC_DIMMA0_Temp":
+        {
+                "short_name": "DIMMA0_TEMP:"
+        },
+        "_SOC_DIMMA1_Temp":
+        {
+                "short_name": "DIMMA1_TEMP:",
+                "precision": 1
+        },
+        "_SOC_DIMMB0_Temp":
+        {
+                "short_name": "DIMMB0_TEMP:"
+        },
+        "_SOC_DIMMB1_Temp":
+        {
+                "short_name": "DIMMB1_TEMP:"
+        },
+        "_SOC_DIMMD0_Temp":
+        {
+                "short_name": "DIMMD0_TEMP:"
+        },
+        "_SOC_DIMMD1_Temp":
+        {
+                "short_name": "DIMMD1_TEMP:"
+        },
+        "_SOC_DIMME0_Temp":
+        {
+                "short_name": "DIMME0_TEMP:"
+        },
+        "_SOC_DIMME1_Temp":
+        {
+                "short_name": "DIMME1_TEMP:"
+        }
+}