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