blob: 272bd1463aea24f505c18ddc25fc2ad38f777e76 [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 Williams563306f2022-07-22 19:26:52 -050033bool PropertyConditionBase::operator()(sdbusplus::bus_t& bus,
34 sdbusplus::message_t&,
Brad Bishop615b2a82018-03-29 10:32:41 -040035 Manager& mgr) const
Brad Bishopd0f48ad2017-01-30 08:52:26 -050036{
37 std::string path(_path);
38 return (*this)(path, bus, mgr);
39}
40
Brad Bishop615b2a82018-03-29 10:32:41 -040041bool PropertyConditionBase::operator()(const std::string& path,
Patrick Williams563306f2022-07-22 19:26:52 -050042 sdbusplus::bus_t& bus,
Matthew Barthf094d442018-09-26 15:06:15 -050043 Manager& mgr) const
Brad Bishopfa51da72017-01-19 11:06:51 -050044{
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 Bishop615b2a82018-03-29 10:32:41 -040054 "xyz.openbmc_project.ObjectMapper",
55 "/xyz/openbmc_project/object_mapper",
56 "xyz.openbmc_project.ObjectMapper", "GetObject");
Brad Bishopd0f48ad2017-01-30 08:52:26 -050057 mapperCall.append(path);
Brad Bishopfa51da72017-01-19 11:06:51 -050058 mapperCall.append(std::vector<std::string>({_iface}));
59
George Liu66431fe2022-04-14 14:51:42 +080060 std::map<std::string, std::vector<std::string>> mapperResponse;
61 try
Brad Bishopfa51da72017-01-19 11:06:51 -050062 {
George Liu66431fe2022-04-14 14:51:42 +080063 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 Bishopfa51da72017-01-19 11:06:51 -050070 return false;
71 }
72
Patrick Williams8ed114b2017-03-09 15:28:43 -060073 if (mapperResponse.empty())
Brad Bishopfa51da72017-01-19 11:06:51 -050074 {
75 return false;
76 }
77
78 host = mapperResponse.begin()->first;
Matthew Barthf094d442018-09-26 15:06:15 -050079 }
Brad Bishopfa51da72017-01-19 11:06:51 -050080
Matthew Barthf094d442018-09-26 15:06:15 -050081 // 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 Bishopfa51da72017-01-19 11:06:51 -050087 {
Matthew Barthf094d442018-09-26 15:06:15 -050088 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 Bishopfa51da72017-01-19 11:06:51 -050094 return false;
95 }
96 }
Matthew Barthf094d442018-09-26 15:06:15 -050097
Brad Bishopfa51da72017-01-19 11:06:51 -050098 auto hostCall = bus.new_method_call(
Brad Bishop615b2a82018-03-29 10:32:41 -040099 host.c_str(), path.c_str(), "org.freedesktop.DBus.Properties", "Get");
Brad Bishopfa51da72017-01-19 11:06:51 -0500100 hostCall.append(_iface);
101 hostCall.append(_property);
102
George Liu66431fe2022-04-14 14:51:42 +0800103 try
Brad Bishopfa51da72017-01-19 11:06:51 -0500104 {
George Liu66431fe2022-04-14 14:51:42 +0800105 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 Bishopfa51da72017-01-19 11:06:51 -0500111 return false;
112 }
Brad Bishopfa51da72017-01-19 11:06:51 -0500113}
114
Brad Bishopc1f47982017-02-09 01:27:38 -0500115} // namespace functor
Brad Bishopfa51da72017-01-19 11:06:51 -0500116} // namespace manager
117} // namespace inventory
118} // namespace phosphor
119
120// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4