Refactor MDRV2 class to allow more than one

Refactored the main MDRV2 class to allow more than one object of this
class to exist at the same time. All hardcoded paths have been made
parameters to the constructor, so that they can be varied at runtime as
needed, to avoid overlap.

Also did some necessary internal cleanups to facilitate this.

Tested: Created multiple copies of the MDRV2 object at the same time,
it worked, all appeared on D-Bus, under distinct object paths and
inventory paths. Destructed the MDRV2 object, it disappeared from
D-Bus, constructed it again, it came back.

https://gist.github.com/Krellan/6930bc2ed1ac16b93afcc3a12c02e545

Change-Id: Icd1ebf50086b526cf0cff149eb8ddc59da78f0a9
Signed-off-by: Josh Lehan <krellan@google.com>
diff --git a/src/mdrv2.cpp b/src/mdrv2.cpp
index aba2775..99dc302 100644
--- a/src/mdrv2.cpp
+++ b/src/mdrv2.cpp
@@ -35,7 +35,7 @@
 {
     std::vector<uint8_t> responseDir;
 
-    std::ifstream smbiosFile(mdrType2File, std::ios_base::binary);
+    std::ifstream smbiosFile(smbiosFilePath, std::ios_base::binary);
     if (!smbiosFile.good())
     {
         phosphor::logging::log<phosphor::logging::level::ERR>(
@@ -216,7 +216,7 @@
             "Read data from flash error - Invalid data point");
         return false;
     }
-    std::ifstream smbiosFile(mdrType2File, std::ios_base::binary);
+    std::ifstream smbiosFile(smbiosFilePath, std::ios_base::binary);
     if (!smbiosFile.good())
     {
         phosphor::logging::log<phosphor::logging::level::ERR>(
@@ -377,7 +377,7 @@
 
 uint8_t MDRV2::directoryEntries(uint8_t value)
 {
-    std::ifstream smbiosFile(mdrType2File, std::ios_base::binary);
+    std::ifstream smbiosFile(smbiosFilePath, std::ios_base::binary);
     if (!smbiosFile.good())
     {
         phosphor::logging::log<phosphor::logging::level::ERR>(
@@ -394,53 +394,88 @@
 
 void MDRV2::systemInfoUpdate()
 {
+    // By default, look for System interface on any system/board/* object
+    std::string mapperAncestorPath = smbiosInventoryPath;
+    std::string matchParentPath = smbiosInventoryPath + "/board/";
+    bool requireExactMatch = false;
+
+    // If customized, look for System on only that custom object
+    if (smbiosInventoryPath != defaultInventoryPath)
+    {
+        std::filesystem::path path(smbiosInventoryPath);
+
+        // Search under parent to find exact match for self
+        mapperAncestorPath = path.parent_path().string();
+        matchParentPath = mapperAncestorPath;
+        requireExactMatch = true;
+    }
+
     std::string motherboardPath;
-    auto method = bus.new_method_call(mapperBusName, mapperPath,
-                                      mapperInterface, "GetSubTreePaths");
-    method.append(systemInterfacePath);
+    auto method = bus->new_method_call(mapperBusName, mapperPath,
+                                       mapperInterface, "GetSubTreePaths");
+    method.append(mapperAncestorPath);
     method.append(0);
     method.append(std::vector<std::string>({systemInterface}));
 
     try
     {
         std::vector<std::string> paths;
-        sdbusplus::message_t reply = bus.call(method);
+        sdbusplus::message_t reply = bus->call(method);
         reply.read(paths);
-        if (paths.size() < 1)
+
+        size_t pathsCount = paths.size();
+        for (size_t i = 0; i < pathsCount; ++i)
+        {
+            if (requireExactMatch && (paths[i] != smbiosInventoryPath))
+            {
+                continue;
+            }
+
+            motherboardPath = std::move(paths[i]);
+            break;
+        }
+
+        if (motherboardPath.empty())
         {
             phosphor::logging::log<phosphor::logging::level::ERR>(
                 "Failed to get system motherboard dbus path. Setting up a "
                 "match rule");
-            // Add match rule if motherboard dbus path is not yet created
-            static std::unique_ptr<sdbusplus::bus::match_t>
+
+            if (!motherboardConfigMatch)
+            {
                 motherboardConfigMatch =
                     std::make_unique<sdbusplus::bus::match_t>(
-                        bus,
+                        *bus,
                         sdbusplus::bus::match::rules::interfacesAdded() +
                             sdbusplus::bus::match::rules::argNpath(
-                                0,
-                                "/xyz/openbmc_project/inventory/system/board/"),
+                                0, matchParentPath),
                         [this](sdbusplus::message_t& msg) {
-                sdbusplus::message::object_path objectName;
-                boost::container::flat_map<
-                    std::string,
+                    sdbusplus::message::object_path objectName;
                     boost::container::flat_map<
-                        std::string, std::variant<std::string, uint64_t>>>
-                    msgData;
-                msg.read(objectName, msgData);
-                if (msgData.contains(systemInterface))
-                {
-                    this->systemInfoUpdate();
-                }
+                        std::string,
+                        boost::container::flat_map<
+                            std::string, std::variant<std::string, uint64_t>>>
+                        msgData;
+                    msg.read(objectName, msgData);
+                    if (msgData.contains(systemInterface))
+                    {
+                        systemInfoUpdate();
+                    }
                         });
+            }
         }
         else
         {
-            motherboardPath = std::move(paths[0]);
+            lg2::info(
+                "Found Inventory anchor object for SMBIOS content {I}: {M}",
+                "I", smbiosInventoryPath, "M", motherboardPath);
         }
     }
     catch (const sdbusplus::exception_t& e)
     {
+        lg2::error(
+            "Exception while trying to find Inventory anchor object for SMBIOS content {I}: {E}",
+            "I", smbiosInventoryPath, "E", e.what());
         phosphor::logging::log<phosphor::logging::level::ERR>(
             "Failed to query system motherboard",
             phosphor::logging::entry("ERROR=%s", e.what()));
@@ -462,11 +497,12 @@
 
     for (unsigned int index = 0; index < *num; index++)
     {
-        std::string path = cpuPath + std::to_string(index);
+        std::string path = smbiosInventoryPath + cpuSuffix +
+                           std::to_string(index);
         if (index + 1 > cpus.size())
         {
             cpus.emplace_back(std::make_unique<phosphor::smbios::Cpu>(
-                bus, path, index, smbiosDir.dir[smbiosDirIndex].dataStorage,
+                *bus, path, index, smbiosDir.dir[smbiosDirIndex].dataStorage,
                 motherboardPath));
         }
         else
@@ -494,11 +530,12 @@
 
     for (unsigned int index = 0; index < *num; index++)
     {
-        std::string path = dimmPath + std::to_string(index);
+        std::string path = smbiosInventoryPath + dimmSuffix +
+                           std::to_string(index);
         if (index + 1 > dimms.size())
         {
             dimms.emplace_back(std::make_unique<phosphor::smbios::Dimm>(
-                bus, path, index, smbiosDir.dir[smbiosDirIndex].dataStorage,
+                *bus, path, index, smbiosDir.dir[smbiosDirIndex].dataStorage,
                 motherboardPath));
         }
         else
@@ -526,11 +563,12 @@
 
     for (unsigned int index = 0; index < *num; index++)
     {
-        std::string path = pciePath + std::to_string(index);
+        std::string path = smbiosInventoryPath + pcieSuffix +
+                           std::to_string(index);
         if (index + 1 > pcies.size())
         {
             pcies.emplace_back(std::make_unique<phosphor::smbios::Pcie>(
-                bus, path, index, smbiosDir.dir[smbiosDirIndex].dataStorage,
+                *bus, path, index, smbiosDir.dir[smbiosDirIndex].dataStorage,
                 motherboardPath));
         }
         else
@@ -541,8 +579,9 @@
     }
 
     system.reset();
-    system = std::make_unique<System>(
-        bus, systemPath, smbiosDir.dir[smbiosDirIndex].dataStorage);
+    system = std::make_unique<System>(bus, smbiosInventoryPath + systemSuffix,
+                                      smbiosDir.dir[smbiosDirIndex].dataStorage,
+                                      smbiosFilePath);
 }
 
 std::optional<size_t> MDRV2::getTotalCpuSlot()
@@ -768,7 +807,7 @@
 
     timer.expires_after(usec);
     timer.async_wait([this](boost::system::error_code ec) {
-        if (ec || this == nullptr)
+        if (ec)
         {
             phosphor::logging::log<phosphor::logging::level::ERR>(
                 "Timer Error!");
diff --git a/src/mdrv2_main.cpp b/src/mdrv2_main.cpp
index 06a51cb..8dad1d1 100644
--- a/src/mdrv2_main.cpp
+++ b/src/mdrv2_main.cpp
@@ -22,26 +22,24 @@
 #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)
+int main()
 {
-    return objServer;
-}
+    auto io = std::make_shared<boost::asio::io_context>();
+    auto connection = std::make_shared<sdbusplus::asio::connection>(*io);
+    auto objServer =
+        std::make_shared<sdbusplus::asio::object_server>(connection);
 
-int main(void)
-{
-    sdbusplus::bus_t& bus = static_cast<sdbusplus::bus_t&>(*connection);
-    sdbusplus::server::manager_t objManager(bus,
+    sdbusplus::server::manager_t objManager(*connection,
                                             "/xyz/openbmc_project/inventory");
 
-    bus.request_name("xyz.openbmc_project.Smbios.MDR_V2");
+    connection->request_name("xyz.openbmc_project.Smbios.MDR_V2");
 
-    phosphor::smbios::MDRV2 mdrV2(bus, phosphor::smbios::mdrV2Path, io);
+    auto mdrV2 = std::make_shared<phosphor::smbios::MDRV2>(
+        io, connection, objServer, mdrDefaultFile,
+        phosphor::smbios::defaultObjectPath,
+        phosphor::smbios::defaultInventoryPath);
 
-    io.run();
+    io->run();
 
     return 0;
 }
diff --git a/src/smbios-ipmi-blobs/handler.cpp b/src/smbios-ipmi-blobs/handler.cpp
index 3763762..fb8111a 100644
--- a/src/smbios-ipmi-blobs/handler.cpp
+++ b/src/smbios-ipmi-blobs/handler.cpp
@@ -15,6 +15,7 @@
 #include <algorithm>
 #include <cstdint>
 #include <ctime>
+#include <filesystem>
 #include <fstream>
 #include <memory>
 #include <string>
@@ -34,7 +35,7 @@
     bool status = false;
     sdbusplus::bus_t bus = sdbusplus::bus_t(ipmid_get_sd_bus_connection());
     sdbusplus::message_t method =
-        bus.new_method_call(mdrV2Service, phosphor::smbios::mdrV2Path,
+        bus.new_method_call(mdrV2Service, phosphor::smbios::defaultObjectPath,
                             mdrV2Interface, "AgentSynchronizeData");
 
     try
@@ -48,7 +49,8 @@
             "Error Sync data with service",
             phosphor::logging::entry("ERROR=%s", e.what()),
             phosphor::logging::entry("SERVICE=%s", mdrV2Service),
-            phosphor::logging::entry("PATH=%s", phosphor::smbios::mdrV2Path));
+            phosphor::logging::entry("PATH=%s",
+                                     phosphor::smbios::defaultObjectPath));
         return false;
     }
 
@@ -191,24 +193,27 @@
     /* Clear the commit_error bit. */
     blobPtr->state &= ~blobs::StateFlags::commit_error;
 
+    std::string defaultDir =
+        std::filesystem::path(mdrDefaultFile).parent_path();
+
     MDRSMBIOSHeader mdrHdr;
     mdrHdr.dirVer = mdrDirVersion;
     mdrHdr.mdrType = mdrTypeII;
     mdrHdr.timestamp = std::time(nullptr);
     mdrHdr.dataSize = blobPtr->buffer.size();
-    if (access(smbiosPath, F_OK) == -1)
+    if (access(defaultDir.c_str(), F_OK) == -1)
     {
-        int flag = mkdir(smbiosPath, S_IRWXU);
+        int flag = mkdir(defaultDir.c_str(), S_IRWXU);
         if (flag != 0)
         {
             phosphor::logging::log<phosphor::logging::level::ERR>(
-                "create folder failed for writting smbios file");
+                "create folder failed for writing smbios file");
             blobPtr->state |= blobs::StateFlags::commit_error;
             return false;
         }
     }
 
-    std::ofstream smbiosFile(mdrType2File,
+    std::ofstream smbiosFile(mdrDefaultFile,
                              std::ios_base::binary | std::ios_base::trunc);
     if (!smbiosFile.good())
     {
diff --git a/src/system.cpp b/src/system.cpp
index c0713a6..a838577 100644
--- a/src/system.cpp
+++ b/src/system.cpp
@@ -133,7 +133,7 @@
         if (std::find_if(tempS.begin(), tempS.end(),
                          [](char ch) { return !isprint(ch); }) != tempS.end())
         {
-            std::ofstream smbiosFile(mdrType2File, std::ios_base::trunc);
+            std::ofstream smbiosFile(smbiosFilePath, std::ios_base::trunc);
             if (!smbiosFile.good())
             {
                 phosphor::logging::log<phosphor::logging::level::ERR>(
@@ -149,7 +149,7 @@
         }
         result = tempS;
 
-        setProperty(bus, biosActiveObjPath, biosVersionIntf, biosVersionProp,
+        setProperty(*bus, biosActiveObjPath, biosVersionIntf, biosVersionProp,
                     result);
     }
     lg2::info("VERSION INFO - BIOS - {VER}", "VER", result);