Refactor: Use hiomapd instead of mboxd

mbox service is renamed to hiomapd and provides "Suspend" and
"Resume" method calls, which is more appropriate for raw "cmd" calls.

Use the new methods to suspend and resume hiomapd.
Move "getService()" function into utils so it is shared by multiple
files.

Tested: Verify the reset works on Romulus

Change-Id: I8f89de134b13126697bfc69a21a3148a01c34cca
Signed-off-by: Lei YU <mine260309@gmail.com>
diff --git a/utils.cpp b/utils.cpp
index 95fc2e0..04b24bf 100644
--- a/utils.cpp
+++ b/utils.cpp
@@ -1,5 +1,12 @@
+#include "config.h"
+
 #include "utils.hpp"
 
+#include <phosphor-logging/elog-errors.hpp>
+#include <phosphor-logging/elog.hpp>
+#include <phosphor-logging/log.hpp>
+#include <xyz/openbmc_project/Common/error.hpp>
+
 #if OPENSSL_VERSION_NUMBER < 0x10100000L
 
 #include <string.h>
@@ -27,3 +34,83 @@
 }
 
 #endif // OPENSSL_VERSION_NUMBER < 0x10100000L
+
+namespace utils
+{
+
+using sdbusplus::exception::SdBusError;
+using namespace phosphor::logging;
+
+constexpr auto HIOMAPD_PATH = "/xyz/openbmc_project/Hiomapd";
+constexpr auto HIOMAPD_INTERFACE = "xyz.openbmc_project.Hiomapd.Control";
+
+using InternalFailure =
+    sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
+
+std::string getService(sdbusplus::bus::bus& bus, const std::string& path,
+                       const std::string& intf)
+{
+    auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
+                                      MAPPER_INTERFACE, "GetObject");
+
+    mapper.append(path, std::vector<std::string>({intf}));
+    try
+    {
+        auto mapperResponseMsg = bus.call(mapper);
+
+        std::vector<std::pair<std::string, std::vector<std::string>>>
+            mapperResponse;
+        mapperResponseMsg.read(mapperResponse);
+        if (mapperResponse.empty())
+        {
+            log<level::ERR>("Error reading mapper response");
+            throw std::runtime_error("Error reading mapper response");
+        }
+        return mapperResponse[0].first;
+    }
+    catch (const sdbusplus::exception::SdBusError& ex)
+    {
+        log<level::ERR>("Mapper call failed", entry("METHOD=%d", "GetObject"),
+                        entry("PATH=%s", path.c_str()),
+                        entry("INTERFACE=%s", intf.c_str()));
+        throw std::runtime_error("Mapper call failed");
+    }
+}
+
+void hiomapdSuspend(sdbusplus::bus::bus& bus)
+{
+    auto service = getService(bus, HIOMAPD_PATH, HIOMAPD_INTERFACE);
+    auto method = bus.new_method_call(service.c_str(), HIOMAPD_PATH,
+                                      HIOMAPD_INTERFACE, "Suspend");
+
+    try
+    {
+        bus.call_noreply(method);
+    }
+    catch (const SdBusError& e)
+    {
+        log<level::ERR>("Error in mboxd suspend call",
+                        entry("ERROR=%s", e.what()));
+    }
+}
+
+void hiomapdResume(sdbusplus::bus::bus& bus)
+{
+    auto service = getService(bus, HIOMAPD_PATH, HIOMAPD_INTERFACE);
+    auto method = bus.new_method_call(service.c_str(), HIOMAPD_PATH,
+                                      HIOMAPD_INTERFACE, "Resume");
+
+    method.append(true); // Indicate PNOR is modified
+
+    try
+    {
+        bus.call_noreply(method);
+    }
+    catch (const SdBusError& e)
+    {
+        log<level::ERR>("Error in mboxd suspend call",
+                        entry("ERROR=%s", e.what()));
+    }
+}
+
+} // namespace utils