blob: da9b387b4d42dd5f63fc3cc319d66067cb128656 [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 Venture863b9242018-03-08 08:29:23 -080016#include <chrono>
17#include <cmath>
Patrick Venture0ef1faf2018-06-13 12:50:53 -070018#include <memory>
Patrick Venture863b9242018-03-08 08:29:23 -080019#include <mutex>
Patrick Venture0ef1faf2018-06-13 12:50:53 -070020#include <sdbusplus/bus.hpp>
21#include <string>
Patrick Venture863b9242018-03-08 08:29:23 -080022
23#include "dbuspassive.hpp"
Patrick Venture0ef1faf2018-06-13 12:50:53 -070024#include "dbus/util.hpp"
25
26std::unique_ptr<ReadInterface> DbusPassive::CreateDbusPassive(
27 sdbusplus::bus::bus& bus, const std::string& type,
28 const std::string& id, DbusHelperInterface *helper)
29{
30 if (helper == nullptr)
31 {
32 return nullptr;
33 }
34 if (!ValidType(type))
35 {
36 return nullptr;
37 }
38
39 return std::make_unique<DbusPassive>(bus, type, id, helper);
40}
Patrick Venture863b9242018-03-08 08:29:23 -080041
42DbusPassive::DbusPassive(
43 sdbusplus::bus::bus& bus,
44 const std::string& type,
Patrick Venture0df7c0f2018-06-13 09:02:13 -070045 const std::string& id,
46 DbusHelperInterface *helper)
Patrick Venture863b9242018-03-08 08:29:23 -080047 : ReadInterface(),
48 _bus(bus),
49 _signal(bus, GetMatch(type, id).c_str(), DbusHandleSignal, this),
Patrick Venture0df7c0f2018-06-13 09:02:13 -070050 _id(id),
51 _helper(helper)
Patrick Venture863b9242018-03-08 08:29:23 -080052{
53 /* Need to get the scale and initial value */
54 auto tempBus = sdbusplus::bus::new_default();
55 /* service == busname */
56 std::string path = GetSensorPath(type, id);
Patrick Venture0df7c0f2018-06-13 09:02:13 -070057 std::string service = _helper->GetService(tempBus, sensorintf, path);
Patrick Venture863b9242018-03-08 08:29:23 -080058
59 struct SensorProperties settings;
Patrick Venture0df7c0f2018-06-13 09:02:13 -070060 _helper->GetProperties(tempBus, service, path, &settings);
Patrick Venture863b9242018-03-08 08:29:23 -080061
62 _scale = settings.scale;
63 _value = settings.value * pow(10, _scale);
64 _updated = std::chrono::high_resolution_clock::now();
65}
66
67ReadReturn DbusPassive::read(void)
68{
69 std::lock_guard<std::mutex> guard(_lock);
70
71 struct ReadReturn r = {
72 _value,
73 _updated
74 };
75
76 return r;
77}
78
79void DbusPassive::setValue(double value)
80{
81 std::lock_guard<std::mutex> guard(_lock);
82
83 _value = value;
84 _updated = std::chrono::high_resolution_clock::now();
85}
86
87int64_t DbusPassive::getScale(void)
88{
89 return _scale;
90}
91
92std::string DbusPassive::getId(void)
93{
94 return _id;
95}
96
Patrick Ventured0c75662018-06-12 19:03:21 -070097int HandleSensorValue(sdbusplus::message::message& msg, DbusPassive* owner)
Patrick Venture863b9242018-03-08 08:29:23 -080098{
Patrick Venture863b9242018-03-08 08:29:23 -080099 std::string msgSensor;
James Feistc065cf12018-07-05 10:23:11 -0700100 std::map<std::string, sdbusplus::message::variant<int64_t, double>> msgData;
Patrick Ventured0c75662018-06-12 19:03:21 -0700101
102 msg.read(msgSensor, msgData);
Patrick Venture863b9242018-03-08 08:29:23 -0800103
104 if (msgSensor == "xyz.openbmc_project.Sensor.Value")
105 {
106 auto valPropMap = msgData.find("Value");
107 if (valPropMap != msgData.end())
108 {
James Feistc065cf12018-07-05 10:23:11 -0700109 double value = mapbox::util::apply_visitor(VariantToDoubleVisitor(),
110 valPropMap->second);
Patrick Venture863b9242018-03-08 08:29:23 -0800111
James Feistc065cf12018-07-05 10:23:11 -0700112 value *= std::pow(10, owner->getScale());
Patrick Venture863b9242018-03-08 08:29:23 -0800113
Patrick Ventured0c75662018-06-12 19:03:21 -0700114 owner->setValue(value);
Patrick Venture863b9242018-03-08 08:29:23 -0800115 }
116 }
117
118 return 0;
119}
Patrick Ventured0c75662018-06-12 19:03:21 -0700120
121int DbusHandleSignal(sd_bus_message* msg, void* usrData, sd_bus_error* err)
122{
123 auto sdbpMsg = sdbusplus::message::message(msg);
124 DbusPassive* obj = static_cast<DbusPassive*>(usrData);
125
126 return HandleSensorValue(sdbpMsg, obj);
127}