blob: 4d2fcffb81359d77961c80c86dcef4ab2ff5cbf1 [file] [log] [blame]
Brad Bishope228acc2017-01-06 15:29:35 -05001#pragma once
2
Patrick Venture7a5285d2018-04-17 19:15:05 -07003#include "env.hpp"
James Feistee73f5b2018-08-01 16:31:42 -07004#include "interface.hpp"
5
6#include <cmath>
Patrick Venture7a5285d2018-04-17 19:15:05 -07007
Brad Bishope228acc2017-01-06 15:29:35 -05008/** @class Thresholds
9 * @brief Threshold type traits.
10 *
11 * @tparam T - The threshold type.
12 */
13template <typename T>
14struct Thresholds
15{
16 static void fail()
17 {
18 static_assert(sizeof(Thresholds) == -1, "Unsupported Threshold type");
19 }
20};
21
22/**@brief Thresholds specialization for warning thresholds. */
23template <>
24struct Thresholds<WarningObject>
25{
26 static constexpr InterfaceType type = InterfaceType::WARN;
27 static constexpr const char* envLo = "WARNLO";
28 static constexpr const char* envHi = "WARNHI";
James Feistee73f5b2018-08-01 16:31:42 -070029 static SensorValueType (WarningObject::*const setLo)(SensorValueType);
30 static SensorValueType (WarningObject::*const setHi)(SensorValueType);
31 static SensorValueType (WarningObject::*const getLo)() const;
32 static SensorValueType (WarningObject::*const getHi)() const;
Saqib Khan973886d2017-03-15 14:01:16 -050033 static bool (WarningObject::*const alarmLo)(bool);
34 static bool (WarningObject::*const alarmHi)(bool);
Brad Bishope228acc2017-01-06 15:29:35 -050035};
36
37/**@brief Thresholds specialization for critical thresholds. */
38template <>
39struct Thresholds<CriticalObject>
40{
41 static constexpr InterfaceType type = InterfaceType::CRIT;
42 static constexpr const char* envLo = "CRITLO";
43 static constexpr const char* envHi = "CRITHI";
James Feistee73f5b2018-08-01 16:31:42 -070044 static SensorValueType (CriticalObject::*const setLo)(SensorValueType);
45 static SensorValueType (CriticalObject::*const setHi)(SensorValueType);
46 static SensorValueType (CriticalObject::*const getLo)() const;
47 static SensorValueType (CriticalObject::*const getHi)() const;
Saqib Khan973886d2017-03-15 14:01:16 -050048 static bool (CriticalObject::*const alarmLo)(bool);
49 static bool (CriticalObject::*const alarmHi)(bool);
Brad Bishope228acc2017-01-06 15:29:35 -050050};
51
52/** @brief checkThresholds
53 *
54 * Compare a sensor reading to threshold values and set the
55 * appropriate alarm property if bounds are exceeded.
56 *
57 * @tparam T - The threshold type.
58 *
59 * @param[in] iface - An sdbusplus server threshold instance.
60 * @param[in] value - The sensor reading to compare to thresholds.
61 */
62template <typename T>
James Feistee73f5b2018-08-01 16:31:42 -070063void checkThresholds(std::any& iface, SensorValueType value)
Brad Bishope228acc2017-01-06 15:29:35 -050064{
William A. Kennington III4cbdfef2018-10-18 19:19:51 -070065 auto realIface = std::any_cast<std::shared_ptr<T>>(iface);
Brad Bishope228acc2017-01-06 15:29:35 -050066 auto lo = (*realIface.*Thresholds<T>::getLo)();
67 auto hi = (*realIface.*Thresholds<T>::getHi)();
Chiabing Leefe17eff2017-10-30 10:51:00 +080068 (*realIface.*Thresholds<T>::alarmLo)(value <= lo);
69 (*realIface.*Thresholds<T>::alarmHi)(value >= hi);
Brad Bishope228acc2017-01-06 15:29:35 -050070}
71
72/** @brief addThreshold
73 *
74 * Look for a configured threshold value in the environment and
75 * create an sdbusplus server threshold if found.
76 *
77 * @tparam T - The threshold type.
78 *
Matt Spinlerccfc77b2017-10-12 16:49:24 -050079 * @param[in] sensorType - sensor type, like 'temp'
80 * @param[in] sensorID - sensor ID, like '5'
Brad Bishope228acc2017-01-06 15:29:35 -050081 * @param[in] value - The sensor reading.
82 * @param[in] info - The sdbusplus server connection and interfaces.
83 */
84template <typename T>
Patrick Venture043d3232018-08-31 10:10:53 -070085auto addThreshold(const std::string& sensorType, const std::string& sensorID,
James Feistee73f5b2018-08-01 16:31:42 -070086 int64_t value, ObjectInfo& info, int64_t scale)
Brad Bishope228acc2017-01-06 15:29:35 -050087{
Brad Bishope228acc2017-01-06 15:29:35 -050088 auto& objPath = std::get<std::string>(info);
Patrick Venture62067232019-06-19 17:39:33 -070089 auto& obj = std::get<InterfaceMap>(info);
Brad Bishope228acc2017-01-06 15:29:35 -050090 std::shared_ptr<T> iface;
Matt Spinlerccfc77b2017-10-12 16:49:24 -050091
Patrick Venture7a5285d2018-04-17 19:15:05 -070092 auto tLo = env::getEnv(Thresholds<T>::envLo, sensorType, sensorID);
93 auto tHi = env::getEnv(Thresholds<T>::envHi, sensorType, sensorID);
Brad Bishope228acc2017-01-06 15:29:35 -050094 if (!tLo.empty() && !tHi.empty())
95 {
Patrick Venture685efa12018-10-12 18:00:13 -070096 static constexpr bool deferSignals = true;
97 auto& bus = *std::get<sdbusplus::bus::bus*>(info);
98
Brad Bishop30dbcee2017-01-18 07:55:42 -050099 iface = std::make_shared<T>(bus, objPath.c_str(), deferSignals);
James Feistee73f5b2018-08-01 16:31:42 -0700100 auto lo = stod(tLo) * std::pow(10, scale);
101 auto hi = stod(tHi) * std::pow(10, scale);
Brad Bishope228acc2017-01-06 15:29:35 -0500102 (*iface.*Thresholds<T>::setLo)(lo);
103 (*iface.*Thresholds<T>::setHi)(hi);
Chiabing Leefe17eff2017-10-30 10:51:00 +0800104 (*iface.*Thresholds<T>::alarmLo)(value <= lo);
105 (*iface.*Thresholds<T>::alarmHi)(value >= hi);
Brad Bishope228acc2017-01-06 15:29:35 -0500106 auto type = Thresholds<T>::type;
107 obj[type] = iface;
108 }
109
110 return iface;
111}
112
113// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4