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 | |
Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 17 | #include "TachSensor.hpp" |
| 18 | |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 19 | #include "SensorPaths.hpp" |
| 20 | #include "Thresholds.hpp" |
Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 21 | #include "Utils.hpp" |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 22 | #include "sensor.hpp" |
Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 23 | |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 24 | #include <boost/asio/buffer.hpp> |
| 25 | #include <boost/asio/error.hpp> |
| 26 | #include <boost/asio/io_context.hpp> |
| 27 | #include <boost/asio/posix/stream_descriptor.hpp> |
| 28 | #include <boost/asio/random_access_file.hpp> |
Zhikui Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 29 | #include <gpiod.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 30 | #include <sdbusplus/asio/connection.hpp> |
| 31 | #include <sdbusplus/asio/object_server.hpp> |
| 32 | |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 33 | #include <charconv> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 34 | #include <chrono> |
| 35 | #include <cstddef> |
| 36 | #include <cstdint> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 37 | #include <iostream> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 38 | #include <memory> |
| 39 | #include <optional> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 40 | #include <string> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 41 | #include <system_error> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 42 | #include <utility> |
| 43 | #include <vector> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 44 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 45 | static constexpr unsigned int pwmPollMs = 500; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 46 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 47 | TachSensor::TachSensor(const std::string& path, const std::string& objectType, |
| 48 | sdbusplus::asio::object_server& objectServer, |
| 49 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
James Feist | 60c0ec7 | 2019-03-18 15:08:43 -0700 | [diff] [blame] | 50 | std::unique_ptr<PresenceSensor>&& presenceSensor, |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 51 | std::optional<RedundancySensor>* redundancy, |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 52 | boost::asio::io_context& io, const std::string& fanName, |
Jeff Lin | 7b7a9de | 2021-02-22 11:16:27 +0800 | [diff] [blame] | 53 | std::vector<thresholds::Threshold>&& thresholdsIn, |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 54 | const std::string& sensorConfiguration, |
Ed Tanous | f69fbf9 | 2022-01-14 10:15:33 -0800 | [diff] [blame] | 55 | const std::pair<double, double>& limits, |
James Feist | 49a8ccd | 2020-09-16 16:09:52 -0700 | [diff] [blame] | 56 | const PowerState& powerState, |
| 57 | const std::optional<std::string>& ledIn) : |
Zhikui Ren | da98f09 | 2021-11-01 09:41:08 -0700 | [diff] [blame] | 58 | Sensor(escapeName(fanName), std::move(thresholdsIn), sensorConfiguration, |
| 59 | objectType, false, false, limits.second, limits.first, conn, |
| 60 | powerState), |
Brad Bishop | fbb44ad | 2019-11-08 09:42:37 -0500 | [diff] [blame] | 61 | objServer(objectServer), redundancy(redundancy), |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 62 | presence(std::move(presenceSensor)), |
| 63 | inputDev(io, path, boost::asio::random_access_file::read_only), |
| 64 | waitTimer(io), path(path), led(ledIn) |
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 | sensorInterface = objectServer.add_interface( |
| 67 | "/xyz/openbmc_project/sensors/fan_tach/" + name, |
| 68 | "xyz.openbmc_project.Sensor.Value"); |
| 69 | |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 70 | for (const auto& threshold : thresholds) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 71 | { |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 72 | std::string interface = thresholds::getInterface(threshold.level); |
| 73 | thresholdInterfaces[static_cast<size_t>(threshold.level)] = |
| 74 | objectServer.add_interface( |
| 75 | "/xyz/openbmc_project/sensors/fan_tach/" + name, interface); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 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( |
Glukhov Mikhail | 6e6561d | 2024-01-10 11:58:48 +0300 | [diff] [blame] | 93 | "Associations", |
James Feist | d8bd562 | 2019-06-26 12:09:05 -0700 | [diff] [blame] | 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 | } |
Andrei Kartashev | 3928741 | 2022-02-04 16:04:47 +0300 | [diff] [blame] | 99 | setInitialProperties(sensor_paths::unitRPMs); |
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(); |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 107 | for (const auto& iface : thresholdInterfaces) |
| 108 | { |
| 109 | objServer.remove_interface(iface); |
| 110 | } |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 111 | objServer.remove_interface(sensorInterface); |
James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 112 | objServer.remove_interface(association); |
James Feist | 60c0ec7 | 2019-03-18 15:08:43 -0700 | [diff] [blame] | 113 | objServer.remove_interface(itemIface); |
James Feist | d8bd562 | 2019-06-26 12:09:05 -0700 | [diff] [blame] | 114 | objServer.remove_interface(itemAssoc); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 117 | void TachSensor::setupRead() |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 118 | { |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 119 | std::weak_ptr<TachSensor> weakRef = weak_from_this(); |
| 120 | inputDev.async_read_some_at( |
| 121 | 0, boost::asio::buffer(readBuf), |
| 122 | [weakRef](const boost::system::error_code& ec, std::size_t bytesRead) { |
| 123 | std::shared_ptr<TachSensor> self = weakRef.lock(); |
| 124 | if (self) |
| 125 | { |
| 126 | self->handleResponse(ec, bytesRead); |
| 127 | } |
Patrick Williams | 597e842 | 2023-10-20 11:19:01 -0500 | [diff] [blame] | 128 | }); |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void TachSensor::restartRead(size_t pollTime) |
| 132 | { |
| 133 | std::weak_ptr<TachSensor> weakRef = weak_from_this(); |
Ed Tanous | 83db50c | 2023-03-01 10:20:24 -0800 | [diff] [blame] | 134 | waitTimer.expires_after(std::chrono::milliseconds(pollTime)); |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 135 | waitTimer.async_wait([weakRef](const boost::system::error_code& ec) { |
| 136 | if (ec == boost::asio::error::operation_aborted) |
| 137 | { |
| 138 | return; // we're being canceled |
| 139 | } |
| 140 | std::shared_ptr<TachSensor> self = weakRef.lock(); |
| 141 | if (!self) |
| 142 | { |
| 143 | return; |
| 144 | } |
| 145 | self->setupRead(); |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 146 | }); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 149 | void TachSensor::handleResponse(const boost::system::error_code& err, |
| 150 | size_t bytesRead) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 151 | { |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 152 | if ((err == boost::system::errc::bad_file_descriptor) || |
| 153 | (err == boost::asio::error::misc_errors::not_found)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 154 | { |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 155 | std::cerr << "TachSensor " << name << " removed " << path << "\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 156 | return; // we're being destroyed |
| 157 | } |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 158 | bool missing = false; |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 159 | size_t pollTime = pwmPollMs; |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 160 | if (presence) |
| 161 | { |
| 162 | if (!presence->getValue()) |
| 163 | { |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 164 | markAvailable(false); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 165 | missing = true; |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 166 | pollTime = sensorFailedPollTimeMs; |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 167 | } |
James Feist | 60c0ec7 | 2019-03-18 15:08:43 -0700 | [diff] [blame] | 168 | itemIface->set_property("Present", !missing); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 169 | } |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 170 | |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 171 | if (!missing) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 172 | { |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 173 | if (!err) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 174 | { |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 175 | const char* bufEnd = readBuf.data() + bytesRead; |
| 176 | int nvalue = 0; |
Patrick Williams | 779c96a | 2023-05-10 07:50:42 -0500 | [diff] [blame] | 177 | std::from_chars_result ret = std::from_chars(readBuf.data(), bufEnd, |
| 178 | nvalue); |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 179 | if (ret.ec != std::errc()) |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 180 | { |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 181 | incrementError(); |
| 182 | pollTime = sensorFailedPollTimeMs; |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 183 | } |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 184 | else |
| 185 | { |
| 186 | updateValue(nvalue); |
| 187 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 188 | } |
| 189 | else |
| 190 | { |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 191 | incrementError(); |
| 192 | pollTime = sensorFailedPollTimeMs; |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 193 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 194 | } |
Ed Tanous | 99c4409 | 2022-01-14 09:59:09 -0800 | [diff] [blame] | 195 | |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 196 | restartRead(pollTime); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 199 | void TachSensor::checkThresholds() |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 200 | { |
Zhikui Ren | 00bd56d | 2021-11-19 12:32:27 -0800 | [diff] [blame] | 201 | bool status = thresholds::checkThresholds(this); |
James Feist | 95e5490 | 2019-09-04 15:13:36 -0700 | [diff] [blame] | 202 | |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 203 | if ((redundancy != nullptr) && *redundancy) |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 204 | { |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 205 | (*redundancy) |
| 206 | ->update("/xyz/openbmc_project/sensors/fan_tach/" + name, !status); |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 207 | } |
James Feist | 49a8ccd | 2020-09-16 16:09:52 -0700 | [diff] [blame] | 208 | |
| 209 | bool curLed = !status; |
| 210 | if (led && ledState != curLed) |
| 211 | { |
| 212 | ledState = curLed; |
| 213 | setLed(dbusConnection, *led, curLed); |
| 214 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Zhikui Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 217 | PresenceSensor::PresenceSensor(const std::string& gpioName, bool inverted, |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 218 | boost::asio::io_context& io, |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 219 | const std::string& name) : |
Ed Tanous | 8a17c30 | 2021-09-02 15:07:11 -0700 | [diff] [blame] | 220 | gpioLine(gpiod::find_line(gpioName)), |
| 221 | gpioFd(io), name(name) |
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 | if (!gpioLine) |
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 | std::cerr << "Error requesting gpio: " << gpioName << "\n"; |
| 226 | status = false; |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 227 | return; |
| 228 | } |
| 229 | |
Zhikui Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 230 | try |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 231 | { |
Zhikui Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 232 | gpioLine.request({"FanSensor", gpiod::line_request::EVENT_BOTH_EDGES, |
| 233 | inverted ? gpiod::line_request::FLAG_ACTIVE_LOW : 0}); |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 234 | status = (gpioLine.get_value() != 0); |
Zhikui Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 235 | |
| 236 | int gpioLineFd = gpioLine.event_get_fd(); |
| 237 | if (gpioLineFd < 0) |
| 238 | { |
| 239 | std::cerr << "Failed to get " << gpioName << " fd\n"; |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | gpioFd.assign(gpioLineFd); |
| 244 | } |
Patrick Williams | 26601e8 | 2021-10-06 12:43:25 -0500 | [diff] [blame] | 245 | catch (const std::system_error&) |
Zhikui Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 246 | { |
| 247 | std::cerr << "Error reading gpio: " << gpioName << "\n"; |
| 248 | status = false; |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 249 | return; |
| 250 | } |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 251 | |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 252 | monitorPresence(); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | PresenceSensor::~PresenceSensor() |
| 256 | { |
Zhikui Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 257 | gpioFd.close(); |
| 258 | gpioLine.release(); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 261 | void PresenceSensor::monitorPresence() |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 262 | { |
Zhikui Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 263 | gpioFd.async_wait(boost::asio::posix::stream_descriptor::wait_read, |
| 264 | [this](const boost::system::error_code& ec) { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 265 | if (ec == boost::system::errc::bad_file_descriptor) |
| 266 | { |
| 267 | return; // we're being destroyed |
| 268 | } |
| 269 | if (ec) |
| 270 | { |
| 271 | std::cerr << "Error on presence sensor " << name << " \n"; |
| 272 | ; |
| 273 | } |
| 274 | else |
| 275 | { |
| 276 | read(); |
| 277 | } |
| 278 | monitorPresence(); |
| 279 | }); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 282 | void PresenceSensor::read() |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 283 | { |
Zhikui Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 284 | gpioLine.event_read(); |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 285 | status = (gpioLine.get_value() != 0); |
Zhikui Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 286 | // Read is invoked when an edge event is detected by monitorPresence |
| 287 | if (status) |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 288 | { |
Zhikui Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 289 | logFanInserted(name); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 290 | } |
| 291 | else |
| 292 | { |
Zhikui Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 293 | logFanRemoved(name); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 297 | bool PresenceSensor::getValue() const |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 298 | { |
| 299 | return status; |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 300 | } |
| 301 | |
James Feist | 9bb6746 | 2019-03-15 15:09:50 -0700 | [diff] [blame] | 302 | RedundancySensor::RedundancySensor(size_t count, |
| 303 | const std::vector<std::string>& children, |
| 304 | sdbusplus::asio::object_server& objectServer, |
| 305 | const std::string& sensorConfiguration) : |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 306 | count(count), |
| 307 | iface(objectServer.add_interface( |
| 308 | "/xyz/openbmc_project/control/FanRedundancy/Tach", |
James Feist | 9bb6746 | 2019-03-15 15:09:50 -0700 | [diff] [blame] | 309 | "xyz.openbmc_project.Control.FanRedundancy")), |
| 310 | association(objectServer.add_interface( |
| 311 | "/xyz/openbmc_project/control/FanRedundancy/Tach", |
James Feist | 2adc95c | 2019-09-30 14:55:28 -0700 | [diff] [blame] | 312 | association::interface)), |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 313 | objectServer(objectServer) |
| 314 | { |
James Feist | 9bb6746 | 2019-03-15 15:09:50 -0700 | [diff] [blame] | 315 | createAssociation(association, sensorConfiguration); |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 316 | iface->register_property("Collection", children); |
| 317 | iface->register_property("Status", std::string("Full")); |
| 318 | iface->register_property("AllowedFailures", static_cast<uint8_t>(count)); |
| 319 | iface->initialize(); |
| 320 | } |
| 321 | RedundancySensor::~RedundancySensor() |
| 322 | { |
James Feist | 9bb6746 | 2019-03-15 15:09:50 -0700 | [diff] [blame] | 323 | objectServer.remove_interface(association); |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 324 | objectServer.remove_interface(iface); |
| 325 | } |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 326 | void RedundancySensor::update(const std::string& name, bool failed) |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 327 | { |
| 328 | statuses[name] = failed; |
| 329 | size_t failedCount = 0; |
| 330 | |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 331 | std::string newState = redundancy::full; |
Zev Weiss | 0521a06 | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 332 | for (const auto& [name, status] : statuses) |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 333 | { |
Zev Weiss | 0521a06 | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 334 | if (status) |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 335 | { |
| 336 | failedCount++; |
| 337 | } |
| 338 | if (failedCount > count) |
| 339 | { |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 340 | newState = redundancy::failed; |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 341 | break; |
| 342 | } |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 343 | if (failedCount != 0U) |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 344 | { |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 345 | newState = redundancy::degraded; |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 346 | } |
| 347 | } |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 348 | if (state != newState) |
| 349 | { |
| 350 | if (state == redundancy::full) |
| 351 | { |
| 352 | logFanRedundancyLost(); |
| 353 | } |
| 354 | else if (newState == redundancy::full) |
| 355 | { |
| 356 | logFanRedundancyRestored(); |
| 357 | } |
| 358 | state = newState; |
| 359 | iface->set_property("Status", state); |
| 360 | } |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 361 | } |