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