xref: /openbmc/phosphor-pid-control/dbus/dbuswrite.cpp (revision a076487aab0ee71ea32d53f798d5ca6b31677278)
1 /*
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 
17 #include "dbus/dbuswrite.hpp"
18 
19 #include <phosphor-logging/log.hpp>
20 #include <sdbusplus/bus.hpp>
21 
22 #include <iostream>
23 #include <memory>
24 #include <string>
25 #include <variant>
26 
27 namespace pid_control
28 {
29 
30 constexpr const char* pwmInterface = "xyz.openbmc_project.Control.FanPwm";
31 
32 using namespace phosphor::logging;
33 
34 std::unique_ptr<WriteInterface>
35     DbusWritePercent::createDbusWrite(const std::string& path, int64_t min,
36                                       int64_t max, DbusHelperInterface& helper)
37 {
38     auto tempBus = sdbusplus::bus::new_system();
39     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 
53 void DbusWritePercent::write(double value)
54 {
55     double minimum = getMin();
56     double maximum = getMax();
57 
58     double range = maximum - minimum;
59     double offset = range * value;
60     double ovalue = offset + minimum;
61 
62     if (oldValue == static_cast<int64_t>(ovalue))
63     {
64         return;
65     }
66     auto writeBus = sdbusplus::bus::new_default();
67     auto mesg =
68         writeBus.new_method_call(connectionName.c_str(), path.c_str(),
69                                  "org.freedesktop.DBus.Properties", "Set");
70     mesg.append(pwmInterface, "Target",
71                 std::variant<uint64_t>(static_cast<uint64_t>(ovalue)));
72 
73     try
74     {
75         // TODO: if we don't use the reply, call_noreply()
76         auto resp = writeBus.call(mesg);
77     }
78     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 
84     oldValue = static_cast<int64_t>(ovalue);
85     return;
86 }
87 
88 std::unique_ptr<WriteInterface>
89     DbusWrite::createDbusWrite(const std::string& path, int64_t min,
90                                int64_t max, DbusHelperInterface& helper)
91 {
92     auto tempBus = sdbusplus::bus::new_system();
93     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 
107 void DbusWrite::write(double value)
108 {
109     if (oldValue == static_cast<int64_t>(value))
110     {
111         return;
112     }
113     auto writeBus = sdbusplus::bus::new_default();
114     auto mesg =
115         writeBus.new_method_call(connectionName.c_str(), path.c_str(),
116                                  "org.freedesktop.DBus.Properties", "Set");
117     mesg.append(pwmInterface, "Target",
118                 std::variant<uint64_t>(static_cast<uint64_t>(value)));
119 
120     try
121     {
122         // TODO: consider call_noreplly
123         auto resp = writeBus.call(mesg);
124     }
125     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 
131     oldValue = static_cast<int64_t>(value);
132     return;
133 }
134 
135 } // namespace pid_control
136