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 <sdbusplus/bus.hpp> 22 #include <string> 23 24 constexpr const char* pwmInterface = "xyz.openbmc_project.Control.FanPwm"; 25 26 // this bus object is treated as a singleton because the class is constructed in 27 // a different thread than it is used, and as bus objects are relatively 28 // expensive we'd prefer to only have one 29 std::unique_ptr<sdbusplus::bus::bus> writeBus = nullptr; 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_default(); 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 initBus() 51 { 52 if (writeBus == nullptr) 53 { 54 writeBus = std::make_unique<sdbusplus::bus::bus>( 55 sdbusplus::bus::new_default()); 56 } 57 } 58 59 void DbusWritePercent::write(double value) 60 { 61 float minimum = getMin(); 62 float maximum = getMax(); 63 64 float range = maximum - minimum; 65 float offset = range * value; 66 float ovalue = offset + minimum; 67 68 if (oldValue == static_cast<int64_t>(ovalue)) 69 { 70 return; 71 } 72 initBus(); 73 auto mesg = 74 writeBus->new_method_call(connectionName.c_str(), path.c_str(), 75 "org.freedesktop.DBus.Properties", "Set"); 76 mesg.append(pwmInterface, "Target", 77 sdbusplus::message::variant<uint64_t>(ovalue)); 78 auto resp = writeBus->call(mesg); 79 if (resp.is_method_error()) 80 { 81 std::cerr << "Error sending message to " << path << "\n"; 82 } 83 oldValue = static_cast<int64_t>(ovalue); 84 return; 85 } 86 87 std::unique_ptr<WriteInterface> 88 DbusWrite::createDbusWrite(const std::string& path, int64_t min, 89 int64_t max, DbusHelperInterface& helper) 90 { 91 auto tempBus = sdbusplus::bus::new_default(); 92 std::string connectionName; 93 94 try 95 { 96 connectionName = helper.getService(tempBus, pwmInterface, path); 97 } 98 catch (const std::exception& e) 99 { 100 return nullptr; 101 } 102 103 return std::make_unique<DbusWrite>(path, min, max, connectionName); 104 } 105 106 void DbusWrite::write(double value) 107 { 108 if (oldValue == static_cast<int64_t>(value)) 109 { 110 return; 111 } 112 initBus(); 113 auto mesg = 114 writeBus->new_method_call(connectionName.c_str(), path.c_str(), 115 "org.freedesktop.DBus.Properties", "Set"); 116 mesg.append(pwmInterface, "Target", 117 sdbusplus::message::variant<uint64_t>(value)); 118 auto resp = writeBus->call(mesg); 119 if (resp.is_method_error()) 120 { 121 std::cerr << "Error sending message to " << path << "\n"; 122 } 123 oldValue = static_cast<int64_t>(value); 124 return; 125 } 126