blob: bac21874336141f5468c57f440e9944b3f334c5f [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
James Feist7136a5a2018-07-19 09:52:05 -070019#include <iostream>
Patrick Venturef5e770b2018-10-30 12:28:53 -070020#include <memory>
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 Venturef5e770b2018-10-30 12:28:53 -070023#include <string>
24
25constexpr const char* pwmInterface = "xyz.openbmc_project.Control.FanPwm";
James Feist7136a5a2018-07-19 09:52:05 -070026
Patrick Venture76ce5d72018-10-30 18:39:50 -070027using namespace phosphor::logging;
28
Patrick Venturef5e770b2018-10-30 12:28:53 -070029std::unique_ptr<WriteInterface>
30 DbusWritePercent::createDbusWrite(const std::string& path, int64_t min,
31 int64_t max, DbusHelperInterface& helper)
32{
James Feist9fa90c12019-01-11 15:35:22 -080033 auto tempBus = sdbusplus::bus::new_system();
Patrick Venturef5e770b2018-10-30 12:28:53 -070034 std::string connectionName;
35
36 try
37 {
38 connectionName = helper.getService(tempBus, pwmInterface, path);
39 }
40 catch (const std::exception& e)
41 {
42 return nullptr;
43 }
44
45 return std::make_unique<DbusWritePercent>(path, min, max, connectionName);
46}
47
James Feist7136a5a2018-07-19 09:52:05 -070048void DbusWritePercent::write(double value)
49{
Patrick Venture5f59c0f2018-11-11 12:55:14 -080050 double minimum = getMin();
51 double maximum = getMax();
James Feist7136a5a2018-07-19 09:52:05 -070052
Patrick Venture5f59c0f2018-11-11 12:55:14 -080053 double range = maximum - minimum;
54 double offset = range * value;
55 double ovalue = offset + minimum;
James Feistcd9e1092018-10-08 13:06:41 -070056
57 if (oldValue == static_cast<int64_t>(ovalue))
58 {
59 return;
60 }
James Feist80b5d9a2019-01-15 12:51:09 -080061 auto writeBus = sdbusplus::bus::new_default();
James Feist7136a5a2018-07-19 09:52:05 -070062 auto mesg =
James Feist80b5d9a2019-01-15 12:51:09 -080063 writeBus.new_method_call(connectionName.c_str(), path.c_str(),
64 "org.freedesktop.DBus.Properties", "Set");
James Feist7136a5a2018-07-19 09:52:05 -070065 mesg.append(pwmInterface, "Target",
66 sdbusplus::message::variant<uint64_t>(ovalue));
Patrick Venture76ce5d72018-10-30 18:39:50 -070067
68 try
James Feist7136a5a2018-07-19 09:52:05 -070069 {
Patrick Venture76ce5d72018-10-30 18:39:50 -070070 // TODO: if we don't use the reply, call_noreply()
James Feist80b5d9a2019-01-15 12:51:09 -080071 auto resp = writeBus.call(mesg);
James Feist7136a5a2018-07-19 09:52:05 -070072 }
Patrick Venture76ce5d72018-10-30 18:39:50 -070073 catch (const sdbusplus::exception::SdBusError& ex)
74 {
75 log<level::ERR>("Dbus Call Failure", entry("PATH=%s", path.c_str()),
76 entry("WHAT=%s", ex.what()));
77 }
78
James Feistcd9e1092018-10-08 13:06:41 -070079 oldValue = static_cast<int64_t>(ovalue);
James Feist7136a5a2018-07-19 09:52:05 -070080 return;
81}
82
Patrick Venturef5e770b2018-10-30 12:28:53 -070083std::unique_ptr<WriteInterface>
84 DbusWrite::createDbusWrite(const std::string& path, int64_t min,
85 int64_t max, DbusHelperInterface& helper)
86{
James Feist9fa90c12019-01-11 15:35:22 -080087 auto tempBus = sdbusplus::bus::new_system();
Patrick Venturef5e770b2018-10-30 12:28:53 -070088 std::string connectionName;
89
90 try
91 {
92 connectionName = helper.getService(tempBus, pwmInterface, path);
93 }
94 catch (const std::exception& e)
95 {
96 return nullptr;
97 }
98
99 return std::make_unique<DbusWrite>(path, min, max, connectionName);
100}
101
James Feist7136a5a2018-07-19 09:52:05 -0700102void DbusWrite::write(double value)
103{
James Feistcd9e1092018-10-08 13:06:41 -0700104 if (oldValue == static_cast<int64_t>(value))
105 {
106 return;
107 }
James Feist80b5d9a2019-01-15 12:51:09 -0800108 auto writeBus = sdbusplus::bus::new_default();
James Feist7136a5a2018-07-19 09:52:05 -0700109 auto mesg =
James Feist80b5d9a2019-01-15 12:51:09 -0800110 writeBus.new_method_call(connectionName.c_str(), path.c_str(),
111 "org.freedesktop.DBus.Properties", "Set");
James Feist7136a5a2018-07-19 09:52:05 -0700112 mesg.append(pwmInterface, "Target",
113 sdbusplus::message::variant<uint64_t>(value));
Patrick Venture76ce5d72018-10-30 18:39:50 -0700114
115 try
James Feist7136a5a2018-07-19 09:52:05 -0700116 {
Patrick Venture76ce5d72018-10-30 18:39:50 -0700117 // TODO: consider call_noreplly
James Feist80b5d9a2019-01-15 12:51:09 -0800118 auto resp = writeBus.call(mesg);
James Feist7136a5a2018-07-19 09:52:05 -0700119 }
Patrick Venture76ce5d72018-10-30 18:39:50 -0700120 catch (const sdbusplus::exception::SdBusError& ex)
121 {
122 log<level::ERR>("Dbus Call Failure", entry("PATH=%s", path.c_str()),
123 entry("WHAT=%s", ex.what()));
124 }
125
James Feistcd9e1092018-10-08 13:06:41 -0700126 oldValue = static_cast<int64_t>(value);
James Feist7136a5a2018-07-19 09:52:05 -0700127 return;
128}