James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <unistd.h> |
| 18 | |
| 19 | #include <TachSensor.hpp> |
| 20 | #include <Utils.hpp> |
| 21 | #include <boost/algorithm/string/predicate.hpp> |
| 22 | #include <boost/algorithm/string/replace.hpp> |
| 23 | #include <boost/date_time/posix_time/posix_time.hpp> |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 24 | #include <fstream> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 25 | #include <iostream> |
| 26 | #include <limits> |
| 27 | #include <sdbusplus/asio/connection.hpp> |
| 28 | #include <sdbusplus/asio/object_server.hpp> |
| 29 | #include <string> |
| 30 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 31 | static constexpr unsigned int pwmPollMs = 500; |
| 32 | static constexpr size_t warnAfterErrorCount = 10; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 33 | |
| 34 | TachSensor::TachSensor(const std::string &path, |
| 35 | sdbusplus::asio::object_server &objectServer, |
| 36 | std::shared_ptr<sdbusplus::asio::connection> &conn, |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 37 | std::unique_ptr<PresenceSensor> &&presence, |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 38 | std::unique_ptr<RedundancySensor> &redundancy, |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 39 | boost::asio::io_service &io, const std::string &fanName, |
| 40 | std::vector<thresholds::Threshold> &&_thresholds, |
| 41 | const std::string &sensorConfiguration) : |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 42 | Sensor(boost::replace_all_copy(fanName, " ", "_"), path, |
| 43 | std::move(_thresholds)), |
| 44 | objServer(objectServer), dbusConnection(conn), |
| 45 | presence(std::move(presence)), redundancy(redundancy), |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 46 | configuration(sensorConfiguration), |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 47 | inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), errCount(0), |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 48 | // todo, get these from config |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 49 | maxValue(25000), minValue(0) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 50 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 51 | sensorInterface = objectServer.add_interface( |
| 52 | "/xyz/openbmc_project/sensors/fan_tach/" + name, |
| 53 | "xyz.openbmc_project.Sensor.Value"); |
| 54 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 55 | if (thresholds::hasWarningInterface(thresholds)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 56 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 57 | thresholdInterfaceWarning = objectServer.add_interface( |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 58 | "/xyz/openbmc_project/sensors/fan_tach/" + name, |
| 59 | "xyz.openbmc_project.Sensor.Threshold.Warning"); |
| 60 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 61 | if (thresholds::hasCriticalInterface(thresholds)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 62 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 63 | thresholdInterfaceCritical = objectServer.add_interface( |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 64 | "/xyz/openbmc_project/sensors/fan_tach/" + name, |
| 65 | "xyz.openbmc_project.Sensor.Threshold.Critical"); |
| 66 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 67 | setInitialProperties(conn); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 68 | isPowerOn(dbusConnection); // first call initializes |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 69 | setupRead(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | TachSensor::~TachSensor() |
| 73 | { |
| 74 | // close the input dev to cancel async operations |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 75 | inputDev.close(); |
| 76 | waitTimer.cancel(); |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 77 | objServer.remove_interface(thresholdInterfaceWarning); |
| 78 | objServer.remove_interface(thresholdInterfaceCritical); |
| 79 | objServer.remove_interface(sensorInterface); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 82 | void TachSensor::setupRead(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 83 | { |
| 84 | boost::asio::async_read_until( |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 85 | inputDev, readBuf, '\n', |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 86 | [&](const boost::system::error_code &ec, |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 87 | std::size_t /*bytes_transfered*/) { handleResponse(ec); }); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 90 | void TachSensor::handleResponse(const boost::system::error_code &err) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 91 | { |
| 92 | if (err == boost::system::errc::bad_file_descriptor) |
| 93 | { |
| 94 | return; // we're being destroyed |
| 95 | } |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 96 | bool missing = false; |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 97 | size_t pollTime = pwmPollMs; |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 98 | if (presence) |
| 99 | { |
| 100 | if (!presence->getValue()) |
| 101 | { |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 102 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 103 | missing = true; |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 104 | pollTime = sensorFailedPollTimeMs; |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 107 | std::istream responseStream(&readBuf); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 108 | if (!missing) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 109 | { |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 110 | if (!err) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 111 | { |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 112 | std::string response; |
| 113 | try |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 114 | { |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 115 | std::getline(responseStream, response); |
| 116 | float nvalue = std::stof(response); |
| 117 | responseStream.clear(); |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 118 | if (!isnan(overriddenValue)) |
| 119 | { |
| 120 | nvalue = overriddenValue; |
| 121 | } |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 122 | if (nvalue != value) |
| 123 | { |
| 124 | updateValue(nvalue); |
| 125 | } |
| 126 | errCount = 0; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 127 | } |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 128 | catch (const std::invalid_argument &) |
| 129 | { |
| 130 | errCount++; |
| 131 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 132 | } |
| 133 | else |
| 134 | { |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 135 | pollTime = sensorFailedPollTimeMs; |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 136 | errCount++; |
| 137 | } |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 138 | if (errCount >= warnAfterErrorCount) |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 139 | { |
| 140 | // only an error if power is on |
| 141 | if (isPowerOn(dbusConnection)) |
| 142 | { |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 143 | // only print once |
| 144 | if (errCount == warnAfterErrorCount) |
| 145 | { |
| 146 | std::cerr << "Failure to read sensor " << name << " at " |
| 147 | << path << " ec:" << err << "\n"; |
| 148 | } |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 149 | updateValue(0); |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | errCount = 0; // check power again in 10 cycles |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 154 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 155 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 158 | responseStream.clear(); |
| 159 | inputDev.close(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 160 | int fd = open(path.c_str(), O_RDONLY); |
| 161 | if (fd <= 0) |
| 162 | { |
| 163 | return; // we're no longer valid |
| 164 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 165 | inputDev.assign(fd); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 166 | waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime)); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 167 | waitTimer.async_wait([&](const boost::system::error_code &ec) { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 168 | if (ec == boost::asio::error::operation_aborted) |
| 169 | { |
| 170 | return; // we're being canceled |
| 171 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 172 | setupRead(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 173 | }); |
| 174 | } |
| 175 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 176 | void TachSensor::checkThresholds(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 177 | { |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 178 | bool status = thresholds::checkThresholds(this); |
| 179 | if (redundancy) |
| 180 | { |
| 181 | redundancy->update("/xyz/openbmc_project/sensors/fan_tach/" + name, |
| 182 | !status); |
| 183 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 186 | void TachSensor::updateValue(const double &newValue) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 187 | { |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 188 | // Indicate that it is internal set call |
| 189 | internalSet = true; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 190 | sensorInterface->set_property("Value", newValue); |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 191 | internalSet = false; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 192 | value = newValue; |
| 193 | checkThresholds(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 196 | void TachSensor::setInitialProperties( |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 197 | std::shared_ptr<sdbusplus::asio::connection> &conn) |
| 198 | { |
| 199 | // todo, get max and min from configuration |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 200 | sensorInterface->register_property("MaxValue", maxValue); |
| 201 | sensorInterface->register_property("MinValue", minValue); |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 202 | sensorInterface->register_property( |
| 203 | "Value", value, [&](const double &newValue, double &oldValue) { |
| 204 | return setSensorValue(newValue, oldValue); |
| 205 | }); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 206 | |
| 207 | for (auto &threshold : thresholds) |
| 208 | { |
| 209 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface; |
| 210 | std::string level; |
| 211 | std::string alarm; |
| 212 | if (threshold.level == thresholds::Level::CRITICAL) |
| 213 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 214 | iface = thresholdInterfaceCritical; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 215 | if (threshold.direction == thresholds::Direction::HIGH) |
| 216 | { |
| 217 | level = "CriticalHigh"; |
| 218 | alarm = "CriticalAlarmHigh"; |
| 219 | } |
| 220 | else |
| 221 | { |
| 222 | level = "CriticalLow"; |
| 223 | alarm = "CriticalAlarmLow"; |
| 224 | } |
| 225 | } |
| 226 | else if (threshold.level == thresholds::Level::WARNING) |
| 227 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 228 | iface = thresholdInterfaceWarning; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 229 | if (threshold.direction == thresholds::Direction::HIGH) |
| 230 | { |
| 231 | level = "WarningHigh"; |
| 232 | alarm = "WarningAlarmHigh"; |
| 233 | } |
| 234 | else |
| 235 | { |
| 236 | level = "WarningLow"; |
| 237 | alarm = "WarningAlarmLow"; |
| 238 | } |
| 239 | } |
| 240 | else |
| 241 | { |
| 242 | std::cerr << "Unknown threshold level" << threshold.level << "\n"; |
| 243 | continue; |
| 244 | } |
| 245 | if (!iface) |
| 246 | { |
| 247 | std::cout << "trying to set uninitialized interface\n"; |
| 248 | continue; |
| 249 | } |
| 250 | iface->register_property( |
| 251 | level, threshold.value, |
| 252 | [&](const double &request, double &oldValue) { |
| 253 | oldValue = request; // todo, just let the config do this? |
| 254 | threshold.value = request; |
| 255 | thresholds::persistThreshold( |
| 256 | configuration, |
| 257 | "xyz.openbmc_project.Configuration.AspeedFan", threshold, |
| 258 | conn); |
| 259 | return 1; |
| 260 | }); |
| 261 | iface->register_property(alarm, false); |
| 262 | } |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 263 | if (!sensorInterface->initialize()) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 264 | { |
| 265 | std::cerr << "error initializing value interface\n"; |
| 266 | } |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 267 | if (thresholdInterfaceWarning && !thresholdInterfaceWarning->initialize()) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 268 | { |
| 269 | std::cerr << "error initializing warning threshold interface\n"; |
| 270 | } |
| 271 | |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 272 | if (thresholdInterfaceCritical && !thresholdInterfaceCritical->initialize()) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 273 | { |
| 274 | std::cerr << "error initializing critical threshold interface\n"; |
| 275 | } |
| 276 | } |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 277 | |
| 278 | PresenceSensor::PresenceSensor(const size_t index, bool inverted, |
| 279 | boost::asio::io_service &io) : |
| 280 | inverted(inverted), |
| 281 | inputDev(io) |
| 282 | { |
| 283 | // todo: use gpiodaemon |
| 284 | std::string device = gpioPath + std::string("gpio") + std::to_string(index); |
| 285 | fd = open((device + "/value").c_str(), O_RDONLY); |
| 286 | if (fd < 0) |
| 287 | { |
| 288 | std::cerr << "Error opening gpio " << index << "\n"; |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | std::ofstream deviceFile(device + "/edge"); |
| 293 | if (!deviceFile.good()) |
| 294 | { |
| 295 | std::cerr << "Error setting edge " << device << "\n"; |
| 296 | return; |
| 297 | } |
| 298 | deviceFile << "both"; |
| 299 | deviceFile.close(); |
| 300 | |
| 301 | inputDev.assign(boost::asio::ip::tcp::v4(), fd); |
| 302 | monitorPresence(); |
| 303 | read(); |
| 304 | } |
| 305 | |
| 306 | PresenceSensor::~PresenceSensor() |
| 307 | { |
| 308 | inputDev.close(); |
| 309 | close(fd); |
| 310 | } |
| 311 | |
| 312 | void PresenceSensor::monitorPresence(void) |
| 313 | { |
| 314 | inputDev.async_wait(boost::asio::ip::tcp::socket::wait_error, |
| 315 | [this](const boost::system::error_code &ec) { |
| 316 | if (ec == boost::system::errc::bad_file_descriptor) |
| 317 | { |
| 318 | return; // we're being destroyed |
| 319 | } |
| 320 | else if (ec) |
| 321 | { |
| 322 | std::cerr |
| 323 | << "Error on presence sensor socket\n"; |
| 324 | } |
| 325 | else |
| 326 | { |
| 327 | read(); |
| 328 | } |
| 329 | monitorPresence(); |
| 330 | }); |
| 331 | } |
| 332 | |
| 333 | void PresenceSensor::read(void) |
| 334 | { |
| 335 | constexpr size_t readSize = sizeof("0"); |
| 336 | std::string readBuf; |
| 337 | readBuf.resize(readSize); |
| 338 | lseek(fd, 0, SEEK_SET); |
| 339 | size_t r = ::read(fd, readBuf.data(), readSize); |
| 340 | if (r != 1) |
| 341 | { |
| 342 | std::cerr << "Error reading gpio\n"; |
| 343 | } |
| 344 | else |
| 345 | { |
| 346 | bool value = std::stoi(readBuf); |
| 347 | if (inverted) |
| 348 | { |
| 349 | value = !value; |
| 350 | } |
| 351 | status = value; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | bool PresenceSensor::getValue(void) |
| 356 | { |
| 357 | return status; |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | RedundancySensor::RedundancySensor( |
| 361 | size_t count, const std::vector<std::string> &children, |
| 362 | sdbusplus::asio::object_server &objectServer) : |
| 363 | count(count), |
| 364 | iface(objectServer.add_interface( |
| 365 | "/xyz/openbmc_project/control/FanRedundancy/Tach", |
| 366 | "xyz.openbmc_project.control.FanRedundancy")), |
| 367 | objectServer(objectServer) |
| 368 | { |
| 369 | iface->register_property("Collection", children); |
| 370 | iface->register_property("Status", std::string("Full")); |
| 371 | iface->register_property("AllowedFailures", static_cast<uint8_t>(count)); |
| 372 | iface->initialize(); |
| 373 | } |
| 374 | RedundancySensor::~RedundancySensor() |
| 375 | { |
| 376 | objectServer.remove_interface(iface); |
| 377 | } |
| 378 | void RedundancySensor::update(const std::string &name, bool failed) |
| 379 | { |
| 380 | statuses[name] = failed; |
| 381 | size_t failedCount = 0; |
| 382 | |
| 383 | std::string state = "Full"; |
| 384 | for (const auto &status : statuses) |
| 385 | { |
| 386 | if (status.second) |
| 387 | { |
| 388 | failedCount++; |
| 389 | } |
| 390 | if (failedCount > count) |
| 391 | { |
| 392 | state = "Failed"; |
| 393 | break; |
| 394 | } |
| 395 | else if (failedCount) |
| 396 | { |
| 397 | state = "Degraded"; |
| 398 | } |
| 399 | } |
| 400 | iface->set_property("Status", state); |
| 401 | } |