xref: /openbmc/phosphor-inventory-manager/functor.cpp (revision a680d1eff9ee796a294761dbdffe62cae4cda161)
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"
17*a680d1efSPatrick Venture 
18c1f4798dSBrad Bishop #include <sdbusplus/bus.hpp>
19c1f4798dSBrad Bishop 
20c1f4798dSBrad Bishop namespace phosphor
21c1f4798dSBrad Bishop {
22c1f4798dSBrad Bishop namespace inventory
23c1f4798dSBrad Bishop {
24c1f4798dSBrad Bishop namespace manager
25c1f4798dSBrad Bishop {
26c1f4798dSBrad Bishop namespace functor
27c1f4798dSBrad Bishop {
28615b2a8fSBrad Bishop bool PropertyConditionBase::operator()(sdbusplus::bus::bus& bus,
29c1f4798dSBrad Bishop                                        sdbusplus::message::message&,
30d0f48adcSBrad Bishop                                        Manager& mgr) const
31d0f48adcSBrad Bishop {
32d0f48adcSBrad Bishop     std::string path(_path);
33d0f48adcSBrad Bishop     return (*this)(path, bus, mgr);
34d0f48adcSBrad Bishop }
35d0f48adcSBrad Bishop 
36615b2a8fSBrad Bishop bool PropertyConditionBase::operator()(const std::string& path,
37615b2a8fSBrad Bishop                                        sdbusplus::bus::bus& bus, Manager&) const
38c1f4798dSBrad Bishop {
39c1f4798dSBrad Bishop     std::string host;
40c1f4798dSBrad Bishop 
41c1f4798dSBrad Bishop     if (_service)
42c1f4798dSBrad Bishop     {
43c1f4798dSBrad Bishop         host.assign(_service);
44c1f4798dSBrad Bishop     }
45c1f4798dSBrad Bishop     else
46c1f4798dSBrad Bishop     {
47c1f4798dSBrad Bishop         auto mapperCall = bus.new_method_call(
48c1f4798dSBrad Bishop             "xyz.openbmc_project.ObjectMapper",
4982aa2232SLeonel Gonzalez             "/xyz/openbmc_project/object_mapper",
50615b2a8fSBrad Bishop             "xyz.openbmc_project.ObjectMapper", "GetObject");
51d0f48adcSBrad Bishop         mapperCall.append(path);
52c1f4798dSBrad Bishop         mapperCall.append(std::vector<std::string>({_iface}));
53c1f4798dSBrad Bishop 
54c1f4798dSBrad Bishop         auto mapperResponseMsg = bus.call(mapperCall);
55c1f4798dSBrad Bishop         if (mapperResponseMsg.is_method_error())
56c1f4798dSBrad Bishop         {
57c1f4798dSBrad Bishop             return false;
58c1f4798dSBrad Bishop         }
59c1f4798dSBrad Bishop 
60c1f4798dSBrad Bishop         std::map<std::string, std::vector<std::string>> mapperResponse;
61c1f4798dSBrad Bishop         mapperResponseMsg.read(mapperResponse);
62c1f4798dSBrad Bishop 
638ed114b6SPatrick Williams         if (mapperResponse.empty())
64c1f4798dSBrad Bishop         {
65c1f4798dSBrad Bishop             return false;
66c1f4798dSBrad Bishop         }
67c1f4798dSBrad Bishop 
68c1f4798dSBrad Bishop         host = mapperResponse.begin()->first;
69c1f4798dSBrad Bishop 
70c1f4798dSBrad Bishop         if (host == bus.get_unique_name())
71c1f4798dSBrad Bishop         {
72c1f4798dSBrad Bishop             // TODO I can't call myself here.
73c1f4798dSBrad Bishop             return false;
74c1f4798dSBrad Bishop         }
75c1f4798dSBrad Bishop     }
76c1f4798dSBrad Bishop     auto hostCall = bus.new_method_call(
77615b2a8fSBrad Bishop         host.c_str(), path.c_str(), "org.freedesktop.DBus.Properties", "Get");
78c1f4798dSBrad Bishop     hostCall.append(_iface);
79c1f4798dSBrad Bishop     hostCall.append(_property);
80c1f4798dSBrad Bishop 
81c1f4798dSBrad Bishop     auto hostResponseMsg = bus.call(hostCall);
82c1f4798dSBrad Bishop     if (hostResponseMsg.is_method_error())
83c1f4798dSBrad Bishop     {
84c1f4798dSBrad Bishop         return false;
85c1f4798dSBrad Bishop     }
86c1f4798dSBrad Bishop 
87c1f4798dSBrad Bishop     return eval(hostResponseMsg);
88c1f4798dSBrad Bishop }
89c1f4798dSBrad Bishop 
90c1f4798dSBrad Bishop } // namespace functor
91c1f4798dSBrad Bishop } // namespace manager
92c1f4798dSBrad Bishop } // namespace inventory
93c1f4798dSBrad Bishop } // namespace phosphor
94c1f4798dSBrad Bishop 
95c1f4798dSBrad Bishop // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
96