blob: 6b4ea5f62eb6cafee7813f1f636aa29302f0f9ef [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,
Patrick Venture0df7c0f2018-06-13 09:02:13 -070026 const std::string& id,
27 DbusHelperInterface *helper)
Patrick Venture863b9242018-03-08 08:29:23 -080028 : ReadInterface(),
29 _bus(bus),
30 _signal(bus, GetMatch(type, id).c_str(), DbusHandleSignal, this),
Patrick Venture0df7c0f2018-06-13 09:02:13 -070031 _id(id),
32 _helper(helper)
Patrick Venture863b9242018-03-08 08:29:23 -080033{
34 /* Need to get the scale and initial value */
35 auto tempBus = sdbusplus::bus::new_default();
36 /* service == busname */
37 std::string path = GetSensorPath(type, id);
Patrick Venture0df7c0f2018-06-13 09:02:13 -070038 std::string service = _helper->GetService(tempBus, sensorintf, path);
Patrick Venture863b9242018-03-08 08:29:23 -080039
40 struct SensorProperties settings;
Patrick Venture0df7c0f2018-06-13 09:02:13 -070041 _helper->GetProperties(tempBus, service, path, &settings);
Patrick Venture863b9242018-03-08 08:29:23 -080042
43 _scale = settings.scale;
44 _value = settings.value * pow(10, _scale);
45 _updated = std::chrono::high_resolution_clock::now();
46}
47
48ReadReturn DbusPassive::read(void)
49{
50 std::lock_guard<std::mutex> guard(_lock);
51
52 struct ReadReturn r = {
53 _value,
54 _updated
55 };
56
57 return r;
58}
59
60void DbusPassive::setValue(double value)
61{
62 std::lock_guard<std::mutex> guard(_lock);
63
64 _value = value;
65 _updated = std::chrono::high_resolution_clock::now();
66}
67
68int64_t DbusPassive::getScale(void)
69{
70 return _scale;
71}
72
73std::string DbusPassive::getId(void)
74{
75 return _id;
76}
77
Patrick Ventured0c75662018-06-12 19:03:21 -070078int HandleSensorValue(sdbusplus::message::message& msg, DbusPassive* owner)
Patrick Venture863b9242018-03-08 08:29:23 -080079{
Patrick Venture863b9242018-03-08 08:29:23 -080080 std::string msgSensor;
Patrick Ventured0c75662018-06-12 19:03:21 -070081 std::map<std::string, sdbusplus::message::variant<int64_t>> msgData;
82
83 msg.read(msgSensor, msgData);
Patrick Venture863b9242018-03-08 08:29:23 -080084
85 if (msgSensor == "xyz.openbmc_project.Sensor.Value")
86 {
87 auto valPropMap = msgData.find("Value");
88 if (valPropMap != msgData.end())
89 {
Patrick Ventured0c75662018-06-12 19:03:21 -070090 int64_t rawValue = sdbusplus::message::variant_ns::get<int64_t>(
91 valPropMap->second);
Patrick Venture863b9242018-03-08 08:29:23 -080092
Patrick Ventured0c75662018-06-12 19:03:21 -070093 double value = rawValue * std::pow(10, owner->getScale());
Patrick Venture863b9242018-03-08 08:29:23 -080094
Patrick Ventured0c75662018-06-12 19:03:21 -070095 owner->setValue(value);
Patrick Venture863b9242018-03-08 08:29:23 -080096 }
97 }
98
99 return 0;
100}
Patrick Ventured0c75662018-06-12 19:03:21 -0700101
102int DbusHandleSignal(sd_bus_message* msg, void* usrData, sd_bus_error* err)
103{
104 auto sdbpMsg = sdbusplus::message::message(msg);
105 DbusPassive* obj = static_cast<DbusPassive*>(usrData);
106
107 return HandleSensorValue(sdbpMsg, obj);
108}