blob: d22afd8038f4592be6554cd131997a9e6f2c546c [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>
Patrick Venture96e97db2019-10-31 13:44:38 -070024#include <cmath>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040025#include <filesystem>
26#include <fstream>
James Feist6714a252018-09-10 15:26:18 -070027#include <iostream>
28#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070029#include <memory>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040030#include <optional>
James Feist6714a252018-09-10 15:26:18 -070031#include <sdbusplus/asio/connection.hpp>
32#include <sdbusplus/asio/object_server.hpp>
33#include <string>
Patrick Venture96e97db2019-10-31 13:44:38 -070034#include <vector>
Patrick Venture2da370e2019-11-01 11:51:31 -070035
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070036static constexpr unsigned int sensorPollMs = 500;
37static constexpr size_t warnAfterErrorCount = 10;
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040038static constexpr unsigned int gpioBridgeEnableMs = 20;
James Feist6714a252018-09-10 15:26:18 -070039// scaling factor from hwmon
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070040static constexpr unsigned int sensorScaleFactor = 1000;
James Feist6714a252018-09-10 15:26:18 -070041
James Feistcc6d4fe2018-11-14 16:38:26 -080042static constexpr double roundFactor = 10000; // 3 decimal places
James Feistce3fca42018-11-21 12:58:24 -080043static constexpr double maxReading = 20;
44static constexpr double minReading = 0;
James Feistcc6d4fe2018-11-14 16:38:26 -080045
James Feistd8705872019-02-08 13:26:09 -080046ADCSensor::ADCSensor(const std::string& path,
47 sdbusplus::asio::object_server& objectServer,
48 std::shared_ptr<sdbusplus::asio::connection>& conn,
49 boost::asio::io_service& io, const std::string& sensorName,
50 std::vector<thresholds::Threshold>&& _thresholds,
James Feist71d31b22019-01-02 16:57:54 -080051 const double scaleFactor, PowerState readState,
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040052 const std::string& sensorConfiguration,
Jae Hyun Yoo1cbd1c62019-07-29 15:02:43 -070053 std::optional<BridgeGpio>&& bridgeGpio) :
James Feist930fcde2019-05-28 12:58:43 -070054 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
James Feistce3fca42018-11-21 12:58:24 -080055 std::move(_thresholds), sensorConfiguration,
56 "xyz.openbmc_project.Configuration.ADC", maxReading, minReading),
Yong Li1afda6b2020-04-09 19:24:13 +080057 std::enable_shared_from_this<ADCSensor>(), objServer(objectServer),
58 inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), path(path),
59 errCount(0), scaleFactor(scaleFactor), bridgeGpio(std::move(bridgeGpio)),
60 readState(std::move(readState)), thresholdTimer(io, this)
James Feist6714a252018-09-10 15:26:18 -070061{
James Feist251c7822018-09-12 12:54:15 -070062 sensorInterface = objectServer.add_interface(
63 "/xyz/openbmc_project/sensors/voltage/" + name,
64 "xyz.openbmc_project.Sensor.Value");
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070065 if (thresholds::hasWarningInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070066 {
James Feist251c7822018-09-12 12:54:15 -070067 thresholdInterfaceWarning = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070068 "/xyz/openbmc_project/sensors/voltage/" + name,
69 "xyz.openbmc_project.Sensor.Threshold.Warning");
70 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070071 if (thresholds::hasCriticalInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070072 {
James Feist251c7822018-09-12 12:54:15 -070073 thresholdInterfaceCritical = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070074 "/xyz/openbmc_project/sensors/voltage/" + name,
75 "xyz.openbmc_project.Sensor.Threshold.Critical");
76 }
James Feist078f2322019-03-08 11:09:05 -080077 association = objectServer.add_interface(
James Feist2adc95c2019-09-30 14:55:28 -070078 "/xyz/openbmc_project/sensors/voltage/" + name, association::interface);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070079 setInitialProperties(conn);
James Feist71d31b22019-01-02 16:57:54 -080080
81 // setup match
82 setupPowerMatch(conn);
James Feist6714a252018-09-10 15:26:18 -070083}
84
85ADCSensor::~ADCSensor()
86{
87 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070088 inputDev.close();
89 waitTimer.cancel();
James Feist251c7822018-09-12 12:54:15 -070090 objServer.remove_interface(thresholdInterfaceWarning);
91 objServer.remove_interface(thresholdInterfaceCritical);
92 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -080093 objServer.remove_interface(association);
James Feist6714a252018-09-10 15:26:18 -070094}
95
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070096void ADCSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -070097{
Yong Li1afda6b2020-04-09 19:24:13 +080098 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, Yungea5b1bbc2019-04-09 19:49:36 -0400103 if (bridgeGpio.has_value())
104 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700105 (*bridgeGpio).set(1);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400106 // 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 Li1afda6b2020-04-09 19:24:13 +0800112 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, Yungea5b1bbc2019-04-09 19:49:36 -0400135 }
136 else
137 {
138 boost::asio::async_read_until(
Yong Li1afda6b2020-04-09 19:24:13 +0800139 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, Yungea5b1bbc2019-04-09 19:49:36 -0400149 }
James Feist6714a252018-09-10 15:26:18 -0700150}
151
James Feistd8705872019-02-08 13:26:09 -0800152void ADCSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700153{
Yong Li1afda6b2020-04-09 19:24:13 +0800154 std::weak_ptr<ADCSensor> weakRef = weak_from_this();
155
James Feist6714a252018-09-10 15:26:18 -0700156 if (err == boost::system::errc::bad_file_descriptor)
157 {
158 return; // we're being destroyed
159 }
Yong Li1afda6b2020-04-09 19:24:13 +0800160 std::istream responseStream(readBuf.get());
James Feist6714a252018-09-10 15:26:18 -0700161
162 if (!err)
163 {
164 std::string response;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700165 std::getline(responseStream, response);
James Feist6714a252018-09-10 15:26:18 -0700166
167 // todo read scaling factors from configuration
168 try
169 {
Josh Lehan833661a2020-03-04 17:35:41 -0800170 double nvalue = std::stod(response);
James Feistcc6d4fe2018-11-14 16:38:26 -0800171
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700172 nvalue = (nvalue / sensorScaleFactor) / scaleFactor;
James Feistcc6d4fe2018-11-14 16:38:26 -0800173 nvalue = std::round(nvalue * roundFactor) / roundFactor;
174
Josh Lehan833661a2020-03-04 17:35:41 -0800175 updateValue(nvalue);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700176 errCount = 0;
James Feist6714a252018-09-10 15:26:18 -0700177 }
James Feistb6c0b912019-07-09 12:21:44 -0700178 catch (std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700179 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700180 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700181 }
182 }
James Feist3459d012020-01-30 13:17:43 -0800183 else if (readState == PowerState::on && !isPowerOn())
184 {
185 errCount = 0;
186 updateValue(std::numeric_limits<double>::quiet_NaN());
187 }
James Feist6714a252018-09-10 15:26:18 -0700188 else
189 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700190 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700191 }
James Feistdc6c55f2018-10-31 12:53:20 -0700192 // only print once
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700193 if (errCount == warnAfterErrorCount)
James Feist6714a252018-09-10 15:26:18 -0700194 {
James Feistdc6c55f2018-10-31 12:53:20 -0700195 std::cerr << "Failure to read sensor " << name << " at " << path
196 << " ec:" << err << "\n";
197 }
198
199 if (errCount >= warnAfterErrorCount)
200 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700201 updateValue(0);
James Feist6714a252018-09-10 15:26:18 -0700202 }
203
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700204 responseStream.clear();
205 inputDev.close();
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400206 if (bridgeGpio.has_value())
207 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700208 (*bridgeGpio).set(0);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400209 }
James Feist6714a252018-09-10 15:26:18 -0700210 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700211 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700212 {
213 return; // we're no longer valid
214 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700215 inputDev.assign(fd);
216 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
Yong Li1afda6b2020-04-09 19:24:13 +0800217 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
218 std::shared_ptr<ADCSensor> self = weakRef.lock();
James Feist6714a252018-09-10 15:26:18 -0700219 if (ec == boost::asio::error::operation_aborted)
220 {
221 return; // we're being canceled
222 }
Yong Li1afda6b2020-04-09 19:24:13 +0800223
224 if (self)
225 {
226 self->setupRead();
227 }
James Feist6714a252018-09-10 15:26:18 -0700228 });
229}
230
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700231void ADCSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700232{
James Feist71d31b22019-01-02 16:57:54 -0800233 if (readState == PowerState::on && !isPowerOn())
234 {
235 return;
236 }
James Feist46342ec2019-03-06 14:03:41 -0800237
238 thresholds::checkThresholdsPowerDelay(this, thresholdTimer);
James Feist6714a252018-09-10 15:26:18 -0700239}