| Alexander Hansen | 46a755f | 2025-10-27 16:31:08 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright 2017 Google Inc |
| Patrick Venture | d82d0b7 | 2020-08-16 09:17:37 -0700 | [diff] [blame] | 3 | |
| Patrick Venture | 09334bb | 2020-08-16 12:22:54 -0700 | [diff] [blame] | 4 | #include "dbus_mode.hpp" |
| 5 | |
| George Liu | 92110f8 | 2025-07-02 15:07:35 +0800 | [diff] [blame] | 6 | #include <ipmid/api-types.hpp> |
| Patrick Venture | d82d0b7 | 2020-08-16 09:17:37 -0700 | [diff] [blame] | 7 | #include <sdbusplus/bus.hpp> |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 8 | #include <sdbusplus/exception.hpp> |
| Patrick Venture | d82d0b7 | 2020-08-16 09:17:37 -0700 | [diff] [blame] | 9 | #include <sdbusplus/message.hpp> |
| 10 | |
| 11 | #include <cstdint> |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 12 | #include <map> |
| Patrick Venture | d82d0b7 | 2020-08-16 09:17:37 -0700 | [diff] [blame] | 13 | #include <string> |
| Patrick Venture | 09334bb | 2020-08-16 12:22:54 -0700 | [diff] [blame] | 14 | #include <variant> |
| Patrick Venture | d82d0b7 | 2020-08-16 09:17:37 -0700 | [diff] [blame] | 15 | |
| George Liu | 92110f8 | 2025-07-02 15:07:35 +0800 | [diff] [blame] | 16 | namespace pid_control::ipmi |
| Patrick Venture | d82d0b7 | 2020-08-16 09:17:37 -0700 | [diff] [blame] | 17 | { |
| 18 | |
| 19 | static constexpr auto objectPath = "/xyz/openbmc_project/settings/fanctrl/zone"; |
| 20 | static constexpr auto busName = "xyz.openbmc_project.State.FanCtrl"; |
| 21 | static constexpr auto intf = "xyz.openbmc_project.Control.Mode"; |
| Patrick Venture | d82d0b7 | 2020-08-16 09:17:37 -0700 | [diff] [blame] | 22 | static constexpr auto propertiesintf = "org.freedesktop.DBus.Properties"; |
| 23 | |
| 24 | using Property = std::string; |
| 25 | using Value = std::variant<bool>; |
| 26 | using PropertyMap = std::map<Property, Value>; |
| 27 | |
| 28 | /* The following was copied directly from my manual thread handler. */ |
| 29 | static std::string getControlPath(int8_t zone) |
| 30 | { |
| 31 | return std::string(objectPath) + std::to_string(zone); |
| 32 | } |
| 33 | |
| Patrick Venture | 09334bb | 2020-08-16 12:22:54 -0700 | [diff] [blame] | 34 | uint8_t DbusZoneControl::getFanCtrlProperty(uint8_t zoneId, bool* value, |
| 35 | const std::string& property) |
| Patrick Venture | d82d0b7 | 2020-08-16 09:17:37 -0700 | [diff] [blame] | 36 | { |
| 37 | std::string path = getControlPath(zoneId); |
| 38 | |
| 39 | auto propertyReadBus = sdbusplus::bus::new_system(); |
| 40 | auto pimMsg = propertyReadBus.new_method_call(busName, path.c_str(), |
| 41 | propertiesintf, "GetAll"); |
| 42 | pimMsg.append(intf); |
| 43 | |
| 44 | try |
| 45 | { |
| 46 | PropertyMap propMap; |
| 47 | |
| 48 | /* a method could error but the call not error. */ |
| 49 | auto valueResponseMsg = propertyReadBus.call(pimMsg); |
| 50 | |
| 51 | valueResponseMsg.read(propMap); |
| 52 | |
| 53 | *value = std::get<bool>(propMap[property]); |
| 54 | } |
| Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 55 | catch (const sdbusplus::exception_t& ex) |
| Patrick Venture | d82d0b7 | 2020-08-16 09:17:37 -0700 | [diff] [blame] | 56 | { |
| George Liu | 92110f8 | 2025-07-02 15:07:35 +0800 | [diff] [blame] | 57 | return ::ipmi::ccInvalidCommand; |
| Patrick Venture | d82d0b7 | 2020-08-16 09:17:37 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| George Liu | 92110f8 | 2025-07-02 15:07:35 +0800 | [diff] [blame] | 60 | return ::ipmi::ccSuccess; |
| Patrick Venture | d82d0b7 | 2020-08-16 09:17:37 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| Patrick Venture | 09334bb | 2020-08-16 12:22:54 -0700 | [diff] [blame] | 63 | uint8_t DbusZoneControl::setFanCtrlProperty(uint8_t zoneId, bool value, |
| 64 | const std::string& property) |
| 65 | { |
| 66 | using Value = std::variant<bool>; |
| 67 | Value v{value}; |
| 68 | |
| 69 | std::string path = getControlPath(zoneId); |
| 70 | |
| 71 | auto PropertyWriteBus = sdbusplus::bus::new_system(); |
| 72 | auto pimMsg = PropertyWriteBus.new_method_call(busName, path.c_str(), |
| 73 | propertiesintf, "Set"); |
| 74 | pimMsg.append(intf); |
| 75 | pimMsg.append(property); |
| 76 | pimMsg.append(v); |
| 77 | |
| 78 | try |
| 79 | { |
| 80 | PropertyWriteBus.call_noreply(pimMsg); |
| 81 | } |
| Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 82 | catch (const sdbusplus::exception_t& ex) |
| Patrick Venture | 09334bb | 2020-08-16 12:22:54 -0700 | [diff] [blame] | 83 | { |
| George Liu | 92110f8 | 2025-07-02 15:07:35 +0800 | [diff] [blame] | 84 | return ::ipmi::ccInvalidCommand; |
| Patrick Venture | 09334bb | 2020-08-16 12:22:54 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /* TODO(venture): Should sanity check the result. */ |
| George Liu | 92110f8 | 2025-07-02 15:07:35 +0800 | [diff] [blame] | 88 | return ::ipmi::ccSuccess; |
| Patrick Venture | 09334bb | 2020-08-16 12:22:54 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| George Liu | 92110f8 | 2025-07-02 15:07:35 +0800 | [diff] [blame] | 91 | } // namespace pid_control::ipmi |