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     mapperResponseMsg.read(mapperResponse);
34     if (mapperResponse.empty())
35     {
36         log<level::ERR>("ERROR reading mapper response",
37                         entry("PATH=%s", path.c_str()),
38                         entry("INTERFACE=%s", interface.c_str()));
39 
40         elog<InternalFailure>();
41     }
42 
43     // the value here will be the service name
44     return mapperResponse.cbegin()->first;
45 }
46 
47 const PropertyValue getProperty(const std::string& objectPath,
48                                 const std::string& interface,
49                                 const std::string& propertyName)
50 {
51     PropertyValue value{};
52 
53     auto& bus = getBus();
54     auto service = getService(objectPath, interface);
55     if (service.empty())
56     {
57         return value;
58     }
59 
60     auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
61                                       DBUS_PROPERTY_IFACE, "Get");
62     method.append(interface, propertyName);
63 
64     auto reply = bus.call(method);
65     reply.read(value);
66 
67     return value;
68 }
69 
70 std::vector<std::string>
71     getSubtreePaths(const std::vector<std::string>& interfaces,
72                     const std::string& path)
73 {
74     std::vector<std::string> paths;
75 
76     auto& bus = getBus();
77     auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH,
78                                       MAPPER_IFACE, "GetSubTreePaths");
79     method.append(path, 0, interfaces);
80 
81     auto reply = bus.call(method);
82     reply.read(paths);
83 
84     return paths;
85 }
86 } // namespace utils
87 } // namespace occ
88 } // namespace open_power
89