Added library support for CPLD communication

Added library support for CPLD communciation. This library exposes
the API's used for fetching the versions(bmc/bios/cpld active & recovery)
information from CPLD.

Tested:
 Loadded this library as part of intel pfr manager service(other commit)
 and verified functionality.

Change-Id: Ibeffd183cacd9d5cc528665eabe4a7ecb5ef7e6a
Signed-off-by: AppaRao Puli <apparao.puli@linux.intel.com>
diff --git a/libpfr/CMakeLists.txt b/libpfr/CMakeLists.txt
new file mode 100644
index 0000000..8957c9f
--- /dev/null
+++ b/libpfr/CMakeLists.txt
@@ -0,0 +1,15 @@
+cmake_minimum_required(VERSION 2.8.10 FATAL_ERROR)
+project(pfr CXX)
+set(CMAKE_CXX_STANDARD 17)
+set(CMAKE_CXX_STANDARD_REQUIRED ON)
+
+include(ExternalProject)
+include_directories(${CMAKE_CURRENT_SOURCE_DIR}/inc)
+
+add_library(${PROJECT_NAME} SHARED src/pfr.cpp)
+
+set_target_properties(${PROJECT_NAME} PROPERTIES VERSION "0.1.0")
+set_target_properties(${PROJECT_NAME} PROPERTIES SOVERSION "0")
+target_link_libraries(${PROJECT_NAME} phosphor_logging)
+
+install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_LIBDIR})
diff --git a/libpfr/inc/file.hpp b/libpfr/inc/file.hpp
new file mode 100644
index 0000000..c7ef58c
--- /dev/null
+++ b/libpfr/inc/file.hpp
@@ -0,0 +1,107 @@
+/*
+// Copyright (c) 2019 Intel Corporation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+*/
+
+#pragma once
+
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <stdio.h>
+#include <experimental/filesystem>
+#include <phosphor-logging/log.hpp>
+
+extern "C" {
+#include <i2c/smbus.h>
+#include <linux/i2c-dev.h>
+}
+
+namespace intel
+{
+namespace pfr
+{
+
+/** @class I2CFile
+ *  @brief Responsible for handling file pointer
+ */
+class I2CFile
+{
+  private:
+    /** @brief handler for operating on file */
+    int fd;
+
+  public:
+    I2CFile() = delete;
+    I2CFile(const I2CFile&) = delete;
+    I2CFile& operator=(const I2CFile&) = delete;
+    I2CFile(I2CFile&&) = delete;
+    I2CFile& operator=(I2CFile&&) = delete;
+
+    /** @brief Opens i2c device file and sets slave
+     *
+     *  @param[in] i2cBus       - I2C bus number
+     *  @param[in] slaveAddr    - I2C slave address
+     *  @param[in] flags        - Flags
+     */
+    I2CFile(const int& i2cBus, const int& slaveAddr, const int& flags)
+    {
+        std::string i2cDev = "/dev/i2c-" + std::to_string(i2cBus);
+
+        fd = open(i2cDev.c_str(), flags);
+        if (fd < 0)
+        {
+            throw std::runtime_error("Unable to open i2c device.");
+        }
+
+        if (ioctl(fd, I2C_SLAVE_FORCE, slaveAddr) < 0)
+        {
+            close(fd);
+            fd = -1;
+            throw std::runtime_error("Unable to set i2c slave address.");
+        }
+    }
+
+    /** @brief Reads the byte data from I2C dev
+     *
+     *  @param[in] Offset       -  Offset value
+     *  @param[out] byte data      -  Offset value
+     */
+    uint8_t i2cReadByteData(const uint8_t& offset)
+    {
+        uint8_t value = i2c_smbus_read_byte_data(fd, offset);
+
+        if (value < 0)
+        {
+            throw std::runtime_error("i2c_smbus_read_byte_data() failed");
+        }
+        return value;
+    }
+
+    ~I2CFile()
+    {
+        if (!(fd < 0))
+        {
+            close(fd);
+        }
+    }
+
+    auto operator()()
+    {
+        return fd;
+    }
+};
+
+} // namespace pfr
+} // namespace intel
diff --git a/libpfr/inc/pfr.hpp b/libpfr/inc/pfr.hpp
new file mode 100644
index 0000000..bc1f3df
--- /dev/null
+++ b/libpfr/inc/pfr.hpp
@@ -0,0 +1,38 @@
+/*
+// Copyright (c) 2019 Intel Corporation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+*/
+#pragma once
+
+#include <string>
+
+namespace intel
+{
+namespace pfr
+{
+
+enum class ImageType
+{
+    cpld,
+    biosActive,
+    biosRecovery,
+    bmcActive,
+    bmcRecovery
+};
+
+std::string getVersionInfoCPLD(ImageType &imgType);
+int getProvisioningStatus(bool &ufmLocked, bool &ufmProvisioned);
+
+} // namespace pfr
+} // namespace intel
diff --git a/libpfr/src/pfr.cpp b/libpfr/src/pfr.cpp
new file mode 100644
index 0000000..59929a2
--- /dev/null
+++ b/libpfr/src/pfr.cpp
@@ -0,0 +1,144 @@
+/*
+// Copyright (c) 2019 Intel Corporation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+*/
+
+#include <unistd.h>
+#include <iostream>
+#include <sstream>
+#include <iomanip>
+#include "pfr.hpp"
+#include "file.hpp"
+
+namespace intel
+{
+namespace pfr
+{
+// TODO: Dynamically pull these values from configuration
+// entity-manager, when needed
+static constexpr int i2cBusNumber = 4;
+static constexpr int i2cSlaveAddress = 0x70;
+
+// CPLD mailbox registers
+static constexpr uint8_t cpldROTVersion = 0x01;
+static constexpr uint8_t cpldROTSvn = 0x02;
+static constexpr uint8_t platformState = 0x03;
+static constexpr uint8_t recoveryCount = 0x04;
+static constexpr uint8_t lastRecoveryReason = 0x05;
+static constexpr uint8_t panicEventCount = 0x06;
+static constexpr uint8_t panicEventReason = 0x07;
+static constexpr uint8_t majorErrorCode = 0x08;
+static constexpr uint8_t minorErrorCode = 0x09;
+static constexpr uint8_t provisioningStatus = 0x0A;
+static constexpr uint8_t pchActiveMajorVersion = 0x17;
+static constexpr uint8_t pchActiveMinorVersion = 0x18;
+static constexpr uint8_t bmcActiveMajorVersion = 0x19;
+static constexpr uint8_t bmcActiveMinorVersion = 0x1A;
+static constexpr uint8_t pchRecoveryMajorVersion = 0x1C;
+static constexpr uint8_t pchRecoveryMinorVersion = 0x1D;
+static constexpr uint8_t bmcRecoveryMajorVersion = 0x1E;
+static constexpr uint8_t bmcRecoveryMinorVersion = 0x1F;
+
+static constexpr uint8_t ufmLockedMask = (0x1 << 0x04);
+static constexpr uint8_t ufmProvisionedMask = (0x1 << 0x05);
+
+template <typename T> std::string int_to_hexstring(T i)
+{
+    std::stringstream stream;
+    stream << std::setfill('0') << std::setw(sizeof(T) * 2) << std::hex
+           << static_cast<int>(i);
+    return stream.str();
+}
+
+std::string getVersionInfoCPLD(ImageType& imgType)
+{
+    try
+    {
+        uint8_t majorReg;
+        uint8_t minorReg;
+
+        switch (imgType)
+        {
+            case (ImageType::cpld):
+            {
+                majorReg = cpldROTVersion;
+                minorReg = cpldROTSvn;
+                break;
+            }
+            case (ImageType::biosActive):
+            {
+                majorReg = pchActiveMajorVersion;
+                minorReg = pchActiveMinorVersion;
+                break;
+            }
+            case (ImageType::biosRecovery):
+            {
+                majorReg = pchRecoveryMajorVersion;
+                minorReg = pchRecoveryMinorVersion;
+                break;
+            }
+            case (ImageType::bmcActive):
+            {
+                majorReg = bmcActiveMajorVersion;
+                minorReg = bmcActiveMinorVersion;
+                break;
+            }
+            case (ImageType::bmcRecovery):
+            {
+                majorReg = bmcRecoveryMajorVersion;
+                minorReg = bmcRecoveryMinorVersion;
+                break;
+            }
+            default:
+                // Invalid image Type.
+                return "";
+        }
+
+        I2CFile cpldDev(i2cBusNumber, i2cSlaveAddress, O_RDWR | O_CLOEXEC);
+        uint8_t majorVer = cpldDev.i2cReadByteData(majorReg);
+        uint8_t minorVer = cpldDev.i2cReadByteData(minorReg);
+        std::string version =
+            int_to_hexstring(majorVer) + "." + int_to_hexstring(minorVer);
+        return version;
+    }
+    catch (const std::exception& e)
+    {
+        phosphor::logging::log<phosphor::logging::level::ERR>(
+            "Exception caught in getVersionInfoCPLD.",
+            phosphor::logging::entry("MSG=%s", e.what()));
+        return "";
+    }
+}
+
+int getProvisioningStatus(bool& ufmLocked, bool& ufmProvisioned)
+{
+    try
+    {
+        I2CFile cpldDev(i2cBusNumber, i2cSlaveAddress, O_RDWR | O_CLOEXEC);
+        uint8_t provStatus = cpldDev.i2cReadByteData(provisioningStatus);
+        ufmLocked = (provStatus & ufmLockedMask);
+        ufmProvisioned = (provStatus & ufmProvisionedMask);
+        return 0;
+    }
+    catch (const std::exception& e)
+    {
+        phosphor::logging::log<phosphor::logging::level::ERR>(
+            "Exception caught in getProvisioningStatus.",
+            phosphor::logging::entry("MSG=%s", e.what()));
+        return -1;
+    }
+}
+
+} // namespace pfr
+} // namespace intel