Add dbus service to support MDR V2 ipmi command

Add send and get DataInfo function for MDR V2 ipmi
command store and get data through dbus interface.

Signed-off-by: Cheng C Yang <cheng.c.yang@linux.intel.com>
Change-Id: Ic069e4670e69de1fb92b4123da6be6588ac45244
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..cb69d6d
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,50 @@
+cmake_minimum_required (VERSION 3.2 FATAL_ERROR)
+include (ExternalProject)
+set (CMAKE_CXX_STANDARD 17)
+set (CMAKE_CXX_STANDARD_REQUIRED ON)
+set (CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})
+set (DBUS_OBJECT_NAME "xyz/openbmc_project/Smbios/MDR_V2")
+set (DBUS_INTF_NAME "xyz.openbmc_project.Smbios.MDR_V2")
+
+set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti")
+
+include_directories (
+    ${CMAKE_CURRENT_SOURCE_DIR}/include/phosphor-dbus-interfaces
+)
+include_directories (${CMAKE_CURRENT_SOURCE_DIR}/include)
+link_directories (${DBUSINTERFACE_LIBRARIES})
+
+find_package (PkgConfig REQUIRED)
+pkg_check_modules (SYSTEMD libsystemd REQUIRED)
+include_directories (${SYSTEMD_INCLUDE_DIRS})
+link_directories (${SYSTEMD_LIBRARY_DIRS})
+
+# import sdbusplus
+find_package (PkgConfig REQUIRED)
+pkg_check_modules (SDBUSPLUSPLUS sdbusplus REQUIRED)
+include_directories (${SDBUSPLUSPLUS_INCLUDE_DIRS})
+link_directories (${SDBUSPLUSPLUS_LIBRARY_DIRS})
+
+# phosphor-dbus-interfaces
+find_package (PkgConfig REQUIRED)
+pkg_check_modules (DBUSINTERFACE phosphor-dbus-interfaces REQUIRED)
+include_directories (${DBUSINTERFACE_INCLUDE_DIRS})
+link_directories (${DBUSINTERFACE_LIBRARY_DIRS})
+
+set (SRC_FILES src/mdrv2.cpp src/mdrv2_main.cpp)
+
+include_directories (${CMAKE_CURRENT_BINARY_DIR})
+
+find_package (PkgConfig REQUIRED)
+pkg_check_modules (SDBUSPLUSPLUS sdbusplus REQUIRED)
+include_directories (${SDBUSPLUSPLUS_INCLUDE_DIRS})
+link_directories (${SDBUSPLUSPLUS_LIBRARY_DIRS})
+find_program (SDBUSPLUSPLUS sdbus++)
+
+add_executable (smbiosmdrv2app ${SRC_FILES})
+target_link_libraries (smbiosmdrv2app ${SYSTEMD_LIBRARIES})
+target_link_libraries (smbiosmdrv2app ${DBUSINTERFACE_LIBRARIES})
+target_link_libraries (smbiosmdrv2app ${SDBUSPLUSPLUS_LIBRARIES})
+target_link_libraries (smbiosmdrv2app phosphor_logging)
+
+install (TARGETS smbiosmdrv2app DESTINATION bin)
diff --git a/include/mdrv2.hpp b/include/mdrv2.hpp
index 5313bec..4a46c12 100644
--- a/include/mdrv2.hpp
+++ b/include/mdrv2.hpp
@@ -24,12 +24,14 @@
 #include <phosphor-logging/log.hpp>
 #include <sdbusplus/server.hpp>
 #include <sdbusplus/timer.hpp>
+#include <smbios.hpp>
+#include <xyz/openbmc_project/Smbios/MDR_V2/server.hpp>
 
 static constexpr uint8_t mdr2Version = 2;
 static constexpr uint32_t mdr2SMSize = 0x00100000;
-static constexpr uint16_t mdrSMBIOSSize = 32 * 1024;
 static constexpr uint32_t mdr2SMBaseAddress = 0x9FF00000;
 static constexpr const char* mdrType2File = "/var/lib/smbios/smbios2";
