1 /**
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  */
16 #include "config.h"
17 
18 #include "dbuspassive.hpp"
19 
20 #include "dbushelper_interface.hpp"
21 #include "dbuspassiveredundancy.hpp"
22 #include "dbusutil.hpp"
23 #include "util.hpp"
24 
25 #include <sdbusplus/bus.hpp>
26 
27 #include <chrono>
28 #include <cmath>
29 #include <memory>
30 #include <mutex>
31 #include <string>
32 #include <variant>
33 
34 namespace pid_control
35 {
36 
37 std::unique_ptr<ReadInterface> DbusPassive::createDbusPassive(
38     sdbusplus::bus_t& bus, const std::string& type, const std::string& id,
39     std::unique_ptr<DbusHelperInterface> helper, const conf::SensorConfig* info,
40     const std::shared_ptr<DbusPassiveRedundancy>& redundancy)
41 {
42     if (helper == nullptr)
43     {
44         return nullptr;
45     }
46     if (!validType(type))
47     {
48         return nullptr;
49     }
50 
51     /* Need to get the scale and initial value */
52     /* service == busname */
53     std::string path;
54     if (info->readPath.empty())
55     {
56         path = getSensorPath(type, id);
57     }
58     else
59     {
60         path = info->readPath;
61     }
62 
63     SensorProperties settings;
64     bool failed;
65 
66     try
67     {
68         std::string service = helper->getService(sensorintf, path);
69 
70         helper->getProperties(service, path, &settings);
71         failed = helper->thresholdsAsserted(service, path);
72     }
73     catch (const std::exception& e)
74     {
75         return nullptr;
76     }
77 
78     /* if these values are zero, they're ignored. */
79     if (info->ignoreDbusMinMax)
80     {
81         settings.min = 0;
82         settings.max = 0;
83     }
84 
85     settings.unavailableAsFailed = info->unavailableAsFailed;
86 
87     return std::make_unique<DbusPassive>(bus, type, id, std::move(helper),
88                                          settings, failed, path, redundancy);
89 }
90 
91 DbusPassive::DbusPassive(
92     sdbusplus::bus_t& bus, const std::string& type, const std::string& id,
93     std::unique_ptr<DbusHelperInterface> helper,
94     const SensorProperties& settings, bool failed, const std::string& path,
95     const std::shared_ptr<DbusPassiveRedundancy>& redundancy) :
96     ReadInterface(),
97     _signal(bus, getMatch(path), dbusHandleSignal, this), _id(id),
98     _helper(std::move(helper)), _failed(failed), path(path),
99     redundancy(redundancy)
100 
101 {
102     _scale = settings.scale;
103     _min = settings.min * std::pow(10.0, _scale);
104     _max = settings.max * std::pow(10.0, _scale);
105     _available = settings.available;
106     _unavailableAsFailed = settings.unavailableAsFailed;
107 
108     // Cache this type knowledge, to avoid repeated string comparison
109     _typeMargin = (type == "margin");
110     _typeFan = (type == "fan");
111 
112     // Force value to be stored, otherwise member would be uninitialized
113     updateValue(settings.value, true);
114 }
115 
116 ReadReturn DbusPassive::read(void)
117 {
118     std::lock_guard<std::mutex> guard(_lock);
119 
120     ReadReturn r = {_value, _updated, _unscaled};
121 
122     return r;
123 }
124 
125 void DbusPassive::setValue(double value, double unscaled)
126 {
127     std::lock_guard<std::mutex> guard(_lock);
128 
129     _value = value;
130     _unscaled = unscaled;
131     _updated = std::chrono::high_resolution_clock::now();
132 }
133 
134 void DbusPassive::setValue(double value)
135 {
136     // First param is scaled, second param is unscaled, assume same here
137     setValue(value, value);
138 }
139 
140 bool DbusPassive::getFailed(void) const
141 {
142     if (redundancy)
143     {
144         const std::set<std::string>& failures = redundancy->getFailed();
145         if (failures.find(path) != failures.end())
146         {
147             return true;
148         }
149     }
150 
151     /*
152      * Unavailable thermal sensors, who are not present or
153      * power-state-not-matching, should not trigger the failSafe mode. For
154      * example, when a system stays at a powered-off state, its CPU Temp
155      * sensors will be unavailable, these unavailable sensors should not be
156      * treated as failed and trigger failSafe.
157      * This is important for systems whose Fans are always on.
158      */
159     if (!_typeFan && !_available && !_unavailableAsFailed)
160     {
161         return false;
162     }
163 
164     // If a reading has came in,
165     // but its value bad in some way (determined by sensor type),
166     // indicate this sensor has failed,
167     // until another value comes in that is no longer bad.
168     // This is different from the overall _failed flag,
169     // which is set and cleared by other causes.
170     if (_badReading)
171     {
172         return true;
173     }
174 
175     // If a reading has came in, and it is not a bad reading,
176     // but it indicates there is no more thermal margin left,
177     // that is bad, something is wrong with the PID loops,
178     // they are not cooling the system, enable failsafe mode also.
179     if (_marginHot)
180     {
181         return true;
182     }
183 
184     return _failed || !_available || !_functional;
185 }
186 
187 void DbusPassive::setFailed(bool value)
188 {
189     _failed = value;
190 }
191 
192 void DbusPassive::setFunctional(bool value)
193 {
194     _functional = value;
195 }
196 
197 void DbusPassive::setAvailable(bool value)
198 {
199     _available = value;
200 }
201 
202 int64_t DbusPassive::getScale(void)
203 {
204     return _scale;
205 }
206 
207 std::string DbusPassive::getID(void)
208 {
209     return _id;
210 }
211 
212 double DbusPassive::getMax(void)
213 {
214     return _max;
215 }
216 
217 double DbusPassive::getMin(void)
218 {
219     return _min;
220 }
221 
222 void DbusPassive::updateValue(double value, bool force)
223 {
224     _badReading = false;
225 
226     // Do not let a NAN, or other floating-point oddity, be used to update
227     // the value, as that indicates the sensor has no valid reading.
228     if (!(std::isfinite(value)))
229     {
230         _badReading = true;
231 
232         // Do not continue with a bad reading, unless caller forcing
233         if (!force)
234         {
235             return;
236         }
237     }
238 
239     value *= std::pow(10.0, _scale);
240 
241     auto unscaled = value;
242     scaleSensorReading(_min, _max, value);
243 
244     if (_typeMargin)
245     {
246         _marginHot = false;
247 
248         // Unlike an absolute temperature sensor,
249         // where 0 degrees C is a good reading,
250         // a value received of 0 (or negative) margin is worrisome,
251         // and should be flagged.
252         // Either it indicates margin not calculated properly,
253         // or somebody forgot to set the margin-zero setpoint,
254         // or the system is really overheating that much.
255         // This is a different condition from _failed
256         // and _badReading, so it merits its own flag.
257         // The sensor has not failed, the reading is good, but the zone
258         // still needs to know that it should go to failsafe mode.
259         if (unscaled <= 0.0)
260         {
261             _marginHot = true;
262         }
263     }
264 
265     setValue(value, unscaled);
266 }
267 
268 int handleSensorValue(sdbusplus::message_t& msg, DbusPassive* owner)
269 {
270     std::string msgSensor;
271     std::map<std::string, std::variant<int64_t, double, bool>> msgData;
272 
273     msg.read(msgSensor, msgData);
274 
275     if (msgSensor == "xyz.openbmc_project.Sensor.Value")
276     {
277         auto valPropMap = msgData.find("Value");
278         if (valPropMap != msgData.end())
279         {
280             double value = std::visit(VariantToDoubleVisitor(),
281                                       valPropMap->second);
282 
283             owner->updateValue(value, false);
284         }
285     }
286     else if (msgSensor == "xyz.openbmc_project.Sensor.Threshold.Critical")
287     {
288         auto criticalAlarmLow = msgData.find("CriticalAlarmLow");
289         auto criticalAlarmHigh = msgData.find("CriticalAlarmHigh");
290         if (criticalAlarmHigh == msgData.end() &&
291             criticalAlarmLow == msgData.end())
292         {
293             return 0;
294         }
295 
296         bool asserted = false;
297         if (criticalAlarmLow != msgData.end())
298         {
299             asserted = std::get<bool>(criticalAlarmLow->second);
300         }
301 
302         // checking both as in theory you could de-assert one threshold and
303         // assert the other at the same moment
304         if (!asserted && criticalAlarmHigh != msgData.end())
305         {
306             asserted = std::get<bool>(criticalAlarmHigh->second);
307         }
308         owner->setFailed(asserted);
309     }
310 #ifdef UNC_FAILSAFE
311     else if (msgSensor == "xyz.openbmc_project.Sensor.Threshold.Warning")
312     {
313         auto warningAlarmHigh = msgData.find("WarningAlarmHigh");
314         if (warningAlarmHigh == msgData.end())
315         {
316             return 0;
317         }
318 
319         bool asserted = false;
320         if (warningAlarmHigh != msgData.end())
321         {
322             asserted = std::get<bool>(warningAlarmHigh->second);
323         }
324         owner->setFailed(asserted);
325     }
326 #endif
327     else if (msgSensor == "xyz.openbmc_project.State.Decorator.Availability")
328     {
329         auto available = msgData.find("Available");
330         if (available == msgData.end())
331         {
332             return 0;
333         }
334         bool asserted = std::get<bool>(available->second);
335         owner->setAvailable(asserted);
336         if (!asserted)
337         {
338             // A thermal controller will continue its PID calculation and not
339             // trigger a 'failsafe' when some inputs are unavailable.
340             // So, forced to clear the value here to prevent a historical
341             // value to participate in a latter PID calculation.
342             owner->updateValue(std::numeric_limits<double>::quiet_NaN(), true);
343         }
344     }
345     else if (msgSensor ==
346              "xyz.openbmc_project.State.Decorator.OperationalStatus")
347     {
348         auto functional = msgData.find("Functional");
349         if (functional == msgData.end())
350         {
351             return 0;
352         }
353         bool asserted = std::get<bool>(functional->second);
354         owner->setFunctional(asserted);
355     }
356 
357     return 0;
358 }
359 
360 int dbusHandleSignal(sd_bus_message* msg, void* usrData,
361                      [[maybe_unused]] sd_bus_error* err)
362 {
363     auto sdbpMsg = sdbusplus::message_t(msg);
364     DbusPassive* obj = static_cast<DbusPassive*>(usrData);
365 
366     return handleSensorValue(sdbpMsg, obj);
367 }
368 
369 } // namespace pid_control
370