blob: de790c8bda2614066f1c79767a60d64c6bd2cfd3 [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 Feistce3fca42018-11-21 12:58:24 -080014 path(path), thresholds(std::move(thresholdData)),
15 configurationPath(configurationPath), maxValue(max), minValue(min)
James Feistdc6c55f2018-10-31 12:53:20 -070016 {
17 }
James Feist8fd8a582018-11-16 11:10:46 -080018 virtual ~Sensor() = default;
James Feistce3fca42018-11-21 12:58:24 -080019 virtual void checkThresholds(void) = 0;
James Feistdc6c55f2018-10-31 12:53:20 -070020 std::string name;
21 std::string path;
James Feistce3fca42018-11-21 12:58:24 -080022 std::string configurationPath;
23 std::string objectType;
24 double maxValue;
25 double minValue;
James Feist8fd8a582018-11-16 11:10:46 -080026 std::vector<thresholds::Threshold> thresholds;
27 std::shared_ptr<sdbusplus::asio::dbus_interface> sensorInterface;
28 std::shared_ptr<sdbusplus::asio::dbus_interface> thresholdInterfaceWarning;
29 std::shared_ptr<sdbusplus::asio::dbus_interface> thresholdInterfaceCritical;
30 double value = std::numeric_limits<double>::quiet_NaN();
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053031 double overriddenValue = std::numeric_limits<double>::quiet_NaN();
Richard Marian Thomaiyarb2eb29a2018-12-08 18:26:58 +053032 bool overridenState = false;
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053033 bool internalSet = false;
James Feistce3fca42018-11-21 12:58:24 -080034
James Feistd8705872019-02-08 13:26:09 -080035 int setSensorValue(const double& newValue, double& oldValue)
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053036 {
37 if (internalSet)
38 {
39 internalSet = false;
40 oldValue = newValue;
41 return 1;
42 }
43 overriddenValue = newValue;
Richard Marian Thomaiyarb2eb29a2018-12-08 18:26:58 +053044 overridenState = true;
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +053045 return 1;
46 }
James Feistce3fca42018-11-21 12:58:24 -080047
48 void
James Feistd8705872019-02-08 13:26:09 -080049 setInitialProperties(std::shared_ptr<sdbusplus::asio::connection>& conn)
James Feistce3fca42018-11-21 12:58:24 -080050 {
51 sensorInterface->register_property("MaxValue", maxValue);
52 sensorInterface->register_property("MinValue", minValue);
53 sensorInterface->register_property(
James Feistd8705872019-02-08 13:26:09 -080054 "Value", value, [&](const double& newValue, double& oldValue) {
James Feistce3fca42018-11-21 12:58:24 -080055 return setSensorValue(newValue, oldValue);
56 });
57
James Feistd8705872019-02-08 13:26:09 -080058 for (auto& threshold : thresholds)
James Feistce3fca42018-11-21 12:58:24 -080059 {
60 std::shared_ptr<sdbusplus::asio::dbus_interface> iface;
61 std::string level;
62 std::string alarm;
63 if (threshold.level == thresholds::Level::CRITICAL)
64 {
65 iface = thresholdInterfaceCritical;
66 if (threshold.direction == thresholds::Direction::HIGH)
67 {
68 level = "CriticalHigh";
69 alarm = "CriticalAlarmHigh";
70 }
71 else
72 {
73 level = "CriticalLow";
74 alarm = "CriticalAlarmLow";
75 }
76 }
77 else if (threshold.level == thresholds::Level::WARNING)
78 {
79 iface = thresholdInterfaceWarning;
80 if (threshold.direction == thresholds::Direction::HIGH)
81 {
82 level = "WarningHigh";
83 alarm = "WarningAlarmHigh";
84 }
85 else
86 {
87 level = "WarningLow";
88 alarm = "WarningAlarmLow";
89 }
90 }
91 else
92 {
93 std::cerr << "Unknown threshold level" << threshold.level
94 << "\n";
95 continue;
96 }
97 if (!iface)
98 {
99 std::cout << "trying to set uninitialized interface\n";
100 continue;
101 }
102 iface->register_property(
103 level, threshold.value,
James Feistd8705872019-02-08 13:26:09 -0800104 [&](const double& request, double& oldValue) {
James Feistce3fca42018-11-21 12:58:24 -0800105 oldValue = request; // todo, just let the config do this?
106 threshold.value = request;
107 thresholds::persistThreshold(configurationPath, objectType,
108 threshold, conn);
109 return 1;
110 });
111 iface->register_property(alarm, false);
112 }
113 if (!sensorInterface->initialize())
114 {
115 std::cerr << "error initializing value interface\n";
116 }
117 if (thresholdInterfaceWarning &&
118 !thresholdInterfaceWarning->initialize())
119 {
120 std::cerr << "error initializing warning threshold interface\n";
121 }
122
123 if (thresholdInterfaceCritical &&
124 !thresholdInterfaceCritical->initialize())
125 {
126 std::cerr << "error initializing critical threshold interface\n";
127 }
128 }
129
James Feistd8705872019-02-08 13:26:09 -0800130 void updateValue(const double& newValue)
James Feistce3fca42018-11-21 12:58:24 -0800131 {
132 // Indicate that it is internal set call
133 internalSet = true;
134 sensorInterface->set_property("Value", newValue);
135 internalSet = false;
136 value = newValue;
137 checkThresholds();
138 }
James Feist8fd8a582018-11-16 11:10:46 -0800139};