Brad Bishop | e228acc | 2017-01-06 15:29:35 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Venture | 7a5285d | 2018-04-17 19:15:05 -0700 | [diff] [blame] | 3 | #include "env.hpp" |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 4 | #include "interface.hpp" |
| 5 | |
| 6 | #include <cmath> |
Patrick Venture | 7a5285d | 2018-04-17 19:15:05 -0700 | [diff] [blame] | 7 | |
Brad Bishop | e228acc | 2017-01-06 15:29:35 -0500 | [diff] [blame] | 8 | /** @class Thresholds |
| 9 | * @brief Threshold type traits. |
| 10 | * |
| 11 | * @tparam T - The threshold type. |
| 12 | */ |
| 13 | template <typename T> |
| 14 | struct 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. */ |
| 23 | template <> |
| 24 | struct 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 Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 29 | 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 Khan | 973886d | 2017-03-15 14:01:16 -0500 | [diff] [blame] | 33 | static bool (WarningObject::*const alarmLo)(bool); |
| 34 | static bool (WarningObject::*const alarmHi)(bool); |
Duke Du | 7376909 | 2021-04-14 15:35:21 +0800 | [diff] [blame] | 35 | static bool (WarningObject::*const getAlarmLow)() const; |
| 36 | static bool (WarningObject::*const getAlarmHigh)() const; |
| 37 | static void (WarningObject::*const assertLowSignal)(SensorValueType); |
| 38 | static void (WarningObject::*const assertHighSignal)(SensorValueType); |
| 39 | static void (WarningObject::*const deassertLowSignal)(SensorValueType); |
| 40 | static void (WarningObject::*const deassertHighSignal)(SensorValueType); |
Brad Bishop | e228acc | 2017-01-06 15:29:35 -0500 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | /**@brief Thresholds specialization for critical thresholds. */ |
| 44 | template <> |
| 45 | struct Thresholds<CriticalObject> |
| 46 | { |
| 47 | static constexpr InterfaceType type = InterfaceType::CRIT; |
| 48 | static constexpr const char* envLo = "CRITLO"; |
| 49 | static constexpr const char* envHi = "CRITHI"; |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 50 | static SensorValueType (CriticalObject::*const setLo)(SensorValueType); |
| 51 | static SensorValueType (CriticalObject::*const setHi)(SensorValueType); |
| 52 | static SensorValueType (CriticalObject::*const getLo)() const; |
| 53 | static SensorValueType (CriticalObject::*const getHi)() const; |
Saqib Khan | 973886d | 2017-03-15 14:01:16 -0500 | [diff] [blame] | 54 | static bool (CriticalObject::*const alarmLo)(bool); |
| 55 | static bool (CriticalObject::*const alarmHi)(bool); |
Duke Du | 7376909 | 2021-04-14 15:35:21 +0800 | [diff] [blame] | 56 | static bool (CriticalObject::*const getAlarmLow)() const; |
| 57 | static bool (CriticalObject::*const getAlarmHigh)() const; |
| 58 | static void (CriticalObject::*const assertLowSignal)(SensorValueType); |
| 59 | static void (CriticalObject::*const assertHighSignal)(SensorValueType); |
| 60 | static void (CriticalObject::*const deassertLowSignal)(SensorValueType); |
| 61 | static void (CriticalObject::*const deassertHighSignal)(SensorValueType); |
Brad Bishop | e228acc | 2017-01-06 15:29:35 -0500 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | /** @brief checkThresholds |
| 65 | * |
| 66 | * Compare a sensor reading to threshold values and set the |
| 67 | * appropriate alarm property if bounds are exceeded. |
| 68 | * |
| 69 | * @tparam T - The threshold type. |
| 70 | * |
| 71 | * @param[in] iface - An sdbusplus server threshold instance. |
| 72 | * @param[in] value - The sensor reading to compare to thresholds. |
| 73 | */ |
| 74 | template <typename T> |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 75 | void checkThresholds(std::any& iface, SensorValueType value) |
Brad Bishop | e228acc | 2017-01-06 15:29:35 -0500 | [diff] [blame] | 76 | { |
William A. Kennington III | 4cbdfef | 2018-10-18 19:19:51 -0700 | [diff] [blame] | 77 | auto realIface = std::any_cast<std::shared_ptr<T>>(iface); |
Brad Bishop | e228acc | 2017-01-06 15:29:35 -0500 | [diff] [blame] | 78 | auto lo = (*realIface.*Thresholds<T>::getLo)(); |
| 79 | auto hi = (*realIface.*Thresholds<T>::getHi)(); |
Duke Du | 7376909 | 2021-04-14 15:35:21 +0800 | [diff] [blame] | 80 | auto alarmLowState = (*realIface.*Thresholds<T>::getAlarmLow)(); |
| 81 | auto alarmHighState = (*realIface.*Thresholds<T>::getAlarmHigh)(); |
Chiabing Lee | fe17eff | 2017-10-30 10:51:00 +0800 | [diff] [blame] | 82 | (*realIface.*Thresholds<T>::alarmLo)(value <= lo); |
| 83 | (*realIface.*Thresholds<T>::alarmHi)(value >= hi); |
Duke Du | 7376909 | 2021-04-14 15:35:21 +0800 | [diff] [blame] | 84 | if (alarmLowState != (value <= lo)) |
| 85 | { |
| 86 | if (value <= lo) |
| 87 | { |
| 88 | (*realIface.*Thresholds<T>::assertLowSignal)(value); |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | (*realIface.*Thresholds<T>::deassertLowSignal)(value); |
| 93 | } |
| 94 | } |
| 95 | if (alarmHighState != (value >= hi)) |
| 96 | { |
| 97 | if (value >= hi) |
| 98 | { |
| 99 | (*realIface.*Thresholds<T>::assertHighSignal)(value); |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | (*realIface.*Thresholds<T>::deassertHighSignal)(value); |
| 104 | } |
| 105 | } |
Brad Bishop | e228acc | 2017-01-06 15:29:35 -0500 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** @brief addThreshold |
| 109 | * |
| 110 | * Look for a configured threshold value in the environment and |
| 111 | * create an sdbusplus server threshold if found. |
| 112 | * |
| 113 | * @tparam T - The threshold type. |
| 114 | * |
Matt Spinler | ccfc77b | 2017-10-12 16:49:24 -0500 | [diff] [blame] | 115 | * @param[in] sensorType - sensor type, like 'temp' |
| 116 | * @param[in] sensorID - sensor ID, like '5' |
Brad Bishop | e228acc | 2017-01-06 15:29:35 -0500 | [diff] [blame] | 117 | * @param[in] value - The sensor reading. |
| 118 | * @param[in] info - The sdbusplus server connection and interfaces. |
| 119 | */ |
| 120 | template <typename T> |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 121 | auto addThreshold(const std::string& sensorType, const std::string& sensorID, |
Duke Du | 7376909 | 2021-04-14 15:35:21 +0800 | [diff] [blame] | 122 | SensorValueType value, ObjectInfo& info, int64_t scale) |
Brad Bishop | e228acc | 2017-01-06 15:29:35 -0500 | [diff] [blame] | 123 | { |
Brad Bishop | e228acc | 2017-01-06 15:29:35 -0500 | [diff] [blame] | 124 | auto& objPath = std::get<std::string>(info); |
Patrick Venture | 6206723 | 2019-06-19 17:39:33 -0700 | [diff] [blame] | 125 | auto& obj = std::get<InterfaceMap>(info); |
Brad Bishop | e228acc | 2017-01-06 15:29:35 -0500 | [diff] [blame] | 126 | std::shared_ptr<T> iface; |
Matt Spinler | ccfc77b | 2017-10-12 16:49:24 -0500 | [diff] [blame] | 127 | |
Patrick Venture | 7a5285d | 2018-04-17 19:15:05 -0700 | [diff] [blame] | 128 | auto tLo = env::getEnv(Thresholds<T>::envLo, sensorType, sensorID); |
| 129 | auto tHi = env::getEnv(Thresholds<T>::envHi, sensorType, sensorID); |
Thang Q. Nguyen | 04da055 | 2021-07-19 04:24:31 +0000 | [diff] [blame] | 130 | if (!tLo.empty() || !tHi.empty()) |
Brad Bishop | e228acc | 2017-01-06 15:29:35 -0500 | [diff] [blame] | 131 | { |
Patrick Williams | ad6043f | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 132 | auto& bus = *std::get<sdbusplus::bus_t*>(info); |
Patrick Venture | 685efa1 | 2018-10-12 18:00:13 -0700 | [diff] [blame] | 133 | |
Patrick Williams | d273b1e | 2022-03-30 21:55:44 -0500 | [diff] [blame] | 134 | iface = std::make_shared<T>(bus, objPath.c_str(), |
| 135 | T::action::emit_no_signals); |
Thang Q. Nguyen | 04da055 | 2021-07-19 04:24:31 +0000 | [diff] [blame] | 136 | if (!tLo.empty()) |
Duke Du | 7376909 | 2021-04-14 15:35:21 +0800 | [diff] [blame] | 137 | { |
Thang Q. Nguyen | 04da055 | 2021-07-19 04:24:31 +0000 | [diff] [blame] | 138 | auto lo = stod(tLo) * std::pow(10, scale); |
| 139 | (*iface.*Thresholds<T>::setLo)(lo); |
| 140 | auto alarmLowState = (*iface.*Thresholds<T>::getAlarmLow)(); |
| 141 | (*iface.*Thresholds<T>::alarmLo)(value <= lo); |
| 142 | if (alarmLowState != (value <= lo)) |
Duke Du | 7376909 | 2021-04-14 15:35:21 +0800 | [diff] [blame] | 143 | { |
Thang Q. Nguyen | 04da055 | 2021-07-19 04:24:31 +0000 | [diff] [blame] | 144 | if (value <= lo) |
| 145 | { |
| 146 | (*iface.*Thresholds<T>::assertLowSignal)(value); |
| 147 | } |
| 148 | else |
| 149 | { |
| 150 | (*iface.*Thresholds<T>::deassertLowSignal)(value); |
| 151 | } |
Duke Du | 7376909 | 2021-04-14 15:35:21 +0800 | [diff] [blame] | 152 | } |
| 153 | } |
Thang Q. Nguyen | 04da055 | 2021-07-19 04:24:31 +0000 | [diff] [blame] | 154 | if (!tHi.empty()) |
Duke Du | 7376909 | 2021-04-14 15:35:21 +0800 | [diff] [blame] | 155 | { |
Thang Q. Nguyen | 04da055 | 2021-07-19 04:24:31 +0000 | [diff] [blame] | 156 | auto hi = stod(tHi) * std::pow(10, scale); |
| 157 | (*iface.*Thresholds<T>::setHi)(hi); |
| 158 | auto alarmHighState = (*iface.*Thresholds<T>::getAlarmHigh)(); |
| 159 | (*iface.*Thresholds<T>::alarmHi)(value >= hi); |
| 160 | if (alarmHighState != (value >= hi)) |
Duke Du | 7376909 | 2021-04-14 15:35:21 +0800 | [diff] [blame] | 161 | { |
Thang Q. Nguyen | 04da055 | 2021-07-19 04:24:31 +0000 | [diff] [blame] | 162 | if (value >= hi) |
| 163 | { |
| 164 | (*iface.*Thresholds<T>::assertHighSignal)(value); |
| 165 | } |
| 166 | else |
| 167 | { |
| 168 | (*iface.*Thresholds<T>::deassertHighSignal)(value); |
| 169 | } |
Duke Du | 7376909 | 2021-04-14 15:35:21 +0800 | [diff] [blame] | 170 | } |
| 171 | } |
Brad Bishop | e228acc | 2017-01-06 15:29:35 -0500 | [diff] [blame] | 172 | auto type = Thresholds<T>::type; |
| 173 | obj[type] = iface; |
| 174 | } |
| 175 | |
| 176 | return iface; |
| 177 | } |
| 178 | |
| 179 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |