Move downstream package to upstream

Use upstream cpu interface

Signed-off-by: Zhikui Ren <zhikui.ren@intel.com>
Change-Id: I490482b212df4b73cbdedaba0bc5fefa229a5489
diff --git a/src/cpu.cpp b/src/cpu.cpp
index ffef3cf..90c1191 100644
--- a/src/cpu.cpp
+++ b/src/cpu.cpp
@@ -16,7 +16,7 @@
 
 #include "cpu.hpp"
 
-#include <iostream>
+#include <bitset>
 #include <map>
 
 namespace phosphor
@@ -24,169 +24,67 @@
 namespace smbios
 {
 
-void Cpu::cpuSocket(const uint8_t positionNum, const uint8_t structLen,
-                    uint8_t* dataIn)
+void Cpu::socket(const uint8_t positionNum, const uint8_t structLen,
+                 uint8_t* dataIn)
 {
     std::string result = positionToString(positionNum, structLen, dataIn);
 
-    processorSocket(result);
+    processor::socket(result);
 }
 
-std::string Cpu::processorSocket(std::string value)
-{
-    return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Cpu::
-        processorSocket(value);
-}
-
-void Cpu::cpuType(const uint8_t value)
-{
-    std::map<uint8_t, const char*>::const_iterator it =
-        processorTypeTable.find(value);
-    if (it == processorTypeTable.end())
-    {
-        processorType("Unknown Processor Type");
-    }
-    else
-    {
-        processorType(it->second);
-    }
-}
-
-std::string Cpu::processorType(std::string value)
-{
-    return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Cpu::
-        processorType(value);
-}
-
-void Cpu::cpuFamily(const uint8_t value)
+void Cpu::family(const uint8_t value)
 {
     std::map<uint8_t, const char*>::const_iterator it = familyTable.find(value);
     if (it == familyTable.end())
     {
-        processorFamily("Unknown Processor Family");
+        processor::family("Unknown Processor Family");
     }
     else
     {
-        processorFamily(it->second);
+        processor::family(it->second);
     }
 }
 
-std::string Cpu::processorFamily(std::string value)
-{
-    return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Cpu::
-        processorFamily(value);
-}
-
-void Cpu::cpuManufacturer(const uint8_t positionNum, const uint8_t structLen,
-                          uint8_t* dataIn)
+void Cpu::manufacturer(const uint8_t positionNum, const uint8_t structLen,
+                       uint8_t* dataIn)
 {
     std::string result = positionToString(positionNum, structLen, dataIn);
 
-    manufacturer(result);
+    asset::manufacturer(result);
 }
 
-std::string Cpu::manufacturer(std::string value)
-{
-    return sdbusplus::xyz::openbmc_project::Inventory::Decorator::server::
-        Asset::manufacturer(value);
-}
-
-uint32_t Cpu::processorId(uint32_t value)
-{
-    return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Cpu::
-        processorId(value);
-}
-
-void Cpu::cpuVersion(const uint8_t positionNum, const uint8_t structLen,
-                     uint8_t* dataIn)
+void Cpu::version(const uint8_t positionNum, const uint8_t structLen,
+                  uint8_t* dataIn)
 {
     std::string result;
 
     result = positionToString(positionNum, structLen, dataIn);
 
-    processorVersion(result);
+    rev::version(result);
 }
 
-std::string Cpu::processorVersion(std::string value)
+void Cpu::characteristics(uint16_t value)
 {
-    return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Cpu::
-        processorVersion(value);
-}
+    std::vector<processor::Capability> result;
+    std::optional<processor::Capability> cap;
 
-uint16_t Cpu::processorMaxSpeed(uint16_t value)
-{
-    return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Cpu::
-        processorMaxSpeed(value);
-}
-
-void Cpu::cpuCharacteristics(uint16_t value)
-{
-    std::string result = "";
-    for (uint8_t index = 0; index < (8 * sizeof(value)); index++)
+    std::bitset<16> charBits = value;
+    for (uint8_t index = 0; index < charBits.size(); index++)
     {
-        if (value & 0x01)
+        if (charBits.test(index))
         {
-            result += characteristicsTable[index];
+            if (cap = characteristicsTable[index])
+            {
+                result.emplace_back(*cap);
+            }
         }
-        value >>= 1;
     }
 
-    processorCharacteristics(result);
-}
-
-std::string Cpu::processorCharacteristics(std::string value)
-{
-    return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Cpu::
-        processorCharacteristics(value);
-}
-
-uint16_t Cpu::processorCoreCount(uint16_t value)
-{
-    return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Cpu::
-        processorCoreCount(value);
-}
-
-uint16_t Cpu::processorThreadCount(uint16_t value)
-{
-    return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Cpu::
-        processorThreadCount(value);
-}
-
-static constexpr const uint8_t populateMask = 1 << 6;
-static constexpr const uint8_t statusMask = 0x07;
-void Cpu::cpuStatus(uint8_t value)
-{
-    if (!(value & populateMask))
-    {
-        present(false);
-        functional(false);
-        return;
-    }
-    present(true);
-    if ((value & statusMask) == 1)
-    {
-        functional(true);
-    }
-    else
-    {
-        functional(false);
-    }
-}
-
-bool Cpu::present(bool value)
-{
-    return sdbusplus::xyz::openbmc_project::Inventory::server::Item::present(
-        value);
-}
-
-bool Cpu::functional(bool value)
-{
-    return sdbusplus::xyz::openbmc_project::State::Decorator::server::
-        OperationalStatus::functional(value);
+    processor::characteristics(result);
 }
 
 static constexpr uint8_t maxOldVersionCount = 0xff;
-void Cpu::processorInfoUpdate(void)
+void Cpu::infoUpdate(void)
 {
     uint8_t* dataIn = storage;
 
@@ -212,36 +110,34 @@
 
     auto cpuInfo = reinterpret_cast<struct ProcessorInfo*>(dataIn);
 
-    cpuSocket(cpuInfo->socketDesignation, cpuInfo->length,
-              dataIn);               // offset 4h
-    cpuType(cpuInfo->processorType); // offset 5h
-    cpuFamily(cpuInfo->family);      // offset 6h
-    cpuManufacturer(cpuInfo->manufacturer, cpuInfo->length,
-                    dataIn);                               // offset 7h
-    processorId(cpuInfo->id);                              // offset 8h
-    cpuVersion(cpuInfo->version, cpuInfo->length, dataIn); // offset 10h
-    processorMaxSpeed(cpuInfo->maxSpeed);                  // offset 14h
-    if (cpuInfo->coreCount < maxOldVersionCount)           // offset 23h or 2Ah
+    socket(cpuInfo->socketDesignation, cpuInfo->length,
+           dataIn); // offset 4h
+    // this class is for type CPU  //offset 5h
+    family(cpuInfo->family); // offset 6h
+    manufacturer(cpuInfo->manufacturer, cpuInfo->length,
+                 dataIn);                               // offset 7h
+    id(cpuInfo->id);                                    // offset 8h
+    version(cpuInfo->version, cpuInfo->length, dataIn); // offset 10h
+    maxSpeedInMhz(cpuInfo->maxSpeed);                   // offset 14h
+    if (cpuInfo->coreCount < maxOldVersionCount)        // offset 23h or 2Ah
     {
-        processorCoreCount(cpuInfo->coreCount);
+        coreCount(cpuInfo->coreCount);
     }
     else
     {
-        processorCoreCount(cpuInfo->coreCount2);
+        coreCount(cpuInfo->coreCount2);
     }
 
     if (cpuInfo->threadCount < maxOldVersionCount) // offset 25h or 2Eh)
     {
-        processorThreadCount(cpuInfo->threadCount);
+        threadCount(cpuInfo->threadCount);
     }
     else
     {
-        processorThreadCount(cpuInfo->threadCount2);
+        threadCount(cpuInfo->threadCount2);
     }
 
-    cpuCharacteristics(cpuInfo->characteristics); // offset 26h
-
-    cpuStatus(cpuInfo->status);
+    characteristics(cpuInfo->characteristics); // offset 26h
 }
 
 } // namespace smbios
