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