blob: 714580b0dac31d6d3131d9d3043d85d2ff37e1aa [file] [log] [blame]
James Feist7136a5a2018-07-19 09:52:05 -07001/*
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 Ventureda4a5dd2018-08-31 09:42:48 -070017#include "dbus/dbuswrite.hpp"
18
James Feist7136a5a2018-07-19 09:52:05 -070019#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
25std::unique_ptr<sdbusplus::bus::bus> writeBus = nullptr;
26
27void initBus()
28{
29 if (writeBus == nullptr)
30 {
31 writeBus = std::make_unique<sdbusplus::bus::bus>(
32 sdbusplus::bus::new_default());
33 }
34}
35
36void 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 Feistcd9e1092018-10-08 13:06:41 -070044
45 if (oldValue == static_cast<int64_t>(ovalue))
46 {
47 return;
48 }
James Feist7136a5a2018-07-19 09:52:05 -070049 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 Feistcd9e1092018-10-08 13:06:41 -070060 oldValue = static_cast<int64_t>(ovalue);
James Feist7136a5a2018-07-19 09:52:05 -070061 return;
62}
63
64void DbusWrite::write(double value)
65{
James Feistcd9e1092018-10-08 13:06:41 -070066 if (oldValue == static_cast<int64_t>(value))
67 {
68 return;
69 }
James Feist7136a5a2018-07-19 09:52:05 -070070 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 Feistcd9e1092018-10-08 13:06:41 -070081 oldValue = static_cast<int64_t>(value);
James Feist7136a5a2018-07-19 09:52:05 -070082 return;
83}