/
sensor.hpp
163 lines (155 loc) · 5.47 KB
/
sensor.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#pragma once
#include "Thresholds.hpp"
#include <limits>
#include <memory>
#include <sdbusplus/asio/object_server.hpp>
#include <string>
#include <vector>
constexpr size_t sensorFailedPollTimeMs = 5000;
constexpr const char* sensorValueInterface = "xyz.openbmc_project.Sensor.Value";
struct Sensor
{
Sensor(const std::string& name,
std::vector<thresholds::Threshold>&& thresholdData,
const std::string& configurationPath, const std::string& objectType,
const double max, const double min) :
name(name),
configurationPath(configurationPath), objectType(objectType),
maxValue(max), minValue(min), thresholds(std::move(thresholdData)),
hysteresis((max - min) * 0.01)
{
}
virtual ~Sensor() = default;
virtual void checkThresholds(void) = 0;
std::string name;
std::string configurationPath;
std::string objectType;
double maxValue;
double minValue;
std::vector<thresholds::Threshold> thresholds;
std::shared_ptr<sdbusplus::asio::dbus_interface> sensorInterface;
std::shared_ptr<sdbusplus::asio::dbus_interface> thresholdInterfaceWarning;
std::shared_ptr<sdbusplus::asio::dbus_interface> thresholdInterfaceCritical;
std::shared_ptr<sdbusplus::asio::dbus_interface> association;
double value = std::numeric_limits<double>::quiet_NaN();
bool overriddenState = false;
bool internalSet = false;
double hysteresis;
int setSensorValue(const double& newValue, double& oldValue)
{
if (!internalSet)
{
oldValue = newValue;
overriddenState = true;
// check thresholds for external set
value = newValue;
checkThresholds();
}
else if (!overriddenState)
{
oldValue = newValue;
}
return 1;
}
void
setInitialProperties(std::shared_ptr<sdbusplus::asio::connection>& conn)
{
createAssociation(association, configurationPath);
sensorInterface->register_property("MaxValue", maxValue);
sensorInterface->register_property("MinValue", minValue);
sensorInterface->register_property(
"Value", value, [&](const double& newValue, double& oldValue) {
return setSensorValue(newValue, oldValue);
});
for (auto& threshold : thresholds)
{
std::shared_ptr<sdbusplus::asio::dbus_interface> iface;
std::string level;
std::string alarm;
if (threshold.level == thresholds::Level::CRITICAL)
{
iface = thresholdInterfaceCritical;
if (threshold.direction == thresholds::Direction::HIGH)
{
level = "CriticalHigh";
alarm = "CriticalAlarmHigh";
}
else
{
level = "CriticalLow";
alarm = "CriticalAlarmLow";
}
}
else if (threshold.level == thresholds::Level::WARNING)
{
iface = thresholdInterfaceWarning;
if (threshold.direction == thresholds::Direction::HIGH)
{
level = "WarningHigh";
alarm = "WarningAlarmHigh";
}
else
{
level = "WarningLow";
alarm = "WarningAlarmLow";
}
}
else
{
std::cerr << "Unknown threshold level" << threshold.level
<< "\n";
continue;
}
if (!iface)
{
std::cout << "trying to set uninitialized interface\n";
continue;
}
iface->register_property(
level, threshold.value,
[&](const double& request, double& oldValue) {
oldValue = request; // todo, just let the config do this?
threshold.value = request;
thresholds::persistThreshold(configurationPath, objectType,
threshold, conn,
thresholds.size());
return 1;
});
iface->register_property(alarm, false);
}
if (!sensorInterface->initialize())
{
std::cerr << "error initializing value interface\n";
}
if (thresholdInterfaceWarning &&
!thresholdInterfaceWarning->initialize())
{
std::cerr << "error initializing warning threshold interface\n";
}
if (thresholdInterfaceCritical &&
!thresholdInterfaceCritical->initialize())
{
std::cerr << "error initializing critical threshold interface\n";
}
}
void updateValue(const double& newValue)
{
// Ignore if overriding is enabled
if (!overriddenState)
{
// Indicate that it is internal set call
internalSet = true;
if (!(sensorInterface->set_property("Value", newValue)))
{
std::cerr << "error setting property to " << newValue << "\n";
}
internalSet = false;
double diff = std::abs(value - newValue);
if (std::isnan(diff) || diff > hysteresis)
{
value = newValue;
}
checkThresholds();
}
}
};