xref: /openbmc/phosphor-inventory-manager/functor.cpp (revision 51aff45e80ec084fb86c634ef84f6ce0a0bb5624)
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  */
16f094d442SMatthew Barth #include "config.h"
17f094d442SMatthew Barth 
18c1f4798dSBrad Bishop #include "functor.hpp"
19a680d1efSPatrick Venture 
20f094d442SMatthew Barth #include "manager.hpp"
21f094d442SMatthew Barth 
2266431fefSGeorge Liu #include <phosphor-logging/lg2.hpp>
23c1f4798dSBrad Bishop #include <sdbusplus/bus.hpp>
24c1f4798dSBrad Bishop 
25c1f4798dSBrad Bishop namespace phosphor
26c1f4798dSBrad Bishop {
27c1f4798dSBrad Bishop namespace inventory
28c1f4798dSBrad Bishop {
29c1f4798dSBrad Bishop namespace manager
30c1f4798dSBrad Bishop {
31c1f4798dSBrad Bishop namespace functor
32c1f4798dSBrad Bishop {
operator ()(sdbusplus::bus_t & bus,sdbusplus::message_t &,Manager & mgr) const33*d8fba8beSPatrick Williams bool PropertyConditionBase::operator()(
34*d8fba8beSPatrick Williams     sdbusplus::bus_t& bus, sdbusplus::message_t&, Manager& mgr) const
35d0f48adcSBrad Bishop {
36d0f48adcSBrad Bishop     std::string path(_path);
37d0f48adcSBrad Bishop     return (*this)(path, bus, mgr);
38d0f48adcSBrad Bishop }
39d0f48adcSBrad Bishop 
operator ()(const std::string & path,sdbusplus::bus_t & bus,Manager & mgr) const40*d8fba8beSPatrick Williams bool PropertyConditionBase::operator()(
41*d8fba8beSPatrick Williams     const std::string& path, sdbusplus::bus_t& bus, Manager& mgr) const
42c1f4798dSBrad Bishop {
43c1f4798dSBrad Bishop     std::string host;
44c1f4798dSBrad Bishop 
45c1f4798dSBrad Bishop     if (_service)
46c1f4798dSBrad Bishop     {
47c1f4798dSBrad Bishop         host.assign(_service);
48c1f4798dSBrad Bishop     }
49c1f4798dSBrad Bishop     else
50c1f4798dSBrad Bishop     {
51c1f4798dSBrad Bishop         auto mapperCall = bus.new_method_call(
52c1f4798dSBrad Bishop             "xyz.openbmc_project.ObjectMapper",
5382aa2232SLeonel Gonzalez             "/xyz/openbmc_project/object_mapper",
54615b2a8fSBrad Bishop             "xyz.openbmc_project.ObjectMapper", "GetObject");
55d0f48adcSBrad Bishop         mapperCall.append(path);
56c1f4798dSBrad Bishop         mapperCall.append(std::vector<std::string>({_iface}));
57c1f4798dSBrad Bishop 
5866431fefSGeorge Liu         std::map<std::string, std::vector<std::string>> mapperResponse;
5966431fefSGeorge Liu         try
60c1f4798dSBrad Bishop         {
6166431fefSGeorge Liu             auto mapperResponseMsg = bus.call(mapperCall);
6266431fefSGeorge Liu             mapperResponseMsg.read(mapperResponse);
6366431fefSGeorge Liu         }
6466431fefSGeorge Liu         catch (const std::exception& e)
6566431fefSGeorge Liu         {
6666431fefSGeorge Liu             lg2::error("Failed to execute GetObject method: {ERROR}", "ERROR",
6766431fefSGeorge Liu                        e);
68c1f4798dSBrad Bishop             return false;
69c1f4798dSBrad Bishop         }
70c1f4798dSBrad Bishop 
718ed114b6SPatrick Williams         if (mapperResponse.empty())
72c1f4798dSBrad Bishop         {
73c1f4798dSBrad Bishop             return false;
74c1f4798dSBrad Bishop         }
75c1f4798dSBrad Bishop 
76c1f4798dSBrad Bishop         host = mapperResponse.begin()->first;
77f094d442SMatthew Barth     }
78c1f4798dSBrad Bishop 
79f094d442SMatthew Barth     // When the host service name is inventory manager, eval using
80f094d442SMatthew Barth     // a given `getProperty` function to retrieve a property value from
81f094d442SMatthew Barth     // an interface hosted by inventory manager.
82f094d442SMatthew Barth     if (host == BUSNAME)
83c1f4798dSBrad Bishop     {
84f094d442SMatthew Barth         try
85f094d442SMatthew Barth         {
86f094d442SMatthew Barth             return eval(mgr);
87f094d442SMatthew Barth         }
88f094d442SMatthew Barth         catch (const std::exception& e)
89f094d442SMatthew Barth         {
90f094d442SMatthew Barth             // Unable to find property on inventory manager,
91f094d442SMatthew Barth             // default condition to false.
92c1f4798dSBrad Bishop             return false;
93c1f4798dSBrad Bishop         }
94c1f4798dSBrad Bishop     }
95f094d442SMatthew Barth 
96c1f4798dSBrad Bishop     auto hostCall = bus.new_method_call(
97615b2a8fSBrad Bishop         host.c_str(), path.c_str(), "org.freedesktop.DBus.Properties", "Get");
98c1f4798dSBrad Bishop     hostCall.append(_iface);
99c1f4798dSBrad Bishop     hostCall.append(_property);
100c1f4798dSBrad Bishop 
10166431fefSGeorge Liu     try
102c1f4798dSBrad Bishop     {
10366431fefSGeorge Liu         auto hostResponseMsg = bus.call(hostCall);
10466431fefSGeorge Liu         return eval(hostResponseMsg);
10566431fefSGeorge Liu     }
10666431fefSGeorge Liu     catch (const std::exception& e)
10766431fefSGeorge Liu     {
10866431fefSGeorge Liu         lg2::error("Failed to execute Get method: {ERROR}", "ERROR", e);
109c1f4798dSBrad Bishop         return false;
110c1f4798dSBrad Bishop     }
111c1f4798dSBrad Bishop }
112c1f4798dSBrad Bishop 
113c1f4798dSBrad Bishop } // namespace functor
114c1f4798dSBrad Bishop } // namespace manager
115c1f4798dSBrad Bishop } // namespace inventory
116c1f4798dSBrad Bishop } // namespace phosphor
117