James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 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 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 17 | #include "dbus/dbuswrite.hpp" |
| 18 | |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 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; |
James Feist | cd9e109 | 2018-10-08 13:06:41 -0700 | [diff] [blame] | 44 | |
| 45 | if (oldValue == static_cast<int64_t>(ovalue)) |
| 46 | { |
| 47 | return; |
| 48 | } |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 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 | } |
James Feist | cd9e109 | 2018-10-08 13:06:41 -0700 | [diff] [blame] | 60 | oldValue = static_cast<int64_t>(ovalue); |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 61 | return; |
| 62 | } |
| 63 | |
| 64 | void DbusWrite::write(double value) |
| 65 | { |
James Feist | cd9e109 | 2018-10-08 13:06:41 -0700 | [diff] [blame] | 66 | if (oldValue == static_cast<int64_t>(value)) |
| 67 | { |
| 68 | return; |
| 69 | } |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 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 | } |
James Feist | cd9e109 | 2018-10-08 13:06:41 -0700 | [diff] [blame] | 81 | oldValue = static_cast<int64_t>(value); |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 82 | return; |
| 83 | } |