blob: 70380d3d3864aed48feee1de88a5e5e19c9df89b [file] [log] [blame]
Matt Spinlerbc997492018-03-27 11:24:45 -05001/**
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 */
Matt Spinlerbc997492018-03-27 11:24:45 -050016#include "dbus.hpp"
17
Matt Spinler66e07072018-09-12 10:36:14 -050018#include <phosphor-logging/log.hpp>
19
Matt Spinlerbc997492018-03-27 11:24:45 -050020namespace ibm
21{
22namespace logging
23{
24
Matt Spinlerd82a6dd2018-05-23 11:31:22 -050025constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
26constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
27constexpr auto MAPPER_IFACE = "xyz.openbmc_project.ObjectMapper";
28constexpr auto PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
29
30using namespace phosphor::logging;
31
Matt Spinler259e7272018-03-29 10:57:17 -050032ObjectValueTree getManagedObjects(sdbusplus::bus::bus& bus,
33 const std::string& service,
34 const std::string& objPath)
Matt Spinlerbc997492018-03-27 11:24:45 -050035{
36 ObjectValueTree interfaces;
37
Matt Spinler259e7272018-03-29 10:57:17 -050038 auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
39 "org.freedesktop.DBus.ObjectManager",
40 "GetManagedObjects");
Matt Spinlerbc997492018-03-27 11:24:45 -050041
42 auto reply = bus.call(method);
43
44 if (reply.is_method_error())
45 {
Matt Spinlerbc997492018-03-27 11:24:45 -050046 log<level::ERR>("Failed to get managed objects",
Matt Spinlerd82a6dd2018-05-23 11:31:22 -050047 entry("SERVICE=%s", service.c_str()),
Matt Spinler259e7272018-03-29 10:57:17 -050048 entry("PATH=%s", objPath.c_str()));
Matt Spinlerbc997492018-03-27 11:24:45 -050049 }
50 else
51 {
52 reply.read(interfaces);
53 }
54
55 return interfaces;
56}
Matt Spinlerd82a6dd2018-05-23 11:31:22 -050057
58DbusPropertyMap 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
85DbusSubtree getSubtree(sdbusplus::bus::bus& bus, const std::string& root,
86 size_t 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
110DbusService 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}
Matt Spinler66e07072018-09-12 10:36:14 -0500133} // namespace logging
134} // namespace ibm