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