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