xref: /openbmc/openpower-occ-control/utils.cpp (revision f3b7514e55c8cd89b56ec04849a48a3e83d664b1)
1 #include "utils.hpp"
2 
3 #include <phosphor-logging/elog-errors.hpp>
4 #include <sdbusplus/bus.hpp>
5 #include <string>
6 #include <xyz/openbmc_project/Common/error.hpp>
7 namespace open_power
8 {
9 namespace occ
10 {
11 namespace utils
12 {
13 // For throwing exceptions
14 using namespace phosphor::logging;
15 using InternalFailure =
16     sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
17 
18 const std::string getService(const std::string& path,
19                              const std::string& interface)
20 {
21 
22     using InterfaceList = std::vector<std::string>;
23     std::map<std::string, std::vector<std::string>> mapperResponse;
24 
25     auto& bus = getBus();
26 
27     auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH,
28                                       MAPPER_IFACE, "GetObject");
29     mapper.append(path, InterfaceList({interface}));
30 
31     auto mapperResponseMsg = bus.call(mapper);
32     if (mapperResponseMsg.is_method_error())
33     {
34         log<level::ERR>("ERROR in getting service",
35                         entry("PATH=%s", path.c_str()),
36                         entry("INTERFACE=%s", interface.c_str()));
37 
38         elog<InternalFailure>();
39     }
40 
41     mapperResponseMsg.read(mapperResponse);
42     if (mapperResponse.empty())
43     {
44         log<level::ERR>("ERROR reading mapper response",
45                         entry("PATH=%s", path.c_str()),
46                         entry("INTERFACE=%s", interface.c_str()));
47 
48         elog<InternalFailure>();
49     }
50 
51     // the value here will be the service name
52     return mapperResponse.cbegin()->first;
53 }
54 
55 const PropertyValue getProperty(const std::string& objectPath,
56                                 const std::string& interface,
57                                 const std::string& propertyName)
58 {
59     PropertyValue value{};
60 
61     auto& bus = getBus();
62     auto service = getService(objectPath, interface);
63     if (service.empty())
64     {
65         return value;
66     }
67 
68     auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
69                                       DBUS_PROPERTY_IFACE, "Get");
70     method.append(interface, propertyName);
71 
72     auto reply = bus.call(method);
73     reply.read(value);
74 
75     return value;
76 }
77 
78 } // namespace utils
79 } // namespace occ
80 } // namespace open_power
81