blob: b8771d729a0e22e074b099d151c5bfc3d097cebf [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
Brad Bishopfa51da72017-01-19 11:06:51 -050022#include <sdbusplus/bus.hpp>
23
24namespace phosphor
25{
26namespace inventory
27{
28namespace manager
29{
Brad Bishopc1f47982017-02-09 01:27:38 -050030namespace functor
Brad Bishopfa51da72017-01-19 11:06:51 -050031{
Brad Bishop615b2a82018-03-29 10:32:41 -040032bool PropertyConditionBase::operator()(sdbusplus::bus::bus& bus,
33 sdbusplus::message::message&,
34 Manager& mgr) const
Brad Bishopd0f48ad2017-01-30 08:52:26 -050035{
36 std::string path(_path);
37 return (*this)(path, bus, mgr);
38}
39
Brad Bishop615b2a82018-03-29 10:32:41 -040040bool PropertyConditionBase::operator()(const std::string& path,
Matthew Barthf094d442018-09-26 15:06:15 -050041 sdbusplus::bus::bus& bus,
42 Manager& mgr) const
Brad Bishopfa51da72017-01-19 11:06:51 -050043{
44 std::string host;
45
46 if (_service)
47 {
48 host.assign(_service);
49 }
50 else
51 {
52 auto mapperCall = bus.new_method_call(
Brad Bishop615b2a82018-03-29 10:32:41 -040053 "xyz.openbmc_project.ObjectMapper",
54 "/xyz/openbmc_project/object_mapper",
55 "xyz.openbmc_project.ObjectMapper", "GetObject");
Brad Bishopd0f48ad2017-01-30 08:52:26 -050056 mapperCall.append(path);
Brad Bishopfa51da72017-01-19 11:06:51 -050057 mapperCall.append(std::vector<std::string>({_iface}));
58
59 auto mapperResponseMsg = bus.call(mapperCall);
60 if (mapperResponseMsg.is_method_error())
61 {
62 return false;
63 }
64
65 std::map<std::string, std::vector<std::string>> mapperResponse;
66 mapperResponseMsg.read(mapperResponse);
67
Patrick Williams8ed114b2017-03-09 15:28:43 -060068 if (mapperResponse.empty())
Brad Bishopfa51da72017-01-19 11:06:51 -050069 {
70 return false;
71 }
72
73 host = mapperResponse.begin()->first;
Matthew Barthf094d442018-09-26 15:06:15 -050074 }
Brad Bishopfa51da72017-01-19 11:06:51 -050075
Matthew Barthf094d442018-09-26 15:06:15 -050076 // When the host service name is inventory manager, eval using
77 // a given `getProperty` function to retrieve a property value from
78 // an interface hosted by inventory manager.
79 if (host == BUSNAME)
80 {
81 try
Brad Bishopfa51da72017-01-19 11:06:51 -050082 {
Matthew Barthf094d442018-09-26 15:06:15 -050083 return eval(mgr);
84 }
85 catch (const std::exception& e)
86 {
87 // Unable to find property on inventory manager,
88 // default condition to false.
Brad Bishopfa51da72017-01-19 11:06:51 -050089 return false;
90 }
91 }
Matthew Barthf094d442018-09-26 15:06:15 -050092
Brad Bishopfa51da72017-01-19 11:06:51 -050093 auto hostCall = bus.new_method_call(
Brad Bishop615b2a82018-03-29 10:32:41 -040094 host.c_str(), path.c_str(), "org.freedesktop.DBus.Properties", "Get");
Brad Bishopfa51da72017-01-19 11:06:51 -050095 hostCall.append(_iface);
96 hostCall.append(_property);
97
98 auto hostResponseMsg = bus.call(hostCall);
99 if (hostResponseMsg.is_method_error())
100 {
101 return false;
102 }
103
104 return eval(hostResponseMsg);
105}
106
Brad Bishopc1f47982017-02-09 01:27:38 -0500107} // namespace functor
Brad Bishopfa51da72017-01-19 11:06:51 -0500108} // namespace manager
109} // namespace inventory
110} // namespace phosphor
111
112// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4