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