blob: e8d535e1ce127ae23dd2ed2a9fa45e27f7e57250 [file] [log] [blame]
Brad Bishopfa51da72017-01-19 11:06:51 -05001/**
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 Barthf094d442018-09-26 15:06:15 -050016#include "config.h"
17
Brad Bishopc1f47982017-02-09 01:27:38 -050018#include "functor.hpp"
Patrick Venturea680d1e2018-10-14 13:34:26 -070019
Matthew Barthf094d442018-09-26 15:06:15 -050020#include "manager.hpp"
21
George Liu66431fe2022-04-14 14:51:42 +080022#include <phosphor-logging/lg2.hpp>
Brad Bishopfa51da72017-01-19 11:06:51 -050023#include <sdbusplus/bus.hpp>
24
25namespace phosphor
26{
27namespace inventory
28{
29namespace manager
30{
Brad Bishopc1f47982017-02-09 01:27:38 -050031namespace functor
Brad Bishopfa51da72017-01-19 11:06:51 -050032{
Patrick Williamsd8fba8b2024-08-16 15:20:15 -040033bool PropertyConditionBase::operator()(
34 sdbusplus::bus_t& bus, sdbusplus::message_t&, Manager& mgr) const
Brad Bishopd0f48ad2017-01-30 08:52:26 -050035{
36 std::string path(_path);
37 return (*this)(path, bus, mgr);
38}
39
Patrick Williamsd8fba8b2024-08-16 15:20:15 -040040bool PropertyConditionBase::operator()(
41 const std::string& path, sdbusplus::bus_t& bus, Manager& mgr) const
Brad Bishopfa51da72017-01-19 11:06:51 -050042{
43 std::string host;
44
45 if (_service)
46 {
47 host.assign(_service);
48 }
49 else
50 {
51 auto mapperCall = bus.new_method_call(
Brad Bishop615b2a82018-03-29 10:32:41 -040052 "xyz.openbmc_project.ObjectMapper",
53 "/xyz/openbmc_project/object_mapper",
54 "xyz.openbmc_project.ObjectMapper", "GetObject");
Brad Bishopd0f48ad2017-01-30 08:52:26 -050055 mapperCall.append(path);
Brad Bishopfa51da72017-01-19 11:06:51 -050056 mapperCall.append(std::vector<std::string>({_iface}));
57
George Liu66431fe2022-04-14 14:51:42 +080058 std::map<std::string, std::vector<std::string>> mapperResponse;
59 try
Brad Bishopfa51da72017-01-19 11:06:51 -050060 {
George Liu66431fe2022-04-14 14:51:42 +080061 auto mapperResponseMsg = bus.call(mapperCall);
62 mapperResponseMsg.read(mapperResponse);
63 }
64 catch (const std::exception& e)
65 {
66 lg2::error("Failed to execute GetObject method: {ERROR}", "ERROR",
67 e);
Brad Bishopfa51da72017-01-19 11:06:51 -050068 return false;
69 }
70
Patrick Williams8ed114b2017-03-09 15:28:43 -060071 if (mapperResponse.empty())
Brad Bishopfa51da72017-01-19 11:06:51 -050072 {
73 return false;
74 }
75
76 host = mapperResponse.begin()->first;
Matthew Barthf094d442018-09-26 15:06:15 -050077 }
Brad Bishopfa51da72017-01-19 11:06:51 -050078
Matthew Barthf094d442018-09-26 15:06:15 -050079 // When the host service name is inventory manager, eval using
80 // a given `getProperty` function to retrieve a property value from
81 // an interface hosted by inventory manager.
82 if (host == BUSNAME)
83 {
84 try
Brad Bishopfa51da72017-01-19 11:06:51 -050085 {
Matthew Barthf094d442018-09-26 15:06:15 -050086 return eval(mgr);
87 }
88 catch (const std::exception& e)
89 {
90 // Unable to find property on inventory manager,
91 // default condition to false.
Brad Bishopfa51da72017-01-19 11:06:51 -050092 return false;
93 }
94 }
Matthew Barthf094d442018-09-26 15:06:15 -050095
Brad Bishopfa51da72017-01-19 11:06:51 -050096 auto hostCall = bus.new_method_call(
Brad Bishop615b2a82018-03-29 10:32:41 -040097 host.c_str(), path.c_str(), "org.freedesktop.DBus.Properties", "Get");
Brad Bishopfa51da72017-01-19 11:06:51 -050098 hostCall.append(_iface);
99 hostCall.append(_property);
100
George Liu66431fe2022-04-14 14:51:42 +0800101 try
Brad Bishopfa51da72017-01-19 11:06:51 -0500102 {
George Liu66431fe2022-04-14 14:51:42 +0800103 auto hostResponseMsg = bus.call(hostCall);
104 return eval(hostResponseMsg);
105 }
106 catch (const std::exception& e)
107 {
108 lg2::error("Failed to execute Get method: {ERROR}", "ERROR", e);
Brad Bishopfa51da72017-01-19 11:06:51 -0500109 return false;
110 }
Brad Bishopfa51da72017-01-19 11:06:51 -0500111}
112
Brad Bishopc1f47982017-02-09 01:27:38 -0500113} // namespace functor
Brad Bishopfa51da72017-01-19 11:06:51 -0500114} // namespace manager
115} // namespace inventory
116} // namespace phosphor