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