blob: bfe17ac7e716c6c5ae67aa58bd766764f7e8a22a [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001/*
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 Ventureca44b2f2019-10-31 11:02:26 -070017#include "ADCSensor.hpp"
18
James Feist6714a252018-09-10 15:26:18 -070019#include <unistd.h>
20
James Feist6714a252018-09-10 15:26:18 -070021#include <boost/algorithm/string/predicate.hpp>
22#include <boost/algorithm/string/replace.hpp>
23#include <boost/date_time/posix_time/posix_time.hpp>
James Feist38fb5982020-05-28 10:09:54 -070024#include <sdbusplus/asio/connection.hpp>
25#include <sdbusplus/asio/object_server.hpp>
26
Patrick Venture96e97db2019-10-31 13:44:38 -070027#include <cmath>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040028#include <filesystem>
29#include <fstream>
James Feist6714a252018-09-10 15:26:18 -070030#include <iostream>
31#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070032#include <memory>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040033#include <optional>
James Feist6714a252018-09-10 15:26:18 -070034#include <string>
Patrick Venture96e97db2019-10-31 13:44:38 -070035#include <vector>
Patrick Venture2da370e2019-11-01 11:51:31 -070036
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070037static constexpr unsigned int sensorPollMs = 500;
38static constexpr size_t warnAfterErrorCount = 10;
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040039static constexpr unsigned int gpioBridgeEnableMs = 20;
James Feist6714a252018-09-10 15:26:18 -070040// scaling factor from hwmon
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070041static constexpr unsigned int sensorScaleFactor = 1000;
James Feist6714a252018-09-10 15:26:18 -070042
James Feistcc6d4fe2018-11-14 16:38:26 -080043static constexpr double roundFactor = 10000; // 3 decimal places
James Feistce3fca42018-11-21 12:58:24 -080044static constexpr double maxReading = 20;
45static constexpr double minReading = 0;
James Feistcc6d4fe2018-11-14 16:38:26 -080046
James Feistd8705872019-02-08 13:26:09 -080047ADCSensor::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 Feist71d31b22019-01-02 16:57:54 -080052 const double scaleFactor, PowerState readState,
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040053 const std::string& sensorConfiguration,
Jae Hyun Yoo1cbd1c62019-07-29 15:02:43 -070054 std::optional<BridgeGpio>&& bridgeGpio) :
James Feist930fcde2019-05-28 12:58:43 -070055 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
James Feistce3fca42018-11-21 12:58:24 -080056 std::move(_thresholds), sensorConfiguration,
57 "xyz.openbmc_project.Configuration.ADC", maxReading, minReading),
Yong Li1afda6b2020-04-09 19:24:13 +080058 std::enable_shared_from_this<ADCSensor>(), objServer(objectServer),
59 inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), path(path),
60 errCount(0), scaleFactor(scaleFactor), bridgeGpio(std::move(bridgeGpio)),
61 readState(std::move(readState)), thresholdTimer(io, this)
James Feist6714a252018-09-10 15:26:18 -070062{
James Feist251c7822018-09-12 12:54:15 -070063 sensorInterface = objectServer.add_interface(
64 "/xyz/openbmc_project/sensors/voltage/" + name,
65 "xyz.openbmc_project.Sensor.Value");
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070066 if (thresholds::hasWarningInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070067 {
James Feist251c7822018-09-12 12:54:15 -070068 thresholdInterfaceWarning = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070069 "/xyz/openbmc_project/sensors/voltage/" + name,
70 "xyz.openbmc_project.Sensor.Threshold.Warning");
71 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070072 if (thresholds::hasCriticalInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070073 {
James Feist251c7822018-09-12 12:54:15 -070074 thresholdInterfaceCritical = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070075 "/xyz/openbmc_project/sensors/voltage/" + name,
76 "xyz.openbmc_project.Sensor.Threshold.Critical");
77 }
James Feist078f2322019-03-08 11:09:05 -080078 association = objectServer.add_interface(
James Feist2adc95c2019-09-30 14:55:28 -070079 "/xyz/openbmc_project/sensors/voltage/" + name, association::interface);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070080 setInitialProperties(conn);
James Feist71d31b22019-01-02 16:57:54 -080081
82 // setup match
83 setupPowerMatch(conn);
James Feist6714a252018-09-10 15:26:18 -070084}
85
86ADCSensor::~ADCSensor()
87{
88 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070089 inputDev.close();
90 waitTimer.cancel();
James Feist251c7822018-09-12 12:54:15 -070091 objServer.remove_interface(thresholdInterfaceWarning);
92 objServer.remove_interface(thresholdInterfaceCritical);
93 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -080094 objServer.remove_interface(association);
James Feist6714a252018-09-10 15:26:18 -070095}
96
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070097void ADCSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -070098{
Yong Li1afda6b2020-04-09 19:24:13 +080099 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, Yungea5b1bbc2019-04-09 19:49:36 -0400104 if (bridgeGpio.has_value())
105 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700106 (*bridgeGpio).set(1);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400107 // 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.
111 waitTimer.expires_from_now(
112 boost::posix_time::milliseconds(gpioBridgeEnableMs));
Yong Li1afda6b2020-04-09 19:24:13 +0800113 waitTimer.async_wait(
114 [weakRef, buffer](const boost::system::error_code& ec) {
115 std::shared_ptr<ADCSensor> self = weakRef.lock();
116 if (ec == boost::asio::error::operation_aborted)
117 {
118 return; // we're being canceled
119 }
120
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 }
133 });
134 }
135 });
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400136 }
137 else
138 {
139 boost::asio::async_read_until(
Yong Li1afda6b2020-04-09 19:24:13 +0800140 inputDev, *buffer, '\n',
141 [weakRef, buffer](const boost::system::error_code& ec,
142 std::size_t /*bytes_transfered*/) {
143 std::shared_ptr<ADCSensor> self = weakRef.lock();
144 if (self)
145 {
146 self->readBuf = buffer;
147 self->handleResponse(ec);
148 }
149 });
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400150 }
James Feist6714a252018-09-10 15:26:18 -0700151}
152
James Feistd8705872019-02-08 13:26:09 -0800153void ADCSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700154{
Yong Li1afda6b2020-04-09 19:24:13 +0800155 std::weak_ptr<ADCSensor> weakRef = weak_from_this();
156
James Feist6714a252018-09-10 15:26:18 -0700157 if (err == boost::system::errc::bad_file_descriptor)
158 {
159 return; // we're being destroyed
160 }
Yong Li1afda6b2020-04-09 19:24:13 +0800161 std::istream responseStream(readBuf.get());
James Feist6714a252018-09-10 15:26:18 -0700162
163 if (!err)
164 {
165 std::string response;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700166 std::getline(responseStream, response);
James Feist6714a252018-09-10 15:26:18 -0700167
168 // todo read scaling factors from configuration
169 try
170 {
Josh Lehan833661a2020-03-04 17:35:41 -0800171 double nvalue = std::stod(response);
James Feistcc6d4fe2018-11-14 16:38:26 -0800172
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700173 nvalue = (nvalue / sensorScaleFactor) / scaleFactor;
James Feistcc6d4fe2018-11-14 16:38:26 -0800174 nvalue = std::round(nvalue * roundFactor) / roundFactor;
175
Josh Lehan833661a2020-03-04 17:35:41 -0800176 updateValue(nvalue);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700177 errCount = 0;
James Feist6714a252018-09-10 15:26:18 -0700178 }
James Feistb6c0b912019-07-09 12:21:44 -0700179 catch (std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700180 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700181 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700182 }
183 }
James Feist3459d012020-01-30 13:17:43 -0800184 else if (readState == PowerState::on && !isPowerOn())
185 {
186 errCount = 0;
187 updateValue(std::numeric_limits<double>::quiet_NaN());
188 }
James Feist6714a252018-09-10 15:26:18 -0700189 else
190 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700191 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700192 }
James Feistdc6c55f2018-10-31 12:53:20 -0700193 // only print once
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700194 if (errCount == warnAfterErrorCount)
James Feist6714a252018-09-10 15:26:18 -0700195 {
James Feistdc6c55f2018-10-31 12:53:20 -0700196 std::cerr << "Failure to read sensor " << name << " at " << path
197 << " ec:" << err << "\n";
198 }
199
200 if (errCount >= warnAfterErrorCount)
201 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700202 updateValue(0);
James Feist6714a252018-09-10 15:26:18 -0700203 }
204
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700205 responseStream.clear();
206 inputDev.close();
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400207 if (bridgeGpio.has_value())
208 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700209 (*bridgeGpio).set(0);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400210 }
James Feist6714a252018-09-10 15:26:18 -0700211 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700212 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700213 {
214 return; // we're no longer valid
215 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700216 inputDev.assign(fd);
217 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
Yong Li1afda6b2020-04-09 19:24:13 +0800218 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
219 std::shared_ptr<ADCSensor> self = weakRef.lock();
James Feist6714a252018-09-10 15:26:18 -0700220 if (ec == boost::asio::error::operation_aborted)
221 {
222 return; // we're being canceled
223 }
Yong Li1afda6b2020-04-09 19:24:13 +0800224
225 if (self)
226 {
227 self->setupRead();
228 }
James Feist6714a252018-09-10 15:26:18 -0700229 });
230}
231
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700232void ADCSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700233{
James Feist71d31b22019-01-02 16:57:54 -0800234 if (readState == PowerState::on && !isPowerOn())
235 {
236 return;
237 }
James Feist46342ec2019-03-06 14:03:41 -0800238
239 thresholds::checkThresholdsPowerDelay(this, thresholdTimer);
James Feist6714a252018-09-10 15:26:18 -0700240}