blob: 3d81f1f1f724258b34110c95ce9331dfad6415e7 [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 Thomaiyarb2eb29a2018-12-08 18:26:58 +053035 bool overridenState = 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 Thomaiyaraf6b87c2019-04-03 23:54:28 +053043 overridenState = true;
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053044 }
Richard Marian Thomaiyaraf6b87c2019-04-03 23:54:28 +053045 else if (!overridenState)
46 {
47 oldValue = newValue;
48 }
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053049 return 1;
50 }
James Feistce3fca42018-11-21 12:58:24 -080051
52 void
James Feistd8705872019-02-08 13:26:09 -080053 setInitialProperties(std::shared_ptr<sdbusplus::asio::connection>& conn)
James Feistce3fca42018-11-21 12:58:24 -080054 {
James Feist82bac4c2019-03-11 11:16:53 -070055 createAssociation(association, configurationPath);
James Feist078f2322019-03-08 11:09:05 -080056
James Feistce3fca42018-11-21 12:58:24 -080057 sensorInterface->register_property("MaxValue", maxValue);
58 sensorInterface->register_property("MinValue", minValue);
59 sensorInterface->register_property(
James Feistd8705872019-02-08 13:26:09 -080060 "Value", value, [&](const double& newValue, double& oldValue) {
James Feistce3fca42018-11-21 12:58:24 -080061 return setSensorValue(newValue, oldValue);
62 });
63
James Feistd8705872019-02-08 13:26:09 -080064 for (auto& threshold : thresholds)
James Feistce3fca42018-11-21 12:58:24 -080065 {
66 std::shared_ptr<sdbusplus::asio::dbus_interface> iface;
67 std::string level;
68 std::string alarm;
69 if (threshold.level == thresholds::Level::CRITICAL)
70 {
71 iface = thresholdInterfaceCritical;
72 if (threshold.direction == thresholds::Direction::HIGH)
73 {
74 level = "CriticalHigh";
75 alarm = "CriticalAlarmHigh";
76 }
77 else
78 {
79 level = "CriticalLow";
80 alarm = "CriticalAlarmLow";
81 }
82 }
83 else if (threshold.level == thresholds::Level::WARNING)
84 {
85 iface = thresholdInterfaceWarning;
86 if (threshold.direction == thresholds::Direction::HIGH)
87 {
88 level = "WarningHigh";
89 alarm = "WarningAlarmHigh";
90 }
91 else
92 {
93 level = "WarningLow";
94 alarm = "WarningAlarmLow";
95 }
96 }
97 else
98 {
99 std::cerr << "Unknown threshold level" << threshold.level
100 << "\n";
101 continue;
102 }
103 if (!iface)
104 {
105 std::cout << "trying to set uninitialized interface\n";
106 continue;
107 }
108 iface->register_property(
109 level, threshold.value,
James Feistd8705872019-02-08 13:26:09 -0800110 [&](const double& request, double& oldValue) {
James Feistce3fca42018-11-21 12:58:24 -0800111 oldValue = request; // todo, just let the config do this?
112 threshold.value = request;
113 thresholds::persistThreshold(configurationPath, objectType,
James Feista222ba72019-03-01 15:57:51 -0800114 threshold, conn,
115 thresholds.size());
James Feistce3fca42018-11-21 12:58:24 -0800116 return 1;
117 });
118 iface->register_property(alarm, false);
119 }
120 if (!sensorInterface->initialize())
121 {
122 std::cerr << "error initializing value interface\n";
123 }
124 if (thresholdInterfaceWarning &&
125 !thresholdInterfaceWarning->initialize())
126 {
127 std::cerr << "error initializing warning threshold interface\n";
128 }
129
130 if (thresholdInterfaceCritical &&
131 !thresholdInterfaceCritical->initialize())
132 {
133 std::cerr << "error initializing critical threshold interface\n";
134 }
135 }
136
James Feistd8705872019-02-08 13:26:09 -0800137 void updateValue(const double& newValue)
James Feistce3fca42018-11-21 12:58:24 -0800138 {
139 // Indicate that it is internal set call
140 internalSet = true;
141 sensorInterface->set_property("Value", newValue);
142 internalSet = false;
143 value = newValue;
144 checkThresholds();
145 }
James Feist8fd8a582018-11-16 11:10:46 -0800146};