Add serial uart mux interface class

1. This change adds the multi-host serial uart mux interface class.

In a multi-host system when the host position is changed,
then the serial uart mux is configured to enable  the
respective host's serial console which is accessed via
OCP debug card.

2. Introduced two new methods in gpio.cpp
   setGpioState - set state for gpio fd based on polarity
   getGpiostate - get state for gpio fd based on polarity

3. Updated the readme file about details on the new gpio
configs

design : https://github.com/openbmc/docs/blob/master/designs/multihost-phosphor-buttons.md

Testing : This change is verified in yosemiteV2 platform.

Signed-off-by: Naveen Moses <naveen.mosess@hcl.com>
Change-Id: I861d70570650d7dfcab842a35bdcf63a9fdd3bd0
diff --git a/src/serial_uart_mux.cpp b/src/serial_uart_mux.cpp
new file mode 100644
index 0000000..00724dd
--- /dev/null
+++ b/src/serial_uart_mux.cpp
@@ -0,0 +1,112 @@
+#include "serial_uart_mux.hpp"
+
+#include "xyz/openbmc_project/Chassis/Buttons/HostSelector/client.hpp"
+#include "xyz/openbmc_project/Chassis/Buttons/HostSelector/server.hpp"
+
+#include <error.h>
+
+#include <phosphor-logging/lg2.hpp>
+namespace sdbusRule = sdbusplus::bus::match::rules;
+// add the button iface class to registry
+static ButtonIFRegister<SerialUartMux> buttonRegister;
+namespace HostSelectorServerObj =
+    sdbusplus::xyz::openbmc_project::Chassis::Buttons::server;
+namespace HostSelectorClientObj =
+    sdbusplus::xyz::openbmc_project::Chassis::Buttons::client::HostSelector;
+
+constexpr std::string_view SERIAL_UART_RX_GPIO = "serial_uart_rx";
+void SerialUartMux::init()
+{
+    try
+    {
+        // when Host Selector Position is changed call the handler
+
+        std::string matchPattern = sdbusRule::propertiesChanged(
+            HS_DBUS_OBJECT_NAME, HostSelectorClientObj::interface);
+
+        hostPositionChanged = std::make_unique<sdbusplus::bus::match_t>(
+            bus, matchPattern,
+            std::bind(std::mem_fn(&SerialUartMux::hostSelectorPositionChanged),
+                      this, std::placeholders::_1));
+    }
+    catch (const std::exception& e)
+    {
+        lg2::error(
+            "Failed binding to matching function : {BUTTON_TYPE},Exception : {ERROR}",
+            "BUTTON_TYPE", getFormFactorName(), "ERROR", e);
+        throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
+            IOError();
+    }
+}
+// check the debug card present pin
+bool SerialUartMux::isOCPDebugCardPresent()
+{
+    auto gpioState =
+        getGpioState(debugCardPresentGpio.fd, debugCardPresentGpio.polarity);
+    return (gpioState == GpioState::assert);
+}
+// set the serial uart MUX to select the console w.r.t host selector position
+void SerialUartMux::configSerialConsoleMux(size_t position)
+{
+    auto debugCardPresent = isOCPDebugCardPresent();
+
+    if (debugCardPresent)
+    {
+        lg2::info("Debug card is present ");
+    }
+    else
+    {
+        lg2::info("Debug card not present ");
+    }
+
+    for (size_t uartMuxSel = 0; uartMuxSel < gpioLineCount; uartMuxSel++)
+    {
+        auto gpioState = GpioState::invalid;
+        gpioInfo gpioConfig = config.gpios[uartMuxSel];
+
+        if (gpioConfig.name == SERIAL_UART_RX_GPIO)
+        {
+            gpioState =
+                debugCardPresent ? GpioState::assert : GpioState::deassert;
+        }
+        else
+        {
+            gpioState = (serialUartMuxMap[position] & (0x1 << uartMuxSel))
+                            ? GpioState::assert
+                            : GpioState::deassert;
+        }
+        setGpioState(gpioConfig.fd, gpioConfig.polarity, gpioState);
+    }
+}
+
+void SerialUartMux::hostSelectorPositionChanged(sdbusplus::message_t& msg)
+{
+    std::string interface;
+    std::map<std::string,
+             HostSelectorServerObj::HostSelector::PropertiesVariant>
+        propertiesChanged;
+    lg2::info("hostSelectorPositionChanged callback : {BUTTON_TYPE}",
+              "BUTTON_TYPE", getFormFactorName());
+
+    try
+    {
+        msg.read(interface, propertiesChanged);
+        for (auto& property : propertiesChanged)
+        {
+            auto propertyName = property.first;
+            if (propertyName == "Position")
+            {
+                size_t hostPosition = std::get<size_t>(property.second);
+                lg2::debug("property changed : {VALUE}", "VALUE", hostPosition);
+                configSerialConsoleMux(hostPosition);
+                return;
+            }
+        }
+    }
+    catch (const std::exception& e)
+    {
+        lg2::error("exception while reading dbus property : {ERROR}", "ERROR",
+                   e.what());
+        return;
+    }
+}