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 "manualcmds.hpp"
18 
19 #include "manual_messages.hpp"
20 
21 #include <ipmid/api.h>
22 
23 #include <sdbusplus/bus.hpp>
24 #include <sdbusplus/message.hpp>
25 
26 #include <map>
27 #include <string>
28 #include <tuple>
29 #include <variant>
30 
31 namespace pid_control
32 {
33 namespace ipmi
34 {
35 
36 static constexpr auto objectPath = "/xyz/openbmc_project/settings/fanctrl/zone";
37 static constexpr auto busName = "xyz.openbmc_project.State.FanCtrl";
38 static constexpr auto intf = "xyz.openbmc_project.Control.Mode";
39 static constexpr auto manualProperty = "Manual";
40 static constexpr auto failsafeProperty = "FailSafe";
41 static constexpr auto propertiesintf = "org.freedesktop.DBus.Properties";
42 
43 using Property = std::string;
44 using Value = std::variant<bool>;
45 using PropertyMap = std::map<Property, Value>;
46 
47 /* The following was copied directly from my manual thread handler. */
48 static std::string getControlPath(int8_t zone)
49 {
50     return std::string(objectPath) + std::to_string(zone);
51 }
52 
53 /*
54  * busctl call xyz.openbmc_project.State.FanCtrl \
55  *     /xyz/openbmc_project/settings/fanctrl/zone1 \
56  *     org.freedesktop.DBus.Properties \
57  *     GetAll \
58  *     s \
59  *     xyz.openbmc_project.Control.Mode
60  * a{sv} 2 "Manual" b false "FailSafe" b false
61  */
62 
63 static ipmi_ret_t getFanCtrlProperty(uint8_t zoneId, bool* value,
64                                      const std::string& property)
65 {
66     std::string path = getControlPath(zoneId);
67 
68     auto propertyReadBus = sdbusplus::bus::new_system();
69     auto pimMsg = propertyReadBus.new_method_call(busName, path.c_str(),
70                                                   propertiesintf, "GetAll");
71     pimMsg.append(intf);
72 
73     try
74     {
75         PropertyMap propMap;
76 
77         /* a method could error but the call not error. */
78         auto valueResponseMsg = propertyReadBus.call(pimMsg);
79 
80         valueResponseMsg.read(propMap);
81 
82         *value = std::get<bool>(propMap[property]);
83     }
84     catch (const sdbusplus::exception::SdBusError& ex)
85     {
86         return IPMI_CC_INVALID;
87     }
88 
89     return IPMI_CC_OK;
90 }
91 
92 static ipmi_ret_t getFailsafeModeState(const uint8_t* reqBuf, uint8_t* replyBuf,
93                                        size_t* dataLen)
94 {
95     bool current;
96 
97     if (*dataLen < sizeof(struct FanCtrlRequest))
98     {
99         return IPMI_CC_INVALID;
100     }
101 
102     const auto request =
103         reinterpret_cast<const struct FanCtrlRequest*>(&reqBuf[0]);
104 
105     ipmi_ret_t rc =
106         getFanCtrlProperty(request->zone, &current, failsafeProperty);
107     if (rc)
108     {
109         return rc;
110     }
111 
112     *replyBuf = (uint8_t)current;
113     *dataLen = sizeof(uint8_t);
114     return IPMI_CC_OK;
115 }
116 
117 /*
118  * <method name="GetAll">
119  *   <arg name="interface" direction="in" type="s"/>
120  *   <arg name="properties" direction="out" type="a{sv}"/>
121  * </method>
122  */
123 static ipmi_ret_t getManualModeState(const uint8_t* reqBuf, uint8_t* replyBuf,
124                                      size_t* dataLen)
125 {
126     bool current;
127 
128     if (*dataLen < sizeof(struct FanCtrlRequest))
129     {
130         return IPMI_CC_INVALID;
131     }
132 
133     const auto request =
134         reinterpret_cast<const struct FanCtrlRequest*>(&reqBuf[0]);
135 
136     ipmi_ret_t rc = getFanCtrlProperty(request->zone, &current, manualProperty);
137     if (rc)
138     {
139         return rc;
140     }
141 
142     *replyBuf = (uint8_t)current;
143     *dataLen = sizeof(uint8_t);
144     return IPMI_CC_OK;
145 }
146 
147 /*
148  * <method name="Set">
149  *   <arg name="interface" direction="in" type="s"/>
150  *   <arg name="property" direction="in" type="s"/>
151  *   <arg name="value" direction="in" type="v"/>
152  * </method>
153  */
154 static ipmi_ret_t setManualModeState(const uint8_t* reqBuf, uint8_t* replyBuf,
155                                      size_t* dataLen)
156 {
157     if (*dataLen < sizeof(struct FanCtrlRequestSet))
158     {
159         return IPMI_CC_INVALID;
160     }
161 
162     using Value = std::variant<bool>;
163 
164     const auto request =
165         reinterpret_cast<const struct FanCtrlRequestSet*>(&reqBuf[0]);
166 
167     /* 0 is false, 1 is true */
168     bool setValue = static_cast<bool>(request->value);
169     Value v{setValue};
170 
171     auto PropertyWriteBus = sdbusplus::bus::new_system();
172 
173     std::string path = getControlPath(request->zone);
174 
175     auto pimMsg = PropertyWriteBus.new_method_call(busName, path.c_str(),
176                                                    propertiesintf, "Set");
177     pimMsg.append(intf);
178     pimMsg.append(manualProperty);
179     pimMsg.append(v);
180 
181     ipmi_ret_t rc = IPMI_CC_OK;
182 
183     try
184     {
185         PropertyWriteBus.call_noreply(pimMsg);
186     }
187     catch (const sdbusplus::exception::SdBusError& ex)
188     {
189         rc = IPMI_CC_INVALID;
190     }
191     /* TODO(venture): Should sanity check the result. */
192 
193     return rc;
194 }
195 
196 /* Three command packages: get, set true, set false */
197 ipmi_ret_t manualModeControl(ipmi_cmd_t cmd, const uint8_t* reqBuf,
198                              uint8_t* replyCmdBuf, size_t* dataLen)
199 {
200     // FanCtrlRequest is the smaller of the requests, so it's at a minimum.
201     if (*dataLen < sizeof(struct FanCtrlRequest))
202     {
203         return IPMI_CC_INVALID;
204     }
205 
206     const auto request =
207         reinterpret_cast<const struct FanCtrlRequest*>(&reqBuf[0]);
208 
209     ipmi_ret_t rc = IPMI_CC_OK;
210 
211     switch (request->command)
212     {
213         case getControlState:
214             return getManualModeState(reqBuf, replyCmdBuf, dataLen);
215         case setControlState:
216             return setManualModeState(reqBuf, replyCmdBuf, dataLen);
217         case getFailsafeState:
218             return getFailsafeModeState(reqBuf, replyCmdBuf, dataLen);
219         default:
220             rc = IPMI_CC_INVALID;
221     }
222 
223     return rc;
224 }
225 
226 } // namespace ipmi
227 } // namespace pid_control
228