1 /**
2  * Copyright 2017 Google Inc.
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  */
16 
17 #include <ipmid/api.h>
18 
19 #include <sdbusplus/bus.hpp>
20 #include <sdbusplus/message.hpp>
21 
22 #include <cstdint>
23 #include <string>
24 
25 namespace pid_control
26 {
27 namespace ipmi
28 {
29 
30 static constexpr auto objectPath = "/xyz/openbmc_project/settings/fanctrl/zone";
31 static constexpr auto busName = "xyz.openbmc_project.State.FanCtrl";
32 static constexpr auto intf = "xyz.openbmc_project.Control.Mode";
33 static constexpr auto manualProperty = "Manual";
34 static constexpr auto failsafeProperty = "FailSafe";
35 static constexpr auto propertiesintf = "org.freedesktop.DBus.Properties";
36 
37 using Property = std::string;
38 using Value = std::variant<bool>;
39 using PropertyMap = std::map<Property, Value>;
40 
41 /* The following was copied directly from my manual thread handler. */
42 static std::string getControlPath(int8_t zone)
43 {
44     return std::string(objectPath) + std::to_string(zone);
45 }
46 
47 uint8_t getFanCtrlProperty(uint8_t zoneId, bool* value,
48                            const std::string& property)
49 {
50     std::string path = getControlPath(zoneId);
51 
52     auto propertyReadBus = sdbusplus::bus::new_system();
53     auto pimMsg = propertyReadBus.new_method_call(busName, path.c_str(),
54                                                   propertiesintf, "GetAll");
55     pimMsg.append(intf);
56 
57     try
58     {
59         PropertyMap propMap;
60 
61         /* a method could error but the call not error. */
62         auto valueResponseMsg = propertyReadBus.call(pimMsg);
63 
64         valueResponseMsg.read(propMap);
65 
66         *value = std::get<bool>(propMap[property]);
67     }
68     catch (const sdbusplus::exception::SdBusError& ex)
69     {
70         return IPMI_CC_INVALID;
71     }
72 
73     return IPMI_CC_OK;
74 }
75 
76 } // namespace ipmi
77 } // namespace pid_control
78