Refactor D-Bus object
- The intent behind this commit is to refactor D-Bus, and abstract
the bus, getService, getProperty and other methods into the utils
file, and other file operations D-Bus only need to include
uitls.hpp.
- We can also continue to add other general methods such as
setPropery, getSubTree in the utils file in the future.
- Also, removed redundant files(occ_finder.hpp and occ_finder.cpp).
Tested: built openpower-occ-control successfully and worked.
Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: I53e61e30a76173c154a9f47fc122936468abbc4b
diff --git a/powercap.cpp b/powercap.cpp
index 1655982..39c7416 100644
--- a/powercap.cpp
+++ b/powercap.cpp
@@ -23,36 +23,6 @@
using namespace phosphor::logging;
namespace fs = std::experimental::filesystem;
-std::string PowerCap::getService(std::string path, std::string interface)
-{
- auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
- MAPPER_INTERFACE, "GetObject");
-
- mapper.append(path, std::vector<std::string>({interface}));
- auto mapperResponseMsg = bus.call(mapper);
-
- if (mapperResponseMsg.is_method_error())
- {
- log<level::ERR>("Error in mapper call", entry("PATH=%s", path.c_str()),
- entry("INTERFACE=%s", interface.c_str()));
- // TODO openbmc/openbmc#851 - Once available, throw returned error
- throw std::runtime_error("Error in mapper call");
- }
-
- std::map<std::string, std::vector<std::string>> mapperResponse;
- mapperResponseMsg.read(mapperResponse);
- if (mapperResponse.empty())
- {
- log<level::ERR>("Error reading mapper response",
- entry("PATH=%s", path.c_str()),
- entry("INTERFACE=%s", interface.c_str()));
- // TODO openbmc/openbmc#1712 - Handle empty mapper resp. consistently
- throw std::runtime_error("Error reading mapper response");
- }
-
- return mapperResponse.begin()->first;
-}
-
uint32_t PowerCap::getOccInput(uint32_t pcap, bool pcapEnabled)
{
if (!pcapEnabled)
@@ -68,46 +38,41 @@
uint32_t PowerCap::getPcap()
{
- auto settingService = getService(PCAP_PATH, PCAP_INTERFACE);
-
- auto method =
- this->bus.new_method_call(settingService.c_str(), PCAP_PATH,
- "org.freedesktop.DBus.Properties", "Get");
-
- method.append(PCAP_INTERFACE, POWER_CAP_PROP);
- auto reply = this->bus.call(method);
-
- if (reply.is_method_error())
+ utils::PropertyValue pcap{};
+ try
{
- log<level::ERR>("Error in getPcap prop");
+ pcap = utils::getProperty(PCAP_PATH, PCAP_INTERFACE, POWER_CAP_PROP);
+
+ return std::get<uint32_t>(pcap);
+ }
+ catch (const sdbusplus::exception::SdBusError& e)
+ {
+ log<level::ERR>("Failed to get PowerCap property",
+ entry("ERROR=%s", e.what()),
+ entry("PATH=%s", PCAP_PATH));
+
return 0;
}
- std::variant<uint32_t> pcap;
- reply.read(pcap);
-
- return std::get<uint32_t>(pcap);
}
bool PowerCap::getPcapEnabled()
{
- auto settingService = getService(PCAP_PATH, PCAP_INTERFACE);
-
- auto method =
- this->bus.new_method_call(settingService.c_str(), PCAP_PATH,
- "org.freedesktop.DBus.Properties", "Get");
-
- method.append(PCAP_INTERFACE, POWER_CAP_ENABLE_PROP);
- auto reply = this->bus.call(method);
-
- if (reply.is_method_error())
+ utils::PropertyValue pcapEnabled{};
+ try
{
- log<level::ERR>("Error in getPcapEnabled prop");
- return 0;
- }
- std::variant<bool> pcapEnabled;
- reply.read(pcapEnabled);
+ pcapEnabled = utils::getProperty(PCAP_PATH, PCAP_INTERFACE,
+ POWER_CAP_ENABLE_PROP);
- return std::get<bool>(pcapEnabled);
+ return std::get<bool>(pcapEnabled);
+ }
+ catch (const sdbusplus::exception::SdBusError& e)
+ {
+ log<level::ERR>("Failed to get PowerCapEnable property",
+ entry("ERROR=%s", e.what()),
+ entry("PATH=%s", PCAP_PATH));
+
+ return false;
+ }
}
std::string PowerCap::getPcapFilename(const fs::path& path)