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