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