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