blob: fa35ef525878efdc30c76d82837365dd372763e8 [file] [log] [blame]
James Feist8fd8a582018-11-16 11:10:46 -08001#pragma once
2
Patrick Ventureca44b2f2019-10-31 11:02:26 -07003#include "Thresholds.hpp"
4
Patrick Venturefd6ba732019-10-31 14:27:39 -07005#include <limits>
6#include <memory>
James Feist8fd8a582018-11-16 11:10:46 -08007#include <sdbusplus/asio/object_server.hpp>
Patrick Venturefd6ba732019-10-31 14:27:39 -07008#include <string>
9#include <vector>
James Feist8fd8a582018-11-16 11:10:46 -080010
James Feist1169eb42018-10-31 10:08:47 -070011constexpr size_t sensorFailedPollTimeMs = 5000;
James Feista5e58722019-04-22 14:43:11 -070012
13constexpr const char* sensorValueInterface = "xyz.openbmc_project.Sensor.Value";
James Feist8fd8a582018-11-16 11:10:46 -080014struct Sensor
15{
James Feist930fcde2019-05-28 12:58:43 -070016 Sensor(const std::string& name,
James Feistd8705872019-02-08 13:26:09 -080017 std::vector<thresholds::Threshold>&& thresholdData,
18 const std::string& configurationPath, const std::string& objectType,
James Feistce3fca42018-11-21 12:58:24 -080019 const double max, const double min) :
James Feistdc6c55f2018-10-31 12:53:20 -070020 name(name),
James Feist930fcde2019-05-28 12:58:43 -070021 configurationPath(configurationPath), objectType(objectType),
Brad Bishopfbb44ad2019-11-08 09:42:37 -050022 maxValue(max), minValue(min), thresholds(std::move(thresholdData)),
James Feist9f16b4b2019-06-04 14:59:42 -070023 hysteresis((max - min) * 0.01)
James Feistdc6c55f2018-10-31 12:53:20 -070024 {
25 }
James Feist8fd8a582018-11-16 11:10:46 -080026 virtual ~Sensor() = default;
James Feistce3fca42018-11-21 12:58:24 -080027 virtual void checkThresholds(void) = 0;
James Feistdc6c55f2018-10-31 12:53:20 -070028 std::string name;
James Feistce3fca42018-11-21 12:58:24 -080029 std::string configurationPath;
30 std::string objectType;
31 double maxValue;
32 double minValue;
James Feist8fd8a582018-11-16 11:10:46 -080033 std::vector<thresholds::Threshold> thresholds;
34 std::shared_ptr<sdbusplus::asio::dbus_interface> sensorInterface;
35 std::shared_ptr<sdbusplus::asio::dbus_interface> thresholdInterfaceWarning;
36 std::shared_ptr<sdbusplus::asio::dbus_interface> thresholdInterfaceCritical;
James Feist078f2322019-03-08 11:09:05 -080037 std::shared_ptr<sdbusplus::asio::dbus_interface> association;
James Feist8fd8a582018-11-16 11:10:46 -080038 double value = std::numeric_limits<double>::quiet_NaN();
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +053039 bool overriddenState = false;
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053040 bool internalSet = false;
James Feist9f16b4b2019-06-04 14:59:42 -070041 double hysteresis;
James Feistce3fca42018-11-21 12:58:24 -080042
James Feistd8705872019-02-08 13:26:09 -080043 int setSensorValue(const double& newValue, double& oldValue)
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053044 {
Richard Marian Thomaiyaraf6b87c2019-04-03 23:54:28 +053045 if (!internalSet)
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053046 {
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053047 oldValue = newValue;
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +053048 overriddenState = true;
49 // check thresholds for external set
50 value = newValue;
51 checkThresholds();
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053052 }
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +053053 else if (!overriddenState)
Richard Marian Thomaiyaraf6b87c2019-04-03 23:54:28 +053054 {
55 oldValue = newValue;
56 }
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053057 return 1;
58 }
James Feistce3fca42018-11-21 12:58:24 -080059
60 void
James Feistd8705872019-02-08 13:26:09 -080061 setInitialProperties(std::shared_ptr<sdbusplus::asio::connection>& conn)
James Feistce3fca42018-11-21 12:58:24 -080062 {
James Feist82bac4c2019-03-11 11:16:53 -070063 createAssociation(association, configurationPath);
AppaRao Pulic82213c2020-02-27 01:24:58 +053064
James Feistce3fca42018-11-21 12:58:24 -080065 sensorInterface->register_property("MaxValue", maxValue);
66 sensorInterface->register_property("MinValue", minValue);
67 sensorInterface->register_property(
James Feistd8705872019-02-08 13:26:09 -080068 "Value", value, [&](const double& newValue, double& oldValue) {
James Feistce3fca42018-11-21 12:58:24 -080069 return setSensorValue(newValue, oldValue);
70 });
James Feistd8705872019-02-08 13:26:09 -080071 for (auto& threshold : thresholds)
James Feistce3fca42018-11-21 12:58:24 -080072 {
73 std::shared_ptr<sdbusplus::asio::dbus_interface> iface;
74 std::string level;
75 std::string alarm;
76 if (threshold.level == thresholds::Level::CRITICAL)
77 {
78 iface = thresholdInterfaceCritical;
79 if (threshold.direction == thresholds::Direction::HIGH)
80 {
81 level = "CriticalHigh";
82 alarm = "CriticalAlarmHigh";
83 }
84 else
85 {
86 level = "CriticalLow";
87 alarm = "CriticalAlarmLow";
88 }
89 }
90 else if (threshold.level == thresholds::Level::WARNING)
91 {
92 iface = thresholdInterfaceWarning;
93 if (threshold.direction == thresholds::Direction::HIGH)
94 {
95 level = "WarningHigh";
96 alarm = "WarningAlarmHigh";
97 }
98 else
99 {
100 level = "WarningLow";
101 alarm = "WarningAlarmLow";
102 }
103 }
104 else
105 {
106 std::cerr << "Unknown threshold level" << threshold.level
107 << "\n";
108 continue;
109 }
110 if (!iface)
111 {
112 std::cout << "trying to set uninitialized interface\n";
113 continue;
114 }
115 iface->register_property(
116 level, threshold.value,
James Feistd8705872019-02-08 13:26:09 -0800117 [&](const double& request, double& oldValue) {
James Feistce3fca42018-11-21 12:58:24 -0800118 oldValue = request; // todo, just let the config do this?
119 threshold.value = request;
120 thresholds::persistThreshold(configurationPath, objectType,
James Feista222ba72019-03-01 15:57:51 -0800121 threshold, conn,
122 thresholds.size());
James Feistce3fca42018-11-21 12:58:24 -0800123 return 1;
124 });
125 iface->register_property(alarm, false);
126 }
127 if (!sensorInterface->initialize())
128 {
129 std::cerr << "error initializing value interface\n";
130 }
131 if (thresholdInterfaceWarning &&
132 !thresholdInterfaceWarning->initialize())
133 {
134 std::cerr << "error initializing warning threshold interface\n";
135 }
136
137 if (thresholdInterfaceCritical &&
138 !thresholdInterfaceCritical->initialize())
139 {
140 std::cerr << "error initializing critical threshold interface\n";
141 }
142 }
143
James Feistd8705872019-02-08 13:26:09 -0800144 void updateValue(const double& newValue)
James Feistce3fca42018-11-21 12:58:24 -0800145 {
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530146 // Ignore if overriding is enabled
147 if (!overriddenState)
148 {
149 // Indicate that it is internal set call
150 internalSet = true;
Josh Lehan432d1ed2019-10-16 12:23:31 -0700151 if (!(sensorInterface->set_property("Value", newValue)))
152 {
153 std::cerr << "error setting property to " << newValue << "\n";
154 }
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530155 internalSet = false;
James Feist52497fd2019-06-07 13:01:33 -0700156 double diff = std::abs(value - newValue);
157 if (std::isnan(diff) || diff > hysteresis)
James Feist9f16b4b2019-06-04 14:59:42 -0700158 {
159 value = newValue;
James Feist9f16b4b2019-06-04 14:59:42 -0700160 }
James Feist19cb01d2019-09-16 13:43:52 -0700161 checkThresholds();
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530162 }
James Feistce3fca42018-11-21 12:58:24 -0800163 }
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530164};