blob: ddb70f91b85450a28e3d7f28ddb470f1618fb7d2 [file] [log] [blame]
Patrick Venture391b8b02018-03-08 08:31:13 -08001/**
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
Patrick Venture36ab6f62020-08-03 10:50:26 -070017#include "manualcmds.hpp"
18
William A. Kennington III331143c2019-02-07 15:52:44 -080019#include <ipmid/api.h>
Patrick Venture391b8b02018-03-08 08:31:13 -080020
William A. Kennington III331143c2019-02-07 15:52:44 -080021#include <ipmid/iana.hpp>
22#include <ipmid/oemopenbmc.hpp>
23#include <ipmid/oemrouter.hpp>
Patrick Venture391b8b02018-03-08 08:31:13 -080024#include <map>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070025#include <sdbusplus/bus.hpp>
26#include <sdbusplus/message.hpp>
Patrick Venture391b8b02018-03-08 08:31:13 -080027#include <string>
28#include <tuple>
James Feist1f802f52019-02-08 13:51:43 -080029#include <variant>
Patrick Venture391b8b02018-03-08 08:31:13 -080030
Patrick Venture36ab6f62020-08-03 10:50:26 -070031namespace pid_control
Patrick Venture391b8b02018-03-08 08:31:13 -080032{
Patrick Venture36ab6f62020-08-03 10:50:26 -070033namespace ipmi
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070034{
Patrick Venture391b8b02018-03-08 08:31:13 -080035
36static constexpr auto objectPath = "/xyz/openbmc_project/settings/fanctrl/zone";
37static constexpr auto busName = "xyz.openbmc_project.State.FanCtrl";
Patrick Venturedc3b7902018-03-24 10:41:19 -070038static constexpr auto intf = "xyz.openbmc_project.Control.Mode";
Patrick Venture391b8b02018-03-08 08:31:13 -080039static constexpr auto manualProperty = "Manual";
40static constexpr auto failsafeProperty = "FailSafe";
41static constexpr auto propertiesintf = "org.freedesktop.DBus.Properties";
42
43using Property = std::string;
William A. Kennington III323f1d92020-06-03 11:18:24 -070044using Value = std::variant<bool>;
Patrick Venture391b8b02018-03-08 08:31:13 -080045using PropertyMap = std::map<Property, Value>;
46
47/* The following was copied directly from my manual thread handler. */
Patrick Venture7af157b2018-10-30 11:24:40 -070048static std::string getControlPath(int8_t zone)
Patrick Venture391b8b02018-03-08 08:31:13 -080049{
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 \
Patrick Venturedc3b7902018-03-24 10:41:19 -070059 * xyz.openbmc_project.Control.Mode
Patrick Venture391b8b02018-03-08 08:31:13 -080060 * a{sv} 2 "Manual" b false "FailSafe" b false
61 */
62
Patrick Venture7af157b2018-10-30 11:24:40 -070063static ipmi_ret_t getFanCtrlProperty(uint8_t zoneId, bool* value,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070064 const std::string& property)
Patrick Venture391b8b02018-03-08 08:31:13 -080065{
Patrick Venture7af157b2018-10-30 11:24:40 -070066 std::string path = getControlPath(zoneId);
Patrick Venture391b8b02018-03-08 08:31:13 -080067
James Feist9fa90c12019-01-11 15:35:22 -080068 auto propertyReadBus = sdbusplus::bus::new_system();
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070069 auto pimMsg = propertyReadBus.new_method_call(busName, path.c_str(),
70 propertiesintf, "GetAll");
Patrick Venture391b8b02018-03-08 08:31:13 -080071 pimMsg.append(intf);
72
Patrick Ventureacecf6b2018-09-06 17:56:41 -070073 try
74 {
Patrick Ventureacecf6b2018-09-06 17:56:41 -070075 PropertyMap propMap;
Patrick Venture8f179142018-09-10 09:24:12 -070076
77 /* a method could error but the call not error. */
78 auto valueResponseMsg = propertyReadBus.call(pimMsg);
Patrick Venture8f179142018-09-10 09:24:12 -070079
Patrick Ventureacecf6b2018-09-06 17:56:41 -070080 valueResponseMsg.read(propMap);
81
James Feist1f802f52019-02-08 13:51:43 -080082 *value = std::get<bool>(propMap[property]);
Patrick Ventureacecf6b2018-09-06 17:56:41 -070083 }
84 catch (const sdbusplus::exception::SdBusError& ex)
Patrick Venture391b8b02018-03-08 08:31:13 -080085 {
86 return IPMI_CC_INVALID;
87 }
88
Patrick Venture391b8b02018-03-08 08:31:13 -080089 return IPMI_CC_OK;
90}
91
Patrick Venture7af157b2018-10-30 11:24:40 -070092static ipmi_ret_t getFailsafeModeState(const uint8_t* reqBuf, uint8_t* replyBuf,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070093 size_t* dataLen)
Patrick Venture391b8b02018-03-08 08:31:13 -080094{
95 ipmi_ret_t rc = IPMI_CC_OK;
96 bool current;
97
98 if (*dataLen < sizeof(struct FanCtrlRequest))
99 {
100 return IPMI_CC_INVALID;
101 }
102
103 const auto request =
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700104 reinterpret_cast<const struct FanCtrlRequest*>(&reqBuf[0]);
Patrick Venture391b8b02018-03-08 08:31:13 -0800105
Patrick Venture7af157b2018-10-30 11:24:40 -0700106 rc = getFanCtrlProperty(request->zone, &current, failsafeProperty);
Patrick Venture391b8b02018-03-08 08:31:13 -0800107 if (rc)
108 {
109 return rc;
110 }
111
112 *replyBuf = (uint8_t)current;
113 *dataLen = sizeof(uint8_t);
114 return rc;
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 */
Patrick Venture7af157b2018-10-30 11:24:40 -0700123static ipmi_ret_t getManualModeState(const uint8_t* reqBuf, uint8_t* replyBuf,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700124 size_t* dataLen)
Patrick Venture391b8b02018-03-08 08:31:13 -0800125{
126 ipmi_ret_t rc = IPMI_CC_OK;
127 bool current;
128
129 if (*dataLen < sizeof(struct FanCtrlRequest))
130 {
131 return IPMI_CC_INVALID;
132 }
133
134 const auto request =
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700135 reinterpret_cast<const struct FanCtrlRequest*>(&reqBuf[0]);
Patrick Venture391b8b02018-03-08 08:31:13 -0800136
Patrick Venture7af157b2018-10-30 11:24:40 -0700137 rc = getFanCtrlProperty(request->zone, &current, manualProperty);
Patrick Venture391b8b02018-03-08 08:31:13 -0800138 if (rc)
139 {
140 return rc;
141 }
142
143 *replyBuf = (uint8_t)current;
144 *dataLen = sizeof(uint8_t);
145 return rc;
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 */
Patrick Venture7af157b2018-10-30 11:24:40 -0700155static ipmi_ret_t setManualModeState(const uint8_t* reqBuf, uint8_t* replyBuf,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700156 size_t* dataLen)
Patrick Venture391b8b02018-03-08 08:31:13 -0800157{
158 ipmi_ret_t rc = IPMI_CC_OK;
159 if (*dataLen < sizeof(struct FanCtrlRequestSet))
160 {
161 return IPMI_CC_INVALID;
162 }
163
William A. Kennington III323f1d92020-06-03 11:18:24 -0700164 using Value = std::variant<bool>;
Patrick Venture391b8b02018-03-08 08:31:13 -0800165
166 const auto request =
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700167 reinterpret_cast<const struct FanCtrlRequestSet*>(&reqBuf[0]);
Patrick Venture391b8b02018-03-08 08:31:13 -0800168
169 /* 0 is false, 1 is true */
170 bool setValue = static_cast<bool>(request->value);
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700171 Value v{setValue};
Patrick Venture391b8b02018-03-08 08:31:13 -0800172
James Feist9fa90c12019-01-11 15:35:22 -0800173 auto PropertyWriteBus = sdbusplus::bus::new_system();
Patrick Venture391b8b02018-03-08 08:31:13 -0800174
Patrick Venture7af157b2018-10-30 11:24:40 -0700175 std::string path = getControlPath(request->zone);
Patrick Venture391b8b02018-03-08 08:31:13 -0800176
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700177 auto pimMsg = PropertyWriteBus.new_method_call(busName, path.c_str(),
178 propertiesintf, "Set");
Patrick Venture391b8b02018-03-08 08:31:13 -0800179 pimMsg.append(intf);
180 pimMsg.append(manualProperty);
181 pimMsg.append(v);
Patrick Ventureacecf6b2018-09-06 17:56:41 -0700182
183 try
184 {
185 PropertyWriteBus.call_noreply(pimMsg);
186 }
187 catch (const sdbusplus::exception::SdBusError& ex)
Patrick Venture391b8b02018-03-08 08:31:13 -0800188 {
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 */
Patrick Venture7af157b2018-10-30 11:24:40 -0700197static ipmi_ret_t manualModeControl(ipmi_cmd_t cmd, const uint8_t* reqBuf,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700198 uint8_t* replyCmdBuf, size_t* dataLen)
Patrick Venture391b8b02018-03-08 08:31:13 -0800199{
200 ipmi_ret_t rc = IPMI_CC_OK;
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 =
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700208 reinterpret_cast<const struct FanCtrlRequest*>(&reqBuf[0]);
Patrick Venture391b8b02018-03-08 08:31:13 -0800209
210 switch (request->command)
211 {
212 case GET_CONTROL_STATE:
Patrick Venture7af157b2018-10-30 11:24:40 -0700213 return getManualModeState(reqBuf, replyCmdBuf, dataLen);
Patrick Venture391b8b02018-03-08 08:31:13 -0800214 case SET_CONTROL_STATE:
Patrick Venture7af157b2018-10-30 11:24:40 -0700215 return setManualModeState(reqBuf, replyCmdBuf, dataLen);
Patrick Venture391b8b02018-03-08 08:31:13 -0800216 case GET_FAILSAFE_STATE:
Patrick Venture7af157b2018-10-30 11:24:40 -0700217 return getFailsafeModeState(reqBuf, replyCmdBuf, dataLen);
Patrick Venture391b8b02018-03-08 08:31:13 -0800218 default:
219 rc = IPMI_CC_INVALID;
220 }
221
222 return rc;
223}
224
Patrick Venture36ab6f62020-08-03 10:50:26 -0700225} // namespace ipmi
226} // namespace pid_control
227
Patrick Venture391b8b02018-03-08 08:31:13 -0800228void setupGlobalOemFanControl() __attribute__((constructor));
229
230void setupGlobalOemFanControl()
231{
Patrick Ventureba003432018-07-27 06:59:05 -0700232 oem::Router* router = oem::mutableRouter();
Patrick Venture391b8b02018-03-08 08:31:13 -0800233
234 fprintf(stderr,
235 "Registering OEM:[%#08X], Cmd:[%#04X] for Manual Zone Control\n",
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700236 oem::obmcOemNumber, oem::Cmd::fanManualCmd);
Patrick Venture391b8b02018-03-08 08:31:13 -0800237
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700238 router->registerHandler(oem::obmcOemNumber, oem::Cmd::fanManualCmd,
Patrick Venture36ab6f62020-08-03 10:50:26 -0700239 pid_control::ipmi::manualModeControl);
Patrick Venture391b8b02018-03-08 08:31:13 -0800240}