1bc997490SMatt Spinler /**
2bc997490SMatt Spinler * Copyright © 2018 IBM Corporation
3bc997490SMatt Spinler *
4bc997490SMatt Spinler * Licensed under the Apache License, Version 2.0 (the "License");
5bc997490SMatt Spinler * you may not use this file except in compliance with the License.
6bc997490SMatt Spinler * You may obtain a copy of the License at
7bc997490SMatt Spinler *
8bc997490SMatt Spinler * http://www.apache.org/licenses/LICENSE-2.0
9bc997490SMatt Spinler *
10bc997490SMatt Spinler * Unless required by applicable law or agreed to in writing, software
11bc997490SMatt Spinler * distributed under the License is distributed on an "AS IS" BASIS,
12bc997490SMatt Spinler * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bc997490SMatt Spinler * See the License for the specific language governing permissions and
14bc997490SMatt Spinler * limitations under the License.
15bc997490SMatt Spinler */
16bc997490SMatt Spinler #include "dbus.hpp"
17bc997490SMatt Spinler
1866e07073SMatt Spinler #include <phosphor-logging/log.hpp>
1966e07073SMatt Spinler
20bc997490SMatt Spinler namespace ibm
21bc997490SMatt Spinler {
22bc997490SMatt Spinler namespace logging
23bc997490SMatt Spinler {
24bc997490SMatt Spinler
25d82a6ddfSMatt Spinler constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
26d82a6ddfSMatt Spinler constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
27d82a6ddfSMatt Spinler constexpr auto MAPPER_IFACE = "xyz.openbmc_project.ObjectMapper";
28d82a6ddfSMatt Spinler constexpr auto PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
29d82a6ddfSMatt Spinler
30d82a6ddfSMatt Spinler using namespace phosphor::logging;
31d82a6ddfSMatt Spinler
getManagedObjects(sdbusplus::bus_t & bus,const std::string & service,const std::string & objPath)328123a713SPatrick Williams ObjectValueTree getManagedObjects(sdbusplus::bus_t& bus,
33bc997490SMatt Spinler const std::string& service,
34bc997490SMatt Spinler const std::string& objPath)
35bc997490SMatt Spinler {
36bc997490SMatt Spinler ObjectValueTree interfaces;
37bc997490SMatt Spinler
38259e7277SMatt Spinler auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
39bc997490SMatt Spinler "org.freedesktop.DBus.ObjectManager",
40bc997490SMatt Spinler "GetManagedObjects");
41bc997490SMatt Spinler
42bc997490SMatt Spinler auto reply = bus.call(method);
43bc997490SMatt Spinler
44bc997490SMatt Spinler reply.read(interfaces);
45bc997490SMatt Spinler
46bc997490SMatt Spinler return interfaces;
47bc997490SMatt Spinler }
48d82a6ddfSMatt Spinler
getAllProperties(sdbusplus::bus_t & bus,const std::string & service,const std::string & objPath,const std::string & interface)498123a713SPatrick Williams DbusPropertyMap getAllProperties(sdbusplus::bus_t& bus,
50d82a6ddfSMatt Spinler const std::string& service,
51d82a6ddfSMatt Spinler const std::string& objPath,
52d82a6ddfSMatt Spinler const std::string& interface)
53d82a6ddfSMatt Spinler {
54d82a6ddfSMatt Spinler DbusPropertyMap properties;
55d82a6ddfSMatt Spinler
56d82a6ddfSMatt Spinler auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
57d82a6ddfSMatt Spinler PROPERTY_IFACE, "GetAll");
58d82a6ddfSMatt Spinler method.append(interface);
59d82a6ddfSMatt Spinler auto reply = bus.call(method);
60d82a6ddfSMatt Spinler
61d82a6ddfSMatt Spinler reply.read(properties);
62d82a6ddfSMatt Spinler
63d82a6ddfSMatt Spinler return properties;
64d82a6ddfSMatt Spinler }
65d82a6ddfSMatt Spinler
getSubtree(sdbusplus::bus_t & bus,const std::string & root,int depth,const std::string & interface)668123a713SPatrick Williams DbusSubtree getSubtree(sdbusplus::bus_t& bus, const std::string& root,
677766e25bSMatt Spinler int depth, const std::string& interface)
68d82a6ddfSMatt Spinler {
69d82a6ddfSMatt Spinler DbusSubtree tree;
70d82a6ddfSMatt Spinler
71d82a6ddfSMatt Spinler auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, MAPPER_IFACE,
72d82a6ddfSMatt Spinler "GetSubTree");
73d82a6ddfSMatt Spinler method.append(root);
74d82a6ddfSMatt Spinler method.append(depth);
75d82a6ddfSMatt Spinler method.append(std::vector<std::string>({interface}));
76d82a6ddfSMatt Spinler auto reply = bus.call(method);
77d82a6ddfSMatt Spinler
78d82a6ddfSMatt Spinler reply.read(tree);
79d82a6ddfSMatt Spinler
80d82a6ddfSMatt Spinler return tree;
81d82a6ddfSMatt Spinler }
82d82a6ddfSMatt Spinler
getService(const std::string & objPath,const std::string & interface,const DbusSubtree & tree)83d82a6ddfSMatt Spinler DbusService getService(const std::string& objPath, const std::string& interface,
84d82a6ddfSMatt Spinler const DbusSubtree& tree)
85d82a6ddfSMatt Spinler {
86d82a6ddfSMatt Spinler DbusService service;
87d82a6ddfSMatt Spinler
88d82a6ddfSMatt Spinler auto services = tree.find(objPath);
89d82a6ddfSMatt Spinler if (services != tree.end())
90d82a6ddfSMatt Spinler {
91d82a6ddfSMatt Spinler auto s = std::find_if(services->second.begin(), services->second.end(),
92d82a6ddfSMatt Spinler [&interface](const auto& entry) {
93*6a2b8956SPatrick Williams auto i = std::find(entry.second.begin(), entry.second.end(),
94*6a2b8956SPatrick Williams interface);
95d82a6ddfSMatt Spinler return i != entry.second.end();
96d82a6ddfSMatt Spinler });
97d82a6ddfSMatt Spinler if (s != services->second.end())
98d82a6ddfSMatt Spinler {
99d82a6ddfSMatt Spinler service = s->first;
100d82a6ddfSMatt Spinler }
101d82a6ddfSMatt Spinler }
102d82a6ddfSMatt Spinler
103d82a6ddfSMatt Spinler return service;
104d82a6ddfSMatt Spinler }
10566e07073SMatt Spinler } // namespace logging
10666e07073SMatt Spinler } // namespace ibm
107