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