blob: 038fc5cd2ea80df552af1338cf574493a28256b6 [file] [log] [blame]
Patrick Venture863b9242018-03-08 08:29:23 -08001/**
2 * Copyright 2017 Google Inc.
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 <chrono>
18#include <cmath>
19#include <mutex>
20
21#include "dbuspassive.hpp"
22
23DbusPassive::DbusPassive(
24 sdbusplus::bus::bus& bus,
25 const std::string& type,
26 const std::string& id)
27 : ReadInterface(),
28 _bus(bus),
29 _signal(bus, GetMatch(type, id).c_str(), DbusHandleSignal, this),
30 _id(id)
31{
32 /* Need to get the scale and initial value */
33 auto tempBus = sdbusplus::bus::new_default();
34 /* service == busname */
35 std::string path = GetSensorPath(type, id);
36 std::string service = GetService(tempBus, sensorintf, path);
37
38 struct SensorProperties settings;
39 GetProperties(tempBus, service, path, &settings);
40
41 _scale = settings.scale;
42 _value = settings.value * pow(10, _scale);
43 _updated = std::chrono::high_resolution_clock::now();
44}
45
46ReadReturn DbusPassive::read(void)
47{
48 std::lock_guard<std::mutex> guard(_lock);
49
50 struct ReadReturn r = {
51 _value,
52 _updated
53 };
54
55 return r;
56}
57
58void DbusPassive::setValue(double value)
59{
60 std::lock_guard<std::mutex> guard(_lock);
61
62 _value = value;
63 _updated = std::chrono::high_resolution_clock::now();
64}
65
66int64_t DbusPassive::getScale(void)
67{
68 return _scale;
69}
70
71std::string DbusPassive::getId(void)
72{
73 return _id;
74}
75
Patrick Ventured0c75662018-06-12 19:03:21 -070076int HandleSensorValue(sdbusplus::message::message& msg, DbusPassive* owner)
Patrick Venture863b9242018-03-08 08:29:23 -080077{
Patrick Venture863b9242018-03-08 08:29:23 -080078 std::string msgSensor;
Patrick Ventured0c75662018-06-12 19:03:21 -070079 std::map<std::string, sdbusplus::message::variant<int64_t>> msgData;
80
81 msg.read(msgSensor, msgData);
Patrick Venture863b9242018-03-08 08:29:23 -080082
83 if (msgSensor == "xyz.openbmc_project.Sensor.Value")
84 {
85 auto valPropMap = msgData.find("Value");
86 if (valPropMap != msgData.end())
87 {
Patrick Ventured0c75662018-06-12 19:03:21 -070088 int64_t rawValue = sdbusplus::message::variant_ns::get<int64_t>(
89 valPropMap->second);
Patrick Venture863b9242018-03-08 08:29:23 -080090
Patrick Ventured0c75662018-06-12 19:03:21 -070091 double value = rawValue * std::pow(10, owner->getScale());
Patrick Venture863b9242018-03-08 08:29:23 -080092
Patrick Ventured0c75662018-06-12 19:03:21 -070093 owner->setValue(value);
Patrick Venture863b9242018-03-08 08:29:23 -080094 }
95 }
96
97 return 0;
98}
Patrick Ventured0c75662018-06-12 19:03:21 -070099
100int DbusHandleSignal(sd_bus_message* msg, void* usrData, sd_bus_error* err)
101{
102 auto sdbpMsg = sdbusplus::message::message(msg);
103 DbusPassive* obj = static_cast<DbusPassive*>(usrData);
104
105 return HandleSensorValue(sdbpMsg, obj);
106}