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