blob: 502b9887025f874e63f7b3e3ef42af0e78014641 [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>
James Feist1f802f52019-02-08 13:51:43 -080024#include <variant>
Patrick Venturef5e770b2018-10-30 12:28:53 -070025
26constexpr const char* pwmInterface = "xyz.openbmc_project.Control.FanPwm";
James Feist7136a5a2018-07-19 09:52:05 -070027
Patrick Venture76ce5d72018-10-30 18:39:50 -070028using namespace phosphor::logging;
29
Patrick Venturef5e770b2018-10-30 12:28:53 -070030std::unique_ptr<WriteInterface>
31 DbusWritePercent::createDbusWrite(const std::string& path, int64_t min,
32 int64_t max, DbusHelperInterface& helper)
33{
James Feist9fa90c12019-01-11 15:35:22 -080034 auto tempBus = sdbusplus::bus::new_system();
Patrick Venturef5e770b2018-10-30 12:28:53 -070035 std::string connectionName;
36
37 try
38 {
39 connectionName = helper.getService(tempBus, pwmInterface, path);
40 }
41 catch (const std::exception& e)
42 {
43 return nullptr;
44 }
45
46 return std::make_unique<DbusWritePercent>(path, min, max, connectionName);
47}
48
James Feist7136a5a2018-07-19 09:52:05 -070049void DbusWritePercent::write(double value)
50{
Patrick Venture5f59c0f2018-11-11 12:55:14 -080051 double minimum = getMin();
52 double maximum = getMax();
James Feist7136a5a2018-07-19 09:52:05 -070053
Patrick Venture5f59c0f2018-11-11 12:55:14 -080054 double range = maximum - minimum;
55 double offset = range * value;
56 double ovalue = offset + minimum;
James Feistcd9e1092018-10-08 13:06:41 -070057
58 if (oldValue == static_cast<int64_t>(ovalue))
59 {
60 return;
61 }
James Feist80b5d9a2019-01-15 12:51:09 -080062 auto writeBus = sdbusplus::bus::new_default();
James Feist7136a5a2018-07-19 09:52:05 -070063 auto mesg =
James Feist80b5d9a2019-01-15 12:51:09 -080064 writeBus.new_method_call(connectionName.c_str(), path.c_str(),
65 "org.freedesktop.DBus.Properties", "Set");
James Feist1f802f52019-02-08 13:51:43 -080066 mesg.append(pwmInterface, "Target", std::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 Feist1f802f52019-02-08 13:51:43 -0800112 mesg.append(pwmInterface, "Target", std::variant<uint64_t>(value));
Patrick Venture76ce5d72018-10-30 18:39:50 -0700113
114 try
James Feist7136a5a2018-07-19 09:52:05 -0700115 {
Patrick Venture76ce5d72018-10-30 18:39:50 -0700116 // TODO: consider call_noreplly
James Feist80b5d9a2019-01-15 12:51:09 -0800117 auto resp = writeBus.call(mesg);
James Feist7136a5a2018-07-19 09:52:05 -0700118 }
Patrick Venture76ce5d72018-10-30 18:39:50 -0700119 catch (const sdbusplus::exception::SdBusError& ex)
120 {
121 log<level::ERR>("Dbus Call Failure", entry("PATH=%s", path.c_str()),
122 entry("WHAT=%s", ex.what()));
123 }
124
James Feistcd9e1092018-10-08 13:06:41 -0700125 oldValue = static_cast<int64_t>(value);
James Feist7136a5a2018-07-19 09:52:05 -0700126 return;
127}