blob: a6b2835bcd521c5080b0106e03cf68bbaab94e53 [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{
James Feist9fa90c12019-01-11 15:35:22 -080041 auto tempBus = sdbusplus::bus::new_system();
Patrick Venturef5e770b2018-10-30 12:28:53 -070042 std::string connectionName;
43
44 try
45 {
Patrick Venture8729eb92020-08-10 10:38:44 -070046 connectionName = helper->getService(tempBus, pwmInterface, path);
Patrick Venturef5e770b2018-10-30 12:28:53 -070047 }
48 catch (const std::exception& e)
49 {
50 return nullptr;
51 }
52
53 return std::make_unique<DbusWritePercent>(path, min, max, connectionName);
54}
55
James Feist7136a5a2018-07-19 09:52:05 -070056void DbusWritePercent::write(double value)
57{
Patrick Venture5f59c0f2018-11-11 12:55:14 -080058 double minimum = getMin();
59 double maximum = getMax();
James Feist7136a5a2018-07-19 09:52:05 -070060
Patrick Venture5f59c0f2018-11-11 12:55:14 -080061 double range = maximum - minimum;
62 double offset = range * value;
63 double ovalue = offset + minimum;
James Feistcd9e1092018-10-08 13:06:41 -070064
65 if (oldValue == static_cast<int64_t>(ovalue))
66 {
67 return;
68 }
James Feist80b5d9a2019-01-15 12:51:09 -080069 auto writeBus = sdbusplus::bus::new_default();
James Feist7136a5a2018-07-19 09:52:05 -070070 auto mesg =
James Feist80b5d9a2019-01-15 12:51:09 -080071 writeBus.new_method_call(connectionName.c_str(), path.c_str(),
72 "org.freedesktop.DBus.Properties", "Set");
Andrew Geissler77ac8562020-05-20 21:09:23 -050073 mesg.append(pwmInterface, "Target",
74 std::variant<uint64_t>(static_cast<uint64_t>(ovalue)));
Patrick Venture76ce5d72018-10-30 18:39:50 -070075
76 try
James Feist7136a5a2018-07-19 09:52:05 -070077 {
Patrick Venture76ce5d72018-10-30 18:39:50 -070078 // TODO: if we don't use the reply, call_noreply()
James Feist80b5d9a2019-01-15 12:51:09 -080079 auto resp = writeBus.call(mesg);
James Feist7136a5a2018-07-19 09:52:05 -070080 }
Patrick Venture76ce5d72018-10-30 18:39:50 -070081 catch (const sdbusplus::exception::SdBusError& ex)
82 {
83 log<level::ERR>("Dbus Call Failure", entry("PATH=%s", path.c_str()),
84 entry("WHAT=%s", ex.what()));
85 }
86
James Feistcd9e1092018-10-08 13:06:41 -070087 oldValue = static_cast<int64_t>(ovalue);
James Feist7136a5a2018-07-19 09:52:05 -070088 return;
89}
90
Patrick Venturef5e770b2018-10-30 12:28:53 -070091std::unique_ptr<WriteInterface>
92 DbusWrite::createDbusWrite(const std::string& path, int64_t min,
Patrick Venture8729eb92020-08-10 10:38:44 -070093 int64_t max,
94 std::unique_ptr<DbusHelperInterface> helper)
Patrick Venturef5e770b2018-10-30 12:28:53 -070095{
James Feist9fa90c12019-01-11 15:35:22 -080096 auto tempBus = sdbusplus::bus::new_system();
Patrick Venturef5e770b2018-10-30 12:28:53 -070097 std::string connectionName;
98
99 try
100 {
Patrick Venture8729eb92020-08-10 10:38:44 -0700101 connectionName = helper->getService(tempBus, pwmInterface, path);
Patrick Venturef5e770b2018-10-30 12:28:53 -0700102 }
103 catch (const std::exception& e)
104 {
105 return nullptr;
106 }
107
108 return std::make_unique<DbusWrite>(path, min, max, connectionName);
109}
110
James Feist7136a5a2018-07-19 09:52:05 -0700111void DbusWrite::write(double value)
112{
James Feistcd9e1092018-10-08 13:06:41 -0700113 if (oldValue == static_cast<int64_t>(value))
114 {
115 return;
116 }
James Feist80b5d9a2019-01-15 12:51:09 -0800117 auto writeBus = sdbusplus::bus::new_default();
James Feist7136a5a2018-07-19 09:52:05 -0700118 auto mesg =
James Feist80b5d9a2019-01-15 12:51:09 -0800119 writeBus.new_method_call(connectionName.c_str(), path.c_str(),
120 "org.freedesktop.DBus.Properties", "Set");
Andrew Geissler77ac8562020-05-20 21:09:23 -0500121 mesg.append(pwmInterface, "Target",
122 std::variant<uint64_t>(static_cast<uint64_t>(value)));
Patrick Venture76ce5d72018-10-30 18:39:50 -0700123
124 try
James Feist7136a5a2018-07-19 09:52:05 -0700125 {
Patrick Venture76ce5d72018-10-30 18:39:50 -0700126 // TODO: consider call_noreplly
James Feist80b5d9a2019-01-15 12:51:09 -0800127 auto resp = writeBus.call(mesg);
James Feist7136a5a2018-07-19 09:52:05 -0700128 }
Patrick Venture76ce5d72018-10-30 18:39:50 -0700129 catch (const sdbusplus::exception::SdBusError& ex)
130 {
131 log<level::ERR>("Dbus Call Failure", entry("PATH=%s", path.c_str()),
132 entry("WHAT=%s", ex.what()));
133 }
134
James Feistcd9e1092018-10-08 13:06:41 -0700135 oldValue = static_cast<int64_t>(value);
James Feist7136a5a2018-07-19 09:52:05 -0700136 return;
137}
Patrick Venturea0764872020-08-08 07:48:43 -0700138
139} // namespace pid_control