blob: 702bf8dd648ba49c8c28cb40a87314b17b34448c [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 */
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070016#include "dbuspassive.hpp"
17
18#include "dbus/util.hpp"
19
Patrick Venture863b9242018-03-08 08:29:23 -080020#include <chrono>
21#include <cmath>
Patrick Venture0ef1faf2018-06-13 12:50:53 -070022#include <memory>
Patrick Venture863b9242018-03-08 08:29:23 -080023#include <mutex>
Patrick Venture0ef1faf2018-06-13 12:50:53 -070024#include <sdbusplus/bus.hpp>
25#include <string>
Patrick Venture863b9242018-03-08 08:29:23 -080026
Patrick Venture0ef1faf2018-06-13 12:50:53 -070027std::unique_ptr<ReadInterface> DbusPassive::CreateDbusPassive(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070028 sdbusplus::bus::bus& bus, const std::string& type, const std::string& id,
29 DbusHelperInterface* helper)
Patrick Venture0ef1faf2018-06-13 12:50:53 -070030{
31 if (helper == nullptr)
32 {
33 return nullptr;
34 }
35 if (!ValidType(type))
36 {
37 return nullptr;
38 }
39
40 return std::make_unique<DbusPassive>(bus, type, id, helper);
41}
Patrick Venture863b9242018-03-08 08:29:23 -080042
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070043DbusPassive::DbusPassive(sdbusplus::bus::bus& bus, const std::string& type,
44 const std::string& id, DbusHelperInterface* helper) :
45 ReadInterface(),
46 _bus(bus), _signal(bus, GetMatch(type, id).c_str(), DbusHandleSignal, this),
47 _id(id), _helper(helper)
Patrick Venture863b9242018-03-08 08:29:23 -080048{
49 /* Need to get the scale and initial value */
50 auto tempBus = sdbusplus::bus::new_default();
51 /* service == busname */
52 std::string path = GetSensorPath(type, id);
Patrick Venture0df7c0f2018-06-13 09:02:13 -070053 std::string service = _helper->GetService(tempBus, sensorintf, path);
Patrick Venture863b9242018-03-08 08:29:23 -080054
55 struct SensorProperties settings;
Patrick Venture0df7c0f2018-06-13 09:02:13 -070056 _helper->GetProperties(tempBus, service, path, &settings);
Patrick Venture863b9242018-03-08 08:29:23 -080057
58 _scale = settings.scale;
59 _value = settings.value * pow(10, _scale);
60 _updated = std::chrono::high_resolution_clock::now();
61}
62
63ReadReturn DbusPassive::read(void)
64{
65 std::lock_guard<std::mutex> guard(_lock);
66
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070067 struct ReadReturn r = {_value, _updated};
Patrick Venture863b9242018-03-08 08:29:23 -080068
69 return r;
70}
71
72void DbusPassive::setValue(double value)
73{
74 std::lock_guard<std::mutex> guard(_lock);
75
76 _value = value;
77 _updated = std::chrono::high_resolution_clock::now();
78}
79
80int64_t DbusPassive::getScale(void)
81{
82 return _scale;
83}
84
85std::string DbusPassive::getId(void)
86{
87 return _id;
88}
89
Patrick Ventured0c75662018-06-12 19:03:21 -070090int HandleSensorValue(sdbusplus::message::message& msg, DbusPassive* owner)
Patrick Venture863b9242018-03-08 08:29:23 -080091{
Patrick Venture863b9242018-03-08 08:29:23 -080092 std::string msgSensor;
James Feistc065cf12018-07-05 10:23:11 -070093 std::map<std::string, sdbusplus::message::variant<int64_t, double>> msgData;
Patrick Ventured0c75662018-06-12 19:03:21 -070094
95 msg.read(msgSensor, msgData);
Patrick Venture863b9242018-03-08 08:29:23 -080096
97 if (msgSensor == "xyz.openbmc_project.Sensor.Value")
98 {
99 auto valPropMap = msgData.find("Value");
100 if (valPropMap != msgData.end())
101 {
James Feistd7a55bf2018-10-11 14:40:07 -0700102 double value = sdbusplus::message::variant_ns::apply_visitor(
103 VariantToDoubleVisitor(), valPropMap->second);
Patrick Venture863b9242018-03-08 08:29:23 -0800104
James Feistc065cf12018-07-05 10:23:11 -0700105 value *= std::pow(10, owner->getScale());
Patrick Venture863b9242018-03-08 08:29:23 -0800106
Patrick Ventured0c75662018-06-12 19:03:21 -0700107 owner->setValue(value);
Patrick Venture863b9242018-03-08 08:29:23 -0800108 }
109 }
110
111 return 0;
112}
Patrick Ventured0c75662018-06-12 19:03:21 -0700113
114int DbusHandleSignal(sd_bus_message* msg, void* usrData, sd_bus_error* err)
115{
116 auto sdbpMsg = sdbusplus::message::message(msg);
117 DbusPassive* obj = static_cast<DbusPassive*>(usrData);
118
119 return HandleSensorValue(sdbpMsg, obj);
120}