blob: efb7dee5dad85e3f21de185cb517aa64659eb717 [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>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070025
Ed Tanousf8b6e552025-06-27 13:27:50 -070026#include <cstdint>
Patrick Venture8729eb92020-08-10 10:38:44 -070027#include <exception>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070028#include <memory>
Patrick Venturef5e770b2018-10-30 12:28:53 -070029#include <string>
James Feist1f802f52019-02-08 13:51:43 -080030#include <variant>
Patrick Venturef5e770b2018-10-30 12:28:53 -070031
Patrick Venturea0764872020-08-08 07:48:43 -070032namespace pid_control
33{
34
Patrick Venturef5e770b2018-10-30 12:28:53 -070035constexpr const char* pwmInterface = "xyz.openbmc_project.Control.FanPwm";
James Feist7136a5a2018-07-19 09:52:05 -070036
Patrick Venture76ce5d72018-10-30 18:39:50 -070037using namespace phosphor::logging;
38
Patrick Venture8729eb92020-08-10 10:38:44 -070039std::unique_ptr<WriteInterface> DbusWritePercent::createDbusWrite(
40 const std::string& path, int64_t min, int64_t max,
41 std::unique_ptr<DbusHelperInterface> helper)
Patrick Venturef5e770b2018-10-30 12:28:53 -070042{
Patrick Venturef5e770b2018-10-30 12:28:53 -070043 std::string connectionName;
44
45 try
46 {
Patrick Venture9b936922020-08-10 11:28:39 -070047 connectionName = helper->getService(pwmInterface, path);
Patrick Venturef5e770b2018-10-30 12:28:53 -070048 }
49 catch (const std::exception& e)
50 {
51 return nullptr;
52 }
53
54 return std::make_unique<DbusWritePercent>(path, min, max, connectionName);
55}
56
James Feist7136a5a2018-07-19 09:52:05 -070057void DbusWritePercent::write(double value)
58{
Josh Lehan2400ce42020-10-01 01:50:39 -070059 return write(value, false, nullptr);
60}
61
62void DbusWritePercent::write(double value, bool force, int64_t* written)
63{
Patrick Venture5f59c0f2018-11-11 12:55:14 -080064 double minimum = getMin();
65 double maximum = getMax();
James Feist7136a5a2018-07-19 09:52:05 -070066
Patrick Venture5f59c0f2018-11-11 12:55:14 -080067 double range = maximum - minimum;
68 double offset = range * value;
69 double ovalue = offset + minimum;
James Feistcd9e1092018-10-08 13:06:41 -070070
71 if (oldValue == static_cast<int64_t>(ovalue))
72 {
Josh Lehan2400ce42020-10-01 01:50:39 -070073 if (!force)
74 {
75 if (written)
76 {
77 *written = oldValue;
78 }
79 return;
80 }
James Feistcd9e1092018-10-08 13:06:41 -070081 }
James Feist80b5d9a2019-01-15 12:51:09 -080082 auto writeBus = sdbusplus::bus::new_default();
Patrick Williamsbd63bca2024-08-16 15:21:10 -040083 auto mesg =
84 writeBus.new_method_call(connectionName.c_str(), path.c_str(),
85 "org.freedesktop.DBus.Properties", "Set");
Andrew Geissler77ac8562020-05-20 21:09:23 -050086 mesg.append(pwmInterface, "Target",
87 std::variant<uint64_t>(static_cast<uint64_t>(ovalue)));
Patrick Venture76ce5d72018-10-30 18:39:50 -070088
89 try
James Feist7136a5a2018-07-19 09:52:05 -070090 {
Patrick Venture76ce5d72018-10-30 18:39:50 -070091 // TODO: if we don't use the reply, call_noreply()
James Feist80b5d9a2019-01-15 12:51:09 -080092 auto resp = writeBus.call(mesg);
James Feist7136a5a2018-07-19 09:52:05 -070093 }
Patrick Williamsb228bc32022-07-22 19:26:56 -050094 catch (const sdbusplus::exception_t& ex)
Patrick Venture76ce5d72018-10-30 18:39:50 -070095 {
96 log<level::ERR>("Dbus Call Failure", entry("PATH=%s", path.c_str()),
97 entry("WHAT=%s", ex.what()));
98 }
99
James Feistcd9e1092018-10-08 13:06:41 -0700100 oldValue = static_cast<int64_t>(ovalue);
Josh Lehan2400ce42020-10-01 01:50:39 -0700101 if (written)
102 {
103 *written = oldValue;
104 }
James Feist7136a5a2018-07-19 09:52:05 -0700105 return;
106}
107
Patrick Williamsbd63bca2024-08-16 15:21:10 -0400108std::unique_ptr<WriteInterface> DbusWrite::createDbusWrite(
109 const std::string& path, int64_t min, int64_t max,
110 std::unique_ptr<DbusHelperInterface> helper)
Patrick Venturef5e770b2018-10-30 12:28:53 -0700111{
Patrick Venturef5e770b2018-10-30 12:28:53 -0700112 std::string connectionName;
113
114 try
115 {
Patrick Venture9b936922020-08-10 11:28:39 -0700116 connectionName = helper->getService(pwmInterface, path);
Patrick Venturef5e770b2018-10-30 12:28:53 -0700117 }
118 catch (const std::exception& e)
119 {
120 return nullptr;
121 }
122
123 return std::make_unique<DbusWrite>(path, min, max, connectionName);
124}
125
James Feist7136a5a2018-07-19 09:52:05 -0700126void DbusWrite::write(double value)
127{
Josh Lehan2400ce42020-10-01 01:50:39 -0700128 return write(value, false, nullptr);
129}
130
131void DbusWrite::write(double value, bool force, int64_t* written)
132{
James Feistcd9e1092018-10-08 13:06:41 -0700133 if (oldValue == static_cast<int64_t>(value))
134 {
Josh Lehan2400ce42020-10-01 01:50:39 -0700135 if (!force)
136 {
137 if (written)
138 {
139 *written = oldValue;
140 }
141 return;
142 }
James Feistcd9e1092018-10-08 13:06:41 -0700143 }
James Feist80b5d9a2019-01-15 12:51:09 -0800144 auto writeBus = sdbusplus::bus::new_default();
Patrick Williamsbd63bca2024-08-16 15:21:10 -0400145 auto mesg =
146 writeBus.new_method_call(connectionName.c_str(), path.c_str(),
147 "org.freedesktop.DBus.Properties", "Set");
Andrew Geissler77ac8562020-05-20 21:09:23 -0500148 mesg.append(pwmInterface, "Target",
149 std::variant<uint64_t>(static_cast<uint64_t>(value)));
Patrick Venture76ce5d72018-10-30 18:39:50 -0700150
151 try
James Feist7136a5a2018-07-19 09:52:05 -0700152 {
Patrick Venture76ce5d72018-10-30 18:39:50 -0700153 // TODO: consider call_noreplly
James Feist80b5d9a2019-01-15 12:51:09 -0800154 auto resp = writeBus.call(mesg);
James Feist7136a5a2018-07-19 09:52:05 -0700155 }
Patrick Williamsb228bc32022-07-22 19:26:56 -0500156 catch (const sdbusplus::exception_t& ex)
Patrick Venture76ce5d72018-10-30 18:39:50 -0700157 {
158 log<level::ERR>("Dbus Call Failure", entry("PATH=%s", path.c_str()),
159 entry("WHAT=%s", ex.what()));
160 }
161
James Feistcd9e1092018-10-08 13:06:41 -0700162 oldValue = static_cast<int64_t>(value);
Josh Lehan2400ce42020-10-01 01:50:39 -0700163 if (written)
164 {
165 *written = oldValue;
166 }
James Feist7136a5a2018-07-19 09:52:05 -0700167 return;
168}
Patrick Venturea0764872020-08-08 07:48:43 -0700169
170} // namespace pid_control