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 <exception>
25 #include <iostream>
26 #include <memory>
27 #include <string>
28 #include <variant>
29 
30 namespace pid_control
31 {
32 
33 constexpr const char* pwmInterface = "xyz.openbmc_project.Control.FanPwm";
34 
35 using namespace phosphor::logging;
36 
37 std::unique_ptr<WriteInterface> DbusWritePercent::createDbusWrite(
38     const std::string& path, int64_t min, int64_t max,
39     std::unique_ptr<DbusHelperInterface> helper)
40 {
41     std::string connectionName;
42 
43     try
44     {
45         connectionName = helper->getService(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     return write(value, false, nullptr);
58 }
59 
60 void DbusWritePercent::write(double value, bool force, int64_t* written)
61 {
62     double minimum = getMin();
63     double maximum = getMax();
64 
65     double range = maximum - minimum;
66     double offset = range * value;
67     double ovalue = offset + minimum;
68 
69     if (oldValue == static_cast<int64_t>(ovalue))
70     {
71         if (!force)
72         {
73             if (written)
74             {
75                 *written = oldValue;
76             }
77             return;
78         }
79     }
80     auto writeBus = sdbusplus::bus::new_default();
81     auto mesg =
82         writeBus.new_method_call(connectionName.c_str(), path.c_str(),
83                                  "org.freedesktop.DBus.Properties", "Set");
84     mesg.append(pwmInterface, "Target",
85                 std::variant<uint64_t>(static_cast<uint64_t>(ovalue)));
86 
87     try
88     {
89         // TODO: if we don't use the reply, call_noreply()
90         auto resp = writeBus.call(mesg);
91     }
92     catch (const sdbusplus::exception::exception& ex)
93     {
94         log<level::ERR>("Dbus Call Failure", entry("PATH=%s", path.c_str()),
95                         entry("WHAT=%s", ex.what()));
96     }
97 
98     oldValue = static_cast<int64_t>(ovalue);
99     if (written)
100     {
101         *written = oldValue;
102     }
103     return;
104 }
105 
106 std::unique_ptr<WriteInterface>
107     DbusWrite::createDbusWrite(const std::string& path, int64_t min,
108                                int64_t max,
109                                std::unique_ptr<DbusHelperInterface> helper)
110 {
111     std::string connectionName;
112 
113     try
114     {
115         connectionName = helper->getService(pwmInterface, path);
116     }
117     catch (const std::exception& e)
118     {
119         return nullptr;
120     }
121 
122     return std::make_unique<DbusWrite>(path, min, max, connectionName);
123 }
124 
125 void DbusWrite::write(double value)
126 {
127     return write(value, false, nullptr);
128 }
129 
130 void DbusWrite::write(double value, bool force, int64_t* written)
131 {
132     if (oldValue == static_cast<int64_t>(value))
133     {
134         if (!force)
135         {
136             if (written)
137             {
138                 *written = oldValue;
139             }
140             return;
141         }
142     }
143     auto writeBus = sdbusplus::bus::new_default();
144     auto mesg =
145         writeBus.new_method_call(connectionName.c_str(), path.c_str(),
146                                  "org.freedesktop.DBus.Properties", "Set");
147     mesg.append(pwmInterface, "Target",
148                 std::variant<uint64_t>(static_cast<uint64_t>(value)));
149 
150     try
151     {
152         // TODO: consider call_noreplly
153         auto resp = writeBus.call(mesg);
154     }
155     catch (const sdbusplus::exception::exception& ex)
156     {
157         log<level::ERR>("Dbus Call Failure", entry("PATH=%s", path.c_str()),
158                         entry("WHAT=%s", ex.what()));
159     }
160 
161     oldValue = static_cast<int64_t>(value);
162     if (written)
163     {
164         *written = oldValue;
165     }
166     return;
167 }
168 
169 } // namespace pid_control
170