James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2017 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 "ADCSensor.hpp" |
| 18 | |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 19 | #include "SensorPaths.hpp" |
| 20 | #include "Thresholds.hpp" |
| 21 | #include "Utils.hpp" |
| 22 | #include "sensor.hpp" |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 23 | |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 24 | #include <fcntl.h> |
| 25 | |
| 26 | #include <boost/asio/error.hpp> |
| 27 | #include <boost/asio/io_context.hpp> |
James Feist | 8086aba | 2020-08-25 16:00:59 -0700 | [diff] [blame] | 28 | #include <boost/asio/read_until.hpp> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 29 | #include <boost/asio/streambuf.hpp> |
George Liu | d4bc41f | 2025-02-20 09:21:38 +0800 | [diff] [blame^] | 30 | #include <phosphor-logging/lg2.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 31 | #include <sdbusplus/asio/connection.hpp> |
| 32 | #include <sdbusplus/asio/object_server.hpp> |
| 33 | |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 34 | #include <chrono> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 35 | #include <cmath> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 36 | #include <cstddef> |
George Liu | d4bc41f | 2025-02-20 09:21:38 +0800 | [diff] [blame^] | 37 | #include <istream> |
Thang Tran | af1724b | 2024-12-09 13:37:01 +0700 | [diff] [blame] | 38 | #include <limits> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 39 | #include <memory> |
Zhu, Yunge | a5b1bbc | 2019-04-09 19:49:36 -0400 | [diff] [blame] | 40 | #include <optional> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 41 | #include <stdexcept> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 42 | #include <string> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 43 | #include <utility> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 44 | #include <vector> |
Patrick Venture | 2da370e | 2019-11-01 11:51:31 -0700 | [diff] [blame] | 45 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 46 | // scaling factor from hwmon |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 47 | static constexpr unsigned int sensorScaleFactor = 1000; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 48 | |
Zhikui Ren | 937eb54 | 2020-10-12 13:42:08 -0700 | [diff] [blame] | 49 | static constexpr double roundFactor = 10000; // 3 decimal places |
| 50 | static constexpr double maxVoltageReading = 1.8; // pre sensor scaling |
| 51 | static constexpr double minVoltageReading = 0; |
James Feist | cc6d4fe | 2018-11-14 16:38:26 -0800 | [diff] [blame] | 52 | |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 53 | ADCSensor::ADCSensor( |
| 54 | const std::string& path, sdbusplus::asio::object_server& objectServer, |
| 55 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 56 | boost::asio::io_context& io, const std::string& sensorName, |
| 57 | std::vector<thresholds::Threshold>&& thresholdsIn, const double scaleFactor, |
| 58 | const float pollRate, PowerState readState, |
| 59 | const std::string& sensorConfiguration, |
| 60 | std::optional<BridgeGpio>&& bridgeGpio) : |
Zhikui Ren | da98f09 | 2021-11-01 09:41:08 -0700 | [diff] [blame] | 61 | Sensor(escapeName(sensorName), std::move(thresholdsIn), sensorConfiguration, |
Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 62 | "ADC", false, false, maxVoltageReading / scaleFactor, |
| 63 | minVoltageReading / scaleFactor, conn, readState), |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 64 | objServer(objectServer), inputDev(io), waitTimer(io), path(path), |
| 65 | scaleFactor(scaleFactor), |
Jeff Lin | d9cd704 | 2020-11-20 15:49:28 +0800 | [diff] [blame] | 66 | sensorPollMs(static_cast<unsigned int>(pollRate * 1000)), |
Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 67 | bridgeGpio(std::move(bridgeGpio)), thresholdTimer(io) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 68 | { |
Ed Tanous | 99c4409 | 2022-01-14 09:59:09 -0800 | [diff] [blame] | 69 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) |
| 70 | int fd = open(path.c_str(), O_RDONLY); |
| 71 | if (fd < 0) |
| 72 | { |
George Liu | d4bc41f | 2025-02-20 09:21:38 +0800 | [diff] [blame^] | 73 | lg2::error("unable to open acd device"); |
Ed Tanous | 99c4409 | 2022-01-14 09:59:09 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | inputDev.assign(fd); |
| 77 | |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 78 | sensorInterface = objectServer.add_interface( |
| 79 | "/xyz/openbmc_project/sensors/voltage/" + name, |
| 80 | "xyz.openbmc_project.Sensor.Value"); |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 81 | for (const auto& threshold : thresholds) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 82 | { |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 83 | std::string interface = thresholds::getInterface(threshold.level); |
| 84 | thresholdInterfaces[static_cast<size_t>(threshold.level)] = |
| 85 | objectServer.add_interface( |
| 86 | "/xyz/openbmc_project/sensors/voltage/" + name, interface); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 87 | } |
James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 88 | association = objectServer.add_interface( |
James Feist | 2adc95c | 2019-09-30 14:55:28 -0700 | [diff] [blame] | 89 | "/xyz/openbmc_project/sensors/voltage/" + name, association::interface); |
Andrei Kartashev | 3928741 | 2022-02-04 16:04:47 +0300 | [diff] [blame] | 90 | setInitialProperties(sensor_paths::unitVolts); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | ADCSensor::~ADCSensor() |
| 94 | { |
| 95 | // close the input dev to cancel async operations |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 96 | inputDev.close(); |
| 97 | waitTimer.cancel(); |
Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 98 | |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 99 | for (const auto& iface : thresholdInterfaces) |
| 100 | { |
| 101 | objServer.remove_interface(iface); |
| 102 | } |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 103 | objServer.remove_interface(sensorInterface); |
James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 104 | objServer.remove_interface(association); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 107 | void ADCSensor::setupRead() |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 108 | { |
Thang Tran | af1724b | 2024-12-09 13:37:01 +0700 | [diff] [blame] | 109 | if (!readingStateGood()) |
| 110 | { |
| 111 | markAvailable(false); |
| 112 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
| 113 | restartRead(); |
| 114 | return; |
| 115 | } |
| 116 | |
Yong Li | 1afda6b | 2020-04-09 19:24:13 +0800 | [diff] [blame] | 117 | std::shared_ptr<boost::asio::streambuf> buffer = |
| 118 | std::make_shared<boost::asio::streambuf>(); |
| 119 | |
| 120 | std::weak_ptr<ADCSensor> weakRef = weak_from_this(); |
| 121 | |
Zhu, Yunge | a5b1bbc | 2019-04-09 19:49:36 -0400 | [diff] [blame] | 122 | if (bridgeGpio.has_value()) |
| 123 | { |
Jae Hyun Yoo | 12bbae0 | 2019-07-22 17:24:09 -0700 | [diff] [blame] | 124 | (*bridgeGpio).set(1); |
Zhu, Yunge | a5b1bbc | 2019-04-09 19:49:36 -0400 | [diff] [blame] | 125 | // In case a channel has a bridge circuit,we have to turn the bridge on |
| 126 | // prior to reading a value at least for one scan cycle to get a valid |
| 127 | // value. Guarantee that the HW signal can be stable, the HW signal |
| 128 | // could be instability. |
Ed Tanous | 83db50c | 2023-03-01 10:20:24 -0800 | [diff] [blame] | 129 | waitTimer.expires_after( |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 130 | std::chrono::milliseconds(bridgeGpio->setupTimeMs)); |
Yong Li | 1afda6b | 2020-04-09 19:24:13 +0800 | [diff] [blame] | 131 | waitTimer.async_wait( |
| 132 | [weakRef, buffer](const boost::system::error_code& ec) { |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 133 | std::shared_ptr<ADCSensor> self = weakRef.lock(); |
| 134 | if (ec == boost::asio::error::operation_aborted) |
| 135 | { |
| 136 | return; // we're being canceled |
| 137 | } |
Yong Li | 1afda6b | 2020-04-09 19:24:13 +0800 | [diff] [blame] | 138 | |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 139 | if (self) |
| 140 | { |
| 141 | boost::asio::async_read_until( |
| 142 | self->inputDev, *buffer, '\n', |
| 143 | [weakRef, buffer](const boost::system::error_code& ec, |
| 144 | std::size_t /*bytes_transfered*/) { |
| 145 | std::shared_ptr<ADCSensor> self = weakRef.lock(); |
| 146 | if (self) |
| 147 | { |
| 148 | self->readBuf = buffer; |
| 149 | self->handleResponse(ec); |
| 150 | } |
| 151 | }); |
| 152 | } |
| 153 | }); |
Zhu, Yunge | a5b1bbc | 2019-04-09 19:49:36 -0400 | [diff] [blame] | 154 | } |
| 155 | else |
| 156 | { |
| 157 | boost::asio::async_read_until( |
Yong Li | 1afda6b | 2020-04-09 19:24:13 +0800 | [diff] [blame] | 158 | inputDev, *buffer, '\n', |
| 159 | [weakRef, buffer](const boost::system::error_code& ec, |
| 160 | std::size_t /*bytes_transfered*/) { |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 161 | std::shared_ptr<ADCSensor> self = weakRef.lock(); |
| 162 | if (self) |
| 163 | { |
| 164 | self->readBuf = buffer; |
| 165 | self->handleResponse(ec); |
| 166 | } |
| 167 | }); |
Zhu, Yunge | a5b1bbc | 2019-04-09 19:49:36 -0400 | [diff] [blame] | 168 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Thang Tran | af1724b | 2024-12-09 13:37:01 +0700 | [diff] [blame] | 171 | void ADCSensor::restartRead() |
| 172 | { |
| 173 | std::weak_ptr<ADCSensor> weakRef = weak_from_this(); |
| 174 | waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs)); |
| 175 | waitTimer.async_wait([weakRef](const boost::system::error_code& ec) { |
| 176 | std::shared_ptr<ADCSensor> self = weakRef.lock(); |
| 177 | if (ec == boost::asio::error::operation_aborted) |
| 178 | { |
| 179 | if (self) |
| 180 | { |
George Liu | d4bc41f | 2025-02-20 09:21:38 +0800 | [diff] [blame^] | 181 | lg2::error("adcsensor '{NAME}' read cancelled", "NAME", |
| 182 | self->name); |
Thang Tran | af1724b | 2024-12-09 13:37:01 +0700 | [diff] [blame] | 183 | } |
| 184 | else |
| 185 | { |
George Liu | d4bc41f | 2025-02-20 09:21:38 +0800 | [diff] [blame^] | 186 | lg2::error("adcsensor read cancelled no self"); |
Thang Tran | af1724b | 2024-12-09 13:37:01 +0700 | [diff] [blame] | 187 | } |
| 188 | return; // we're being canceled |
| 189 | } |
| 190 | |
| 191 | if (self) |
| 192 | { |
| 193 | self->setupRead(); |
| 194 | } |
| 195 | else |
| 196 | { |
George Liu | d4bc41f | 2025-02-20 09:21:38 +0800 | [diff] [blame^] | 197 | lg2::error("adcsensor weakref no self"); |
Thang Tran | af1724b | 2024-12-09 13:37:01 +0700 | [diff] [blame] | 198 | } |
| 199 | }); |
| 200 | } |
| 201 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 202 | void ADCSensor::handleResponse(const boost::system::error_code& err) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 203 | { |
Yong Li | 1afda6b | 2020-04-09 19:24:13 +0800 | [diff] [blame] | 204 | std::weak_ptr<ADCSensor> weakRef = weak_from_this(); |
| 205 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 206 | if (err == boost::system::errc::bad_file_descriptor) |
| 207 | { |
| 208 | return; // we're being destroyed |
| 209 | } |
Yong Li | 1afda6b | 2020-04-09 19:24:13 +0800 | [diff] [blame] | 210 | std::istream responseStream(readBuf.get()); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 211 | |
| 212 | if (!err) |
| 213 | { |
| 214 | std::string response; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 215 | std::getline(responseStream, response); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 216 | |
| 217 | // todo read scaling factors from configuration |
| 218 | try |
| 219 | { |
Zhikui Ren | d3da128 | 2020-09-11 17:02:01 -0700 | [diff] [blame] | 220 | rawValue = std::stod(response); |
| 221 | double nvalue = (rawValue / sensorScaleFactor) / scaleFactor; |
James Feist | cc6d4fe | 2018-11-14 16:38:26 -0800 | [diff] [blame] | 222 | nvalue = std::round(nvalue * roundFactor) / roundFactor; |
Josh Lehan | 833661a | 2020-03-04 17:35:41 -0800 | [diff] [blame] | 223 | updateValue(nvalue); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 224 | } |
Patrick Williams | 26601e8 | 2021-10-06 12:43:25 -0500 | [diff] [blame] | 225 | catch (const std::invalid_argument&) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 226 | { |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 227 | incrementError(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | else |
| 231 | { |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 232 | incrementError(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 235 | responseStream.clear(); |
| 236 | inputDev.close(); |
Zhu, Yunge | a5b1bbc | 2019-04-09 19:49:36 -0400 | [diff] [blame] | 237 | if (bridgeGpio.has_value()) |
| 238 | { |
Jae Hyun Yoo | 12bbae0 | 2019-07-22 17:24:09 -0700 | [diff] [blame] | 239 | (*bridgeGpio).set(0); |
Zhu, Yunge | a5b1bbc | 2019-04-09 19:49:36 -0400 | [diff] [blame] | 240 | } |
Ed Tanous | 99c4409 | 2022-01-14 09:59:09 -0800 | [diff] [blame] | 241 | |
| 242 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 243 | int fd = open(path.c_str(), O_RDONLY); |
Jae Hyun Yoo | ef9665a | 2019-10-10 10:26:39 -0700 | [diff] [blame] | 244 | if (fd < 0) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 245 | { |
George Liu | d4bc41f | 2025-02-20 09:21:38 +0800 | [diff] [blame^] | 246 | lg2::error("adcsensor '{NAME}' failed to open '{PATH}'", "NAME", name, |
| 247 | "PATH", path); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 248 | return; // we're no longer valid |
| 249 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 250 | inputDev.assign(fd); |
Thang Tran | af1724b | 2024-12-09 13:37:01 +0700 | [diff] [blame] | 251 | restartRead(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 254 | void ADCSensor::checkThresholds() |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 255 | { |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 256 | if (!readingStateGood()) |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 257 | { |
| 258 | return; |
| 259 | } |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 260 | |
Zhikui Ren | 12c2c0e | 2021-04-28 17:21:21 -0700 | [diff] [blame] | 261 | thresholds::checkThresholdsPowerDelay(weak_from_this(), thresholdTimer); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 262 | } |