blob: b691b66e6d9fae0f9fa61bb651fc015cf9b8c1bd [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>
James Feist7136a5a2018-07-19 09:52:05 -070021#include <sdbusplus/bus.hpp>
Patrick Venturef5e770b2018-10-30 12:28:53 -070022#include <string>
23
24constexpr const char* pwmInterface = "xyz.openbmc_project.Control.FanPwm";
James Feist7136a5a2018-07-19 09:52:05 -070025
26// this bus object is treated as a singleton because the class is constructed in
27// a different thread than it is used, and as bus objects are relatively
28// expensive we'd prefer to only have one
29std::unique_ptr<sdbusplus::bus::bus> writeBus = nullptr;
30
Patrick Venturef5e770b2018-10-30 12:28:53 -070031std::unique_ptr<WriteInterface>
32 DbusWritePercent::createDbusWrite(const std::string& path, int64_t min,
33 int64_t max, DbusHelperInterface& helper)
34{
35 auto tempBus = sdbusplus::bus::new_default();
36 std::string connectionName;
37
38 try
39 {
40 connectionName = helper.getService(tempBus, pwmInterface, path);
41 }
42 catch (const std::exception& e)
43 {
44 return nullptr;
45 }
46
47 return std::make_unique<DbusWritePercent>(path, min, max, connectionName);
48}
49
James Feist7136a5a2018-07-19 09:52:05 -070050void initBus()
51{
52 if (writeBus == nullptr)
53 {
54 writeBus = std::make_unique<sdbusplus::bus::bus>(
55 sdbusplus::bus::new_default());
56 }
57}
58
59void DbusWritePercent::write(double value)
60{
61 float minimum = getMin();
62 float maximum = getMax();
63
64 float range = maximum - minimum;
65 float offset = range * value;
66 float ovalue = offset + minimum;
James Feistcd9e1092018-10-08 13:06:41 -070067
68 if (oldValue == static_cast<int64_t>(ovalue))
69 {
70 return;
71 }
James Feist7136a5a2018-07-19 09:52:05 -070072 initBus();
73 auto mesg =
74 writeBus->new_method_call(connectionName.c_str(), path.c_str(),
75 "org.freedesktop.DBus.Properties", "Set");
76 mesg.append(pwmInterface, "Target",
77 sdbusplus::message::variant<uint64_t>(ovalue));
78 auto resp = writeBus->call(mesg);
79 if (resp.is_method_error())
80 {
81 std::cerr << "Error sending message to " << path << "\n";
82 }
James Feistcd9e1092018-10-08 13:06:41 -070083 oldValue = static_cast<int64_t>(ovalue);
James Feist7136a5a2018-07-19 09:52:05 -070084 return;
85}
86
Patrick Venturef5e770b2018-10-30 12:28:53 -070087std::unique_ptr<WriteInterface>
88 DbusWrite::createDbusWrite(const std::string& path, int64_t min,
89 int64_t max, DbusHelperInterface& helper)
90{
91 auto tempBus = sdbusplus::bus::new_default();
92 std::string connectionName;
93
94 try
95 {
96 connectionName = helper.getService(tempBus, pwmInterface, path);
97 }
98 catch (const std::exception& e)
99 {
100 return nullptr;
101 }
102
103 return std::make_unique<DbusWrite>(path, min, max, connectionName);
104}
105
James Feist7136a5a2018-07-19 09:52:05 -0700106void DbusWrite::write(double value)
107{
James Feistcd9e1092018-10-08 13:06:41 -0700108 if (oldValue == static_cast<int64_t>(value))
109 {
110 return;
111 }
James Feist7136a5a2018-07-19 09:52:05 -0700112 initBus();
113 auto mesg =
114 writeBus->new_method_call(connectionName.c_str(), path.c_str(),
115 "org.freedesktop.DBus.Properties", "Set");
116 mesg.append(pwmInterface, "Target",
117 sdbusplus::message::variant<uint64_t>(value));
118 auto resp = writeBus->call(mesg);
119 if (resp.is_method_error())
120 {
121 std::cerr << "Error sending message to " << path << "\n";
122 }
James Feistcd9e1092018-10-08 13:06:41 -0700123 oldValue = static_cast<int64_t>(value);
James Feist7136a5a2018-07-19 09:52:05 -0700124 return;
125}