blob: ffe272b71d9ef343954d0542506e3b3ab2bf91d6 [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"
Brad Bishopfa51da72017-01-19 11:06:51 -050017#include <sdbusplus/bus.hpp>
18
19namespace phosphor
20{
21namespace inventory
22{
23namespace manager
24{
Brad Bishopc1f47982017-02-09 01:27:38 -050025namespace functor
Brad Bishopfa51da72017-01-19 11:06:51 -050026{
Brad Bishopfa51da72017-01-19 11:06:51 -050027bool PropertyConditionBase::operator()(
28 sdbusplus::bus::bus& bus,
29 sdbusplus::message::message&,
Brad Bishopd0f48ad2017-01-30 08:52:26 -050030 Manager& mgr) const
31{
32 std::string path(_path);
33 return (*this)(path, bus, mgr);
34}
35
36bool PropertyConditionBase::operator()(
37 const std::string& path,
38 sdbusplus::bus::bus& bus,
Brad Bishopfa51da72017-01-19 11:06:51 -050039 Manager&) const
40{
41 std::string host;
42
43 if (_service)
44 {
45 host.assign(_service);
46 }
47 else
48 {
49 auto mapperCall = bus.new_method_call(
50 "xyz.openbmc_project.ObjectMapper",
Leonel Gonzalez82aa2232017-03-16 13:47:55 -050051 "/xyz/openbmc_project/object_mapper",
Brad Bishopfa51da72017-01-19 11:06:51 -050052 "xyz.openbmc_project.ObjectMapper",
53 "GetObject");
Brad Bishopd0f48ad2017-01-30 08:52:26 -050054 mapperCall.append(path);
Brad Bishopfa51da72017-01-19 11:06:51 -050055 mapperCall.append(std::vector<std::string>({_iface}));
56
57 auto mapperResponseMsg = bus.call(mapperCall);
58 if (mapperResponseMsg.is_method_error())
59 {
60 return false;
61 }
62
63 std::map<std::string, std::vector<std::string>> mapperResponse;
64 mapperResponseMsg.read(mapperResponse);
65
Patrick Williams8ed114b2017-03-09 15:28:43 -060066 if (mapperResponse.empty())
Brad Bishopfa51da72017-01-19 11:06:51 -050067 {
68 return false;
69 }
70
71 host = mapperResponse.begin()->first;
72
73 if (host == bus.get_unique_name())
74 {
75 // TODO I can't call myself here.
76 return false;
77 }
78 }
79 auto hostCall = bus.new_method_call(
80 host.c_str(),
Brad Bishopd0f48ad2017-01-30 08:52:26 -050081 path.c_str(),
Brad Bishopfa51da72017-01-19 11:06:51 -050082 "org.freedesktop.DBus.Properties",
83 "Get");
84 hostCall.append(_iface);
85 hostCall.append(_property);
86
87 auto hostResponseMsg = bus.call(hostCall);
88 if (hostResponseMsg.is_method_error())
89 {
90 return false;
91 }
92
93 return eval(hostResponseMsg);
94}
95
Brad Bishopc1f47982017-02-09 01:27:38 -050096} // namespace functor
Brad Bishopfa51da72017-01-19 11:06:51 -050097} // namespace manager
98} // namespace inventory
99} // namespace phosphor
100
101// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4