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), |
| 54 | sensorConfiguration, objectType, limits.second, limits.first), |
Brad Bishop | fbb44ad | 2019-11-08 09:42:37 -0500 | [diff] [blame] | 55 | objServer(objectServer), redundancy(redundancy), |
| 56 | presence(std::move(presenceSensor)), |
| 57 | inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), path(path), |
| 58 | errCount(0) |
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); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 99 | setupPowerMatch(conn); |
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 | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 136 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
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); |
Josh Lehan | 833661a | 2020-03-04 17:35:41 -0800 | [diff] [blame] | 151 | double nvalue = std::stod(response); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 152 | responseStream.clear(); |
Josh Lehan | 833661a | 2020-03-04 17:35:41 -0800 | [diff] [blame] | 153 | updateValue(nvalue); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 154 | errCount = 0; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 155 | } |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 156 | catch (const std::invalid_argument&) |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 157 | { |
| 158 | errCount++; |
| 159 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 160 | } |
| 161 | else |
| 162 | { |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 163 | if (!isPowerOn()) |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 164 | { |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 165 | errCount = 0; |
| 166 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 167 | } |
| 168 | else |
| 169 | { |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 170 | pollTime = sensorFailedPollTimeMs; |
| 171 | errCount++; |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 172 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 173 | } |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 174 | if (errCount >= warnAfterErrorCount) |
| 175 | { |
| 176 | // only print once |
| 177 | if (errCount == warnAfterErrorCount) |
| 178 | { |
| 179 | std::cerr << "Failure to read sensor " << name << " at " << path |
| 180 | << " ec:" << err << "\n"; |
| 181 | } |
| 182 | updateValue(0); |
| 183 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 184 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 185 | responseStream.clear(); |
| 186 | inputDev.close(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 187 | int fd = open(path.c_str(), O_RDONLY); |
Jae Hyun Yoo | ef9665a | 2019-10-10 10:26:39 -0700 | [diff] [blame] | 188 | if (fd < 0) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 189 | { |
| 190 | return; // we're no longer valid |
| 191 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 192 | inputDev.assign(fd); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 193 | waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime)); |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 194 | waitTimer.async_wait([&](const boost::system::error_code& ec) { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 195 | if (ec == boost::asio::error::operation_aborted) |
| 196 | { |
| 197 | return; // we're being canceled |
| 198 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 199 | setupRead(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 200 | }); |
| 201 | } |
| 202 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 203 | void TachSensor::checkThresholds(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 204 | { |
James Feist | 95e5490 | 2019-09-04 15:13:36 -0700 | [diff] [blame] | 205 | if (!isPowerOn()) |
| 206 | { |
| 207 | return; |
| 208 | } |
| 209 | |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 210 | bool status = thresholds::checkThresholds(this); |
James Feist | 95e5490 | 2019-09-04 15:13:36 -0700 | [diff] [blame] | 211 | |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 212 | if (redundancy && *redundancy) |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 213 | { |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 214 | (*redundancy) |
| 215 | ->update("/xyz/openbmc_project/sensors/fan_tach/" + name, !status); |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 216 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Zhikui Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 219 | PresenceSensor::PresenceSensor(const std::string& gpioName, bool inverted, |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 220 | boost::asio::io_service& io, |
| 221 | const std::string& name) : |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 222 | inverted(inverted), |
Zhikui Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 223 | gpioLine(gpiod::find_line(gpioName)), gpioFd(io), name(name) |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 224 | { |
Zhikui Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 225 | if (!gpioLine) |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 226 | { |
Zhikui Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 227 | std::cerr << "Error requesting gpio: " << gpioName << "\n"; |
| 228 | status = false; |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 229 | return; |
| 230 | } |
| 231 | |
Zhikui Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 232 | try |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 233 | { |
Zhikui Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 234 | gpioLine.request({"FanSensor", gpiod::line_request::EVENT_BOTH_EDGES, |
| 235 | inverted ? gpiod::line_request::FLAG_ACTIVE_LOW : 0}); |
| 236 | status = gpioLine.get_value(); |
| 237 | |
| 238 | int gpioLineFd = gpioLine.event_get_fd(); |
| 239 | if (gpioLineFd < 0) |
| 240 | { |
| 241 | std::cerr << "Failed to get " << gpioName << " fd\n"; |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | gpioFd.assign(gpioLineFd); |
| 246 | } |
| 247 | catch (std::system_error&) |
| 248 | { |
| 249 | std::cerr << "Error reading gpio: " << gpioName << "\n"; |
| 250 | status = false; |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 251 | return; |
| 252 | } |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 253 | |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 254 | monitorPresence(); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | PresenceSensor::~PresenceSensor() |
| 258 | { |
Zhikui Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 259 | gpioFd.close(); |
| 260 | gpioLine.release(); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | void PresenceSensor::monitorPresence(void) |
| 264 | { |
Zhikui Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 265 | gpioFd.async_wait(boost::asio::posix::stream_descriptor::wait_read, |
| 266 | [this](const boost::system::error_code& ec) { |
| 267 | if (ec == boost::system::errc::bad_file_descriptor) |
| 268 | { |
| 269 | return; // we're being destroyed |
| 270 | } |
| 271 | else if (ec) |
| 272 | { |
| 273 | std::cerr << "Error on presence sensor " << name |
| 274 | << " \n"; |
| 275 | ; |
| 276 | } |
| 277 | else |
| 278 | { |
| 279 | read(); |
| 280 | } |
| 281 | monitorPresence(); |
| 282 | }); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | void PresenceSensor::read(void) |
| 286 | { |
Zhikui Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 287 | gpioLine.event_read(); |
| 288 | status = gpioLine.get_value(); |
| 289 | // Read is invoked when an edge event is detected by monitorPresence |
| 290 | if (status) |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 291 | { |
Zhikui Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 292 | logFanInserted(name); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 293 | } |
| 294 | else |
| 295 | { |
Zhikui Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 296 | logFanRemoved(name); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
| 300 | bool PresenceSensor::getValue(void) |
| 301 | { |
| 302 | return status; |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 303 | } |
| 304 | |
James Feist | 9bb6746 | 2019-03-15 15:09:50 -0700 | [diff] [blame] | 305 | RedundancySensor::RedundancySensor(size_t count, |
| 306 | const std::vector<std::string>& children, |
| 307 | sdbusplus::asio::object_server& objectServer, |
| 308 | const std::string& sensorConfiguration) : |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 309 | count(count), |
| 310 | iface(objectServer.add_interface( |
| 311 | "/xyz/openbmc_project/control/FanRedundancy/Tach", |
James Feist | 9bb6746 | 2019-03-15 15:09:50 -0700 | [diff] [blame] | 312 | "xyz.openbmc_project.Control.FanRedundancy")), |
| 313 | association(objectServer.add_interface( |
| 314 | "/xyz/openbmc_project/control/FanRedundancy/Tach", |
James Feist | 2adc95c | 2019-09-30 14:55:28 -0700 | [diff] [blame] | 315 | association::interface)), |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 316 | objectServer(objectServer) |
| 317 | { |
James Feist | 9bb6746 | 2019-03-15 15:09:50 -0700 | [diff] [blame] | 318 | createAssociation(association, sensorConfiguration); |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 319 | iface->register_property("Collection", children); |
| 320 | iface->register_property("Status", std::string("Full")); |
| 321 | iface->register_property("AllowedFailures", static_cast<uint8_t>(count)); |
| 322 | iface->initialize(); |
| 323 | } |
| 324 | RedundancySensor::~RedundancySensor() |
| 325 | { |
James Feist | 9bb6746 | 2019-03-15 15:09:50 -0700 | [diff] [blame] | 326 | objectServer.remove_interface(association); |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 327 | objectServer.remove_interface(iface); |
| 328 | } |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 329 | void RedundancySensor::update(const std::string& name, bool failed) |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 330 | { |
| 331 | statuses[name] = failed; |
| 332 | size_t failedCount = 0; |
| 333 | |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 334 | std::string newState = redundancy::full; |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 335 | for (const auto& status : statuses) |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 336 | { |
| 337 | if (status.second) |
| 338 | { |
| 339 | failedCount++; |
| 340 | } |
| 341 | if (failedCount > count) |
| 342 | { |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 343 | newState = redundancy::failed; |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 344 | break; |
| 345 | } |
| 346 | else if (failedCount) |
| 347 | { |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 348 | newState = redundancy::degraded; |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 349 | } |
| 350 | } |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 351 | if (state != newState) |
| 352 | { |
| 353 | if (state == redundancy::full) |
| 354 | { |
| 355 | logFanRedundancyLost(); |
| 356 | } |
| 357 | else if (newState == redundancy::full) |
| 358 | { |
| 359 | logFanRedundancyRestored(); |
| 360 | } |
| 361 | state = newState; |
| 362 | iface->set_property("Status", state); |
| 363 | } |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 364 | } |