blob: ea471226dc25e23b63156432e9595b1dbae01859 [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
Patrick Ventureaadb30d2020-08-10 09:17:11 -070018#include "dbushelper_interface.hpp"
James Feist98b704e2019-06-03 16:24:53 -070019#include "dbuspassiveredundancy.hpp"
Patrick Ventureaadb30d2020-08-10 09:17:11 -070020#include "dbusutil.hpp"
James Feist0c8223b2019-05-08 15:33:33 -070021#include "util.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070022
Patrick Venturea83a3ec2020-08-04 09:52:05 -070023#include <sdbusplus/bus.hpp>
24
Patrick Venture863b9242018-03-08 08:29:23 -080025#include <chrono>
26#include <cmath>
Patrick Venture0ef1faf2018-06-13 12:50:53 -070027#include <memory>
Patrick Venture863b9242018-03-08 08:29:23 -080028#include <mutex>
Patrick Venture0ef1faf2018-06-13 12:50:53 -070029#include <string>
James Feist1f802f52019-02-08 13:51:43 -080030#include <variant>
Patrick Venture863b9242018-03-08 08:29:23 -080031
Patrick Venturea0764872020-08-08 07:48:43 -070032namespace pid_control
33{
34
Patrick Venture563a3562018-10-30 09:31:26 -070035std::unique_ptr<ReadInterface> DbusPassive::createDbusPassive(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070036 sdbusplus::bus::bus& bus, const std::string& type, const std::string& id,
Patrick Venture8729eb92020-08-10 10:38:44 -070037 std::unique_ptr<DbusHelperInterface> helper, const conf::SensorConfig* info,
James Feist98b704e2019-06-03 16:24:53 -070038 const std::shared_ptr<DbusPassiveRedundancy>& redundancy)
Patrick Venture0ef1faf2018-06-13 12:50:53 -070039{
40 if (helper == nullptr)
41 {
42 return nullptr;
43 }
Patrick Venture7af157b2018-10-30 11:24:40 -070044 if (!validType(type))
Patrick Venture0ef1faf2018-06-13 12:50:53 -070045 {
46 return nullptr;
47 }
48
Patrick Venture863b9242018-03-08 08:29:23 -080049 /* Need to get the scale and initial value */
Patrick Venture863b9242018-03-08 08:29:23 -080050 /* service == busname */
Patrick Venture7af157b2018-10-30 11:24:40 -070051 std::string path = getSensorPath(type, id);
Patrick Venture34ddc902018-10-30 11:05:17 -070052
Patrick Venture863b9242018-03-08 08:29:23 -080053 struct SensorProperties settings;
Patrick Venturef8cb4642018-10-30 12:02:53 -070054 bool failed;
Patrick Venture863b9242018-03-08 08:29:23 -080055
Patrick Venturef8cb4642018-10-30 12:02:53 -070056 try
57 {
Patrick Venture9b936922020-08-10 11:28:39 -070058 std::string service = helper->getService(sensorintf, path);
Patrick Venturef8cb4642018-10-30 12:02:53 -070059
Patrick Venture9b936922020-08-10 11:28:39 -070060 helper->getProperties(service, path, &settings);
61 failed = helper->thresholdsAsserted(service, path);
Patrick Venturef8cb4642018-10-30 12:02:53 -070062 }
63 catch (const std::exception& e)
64 {
65 return nullptr;
66 }
67
Patrick Venture6b9f5992019-09-10 09:18:28 -070068 /* if these values are zero, they're ignored. */
69 if (info->ignoreDbusMinMax)
70 {
71 settings.min = 0;
72 settings.max = 0;
73 }
74
Patrick Venture8729eb92020-08-10 10:38:44 -070075 return std::make_unique<DbusPassive>(bus, type, id, std::move(helper),
76 settings, failed, path, redundancy);
Patrick Venturef8cb4642018-10-30 12:02:53 -070077}
78
James Feist98b704e2019-06-03 16:24:53 -070079DbusPassive::DbusPassive(
80 sdbusplus::bus::bus& bus, const std::string& type, const std::string& id,
Patrick Venture8729eb92020-08-10 10:38:44 -070081 std::unique_ptr<DbusHelperInterface> helper,
82 const struct SensorProperties& settings, bool failed,
83 const std::string& path,
James Feist98b704e2019-06-03 16:24:53 -070084 const std::shared_ptr<DbusPassiveRedundancy>& redundancy) :
Patrick Venturef8cb4642018-10-30 12:02:53 -070085 ReadInterface(),
86 _bus(bus), _signal(bus, getMatch(type, id).c_str(), dbusHandleSignal, this),
Patrick Venture8729eb92020-08-10 10:38:44 -070087 _id(id), _helper(std::move(helper)), _failed(failed), path(path),
James Feist98b704e2019-06-03 16:24:53 -070088 redundancy(redundancy)
89
Patrick Venturef8cb4642018-10-30 12:02:53 -070090{
Patrick Venture863b9242018-03-08 08:29:23 -080091 _scale = settings.scale;
92 _value = settings.value * pow(10, _scale);
James Feist75eb7692019-02-25 12:50:02 -080093 _min = settings.min * pow(10, _scale);
94 _max = settings.max * pow(10, _scale);
Patrick Venture863b9242018-03-08 08:29:23 -080095 _updated = std::chrono::high_resolution_clock::now();
96}
97
98ReadReturn DbusPassive::read(void)
99{
100 std::lock_guard<std::mutex> guard(_lock);
101
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700102 struct ReadReturn r = {_value, _updated};
Patrick Venture863b9242018-03-08 08:29:23 -0800103
104 return r;
105}
106
107void DbusPassive::setValue(double value)
108{
109 std::lock_guard<std::mutex> guard(_lock);
110
111 _value = value;
112 _updated = std::chrono::high_resolution_clock::now();
113}
114
James Feist36b7d8e2018-10-05 15:39:01 -0700115bool DbusPassive::getFailed(void) const
116{
James Feist98b704e2019-06-03 16:24:53 -0700117 if (redundancy)
118 {
119 const std::set<std::string>& failures = redundancy->getFailed();
120 if (failures.find(path) != failures.end())
121 {
122 return true;
123 }
124 }
James Feist4b36f262020-07-07 16:56:41 -0700125
126 return _failed || !_functional;
James Feist36b7d8e2018-10-05 15:39:01 -0700127}
128
129void DbusPassive::setFailed(bool value)
130{
131 _failed = value;
132}
133
James Feist4b36f262020-07-07 16:56:41 -0700134void DbusPassive::setFunctional(bool value)
135{
136 _functional = value;
137}
138
Patrick Venture863b9242018-03-08 08:29:23 -0800139int64_t DbusPassive::getScale(void)
140{
141 return _scale;
142}
143
Patrick Venture563a3562018-10-30 09:31:26 -0700144std::string DbusPassive::getID(void)
Patrick Venture863b9242018-03-08 08:29:23 -0800145{
146 return _id;
147}
148
James Feist75eb7692019-02-25 12:50:02 -0800149double DbusPassive::getMax(void)
150{
151 return _max;
152}
153
154double DbusPassive::getMin(void)
155{
156 return _min;
157}
158
Patrick Venture7af157b2018-10-30 11:24:40 -0700159int handleSensorValue(sdbusplus::message::message& msg, DbusPassive* owner)
Patrick Venture863b9242018-03-08 08:29:23 -0800160{
Patrick Venture863b9242018-03-08 08:29:23 -0800161 std::string msgSensor;
James Feist1f802f52019-02-08 13:51:43 -0800162 std::map<std::string, std::variant<int64_t, double, bool>> msgData;
Patrick Ventured0c75662018-06-12 19:03:21 -0700163
164 msg.read(msgSensor, msgData);
Patrick Venture863b9242018-03-08 08:29:23 -0800165
166 if (msgSensor == "xyz.openbmc_project.Sensor.Value")
167 {
168 auto valPropMap = msgData.find("Value");
169 if (valPropMap != msgData.end())
170 {
James Feist1f802f52019-02-08 13:51:43 -0800171 double value =
172 std::visit(VariantToDoubleVisitor(), valPropMap->second);
Patrick Venture863b9242018-03-08 08:29:23 -0800173
James Feistc065cf12018-07-05 10:23:11 -0700174 value *= std::pow(10, owner->getScale());
Patrick Venture863b9242018-03-08 08:29:23 -0800175
James Feist75eb7692019-02-25 12:50:02 -0800176 scaleSensorReading(owner->getMin(), owner->getMax(), value);
177
Patrick Ventured0c75662018-06-12 19:03:21 -0700178 owner->setValue(value);
Patrick Venture863b9242018-03-08 08:29:23 -0800179 }
180 }
James Feist36b7d8e2018-10-05 15:39:01 -0700181 else if (msgSensor == "xyz.openbmc_project.Sensor.Threshold.Critical")
182 {
183 auto criticalAlarmLow = msgData.find("CriticalAlarmLow");
184 auto criticalAlarmHigh = msgData.find("CriticalAlarmHigh");
185 if (criticalAlarmHigh == msgData.end() &&
186 criticalAlarmLow == msgData.end())
187 {
188 return 0;
189 }
190
191 bool asserted = false;
192 if (criticalAlarmLow != msgData.end())
193 {
James Feist1f802f52019-02-08 13:51:43 -0800194 asserted = std::get<bool>(criticalAlarmLow->second);
James Feist36b7d8e2018-10-05 15:39:01 -0700195 }
196
197 // checking both as in theory you could de-assert one threshold and
198 // assert the other at the same moment
199 if (!asserted && criticalAlarmHigh != msgData.end())
200 {
James Feist1f802f52019-02-08 13:51:43 -0800201 asserted = std::get<bool>(criticalAlarmHigh->second);
James Feist36b7d8e2018-10-05 15:39:01 -0700202 }
203 owner->setFailed(asserted);
204 }
James Feist4b36f262020-07-07 16:56:41 -0700205 else if (msgSensor ==
206 "xyz.openbmc_project.State.Decorator.OperationalStatus")
207 {
208 auto functional = msgData.find("Functional");
209 if (functional == msgData.end())
210 {
211 return 0;
212 }
213 bool asserted = std::get<bool>(functional->second);
214 owner->setFunctional(asserted);
215 }
Patrick Venture863b9242018-03-08 08:29:23 -0800216
217 return 0;
218}
Patrick Ventured0c75662018-06-12 19:03:21 -0700219
Patrick Venture7af157b2018-10-30 11:24:40 -0700220int dbusHandleSignal(sd_bus_message* msg, void* usrData, sd_bus_error* err)
Patrick Ventured0c75662018-06-12 19:03:21 -0700221{
222 auto sdbpMsg = sdbusplus::message::message(msg);
223 DbusPassive* obj = static_cast<DbusPassive*>(usrData);
224
Patrick Venture7af157b2018-10-30 11:24:40 -0700225 return handleSensorValue(sdbpMsg, obj);
Patrick Ventured0c75662018-06-12 19:03:21 -0700226}
Patrick Venturea0764872020-08-08 07:48:43 -0700227
228} // namespace pid_control