Matt Spinler | bc99749 | 2018-03-27 11:24:45 -0500 | [diff] [blame] | 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 | */ |
Matt Spinler | bc99749 | 2018-03-27 11:24:45 -0500 | [diff] [blame] | 16 | #include "dbus.hpp" |
| 17 | |
Matt Spinler | 66e0707 | 2018-09-12 10:36:14 -0500 | [diff] [blame] | 18 | #include <phosphor-logging/log.hpp> |
| 19 | |
Matt Spinler | bc99749 | 2018-03-27 11:24:45 -0500 | [diff] [blame] | 20 | namespace ibm |
| 21 | { |
| 22 | namespace logging |
| 23 | { |
| 24 | |
Matt Spinler | d82a6dd | 2018-05-23 11:31:22 -0500 | [diff] [blame] | 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 | |
Patrick Williams | 8123a71 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 32 | ObjectValueTree getManagedObjects(sdbusplus::bus_t& bus, |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 33 | const std::string& service, |
| 34 | const std::string& objPath) |
Matt Spinler | bc99749 | 2018-03-27 11:24:45 -0500 | [diff] [blame] | 35 | { |
| 36 | ObjectValueTree interfaces; |
| 37 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 38 | auto method = bus.new_method_call(service.c_str(), objPath.c_str(), |
| 39 | "org.freedesktop.DBus.ObjectManager", |
| 40 | "GetManagedObjects"); |
Matt Spinler | bc99749 | 2018-03-27 11:24:45 -0500 | [diff] [blame] | 41 | |
| 42 | auto reply = bus.call(method); |
| 43 | |
Matt Spinler | 9351665 | 2018-10-23 10:49:47 -0500 | [diff] [blame] | 44 | reply.read(interfaces); |
Matt Spinler | bc99749 | 2018-03-27 11:24:45 -0500 | [diff] [blame] | 45 | |
| 46 | return interfaces; |
| 47 | } |
Matt Spinler | d82a6dd | 2018-05-23 11:31:22 -0500 | [diff] [blame] | 48 | |
Patrick Williams | 8123a71 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 49 | DbusPropertyMap getAllProperties(sdbusplus::bus_t& bus, |
Matt Spinler | d82a6dd | 2018-05-23 11:31:22 -0500 | [diff] [blame] | 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 | |
Matt Spinler | 9351665 | 2018-10-23 10:49:47 -0500 | [diff] [blame] | 61 | reply.read(properties); |
Matt Spinler | d82a6dd | 2018-05-23 11:31:22 -0500 | [diff] [blame] | 62 | |
| 63 | return properties; |
| 64 | } |
| 65 | |
Patrick Williams | 8123a71 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 66 | DbusSubtree getSubtree(sdbusplus::bus_t& bus, const std::string& root, |
Matt Spinler | 7766e25 | 2018-09-12 10:37:56 -0500 | [diff] [blame] | 67 | int depth, const std::string& interface) |
Matt Spinler | d82a6dd | 2018-05-23 11:31:22 -0500 | [diff] [blame] | 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 | |
Matt Spinler | 9351665 | 2018-10-23 10:49:47 -0500 | [diff] [blame] | 78 | reply.read(tree); |
Matt Spinler | d82a6dd | 2018-05-23 11:31:22 -0500 | [diff] [blame] | 79 | |
| 80 | return tree; |
| 81 | } |
| 82 | |
| 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 = |
| 94 | std::find(entry.second.begin(), |
| 95 | entry.second.end(), interface); |
| 96 | return i != entry.second.end(); |
| 97 | }); |
| 98 | if (s != services->second.end()) |
| 99 | { |
| 100 | service = s->first; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | return service; |
| 105 | } |
Matt Spinler | 66e0707 | 2018-09-12 10:36:14 -0500 | [diff] [blame] | 106 | } // namespace logging |
| 107 | } // namespace ibm |