| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel Corporation |
| 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 | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 17 | #include "dbuswrite.hpp" |
| 18 | |
| 19 | #include "dbushelper_interface.hpp" |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 20 | #include "interfaces.hpp" |
| Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 21 | |
| Patrick Venture | 76ce5d7 | 2018-10-30 18:39:50 -0700 | [diff] [blame] | 22 | #include <phosphor-logging/log.hpp> |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 23 | #include <sdbusplus/bus.hpp> |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 24 | #include <sdbusplus/exception.hpp> |
| Alexander Hansen | bd2c98e | 2025-11-12 12:47:00 +0100 | [diff] [blame^] | 25 | #include <xyz/openbmc_project/Control/FanPwm/client.hpp> |
| Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 26 | |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 27 | #include <cstdint> |
| Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 28 | #include <exception> |
| Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 29 | #include <memory> |
| Patrick Venture | f5e770b | 2018-10-30 12:28:53 -0700 | [diff] [blame] | 30 | #include <string> |
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 31 | #include <variant> |
| Patrick Venture | f5e770b | 2018-10-30 12:28:53 -0700 | [diff] [blame] | 32 | |
| Alexander Hansen | bd2c98e | 2025-11-12 12:47:00 +0100 | [diff] [blame^] | 33 | using ControlFanPwm = sdbusplus::common::xyz::openbmc_project::control::FanPwm; |
| 34 | |
| Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 35 | namespace pid_control |
| 36 | { |
| 37 | |
| Patrick Venture | 76ce5d7 | 2018-10-30 18:39:50 -0700 | [diff] [blame] | 38 | using namespace phosphor::logging; |
| 39 | |
| Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 40 | std::unique_ptr<WriteInterface> DbusWritePercent::createDbusWrite( |
| 41 | const std::string& path, int64_t min, int64_t max, |
| 42 | std::unique_ptr<DbusHelperInterface> helper) |
| Patrick Venture | f5e770b | 2018-10-30 12:28:53 -0700 | [diff] [blame] | 43 | { |
| Patrick Venture | f5e770b | 2018-10-30 12:28:53 -0700 | [diff] [blame] | 44 | std::string connectionName; |
| 45 | |
| 46 | try |
| 47 | { |
| Alexander Hansen | bd2c98e | 2025-11-12 12:47:00 +0100 | [diff] [blame^] | 48 | connectionName = helper->getService(ControlFanPwm::interface, path); |
| Patrick Venture | f5e770b | 2018-10-30 12:28:53 -0700 | [diff] [blame] | 49 | } |
| 50 | catch (const std::exception& e) |
| 51 | { |
| 52 | return nullptr; |
| 53 | } |
| 54 | |
| 55 | return std::make_unique<DbusWritePercent>(path, min, max, connectionName); |
| 56 | } |
| 57 | |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 58 | void DbusWritePercent::write(double value) |
| 59 | { |
| Josh Lehan | 2400ce4 | 2020-10-01 01:50:39 -0700 | [diff] [blame] | 60 | return write(value, false, nullptr); |
| 61 | } |
| 62 | |
| 63 | void DbusWritePercent::write(double value, bool force, int64_t* written) |
| 64 | { |
| Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 65 | double minimum = getMin(); |
| 66 | double maximum = getMax(); |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 67 | |
| Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 68 | double range = maximum - minimum; |
| 69 | double offset = range * value; |
| 70 | double ovalue = offset + minimum; |
| James Feist | cd9e109 | 2018-10-08 13:06:41 -0700 | [diff] [blame] | 71 | |
| 72 | if (oldValue == static_cast<int64_t>(ovalue)) |
| 73 | { |
| Josh Lehan | 2400ce4 | 2020-10-01 01:50:39 -0700 | [diff] [blame] | 74 | if (!force) |
| 75 | { |
| 76 | if (written) |
| 77 | { |
| 78 | *written = oldValue; |
| 79 | } |
| 80 | return; |
| 81 | } |
| James Feist | cd9e109 | 2018-10-08 13:06:41 -0700 | [diff] [blame] | 82 | } |
| James Feist | 80b5d9a | 2019-01-15 12:51:09 -0800 | [diff] [blame] | 83 | auto writeBus = sdbusplus::bus::new_default(); |
| Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 84 | auto mesg = |
| 85 | writeBus.new_method_call(connectionName.c_str(), path.c_str(), |
| 86 | "org.freedesktop.DBus.Properties", "Set"); |
| Alexander Hansen | bd2c98e | 2025-11-12 12:47:00 +0100 | [diff] [blame^] | 87 | mesg.append(ControlFanPwm::interface, ControlFanPwm::property_names::target, |
| Andrew Geissler | 77ac856 | 2020-05-20 21:09:23 -0500 | [diff] [blame] | 88 | std::variant<uint64_t>(static_cast<uint64_t>(ovalue))); |
| Patrick Venture | 76ce5d7 | 2018-10-30 18:39:50 -0700 | [diff] [blame] | 89 | |
| 90 | try |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 91 | { |
| Patrick Venture | 76ce5d7 | 2018-10-30 18:39:50 -0700 | [diff] [blame] | 92 | // TODO: if we don't use the reply, call_noreply() |
| James Feist | 80b5d9a | 2019-01-15 12:51:09 -0800 | [diff] [blame] | 93 | auto resp = writeBus.call(mesg); |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 94 | } |
| Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 95 | catch (const sdbusplus::exception_t& ex) |
| Patrick Venture | 76ce5d7 | 2018-10-30 18:39:50 -0700 | [diff] [blame] | 96 | { |
| 97 | log<level::ERR>("Dbus Call Failure", entry("PATH=%s", path.c_str()), |
| 98 | entry("WHAT=%s", ex.what())); |
| 99 | } |
| 100 | |
| James Feist | cd9e109 | 2018-10-08 13:06:41 -0700 | [diff] [blame] | 101 | oldValue = static_cast<int64_t>(ovalue); |
| Josh Lehan | 2400ce4 | 2020-10-01 01:50:39 -0700 | [diff] [blame] | 102 | if (written) |
| 103 | { |
| 104 | *written = oldValue; |
| 105 | } |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 106 | return; |
| 107 | } |
| 108 | |
| Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 109 | std::unique_ptr<WriteInterface> DbusWrite::createDbusWrite( |
| 110 | const std::string& path, int64_t min, int64_t max, |
| 111 | std::unique_ptr<DbusHelperInterface> helper) |
| Patrick Venture | f5e770b | 2018-10-30 12:28:53 -0700 | [diff] [blame] | 112 | { |
| Patrick Venture | f5e770b | 2018-10-30 12:28:53 -0700 | [diff] [blame] | 113 | std::string connectionName; |
| 114 | |
| 115 | try |
| 116 | { |
| Alexander Hansen | bd2c98e | 2025-11-12 12:47:00 +0100 | [diff] [blame^] | 117 | connectionName = helper->getService(ControlFanPwm::interface, path); |
| Patrick Venture | f5e770b | 2018-10-30 12:28:53 -0700 | [diff] [blame] | 118 | } |
| 119 | catch (const std::exception& e) |
| 120 | { |
| 121 | return nullptr; |
| 122 | } |
| 123 | |
| 124 | return std::make_unique<DbusWrite>(path, min, max, connectionName); |
| 125 | } |
| 126 | |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 127 | void DbusWrite::write(double value) |
| 128 | { |
| Josh Lehan | 2400ce4 | 2020-10-01 01:50:39 -0700 | [diff] [blame] | 129 | return write(value, false, nullptr); |
| 130 | } |
| 131 | |
| 132 | void DbusWrite::write(double value, bool force, int64_t* written) |
| 133 | { |
| James Feist | cd9e109 | 2018-10-08 13:06:41 -0700 | [diff] [blame] | 134 | if (oldValue == static_cast<int64_t>(value)) |
| 135 | { |
| Josh Lehan | 2400ce4 | 2020-10-01 01:50:39 -0700 | [diff] [blame] | 136 | if (!force) |
| 137 | { |
| 138 | if (written) |
| 139 | { |
| 140 | *written = oldValue; |
| 141 | } |
| 142 | return; |
| 143 | } |
| James Feist | cd9e109 | 2018-10-08 13:06:41 -0700 | [diff] [blame] | 144 | } |
| James Feist | 80b5d9a | 2019-01-15 12:51:09 -0800 | [diff] [blame] | 145 | auto writeBus = sdbusplus::bus::new_default(); |
| Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 146 | auto mesg = |
| 147 | writeBus.new_method_call(connectionName.c_str(), path.c_str(), |
| 148 | "org.freedesktop.DBus.Properties", "Set"); |
| Alexander Hansen | bd2c98e | 2025-11-12 12:47:00 +0100 | [diff] [blame^] | 149 | mesg.append(ControlFanPwm::interface, ControlFanPwm::property_names::target, |
| Andrew Geissler | 77ac856 | 2020-05-20 21:09:23 -0500 | [diff] [blame] | 150 | std::variant<uint64_t>(static_cast<uint64_t>(value))); |
| Patrick Venture | 76ce5d7 | 2018-10-30 18:39:50 -0700 | [diff] [blame] | 151 | |
| 152 | try |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 153 | { |
| Patrick Venture | 76ce5d7 | 2018-10-30 18:39:50 -0700 | [diff] [blame] | 154 | // TODO: consider call_noreplly |
| James Feist | 80b5d9a | 2019-01-15 12:51:09 -0800 | [diff] [blame] | 155 | auto resp = writeBus.call(mesg); |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 156 | } |
| Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 157 | catch (const sdbusplus::exception_t& ex) |
| Patrick Venture | 76ce5d7 | 2018-10-30 18:39:50 -0700 | [diff] [blame] | 158 | { |
| 159 | log<level::ERR>("Dbus Call Failure", entry("PATH=%s", path.c_str()), |
| 160 | entry("WHAT=%s", ex.what())); |
| 161 | } |
| 162 | |
| James Feist | cd9e109 | 2018-10-08 13:06:41 -0700 | [diff] [blame] | 163 | oldValue = static_cast<int64_t>(value); |
| Josh Lehan | 2400ce4 | 2020-10-01 01:50:39 -0700 | [diff] [blame] | 164 | if (written) |
| 165 | { |
| 166 | *written = oldValue; |
| 167 | } |
| James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 168 | return; |
| 169 | } |
| Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 170 | |
| 171 | } // namespace pid_control |