diff --git a/src/cpuinfo_main.cpp b/src/cpuinfo_main.cpp
new file mode 100644
index 0000000..dc3a8e8
--- /dev/null
+++ b/src/cpuinfo_main.cpp
@@ -0,0 +1,425 @@
+/*
+// Copyright (c) 2020 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 "cpuinfo.hpp"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <sys/ioctl.h>
+
+#include <boost/asio/io_service.hpp>
+#include <boost/asio/steady_timer.hpp>
+
+#include <optional>
+#include <sstream>
+#include <string>
+
+extern "C"
+{
+#include <i2c/smbus.h>
+#include <linux/i2c-dev.h>
+}
+
+#include <peci.h>
+
+#include <phosphor-logging/log.hpp>
+#include <sdbusplus/asio/object_server.hpp>
+
+namespace phosphor
+{
+namespace cpu_info
+{
+
+static constexpr const char* cpuPath =
+    "/xyz/openbmc_project/inventory/system/chassis/motherboard/cpu";
+static constexpr const char* cpuInterfaceName =
+    "xyz.openbmc_project.Inventory.Decorator.Asset";
+static constexpr const char* cpuProcessName =
+    "xyz.openbmc_project.Smbios.MDR_V2";
+
+struct ProcessorInfo
+{
+    uint64_t ppin;
+    std::string sspec;
+};
+
+using CPUMap =
+    boost::container::flat_map<size_t,
+                               std::pair<int, std::shared_ptr<CPUInfo>>>;
+
+static CPUMap cpuMap = {};
+
+static std::unique_ptr<sdbusplus::bus::match_t> cpuUpdatedMatch = nullptr;
+
+static std::optional<std::string> readSSpec(uint8_t bus, uint8_t slaveAddr,
+                                            uint8_t regAddr, size_t count)
+{
+    unsigned long funcs = 0;
+    std::string devPath = "/dev/i2c-" + std::to_string(bus);
+
+    int fd = ::open(devPath.c_str(), O_RDWR);
+    if (fd < 0)
+    {
+        phosphor::logging::log<phosphor::logging::level::ERR>(
+            "Error in open!",
+            phosphor::logging::entry("PATH=%s", devPath.c_str()),
+            phosphor::logging::entry("SLAVEADDR=0x%x", slaveAddr));
+        return std::nullopt;
+    }
+
+    if (::ioctl(fd, I2C_FUNCS, &funcs) < 0)
+    {
+        phosphor::logging::log<phosphor::logging::level::ERR>(
+            "Error in I2C_FUNCS!",
+            phosphor::logging::entry("PATH=%s", devPath.c_str()),
+            phosphor::logging::entry("SLAVEADDR=0x%x", slaveAddr));
+        ::close(fd);
+        return std::nullopt;
+    }
+
+    if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE_DATA))
+    {
+        phosphor::logging::log<phosphor::logging::level::ERR>(
+            "i2c bus does not support read!",
+            phosphor::logging::entry("PATH=%s", devPath.c_str()),
+            phosphor::logging::entry("SLAVEADDR=0x%x", slaveAddr));
+        ::close(fd);
+        return std::nullopt;
+    }
+
+    if (::ioctl(fd, I2C_SLAVE_FORCE, slaveAddr) < 0)
+    {
+        phosphor::logging::log<phosphor::logging::level::ERR>(
+            "Error in I2C_SLAVE_FORCE!",
+            phosphor::logging::entry("PATH=%s", devPath.c_str()),
+            phosphor::logging::entry("SLAVEADDR=0x%x", slaveAddr));
+        ::close(fd);
+        return std::nullopt;
+    }
+
+    int value = 0;
+    std::string sspec;
+    sspec.reserve(count);
+
+    for (size_t i = 0; i < count; i++)
+    {
+        value = ::i2c_smbus_read_byte_data(fd, regAddr++);
+        if (value < 0)
+        {
+            phosphor::logging::log<phosphor::logging::level::ERR>(
+                "Error in i2c read!",
+                phosphor::logging::entry("PATH=%s", devPath.c_str()),
+                phosphor::logging::entry("SLAVEADDR=0x%x", slaveAddr));
+            ::close(fd);
+            return std::nullopt;
+        }
+        if (!std::isprint(static_cast<unsigned char>(value)))
+        {
+            phosphor::logging::log<phosphor::logging::level::ERR>(
+                "Non printable value in sspec, ignored.");
+            continue;
+        }
+        sspec.push_back(static_cast<unsigned char>(value));
+    }
+    ::close(fd);
+    return sspec;
+}
+
+// PECI Client Address Map
+static void getPECIAddrMap(CPUMap& cpuMap)
+{
+    int idx = 0;
+    for (size_t i = MIN_CLIENT_ADDR; i <= MAX_CLIENT_ADDR; i++)
+    {
+        if (peci_Ping(i) == PECI_CC_SUCCESS)
+        {
+            cpuMap.emplace(std::make_pair(i, std::make_pair(idx, nullptr)));
+            idx++;
+        }
+    }
+}
+
+static std::shared_ptr<CPUInfo>
+    createCPUInfo(std::shared_ptr<sdbusplus::asio::connection>& conn,
+                  const int& cpu)
+{
+    std::string path = cpuPath + std::to_string(cpu);
+    std::shared_ptr<CPUInfo> cpuInfo = std::make_shared<CPUInfo>(
+        static_cast<sdbusplus::bus::bus&>(*conn), path);
+    return cpuInfo;
+}
+
+static void setAssetProperty(
+    std::shared_ptr<sdbusplus::asio::connection>& conn, const int& cpu,
+    const std::vector<std::pair<std::string, std::string>>& propValues)
+{
+
+    const std::string objectPath = cpuPath + std::to_string(cpu);
+    for (const auto& prop : propValues)
+    {
+        conn->async_method_call(
+            [](const boost::system::error_code ec) {
+                // Use "Set" method to set the property value.
+                if (ec)
+                {
+                    phosphor::logging::log<phosphor::logging::level::ERR>(
+                        "Cannot get CPU property!");
+                    return;
+                }
+            },
+            cpuProcessName, objectPath.c_str(),
+            "org.freedesktop.DBus.Properties", "Set", cpuInterfaceName,
+            prop.first.c_str(), std::variant<std::string>{prop.second});
+    }
+}
+
+static void createCpuUpdatedMatch(
+    std::shared_ptr<sdbusplus::asio::connection>& conn, const int& cpu,
+    const std::vector<std::pair<std::string, std::string>>& propValues)
+{
+    if (cpuUpdatedMatch)
+    {
+        return;
+    }
+
+    const std::string objectPath = cpuPath + std::to_string(cpu);
+
+    cpuUpdatedMatch = std::make_unique<sdbusplus::bus::match::match>(
+        static_cast<sdbusplus::bus::bus&>(*conn),
+        sdbusplus::bus::match::rules::interfacesAdded() +
+            sdbusplus::bus::match::rules::argNpath(0, objectPath.c_str()),
+        [&conn, cpu, propValues](sdbusplus::message::message& msg) {
+            std::string objectName;
+            boost::container::flat_map<
+                std::string,
+                boost::container::flat_map<std::string,
+                                           std::variant<std::string, uint64_t>>>
+                msgData;
+
+            msg.read(objectName, msgData);
+
+            // Check for xyz.openbmc_project.Inventory.Item.Cpu
+            // interface match
+            auto intfFound = msgData.find(cpuInterfaceName);
+            if (msgData.end() != intfFound)
+            {
+                setAssetProperty(conn, cpu, propValues);
+            }
+        });
+}
+
+// constants for reading QDF string from PIROM
+// Currently, they are the same for platforms with icx
+// \todo: move into configuration file to be more robust
+static constexpr uint8_t i2cBus = 13;
+static constexpr uint8_t slaveAddr0 = 0x50;
+static constexpr uint8_t regAddr = 0xf;
+static constexpr uint8_t sspecSize = 4;
+
+static void getProcessorInfo(std::shared_ptr<sdbusplus::asio::connection>& conn,
+                             sdbusplus::asio::object_server& objServer,
+                             CPUMap& cpuMap)
+{
+
+    for (auto& cpu : cpuMap)
+    {
+        uint8_t cc = 0;
+        CPUModel model{};
+        uint8_t stepping = 0;
+
+        /// \todo in a follwup patch
+        // CPUInfo will be updated as the centrol place for CPU information
+        // std::shared_ptr<CPUInfo> cpuInfo =
+        //    createCPUInfo(conn, cpu.second.first);
+        // cpu.second.second = cpuInfo;
+
+        if (peci_GetCPUID(cpu.first, &model, &stepping, &cc) != PECI_CC_SUCCESS)
+        {
+            phosphor::logging::log<phosphor::logging::level::ERR>(
+                "Cannot get CPUID!",
+                phosphor::logging::entry("PECIADDR=0x%x", cpu.first));
+            continue;
+        }
+
+        switch (model)
+        {
+            case icx:
+            {
+                // get processor ID
+                static constexpr uint8_t u8Size = 4; // default to a DWORD
+                static constexpr uint8_t u8PPINPkgIndex = 19;
+                static constexpr uint16_t u16PPINPkgParamHigh = 2;
+                static constexpr uint16_t u16PPINPkgParamLow = 1;
+                uint64_t cpuPPIN = 0;
+                uint32_t u32PkgValue = 0;
+
+                int ret = peci_RdPkgConfig(cpu.first, u8PPINPkgIndex,
+                                           u16PPINPkgParamLow, u8Size,
+                                           (uint8_t*)&u32PkgValue, &cc);
+                if (0 != ret)
+                {
+                    phosphor::logging::log<phosphor::logging::level::ERR>(
+                        "peci read package config failed at address",
+                        phosphor::logging::entry("PECIADDR=0x%x", cpu.first),
+                        phosphor::logging::entry("CC=0x%x", cc));
+                    u32PkgValue = 0;
+                }
+
+                cpuPPIN = u32PkgValue;
+                ret = peci_RdPkgConfig(cpu.first, u8PPINPkgIndex,
+                                       u16PPINPkgParamHigh, u8Size,
+                                       (uint8_t*)&u32PkgValue, &cc);
+                if (0 != ret)
+                {
+                    phosphor::logging::log<phosphor::logging::level::ERR>(
+                        "peci read package config failed at address",
+                        phosphor::logging::entry("PECIADDR=0x%x", cpu.first),
+                        phosphor::logging::entry("CC=0x%x", cc));
+                    cpuPPIN = 0;
+                    u32PkgValue = 0;
+                }
+
+                cpuPPIN |= static_cast<uint64_t>(u32PkgValue) << 32;
+
+                std::vector<std::pair<std::string, std::string>> values;
+
+                // set SerialNumber if cpuPPIN is valid
+                if (0 != cpuPPIN)
+                {
+                    std::stringstream stream;
+                    stream << std::hex << cpuPPIN;
+                    std::string serialNumber(stream.str());
+                    // cpuInfo->serialNumber(serialNumber);
+                    values.emplace_back(
+                        std::make_pair("SerialNumber", serialNumber));
+                }
+
+                // assuming the slaveAddress will be incrementing like peci
+                // client address
+                std::optional<std::string> sspec = readSSpec(
+                    i2cBus, static_cast<uint8_t>(slaveAddr0 + cpu.second.first),
+                    regAddr, sspecSize);
+                // cpuInfo->model(sspec.value_or(""));
+                values.emplace_back(
+                    std::make_pair("Model", sspec.value_or("")));
+
+                /// \todo in followup patch
+                // CPUInfo is created by this service
+                // update the below logic, which is needed because smbios
+                // service creates the cpu object
+                createCpuUpdatedMatch(conn, cpu.second.first, values);
+                setAssetProperty(conn, cpu.second.first, values);
+                break;
+            }
+            default:
+                phosphor::logging::log<phosphor::logging::level::INFO>(
+                    "in-compatible cpu for cpu asset info");
+                break;
+        }
+    }
+}
+
+static bool isPECIAvailable(void)
+{
+    for (size_t i = MIN_CLIENT_ADDR; i <= MAX_CLIENT_ADDR; i++)
+    {
+        if (peci_Ping(i) == PECI_CC_SUCCESS)
+        {
+            return true;
+        }
+    }
+    return false;
+}
+
+static void
+    peciAvailableCheck(boost::asio::steady_timer& peciWaitTimer,
+                       boost::asio::io_service& io,
+                       std::shared_ptr<sdbusplus::asio::connection>& conn,
+                       sdbusplus::asio::object_server& objServer)
+{
+    bool peciAvailable = isPECIAvailable();
+    if (peciAvailable)
+    {
+        // get the PECI client address list
+        getPECIAddrMap(cpuMap);
+        getProcessorInfo(conn, objServer, cpuMap);
+    }
+    if (!peciAvailable || !cpuMap.size())
+    {
+        peciWaitTimer.expires_after(
+            std::chrono::seconds(6 * peciCheckInterval));
+        peciWaitTimer.async_wait([&peciWaitTimer, &io, &conn, &objServer](
+                                     const boost::system::error_code& ec) {
+            if (ec)
+            {
+                // operation_aborted is expected if timer is canceled
+                // before completion.
+                if (ec != boost::asio::error::operation_aborted)
+                {
+                    phosphor::logging::log<phosphor::logging::level::ERR>(
+                        "PECI Available Check async_wait failed",
+                        phosphor::logging::entry("EC=0x%x", ec.value()));
+                }
+                return;
+            }
+            peciAvailableCheck(peciWaitTimer, io, conn, objServer);
+        });
+    }
+}
+
+} // namespace cpu_info
+} // namespace phosphor
+
+int main(int argc, char* argv[])
+{
+    // setup connection to dbus
+    boost::asio::io_service io;
+    std::shared_ptr<sdbusplus::asio::connection> conn =
+        std::make_shared<sdbusplus::asio::connection>(io);
+
+    // CPUInfo Object
+    conn->request_name(phosphor::cpu_info::cpuInfoObject);
+    sdbusplus::asio::object_server server =
+        sdbusplus::asio::object_server(conn);
+    sdbusplus::bus::bus& bus = static_cast<sdbusplus::bus::bus&>(*conn);
+    sdbusplus::server::manager::manager objManager(
+        bus, "/xyz/openbmc_project/inventory");
+
+    // Start the PECI check loop
+    boost::asio::steady_timer peciWaitTimer(
+        io, std::chrono::seconds(phosphor::cpu_info::peciCheckInterval));
+    peciWaitTimer.async_wait([&peciWaitTimer, &io, &conn,
+                              &server](const boost::system::error_code& ec) {
+        if (ec)
+        {
+            // operation_aborted is expected if timer is canceled
+            // before completion.
+            if (ec != boost::asio::error::operation_aborted)
+            {
+                phosphor::logging::log<phosphor::logging::level::ERR>(
+                    "PECI Available Check async_wait failed ",
+                    phosphor::logging::entry("EC=0x%x", ec.value()));
+            }
+            return;
+        }
+        phosphor::cpu_info::peciAvailableCheck(peciWaitTimer, io, conn, server);
+    });
+
+    io.run();
+
+    return 0;
+}
diff --git a/src/mdrv2.cpp b/src/mdrv2.cpp
index 07dcc6b..9a73c65 100644
--- a/src/mdrv2.cpp
+++ b/src/mdrv2.cpp
@@ -1,5 +1,5 @@
 /*
-// Copyright (c) 2019 Intel Corporation
+// Copyright (c) 2018 Intel Corporation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -18,11 +18,12 @@
 
 #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>
 
+#include <fstream>
+
 namespace phosphor
 {
 namespace smbios
@@ -154,6 +155,17 @@
     return static_cast<uint8_t>(ret);
 }
 
+// If source variable size is 4 bytes (uint32_t) and destination is Vector type
+// is 1 byte (uint8_t), then by using this API can copy data byte by byte. For
+// Example copying data from smbiosDir.dir[idIndex].common.size and
+// smbiosDir.dir[idIndex].common.timestamp to vector variable responseInfo
+template <typename T>
+void appendReversed(std::vector<uint8_t>& vector, const T& value)
+{
+    auto data = reinterpret_cast<const uint8_t*>(&value);
+    std::reverse_copy(data, data + sizeof(value), std::back_inserter(vector));
+}
+
 std::vector<uint8_t> MDR_V2::getDataInformation(uint8_t idIndex)
 {
     std::vector<uint8_t> responseInfo;
@@ -170,14 +182,67 @@
         responseInfo.push_back(
             smbiosDir.dir[idIndex].common.id.dataInfo[index]);
     }
+
     responseInfo.push_back(smbiosValidFlag(idIndex));
-    responseInfo.push_back(smbiosDir.dir[idIndex].common.size);
+    appendReversed(responseInfo, smbiosDir.dir[idIndex].common.size);
     responseInfo.push_back(smbiosDir.dir[idIndex].common.dataVersion);
-    responseInfo.push_back(smbiosDir.dir[idIndex].common.timestamp);
+    appendReversed(responseInfo, smbiosDir.dir[idIndex].common.timestamp);
 
     return responseInfo;
 }
 
+bool MDR_V2::readDataFromFlash(MDRSMBIOSHeader* mdrHdr, uint8_t* data)
+{
+    if (mdrHdr == nullptr)
+    {
+        phosphor::logging::log<phosphor::logging::level::ERR>(
+            "Read data from flash error - Invalid mdr header");
+        return false;
+    }
+    if (data == nullptr)
+    {
+        phosphor::logging::log<phosphor::logging::level::ERR>(
+            "Read data from flash error - Invalid data point");
+        return false;
+    }
+    std::ifstream smbiosFile(mdrType2File, std::ios_base::binary);
+    if (!smbiosFile.good())
+    {
+        phosphor::logging::log<phosphor::logging::level::ERR>(
+            "Read data from flash error - Open MDRV2 table file failure");
+        return false;
+    }
+    smbiosFile.clear();
+    smbiosFile.seekg(0, std::ios_base::end);
+    int fileLength = smbiosFile.tellg();
+    smbiosFile.seekg(0, std::ios_base::beg);
+    if (fileLength < sizeof(MDRSMBIOSHeader))
+    {
+        phosphor::logging::log<phosphor::logging::level::ERR>(
+            "MDR V2 file size is smaller than mdr header");
+        return false;
+    }
+    smbiosFile.read(reinterpret_cast<char*>(mdrHdr), sizeof(MDRSMBIOSHeader));
+    if (mdrHdr->dataSize > smbiosTableStorageSize)
+    {
+        phosphor::logging::log<phosphor::logging::level::ERR>(
+            "Data size out of limitation");
+        smbiosFile.close();
+        return false;
+    }
+    fileLength -= sizeof(MDRSMBIOSHeader);
+    if (fileLength < mdrHdr->dataSize)
+    {
+        smbiosFile.read(reinterpret_cast<char*>(data), fileLength);
+    }
+    else
+    {
+        smbiosFile.read(reinterpret_cast<char*>(data), mdrHdr->dataSize);
+    }
+    smbiosFile.close();
+    return true;
+}
+
 bool MDR_V2::sendDirectoryInformation(uint8_t dirVersion, uint8_t dirIndex,
                                       uint8_t returnedEntries,
                                       uint8_t remainingEntries,
@@ -236,58 +301,6 @@
     return teminate;
 }
 
-bool MDR_V2::readDataFromFlash(MDRSMBIOSHeader* mdrHdr, uint8_t* data)
-{
-    if (mdrHdr == nullptr)
-    {
-        phosphor::logging::log<phosphor::logging::level::ERR>(
-            "Read data from flash error - Invalid mdr header");
-        return false;
-    }
-    if (data == nullptr)
-    {
-        phosphor::logging::log<phosphor::logging::level::ERR>(
-            "Read data from flash error - Invalid data point");
-        return false;
-    }
-    std::ifstream smbiosFile(mdrType2File, std::ios_base::binary);
-    if (!smbiosFile.good())
-    {
-        phosphor::logging::log<phosphor::logging::level::ERR>(
-            "Read data from flash error - Open MDRV2 table file failure");
-        return false;
-    }
-    smbiosFile.clear();
-    smbiosFile.seekg(0, std::ios_base::end);
-    int fileLength = smbiosFile.tellg();
-    smbiosFile.seekg(0, std::ios_base::beg);
-    if (fileLength < sizeof(MDRSMBIOSHeader))
-    {
-        phosphor::logging::log<phosphor::logging::level::ERR>(
-            "MDR V2 file size is smaller than mdr header");
-        return false;
-    }
-    smbiosFile.read(reinterpret_cast<char*>(mdrHdr), sizeof(MDRSMBIOSHeader));
-    if (mdrHdr->dataSize > smbiosTableStorageSize)
-    {
-        phosphor::logging::log<phosphor::logging::level::ERR>(
-            "Data size out of limitation");
-        smbiosFile.close();
-        return false;
-    }
-    fileLength -= sizeof(MDRSMBIOSHeader);
-    if (fileLength < mdrHdr->dataSize)
-    {
-        smbiosFile.read(reinterpret_cast<char*>(data), fileLength);
-    }
-    else
-    {
-        smbiosFile.read(reinterpret_cast<char*>(data), mdrHdr->dataSize);
-    }
-    smbiosFile.close();
-    return true;
-}
-
 bool MDR_V2::sendDataInformation(uint8_t idIndex, uint8_t flag,
                                  uint32_t dataLen, uint32_t dataVer,
                                  uint32_t timeStamp)
@@ -380,6 +393,8 @@
             bus, path, index, smbiosDir.dir[smbiosDirIndex].dataStorage));
     }
 
+#ifdef DIMM_DBUS
+
     dimms.clear();
 
     num = getTotalDimmSlot();
@@ -397,6 +412,8 @@
             bus, path, index, smbiosDir.dir[smbiosDirIndex].dataStorage));
     }
 
+#endif
+
     system.reset();
     system = std::make_unique<System>(
         bus, systemPath, smbiosDir.dir[smbiosDirIndex].dataStorage);
@@ -490,7 +507,6 @@
         smbiosDir.dir[smbiosDirIndex].stage = MDR2SMBIOSStatusEnum::mdr2Loaded;
         smbiosDir.dir[smbiosDirIndex].lock = MDR2DirLockEnum::mdr2DirUnlock;
     }
-    timer.stop();
     return true;
 }
 
@@ -505,9 +521,105 @@
     result.push_back(smbiosDir.dir[idIndex].common.dataVersion);
     result.push_back(smbiosDir.dir[idIndex].common.timestamp);
 
-    timer.start(usec);
+    timer.expires_after(usec);
+    timer.async_wait([this](boost::system::error_code ec) {
+        if (ec || this == nullptr)
+        {
+            phosphor::logging::log<phosphor::logging::level::ERR>(
+                "Timer Error!");
+            return;
+        }
+        agentSynchronizeData();
+    });
     return result;
 }
 
+std::vector<boost::container::flat_map<std::string, RecordVariant>>
+    MDR_V2::getRecordType(size_t type)
+{
+
+    std::vector<boost::container::flat_map<std::string, RecordVariant>> ret;
+    if (type == memoryDeviceType)
+    {
+
+        uint8_t* dataIn = smbiosDir.dir[smbiosDirIndex].dataStorage;
+
+        if (dataIn == nullptr)
+        {
+            throw std::runtime_error("Data not populated");
+        }
+
+        do
+        {
+            dataIn =
+                getSMBIOSTypePtr(dataIn, memoryDeviceType, sizeof(MemoryInfo));
+            if (dataIn == nullptr)
+            {
+                break;
+            }
+            boost::container::flat_map<std::string, RecordVariant>& record =
+                ret.emplace_back();
+
+            auto memoryInfo = reinterpret_cast<MemoryInfo*>(dataIn);
+
+            record["Type"] = memoryInfo->type;
+            record["Length"] = memoryInfo->length;
+            record["Handle"] = uint16_t(memoryInfo->handle);
+            record["Physical Memory Array Handle"] =
+                uint16_t(memoryInfo->phyArrayHandle);
+            record["Memory Error Information Handle"] =
+                uint16_t(memoryInfo->errInfoHandle);
+            record["Total Width"] = uint16_t(memoryInfo->totalWidth);
+            record["Data Width"] = uint16_t(memoryInfo->dataWidth);
+            record["Size"] = uint16_t(memoryInfo->size);
+            record["Form Factor"] = memoryInfo->formFactor;
+            record["Device Set"] = memoryInfo->deviceSet;
+            record["Device Locator"] = positionToString(
+                memoryInfo->deviceLocator, memoryInfo->length, dataIn);
+            record["Bank Locator"] = positionToString(
+                memoryInfo->bankLocator, memoryInfo->length, dataIn);
+            record["Memory Type"] = memoryInfo->memoryType;
+            record["Type Detail"] = uint16_t(memoryInfo->typeDetail);
+            record["Speed"] = uint16_t(memoryInfo->speed);
+            record["Manufacturer"] = positionToString(
+                memoryInfo->manufacturer, memoryInfo->length, dataIn);
+            record["Serial Number"] = positionToString(
+                memoryInfo->serialNum, memoryInfo->length, dataIn);
+            record["Asset Tag"] = positionToString(memoryInfo->assetTag,
+                                                   memoryInfo->length, dataIn);
+            record["Part Number"] = positionToString(
+                memoryInfo->partNum, memoryInfo->length, dataIn);
+            record["Attributes"] = memoryInfo->attributes;
+            record["Extended Size"] = uint32_t(memoryInfo->extendedSize);
+            record["Configured Memory Speed"] =
+                uint32_t(memoryInfo->confClockSpeed);
+            record["Minimum voltage"] = uint16_t(memoryInfo->minimumVoltage);
+            record["Maximum voltage"] = uint16_t(memoryInfo->maximumVoltage);
+            record["Configured voltage"] =
+                uint16_t(memoryInfo->configuredVoltage);
+            record["Memory Technology"] = memoryInfo->memoryTechnology;
+            record["Memory Operating Mode Capabilty"] =
+                uint16_t(memoryInfo->memoryOperatingModeCap);
+            record["Firmare Version"] = memoryInfo->firwareVersion;
+            record["Module Manufacturer ID"] =
+                uint16_t(memoryInfo->modelManufId);
+            record["Module Product ID"] = uint16_t(memoryInfo->modelProdId);
+            record["Memory Subsystem Controller Manufacturer ID"] =
+                uint16_t(memoryInfo->memSubConManufId);
+            record["Memory Subsystem Controller Product Id"] =
+                uint16_t(memoryInfo->memSubConProdId);
+            record["Non-volatile Size"] = uint64_t(memoryInfo->nvSize);
+            record["Volatile Size"] = uint64_t(memoryInfo->volatileSize);
+            record["Cache Size"] = uint64_t(memoryInfo->cacheSize);
+            record["Logical Size"] = uint64_t(memoryInfo->logicalSize);
+        } while ((dataIn = smbiosNextPtr(dataIn)) != nullptr);
+
+        return ret;
+    }
+
+    throw std::invalid_argument("Invalid record type");
+    return ret;
+}
+
 } // namespace smbios
 } // namespace phosphor
diff --git a/src/mdrv2_main.cpp b/src/mdrv2_main.cpp
index 42b874f..2699571 100644
--- a/src/mdrv2_main.cpp
+++ b/src/mdrv2_main.cpp
@@ -1,5 +1,5 @@
 /*
-// Copyright (c) 2019 Intel Corporation
+// Copyright (c) 2018 Intel Corporation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -16,33 +16,32 @@
 
 #include "mdrv2.hpp"
 
-#include <systemd/sd-event.h>
-
+#include <boost/asio/io_context.hpp>
 #include <phosphor-logging/elog-errors.hpp>
 #include <phosphor-logging/elog.hpp>
+#include <sdbusplus/asio/connection.hpp>
+#include <sdbusplus/asio/object_server.hpp>
+
+boost::asio::io_context io;
+auto connection = std::make_shared<sdbusplus::asio::connection>(io);
+auto objServer = sdbusplus::asio::object_server(connection);
+
+sdbusplus::asio::object_server& getObjectServer(void)
+{
+    return objServer;
+}
 
 int main(void)
 {
-    sd_event* events = nullptr;
-    sd_event_default(&events);
-    sdbusplus::bus::bus bus = sdbusplus::bus::new_default();
+    sdbusplus::bus::bus& bus = static_cast<sdbusplus::bus::bus&>(*connection);
     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);
+    phosphor::smbios::MDR_V2 mdrV2(bus, phosphor::smbios::mdrV2Path, io);
 
-    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;
-        }
-    }
+    io.run();
+
     return 0;
 }