blob: 4d3628db32b1feadbcb339d06610b84772fbf24d [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>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040024#include <filesystem>
25#include <fstream>
James Feist6714a252018-09-10 15:26:18 -070026#include <iostream>
27#include <limits>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040028#include <optional>
James Feist6714a252018-09-10 15:26:18 -070029#include <sdbusplus/asio/connection.hpp>
30#include <sdbusplus/asio/object_server.hpp>
31#include <string>
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070032static constexpr unsigned int sensorPollMs = 500;
33static constexpr size_t warnAfterErrorCount = 10;
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040034static constexpr unsigned int gpioBridgeEnableMs = 20;
James Feist6714a252018-09-10 15:26:18 -070035// scaling factor from hwmon
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070036static constexpr unsigned int sensorScaleFactor = 1000;
James Feist6714a252018-09-10 15:26:18 -070037
James Feistcc6d4fe2018-11-14 16:38:26 -080038static constexpr double roundFactor = 10000; // 3 decimal places
James Feistce3fca42018-11-21 12:58:24 -080039static constexpr double maxReading = 20;
40static constexpr double minReading = 0;
James Feistcc6d4fe2018-11-14 16:38:26 -080041
James Feistd8705872019-02-08 13:26:09 -080042ADCSensor::ADCSensor(const std::string& path,
43 sdbusplus::asio::object_server& objectServer,
44 std::shared_ptr<sdbusplus::asio::connection>& conn,
45 boost::asio::io_service& io, const std::string& sensorName,
46 std::vector<thresholds::Threshold>&& _thresholds,
James Feist71d31b22019-01-02 16:57:54 -080047 const double scaleFactor, PowerState readState,
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040048 const std::string& sensorConfiguration,
Jae Hyun Yoo1cbd1c62019-07-29 15:02:43 -070049 std::optional<BridgeGpio>&& bridgeGpio) :
James Feist930fcde2019-05-28 12:58:43 -070050 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
James Feistce3fca42018-11-21 12:58:24 -080051 std::move(_thresholds), sensorConfiguration,
52 "xyz.openbmc_project.Configuration.ADC", maxReading, minReading),
James Feist930fcde2019-05-28 12:58:43 -070053 objServer(objectServer), scaleFactor(scaleFactor), path(path),
James Feist71d31b22019-01-02 16:57:54 -080054 readState(std::move(readState)), inputDev(io, open(path.c_str(), O_RDONLY)),
Jae Hyun Yoo1cbd1c62019-07-29 15:02:43 -070055 waitTimer(io), errCount(0), thresholdTimer(io, this),
56 bridgeGpio(std::move(bridgeGpio))
James Feist6714a252018-09-10 15:26:18 -070057{
James Feist251c7822018-09-12 12:54:15 -070058 sensorInterface = objectServer.add_interface(
59 "/xyz/openbmc_project/sensors/voltage/" + name,
60 "xyz.openbmc_project.Sensor.Value");
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070061 if (thresholds::hasWarningInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070062 {
James Feist251c7822018-09-12 12:54:15 -070063 thresholdInterfaceWarning = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070064 "/xyz/openbmc_project/sensors/voltage/" + name,
65 "xyz.openbmc_project.Sensor.Threshold.Warning");
66 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070067 if (thresholds::hasCriticalInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070068 {
James Feist251c7822018-09-12 12:54:15 -070069 thresholdInterfaceCritical = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070070 "/xyz/openbmc_project/sensors/voltage/" + name,
71 "xyz.openbmc_project.Sensor.Threshold.Critical");
72 }
James Feist078f2322019-03-08 11:09:05 -080073 association = objectServer.add_interface(
James Feist2adc95c2019-09-30 14:55:28 -070074 "/xyz/openbmc_project/sensors/voltage/" + name, association::interface);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070075 setInitialProperties(conn);
76 setupRead();
James Feist71d31b22019-01-02 16:57:54 -080077
78 // setup match
79 setupPowerMatch(conn);
James Feist6714a252018-09-10 15:26:18 -070080}
81
82ADCSensor::~ADCSensor()
83{
84 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070085 inputDev.close();
86 waitTimer.cancel();
James Feist251c7822018-09-12 12:54:15 -070087 objServer.remove_interface(thresholdInterfaceWarning);
88 objServer.remove_interface(thresholdInterfaceCritical);
89 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -080090 objServer.remove_interface(association);
James Feist6714a252018-09-10 15:26:18 -070091}
92
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070093void ADCSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -070094{
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040095 if (bridgeGpio.has_value())
96 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -070097 (*bridgeGpio).set(1);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040098 // In case a channel has a bridge circuit,we have to turn the bridge on
99 // prior to reading a value at least for one scan cycle to get a valid
100 // value. Guarantee that the HW signal can be stable, the HW signal
101 // could be instability.
102 waitTimer.expires_from_now(
103 boost::posix_time::milliseconds(gpioBridgeEnableMs));
104 waitTimer.async_wait([&](const boost::system::error_code& ec) {
105 if (ec == boost::asio::error::operation_aborted)
106 {
107 return; // we're being canceled
108 }
109 boost::asio::async_read_until(
110 inputDev, readBuf, '\n',
111 [&](const boost::system::error_code& ec,
112 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
113 });
114 }
115 else
116 {
117 boost::asio::async_read_until(
118 inputDev, readBuf, '\n',
119 [&](const boost::system::error_code& ec,
120 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
121 }
James Feist6714a252018-09-10 15:26:18 -0700122}
123
James Feistd8705872019-02-08 13:26:09 -0800124void ADCSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700125{
126 if (err == boost::system::errc::bad_file_descriptor)
127 {
128 return; // we're being destroyed
129 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700130 std::istream responseStream(&readBuf);
James Feist6714a252018-09-10 15:26:18 -0700131
132 if (!err)
133 {
134 std::string response;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700135 std::getline(responseStream, response);
James Feist6714a252018-09-10 15:26:18 -0700136
137 // todo read scaling factors from configuration
138 try
139 {
James Feistcc6d4fe2018-11-14 16:38:26 -0800140 double nvalue = std::stof(response);
141
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700142 nvalue = (nvalue / sensorScaleFactor) / scaleFactor;
James Feistcc6d4fe2018-11-14 16:38:26 -0800143 nvalue = std::round(nvalue * roundFactor) / roundFactor;
144
James Feist6714a252018-09-10 15:26:18 -0700145 if (nvalue != value)
146 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700147 updateValue(nvalue);
James Feist6714a252018-09-10 15:26:18 -0700148 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700149 errCount = 0;
James Feist6714a252018-09-10 15:26:18 -0700150 }
James Feistb6c0b912019-07-09 12:21:44 -0700151 catch (std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700152 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700153 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700154 }
155 }
156 else
157 {
James Feist6714a252018-09-10 15:26:18 -0700158
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700159 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700160 }
James Feistdc6c55f2018-10-31 12:53:20 -0700161 // only print once
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700162 if (errCount == warnAfterErrorCount)
James Feist6714a252018-09-10 15:26:18 -0700163 {
James Feistdc6c55f2018-10-31 12:53:20 -0700164 std::cerr << "Failure to read sensor " << name << " at " << path
165 << " ec:" << err << "\n";
166 }
167
168 if (errCount >= warnAfterErrorCount)
169 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700170 updateValue(0);
James Feist6714a252018-09-10 15:26:18 -0700171 }
172
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700173 responseStream.clear();
174 inputDev.close();
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400175 if (bridgeGpio.has_value())
176 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700177 (*bridgeGpio).set(0);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400178 }
James Feist6714a252018-09-10 15:26:18 -0700179 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700180 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700181 {
182 return; // we're no longer valid
183 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700184 inputDev.assign(fd);
185 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
James Feistd8705872019-02-08 13:26:09 -0800186 waitTimer.async_wait([&](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700187 if (ec == boost::asio::error::operation_aborted)
188 {
189 return; // we're being canceled
190 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700191 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700192 });
193}
194
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700195void ADCSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700196{
James Feist71d31b22019-01-02 16:57:54 -0800197 if (readState == PowerState::on && !isPowerOn())
198 {
199 return;
200 }
James Feist46342ec2019-03-06 14:03:41 -0800201
202 thresholds::checkThresholdsPowerDelay(this, thresholdTimer);
James Feist6714a252018-09-10 15:26:18 -0700203}