blob: baae941cff3fc9a3303afa6a0bb5d575b2fd013e [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"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070020
Patrick Venture76ce5d72018-10-30 18:39:50 -070021#include <phosphor-logging/log.hpp>
James Feist7136a5a2018-07-19 09:52:05 -070022#include <sdbusplus/bus.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070023
Patrick Venture8729eb92020-08-10 10:38:44 -070024#include <exception>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070025#include <iostream>
26#include <memory>
Patrick Venturef5e770b2018-10-30 12:28:53 -070027#include <string>
James Feist1f802f52019-02-08 13:51:43 -080028#include <variant>
Patrick Venturef5e770b2018-10-30 12:28:53 -070029
Patrick Venturea0764872020-08-08 07:48:43 -070030namespace pid_control
31{
32
Patrick Venturef5e770b2018-10-30 12:28:53 -070033constexpr const char* pwmInterface = "xyz.openbmc_project.Control.FanPwm";
James Feist7136a5a2018-07-19 09:52:05 -070034
Patrick Venture76ce5d72018-10-30 18:39:50 -070035using namespace phosphor::logging;
36
Patrick Venture8729eb92020-08-10 10:38:44 -070037std::unique_ptr<WriteInterface> DbusWritePercent::createDbusWrite(
38 const std::string& path, int64_t min, int64_t max,
39 std::unique_ptr<DbusHelperInterface> helper)
Patrick Venturef5e770b2018-10-30 12:28:53 -070040{
Patrick Venturef5e770b2018-10-30 12:28:53 -070041 std::string connectionName;
42
43 try
44 {
Patrick Venture9b936922020-08-10 11:28:39 -070045 connectionName = helper->getService(pwmInterface, path);
Patrick Venturef5e770b2018-10-30 12:28:53 -070046 }
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 Feist7136a5a2018-07-19 09:52:05 -070055void DbusWritePercent::write(double value)
56{
Josh Lehan2400ce42020-10-01 01:50:39 -070057 return write(value, false, nullptr);
58}
59
60void DbusWritePercent::write(double value, bool force, int64_t* written)
61{
Patrick Venture5f59c0f2018-11-11 12:55:14 -080062 double minimum = getMin();
63 double maximum = getMax();
James Feist7136a5a2018-07-19 09:52:05 -070064
Patrick Venture5f59c0f2018-11-11 12:55:14 -080065 double range = maximum - minimum;
66 double offset = range * value;
67 double ovalue = offset + minimum;
James Feistcd9e1092018-10-08 13:06:41 -070068
69 if (oldValue == static_cast<int64_t>(ovalue))
70 {
Josh Lehan2400ce42020-10-01 01:50:39 -070071 if (!force)
72 {
73 if (written)
74 {
75 *written = oldValue;
76 }
77 return;
78 }
James Feistcd9e1092018-10-08 13:06:41 -070079 }
James Feist80b5d9a2019-01-15 12:51:09 -080080 auto writeBus = sdbusplus::bus::new_default();
Patrick Williamsbd63bca2024-08-16 15:21:10 -040081 auto mesg =
82 writeBus.new_method_call(connectionName.c_str(), path.c_str(),
83 "org.freedesktop.DBus.Properties", "Set");
Andrew Geissler77ac8562020-05-20 21:09:23 -050084 mesg.append(pwmInterface, "Target",
85 std::variant<uint64_t>(static_cast<uint64_t>(ovalue)));
Patrick Venture76ce5d72018-10-30 18:39:50 -070086
87 try
James Feist7136a5a2018-07-19 09:52:05 -070088 {
Patrick Venture76ce5d72018-10-30 18:39:50 -070089 // TODO: if we don't use the reply, call_noreply()
James Feist80b5d9a2019-01-15 12:51:09 -080090 auto resp = writeBus.call(mesg);
James Feist7136a5a2018-07-19 09:52:05 -070091 }
Patrick Williamsb228bc32022-07-22 19:26:56 -050092 catch (const sdbusplus::exception_t& ex)
Patrick Venture76ce5d72018-10-30 18:39:50 -070093 {
94 log<level::ERR>("Dbus Call Failure", entry("PATH=%s", path.c_str()),
95 entry("WHAT=%s", ex.what()));
96 }
97
James Feistcd9e1092018-10-08 13:06:41 -070098 oldValue = static_cast<int64_t>(ovalue);
Josh Lehan2400ce42020-10-01 01:50:39 -070099 if (written)
100 {
101 *written = oldValue;
102 }
James Feist7136a5a2018-07-19 09:52:05 -0700103 return;
104}
105
Patrick Williamsbd63bca2024-08-16 15:21:10 -0400106std::unique_ptr<WriteInterface> DbusWrite::createDbusWrite(
107 const std::string& path, int64_t min, int64_t max,
108 std::unique_ptr<DbusHelperInterface> helper)
Patrick Venturef5e770b2018-10-30 12:28:53 -0700109{
Patrick Venturef5e770b2018-10-30 12:28:53 -0700110 std::string connectionName;
111
112 try
113 {
Patrick Venture9b936922020-08-10 11:28:39 -0700114 connectionName = helper->getService(pwmInterface, path);
Patrick Venturef5e770b2018-10-30 12:28:53 -0700115 }
116 catch (const std::exception& e)
117 {
118 return nullptr;
119 }
120
121 return std::make_unique<DbusWrite>(path, min, max, connectionName);
122}
123
James Feist7136a5a2018-07-19 09:52:05 -0700124void DbusWrite::write(double value)
125{
Josh Lehan2400ce42020-10-01 01:50:39 -0700126 return write(value, false, nullptr);
127}
128
129void DbusWrite::write(double value, bool force, int64_t* written)
130{
James Feistcd9e1092018-10-08 13:06:41 -0700131 if (oldValue == static_cast<int64_t>(value))
132 {
Josh Lehan2400ce42020-10-01 01:50:39 -0700133 if (!force)
134 {
135 if (written)
136 {
137 *written = oldValue;
138 }
139 return;
140 }
James Feistcd9e1092018-10-08 13:06:41 -0700141 }
James Feist80b5d9a2019-01-15 12:51:09 -0800142 auto writeBus = sdbusplus::bus::new_default();
Patrick Williamsbd63bca2024-08-16 15:21:10 -0400143 auto mesg =
144 writeBus.new_method_call(connectionName.c_str(), path.c_str(),
145 "org.freedesktop.DBus.Properties", "Set");
Andrew Geissler77ac8562020-05-20 21:09:23 -0500146 mesg.append(pwmInterface, "Target",
147 std::variant<uint64_t>(static_cast<uint64_t>(value)));
Patrick Venture76ce5d72018-10-30 18:39:50 -0700148
149 try
James Feist7136a5a2018-07-19 09:52:05 -0700150 {
Patrick Venture76ce5d72018-10-30 18:39:50 -0700151 // TODO: consider call_noreplly
James Feist80b5d9a2019-01-15 12:51:09 -0800152 auto resp = writeBus.call(mesg);
James Feist7136a5a2018-07-19 09:52:05 -0700153 }
Patrick Williamsb228bc32022-07-22 19:26:56 -0500154 catch (const sdbusplus::exception_t& ex)
Patrick Venture76ce5d72018-10-30 18:39:50 -0700155 {
156 log<level::ERR>("Dbus Call Failure", entry("PATH=%s", path.c_str()),
157 entry("WHAT=%s", ex.what()));
158 }
159
James Feistcd9e1092018-10-08 13:06:41 -0700160 oldValue = static_cast<int64_t>(value);
Josh Lehan2400ce42020-10-01 01:50:39 -0700161 if (written)
162 {
163 *written = oldValue;
164 }
James Feist7136a5a2018-07-19 09:52:05 -0700165 return;
166}
Patrick Venturea0764872020-08-08 07:48:43 -0700167
168} // namespace pid_control