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 <sdbusplus/bus.hpp> 21 22 // this bus object is treated as a singleton because the class is constructed in 23 // a different thread than it is used, and as bus objects are relatively 24 // expensive we'd prefer to only have one 25 std::unique_ptr<sdbusplus::bus::bus> writeBus = nullptr; 26 27 void initBus() 28 { 29 if (writeBus == nullptr) 30 { 31 writeBus = std::make_unique<sdbusplus::bus::bus>( 32 sdbusplus::bus::new_default()); 33 } 34 } 35 36 void DbusWritePercent::write(double value) 37 { 38 float minimum = getMin(); 39 float maximum = getMax(); 40 41 float range = maximum - minimum; 42 float offset = range * value; 43 float ovalue = offset + minimum; 44 45 if (oldValue == static_cast<int64_t>(ovalue)) 46 { 47 return; 48 } 49 initBus(); 50 auto mesg = 51 writeBus->new_method_call(connectionName.c_str(), path.c_str(), 52 "org.freedesktop.DBus.Properties", "Set"); 53 mesg.append(pwmInterface, "Target", 54 sdbusplus::message::variant<uint64_t>(ovalue)); 55 auto resp = writeBus->call(mesg); 56 if (resp.is_method_error()) 57 { 58 std::cerr << "Error sending message to " << path << "\n"; 59 } 60 oldValue = static_cast<int64_t>(ovalue); 61 return; 62 } 63 64 void DbusWrite::write(double value) 65 { 66 if (oldValue == static_cast<int64_t>(value)) 67 { 68 return; 69 } 70 initBus(); 71 auto mesg = 72 writeBus->new_method_call(connectionName.c_str(), path.c_str(), 73 "org.freedesktop.DBus.Properties", "Set"); 74 mesg.append(pwmInterface, "Target", 75 sdbusplus::message::variant<uint64_t>(value)); 76 auto resp = writeBus->call(mesg); 77 if (resp.is_method_error()) 78 { 79 std::cerr << "Error sending message to " << path << "\n"; 80 } 81 oldValue = static_cast<int64_t>(value); 82 return; 83 } 84