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