xref: /openbmc/ibm-logging/dbus.cpp (revision 6a2b8956)
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 
getManagedObjects(sdbusplus::bus_t & bus,const std::string & service,const std::string & objPath)32 ObjectValueTree getManagedObjects(sdbusplus::bus_t& 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     reply.read(interfaces);
45 
46     return interfaces;
47 }
48 
getAllProperties(sdbusplus::bus_t & bus,const std::string & service,const std::string & objPath,const std::string & interface)49 DbusPropertyMap getAllProperties(sdbusplus::bus_t& bus,
50                                  const std::string& service,
51                                  const std::string& objPath,
52                                  const std::string& interface)
53 {
54     DbusPropertyMap properties;
55 
56     auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
57                                       PROPERTY_IFACE, "GetAll");
58     method.append(interface);
59     auto reply = bus.call(method);
60 
61     reply.read(properties);
62 
63     return properties;
64 }
65 
getSubtree(sdbusplus::bus_t & bus,const std::string & root,int depth,const std::string & interface)66 DbusSubtree getSubtree(sdbusplus::bus_t& bus, const std::string& root,
67                        int depth, const std::string& interface)
68 {
69     DbusSubtree tree;
70 
71     auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, MAPPER_IFACE,
72                                       "GetSubTree");
73     method.append(root);
74     method.append(depth);
75     method.append(std::vector<std::string>({interface}));
76     auto reply = bus.call(method);
77 
78     reply.read(tree);
79 
80     return tree;
81 }
82 
getService(const std::string & objPath,const std::string & interface,const DbusSubtree & tree)83 DbusService getService(const std::string& objPath, const std::string& interface,
84                        const DbusSubtree& tree)
85 {
86     DbusService service;
87 
88     auto services = tree.find(objPath);
89     if (services != tree.end())
90     {
91         auto s = std::find_if(services->second.begin(), services->second.end(),
92                               [&interface](const auto& entry) {
93             auto i = std::find(entry.second.begin(), entry.second.end(),
94                                interface);
95             return i != entry.second.end();
96         });
97         if (s != services->second.end())
98         {
99             service = s->first;
100         }
101     }
102 
103     return service;
104 }
105 } // namespace logging
106 } // namespace ibm
107