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