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 | |
Patrick Venture | ca44b2f | 2019-10-31 11:02:26 -0700 | [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 | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 21 | #include <boost/algorithm/string/predicate.hpp> |
| 22 | #include <boost/algorithm/string/replace.hpp> |
| 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 | |
James Feist | cc6d4fe | 2018-11-14 16:38:26 -0800 | [diff] [blame] | 43 | static constexpr double roundFactor = 10000; // 3 decimal places |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 44 | static constexpr double maxReading = 20; |
| 45 | static constexpr double minReading = 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, |
| 51 | std::vector<thresholds::Threshold>&& _thresholds, |
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) : |
James Feist | 930fcde | 2019-05-28 12:58:43 -0700 | [diff] [blame] | 55 | Sensor(boost::replace_all_copy(sensorName, " ", "_"), |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 56 | std::move(_thresholds), sensorConfiguration, |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 57 | "xyz.openbmc_project.Configuration.ADC", maxReading, minReading, |
| 58 | readState), |
Yong Li | 1afda6b | 2020-04-09 19:24:13 +0800 | [diff] [blame] | 59 | std::enable_shared_from_this<ADCSensor>(), objServer(objectServer), |
| 60 | inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), path(path), |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 61 | scaleFactor(scaleFactor), bridgeGpio(std::move(bridgeGpio)), |
| 62 | thresholdTimer(io, this) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 63 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 64 | sensorInterface = objectServer.add_interface( |
| 65 | "/xyz/openbmc_project/sensors/voltage/" + name, |
| 66 | "xyz.openbmc_project.Sensor.Value"); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 67 | if (thresholds::hasWarningInterface(thresholds)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 68 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 69 | thresholdInterfaceWarning = objectServer.add_interface( |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 70 | "/xyz/openbmc_project/sensors/voltage/" + name, |
| 71 | "xyz.openbmc_project.Sensor.Threshold.Warning"); |
| 72 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 73 | if (thresholds::hasCriticalInterface(thresholds)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 74 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 75 | thresholdInterfaceCritical = objectServer.add_interface( |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 76 | "/xyz/openbmc_project/sensors/voltage/" + name, |
| 77 | "xyz.openbmc_project.Sensor.Threshold.Critical"); |
| 78 | } |
James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 79 | association = objectServer.add_interface( |
James Feist | 2adc95c | 2019-09-30 14:55:28 -0700 | [diff] [blame] | 80 | "/xyz/openbmc_project/sensors/voltage/" + name, association::interface); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 81 | setInitialProperties(conn); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | ADCSensor::~ADCSensor() |
| 85 | { |
| 86 | // close the input dev to cancel async operations |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 87 | inputDev.close(); |
| 88 | waitTimer.cancel(); |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 89 | objServer.remove_interface(thresholdInterfaceWarning); |
| 90 | objServer.remove_interface(thresholdInterfaceCritical); |
| 91 | objServer.remove_interface(sensorInterface); |
James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 92 | objServer.remove_interface(association); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 95 | void ADCSensor::setupRead(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 96 | { |
Yong Li | 1afda6b | 2020-04-09 19:24:13 +0800 | [diff] [blame] | 97 | std::shared_ptr<boost::asio::streambuf> buffer = |
| 98 | std::make_shared<boost::asio::streambuf>(); |
| 99 | |
| 100 | std::weak_ptr<ADCSensor> weakRef = weak_from_this(); |
| 101 | |
Zhu, Yunge | a5b1bbc | 2019-04-09 19:49:36 -0400 | [diff] [blame] | 102 | if (bridgeGpio.has_value()) |
| 103 | { |
Jae Hyun Yoo | 12bbae0 | 2019-07-22 17:24:09 -0700 | [diff] [blame] | 104 | (*bridgeGpio).set(1); |
Zhu, Yunge | a5b1bbc | 2019-04-09 19:49:36 -0400 | [diff] [blame] | 105 | // In case a channel has a bridge circuit,we have to turn the bridge on |
| 106 | // prior to reading a value at least for one scan cycle to get a valid |
| 107 | // value. Guarantee that the HW signal can be stable, the HW signal |
| 108 | // could be instability. |
| 109 | waitTimer.expires_from_now( |
| 110 | boost::posix_time::milliseconds(gpioBridgeEnableMs)); |
Yong Li | 1afda6b | 2020-04-09 19:24:13 +0800 | [diff] [blame] | 111 | waitTimer.async_wait( |
| 112 | [weakRef, buffer](const boost::system::error_code& ec) { |
| 113 | std::shared_ptr<ADCSensor> self = weakRef.lock(); |
| 114 | if (ec == boost::asio::error::operation_aborted) |
| 115 | { |
| 116 | return; // we're being canceled |
| 117 | } |
| 118 | |
| 119 | if (self) |
| 120 | { |
| 121 | boost::asio::async_read_until( |
| 122 | self->inputDev, *buffer, '\n', |
| 123 | [weakRef, buffer](const boost::system::error_code& ec, |
| 124 | std::size_t /*bytes_transfered*/) { |
| 125 | std::shared_ptr<ADCSensor> self = weakRef.lock(); |
| 126 | if (self) |
| 127 | { |
| 128 | self->readBuf = buffer; |
| 129 | self->handleResponse(ec); |
| 130 | } |
| 131 | }); |
| 132 | } |
| 133 | }); |
Zhu, Yunge | a5b1bbc | 2019-04-09 19:49:36 -0400 | [diff] [blame] | 134 | } |
| 135 | else |
| 136 | { |
| 137 | boost::asio::async_read_until( |
Yong Li | 1afda6b | 2020-04-09 19:24:13 +0800 | [diff] [blame] | 138 | inputDev, *buffer, '\n', |
| 139 | [weakRef, buffer](const boost::system::error_code& ec, |
| 140 | std::size_t /*bytes_transfered*/) { |
| 141 | std::shared_ptr<ADCSensor> self = weakRef.lock(); |
| 142 | if (self) |
| 143 | { |
| 144 | self->readBuf = buffer; |
| 145 | self->handleResponse(ec); |
| 146 | } |
| 147 | }); |
Zhu, Yunge | a5b1bbc | 2019-04-09 19:49:36 -0400 | [diff] [blame] | 148 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 149 | } |
| 150 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 151 | void ADCSensor::handleResponse(const boost::system::error_code& err) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 152 | { |
Yong Li | 1afda6b | 2020-04-09 19:24:13 +0800 | [diff] [blame] | 153 | std::weak_ptr<ADCSensor> weakRef = weak_from_this(); |
| 154 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 155 | if (err == boost::system::errc::bad_file_descriptor) |
| 156 | { |
| 157 | return; // we're being destroyed |
| 158 | } |
Yong Li | 1afda6b | 2020-04-09 19:24:13 +0800 | [diff] [blame] | 159 | std::istream responseStream(readBuf.get()); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 160 | |
| 161 | if (!err) |
| 162 | { |
| 163 | std::string response; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 164 | std::getline(responseStream, response); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 165 | |
| 166 | // todo read scaling factors from configuration |
| 167 | try |
| 168 | { |
Josh Lehan | 833661a | 2020-03-04 17:35:41 -0800 | [diff] [blame] | 169 | double nvalue = std::stod(response); |
James Feist | cc6d4fe | 2018-11-14 16:38:26 -0800 | [diff] [blame] | 170 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 171 | nvalue = (nvalue / sensorScaleFactor) / scaleFactor; |
James Feist | cc6d4fe | 2018-11-14 16:38:26 -0800 | [diff] [blame] | 172 | nvalue = std::round(nvalue * roundFactor) / roundFactor; |
| 173 | |
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 | } |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 176 | catch (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 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 192 | int fd = open(path.c_str(), O_RDONLY); |
Jae Hyun Yoo | ef9665a | 2019-10-10 10:26:39 -0700 | [diff] [blame] | 193 | if (fd < 0) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 194 | { |
| 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 | { |
| 203 | return; // we're being canceled |
| 204 | } |
Yong Li | 1afda6b | 2020-04-09 19:24:13 +0800 | [diff] [blame] | 205 | |
| 206 | if (self) |
| 207 | { |
| 208 | self->setupRead(); |
| 209 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 210 | }); |
| 211 | } |
| 212 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 213 | void ADCSensor::checkThresholds(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 214 | { |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 215 | if (!readingStateGood()) |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 216 | { |
| 217 | return; |
| 218 | } |
James Feist | 46342ec | 2019-03-06 14:03:41 -0800 | [diff] [blame] | 219 | |
| 220 | thresholds::checkThresholdsPowerDelay(this, thresholdTimer); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 221 | } |