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 <map> 25 #include <sdbusplus/bus.hpp> 26 #include <sdbusplus/message.hpp> 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 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 = 104 reinterpret_cast<const struct FanCtrlRequest*>(&reqBuf[0]); 105 106 rc = getFanCtrlProperty(request->zone, ¤t, failsafeProperty); 107 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 */ 123 static ipmi_ret_t getManualModeState(const uint8_t* reqBuf, uint8_t* replyBuf, 124 size_t* dataLen) 125 { 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 = 135 reinterpret_cast<const struct FanCtrlRequest*>(&reqBuf[0]); 136 137 rc = getFanCtrlProperty(request->zone, ¤t, manualProperty); 138 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 */ 155 static ipmi_ret_t setManualModeState(const uint8_t* reqBuf, uint8_t* replyBuf, 156 size_t* dataLen) 157 { 158 ipmi_ret_t rc = IPMI_CC_OK; 159 if (*dataLen < sizeof(struct FanCtrlRequestSet)) 160 { 161 return IPMI_CC_INVALID; 162 } 163 164 using Value = std::variant<bool>; 165 166 const auto request = 167 reinterpret_cast<const struct FanCtrlRequestSet*>(&reqBuf[0]); 168 169 /* 0 is false, 1 is true */ 170 bool setValue = static_cast<bool>(request->value); 171 Value v{setValue}; 172 173 auto PropertyWriteBus = sdbusplus::bus::new_system(); 174 175 std::string path = getControlPath(request->zone); 176 177 auto pimMsg = PropertyWriteBus.new_method_call(busName, path.c_str(), 178 propertiesintf, "Set"); 179 pimMsg.append(intf); 180 pimMsg.append(manualProperty); 181 pimMsg.append(v); 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 static ipmi_ret_t manualModeControl(ipmi_cmd_t cmd, const uint8_t* reqBuf, 198 uint8_t* replyCmdBuf, size_t* dataLen) 199 { 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 = 208 reinterpret_cast<const struct FanCtrlRequest*>(&reqBuf[0]); 209 210 switch (request->command) 211 { 212 case GET_CONTROL_STATE: 213 return getManualModeState(reqBuf, replyCmdBuf, dataLen); 214 case SET_CONTROL_STATE: 215 return setManualModeState(reqBuf, replyCmdBuf, dataLen); 216 case GET_FAILSAFE_STATE: 217 return getFailsafeModeState(reqBuf, replyCmdBuf, dataLen); 218 default: 219 rc = IPMI_CC_INVALID; 220 } 221 222 return rc; 223 } 224 225 } // namespace ipmi 226 } // namespace pid_control 227 228 void setupGlobalOemFanControl() __attribute__((constructor)); 229 230 void setupGlobalOemFanControl() 231 { 232 oem::Router* router = oem::mutableRouter(); 233 234 fprintf(stderr, 235 "Registering OEM:[%#08X], Cmd:[%#04X] for Manual Zone Control\n", 236 oem::obmcOemNumber, oem::Cmd::fanManualCmd); 237 238 router->registerHandler(oem::obmcOemNumber, oem::Cmd::fanManualCmd, 239 pid_control::ipmi::manualModeControl); 240 } 241