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