Segregate utility methods
This commit segregates utility methods required commonly by both
openpower-read-vpd and ibm-read-vpd from methods only required by
ibm-read-vpd.
All dependency required by utility methods specific to ibm-read-vpd
is not applicable to utility methods required by openpower-read-vpd.
Hence to avoid un-necessary dependency inclusion, this change is
introduced.
Signed-off-by: Sunny Srivastava <sunnsr25@in.ibm.com>
Change-Id: I95f2be27dc0c391a45beb1654a99506317aaa52b
diff --git a/common_utility.cpp b/common_utility.cpp
new file mode 100644
index 0000000..5a12b6c
--- /dev/null
+++ b/common_utility.cpp
@@ -0,0 +1,75 @@
+#include "common_utility.hpp"
+
+#include "const.hpp"
+
+#include <iostream>
+#include <phosphor-logging/log.hpp>
+
+namespace openpower
+{
+namespace vpd
+{
+namespace common
+{
+namespace utility
+{
+using namespace constants;
+using namespace inventory;
+using namespace phosphor::logging;
+
+std::string getService(sdbusplus::bus::bus& bus, const std::string& path,
+ const std::string& interface)
+{
+ auto mapper = bus.new_method_call(mapperDestination, mapperObjectPath,
+ mapperInterface, "GetObject");
+ mapper.append(path, std::vector<std::string>({interface}));
+
+ std::map<std::string, std::vector<std::string>> response;
+ try
+ {
+ auto reply = bus.call(mapper);
+ reply.read(response);
+ }
+ catch (const sdbusplus::exception::SdBusError& e)
+ {
+ log<level::ERR>("D-Bus call exception",
+ entry("OBJPATH=%s", mapperObjectPath),
+ entry("INTERFACE=%s", mapperInterface),
+ entry("EXCEPTION=%s", e.what()));
+
+ throw std::runtime_error("Service name is not found");
+ }
+
+ if (response.empty())
+ {
+ throw std::runtime_error("Service name response is empty");
+ }
+
+ return response.begin()->first;
+}
+
+void callPIM(ObjectMap&& objects)
+{
+ try
+ {
+ auto bus = sdbusplus::bus::new_default();
+ auto service = getService(bus, pimPath, pimIntf);
+ auto pimMsg =
+ bus.new_method_call(service.c_str(), pimPath, pimIntf, "Notify");
+ pimMsg.append(std::move(objects));
+ auto result = bus.call(pimMsg);
+ if (result.is_method_error())
+ {
+ std::cerr << "PIM Notify() failed\n";
+ }
+ }
+ catch (const std::runtime_error& e)
+ {
+ log<level::ERR>(e.what());
+ }
+}
+
+} // namespace utility
+} // namespace common
+} // namespace vpd
+} // namespace openpower
\ No newline at end of file