xref: /openbmc/ibm-logging/dbus.cpp (revision 3e71c977)
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 <phosphor-logging/log.hpp>
17 #include "dbus.hpp"
18 
19 namespace ibm
20 {
21 namespace logging
22 {
23 
24 constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
25 constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
26 constexpr auto MAPPER_IFACE = "xyz.openbmc_project.ObjectMapper";
27 constexpr auto PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
28 
29 using namespace phosphor::logging;
30 
31 ObjectValueTree getManagedObjects(sdbusplus::bus::bus& bus,
32                                   const std::string& service,
33                                   const std::string& objPath)
34 {
35     ObjectValueTree interfaces;
36 
37     auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
38                                       "org.freedesktop.DBus.ObjectManager",
39                                       "GetManagedObjects");
40 
41     auto reply = bus.call(method);
42 
43     if (reply.is_method_error())
44     {
45         log<level::ERR>("Failed to get managed objects",
46                         entry("SERVICE=%s", service.c_str()),
47                         entry("PATH=%s", objPath.c_str()));
48     }
49     else
50     {
51         reply.read(interfaces);
52     }
53 
54     return interfaces;
55 }
56 
57 DbusPropertyMap getAllProperties(sdbusplus::bus::bus& bus,
58                                  const std::string& service,
59                                  const std::string& objPath,
60                                  const std::string& interface)
61 {
62     DbusPropertyMap properties;
63 
64     auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
65                                       PROPERTY_IFACE, "GetAll");
66     method.append(interface);
67     auto reply = bus.call(method);
68 
69     if (reply.is_method_error())
70     {
71         log<level::ERR>("Failed to get all properties",
72                         entry("SERVICE=%s", service.c_str()),
73                         entry("PATH=%s", objPath.c_str()),
74                         entry("INTERFACE=%s", interface.c_str()));
75     }
76     else
77     {
78         reply.read(properties);
79     }
80 
81     return properties;
82 }
83 
84 DbusSubtree getSubtree(sdbusplus::bus::bus& bus, const std::string& root,
85                        size_t depth, const std::string& interface)
86 {
87     DbusSubtree tree;
88 
89     auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, MAPPER_IFACE,
90                                       "GetSubTree");
91     method.append(root);
92     method.append(depth);
93     method.append(std::vector<std::string>({interface}));
94     auto reply = bus.call(method);
95 
96     if (reply.is_method_error())
97     {
98         log<level::ERR>("Failed to get subtree", entry("ROOT=%s", root.c_str()),
99                         entry("INTERFACE=%s", interface.c_str()));
100     }
101     else
102     {
103         reply.read(tree);
104     }
105 
106     return tree;
107 }
108 
109 DbusService getService(const std::string& objPath, const std::string& interface,
110                        const DbusSubtree& tree)
111 {
112     DbusService service;
113 
114     auto services = tree.find(objPath);
115     if (services != tree.end())
116     {
117         auto s = std::find_if(services->second.begin(), services->second.end(),
118                               [&interface](const auto& entry) {
119                                   auto i =
120                                       std::find(entry.second.begin(),
121                                                 entry.second.end(), interface);
122                                   return i != entry.second.end();
123                               });
124         if (s != services->second.end())
125         {
126             service = s->first;
127         }
128     }
129 
130     return service;
131 }
132 }
133 }
134