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