blob: 2a414da8d1d2cfe06390e15846afa218f7a3bff7 [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 Feist8fd8a582018-11-16 11:10:46 -08007struct Sensor
8{
James Feistd8705872019-02-08 13:26:09 -08009 Sensor(const std::string& name, const std::string& path,
10 std::vector<thresholds::Threshold>&& thresholdData,
11 const std::string& configurationPath, const std::string& objectType,
James Feistce3fca42018-11-21 12:58:24 -080012 const double max, const double min) :
James Feistdc6c55f2018-10-31 12:53:20 -070013 name(name),
James Feista222ba72019-03-01 15:57:51 -080014 path(path), configurationPath(configurationPath),
15 objectType(objectType), thresholds(std::move(thresholdData)),
16 maxValue(max), minValue(min)
James Feistdc6c55f2018-10-31 12:53:20 -070017 {
18 }
James Feist8fd8a582018-11-16 11:10:46 -080019 virtual ~Sensor() = default;
James Feistce3fca42018-11-21 12:58:24 -080020 virtual void checkThresholds(void) = 0;
James Feistdc6c55f2018-10-31 12:53:20 -070021 std::string name;
22 std::string path;
James Feistce3fca42018-11-21 12:58:24 -080023 std::string configurationPath;
24 std::string objectType;
25 double maxValue;
26 double minValue;
James Feist8fd8a582018-11-16 11:10:46 -080027 std::vector<thresholds::Threshold> thresholds;
28 std::shared_ptr<sdbusplus::asio::dbus_interface> sensorInterface;
29 std::shared_ptr<sdbusplus::asio::dbus_interface> thresholdInterfaceWarning;
30 std::shared_ptr<sdbusplus::asio::dbus_interface> thresholdInterfaceCritical;
James Feist078f2322019-03-08 11:09:05 -080031 std::shared_ptr<sdbusplus::asio::dbus_interface> association;
James Feist8fd8a582018-11-16 11:10:46 -080032 double value = std::numeric_limits<double>::quiet_NaN();
Richard Marian Thomaiyarb2eb29a2018-12-08 18:26:58 +053033 bool overridenState = false;
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053034 bool internalSet = false;
James Feistce3fca42018-11-21 12:58:24 -080035
James Feistd8705872019-02-08 13:26:09 -080036 int setSensorValue(const double& newValue, double& oldValue)
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053037 {
Richard Marian Thomaiyaraf6b87c2019-04-03 23:54:28 +053038 if (!internalSet)
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053039 {
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053040 oldValue = newValue;
Richard Marian Thomaiyaraf6b87c2019-04-03 23:54:28 +053041 overridenState = true;
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053042 }
Richard Marian Thomaiyaraf6b87c2019-04-03 23:54:28 +053043 else if (!overridenState)
44 {
45 oldValue = newValue;
46 }
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053047 return 1;
48 }
James Feistce3fca42018-11-21 12:58:24 -080049
50 void
James Feistd8705872019-02-08 13:26:09 -080051 setInitialProperties(std::shared_ptr<sdbusplus::asio::connection>& conn)
James Feistce3fca42018-11-21 12:58:24 -080052 {
James Feist82bac4c2019-03-11 11:16:53 -070053 createAssociation(association, configurationPath);
James Feist078f2322019-03-08 11:09:05 -080054
James Feistce3fca42018-11-21 12:58:24 -080055 sensorInterface->register_property("MaxValue", maxValue);
56 sensorInterface->register_property("MinValue", minValue);
57 sensorInterface->register_property(
James Feistd8705872019-02-08 13:26:09 -080058 "Value", value, [&](const double& newValue, double& oldValue) {
James Feistce3fca42018-11-21 12:58:24 -080059 return setSensorValue(newValue, oldValue);
60 });
61
James Feistd8705872019-02-08 13:26:09 -080062 for (auto& threshold : thresholds)
James Feistce3fca42018-11-21 12:58:24 -080063 {
64 std::shared_ptr<sdbusplus::asio::dbus_interface> iface;
65 std::string level;
66 std::string alarm;
67 if (threshold.level == thresholds::Level::CRITICAL)
68 {
69 iface = thresholdInterfaceCritical;
70 if (threshold.direction == thresholds::Direction::HIGH)
71 {
72 level = "CriticalHigh";
73 alarm = "CriticalAlarmHigh";
74 }
75 else
76 {
77 level = "CriticalLow";
78 alarm = "CriticalAlarmLow";
79 }
80 }
81 else if (threshold.level == thresholds::Level::WARNING)
82 {
83 iface = thresholdInterfaceWarning;
84 if (threshold.direction == thresholds::Direction::HIGH)
85 {
86 level = "WarningHigh";
87 alarm = "WarningAlarmHigh";
88 }
89 else
90 {
91 level = "WarningLow";
92 alarm = "WarningAlarmLow";
93 }
94 }
95 else
96 {
97 std::cerr << "Unknown threshold level" << threshold.level
98 << "\n";
99 continue;
100 }
101 if (!iface)
102 {
103 std::cout << "trying to set uninitialized interface\n";
104 continue;
105 }
106 iface->register_property(
107 level, threshold.value,
James Feistd8705872019-02-08 13:26:09 -0800108 [&](const double& request, double& oldValue) {
James Feistce3fca42018-11-21 12:58:24 -0800109 oldValue = request; // todo, just let the config do this?
110 threshold.value = request;
111 thresholds::persistThreshold(configurationPath, objectType,
James Feista222ba72019-03-01 15:57:51 -0800112 threshold, conn,
113 thresholds.size());
James Feistce3fca42018-11-21 12:58:24 -0800114 return 1;
115 });
116 iface->register_property(alarm, false);
117 }
118 if (!sensorInterface->initialize())
119 {
120 std::cerr << "error initializing value interface\n";
121 }
122 if (thresholdInterfaceWarning &&
123 !thresholdInterfaceWarning->initialize())
124 {
125 std::cerr << "error initializing warning threshold interface\n";
126 }
127
128 if (thresholdInterfaceCritical &&
129 !thresholdInterfaceCritical->initialize())
130 {
131 std::cerr << "error initializing critical threshold interface\n";
132 }
133 }
134
James Feistd8705872019-02-08 13:26:09 -0800135 void updateValue(const double& newValue)
James Feistce3fca42018-11-21 12:58:24 -0800136 {
137 // Indicate that it is internal set call
138 internalSet = true;
139 sensorInterface->set_property("Value", newValue);
140 internalSet = false;
141 value = newValue;
142 checkThresholds();
143 }
James Feist8fd8a582018-11-16 11:10:46 -0800144};