blob: 27255e75841b5dfc83dda99df4e3a05937c3b1fd [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,
James Feist98b704e2019-06-03 16:24:53 -070037 DbusHelperInterface* helper, const conf::SensorConfig* info,
38 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 */
James Feist9fa90c12019-01-11 15:35:22 -080050 auto tempBus = sdbusplus::bus::new_system();
Patrick Venturef8cb4642018-10-30 12:02:53 -070051
Patrick Venture863b9242018-03-08 08:29:23 -080052 /* service == busname */
Patrick Venture7af157b2018-10-30 11:24:40 -070053 std::string path = getSensorPath(type, id);
Patrick Venture34ddc902018-10-30 11:05:17 -070054
Patrick Venture863b9242018-03-08 08:29:23 -080055 struct SensorProperties settings;
Patrick Venturef8cb4642018-10-30 12:02:53 -070056 bool failed;
Patrick Venture863b9242018-03-08 08:29:23 -080057
Patrick Venturef8cb4642018-10-30 12:02:53 -070058 try
59 {
60 std::string service = helper->getService(tempBus, sensorintf, path);
61
62 helper->getProperties(tempBus, service, path, &settings);
63 failed = helper->thresholdsAsserted(tempBus, service, path);
64 }
65 catch (const std::exception& e)
66 {
67 return nullptr;
68 }
69
Patrick Venture6b9f5992019-09-10 09:18:28 -070070 /* if these values are zero, they're ignored. */
71 if (info->ignoreDbusMinMax)
72 {
73 settings.min = 0;
74 settings.max = 0;
75 }
76
Patrick Venturef8cb4642018-10-30 12:02:53 -070077 return std::make_unique<DbusPassive>(bus, type, id, helper, settings,
James Feist98b704e2019-06-03 16:24:53 -070078 failed, path, redundancy);
Patrick Venturef8cb4642018-10-30 12:02:53 -070079}
80
James Feist98b704e2019-06-03 16:24:53 -070081DbusPassive::DbusPassive(
82 sdbusplus::bus::bus& bus, const std::string& type, const std::string& id,
83 DbusHelperInterface* helper, const struct SensorProperties& settings,
84 bool failed, const std::string& path,
85 const std::shared_ptr<DbusPassiveRedundancy>& redundancy) :
Patrick Venturef8cb4642018-10-30 12:02:53 -070086 ReadInterface(),
87 _bus(bus), _signal(bus, getMatch(type, id).c_str(), dbusHandleSignal, this),
James Feist98b704e2019-06-03 16:24:53 -070088 _id(id), _helper(helper), _failed(failed), path(path),
89 redundancy(redundancy)
90
Patrick Venturef8cb4642018-10-30 12:02:53 -070091{
Patrick Venture863b9242018-03-08 08:29:23 -080092 _scale = settings.scale;
93 _value = settings.value * pow(10, _scale);
James Feist75eb7692019-02-25 12:50:02 -080094 _min = settings.min * pow(10, _scale);
95 _max = settings.max * pow(10, _scale);
Patrick Venture863b9242018-03-08 08:29:23 -080096 _updated = std::chrono::high_resolution_clock::now();
97}
98
99ReadReturn DbusPassive::read(void)
100{
101 std::lock_guard<std::mutex> guard(_lock);
102
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700103 struct ReadReturn r = {_value, _updated};
Patrick Venture863b9242018-03-08 08:29:23 -0800104
105 return r;
106}
107
108void DbusPassive::setValue(double value)
109{
110 std::lock_guard<std::mutex> guard(_lock);
111
112 _value = value;
113 _updated = std::chrono::high_resolution_clock::now();
114}
115
James Feist36b7d8e2018-10-05 15:39:01 -0700116bool DbusPassive::getFailed(void) const
117{
James Feist98b704e2019-06-03 16:24:53 -0700118 if (redundancy)
119 {
120 const std::set<std::string>& failures = redundancy->getFailed();
121 if (failures.find(path) != failures.end())
122 {
123 return true;
124 }
125 }
James Feist4b36f262020-07-07 16:56:41 -0700126
127 return _failed || !_functional;
James Feist36b7d8e2018-10-05 15:39:01 -0700128}
129
130void DbusPassive::setFailed(bool value)
131{
132 _failed = value;
133}
134
James Feist4b36f262020-07-07 16:56:41 -0700135void DbusPassive::setFunctional(bool value)
136{
137 _functional = value;
138}
139
Patrick Venture863b9242018-03-08 08:29:23 -0800140int64_t DbusPassive::getScale(void)
141{
142 return _scale;
143}
144
Patrick Venture563a3562018-10-30 09:31:26 -0700145std::string DbusPassive::getID(void)
Patrick Venture863b9242018-03-08 08:29:23 -0800146{
147 return _id;
148}
149
James Feist75eb7692019-02-25 12:50:02 -0800150double DbusPassive::getMax(void)
151{
152 return _max;
153}
154
155double DbusPassive::getMin(void)
156{
157 return _min;
158}
159
Patrick Venture7af157b2018-10-30 11:24:40 -0700160int handleSensorValue(sdbusplus::message::message& msg, DbusPassive* owner)
Patrick Venture863b9242018-03-08 08:29:23 -0800161{
Patrick Venture863b9242018-03-08 08:29:23 -0800162 std::string msgSensor;
James Feist1f802f52019-02-08 13:51:43 -0800163 std::map<std::string, std::variant<int64_t, double, bool>> msgData;
Patrick Ventured0c75662018-06-12 19:03:21 -0700164
165 msg.read(msgSensor, msgData);
Patrick Venture863b9242018-03-08 08:29:23 -0800166
167 if (msgSensor == "xyz.openbmc_project.Sensor.Value")
168 {
169 auto valPropMap = msgData.find("Value");
170 if (valPropMap != msgData.end())
171 {
James Feist1f802f52019-02-08 13:51:43 -0800172 double value =
173 std::visit(VariantToDoubleVisitor(), valPropMap->second);
Patrick Venture863b9242018-03-08 08:29:23 -0800174
James Feistc065cf12018-07-05 10:23:11 -0700175 value *= std::pow(10, owner->getScale());
Patrick Venture863b9242018-03-08 08:29:23 -0800176
James Feist75eb7692019-02-25 12:50:02 -0800177 scaleSensorReading(owner->getMin(), owner->getMax(), value);
178
Patrick Ventured0c75662018-06-12 19:03:21 -0700179 owner->setValue(value);
Patrick Venture863b9242018-03-08 08:29:23 -0800180 }
181 }
James Feist36b7d8e2018-10-05 15:39:01 -0700182 else if (msgSensor == "xyz.openbmc_project.Sensor.Threshold.Critical")
183 {
184 auto criticalAlarmLow = msgData.find("CriticalAlarmLow");
185 auto criticalAlarmHigh = msgData.find("CriticalAlarmHigh");
186 if (criticalAlarmHigh == msgData.end() &&
187 criticalAlarmLow == msgData.end())
188 {
189 return 0;
190 }
191
192 bool asserted = false;
193 if (criticalAlarmLow != msgData.end())
194 {
James Feist1f802f52019-02-08 13:51:43 -0800195 asserted = std::get<bool>(criticalAlarmLow->second);
James Feist36b7d8e2018-10-05 15:39:01 -0700196 }
197
198 // checking both as in theory you could de-assert one threshold and
199 // assert the other at the same moment
200 if (!asserted && criticalAlarmHigh != msgData.end())
201 {
James Feist1f802f52019-02-08 13:51:43 -0800202 asserted = std::get<bool>(criticalAlarmHigh->second);
James Feist36b7d8e2018-10-05 15:39:01 -0700203 }
204 owner->setFailed(asserted);
205 }
James Feist4b36f262020-07-07 16:56:41 -0700206 else if (msgSensor ==
207 "xyz.openbmc_project.State.Decorator.OperationalStatus")
208 {
209 auto functional = msgData.find("Functional");
210 if (functional == msgData.end())
211 {
212 return 0;
213 }
214 bool asserted = std::get<bool>(functional->second);
215 owner->setFunctional(asserted);
216 }
Patrick Venture863b9242018-03-08 08:29:23 -0800217
218 return 0;
219}
Patrick Ventured0c75662018-06-12 19:03:21 -0700220
Patrick Venture7af157b2018-10-30 11:24:40 -0700221int dbusHandleSignal(sd_bus_message* msg, void* usrData, sd_bus_error* err)
Patrick Ventured0c75662018-06-12 19:03:21 -0700222{
223 auto sdbpMsg = sdbusplus::message::message(msg);
224 DbusPassive* obj = static_cast<DbusPassive*>(usrData);
225
Patrick Venture7af157b2018-10-30 11:24:40 -0700226 return handleSensorValue(sdbpMsg, obj);
Patrick Ventured0c75662018-06-12 19:03:21 -0700227}
Patrick Venturea0764872020-08-08 07:48:43 -0700228
229} // namespace pid_control