+static constexpr const char* mdrV2Path = "/xyz/openbmc_project/Smbios/MDR_V2";
 
 enum class MDR2SMBIOSStatusEnum
 {
@@ -95,7 +97,8 @@
 namespace smbios
 {
 
-class MDR_V2
+class MDR_V2 : sdbusplus::server::object::object<
+                   sdbusplus::xyz::openbmc_project::Smbios::server::MDR_V2>
 {
   public:
     MDR_V2() = delete;
@@ -106,6 +109,8 @@
     ~MDR_V2() = default;
 
     MDR_V2(sdbusplus::bus::bus& bus, const char* path, sd_event* event) :
+        sdbusplus::server::object::object<
+            sdbusplus::xyz::openbmc_project::Smbios::server::MDR_V2>(bus, path),
         bus(bus), timer(event, [&](void) { agentSynchronizeData(); })
     {
 
@@ -124,6 +129,29 @@
         agentSynchronizeData();
     }
 
+    std::vector<uint8_t> getDirectoryInformation(uint8_t dirIndex) override;
+
+    std::vector<uint8_t> getDataInformation(uint8_t idIndex) override;
+
+    bool sendDirectoryInformation(uint8_t dirVersion, uint8_t dirIndex,
+                                  uint8_t returnedEntries,
+                                  uint8_t remainingEntries,
+                                  std::vector<uint8_t> dirEntry) override;
+
+    std::vector<uint8_t> getDataOffer() override;
+
+    bool sendDataInformation(uint8_t idIndex, uint8_t flag, uint32_t dataLen,
+                             uint32_t dataVer, uint32_t timeStamp) override;
+
+    int findIdIndex(std::vector<uint8_t> dataInfo) override;
+
+    bool agentSynchronizeData() override;
+
+    std::vector<uint32_t>
+        synchronizeDirectoryCommonData(uint8_t idIndex, uint32_t size) override;
+
+    uint8_t directoryEntries(uint8_t value) override;
+
   private:
     Timer timer;
 
diff --git a/include/smbios.hpp b/include/smbios.hpp
index b39229d..71140de 100644
--- a/include/smbios.hpp
+++ b/include/smbios.hpp
@@ -31,6 +31,7 @@
 static constexpr uint32_t smbiosTableTimestamp = 0x45464748;
 static constexpr uint32_t smbiosTableStorageSize = 64 * 1024;
 static constexpr const char* smbiosPath = "/var/lib/smbios";
+static constexpr uint16_t mdrSMBIOSSize = 32 * 1024;
 
 enum class DirDataRequestEnum
 {
diff --git a/src/mdrv2.cpp b/src/mdrv2.cpp
new file mode 100644
index 0000000..7568603
--- /dev/null
+++ b/src/mdrv2.cpp
@@ -0,0 +1,241 @@
+/*
+// 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 "mdrv2.hpp"
+
+#include <sys/mman.h>
+
+#include <fstream>
+#include <phosphor-logging/elog-errors.hpp>
+#include <sdbusplus/exception.hpp>
+#include <xyz/openbmc_project/Smbios/MDR_V2/error.hpp>
+
+namespace phosphor
+{
+namespace smbios
+{
+
+std::vector<uint8_t> MDR_V2::getDirectoryInformation(uint8_t dirIndex)
+{
+}
+
+bool MDR_V2::smbiosIsAvailForUpdate(uint8_t index)
+{
+    bool ret = false;
+    if (index > maxDirEntries)
+    {
+        return ret;
+    }
+
+    switch (smbiosDir.dir[index].stage)
+    {
+        case MDR2SMBIOSStatusEnum::mdr2Updating:
+            ret = false;
+            break;
+
+        case MDR2SMBIOSStatusEnum::mdr2Init:
+            // This *looks* like there should be a break statement here,
+            // as the effects of the previous statement are a noop given
+            // the following code that this falls through to.
+            // We've elected not to change it, though, since it's been
+            // this way since the old generation, and it would affect
+            // the way the code functions.
+            // If it ain't broke, don't fix it.
+
+        case MDR2SMBIOSStatusEnum::mdr2Loaded:
+        case MDR2SMBIOSStatusEnum::mdr2Updated:
+            if (smbiosDir.dir[index].lock == MDR2DirLockEnum::mdr2DirLock)
+            {
+                ret = false;
+            }
+            else
+            {
+                ret = true;
+            }
+            break;
+
+        default:
+            break;
+    }
+
+    return ret;
+}
+
+std::vector<uint8_t> MDR_V2::getDataOffer()
+{
+    std::vector<uint8_t> offer(sizeof(DataIdStruct));
+    if (smbiosIsAvailForUpdate(0))
+    {
+        std::copy(smbiosDir.dir[0].common.id.dataInfo,
+                  &smbiosDir.dir[0].common.id.dataInfo[16], offer.data());
+    }
+    else
+    {
+        phosphor::logging::log<phosphor::logging::level::ERR>(
+            "smbios is not ready for update");
+        throw sdbusplus::xyz::openbmc_project::Smbios::MDR_V2::Error::
+            UpdateInProgress();
+    }
+    return offer;
+}
+
+inline uint8_t MDR_V2::smbiosValidFlag(uint8_t index)
+{
+    FlagStatus ret = FlagStatus::flagIsInvalid;
+    MDR2SMBIOSStatusEnum stage = smbiosDir.dir[index].stage;
+    MDR2DirLockEnum lock = smbiosDir.dir[index].lock;
+
+    switch (stage)
+    {
+        case MDR2SMBIOSStatusEnum::mdr2Loaded:
+        case MDR2SMBIOSStatusEnum::mdr2Updated:
+            if (lock == MDR2DirLockEnum::mdr2DirLock)
+            {
+                ret = FlagStatus::flagIsLocked; // locked
+            }
+            else
+            {
+                ret = FlagStatus::flagIsValid; // valid
+            }
+            break;
+
+        case MDR2SMBIOSStatusEnum::mdr2Updating:
+        case MDR2SMBIOSStatusEnum::mdr2Init:
+            ret = FlagStatus::flagIsInvalid; // invalid
+            break;
+
+        default:
+            break;
+    }
+
+    return static_cast<uint8_t>(ret);
+}
+
+std::vector<uint8_t> MDR_V2::getDataInformation(uint8_t idIndex)
+{
+    std::vector<uint8_t> responseInfo;
+    responseInfo.push_back(mdr2Version);
+
+    if (idIndex >= maxDirEntries)
+    {
+        throw sdbusplus::xyz::openbmc_project::Smbios::MDR_V2::Error::
+            InvalidParameter();
+    }
+
+    for (uint8_t index = 0; index < sizeof(DataIdStruct); index++)
+    {
+        responseInfo.push_back(
+            smbiosDir.dir[idIndex].common.id.dataInfo[index]);
+    }
+    responseInfo.push_back(smbiosValidFlag(idIndex));
+    responseInfo.push_back(smbiosDir.dir[idIndex].common.size);
+    responseInfo.push_back(smbiosDir.dir[idIndex].common.dataVersion);
+    responseInfo.push_back(smbiosDir.dir[idIndex].common.timestamp);
+
+    return responseInfo;
+}
+
+bool MDR_V2::sendDirectoryInformation(uint8_t dirVersion, uint8_t dirIndex,
+                                      uint8_t returnedEntries,
+                                      uint8_t remainingEntries,
+                                      std::vector<uint8_t> dirEntry)
+{
+}
+
+bool MDR_V2::sendDataInformation(uint8_t idIndex, uint8_t flag,
+                                 uint32_t dataLen, uint32_t dataVer,
+                                 uint32_t timeStamp)
+{
+    if (idIndex >= maxDirEntries)
+    {
+        throw sdbusplus::xyz::openbmc_project::Smbios::MDR_V2::Error::
+            InvalidParameter();
+    }
+    int entryChanged = 0;
+    if (smbiosDir.dir[idIndex].common.dataSetSize != dataLen)
+    {
+        entryChanged++;
+        smbiosDir.dir[idIndex].common.dataSetSize = dataLen;
+    }
+
+    if (smbiosDir.dir[idIndex].common.dataVersion != dataVer)
+    {
+        entryChanged++;
+        smbiosDir.dir[idIndex].common.dataVersion = dataVer;
+    }
+
+    if (smbiosDir.dir[idIndex].common.timestamp != timeStamp)
+    {
+        entryChanged++;
+        smbiosDir.dir[idIndex].common.timestamp = timeStamp;
+    }
+    if (entryChanged == 0)
+    {
+        return false;
+    }
+    return true;
+}
+
+int MDR_V2::findIdIndex(std::vector<uint8_t> dataInfo)
+{
+    if (dataInfo.size() != sizeof(DataIdStruct))
+    {
+        phosphor::logging::log<phosphor::logging::level::ERR>(
+            "Length of dataInfo invalid");
+        throw sdbusplus::xyz::openbmc_project::Smbios::MDR_V2::Error::
+            InvalidParameter();
+    }
+    std::array<uint8_t, 16> arrayDataInfo;
+
+    std::copy(dataInfo.begin(), dataInfo.end(), arrayDataInfo.begin());
+
+    for (int index = 0; index < smbiosDir.dirEntries; index++)
+    {
+        int info = 0;
+        for (; info < arrayDataInfo.size(); info++)
+        {
+            if (arrayDataInfo[info] !=
+                smbiosDir.dir[index].common.id.dataInfo[info])
+            {
+                break;
+            }
+        }
+        if (info == arrayDataInfo.size())
+        {
+            return index;
+        }
+    }
+    throw sdbusplus::xyz::openbmc_project::Smbios::MDR_V2::Error::InvalidId();
+}
+
+uint8_t MDR_V2::directoryEntries(uint8_t value)
+{
+    value = smbiosDir.dirEntries;
+    return sdbusplus::xyz::openbmc_project::Smbios::server::MDR_V2::
+        directoryEntries(value);
+}
+
+bool MDR_V2::agentSynchronizeData()
+{
+}
+
+std::vector<uint32_t> MDR_V2::synchronizeDirectoryCommonData(uint8_t idIndex,
+                                                             uint32_t size)
+{
+}
+
+} // namespace smbios
+} // namespace phosphor
diff --git a/src/mdrv2_main.cpp b/src/mdrv2_main.cpp
new file mode 100644
index 0000000..42b874f
--- /dev/null
+++ b/src/mdrv2_main.cpp
@@ -0,0 +1,48 @@
+/*
+// 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 "mdrv2.hpp"
+
+#include <systemd/sd-event.h>
+
+#include <phosphor-logging/elog-errors.hpp>
+#include <phosphor-logging/elog.hpp>
+
+int main(void)
+{
+    sd_event* events = nullptr;
+    sd_event_default(&events);
+    sdbusplus::bus::bus bus = sdbusplus::bus::new_default();
+    sdbusplus::server::manager::manager objManager(
+        bus, "/xyz/openbmc_project/inventory");
+    bus.attach_event(events, SD_EVENT_PRIORITY_NORMAL);
+    bus.request_name("xyz.openbmc_project.Smbios.MDR_V2");
+
+    phosphor::smbios::MDR_V2 mdrV2(bus, mdrV2Path, events);
+
+    while (true)
+    {
+        int r = sd_event_run(events, (uint64_t)-1);
+        if (r < 0)
+        {
+            phosphor::logging::log<phosphor::logging::level::ERR>(
+                "Failure processing request",
+                phosphor::logging::entry("errno=0x%X", -r));
+            return -1;
+        }
+    }
+    return 0;
+}