blob: b26b96e2c8f14da367a29e5afdb1f499886d4a61 [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;
44 initBus();
45 auto mesg =
46 writeBus->new_method_call(connectionName.c_str(), path.c_str(),
47 "org.freedesktop.DBus.Properties", "Set");
48 mesg.append(pwmInterface, "Target",
49 sdbusplus::message::variant<uint64_t>(ovalue));
50 auto resp = writeBus->call(mesg);
51 if (resp.is_method_error())
52 {
53 std::cerr << "Error sending message to " << path << "\n";
54 }
55 return;
56}
57
58void DbusWrite::write(double value)
59{
60 initBus();
61 auto mesg =
62 writeBus->new_method_call(connectionName.c_str(), path.c_str(),
63 "org.freedesktop.DBus.Properties", "Set");
64 mesg.append(pwmInterface, "Target",
65 sdbusplus::message::variant<uint64_t>(value));
66 auto resp = writeBus->call(mesg);
67 if (resp.is_method_error())
68 {
69 std::cerr << "Error sending message to " << path << "\n";
70 }
71
72 return;
73}