blob: ee165b9adf898f095096dbf6cf33a5e9a734bc5e [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>
James Feist1f802f52019-02-08 13:51:43 -080026#include <variant>
Patrick Venture863b9242018-03-08 08:29:23 -080027
Patrick Venture563a3562018-10-30 09:31:26 -070028std::unique_ptr<ReadInterface> DbusPassive::createDbusPassive(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070029 sdbusplus::bus::bus& bus, const std::string& type, const std::string& id,
James Feistf81f2882019-02-26 11:26:36 -080030 DbusHelperInterface* helper, const conf::SensorConfig* info)
Patrick Venture0ef1faf2018-06-13 12:50:53 -070031{
32 if (helper == nullptr)
33 {
34 return nullptr;
35 }
Patrick Venture7af157b2018-10-30 11:24:40 -070036 if (!validType(type))
Patrick Venture0ef1faf2018-06-13 12:50:53 -070037 {
38 return nullptr;
39 }
40
Patrick Venture863b9242018-03-08 08:29:23 -080041 /* Need to get the scale and initial value */
James Feist9fa90c12019-01-11 15:35:22 -080042 auto tempBus = sdbusplus::bus::new_system();
Patrick Venturef8cb4642018-10-30 12:02:53 -070043
Patrick Venture863b9242018-03-08 08:29:23 -080044 /* service == busname */
Patrick Venture7af157b2018-10-30 11:24:40 -070045 std::string path = getSensorPath(type, id);
Patrick Venture34ddc902018-10-30 11:05:17 -070046
Patrick Venture863b9242018-03-08 08:29:23 -080047 struct SensorProperties settings;
Patrick Venturef8cb4642018-10-30 12:02:53 -070048 bool failed;
Patrick Venture863b9242018-03-08 08:29:23 -080049
Patrick Venturef8cb4642018-10-30 12:02:53 -070050 try
51 {
52 std::string service = helper->getService(tempBus, sensorintf, path);
53
54 helper->getProperties(tempBus, service, path, &settings);
55 failed = helper->thresholdsAsserted(tempBus, service, path);
56 }
57 catch (const std::exception& e)
58 {
59 return nullptr;
60 }
James Feist75eb7692019-02-25 12:50:02 -080061 if (info->max != conf::inheritValueFromDbus)
62 {
63 settings.max = info->max;
64 }
65
66 if (info->max != conf::inheritValueFromDbus)
67 {
68 settings.min = info->min;
69 }
Patrick Venturef8cb4642018-10-30 12:02:53 -070070
71 return std::make_unique<DbusPassive>(bus, type, id, helper, settings,
72 failed);
73}
74
75DbusPassive::DbusPassive(sdbusplus::bus::bus& bus, const std::string& type,
76 const std::string& id, DbusHelperInterface* helper,
77 const struct SensorProperties& settings, bool failed) :
78 ReadInterface(),
79 _bus(bus), _signal(bus, getMatch(type, id).c_str(), dbusHandleSignal, this),
80 _id(id), _helper(helper), _failed(failed)
81{
Patrick Venture863b9242018-03-08 08:29:23 -080082 _scale = settings.scale;
83 _value = settings.value * pow(10, _scale);
James Feist75eb7692019-02-25 12:50:02 -080084 _min = settings.min * pow(10, _scale);
85 _max = settings.max * pow(10, _scale);
Patrick Venture863b9242018-03-08 08:29:23 -080086 _updated = std::chrono::high_resolution_clock::now();
87}
88
89ReadReturn DbusPassive::read(void)
90{
91 std::lock_guard<std::mutex> guard(_lock);
92
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070093 struct ReadReturn r = {_value, _updated};
Patrick Venture863b9242018-03-08 08:29:23 -080094
95 return r;
96}
97
98void DbusPassive::setValue(double value)
99{
100 std::lock_guard<std::mutex> guard(_lock);
101
102 _value = value;
103 _updated = std::chrono::high_resolution_clock::now();
104}
105
James Feist36b7d8e2018-10-05 15:39:01 -0700106bool DbusPassive::getFailed(void) const
107{
108 return _failed;
109}
110
111void DbusPassive::setFailed(bool value)
112{
113 _failed = value;
114}
115
Patrick Venture863b9242018-03-08 08:29:23 -0800116int64_t DbusPassive::getScale(void)
117{
118 return _scale;
119}
120
Patrick Venture563a3562018-10-30 09:31:26 -0700121std::string DbusPassive::getID(void)
Patrick Venture863b9242018-03-08 08:29:23 -0800122{
123 return _id;
124}
125
James Feist75eb7692019-02-25 12:50:02 -0800126double DbusPassive::getMax(void)
127{
128 return _max;
129}
130
131double DbusPassive::getMin(void)
132{
133 return _min;
134}
135
Patrick Venture7af157b2018-10-30 11:24:40 -0700136int handleSensorValue(sdbusplus::message::message& msg, DbusPassive* owner)
Patrick Venture863b9242018-03-08 08:29:23 -0800137{
Patrick Venture863b9242018-03-08 08:29:23 -0800138 std::string msgSensor;
James Feist1f802f52019-02-08 13:51:43 -0800139 std::map<std::string, std::variant<int64_t, double, bool>> msgData;
Patrick Ventured0c75662018-06-12 19:03:21 -0700140
141 msg.read(msgSensor, msgData);
Patrick Venture863b9242018-03-08 08:29:23 -0800142
143 if (msgSensor == "xyz.openbmc_project.Sensor.Value")
144 {
145 auto valPropMap = msgData.find("Value");
146 if (valPropMap != msgData.end())
147 {
James Feist1f802f52019-02-08 13:51:43 -0800148 double value =
149 std::visit(VariantToDoubleVisitor(), valPropMap->second);
Patrick Venture863b9242018-03-08 08:29:23 -0800150
James Feistc065cf12018-07-05 10:23:11 -0700151 value *= std::pow(10, owner->getScale());
Patrick Venture863b9242018-03-08 08:29:23 -0800152
James Feist75eb7692019-02-25 12:50:02 -0800153 scaleSensorReading(owner->getMin(), owner->getMax(), value);
154
Patrick Ventured0c75662018-06-12 19:03:21 -0700155 owner->setValue(value);
Patrick Venture863b9242018-03-08 08:29:23 -0800156 }
157 }
James Feist36b7d8e2018-10-05 15:39:01 -0700158 else if (msgSensor == "xyz.openbmc_project.Sensor.Threshold.Critical")
159 {
160 auto criticalAlarmLow = msgData.find("CriticalAlarmLow");
161 auto criticalAlarmHigh = msgData.find("CriticalAlarmHigh");
162 if (criticalAlarmHigh == msgData.end() &&
163 criticalAlarmLow == msgData.end())
164 {
165 return 0;
166 }
167
168 bool asserted = false;
169 if (criticalAlarmLow != msgData.end())
170 {
James Feist1f802f52019-02-08 13:51:43 -0800171 asserted = std::get<bool>(criticalAlarmLow->second);
James Feist36b7d8e2018-10-05 15:39:01 -0700172 }
173
174 // checking both as in theory you could de-assert one threshold and
175 // assert the other at the same moment
176 if (!asserted && criticalAlarmHigh != msgData.end())
177 {
James Feist1f802f52019-02-08 13:51:43 -0800178 asserted = std::get<bool>(criticalAlarmHigh->second);
James Feist36b7d8e2018-10-05 15:39:01 -0700179 }
180 owner->setFailed(asserted);
181 }
Patrick Venture863b9242018-03-08 08:29:23 -0800182
183 return 0;
184}
Patrick Ventured0c75662018-06-12 19:03:21 -0700185
Patrick Venture7af157b2018-10-30 11:24:40 -0700186int dbusHandleSignal(sd_bus_message* msg, void* usrData, sd_bus_error* err)
Patrick Ventured0c75662018-06-12 19:03:21 -0700187{
188 auto sdbpMsg = sdbusplus::message::message(msg);
189 DbusPassive* obj = static_cast<DbusPassive*>(usrData);
190
Patrick Venture7af157b2018-10-30 11:24:40 -0700191 return handleSensorValue(sdbpMsg, obj);
Patrick Ventured0c75662018-06-12 19:03:21 -0700192}