Brad Bishop | fa51da7 | 2017-01-19 11:06:51 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2017 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 | */ |
Matthew Barth | f094d44 | 2018-09-26 15:06:15 -0500 | [diff] [blame] | 16 | #include "config.h" |
| 17 | |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 18 | #include "functor.hpp" |
Patrick Venture | a680d1e | 2018-10-14 13:34:26 -0700 | [diff] [blame] | 19 | |
Matthew Barth | f094d44 | 2018-09-26 15:06:15 -0500 | [diff] [blame] | 20 | #include "manager.hpp" |
| 21 | |
George Liu | 66431fe | 2022-04-14 14:51:42 +0800 | [diff] [blame] | 22 | #include <phosphor-logging/lg2.hpp> |
Brad Bishop | fa51da7 | 2017-01-19 11:06:51 -0500 | [diff] [blame] | 23 | #include <sdbusplus/bus.hpp> |
| 24 | |
| 25 | namespace phosphor |
| 26 | { |
| 27 | namespace inventory |
| 28 | { |
| 29 | namespace manager |
| 30 | { |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 31 | namespace functor |
Brad Bishop | fa51da7 | 2017-01-19 11:06:51 -0500 | [diff] [blame] | 32 | { |
Patrick Williams | 563306f | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 33 | bool PropertyConditionBase::operator()(sdbusplus::bus_t& bus, |
| 34 | sdbusplus::message_t&, |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 35 | Manager& mgr) const |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 36 | { |
| 37 | std::string path(_path); |
| 38 | return (*this)(path, bus, mgr); |
| 39 | } |
| 40 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 41 | bool PropertyConditionBase::operator()(const std::string& path, |
Patrick Williams | 563306f | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 42 | sdbusplus::bus_t& bus, |
Matthew Barth | f094d44 | 2018-09-26 15:06:15 -0500 | [diff] [blame] | 43 | Manager& mgr) const |
Brad Bishop | fa51da7 | 2017-01-19 11:06:51 -0500 | [diff] [blame] | 44 | { |
| 45 | std::string host; |
| 46 | |
| 47 | if (_service) |
| 48 | { |
| 49 | host.assign(_service); |
| 50 | } |
| 51 | else |
| 52 | { |
| 53 | auto mapperCall = bus.new_method_call( |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 54 | "xyz.openbmc_project.ObjectMapper", |
| 55 | "/xyz/openbmc_project/object_mapper", |
| 56 | "xyz.openbmc_project.ObjectMapper", "GetObject"); |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 57 | mapperCall.append(path); |
Brad Bishop | fa51da7 | 2017-01-19 11:06:51 -0500 | [diff] [blame] | 58 | mapperCall.append(std::vector<std::string>({_iface})); |
| 59 | |
George Liu | 66431fe | 2022-04-14 14:51:42 +0800 | [diff] [blame] | 60 | std::map<std::string, std::vector<std::string>> mapperResponse; |
| 61 | try |
Brad Bishop | fa51da7 | 2017-01-19 11:06:51 -0500 | [diff] [blame] | 62 | { |
George Liu | 66431fe | 2022-04-14 14:51:42 +0800 | [diff] [blame] | 63 | auto mapperResponseMsg = bus.call(mapperCall); |
| 64 | mapperResponseMsg.read(mapperResponse); |
| 65 | } |
| 66 | catch (const std::exception& e) |
| 67 | { |
| 68 | lg2::error("Failed to execute GetObject method: {ERROR}", "ERROR", |
| 69 | e); |
Brad Bishop | fa51da7 | 2017-01-19 11:06:51 -0500 | [diff] [blame] | 70 | return false; |
| 71 | } |
| 72 | |
Patrick Williams | 8ed114b | 2017-03-09 15:28:43 -0600 | [diff] [blame] | 73 | if (mapperResponse.empty()) |
Brad Bishop | fa51da7 | 2017-01-19 11:06:51 -0500 | [diff] [blame] | 74 | { |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | host = mapperResponse.begin()->first; |
Matthew Barth | f094d44 | 2018-09-26 15:06:15 -0500 | [diff] [blame] | 79 | } |
Brad Bishop | fa51da7 | 2017-01-19 11:06:51 -0500 | [diff] [blame] | 80 | |
Matthew Barth | f094d44 | 2018-09-26 15:06:15 -0500 | [diff] [blame] | 81 | // When the host service name is inventory manager, eval using |
| 82 | // a given `getProperty` function to retrieve a property value from |
| 83 | // an interface hosted by inventory manager. |
| 84 | if (host == BUSNAME) |
| 85 | { |
| 86 | try |
Brad Bishop | fa51da7 | 2017-01-19 11:06:51 -0500 | [diff] [blame] | 87 | { |
Matthew Barth | f094d44 | 2018-09-26 15:06:15 -0500 | [diff] [blame] | 88 | return eval(mgr); |
| 89 | } |
| 90 | catch (const std::exception& e) |
| 91 | { |
| 92 | // Unable to find property on inventory manager, |
| 93 | // default condition to false. |
Brad Bishop | fa51da7 | 2017-01-19 11:06:51 -0500 | [diff] [blame] | 94 | return false; |
| 95 | } |
| 96 | } |
Matthew Barth | f094d44 | 2018-09-26 15:06:15 -0500 | [diff] [blame] | 97 | |
Brad Bishop | fa51da7 | 2017-01-19 11:06:51 -0500 | [diff] [blame] | 98 | auto hostCall = bus.new_method_call( |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 99 | host.c_str(), path.c_str(), "org.freedesktop.DBus.Properties", "Get"); |
Brad Bishop | fa51da7 | 2017-01-19 11:06:51 -0500 | [diff] [blame] | 100 | hostCall.append(_iface); |
| 101 | hostCall.append(_property); |
| 102 | |
George Liu | 66431fe | 2022-04-14 14:51:42 +0800 | [diff] [blame] | 103 | try |
Brad Bishop | fa51da7 | 2017-01-19 11:06:51 -0500 | [diff] [blame] | 104 | { |
George Liu | 66431fe | 2022-04-14 14:51:42 +0800 | [diff] [blame] | 105 | auto hostResponseMsg = bus.call(hostCall); |
| 106 | return eval(hostResponseMsg); |
| 107 | } |
| 108 | catch (const std::exception& e) |
| 109 | { |
| 110 | lg2::error("Failed to execute Get method: {ERROR}", "ERROR", e); |
Brad Bishop | fa51da7 | 2017-01-19 11:06:51 -0500 | [diff] [blame] | 111 | return false; |
| 112 | } |
Brad Bishop | fa51da7 | 2017-01-19 11:06:51 -0500 | [diff] [blame] | 113 | } |
| 114 | |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 115 | } // namespace functor |
Brad Bishop | fa51da7 | 2017-01-19 11:06:51 -0500 | [diff] [blame] | 116 | } // namespace manager |
| 117 | } // namespace inventory |
| 118 | } // namespace phosphor |
| 119 | |
| 120 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |