Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 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 | |
Patrick Venture | 36ab6f6 | 2020-08-03 10:50:26 -0700 | [diff] [blame] | 17 | #include "manualcmds.hpp" |
| 18 | |
Patrick Venture | d82d0b7 | 2020-08-16 09:17:37 -0700 | [diff] [blame^] | 19 | #include "dbus_mode.hpp" |
Patrick Venture | 9bf5cef | 2020-08-16 08:59:54 -0700 | [diff] [blame] | 20 | #include "manual_messages.hpp" |
| 21 | |
William A. Kennington III | 331143c | 2019-02-07 15:52:44 -0800 | [diff] [blame] | 22 | #include <ipmid/api.h> |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 23 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 24 | #include <sdbusplus/bus.hpp> |
| 25 | #include <sdbusplus/message.hpp> |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 26 | |
| 27 | #include <map> |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 28 | #include <string> |
| 29 | #include <tuple> |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 30 | #include <variant> |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 31 | |
Patrick Venture | 36ab6f6 | 2020-08-03 10:50:26 -0700 | [diff] [blame] | 32 | namespace pid_control |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 33 | { |
Patrick Venture | 36ab6f6 | 2020-08-03 10:50:26 -0700 | [diff] [blame] | 34 | namespace ipmi |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 35 | { |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 36 | |
| 37 | static constexpr auto objectPath = "/xyz/openbmc_project/settings/fanctrl/zone"; |
| 38 | static constexpr auto busName = "xyz.openbmc_project.State.FanCtrl"; |
Patrick Venture | dc3b790 | 2018-03-24 10:41:19 -0700 | [diff] [blame] | 39 | static constexpr auto intf = "xyz.openbmc_project.Control.Mode"; |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 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; |
William A. Kennington III | 323f1d9 | 2020-06-03 11:18:24 -0700 | [diff] [blame] | 45 | using Value = std::variant<bool>; |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 46 | using PropertyMap = std::map<Property, Value>; |
| 47 | |
| 48 | /* The following was copied directly from my manual thread handler. */ |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 49 | static std::string getControlPath(int8_t zone) |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 50 | { |
| 51 | return std::string(objectPath) + std::to_string(zone); |
| 52 | } |
| 53 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 54 | static ipmi_ret_t getFailsafeModeState(const uint8_t* reqBuf, uint8_t* replyBuf, |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 55 | size_t* dataLen) |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 56 | { |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 57 | bool current; |
| 58 | |
| 59 | if (*dataLen < sizeof(struct FanCtrlRequest)) |
| 60 | { |
| 61 | return IPMI_CC_INVALID; |
| 62 | } |
| 63 | |
| 64 | const auto request = |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 65 | reinterpret_cast<const struct FanCtrlRequest*>(&reqBuf[0]); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 66 | |
Patrick Venture | 37b247a | 2020-08-03 11:15:21 -0700 | [diff] [blame] | 67 | ipmi_ret_t rc = |
| 68 | getFanCtrlProperty(request->zone, ¤t, failsafeProperty); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 69 | if (rc) |
| 70 | { |
| 71 | return rc; |
| 72 | } |
| 73 | |
| 74 | *replyBuf = (uint8_t)current; |
| 75 | *dataLen = sizeof(uint8_t); |
Patrick Venture | 37b247a | 2020-08-03 11:15:21 -0700 | [diff] [blame] | 76 | return IPMI_CC_OK; |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /* |
| 80 | * <method name="GetAll"> |
| 81 | * <arg name="interface" direction="in" type="s"/> |
| 82 | * <arg name="properties" direction="out" type="a{sv}"/> |
| 83 | * </method> |
| 84 | */ |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 85 | static ipmi_ret_t getManualModeState(const uint8_t* reqBuf, uint8_t* replyBuf, |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 86 | size_t* dataLen) |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 87 | { |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 88 | bool current; |
| 89 | |
| 90 | if (*dataLen < sizeof(struct FanCtrlRequest)) |
| 91 | { |
| 92 | return IPMI_CC_INVALID; |
| 93 | } |
| 94 | |
| 95 | const auto request = |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 96 | reinterpret_cast<const struct FanCtrlRequest*>(&reqBuf[0]); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 97 | |
Patrick Venture | 37b247a | 2020-08-03 11:15:21 -0700 | [diff] [blame] | 98 | ipmi_ret_t rc = getFanCtrlProperty(request->zone, ¤t, manualProperty); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 99 | if (rc) |
| 100 | { |
| 101 | return rc; |
| 102 | } |
| 103 | |
| 104 | *replyBuf = (uint8_t)current; |
| 105 | *dataLen = sizeof(uint8_t); |
Patrick Venture | 37b247a | 2020-08-03 11:15:21 -0700 | [diff] [blame] | 106 | return IPMI_CC_OK; |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /* |
| 110 | * <method name="Set"> |
| 111 | * <arg name="interface" direction="in" type="s"/> |
| 112 | * <arg name="property" direction="in" type="s"/> |
| 113 | * <arg name="value" direction="in" type="v"/> |
| 114 | * </method> |
| 115 | */ |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 116 | static ipmi_ret_t setManualModeState(const uint8_t* reqBuf, uint8_t* replyBuf, |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 117 | size_t* dataLen) |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 118 | { |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 119 | if (*dataLen < sizeof(struct FanCtrlRequestSet)) |
| 120 | { |
| 121 | return IPMI_CC_INVALID; |
| 122 | } |
| 123 | |
William A. Kennington III | 323f1d9 | 2020-06-03 11:18:24 -0700 | [diff] [blame] | 124 | using Value = std::variant<bool>; |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 125 | |
| 126 | const auto request = |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 127 | reinterpret_cast<const struct FanCtrlRequestSet*>(&reqBuf[0]); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 128 | |
| 129 | /* 0 is false, 1 is true */ |
| 130 | bool setValue = static_cast<bool>(request->value); |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 131 | Value v{setValue}; |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 132 | |
James Feist | 9fa90c1 | 2019-01-11 15:35:22 -0800 | [diff] [blame] | 133 | auto PropertyWriteBus = sdbusplus::bus::new_system(); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 134 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 135 | std::string path = getControlPath(request->zone); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 136 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 137 | auto pimMsg = PropertyWriteBus.new_method_call(busName, path.c_str(), |
| 138 | propertiesintf, "Set"); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 139 | pimMsg.append(intf); |
| 140 | pimMsg.append(manualProperty); |
| 141 | pimMsg.append(v); |
Patrick Venture | acecf6b | 2018-09-06 17:56:41 -0700 | [diff] [blame] | 142 | |
Patrick Venture | 37b247a | 2020-08-03 11:15:21 -0700 | [diff] [blame] | 143 | ipmi_ret_t rc = IPMI_CC_OK; |
| 144 | |
Patrick Venture | acecf6b | 2018-09-06 17:56:41 -0700 | [diff] [blame] | 145 | try |
| 146 | { |
| 147 | PropertyWriteBus.call_noreply(pimMsg); |
| 148 | } |
| 149 | catch (const sdbusplus::exception::SdBusError& ex) |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 150 | { |
| 151 | rc = IPMI_CC_INVALID; |
| 152 | } |
| 153 | /* TODO(venture): Should sanity check the result. */ |
| 154 | |
| 155 | return rc; |
| 156 | } |
| 157 | |
| 158 | /* Three command packages: get, set true, set false */ |
Patrick Venture | 9bf5cef | 2020-08-16 08:59:54 -0700 | [diff] [blame] | 159 | ipmi_ret_t manualModeControl(ipmi_cmd_t cmd, const uint8_t* reqBuf, |
| 160 | uint8_t* replyCmdBuf, size_t* dataLen) |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 161 | { |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 162 | // FanCtrlRequest is the smaller of the requests, so it's at a minimum. |
| 163 | if (*dataLen < sizeof(struct FanCtrlRequest)) |
| 164 | { |
| 165 | return IPMI_CC_INVALID; |
| 166 | } |
| 167 | |
| 168 | const auto request = |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 169 | reinterpret_cast<const struct FanCtrlRequest*>(&reqBuf[0]); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 170 | |
Patrick Venture | 37b247a | 2020-08-03 11:15:21 -0700 | [diff] [blame] | 171 | ipmi_ret_t rc = IPMI_CC_OK; |
| 172 | |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 173 | switch (request->command) |
| 174 | { |
Patrick Venture | 1277543 | 2020-08-04 09:57:36 -0700 | [diff] [blame] | 175 | case getControlState: |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 176 | return getManualModeState(reqBuf, replyCmdBuf, dataLen); |
Patrick Venture | 1277543 | 2020-08-04 09:57:36 -0700 | [diff] [blame] | 177 | case setControlState: |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 178 | return setManualModeState(reqBuf, replyCmdBuf, dataLen); |
Patrick Venture | 1277543 | 2020-08-04 09:57:36 -0700 | [diff] [blame] | 179 | case getFailsafeState: |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 180 | return getFailsafeModeState(reqBuf, replyCmdBuf, dataLen); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 181 | default: |
| 182 | rc = IPMI_CC_INVALID; |
| 183 | } |
| 184 | |
| 185 | return rc; |
| 186 | } |
| 187 | |
Patrick Venture | 36ab6f6 | 2020-08-03 10:50:26 -0700 | [diff] [blame] | 188 | } // namespace ipmi |
| 189 | } // namespace pid_control |