blob: 19c37335ef4c67977897f4f42e6d87ca27e19aeb [file] [log] [blame]
James Feist8fd8a582018-11-16 11:10:46 -08001#pragma once
2
3#include <Thresholds.hpp>
4#include <sdbusplus/asio/object_server.hpp>
5
James Feist1169eb42018-10-31 10:08:47 -07006constexpr size_t sensorFailedPollTimeMs = 5000;
James Feista5e58722019-04-22 14:43:11 -07007
8constexpr const char* sensorValueInterface = "xyz.openbmc_project.Sensor.Value";
James Feist8fd8a582018-11-16 11:10:46 -08009struct Sensor
10{
James Feistd8705872019-02-08 13:26:09 -080011 Sensor(const std::string& name, const std::string& path,
12 std::vector<thresholds::Threshold>&& thresholdData,
13 const std::string& configurationPath, const std::string& objectType,
James Feistce3fca42018-11-21 12:58:24 -080014 const double max, const double min) :
James Feistdc6c55f2018-10-31 12:53:20 -070015 name(name),
James Feista222ba72019-03-01 15:57:51 -080016 path(path), configurationPath(configurationPath),
17 objectType(objectType), thresholds(std::move(thresholdData)),
18 maxValue(max), minValue(min)
James Feistdc6c55f2018-10-31 12:53:20 -070019 {
20 }
James Feist8fd8a582018-11-16 11:10:46 -080021 virtual ~Sensor() = default;
James Feistce3fca42018-11-21 12:58:24 -080022 virtual void checkThresholds(void) = 0;
James Feistdc6c55f2018-10-31 12:53:20 -070023 std::string name;
24 std::string path;
James Feistce3fca42018-11-21 12:58:24 -080025 std::string configurationPath;
26 std::string objectType;
27 double maxValue;
28 double minValue;
James Feist8fd8a582018-11-16 11:10:46 -080029 std::vector<thresholds::Threshold> thresholds;
30 std::shared_ptr<sdbusplus::asio::dbus_interface> sensorInterface;
31 std::shared_ptr<sdbusplus::asio::dbus_interface> thresholdInterfaceWarning;
32 std::shared_ptr<sdbusplus::asio::dbus_interface> thresholdInterfaceCritical;
James Feist078f2322019-03-08 11:09:05 -080033 std::shared_ptr<sdbusplus::asio::dbus_interface> association;
James Feist8fd8a582018-11-16 11:10:46 -080034 double value = std::numeric_limits<double>::quiet_NaN();
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +053035 bool overriddenState = false;
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053036 bool internalSet = false;
James Feistce3fca42018-11-21 12:58:24 -080037
James Feistd8705872019-02-08 13:26:09 -080038 int setSensorValue(const double& newValue, double& oldValue)
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053039 {
Richard Marian Thomaiyaraf6b87c2019-04-03 23:54:28 +053040 if (!internalSet)
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053041 {
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053042 oldValue = newValue;
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +053043 overriddenState = true;
44 // check thresholds for external set
45 value = newValue;
46 checkThresholds();
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053047 }
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +053048 else if (!overriddenState)
Richard Marian Thomaiyaraf6b87c2019-04-03 23:54:28 +053049 {
50 oldValue = newValue;
51 }
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053052 return 1;
53 }
James Feistce3fca42018-11-21 12:58:24 -080054
55 void
James Feistd8705872019-02-08 13:26:09 -080056 setInitialProperties(std::shared_ptr<sdbusplus::asio::connection>& conn)
James Feistce3fca42018-11-21 12:58:24 -080057 {
James Feist82bac4c2019-03-11 11:16:53 -070058 createAssociation(association, configurationPath);
James Feist078f2322019-03-08 11:09:05 -080059
James Feistce3fca42018-11-21 12:58:24 -080060 sensorInterface->register_property("MaxValue", maxValue);
61 sensorInterface->register_property("MinValue", minValue);
62 sensorInterface->register_property(
James Feistd8705872019-02-08 13:26:09 -080063 "Value", value, [&](const double& newValue, double& oldValue) {
James Feistce3fca42018-11-21 12:58:24 -080064 return setSensorValue(newValue, oldValue);
65 });
66
James Feistd8705872019-02-08 13:26:09 -080067 for (auto& threshold : thresholds)
James Feistce3fca42018-11-21 12:58:24 -080068 {
69 std::shared_ptr<sdbusplus::asio::dbus_interface> iface;
70 std::string level;
71 std::string alarm;
72 if (threshold.level == thresholds::Level::CRITICAL)
73 {
74 iface = thresholdInterfaceCritical;
75 if (threshold.direction == thresholds::Direction::HIGH)
76 {
77 level = "CriticalHigh";
78 alarm = "CriticalAlarmHigh";
79 }
80 else
81 {
82 level = "CriticalLow";
83 alarm = "CriticalAlarmLow";
84 }
85 }
86 else if (threshold.level == thresholds::Level::WARNING)
87 {
88 iface = thresholdInterfaceWarning;
89 if (threshold.direction == thresholds::Direction::HIGH)
90 {
91 level = "WarningHigh";
92 alarm = "WarningAlarmHigh";
93 }
94 else
95 {
96 level = "WarningLow";
97 alarm = "WarningAlarmLow";
98 }
99 }
100 else
101 {
102 std::cerr << "Unknown threshold level" << threshold.level
103 << "\n";
104 continue;
105 }
106 if (!iface)
107 {
108 std::cout << "trying to set uninitialized interface\n";
109 continue;
110 }
111 iface->register_property(
112 level, threshold.value,
James Feistd8705872019-02-08 13:26:09 -0800113 [&](const double& request, double& oldValue) {
James Feistce3fca42018-11-21 12:58:24 -0800114 oldValue = request; // todo, just let the config do this?
115 threshold.value = request;
116 thresholds::persistThreshold(configurationPath, objectType,
James Feista222ba72019-03-01 15:57:51 -0800117 threshold, conn,
118 thresholds.size());
James Feistce3fca42018-11-21 12:58:24 -0800119 return 1;
120 });
121 iface->register_property(alarm, false);
122 }
123 if (!sensorInterface->initialize())
124 {
125 std::cerr << "error initializing value interface\n";
126 }
127 if (thresholdInterfaceWarning &&
128 !thresholdInterfaceWarning->initialize())
129 {
130 std::cerr << "error initializing warning threshold interface\n";
131 }
132
133 if (thresholdInterfaceCritical &&
134 !thresholdInterfaceCritical->initialize())
135 {
136 std::cerr << "error initializing critical threshold interface\n";
137 }
138 }
139
James Feistd8705872019-02-08 13:26:09 -0800140 void updateValue(const double& newValue)
James Feistce3fca42018-11-21 12:58:24 -0800141 {
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530142 // Ignore if overriding is enabled
143 if (!overriddenState)
144 {
145 // Indicate that it is internal set call
146 internalSet = true;
147 sensorInterface->set_property("Value", newValue);
148 internalSet = false;
149 value = newValue;
150 checkThresholds();
151 }
James Feistce3fca42018-11-21 12:58:24 -0800152 }
Richard Marian Thomaiyarc0ca7ee2019-04-24 21:22:52 +0530153};