xref: /openbmc/phosphor-inventory-manager/functor.cpp (revision 615b2a8f8cc0cffc5773cb40f7f3d74b8ea1e81f)
1c1f4798dSBrad Bishop /**
2c1f4798dSBrad Bishop  * Copyright © 2017 IBM Corporation
3c1f4798dSBrad Bishop  *
4c1f4798dSBrad Bishop  * Licensed under the Apache License, Version 2.0 (the "License");
5c1f4798dSBrad Bishop  * you may not use this file except in compliance with the License.
6c1f4798dSBrad Bishop  * You may obtain a copy of the License at
7c1f4798dSBrad Bishop  *
8c1f4798dSBrad Bishop  *     http://www.apache.org/licenses/LICENSE-2.0
9c1f4798dSBrad Bishop  *
10c1f4798dSBrad Bishop  * Unless required by applicable law or agreed to in writing, software
11c1f4798dSBrad Bishop  * distributed under the License is distributed on an "AS IS" BASIS,
12c1f4798dSBrad Bishop  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c1f4798dSBrad Bishop  * See the License for the specific language governing permissions and
14c1f4798dSBrad Bishop  * limitations under the License.
15c1f4798dSBrad Bishop  */
16c1f4798dSBrad Bishop #include "functor.hpp"
17c1f4798dSBrad Bishop #include <sdbusplus/bus.hpp>
18c1f4798dSBrad Bishop 
19c1f4798dSBrad Bishop namespace phosphor
20c1f4798dSBrad Bishop {
21c1f4798dSBrad Bishop namespace inventory
22c1f4798dSBrad Bishop {
23c1f4798dSBrad Bishop namespace manager
24c1f4798dSBrad Bishop {
25c1f4798dSBrad Bishop namespace functor
26c1f4798dSBrad Bishop {
27*615b2a8fSBrad Bishop bool PropertyConditionBase::operator()(sdbusplus::bus::bus& bus,
28c1f4798dSBrad Bishop                                        sdbusplus::message::message&,
29d0f48adcSBrad Bishop                                        Manager& mgr) const
30d0f48adcSBrad Bishop {
31d0f48adcSBrad Bishop     std::string path(_path);
32d0f48adcSBrad Bishop     return (*this)(path, bus, mgr);
33d0f48adcSBrad Bishop }
34d0f48adcSBrad Bishop 
35*615b2a8fSBrad Bishop bool PropertyConditionBase::operator()(const std::string& path,
36*615b2a8fSBrad Bishop                                        sdbusplus::bus::bus& bus, Manager&) const
37c1f4798dSBrad Bishop {
38c1f4798dSBrad Bishop     std::string host;
39c1f4798dSBrad Bishop 
40c1f4798dSBrad Bishop     if (_service)
41c1f4798dSBrad Bishop     {
42c1f4798dSBrad Bishop         host.assign(_service);
43c1f4798dSBrad Bishop     }
44c1f4798dSBrad Bishop     else
45c1f4798dSBrad Bishop     {
46c1f4798dSBrad Bishop         auto mapperCall = bus.new_method_call(
47c1f4798dSBrad Bishop             "xyz.openbmc_project.ObjectMapper",
4882aa2232SLeonel Gonzalez             "/xyz/openbmc_project/object_mapper",
49*615b2a8fSBrad Bishop             "xyz.openbmc_project.ObjectMapper", "GetObject");
50d0f48adcSBrad Bishop         mapperCall.append(path);
51c1f4798dSBrad Bishop         mapperCall.append(std::vector<std::string>({_iface}));
52c1f4798dSBrad Bishop 
53c1f4798dSBrad Bishop         auto mapperResponseMsg = bus.call(mapperCall);
54c1f4798dSBrad Bishop         if (mapperResponseMsg.is_method_error())
55c1f4798dSBrad Bishop         {
56c1f4798dSBrad Bishop             return false;
57c1f4798dSBrad Bishop         }
58c1f4798dSBrad Bishop 
59c1f4798dSBrad Bishop         std::map<std::string, std::vector<std::string>> mapperResponse;
60c1f4798dSBrad Bishop         mapperResponseMsg.read(mapperResponse);
61c1f4798dSBrad Bishop 
628ed114b6SPatrick Williams         if (mapperResponse.empty())
63c1f4798dSBrad Bishop         {
64c1f4798dSBrad Bishop             return false;
65c1f4798dSBrad Bishop         }
66c1f4798dSBrad Bishop 
67c1f4798dSBrad Bishop         host = mapperResponse.begin()->first;
68c1f4798dSBrad Bishop 
69c1f4798dSBrad Bishop         if (host == bus.get_unique_name())
70c1f4798dSBrad Bishop         {
71c1f4798dSBrad Bishop             // TODO I can't call myself here.
72c1f4798dSBrad Bishop             return false;
73c1f4798dSBrad Bishop         }
74c1f4798dSBrad Bishop     }
75c1f4798dSBrad Bishop     auto hostCall = bus.new_method_call(
76*615b2a8fSBrad Bishop         host.c_str(), path.c_str(), "org.freedesktop.DBus.Properties", "Get");
77c1f4798dSBrad Bishop     hostCall.append(_iface);
78c1f4798dSBrad Bishop     hostCall.append(_property);
79c1f4798dSBrad Bishop 
80c1f4798dSBrad Bishop     auto hostResponseMsg = bus.call(hostCall);
81c1f4798dSBrad Bishop     if (hostResponseMsg.is_method_error())
82c1f4798dSBrad Bishop     {
83c1f4798dSBrad Bishop         return false;
84c1f4798dSBrad Bishop     }
85c1f4798dSBrad Bishop 
86c1f4798dSBrad Bishop     return eval(hostResponseMsg);
87c1f4798dSBrad Bishop }
88c1f4798dSBrad Bishop 
89c1f4798dSBrad Bishop } // namespace functor
90c1f4798dSBrad Bishop } // namespace manager
91c1f4798dSBrad Bishop } // namespace inventory
92c1f4798dSBrad Bishop } // namespace phosphor
93c1f4798dSBrad Bishop 
94c1f4798dSBrad Bishop // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
95