xref: /openbmc/ibm-logging/dbus.cpp (revision c57aa4b9310eed09a0ad3abd1232386c15d71064)
1 /**
2  * Copyright © 2018 IBM Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "dbus.hpp"
17 
18 #include <phosphor-logging/log.hpp>
19 
20 namespace ibm
21 {
22 namespace logging
23 {
24 
25 constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
26 constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
27 constexpr auto MAPPER_IFACE = "xyz.openbmc_project.ObjectMapper";
28 constexpr auto PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
29 
30 using namespace phosphor::logging;
31 
32 ObjectValueTree getManagedObjects(sdbusplus::bus::bus& bus,
33                                   const std::string& service,
34                                   const std::string& objPath)
35 {
36     ObjectValueTree interfaces;
37 
38     auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
39                                       "org.freedesktop.DBus.ObjectManager",
40                                       "GetManagedObjects");
41 
42     auto reply = bus.call(method);
43 
44     if (reply.is_method_error())
45     {
46         log<level::ERR>("Failed to get managed objects",
47                         entry("SERVICE=%s", service.c_str()),
48                         entry("PATH=%s", objPath.c_str()));
49     }
50     else
51     {
52         reply.read(interfaces);
53     }
54 
55     return interfaces;
56 }
57 
58 DbusPropertyMap getAllProperties(sdbusplus::bus::bus& bus,
59                                  const std::string& service,
60                                  const std::string& objPath,
61                                  const std::string& interface)
62 {
63     DbusPropertyMap properties;
64 
65     auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
66                                       PROPERTY_IFACE, "GetAll");
67     method.append(interface);
68     auto reply = bus.call(method);
69 
70     if (reply.is_method_error())
71     {
72         log<level::ERR>("Failed to get all properties",
73                         entry("SERVICE=%s", service.c_str()),
74                         entry("PATH=%s", objPath.c_str()),
75                         entry("INTERFACE=%s", interface.c_str()));
76     }
77     else
78     {
79         reply.read(properties);
80     }
81 
82     return properties;
83 }
84 
85 DbusSubtree getSubtree(sdbusplus::bus::bus& bus, const std::string& root,
86                        int depth, const std::string& interface)
87 {
88     DbusSubtree tree;
89 
90     auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, MAPPER_IFACE,
91                                       "GetSubTree");
92     method.append(root);
93     method.append(depth);
94     method.append(std::vector<std::string>({interface}));
95     auto reply = bus.call(method);
96 
97     if (reply.is_method_error())
98     {
99         log<level::ERR>("Failed to get subtree", entry("ROOT=%s", root.c_str()),
100                         entry("INTERFACE=%s", interface.c_str()));
101     }
102     else
103     {
104         reply.read(tree);
105     }
106 
107     return tree;
108 }
109 
110 DbusService getService(const std::string& objPath, const std::string& interface,
111                        const DbusSubtree& tree)
112 {
113     DbusService service;
114 
115     auto services = tree.find(objPath);
116     if (services != tree.end())
117     {
118         auto s = std::find_if(services->second.begin(), services->second.end(),
119                               [&interface](const auto& entry) {
120                                   auto i =
121                                       std::find(entry.second.begin(),
122                                                 entry.second.end(), interface);
123                                   return i != entry.second.end();
124                               });
125         if (s != services->second.end())
126         {
127             service = s->first;
128         }
129     }
130 
131     return service;
132 }
133 } // namespace logging
134 } // namespace ibm
135