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 | b2eb29a | 2018-12-08 18:26:58 +0530 | [diff] [blame] | 35 | bool overridenState = 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 | af6b87c | 2019-04-03 23:54:28 +0530 | [diff] [blame] | 43 | overridenState = true; |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 44 | } |
Richard Marian Thomaiyar | af6b87c | 2019-04-03 23:54:28 +0530 | [diff] [blame] | 45 | else if (!overridenState) |
| 46 | { |
| 47 | oldValue = newValue; |
| 48 | } |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 49 | return 1; |
| 50 | } |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 51 | |
| 52 | void |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 53 | setInitialProperties(std::shared_ptr<sdbusplus::asio::connection>& conn) |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 54 | { |
James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 55 | createAssociation(association, configurationPath); |
James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 56 | |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 57 | sensorInterface->register_property("MaxValue", maxValue); |
| 58 | sensorInterface->register_property("MinValue", minValue); |
| 59 | sensorInterface->register_property( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 60 | "Value", value, [&](const double& newValue, double& oldValue) { |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 61 | return setSensorValue(newValue, oldValue); |
| 62 | }); |
| 63 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 64 | for (auto& threshold : thresholds) |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 65 | { |
| 66 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface; |
| 67 | std::string level; |
| 68 | std::string alarm; |
| 69 | if (threshold.level == thresholds::Level::CRITICAL) |
| 70 | { |
| 71 | iface = thresholdInterfaceCritical; |
| 72 | if (threshold.direction == thresholds::Direction::HIGH) |
| 73 | { |
| 74 | level = "CriticalHigh"; |
| 75 | alarm = "CriticalAlarmHigh"; |
| 76 | } |
| 77 | else |
| 78 | { |
| 79 | level = "CriticalLow"; |
| 80 | alarm = "CriticalAlarmLow"; |
| 81 | } |
| 82 | } |
| 83 | else if (threshold.level == thresholds::Level::WARNING) |
| 84 | { |
| 85 | iface = thresholdInterfaceWarning; |
| 86 | if (threshold.direction == thresholds::Direction::HIGH) |
| 87 | { |
| 88 | level = "WarningHigh"; |
| 89 | alarm = "WarningAlarmHigh"; |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | level = "WarningLow"; |
| 94 | alarm = "WarningAlarmLow"; |
| 95 | } |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | std::cerr << "Unknown threshold level" << threshold.level |
| 100 | << "\n"; |
| 101 | continue; |
| 102 | } |
| 103 | if (!iface) |
| 104 | { |
| 105 | std::cout << "trying to set uninitialized interface\n"; |
| 106 | continue; |
| 107 | } |
| 108 | iface->register_property( |
| 109 | level, threshold.value, |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 110 | [&](const double& request, double& oldValue) { |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 111 | oldValue = request; // todo, just let the config do this? |
| 112 | threshold.value = request; |
| 113 | thresholds::persistThreshold(configurationPath, objectType, |
James Feist | a222ba7 | 2019-03-01 15:57:51 -0800 | [diff] [blame] | 114 | threshold, conn, |
| 115 | thresholds.size()); |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 116 | return 1; |
| 117 | }); |
| 118 | iface->register_property(alarm, false); |
| 119 | } |
| 120 | if (!sensorInterface->initialize()) |
| 121 | { |
| 122 | std::cerr << "error initializing value interface\n"; |
| 123 | } |
| 124 | if (thresholdInterfaceWarning && |
| 125 | !thresholdInterfaceWarning->initialize()) |
| 126 | { |
| 127 | std::cerr << "error initializing warning threshold interface\n"; |
| 128 | } |
| 129 | |
| 130 | if (thresholdInterfaceCritical && |
| 131 | !thresholdInterfaceCritical->initialize()) |
| 132 | { |
| 133 | std::cerr << "error initializing critical threshold interface\n"; |
| 134 | } |
| 135 | } |
| 136 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 137 | void updateValue(const double& newValue) |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 138 | { |
| 139 | // Indicate that it is internal set call |
| 140 | internalSet = true; |
| 141 | sensorInterface->set_property("Value", newValue); |
| 142 | internalSet = false; |
| 143 | value = newValue; |
| 144 | checkThresholds(); |
| 145 | } |
James Feist | 8fd8a58 | 2018-11-16 11:10:46 -0800 | [diff] [blame] | 146 | }; |