blob: 90d713ab47033be75cd7a0de6679871c85c9bf9a [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 */
Brad Bishopc1f47982017-02-09 01:27:38 -050016#include "functor.hpp"
Patrick Venturea680d1e2018-10-14 13:34:26 -070017
Brad Bishopfa51da72017-01-19 11:06:51 -050018#include <sdbusplus/bus.hpp>
19
20namespace phosphor
21{
22namespace inventory
23{
24namespace manager
25{
Brad Bishopc1f47982017-02-09 01:27:38 -050026namespace functor
Brad Bishopfa51da72017-01-19 11:06:51 -050027{
Brad Bishop615b2a82018-03-29 10:32:41 -040028bool PropertyConditionBase::operator()(sdbusplus::bus::bus& bus,
29 sdbusplus::message::message&,
30 Manager& mgr) const
Brad Bishopd0f48ad2017-01-30 08:52:26 -050031{
32 std::string path(_path);
33 return (*this)(path, bus, mgr);
34}
35
Brad Bishop615b2a82018-03-29 10:32:41 -040036bool PropertyConditionBase::operator()(const std::string& path,
37 sdbusplus::bus::bus& bus, Manager&) const
Brad Bishopfa51da72017-01-19 11:06:51 -050038{
39 std::string host;
40
41 if (_service)
42 {
43 host.assign(_service);
44 }
45 else
46 {
47 auto mapperCall = bus.new_method_call(
Brad Bishop615b2a82018-03-29 10:32:41 -040048 "xyz.openbmc_project.ObjectMapper",
49 "/xyz/openbmc_project/object_mapper",
50 "xyz.openbmc_project.ObjectMapper", "GetObject");
Brad Bishopd0f48ad2017-01-30 08:52:26 -050051 mapperCall.append(path);
Brad Bishopfa51da72017-01-19 11:06:51 -050052 mapperCall.append(std::vector<std::string>({_iface}));
53
54 auto mapperResponseMsg = bus.call(mapperCall);
55 if (mapperResponseMsg.is_method_error())
56 {
57 return false;
58 }
59
60 std::map<std::string, std::vector<std::string>> mapperResponse;
61 mapperResponseMsg.read(mapperResponse);
62
Patrick Williams8ed114b2017-03-09 15:28:43 -060063 if (mapperResponse.empty())
Brad Bishopfa51da72017-01-19 11:06:51 -050064 {
65 return false;
66 }
67
68 host = mapperResponse.begin()->first;
69
70 if (host == bus.get_unique_name())
71 {
72 // TODO I can't call myself here.
73 return false;
74 }
75 }
76 auto hostCall = bus.new_method_call(
Brad Bishop615b2a82018-03-29 10:32:41 -040077 host.c_str(), path.c_str(), "org.freedesktop.DBus.Properties", "Get");
Brad Bishopfa51da72017-01-19 11:06:51 -050078 hostCall.append(_iface);
79 hostCall.append(_property);
80
81 auto hostResponseMsg = bus.call(hostCall);
82 if (hostResponseMsg.is_method_error())
83 {
84 return false;
85 }
86
87 return eval(hostResponseMsg);
88}
89
Brad Bishopc1f47982017-02-09 01:27:38 -050090} // namespace functor
Brad Bishopfa51da72017-01-19 11:06:51 -050091} // namespace manager
92} // namespace inventory
93} // namespace phosphor
94
95// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4