blob: c483665c57a761cc5595e4438eda12bfe52381ee [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"
4
Brad Bishope228acc2017-01-06 15:29:35 -05005/** @class Thresholds
6 * @brief Threshold type traits.
7 *
8 * @tparam T - The threshold type.
9 */
10template <typename T>
11struct Thresholds
12{
13 static void fail()
14 {
15 static_assert(sizeof(Thresholds) == -1, "Unsupported Threshold type");
16 }
17};
18
19/**@brief Thresholds specialization for warning thresholds. */
20template <>
21struct Thresholds<WarningObject>
22{
23 static constexpr InterfaceType type = InterfaceType::WARN;
24 static constexpr const char* envLo = "WARNLO";
25 static constexpr const char* envHi = "WARNHI";
Saqib Khan973886d2017-03-15 14:01:16 -050026 static int64_t (WarningObject::*const setLo)(int64_t);
27 static int64_t (WarningObject::*const setHi)(int64_t);
28 static int64_t (WarningObject::*const getLo)() const;
29 static int64_t (WarningObject::*const getHi)() const;
30 static bool (WarningObject::*const alarmLo)(bool);
31 static bool (WarningObject::*const alarmHi)(bool);
Brad Bishope228acc2017-01-06 15:29:35 -050032};
33
34/**@brief Thresholds specialization for critical thresholds. */
35template <>
36struct Thresholds<CriticalObject>
37{
38 static constexpr InterfaceType type = InterfaceType::CRIT;
39 static constexpr const char* envLo = "CRITLO";
40 static constexpr const char* envHi = "CRITHI";
Saqib Khan973886d2017-03-15 14:01:16 -050041 static int64_t (CriticalObject::*const setLo)(int64_t);
42 static int64_t (CriticalObject::*const setHi)(int64_t);
43 static int64_t (CriticalObject::*const getLo)() const;
44 static int64_t (CriticalObject::*const getHi)() const;
45 static bool (CriticalObject::*const alarmLo)(bool);
46 static bool (CriticalObject::*const alarmHi)(bool);
Brad Bishope228acc2017-01-06 15:29:35 -050047};
48
49/** @brief checkThresholds
50 *
51 * Compare a sensor reading to threshold values and set the
52 * appropriate alarm property if bounds are exceeded.
53 *
54 * @tparam T - The threshold type.
55 *
56 * @param[in] iface - An sdbusplus server threshold instance.
57 * @param[in] value - The sensor reading to compare to thresholds.
58 */
59template <typename T>
60void checkThresholds(std::experimental::any& iface, int64_t value)
61{
Patrick Venture043d3232018-08-31 10:10:53 -070062 auto realIface = std::experimental::any_cast<std::shared_ptr<T>>(iface);
Brad Bishope228acc2017-01-06 15:29:35 -050063 auto lo = (*realIface.*Thresholds<T>::getLo)();
64 auto hi = (*realIface.*Thresholds<T>::getHi)();
Chiabing Leefe17eff2017-10-30 10:51:00 +080065 (*realIface.*Thresholds<T>::alarmLo)(value <= lo);
66 (*realIface.*Thresholds<T>::alarmHi)(value >= hi);
Brad Bishope228acc2017-01-06 15:29:35 -050067}
68
69/** @brief addThreshold
70 *
71 * Look for a configured threshold value in the environment and
72 * create an sdbusplus server threshold if found.
73 *
74 * @tparam T - The threshold type.
75 *
Matt Spinlerccfc77b2017-10-12 16:49:24 -050076 * @param[in] sensorType - sensor type, like 'temp'
77 * @param[in] sensorID - sensor ID, like '5'
Brad Bishope228acc2017-01-06 15:29:35 -050078 * @param[in] value - The sensor reading.
79 * @param[in] info - The sdbusplus server connection and interfaces.
80 */
81template <typename T>
Patrick Venture043d3232018-08-31 10:10:53 -070082auto addThreshold(const std::string& sensorType, const std::string& sensorID,
83 int64_t value, ObjectInfo& info)
Brad Bishope228acc2017-01-06 15:29:35 -050084{
Brad Bishop30dbcee2017-01-18 07:55:42 -050085 static constexpr bool deferSignals = true;
86
Brad Bishope228acc2017-01-06 15:29:35 -050087 auto& bus = *std::get<sdbusplus::bus::bus*>(info);
88 auto& objPath = std::get<std::string>(info);
89 auto& obj = std::get<Object>(info);
90 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 {
Brad Bishop30dbcee2017-01-18 07:55:42 -050096 iface = std::make_shared<T>(bus, objPath.c_str(), deferSignals);
Matt Spinler325456c2017-10-12 16:20:38 -050097 auto lo = stoll(tLo);
98 auto hi = stoll(tHi);
Brad Bishope228acc2017-01-06 15:29:35 -050099 (*iface.*Thresholds<T>::setLo)(lo);
100 (*iface.*Thresholds<T>::setHi)(hi);
Chiabing Leefe17eff2017-10-30 10:51:00 +0800101 (*iface.*Thresholds<T>::alarmLo)(value <= lo);
102 (*iface.*Thresholds<T>::alarmHi)(value >= hi);
Brad Bishope228acc2017-01-06 15:29:35 -0500103 auto type = Thresholds<T>::type;
104 obj[type] = iface;
105 }
106
107 return iface;
108}
109
110// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4