Patrick Venture | d82d0b7 | 2020-08-16 09:17:37 -0700 | [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 | 09334bb | 2020-08-16 12:22:54 -0700 | [diff] [blame] | 17 | #include "dbus_mode.hpp" |
| 18 | |
Patrick Venture | d82d0b7 | 2020-08-16 09:17:37 -0700 | [diff] [blame] | 19 | #include <ipmid/api.h> |
| 20 | |
| 21 | #include <sdbusplus/bus.hpp> |
| 22 | #include <sdbusplus/message.hpp> |
| 23 | |
| 24 | #include <cstdint> |
| 25 | #include <string> |
Patrick Venture | 09334bb | 2020-08-16 12:22:54 -0700 | [diff] [blame] | 26 | #include <variant> |
Patrick Venture | d82d0b7 | 2020-08-16 09:17:37 -0700 | [diff] [blame] | 27 | |
| 28 | namespace pid_control |
| 29 | { |
| 30 | namespace ipmi |
| 31 | { |
| 32 | |
| 33 | static constexpr auto objectPath = "/xyz/openbmc_project/settings/fanctrl/zone"; |
| 34 | static constexpr auto busName = "xyz.openbmc_project.State.FanCtrl"; |
| 35 | static constexpr auto intf = "xyz.openbmc_project.Control.Mode"; |
| 36 | static constexpr auto manualProperty = "Manual"; |
| 37 | static constexpr auto failsafeProperty = "FailSafe"; |
| 38 | static constexpr auto propertiesintf = "org.freedesktop.DBus.Properties"; |
| 39 | |
| 40 | using Property = std::string; |
| 41 | using Value = std::variant<bool>; |
| 42 | using PropertyMap = std::map<Property, Value>; |
| 43 | |
| 44 | /* The following was copied directly from my manual thread handler. */ |
| 45 | static std::string getControlPath(int8_t zone) |
| 46 | { |
| 47 | return std::string(objectPath) + std::to_string(zone); |
| 48 | } |
| 49 | |
Patrick Venture | 09334bb | 2020-08-16 12:22:54 -0700 | [diff] [blame] | 50 | uint8_t DbusZoneControl::getFanCtrlProperty(uint8_t zoneId, bool* value, |
| 51 | const std::string& property) |
Patrick Venture | d82d0b7 | 2020-08-16 09:17:37 -0700 | [diff] [blame] | 52 | { |
| 53 | std::string path = getControlPath(zoneId); |
| 54 | |
| 55 | auto propertyReadBus = sdbusplus::bus::new_system(); |
| 56 | auto pimMsg = propertyReadBus.new_method_call(busName, path.c_str(), |
| 57 | propertiesintf, "GetAll"); |
| 58 | pimMsg.append(intf); |
| 59 | |
| 60 | try |
| 61 | { |
| 62 | PropertyMap propMap; |
| 63 | |
| 64 | /* a method could error but the call not error. */ |
| 65 | auto valueResponseMsg = propertyReadBus.call(pimMsg); |
| 66 | |
| 67 | valueResponseMsg.read(propMap); |
| 68 | |
| 69 | *value = std::get<bool>(propMap[property]); |
| 70 | } |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 71 | catch (const sdbusplus::exception_t& ex) |
Patrick Venture | d82d0b7 | 2020-08-16 09:17:37 -0700 | [diff] [blame] | 72 | { |
| 73 | return IPMI_CC_INVALID; |
| 74 | } |
| 75 | |
| 76 | return IPMI_CC_OK; |
| 77 | } |
| 78 | |
Patrick Venture | 09334bb | 2020-08-16 12:22:54 -0700 | [diff] [blame] | 79 | uint8_t DbusZoneControl::setFanCtrlProperty(uint8_t zoneId, bool value, |
| 80 | const std::string& property) |
| 81 | { |
| 82 | using Value = std::variant<bool>; |
| 83 | Value v{value}; |
| 84 | |
| 85 | std::string path = getControlPath(zoneId); |
| 86 | |
| 87 | auto PropertyWriteBus = sdbusplus::bus::new_system(); |
| 88 | auto pimMsg = PropertyWriteBus.new_method_call(busName, path.c_str(), |
| 89 | propertiesintf, "Set"); |
| 90 | pimMsg.append(intf); |
| 91 | pimMsg.append(property); |
| 92 | pimMsg.append(v); |
| 93 | |
| 94 | try |
| 95 | { |
| 96 | PropertyWriteBus.call_noreply(pimMsg); |
| 97 | } |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 98 | catch (const sdbusplus::exception_t& ex) |
Patrick Venture | 09334bb | 2020-08-16 12:22:54 -0700 | [diff] [blame] | 99 | { |
| 100 | return IPMI_CC_INVALID; |
| 101 | } |
| 102 | |
| 103 | /* TODO(venture): Should sanity check the result. */ |
| 104 | return IPMI_CC_OK; |
| 105 | } |
| 106 | |
Patrick Venture | d82d0b7 | 2020-08-16 09:17:37 -0700 | [diff] [blame] | 107 | } // namespace ipmi |
| 108 | } // namespace pid_control |