blob: 8a87116d910ba3698886cb150ecfb30418598eb9 [file] [log] [blame]
James Feist7136a5a2018-07-19 09:52:05 -07001/*
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 Ventureaadb30d2020-08-10 09:17:11 -070017#include "dbuswrite.hpp"
18
19#include "dbushelper_interface.hpp"
Ed Tanousf8b6e552025-06-27 13:27:50 -070020#include "interfaces.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070021
Patrick Venture76ce5d72018-10-30 18:39:50 -070022#include <phosphor-logging/log.hpp>
James Feist7136a5a2018-07-19 09:52:05 -070023#include <sdbusplus/bus.hpp>
Ed Tanousf8b6e552025-06-27 13:27:50 -070024#include <sdbusplus/exception.hpp>
Alexander Hansenbd2c98e2025-11-12 12:47:00 +010025#include <xyz/openbmc_project/Control/FanPwm/client.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070026
Ed Tanousf8b6e552025-06-27 13:27:50 -070027#include <cstdint>
Patrick Venture8729eb92020-08-10 10:38:44 -070028#include <exception>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070029#include <memory>
Patrick Venturef5e770b2018-10-30 12:28:53 -070030#include <string>
James Feist1f802f52019-02-08 13:51:43 -080031#include <variant>
Patrick Venturef5e770b2018-10-30 12:28:53 -070032
Alexander Hansenbd2c98e2025-11-12 12:47:00 +010033using ControlFanPwm = sdbusplus::common::xyz::openbmc_project::control::FanPwm;
34
Patrick Venturea0764872020-08-08 07:48:43 -070035namespace pid_control
36{
37
Patrick Venture76ce5d72018-10-30 18:39:50 -070038using namespace phosphor::logging;
39
Patrick Venture8729eb92020-08-10 10:38:44 -070040std::unique_ptr<WriteInterface> DbusWritePercent::createDbusWrite(
41 const std::string& path, int64_t min, int64_t max,
42 std::unique_ptr<DbusHelperInterface> helper)
Patrick Venturef5e770b2018-10-30 12:28:53 -070043{
Patrick Venturef5e770b2018-10-30 12:28:53 -070044 std::string connectionName;
45
46 try
47 {
Alexander Hansenbd2c98e2025-11-12 12:47:00 +010048 connectionName = helper->getService(ControlFanPwm::interface, path);
Patrick Venturef5e770b2018-10-30 12:28:53 -070049 }
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 Feist7136a5a2018-07-19 09:52:05 -070058void DbusWritePercent::write(double value)
59{
Josh Lehan2400ce42020-10-01 01:50:39 -070060 return write(value, false, nullptr);
61}
62
63void DbusWritePercent::write(double value, bool force, int64_t* written)
64{
Patrick Venture5f59c0f2018-11-11 12:55:14 -080065 double minimum = getMin();
66 double maximum = getMax();
James Feist7136a5a2018-07-19 09:52:05 -070067
Patrick Venture5f59c0f2018-11-11 12:55:14 -080068 double range = maximum - minimum;
69 double offset = range * value;
70 double ovalue = offset + minimum;
James Feistcd9e1092018-10-08 13:06:41 -070071
72 if (oldValue == static_cast<int64_t>(ovalue))
73 {
Josh Lehan2400ce42020-10-01 01:50:39 -070074 if (!force)
75 {
76 if (written)
77 {
78 *written = oldValue;
79 }
80 return;
81 }
James Feistcd9e1092018-10-08 13:06:41 -070082 }
James Feist80b5d9a2019-01-15 12:51:09 -080083 auto writeBus = sdbusplus::bus::new_default();
Patrick Williamsbd63bca2024-08-16 15:21:10 -040084 auto mesg =
85 writeBus.new_method_call(connectionName.c_str(), path.c_str(),
86 "org.freedesktop.DBus.Properties", "Set");
Alexander Hansenbd2c98e2025-11-12 12:47:00 +010087 mesg.append(ControlFanPwm::interface, ControlFanPwm::property_names::target,
Andrew Geissler77ac8562020-05-20 21:09:23 -050088 std::variant<uint64_t>(static_cast<uint64_t>(ovalue)));
Patrick Venture76ce5d72018-10-30 18:39:50 -070089
90 try
James Feist7136a5a2018-07-19 09:52:05 -070091 {
Patrick Venture76ce5d72018-10-30 18:39:50 -070092 // TODO: if we don't use the reply, call_noreply()
James Feist80b5d9a2019-01-15 12:51:09 -080093 auto resp = writeBus.call(mesg);
James Feist7136a5a2018-07-19 09:52:05 -070094 }
Patrick Williamsb228bc32022-07-22 19:26:56 -050095 catch (const sdbusplus::exception_t& ex)
Patrick Venture76ce5d72018-10-30 18:39:50 -070096 {
97 log<level::ERR>("Dbus Call Failure", entry("PATH=%s", path.c_str()),
98 entry("WHAT=%s", ex.what()));
99 }
100
James Feistcd9e1092018-10-08 13:06:41 -0700101 oldValue = static_cast<int64_t>(ovalue);
Josh Lehan2400ce42020-10-01 01:50:39 -0700102 if (written)
103 {
104 *written = oldValue;
105 }
James Feist7136a5a2018-07-19 09:52:05 -0700106 return;
107}
108
Patrick Williamsbd63bca2024-08-16 15:21:10 -0400109std::unique_ptr<WriteInterface> DbusWrite::createDbusWrite(
110 const std::string& path, int64_t min, int64_t max,
111 std::unique_ptr<DbusHelperInterface> helper)
Patrick Venturef5e770b2018-10-30 12:28:53 -0700112{
Patrick Venturef5e770b2018-10-30 12:28:53 -0700113 std::string connectionName;
114
115 try
116 {
Alexander Hansenbd2c98e2025-11-12 12:47:00 +0100117 connectionName = helper->getService(ControlFanPwm::interface, path);
Patrick Venturef5e770b2018-10-30 12:28:53 -0700118 }
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 Feist7136a5a2018-07-19 09:52:05 -0700127void DbusWrite::write(double value)
128{
Josh Lehan2400ce42020-10-01 01:50:39 -0700129 return write(value, false, nullptr);
130}
131
132void DbusWrite::write(double value, bool force, int64_t* written)
133{
James Feistcd9e1092018-10-08 13:06:41 -0700134 if (oldValue == static_cast<int64_t>(value))
135 {
Josh Lehan2400ce42020-10-01 01:50:39 -0700136 if (!force)
137 {
138 if (written)
139 {
140 *written = oldValue;
141 }
142 return;
143 }
James Feistcd9e1092018-10-08 13:06:41 -0700144 }
James Feist80b5d9a2019-01-15 12:51:09 -0800145 auto writeBus = sdbusplus::bus::new_default();
Patrick Williamsbd63bca2024-08-16 15:21:10 -0400146 auto mesg =
147 writeBus.new_method_call(connectionName.c_str(), path.c_str(),
148 "org.freedesktop.DBus.Properties", "Set");
Alexander Hansenbd2c98e2025-11-12 12:47:00 +0100149 mesg.append(ControlFanPwm::interface, ControlFanPwm::property_names::target,
Andrew Geissler77ac8562020-05-20 21:09:23 -0500150 std::variant<uint64_t>(static_cast<uint64_t>(value)));
Patrick Venture76ce5d72018-10-30 18:39:50 -0700151
152 try
James Feist7136a5a2018-07-19 09:52:05 -0700153 {
Patrick Venture76ce5d72018-10-30 18:39:50 -0700154 // TODO: consider call_noreplly
James Feist80b5d9a2019-01-15 12:51:09 -0800155 auto resp = writeBus.call(mesg);
James Feist7136a5a2018-07-19 09:52:05 -0700156 }
Patrick Williamsb228bc32022-07-22 19:26:56 -0500157 catch (const sdbusplus::exception_t& ex)
Patrick Venture76ce5d72018-10-30 18:39:50 -0700158 {
159 log<level::ERR>("Dbus Call Failure", entry("PATH=%s", path.c_str()),
160 entry("WHAT=%s", ex.what()));
161 }
162
James Feistcd9e1092018-10-08 13:06:41 -0700163 oldValue = static_cast<int64_t>(value);
Josh Lehan2400ce42020-10-01 01:50:39 -0700164 if (written)
165 {
166 *written = oldValue;
167 }
James Feist7136a5a2018-07-19 09:52:05 -0700168 return;
169}
Patrick Venturea0764872020-08-08 07:48:43 -0700170
171} // namespace pid_control