blob: bba2c4d9cb52d4666699c25450807e972d213e61 [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;
31 double value = std::numeric_limits<double>::quiet_NaN();
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053032 double overriddenValue = 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 {
38 if (internalSet)
39 {
40 internalSet = false;
41 oldValue = newValue;
42 return 1;
43 }
44 overriddenValue = newValue;
Richard Marian Thomaiyarb2eb29a2018-12-08 18:26:58 +053045 overridenState = true;
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053046 return 1;
47 }
James Feistce3fca42018-11-21 12:58:24 -080048
49 void
James Feistd8705872019-02-08 13:26:09 -080050 setInitialProperties(std::shared_ptr<sdbusplus::asio::connection>& conn)
James Feistce3fca42018-11-21 12:58:24 -080051 {
52 sensorInterface->register_property("MaxValue", maxValue);
53 sensorInterface->register_property("MinValue", minValue);
54 sensorInterface->register_property(
James Feistd8705872019-02-08 13:26:09 -080055 "Value", value, [&](const double& newValue, double& oldValue) {
James Feistce3fca42018-11-21 12:58:24 -080056 return setSensorValue(newValue, oldValue);
57 });
58
James Feistd8705872019-02-08 13:26:09 -080059 for (auto& threshold : thresholds)
James Feistce3fca42018-11-21 12:58:24 -080060 {
61 std::shared_ptr<sdbusplus::asio::dbus_interface> iface;
62 std::string level;
63 std::string alarm;
64 if (threshold.level == thresholds::Level::CRITICAL)
65 {
66 iface = thresholdInterfaceCritical;
67 if (threshold.direction == thresholds::Direction::HIGH)
68 {
69 level = "CriticalHigh";
70 alarm = "CriticalAlarmHigh";
71 }
72 else
73 {
74 level = "CriticalLow";
75 alarm = "CriticalAlarmLow";
76 }
77 }
78 else if (threshold.level == thresholds::Level::WARNING)
79 {
80 iface = thresholdInterfaceWarning;
81 if (threshold.direction == thresholds::Direction::HIGH)
82 {
83 level = "WarningHigh";
84 alarm = "WarningAlarmHigh";
85 }
86 else
87 {
88 level = "WarningLow";
89 alarm = "WarningAlarmLow";
90 }
91 }
92 else
93 {
94 std::cerr << "Unknown threshold level" << threshold.level
95 << "\n";
96 continue;
97 }
98 if (!iface)
99 {
100 std::cout << "trying to set uninitialized interface\n";
101 continue;
102 }
103 iface->register_property(
104 level, threshold.value,
James Feistd8705872019-02-08 13:26:09 -0800105 [&](const double& request, double& oldValue) {
James Feistce3fca42018-11-21 12:58:24 -0800106 oldValue = request; // todo, just let the config do this?
107 threshold.value = request;
108 thresholds::persistThreshold(configurationPath, objectType,
James Feista222ba72019-03-01 15:57:51 -0800109 threshold, conn,
110 thresholds.size());
James Feistce3fca42018-11-21 12:58:24 -0800111 return 1;
112 });
113 iface->register_property(alarm, false);
114 }
115 if (!sensorInterface->initialize())
116 {
117 std::cerr << "error initializing value interface\n";
118 }
119 if (thresholdInterfaceWarning &&
120 !thresholdInterfaceWarning->initialize())
121 {
122 std::cerr << "error initializing warning threshold interface\n";
123 }
124
125 if (thresholdInterfaceCritical &&
126 !thresholdInterfaceCritical->initialize())
127 {
128 std::cerr << "error initializing critical threshold interface\n";
129 }
130 }
131
James Feistd8705872019-02-08 13:26:09 -0800132 void updateValue(const double& newValue)
James Feistce3fca42018-11-21 12:58:24 -0800133 {
134 // Indicate that it is internal set call
135 internalSet = true;
136 sensorInterface->set_property("Value", newValue);
137 internalSet = false;
138 value = newValue;
139 checkThresholds();
140 }
James Feist8fd8a582018-11-16 11:10:46 -0800141};