blob: 06b0078af96f1925c0057500a97cb03a8124f5cc [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 */
16#include <phosphor-logging/log.hpp>
17#include "dbus.hpp"
18
19namespace ibm
20{
21namespace logging
22{
23
Matt Spinlerd82a6dd2018-05-23 11:31:22 -050024constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
25constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
26constexpr auto MAPPER_IFACE = "xyz.openbmc_project.ObjectMapper";
27constexpr auto PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
28
29using namespace phosphor::logging;
30
Matt Spinler259e7272018-03-29 10:57:17 -050031ObjectValueTree getManagedObjects(sdbusplus::bus::bus& bus,
32 const std::string& service,
33 const std::string& objPath)
Matt Spinlerbc997492018-03-27 11:24:45 -050034{
35 ObjectValueTree interfaces;
36
Matt Spinler259e7272018-03-29 10:57:17 -050037 auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
38 "org.freedesktop.DBus.ObjectManager",
39 "GetManagedObjects");
Matt Spinlerbc997492018-03-27 11:24:45 -050040
41 auto reply = bus.call(method);
42
43 if (reply.is_method_error())
44 {
Matt Spinlerbc997492018-03-27 11:24:45 -050045 log<level::ERR>("Failed to get managed objects",
Matt Spinlerd82a6dd2018-05-23 11:31:22 -050046 entry("SERVICE=%s", service.c_str()),
Matt Spinler259e7272018-03-29 10:57:17 -050047 entry("PATH=%s", objPath.c_str()));
Matt Spinlerbc997492018-03-27 11:24:45 -050048 }
49 else
50 {
51 reply.read(interfaces);
52 }
53
54 return interfaces;
55}
Matt Spinlerd82a6dd2018-05-23 11:31:22 -050056
57DbusPropertyMap 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
84DbusSubtree 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
109DbusService 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}
Matt Spinlerbc997492018-03-27 11:24:45 -0500132}
133}