blob: 2ab1a4f3aa4ccf8726acf0f1db9866897268942e [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),
Brad Bishopfbb44ad2019-11-08 09:42:37 -050057 objServer(objectServer), inputDev(io, open(path.c_str(), O_RDONLY)),
58 waitTimer(io), path(path), errCount(0), scaleFactor(scaleFactor),
59 bridgeGpio(std::move(bridgeGpio)), readState(std::move(readState)),
60 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);
80 setupRead();
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{
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040099 if (bridgeGpio.has_value())
100 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700101 (*bridgeGpio).set(1);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400102 // In case a channel has a bridge circuit,we have to turn the bridge on
103 // prior to reading a value at least for one scan cycle to get a valid
104 // value. Guarantee that the HW signal can be stable, the HW signal
105 // could be instability.
106 waitTimer.expires_from_now(
107 boost::posix_time::milliseconds(gpioBridgeEnableMs));
108 waitTimer.async_wait([&](const boost::system::error_code& ec) {
109 if (ec == boost::asio::error::operation_aborted)
110 {
111 return; // we're being canceled
112 }
113 boost::asio::async_read_until(
114 inputDev, readBuf, '\n',
115 [&](const boost::system::error_code& ec,
116 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
117 });
118 }
119 else
120 {
121 boost::asio::async_read_until(
122 inputDev, readBuf, '\n',
123 [&](const boost::system::error_code& ec,
124 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
125 }
James Feist6714a252018-09-10 15:26:18 -0700126}
127
James Feistd8705872019-02-08 13:26:09 -0800128void ADCSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700129{
130 if (err == boost::system::errc::bad_file_descriptor)
131 {
132 return; // we're being destroyed
133 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700134 std::istream responseStream(&readBuf);
James Feist6714a252018-09-10 15:26:18 -0700135
136 if (!err)
137 {
138 std::string response;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700139 std::getline(responseStream, response);
James Feist6714a252018-09-10 15:26:18 -0700140
141 // todo read scaling factors from configuration
142 try
143 {
Josh Lehan833661a2020-03-04 17:35:41 -0800144 double nvalue = std::stod(response);
James Feistcc6d4fe2018-11-14 16:38:26 -0800145
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700146 nvalue = (nvalue / sensorScaleFactor) / scaleFactor;
James Feistcc6d4fe2018-11-14 16:38:26 -0800147 nvalue = std::round(nvalue * roundFactor) / roundFactor;
148
Josh Lehan833661a2020-03-04 17:35:41 -0800149 updateValue(nvalue);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700150 errCount = 0;
James Feist6714a252018-09-10 15:26:18 -0700151 }
James Feistb6c0b912019-07-09 12:21:44 -0700152 catch (std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700153 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700154 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700155 }
156 }
James Feist3459d012020-01-30 13:17:43 -0800157 else if (readState == PowerState::on && !isPowerOn())
158 {
159 errCount = 0;
160 updateValue(std::numeric_limits<double>::quiet_NaN());
161 }
James Feist6714a252018-09-10 15:26:18 -0700162 else
163 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700164 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700165 }
James Feistdc6c55f2018-10-31 12:53:20 -0700166 // only print once
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700167 if (errCount == warnAfterErrorCount)
James Feist6714a252018-09-10 15:26:18 -0700168 {
James Feistdc6c55f2018-10-31 12:53:20 -0700169 std::cerr << "Failure to read sensor " << name << " at " << path
170 << " ec:" << err << "\n";
171 }
172
173 if (errCount >= warnAfterErrorCount)
174 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700175 updateValue(0);
James Feist6714a252018-09-10 15:26:18 -0700176 }
177
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700178 responseStream.clear();
179 inputDev.close();
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400180 if (bridgeGpio.has_value())
181 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700182 (*bridgeGpio).set(0);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400183 }
James Feist6714a252018-09-10 15:26:18 -0700184 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700185 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700186 {
187 return; // we're no longer valid
188 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700189 inputDev.assign(fd);
190 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
James Feistd8705872019-02-08 13:26:09 -0800191 waitTimer.async_wait([&](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700192 if (ec == boost::asio::error::operation_aborted)
193 {
194 return; // we're being canceled
195 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700196 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700197 });
198}
199
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700200void ADCSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700201{
James Feist71d31b22019-01-02 16:57:54 -0800202 if (readState == PowerState::on && !isPowerOn())
203 {
204 return;
205 }
James Feist46342ec2019-03-06 14:03:41 -0800206
207 thresholds::checkThresholdsPowerDelay(this, thresholdTimer);
James Feist6714a252018-09-10 15:26:18 -0700208}