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 | |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 17 | #include "TachSensor.hpp" |
| 18 | |
| 19 | #include "Utils.hpp" |
| 20 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 21 | #include <unistd.h> |
| 22 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 23 | #include <boost/algorithm/string/predicate.hpp> |
| 24 | #include <boost/algorithm/string/replace.hpp> |
| 25 | #include <boost/date_time/posix_time/posix_time.hpp> |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 26 | #include <fstream> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 27 | #include <iostream> |
| 28 | #include <limits> |
| 29 | #include <sdbusplus/asio/connection.hpp> |
| 30 | #include <sdbusplus/asio/object_server.hpp> |
| 31 | #include <string> |
| 32 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 33 | static constexpr unsigned int pwmPollMs = 500; |
| 34 | static constexpr size_t warnAfterErrorCount = 10; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 35 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 36 | TachSensor::TachSensor(const std::string& path, const std::string& objectType, |
| 37 | sdbusplus::asio::object_server& objectServer, |
| 38 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
James Feist | 60c0ec7 | 2019-03-18 15:08:43 -0700 | [diff] [blame] | 39 | std::unique_ptr<PresenceSensor>&& presenceSensor, |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 40 | std::optional<RedundancySensor>* redundancy, |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 41 | boost::asio::io_service& io, const std::string& fanName, |
| 42 | std::vector<thresholds::Threshold>&& _thresholds, |
| 43 | const std::string& sensorConfiguration, |
| 44 | const std::pair<size_t, size_t>& limits) : |
James Feist | 930fcde | 2019-05-28 12:58:43 -0700 | [diff] [blame] | 45 | Sensor(boost::replace_all_copy(fanName, " ", "_"), std::move(_thresholds), |
| 46 | sensorConfiguration, objectType, limits.second, limits.first), |
| 47 | path(path), objServer(objectServer), presence(std::move(presenceSensor)), |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 48 | redundancy(redundancy), inputDev(io, open(path.c_str(), O_RDONLY)), |
| 49 | waitTimer(io), errCount(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 | } |
James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 67 | association = objectServer.add_interface( |
| 68 | "/xyz/openbmc_project/sensors/fan_tach/" + name, |
| 69 | "org.openbmc.Associations"); |
James Feist | 60c0ec7 | 2019-03-18 15:08:43 -0700 | [diff] [blame] | 70 | |
| 71 | if (presence) |
| 72 | { |
| 73 | itemIface = |
James Feist | d8bd562 | 2019-06-26 12:09:05 -0700 | [diff] [blame] | 74 | objectServer.add_interface("/xyz/openbmc_project/inventory/" + name, |
James Feist | 60c0ec7 | 2019-03-18 15:08:43 -0700 | [diff] [blame] | 75 | "xyz.openbmc_project.Inventory.Item"); |
| 76 | itemIface->register_property("PrettyName", |
| 77 | std::string()); // unused property |
| 78 | itemIface->register_property("Present", true); |
| 79 | itemIface->initialize(); |
James Feist | d8bd562 | 2019-06-26 12:09:05 -0700 | [diff] [blame] | 80 | itemAssoc = |
| 81 | objectServer.add_interface("/xyz/openbmc_project/inventory/" + name, |
| 82 | "org.openbmc.Associations"); |
| 83 | itemAssoc->register_property( |
| 84 | "associations", |
| 85 | std::vector<Association>{ |
| 86 | {"sensors", "inventory", |
| 87 | "/xyz/openbmc_project/sensors/fan_tach/" + name}}); |
| 88 | itemAssoc->initialize(); |
James Feist | 60c0ec7 | 2019-03-18 15:08:43 -0700 | [diff] [blame] | 89 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 90 | setInitialProperties(conn); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 91 | setupPowerMatch(conn); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 92 | setupRead(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | TachSensor::~TachSensor() |
| 96 | { |
| 97 | // close the input dev to cancel async operations |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 98 | inputDev.close(); |
| 99 | waitTimer.cancel(); |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 100 | objServer.remove_interface(thresholdInterfaceWarning); |
| 101 | objServer.remove_interface(thresholdInterfaceCritical); |
| 102 | objServer.remove_interface(sensorInterface); |
James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 103 | objServer.remove_interface(association); |
James Feist | 60c0ec7 | 2019-03-18 15:08:43 -0700 | [diff] [blame] | 104 | objServer.remove_interface(itemIface); |
James Feist | d8bd562 | 2019-06-26 12:09:05 -0700 | [diff] [blame] | 105 | objServer.remove_interface(itemAssoc); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 108 | void TachSensor::setupRead(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 109 | { |
| 110 | boost::asio::async_read_until( |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 111 | inputDev, readBuf, '\n', |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 112 | [&](const boost::system::error_code& ec, |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 113 | std::size_t /*bytes_transfered*/) { handleResponse(ec); }); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 114 | } |
| 115 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 116 | void TachSensor::handleResponse(const boost::system::error_code& err) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 117 | { |
| 118 | if (err == boost::system::errc::bad_file_descriptor) |
| 119 | { |
| 120 | return; // we're being destroyed |
| 121 | } |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 122 | bool missing = false; |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 123 | size_t pollTime = pwmPollMs; |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 124 | if (presence) |
| 125 | { |
| 126 | if (!presence->getValue()) |
| 127 | { |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 128 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 129 | missing = true; |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 130 | pollTime = sensorFailedPollTimeMs; |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 131 | } |
James Feist | 60c0ec7 | 2019-03-18 15:08:43 -0700 | [diff] [blame] | 132 | itemIface->set_property("Present", !missing); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 133 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 134 | std::istream responseStream(&readBuf); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 135 | if (!missing) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 136 | { |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 137 | if (!err) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 138 | { |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 139 | std::string response; |
| 140 | try |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 141 | { |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 142 | std::getline(responseStream, response); |
| 143 | float nvalue = std::stof(response); |
| 144 | responseStream.clear(); |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 145 | if (static_cast<double>(nvalue) != value) |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 146 | { |
| 147 | updateValue(nvalue); |
| 148 | } |
| 149 | errCount = 0; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 150 | } |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 151 | catch (const std::invalid_argument&) |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 152 | { |
| 153 | errCount++; |
| 154 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 155 | } |
| 156 | else |
| 157 | { |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 158 | if (!isPowerOn()) |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 159 | { |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 160 | errCount = 0; |
| 161 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 162 | } |
| 163 | else |
| 164 | { |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 165 | pollTime = sensorFailedPollTimeMs; |
| 166 | errCount++; |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 167 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 168 | } |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 169 | if (errCount >= warnAfterErrorCount) |
| 170 | { |
| 171 | // only print once |
| 172 | if (errCount == warnAfterErrorCount) |
| 173 | { |
| 174 | std::cerr << "Failure to read sensor " << name << " at " << path |
| 175 | << " ec:" << err << "\n"; |
| 176 | } |
| 177 | updateValue(0); |
| 178 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 179 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 180 | responseStream.clear(); |
| 181 | inputDev.close(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 182 | int fd = open(path.c_str(), O_RDONLY); |
| 183 | if (fd <= 0) |
| 184 | { |
| 185 | return; // we're no longer valid |
| 186 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 187 | inputDev.assign(fd); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 188 | waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime)); |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 189 | waitTimer.async_wait([&](const boost::system::error_code& ec) { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 190 | if (ec == boost::asio::error::operation_aborted) |
| 191 | { |
| 192 | return; // we're being canceled |
| 193 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 194 | setupRead(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 195 | }); |
| 196 | } |
| 197 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 198 | void TachSensor::checkThresholds(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 199 | { |
James Feist | 95e5490 | 2019-09-04 15:13:36 -0700 | [diff] [blame^] | 200 | if (!isPowerOn()) |
| 201 | { |
| 202 | return; |
| 203 | } |
| 204 | |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 205 | bool status = thresholds::checkThresholds(this); |
James Feist | 95e5490 | 2019-09-04 15:13:36 -0700 | [diff] [blame^] | 206 | |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 207 | if (redundancy && *redundancy) |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 208 | { |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 209 | (*redundancy) |
| 210 | ->update("/xyz/openbmc_project/sensors/fan_tach/" + name, !status); |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 211 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 212 | } |
| 213 | |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 214 | PresenceSensor::PresenceSensor(const size_t index, bool inverted, |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 215 | boost::asio::io_service& io, |
| 216 | const std::string& name) : |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 217 | inverted(inverted), |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 218 | inputDev(io), name(name) |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 219 | { |
| 220 | // todo: use gpiodaemon |
| 221 | std::string device = gpioPath + std::string("gpio") + std::to_string(index); |
| 222 | fd = open((device + "/value").c_str(), O_RDONLY); |
| 223 | if (fd < 0) |
| 224 | { |
| 225 | std::cerr << "Error opening gpio " << index << "\n"; |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | std::ofstream deviceFile(device + "/edge"); |
| 230 | if (!deviceFile.good()) |
| 231 | { |
| 232 | std::cerr << "Error setting edge " << device << "\n"; |
| 233 | return; |
| 234 | } |
| 235 | deviceFile << "both"; |
| 236 | deviceFile.close(); |
| 237 | |
| 238 | inputDev.assign(boost::asio::ip::tcp::v4(), fd); |
| 239 | monitorPresence(); |
| 240 | read(); |
| 241 | } |
| 242 | |
| 243 | PresenceSensor::~PresenceSensor() |
| 244 | { |
| 245 | inputDev.close(); |
| 246 | close(fd); |
| 247 | } |
| 248 | |
| 249 | void PresenceSensor::monitorPresence(void) |
| 250 | { |
| 251 | inputDev.async_wait(boost::asio::ip::tcp::socket::wait_error, |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 252 | [this](const boost::system::error_code& ec) { |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 253 | if (ec == boost::system::errc::bad_file_descriptor) |
| 254 | { |
| 255 | return; // we're being destroyed |
| 256 | } |
| 257 | else if (ec) |
| 258 | { |
| 259 | std::cerr |
| 260 | << "Error on presence sensor socket\n"; |
| 261 | } |
| 262 | else |
| 263 | { |
| 264 | read(); |
| 265 | } |
| 266 | monitorPresence(); |
| 267 | }); |
| 268 | } |
| 269 | |
| 270 | void PresenceSensor::read(void) |
| 271 | { |
| 272 | constexpr size_t readSize = sizeof("0"); |
| 273 | std::string readBuf; |
| 274 | readBuf.resize(readSize); |
| 275 | lseek(fd, 0, SEEK_SET); |
| 276 | size_t r = ::read(fd, readBuf.data(), readSize); |
James Feist | 95b079b | 2018-11-21 09:28:00 -0800 | [diff] [blame] | 277 | if (r != readSize) |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 278 | { |
| 279 | std::cerr << "Error reading gpio\n"; |
| 280 | } |
| 281 | else |
| 282 | { |
| 283 | bool value = std::stoi(readBuf); |
| 284 | if (inverted) |
| 285 | { |
| 286 | value = !value; |
| 287 | } |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 288 | if (value != status) |
| 289 | { |
| 290 | status = value; |
| 291 | if (status) |
| 292 | { |
| 293 | logFanInserted(name); |
| 294 | } |
| 295 | else |
| 296 | { |
| 297 | logFanRemoved(name); |
| 298 | } |
| 299 | } |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
| 303 | bool PresenceSensor::getValue(void) |
| 304 | { |
| 305 | return status; |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 306 | } |
| 307 | |
James Feist | 9bb6746 | 2019-03-15 15:09:50 -0700 | [diff] [blame] | 308 | RedundancySensor::RedundancySensor(size_t count, |
| 309 | const std::vector<std::string>& children, |
| 310 | sdbusplus::asio::object_server& objectServer, |
| 311 | const std::string& sensorConfiguration) : |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 312 | count(count), |
| 313 | iface(objectServer.add_interface( |
| 314 | "/xyz/openbmc_project/control/FanRedundancy/Tach", |
James Feist | 9bb6746 | 2019-03-15 15:09:50 -0700 | [diff] [blame] | 315 | "xyz.openbmc_project.Control.FanRedundancy")), |
| 316 | association(objectServer.add_interface( |
| 317 | "/xyz/openbmc_project/control/FanRedundancy/Tach", |
| 318 | "org.openbmc.Associations")), |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 319 | objectServer(objectServer) |
| 320 | { |
James Feist | 9bb6746 | 2019-03-15 15:09:50 -0700 | [diff] [blame] | 321 | createAssociation(association, sensorConfiguration); |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 322 | iface->register_property("Collection", children); |
| 323 | iface->register_property("Status", std::string("Full")); |
| 324 | iface->register_property("AllowedFailures", static_cast<uint8_t>(count)); |
| 325 | iface->initialize(); |
| 326 | } |
| 327 | RedundancySensor::~RedundancySensor() |
| 328 | { |
James Feist | 9bb6746 | 2019-03-15 15:09:50 -0700 | [diff] [blame] | 329 | objectServer.remove_interface(association); |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 330 | objectServer.remove_interface(iface); |
| 331 | } |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 332 | void RedundancySensor::update(const std::string& name, bool failed) |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 333 | { |
| 334 | statuses[name] = failed; |
| 335 | size_t failedCount = 0; |
| 336 | |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 337 | std::string newState = redundancy::full; |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 338 | for (const auto& status : statuses) |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 339 | { |
| 340 | if (status.second) |
| 341 | { |
| 342 | failedCount++; |
| 343 | } |
| 344 | if (failedCount > count) |
| 345 | { |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 346 | newState = redundancy::failed; |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 347 | break; |
| 348 | } |
| 349 | else if (failedCount) |
| 350 | { |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 351 | newState = redundancy::degraded; |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 352 | } |
| 353 | } |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 354 | if (state != newState) |
| 355 | { |
| 356 | if (state == redundancy::full) |
| 357 | { |
| 358 | logFanRedundancyLost(); |
| 359 | } |
| 360 | else if (newState == redundancy::full) |
| 361 | { |
| 362 | logFanRedundancyRestored(); |
| 363 | } |
| 364 | state = newState; |
| 365 | iface->set_property("Status", state); |
| 366 | } |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 367 | } |