blob: 20e8041d12a6836bcdb2529eaf4a83b2870f0d66 [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,
James Feist961bf092020-07-01 16:38:12 -070057 "xyz.openbmc_project.Configuration.ADC", maxReading, minReading,
58 readState),
Yong Li1afda6b2020-04-09 19:24:13 +080059 std::enable_shared_from_this<ADCSensor>(), objServer(objectServer),
60 inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), path(path),
James Feist961bf092020-07-01 16:38:12 -070061 scaleFactor(scaleFactor), bridgeGpio(std::move(bridgeGpio)),
62 thresholdTimer(io, this)
James Feist6714a252018-09-10 15:26:18 -070063{
James Feist251c7822018-09-12 12:54:15 -070064 sensorInterface = objectServer.add_interface(
65 "/xyz/openbmc_project/sensors/voltage/" + name,
66 "xyz.openbmc_project.Sensor.Value");
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070067 if (thresholds::hasWarningInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070068 {
James Feist251c7822018-09-12 12:54:15 -070069 thresholdInterfaceWarning = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070070 "/xyz/openbmc_project/sensors/voltage/" + name,
71 "xyz.openbmc_project.Sensor.Threshold.Warning");
72 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070073 if (thresholds::hasCriticalInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070074 {
James Feist251c7822018-09-12 12:54:15 -070075 thresholdInterfaceCritical = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070076 "/xyz/openbmc_project/sensors/voltage/" + name,
77 "xyz.openbmc_project.Sensor.Threshold.Critical");
78 }
James Feist078f2322019-03-08 11:09:05 -080079 association = objectServer.add_interface(
James Feist2adc95c2019-09-30 14:55:28 -070080 "/xyz/openbmc_project/sensors/voltage/" + name, association::interface);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070081 setInitialProperties(conn);
James Feist6714a252018-09-10 15:26:18 -070082}
83
84ADCSensor::~ADCSensor()
85{
86 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070087 inputDev.close();
88 waitTimer.cancel();
James Feist251c7822018-09-12 12:54:15 -070089 objServer.remove_interface(thresholdInterfaceWarning);
90 objServer.remove_interface(thresholdInterfaceCritical);
91 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -080092 objServer.remove_interface(association);
James Feist6714a252018-09-10 15:26:18 -070093}
94
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070095void ADCSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -070096{
Yong Li1afda6b2020-04-09 19:24:13 +080097 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, Yungea5b1bbc2019-04-09 19:49:36 -0400102 if (bridgeGpio.has_value())
103 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700104 (*bridgeGpio).set(1);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400105 // 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 Li1afda6b2020-04-09 19:24:13 +0800111 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, Yungea5b1bbc2019-04-09 19:49:36 -0400134 }
135 else
136 {
137 boost::asio::async_read_until(
Yong Li1afda6b2020-04-09 19:24:13 +0800138 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, Yungea5b1bbc2019-04-09 19:49:36 -0400148 }
James Feist6714a252018-09-10 15:26:18 -0700149}
150
James Feistd8705872019-02-08 13:26:09 -0800151void ADCSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700152{
Yong Li1afda6b2020-04-09 19:24:13 +0800153 std::weak_ptr<ADCSensor> weakRef = weak_from_this();
154
James Feist6714a252018-09-10 15:26:18 -0700155 if (err == boost::system::errc::bad_file_descriptor)
156 {
157 return; // we're being destroyed
158 }
Yong Li1afda6b2020-04-09 19:24:13 +0800159 std::istream responseStream(readBuf.get());
James Feist6714a252018-09-10 15:26:18 -0700160
161 if (!err)
162 {
163 std::string response;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700164 std::getline(responseStream, response);
James Feist6714a252018-09-10 15:26:18 -0700165
166 // todo read scaling factors from configuration
167 try
168 {
Josh Lehan833661a2020-03-04 17:35:41 -0800169 double nvalue = std::stod(response);
James Feistcc6d4fe2018-11-14 16:38:26 -0800170
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700171 nvalue = (nvalue / sensorScaleFactor) / scaleFactor;
James Feistcc6d4fe2018-11-14 16:38:26 -0800172 nvalue = std::round(nvalue * roundFactor) / roundFactor;
173
Josh Lehan833661a2020-03-04 17:35:41 -0800174 updateValue(nvalue);
James Feist6714a252018-09-10 15:26:18 -0700175 }
James Feistb6c0b912019-07-09 12:21:44 -0700176 catch (std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700177 {
James Feist961bf092020-07-01 16:38:12 -0700178 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700179 }
180 }
181 else
182 {
James Feist961bf092020-07-01 16:38:12 -0700183 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700184 }
185
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700186 responseStream.clear();
187 inputDev.close();
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400188 if (bridgeGpio.has_value())
189 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700190 (*bridgeGpio).set(0);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400191 }
James Feist6714a252018-09-10 15:26:18 -0700192 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700193 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700194 {
195 return; // we're no longer valid
196 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700197 inputDev.assign(fd);
198 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
Yong Li1afda6b2020-04-09 19:24:13 +0800199 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
200 std::shared_ptr<ADCSensor> self = weakRef.lock();
James Feist6714a252018-09-10 15:26:18 -0700201 if (ec == boost::asio::error::operation_aborted)
202 {
203 return; // we're being canceled
204 }
Yong Li1afda6b2020-04-09 19:24:13 +0800205
206 if (self)
207 {
208 self->setupRead();
209 }
James Feist6714a252018-09-10 15:26:18 -0700210 });
211}
212
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700213void ADCSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700214{
James Feist961bf092020-07-01 16:38:12 -0700215 if (!readingStateGood())
James Feist71d31b22019-01-02 16:57:54 -0800216 {
217 return;
218 }
James Feist46342ec2019-03-06 14:03:41 -0800219
220 thresholds::checkThresholdsPowerDelay(this, thresholdTimer);
James Feist6714a252018-09-10 15:26:18 -0700221}