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