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 "dbus_mode.hpp"
18
19 #include <ipmid/api-types.hpp>
20 #include <sdbusplus/bus.hpp>
21 #include <sdbusplus/exception.hpp>
22 #include <sdbusplus/message.hpp>
23
24 #include <cstdint>
25 #include <map>
26 #include <string>
27 #include <variant>
28
29 namespace pid_control::ipmi
30 {
31
32 static constexpr auto objectPath = "/xyz/openbmc_project/settings/fanctrl/zone";
33 static constexpr auto busName = "xyz.openbmc_project.State.FanCtrl";
34 static constexpr auto intf = "xyz.openbmc_project.Control.Mode";
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. */
getControlPath(int8_t zone)42 static std::string getControlPath(int8_t zone)
43 {
44 return std::string(objectPath) + std::to_string(zone);
45 }
46
getFanCtrlProperty(uint8_t zoneId,bool * value,const std::string & property)47 uint8_t DbusZoneControl::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_t& ex)
69 {
70 return ::ipmi::ccInvalidCommand;
71 }
72
73 return ::ipmi::ccSuccess;
74 }
75
setFanCtrlProperty(uint8_t zoneId,bool value,const std::string & property)76 uint8_t DbusZoneControl::setFanCtrlProperty(uint8_t zoneId, bool value,
77 const std::string& property)
78 {
79 using Value = std::variant<bool>;
80 Value v{value};
81
82 std::string path = getControlPath(zoneId);
83
84 auto PropertyWriteBus = sdbusplus::bus::new_system();
85 auto pimMsg = PropertyWriteBus.new_method_call(busName, path.c_str(),
86 propertiesintf, "Set");
87 pimMsg.append(intf);
88 pimMsg.append(property);
89 pimMsg.append(v);
90
91 try
92 {
93 PropertyWriteBus.call_noreply(pimMsg);
94 }
95 catch (const sdbusplus::exception_t& ex)
96 {
97 return ::ipmi::ccInvalidCommand;
98 }
99
100 /* TODO(venture): Should sanity check the result. */
101 return ::ipmi::ccSuccess;
102 }
103
104 } // namespace pid_control::ipmi
105