blob: 41047a1568a51538f287b41c446a6634d25142a0 [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 Williamsb228bc32022-07-22 19:26:56 -050036 sdbusplus::bus_t& 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 */
Harvey.Wuf2efcbb2022-02-09 10:24:30 +080051 std::string path;
52 if (info->readPath.empty())
53 {
54 path = getSensorPath(type, id);
55 }
56 else
57 {
58 path = info->readPath;
59 }
Patrick Venture34ddc902018-10-30 11:05:17 -070060
Patrick Venture1df9e872020-10-08 15:35:01 -070061 SensorProperties settings;
Patrick Venturef8cb4642018-10-30 12:02:53 -070062 bool failed;
Patrick Venture863b9242018-03-08 08:29:23 -080063
Patrick Venturef8cb4642018-10-30 12:02:53 -070064 try
65 {
Patrick Venture9b936922020-08-10 11:28:39 -070066 std::string service = helper->getService(sensorintf, path);
Patrick Venturef8cb4642018-10-30 12:02:53 -070067
Patrick Venture9b936922020-08-10 11:28:39 -070068 helper->getProperties(service, path, &settings);
69 failed = helper->thresholdsAsserted(service, path);
Patrick Venturef8cb4642018-10-30 12:02:53 -070070 }
71 catch (const std::exception& e)
72 {
73 return nullptr;
74 }
75
Patrick Venture6b9f5992019-09-10 09:18:28 -070076 /* if these values are zero, they're ignored. */
77 if (info->ignoreDbusMinMax)
78 {
79 settings.min = 0;
80 settings.max = 0;
81 }
82
Alex.Song8f73ad72021-10-07 00:18:27 +080083 settings.unavailableAsFailed = info->unavailableAsFailed;
84
Patrick Venture8729eb92020-08-10 10:38:44 -070085 return std::make_unique<DbusPassive>(bus, type, id, std::move(helper),
86 settings, failed, path, redundancy);
Patrick Venturef8cb4642018-10-30 12:02:53 -070087}
88
James Feist98b704e2019-06-03 16:24:53 -070089DbusPassive::DbusPassive(
Patrick Williamsb228bc32022-07-22 19:26:56 -050090 sdbusplus::bus_t& bus, const std::string& type, const std::string& id,
Patrick Venture8729eb92020-08-10 10:38:44 -070091 std::unique_ptr<DbusHelperInterface> helper,
Patrick Venture1df9e872020-10-08 15:35:01 -070092 const SensorProperties& settings, bool failed, const std::string& path,
James Feist98b704e2019-06-03 16:24:53 -070093 const std::shared_ptr<DbusPassiveRedundancy>& redundancy) :
Patrick Venturef8cb4642018-10-30 12:02:53 -070094 ReadInterface(),
Harvey.Wuf2efcbb2022-02-09 10:24:30 +080095 _signal(bus, getMatch(path).c_str(), dbusHandleSignal, this), _id(id),
Patrick Ventured0571302020-08-10 12:42:23 -070096 _helper(std::move(helper)), _failed(failed), path(path),
James Feist98b704e2019-06-03 16:24:53 -070097 redundancy(redundancy)
98
Patrick Venturef8cb4642018-10-30 12:02:53 -070099{
Patrick Venture863b9242018-03-08 08:29:23 -0800100 _scale = settings.scale;
Josh Lehan3e2f7582020-09-20 22:06:03 -0700101 _min = settings.min * std::pow(10.0, _scale);
102 _max = settings.max * std::pow(10.0, _scale);
Alex.Song8f73ad72021-10-07 00:18:27 +0800103 _available = settings.available;
104 _unavailableAsFailed = settings.unavailableAsFailed;
Josh Lehan3e2f7582020-09-20 22:06:03 -0700105
106 // Cache this type knowledge, to avoid repeated string comparison
107 _typeMargin = (type == "margin");
Alex.Song8f73ad72021-10-07 00:18:27 +0800108 _typeFan = (type == "fan");
Josh Lehan3e2f7582020-09-20 22:06:03 -0700109
110 // Force value to be stored, otherwise member would be uninitialized
111 updateValue(settings.value, true);
Patrick Venture863b9242018-03-08 08:29:23 -0800112}
113
114ReadReturn DbusPassive::read(void)
115{
116 std::lock_guard<std::mutex> guard(_lock);
117
Patrick Venture1df9e872020-10-08 15:35:01 -0700118 ReadReturn r = {_value, _updated};
Patrick Venture863b9242018-03-08 08:29:23 -0800119
120 return r;
121}
122
123void DbusPassive::setValue(double value)
124{
125 std::lock_guard<std::mutex> guard(_lock);
126
127 _value = value;
128 _updated = std::chrono::high_resolution_clock::now();
129}
130
James Feist36b7d8e2018-10-05 15:39:01 -0700131bool DbusPassive::getFailed(void) const
132{
James Feist98b704e2019-06-03 16:24:53 -0700133 if (redundancy)
134 {
135 const std::set<std::string>& failures = redundancy->getFailed();
136 if (failures.find(path) != failures.end())
137 {
138 return true;
139 }
140 }
James Feist4b36f262020-07-07 16:56:41 -0700141
Alex.Song8f73ad72021-10-07 00:18:27 +0800142 /*
143 * Unavailable thermal sensors, who are not present or
144 * power-state-not-matching, should not trigger the failSafe mode. For
145 * example, when a system stays at a powered-off state, its CPU Temp
146 * sensors will be unavailable, these unavailable sensors should not be
147 * treated as failed and trigger failSafe.
148 * This is important for systems whose Fans are always on.
149 */
150 if (!_typeFan && !_available && !_unavailableAsFailed)
151 {
152 return false;
153 }
154
Josh Lehan3e2f7582020-09-20 22:06:03 -0700155 // If a reading has came in,
156 // but its value bad in some way (determined by sensor type),
157 // indicate this sensor has failed,
158 // until another value comes in that is no longer bad.
159 // This is different from the overall _failed flag,
160 // which is set and cleared by other causes.
161 if (_badReading)
162 {
163 return true;
164 }
165
166 // If a reading has came in, and it is not a bad reading,
167 // but it indicates there is no more thermal margin left,
168 // that is bad, something is wrong with the PID loops,
169 // they are not cooling the system, enable failsafe mode also.
170 if (_marginHot)
171 {
172 return true;
173 }
174
Alex.Song8f73ad72021-10-07 00:18:27 +0800175 return _failed || !_available || !_functional;
James Feist36b7d8e2018-10-05 15:39:01 -0700176}
177
178void DbusPassive::setFailed(bool value)
179{
180 _failed = value;
181}
182
James Feist4b36f262020-07-07 16:56:41 -0700183void DbusPassive::setFunctional(bool value)
184{
185 _functional = value;
186}
187
Alex.Song8f73ad72021-10-07 00:18:27 +0800188void DbusPassive::setAvailable(bool value)
189{
190 _available = value;
191}
192
Patrick Venture863b9242018-03-08 08:29:23 -0800193int64_t DbusPassive::getScale(void)
194{
195 return _scale;
196}
197
Patrick Venture563a3562018-10-30 09:31:26 -0700198std::string DbusPassive::getID(void)
Patrick Venture863b9242018-03-08 08:29:23 -0800199{
200 return _id;
201}
202
James Feist75eb7692019-02-25 12:50:02 -0800203double DbusPassive::getMax(void)
204{
205 return _max;
206}
207
208double DbusPassive::getMin(void)
209{
210 return _min;
211}
212
Josh Lehan3e2f7582020-09-20 22:06:03 -0700213void DbusPassive::updateValue(double value, bool force)
214{
215 _badReading = false;
216
217 // Do not let a NAN, or other floating-point oddity, be used to update
218 // the value, as that indicates the sensor has no valid reading.
219 if (!(std::isfinite(value)))
220 {
221 _badReading = true;
222
223 // Do not continue with a bad reading, unless caller forcing
224 if (!force)
225 {
226 return;
227 }
228 }
229
230 value *= std::pow(10.0, _scale);
231
232 auto unscaled = value;
233 scaleSensorReading(_min, _max, value);
234
235 if (_typeMargin)
236 {
237 _marginHot = false;
238
239 // Unlike an absolute temperature sensor,
240 // where 0 degrees C is a good reading,
241 // a value received of 0 (or negative) margin is worrisome,
242 // and should be flagged.
243 // Either it indicates margin not calculated properly,
244 // or somebody forgot to set the margin-zero setpoint,
245 // or the system is really overheating that much.
246 // This is a different condition from _failed
247 // and _badReading, so it merits its own flag.
248 // The sensor has not failed, the reading is good, but the zone
249 // still needs to know that it should go to failsafe mode.
250 if (unscaled <= 0.0)
251 {
252 _marginHot = true;
253 }
254 }
255
256 setValue(value);
257}
258
Patrick Williamsb228bc32022-07-22 19:26:56 -0500259int handleSensorValue(sdbusplus::message_t& msg, DbusPassive* owner)
Patrick Venture863b9242018-03-08 08:29:23 -0800260{
Patrick Venture863b9242018-03-08 08:29:23 -0800261 std::string msgSensor;
James Feist1f802f52019-02-08 13:51:43 -0800262 std::map<std::string, std::variant<int64_t, double, bool>> msgData;
Patrick Ventured0c75662018-06-12 19:03:21 -0700263
264 msg.read(msgSensor, msgData);
Patrick Venture863b9242018-03-08 08:29:23 -0800265
266 if (msgSensor == "xyz.openbmc_project.Sensor.Value")
267 {
268 auto valPropMap = msgData.find("Value");
269 if (valPropMap != msgData.end())
270 {
James Feist1f802f52019-02-08 13:51:43 -0800271 double value =
272 std::visit(VariantToDoubleVisitor(), valPropMap->second);
Patrick Venture863b9242018-03-08 08:29:23 -0800273
Josh Lehan3e2f7582020-09-20 22:06:03 -0700274 owner->updateValue(value, false);
Patrick Venture863b9242018-03-08 08:29:23 -0800275 }
276 }
James Feist36b7d8e2018-10-05 15:39:01 -0700277 else if (msgSensor == "xyz.openbmc_project.Sensor.Threshold.Critical")
278 {
279 auto criticalAlarmLow = msgData.find("CriticalAlarmLow");
280 auto criticalAlarmHigh = msgData.find("CriticalAlarmHigh");
281 if (criticalAlarmHigh == msgData.end() &&
282 criticalAlarmLow == msgData.end())
283 {
284 return 0;
285 }
286
287 bool asserted = false;
288 if (criticalAlarmLow != msgData.end())
289 {
James Feist1f802f52019-02-08 13:51:43 -0800290 asserted = std::get<bool>(criticalAlarmLow->second);
James Feist36b7d8e2018-10-05 15:39:01 -0700291 }
292
293 // checking both as in theory you could de-assert one threshold and
294 // assert the other at the same moment
295 if (!asserted && criticalAlarmHigh != msgData.end())
296 {
James Feist1f802f52019-02-08 13:51:43 -0800297 asserted = std::get<bool>(criticalAlarmHigh->second);
James Feist36b7d8e2018-10-05 15:39:01 -0700298 }
299 owner->setFailed(asserted);
300 }
Alex.Song8f73ad72021-10-07 00:18:27 +0800301 else if (msgSensor == "xyz.openbmc_project.State.Decorator.Availability")
302 {
303 auto available = msgData.find("Available");
304 if (available == msgData.end())
305 {
306 return 0;
307 }
308 bool asserted = std::get<bool>(available->second);
309 owner->setAvailable(asserted);
310 if (!asserted)
311 {
312 // A thermal controller will continue its PID calculation and not
313 // trigger a 'failsafe' when some inputs are unavailable.
314 // So, forced to clear the value here to prevent a historical
315 // value to participate in a latter PID calculation.
316 owner->updateValue(std::numeric_limits<double>::quiet_NaN(), true);
317 }
318 }
James Feist4b36f262020-07-07 16:56:41 -0700319 else if (msgSensor ==
320 "xyz.openbmc_project.State.Decorator.OperationalStatus")
321 {
322 auto functional = msgData.find("Functional");
323 if (functional == msgData.end())
324 {
325 return 0;
326 }
327 bool asserted = std::get<bool>(functional->second);
328 owner->setFunctional(asserted);
329 }
Patrick Venture863b9242018-03-08 08:29:23 -0800330
331 return 0;
332}
Patrick Ventured0c75662018-06-12 19:03:21 -0700333
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800334int dbusHandleSignal(sd_bus_message* msg, void* usrData,
335 [[maybe_unused]] sd_bus_error* err)
Patrick Ventured0c75662018-06-12 19:03:21 -0700336{
Patrick Williamsb228bc32022-07-22 19:26:56 -0500337 auto sdbpMsg = sdbusplus::message_t(msg);
Patrick Ventured0c75662018-06-12 19:03:21 -0700338 DbusPassive* obj = static_cast<DbusPassive*>(usrData);
339
Patrick Venture7af157b2018-10-30 11:24:40 -0700340 return handleSensorValue(sdbpMsg, obj);
Patrick Ventured0c75662018-06-12 19:03:21 -0700341}
Patrick Venturea0764872020-08-08 07:48:43 -0700342
343} // namespace pid_control