blob: e11d49aaecf122a82c47b3eae10915325bdcbae9 [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
Patrick Venture9bf5cef2020-08-16 08:59:54 -070019#include "manual_messages.hpp"
20
William A. Kennington III331143c2019-02-07 15:52:44 -080021#include <ipmid/api.h>
Patrick Venture391b8b02018-03-08 08:31:13 -080022
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070023#include <sdbusplus/bus.hpp>
24#include <sdbusplus/message.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070025
26#include <map>
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{
Patrick Venture391b8b02018-03-08 08:31:13 -080095 bool current;
96
97 if (*dataLen < sizeof(struct FanCtrlRequest))
98 {
99 return IPMI_CC_INVALID;
100 }
101
102 const auto request =
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700103 reinterpret_cast<const struct FanCtrlRequest*>(&reqBuf[0]);
Patrick Venture391b8b02018-03-08 08:31:13 -0800104
Patrick Venture37b247a2020-08-03 11:15:21 -0700105 ipmi_ret_t rc =
106 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);
Patrick Venture37b247a2020-08-03 11:15:21 -0700114 return IPMI_CC_OK;
Patrick Venture391b8b02018-03-08 08:31:13 -0800115}
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{
Patrick Venture391b8b02018-03-08 08:31:13 -0800126 bool current;
127
128 if (*dataLen < sizeof(struct FanCtrlRequest))
129 {
130 return IPMI_CC_INVALID;
131 }
132
133 const auto request =
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700134 reinterpret_cast<const struct FanCtrlRequest*>(&reqBuf[0]);
Patrick Venture391b8b02018-03-08 08:31:13 -0800135
Patrick Venture37b247a2020-08-03 11:15:21 -0700136 ipmi_ret_t rc = getFanCtrlProperty(request->zone, &current, manualProperty);
Patrick Venture391b8b02018-03-08 08:31:13 -0800137 if (rc)
138 {
139 return rc;
140 }
141
142 *replyBuf = (uint8_t)current;
143 *dataLen = sizeof(uint8_t);
Patrick Venture37b247a2020-08-03 11:15:21 -0700144 return IPMI_CC_OK;
Patrick Venture391b8b02018-03-08 08:31:13 -0800145}
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 */
Patrick Venture7af157b2018-10-30 11:24:40 -0700154static ipmi_ret_t setManualModeState(const uint8_t* reqBuf, uint8_t* replyBuf,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700155 size_t* dataLen)
Patrick Venture391b8b02018-03-08 08:31:13 -0800156{
Patrick Venture391b8b02018-03-08 08:31:13 -0800157 if (*dataLen < sizeof(struct FanCtrlRequestSet))
158 {
159 return IPMI_CC_INVALID;
160 }
161
William A. Kennington III323f1d92020-06-03 11:18:24 -0700162 using Value = std::variant<bool>;
Patrick Venture391b8b02018-03-08 08:31:13 -0800163
164 const auto request =
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700165 reinterpret_cast<const struct FanCtrlRequestSet*>(&reqBuf[0]);
Patrick Venture391b8b02018-03-08 08:31:13 -0800166
167 /* 0 is false, 1 is true */
168 bool setValue = static_cast<bool>(request->value);
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700169 Value v{setValue};
Patrick Venture391b8b02018-03-08 08:31:13 -0800170
James Feist9fa90c12019-01-11 15:35:22 -0800171 auto PropertyWriteBus = sdbusplus::bus::new_system();
Patrick Venture391b8b02018-03-08 08:31:13 -0800172
Patrick Venture7af157b2018-10-30 11:24:40 -0700173 std::string path = getControlPath(request->zone);
Patrick Venture391b8b02018-03-08 08:31:13 -0800174
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700175 auto pimMsg = PropertyWriteBus.new_method_call(busName, path.c_str(),
176 propertiesintf, "Set");
Patrick Venture391b8b02018-03-08 08:31:13 -0800177 pimMsg.append(intf);
178 pimMsg.append(manualProperty);
179 pimMsg.append(v);
Patrick Ventureacecf6b2018-09-06 17:56:41 -0700180
Patrick Venture37b247a2020-08-03 11:15:21 -0700181 ipmi_ret_t rc = IPMI_CC_OK;
182
Patrick Ventureacecf6b2018-09-06 17:56:41 -0700183 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 Venture9bf5cef2020-08-16 08:59:54 -0700197ipmi_ret_t manualModeControl(ipmi_cmd_t cmd, const uint8_t* reqBuf,
198 uint8_t* replyCmdBuf, size_t* dataLen)
Patrick Venture391b8b02018-03-08 08:31:13 -0800199{
Patrick Venture391b8b02018-03-08 08:31:13 -0800200 // 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 =
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700207 reinterpret_cast<const struct FanCtrlRequest*>(&reqBuf[0]);
Patrick Venture391b8b02018-03-08 08:31:13 -0800208
Patrick Venture37b247a2020-08-03 11:15:21 -0700209 ipmi_ret_t rc = IPMI_CC_OK;
210
Patrick Venture391b8b02018-03-08 08:31:13 -0800211 switch (request->command)
212 {
Patrick Venture12775432020-08-04 09:57:36 -0700213 case getControlState:
Patrick Venture7af157b2018-10-30 11:24:40 -0700214 return getManualModeState(reqBuf, replyCmdBuf, dataLen);
Patrick Venture12775432020-08-04 09:57:36 -0700215 case setControlState:
Patrick Venture7af157b2018-10-30 11:24:40 -0700216 return setManualModeState(reqBuf, replyCmdBuf, dataLen);
Patrick Venture12775432020-08-04 09:57:36 -0700217 case getFailsafeState:
Patrick Venture7af157b2018-10-30 11:24:40 -0700218 return getFailsafeModeState(reqBuf, replyCmdBuf, dataLen);
Patrick Venture391b8b02018-03-08 08:31:13 -0800219 default:
220 rc = IPMI_CC_INVALID;
221 }
222
223 return rc;
224}
225
Patrick Venture36ab6f62020-08-03 10:50:26 -0700226} // namespace ipmi
227} // namespace pid_control