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 "dbuswrite.hpp"
18 
19 #include "dbushelper_interface.hpp"
20 
21 #include <phosphor-logging/log.hpp>
22 #include <sdbusplus/bus.hpp>
23 
24 #include <iostream>
25 #include <memory>
26 #include <string>
27 #include <variant>
28 
29 namespace pid_control
30 {
31 
32 constexpr const char* pwmInterface = "xyz.openbmc_project.Control.FanPwm";
33 
34 using namespace phosphor::logging;
35 
36 std::unique_ptr<WriteInterface>
37     DbusWritePercent::createDbusWrite(const std::string& path, int64_t min,
38                                       int64_t max, DbusHelperInterface& helper)
39 {
40     auto tempBus = sdbusplus::bus::new_system();
41     std::string connectionName;
42 
43     try
44     {
45         connectionName = helper.getService(tempBus, pwmInterface, path);
46     }
47     catch (const std::exception& e)
48     {
49         return nullptr;
50     }
51 
52     return std::make_unique<DbusWritePercent>(path, min, max, connectionName);
53 }
54 
55 void DbusWritePercent::write(double value)
56 {
57     double minimum = getMin();
58     double maximum = getMax();
59 
60     double range = maximum - minimum;
61     double offset = range * value;
62     double ovalue = offset + minimum;
63 
64     if (oldValue == static_cast<int64_t>(ovalue))
65     {
66         return;
67     }
68     auto writeBus = sdbusplus::bus::new_default();
69     auto mesg =
70         writeBus.new_method_call(connectionName.c_str(), path.c_str(),
71                                  "org.freedesktop.DBus.Properties", "Set");
72     mesg.append(pwmInterface, "Target",
73                 std::variant<uint64_t>(static_cast<uint64_t>(ovalue)));
74 
75     try
76     {
77         // TODO: if we don't use the reply, call_noreply()
78         auto resp = writeBus.call(mesg);
79     }
80     catch (const sdbusplus::exception::SdBusError& ex)
81     {
82         log<level::ERR>("Dbus Call Failure", entry("PATH=%s", path.c_str()),
83                         entry("WHAT=%s", ex.what()));
84     }
85 
86     oldValue = static_cast<int64_t>(ovalue);
87     return;
88 }
89 
90 std::unique_ptr<WriteInterface>
91     DbusWrite::createDbusWrite(const std::string& path, int64_t min,
92                                int64_t max, DbusHelperInterface& helper)
93 {
94     auto tempBus = sdbusplus::bus::new_system();
95     std::string connectionName;
96 
97     try
98     {
99         connectionName = helper.getService(tempBus, pwmInterface, path);
100     }
101     catch (const std::exception& e)
102     {
103         return nullptr;
104     }
105 
106     return std::make_unique<DbusWrite>(path, min, max, connectionName);
107 }
108 
109 void DbusWrite::write(double value)
110 {
111     if (oldValue == static_cast<int64_t>(value))
112     {
113         return;
114     }
115     auto writeBus = sdbusplus::bus::new_default();
116     auto mesg =
117         writeBus.new_method_call(connectionName.c_str(), path.c_str(),
118                                  "org.freedesktop.DBus.Properties", "Set");
119     mesg.append(pwmInterface, "Target",
120                 std::variant<uint64_t>(static_cast<uint64_t>(value)));
121 
122     try
123     {
124         // TODO: consider call_noreplly
125         auto resp = writeBus.call(mesg);
126     }
127     catch (const sdbusplus::exception::SdBusError& ex)
128     {
129         log<level::ERR>("Dbus Call Failure", entry("PATH=%s", path.c_str()),
130                         entry("WHAT=%s", ex.what()));
131     }
132 
133     oldValue = static_cast<int64_t>(value);
134     return;
135 }
136 
137 } // namespace pid_control
138