blob: 0b02872d48922515f99df77b4b84a2b191c5d384 [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 Ventureda4a5dd2018-08-31 09:42:48 -070017#include "dbus/dbuswrite.hpp"
18
Patrick Venture76ce5d72018-10-30 18:39:50 -070019#include <phosphor-logging/log.hpp>
James Feist7136a5a2018-07-19 09:52:05 -070020#include <sdbusplus/bus.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070021
22#include <iostream>
23#include <memory>
Patrick Venturef5e770b2018-10-30 12:28:53 -070024#include <string>
James Feist1f802f52019-02-08 13:51:43 -080025#include <variant>
Patrick Venturef5e770b2018-10-30 12:28:53 -070026
Patrick Venturea0764872020-08-08 07:48:43 -070027namespace pid_control
28{
29
Patrick Venturef5e770b2018-10-30 12:28:53 -070030constexpr const char* pwmInterface = "xyz.openbmc_project.Control.FanPwm";
James Feist7136a5a2018-07-19 09:52:05 -070031
Patrick Venture76ce5d72018-10-30 18:39:50 -070032using namespace phosphor::logging;
33
Patrick Venturef5e770b2018-10-30 12:28:53 -070034std::unique_ptr<WriteInterface>
35 DbusWritePercent::createDbusWrite(const std::string& path, int64_t min,
36 int64_t max, DbusHelperInterface& helper)
37{
James Feist9fa90c12019-01-11 15:35:22 -080038 auto tempBus = sdbusplus::bus::new_system();
Patrick Venturef5e770b2018-10-30 12:28:53 -070039 std::string connectionName;
40
41 try
42 {
43 connectionName = helper.getService(tempBus, pwmInterface, path);
44 }
45 catch (const std::exception& e)
46 {
47 return nullptr;
48 }
49
50 return std::make_unique<DbusWritePercent>(path, min, max, connectionName);
51}
52
James Feist7136a5a2018-07-19 09:52:05 -070053void DbusWritePercent::write(double value)
54{
Patrick Venture5f59c0f2018-11-11 12:55:14 -080055 double minimum = getMin();
56 double maximum = getMax();
James Feist7136a5a2018-07-19 09:52:05 -070057
Patrick Venture5f59c0f2018-11-11 12:55:14 -080058 double range = maximum - minimum;
59 double offset = range * value;
60 double ovalue = offset + minimum;
James Feistcd9e1092018-10-08 13:06:41 -070061
62 if (oldValue == static_cast<int64_t>(ovalue))
63 {
64 return;
65 }
James Feist80b5d9a2019-01-15 12:51:09 -080066 auto writeBus = sdbusplus::bus::new_default();
James Feist7136a5a2018-07-19 09:52:05 -070067 auto mesg =
James Feist80b5d9a2019-01-15 12:51:09 -080068 writeBus.new_method_call(connectionName.c_str(), path.c_str(),
69 "org.freedesktop.DBus.Properties", "Set");
Andrew Geissler77ac8562020-05-20 21:09:23 -050070 mesg.append(pwmInterface, "Target",
71 std::variant<uint64_t>(static_cast<uint64_t>(ovalue)));
Patrick Venture76ce5d72018-10-30 18:39:50 -070072
73 try
James Feist7136a5a2018-07-19 09:52:05 -070074 {
Patrick Venture76ce5d72018-10-30 18:39:50 -070075 // TODO: if we don't use the reply, call_noreply()
James Feist80b5d9a2019-01-15 12:51:09 -080076 auto resp = writeBus.call(mesg);
James Feist7136a5a2018-07-19 09:52:05 -070077 }
Patrick Venture76ce5d72018-10-30 18:39:50 -070078 catch (const sdbusplus::exception::SdBusError& ex)
79 {
80 log<level::ERR>("Dbus Call Failure", entry("PATH=%s", path.c_str()),
81 entry("WHAT=%s", ex.what()));
82 }
83
James Feistcd9e1092018-10-08 13:06:41 -070084 oldValue = static_cast<int64_t>(ovalue);
James Feist7136a5a2018-07-19 09:52:05 -070085 return;
86}
87
Patrick Venturef5e770b2018-10-30 12:28:53 -070088std::unique_ptr<WriteInterface>
89 DbusWrite::createDbusWrite(const std::string& path, int64_t min,
90 int64_t max, DbusHelperInterface& helper)
91{
James Feist9fa90c12019-01-11 15:35:22 -080092 auto tempBus = sdbusplus::bus::new_system();
Patrick Venturef5e770b2018-10-30 12:28:53 -070093 std::string connectionName;
94
95 try
96 {
97 connectionName = helper.getService(tempBus, pwmInterface, path);
98 }
99 catch (const std::exception& e)
100 {
101 return nullptr;
102 }
103
104 return std::make_unique<DbusWrite>(path, min, max, connectionName);
105}
106
James Feist7136a5a2018-07-19 09:52:05 -0700107void DbusWrite::write(double value)
108{
James Feistcd9e1092018-10-08 13:06:41 -0700109 if (oldValue == static_cast<int64_t>(value))
110 {
111 return;
112 }
James Feist80b5d9a2019-01-15 12:51:09 -0800113 auto writeBus = sdbusplus::bus::new_default();
James Feist7136a5a2018-07-19 09:52:05 -0700114 auto mesg =
James Feist80b5d9a2019-01-15 12:51:09 -0800115 writeBus.new_method_call(connectionName.c_str(), path.c_str(),
116 "org.freedesktop.DBus.Properties", "Set");
Andrew Geissler77ac8562020-05-20 21:09:23 -0500117 mesg.append(pwmInterface, "Target",
118 std::variant<uint64_t>(static_cast<uint64_t>(value)));
Patrick Venture76ce5d72018-10-30 18:39:50 -0700119
120 try
James Feist7136a5a2018-07-19 09:52:05 -0700121 {
Patrick Venture76ce5d72018-10-30 18:39:50 -0700122 // TODO: consider call_noreplly
James Feist80b5d9a2019-01-15 12:51:09 -0800123 auto resp = writeBus.call(mesg);
James Feist7136a5a2018-07-19 09:52:05 -0700124 }
Patrick Venture76ce5d72018-10-30 18:39:50 -0700125 catch (const sdbusplus::exception::SdBusError& ex)
126 {
127 log<level::ERR>("Dbus Call Failure", entry("PATH=%s", path.c_str()),
128 entry("WHAT=%s", ex.what()));
129 }
130
James Feistcd9e1092018-10-08 13:06:41 -0700131 oldValue = static_cast<int64_t>(value);
James Feist7136a5a2018-07-19 09:52:05 -0700132 return;
133}
Patrick Venturea0764872020-08-08 07:48:43 -0700134
135} // namespace pid_control