xref: /openbmc/phosphor-inventory-manager/functor.cpp (revision f094d4428ecad7c2c349287fb811e5d6568644a9)
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  */
16*f094d442SMatthew Barth #include "config.h"
17*f094d442SMatthew Barth 
18c1f4798dSBrad Bishop #include "functor.hpp"
19a680d1efSPatrick Venture 
20*f094d442SMatthew Barth #include "manager.hpp"
21*f094d442SMatthew Barth 
22c1f4798dSBrad Bishop #include <sdbusplus/bus.hpp>
23c1f4798dSBrad Bishop 
24c1f4798dSBrad Bishop namespace phosphor
25c1f4798dSBrad Bishop {
26c1f4798dSBrad Bishop namespace inventory
27c1f4798dSBrad Bishop {
28c1f4798dSBrad Bishop namespace manager
29c1f4798dSBrad Bishop {
30c1f4798dSBrad Bishop namespace functor
31c1f4798dSBrad Bishop {
32615b2a8fSBrad Bishop bool PropertyConditionBase::operator()(sdbusplus::bus::bus& bus,
33c1f4798dSBrad Bishop                                        sdbusplus::message::message&,
34d0f48adcSBrad Bishop                                        Manager& mgr) const
35d0f48adcSBrad Bishop {
36d0f48adcSBrad Bishop     std::string path(_path);
37d0f48adcSBrad Bishop     return (*this)(path, bus, mgr);
38d0f48adcSBrad Bishop }
39d0f48adcSBrad Bishop 
40615b2a8fSBrad Bishop bool PropertyConditionBase::operator()(const std::string& path,
41*f094d442SMatthew Barth                                        sdbusplus::bus::bus& bus,
42*f094d442SMatthew Barth                                        Manager& mgr) const
43c1f4798dSBrad Bishop {
44c1f4798dSBrad Bishop     std::string host;
45c1f4798dSBrad Bishop 
46c1f4798dSBrad Bishop     if (_service)
47c1f4798dSBrad Bishop     {
48c1f4798dSBrad Bishop         host.assign(_service);
49c1f4798dSBrad Bishop     }
50c1f4798dSBrad Bishop     else
51c1f4798dSBrad Bishop     {
52c1f4798dSBrad Bishop         auto mapperCall = bus.new_method_call(
53c1f4798dSBrad Bishop             "xyz.openbmc_project.ObjectMapper",
5482aa2232SLeonel Gonzalez             "/xyz/openbmc_project/object_mapper",
55615b2a8fSBrad Bishop             "xyz.openbmc_project.ObjectMapper", "GetObject");
56d0f48adcSBrad Bishop         mapperCall.append(path);
57c1f4798dSBrad Bishop         mapperCall.append(std::vector<std::string>({_iface}));
58c1f4798dSBrad Bishop 
59c1f4798dSBrad Bishop         auto mapperResponseMsg = bus.call(mapperCall);
60c1f4798dSBrad Bishop         if (mapperResponseMsg.is_method_error())
61c1f4798dSBrad Bishop         {
62c1f4798dSBrad Bishop             return false;
63c1f4798dSBrad Bishop         }
64c1f4798dSBrad Bishop 
65c1f4798dSBrad Bishop         std::map<std::string, std::vector<std::string>> mapperResponse;
66c1f4798dSBrad Bishop         mapperResponseMsg.read(mapperResponse);
67c1f4798dSBrad Bishop 
688ed114b6SPatrick Williams         if (mapperResponse.empty())
69c1f4798dSBrad Bishop         {
70c1f4798dSBrad Bishop             return false;
71c1f4798dSBrad Bishop         }
72c1f4798dSBrad Bishop 
73c1f4798dSBrad Bishop         host = mapperResponse.begin()->first;
74*f094d442SMatthew Barth     }
75c1f4798dSBrad Bishop 
76*f094d442SMatthew Barth     // When the host service name is inventory manager, eval using
77*f094d442SMatthew Barth     // a given `getProperty` function to retrieve a property value from
78*f094d442SMatthew Barth     // an interface hosted by inventory manager.
79*f094d442SMatthew Barth     if (host == BUSNAME)
80c1f4798dSBrad Bishop     {
81*f094d442SMatthew Barth         try
82*f094d442SMatthew Barth         {
83*f094d442SMatthew Barth             return eval(mgr);
84*f094d442SMatthew Barth         }
85*f094d442SMatthew Barth         catch (const std::exception& e)
86*f094d442SMatthew Barth         {
87*f094d442SMatthew Barth             // Unable to find property on inventory manager,
88*f094d442SMatthew Barth             // default condition to false.
89c1f4798dSBrad Bishop             return false;
90c1f4798dSBrad Bishop         }
91c1f4798dSBrad Bishop     }
92*f094d442SMatthew Barth 
93c1f4798dSBrad Bishop     auto hostCall = bus.new_method_call(
94615b2a8fSBrad Bishop         host.c_str(), path.c_str(), "org.freedesktop.DBus.Properties", "Get");
95c1f4798dSBrad Bishop     hostCall.append(_iface);
96c1f4798dSBrad Bishop     hostCall.append(_property);
97c1f4798dSBrad Bishop 
98c1f4798dSBrad Bishop     auto hostResponseMsg = bus.call(hostCall);
99c1f4798dSBrad Bishop     if (hostResponseMsg.is_method_error())
100c1f4798dSBrad Bishop     {
101c1f4798dSBrad Bishop         return false;
102c1f4798dSBrad Bishop     }
103c1f4798dSBrad Bishop 
104c1f4798dSBrad Bishop     return eval(hostResponseMsg);
105c1f4798dSBrad Bishop }
106c1f4798dSBrad Bishop 
107c1f4798dSBrad Bishop } // namespace functor
108c1f4798dSBrad Bishop } // namespace manager
109c1f4798dSBrad Bishop } // namespace inventory
110c1f4798dSBrad Bishop } // namespace phosphor
111c1f4798dSBrad Bishop 
112c1f4798dSBrad Bishop // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
113