blob: 330de52d4001b812baa27752c7bea99d27a2981d [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
17#include <unistd.h>
18
19#include <ADCSensor.hpp>
20#include <boost/algorithm/string/predicate.hpp>
21#include <boost/algorithm/string/replace.hpp>
22#include <boost/date_time/posix_time/posix_time.hpp>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040023#include <filesystem>
24#include <fstream>
James Feist6714a252018-09-10 15:26:18 -070025#include <iostream>
26#include <limits>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040027#include <optional>
James Feist6714a252018-09-10 15:26:18 -070028#include <sdbusplus/asio/connection.hpp>
29#include <sdbusplus/asio/object_server.hpp>
30#include <string>
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070031static constexpr unsigned int sensorPollMs = 500;
32static constexpr size_t warnAfterErrorCount = 10;
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040033static constexpr unsigned int gpioBridgeEnableMs = 20;
James Feist6714a252018-09-10 15:26:18 -070034// scaling factor from hwmon
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070035static constexpr unsigned int sensorScaleFactor = 1000;
James Feist6714a252018-09-10 15:26:18 -070036
James Feistcc6d4fe2018-11-14 16:38:26 -080037static constexpr double roundFactor = 10000; // 3 decimal places
James Feistce3fca42018-11-21 12:58:24 -080038static constexpr double maxReading = 20;
39static constexpr double minReading = 0;
James Feistcc6d4fe2018-11-14 16:38:26 -080040
James Feistd8705872019-02-08 13:26:09 -080041ADCSensor::ADCSensor(const std::string& path,
42 sdbusplus::asio::object_server& objectServer,
43 std::shared_ptr<sdbusplus::asio::connection>& conn,
44 boost::asio::io_service& io, const std::string& sensorName,
45 std::vector<thresholds::Threshold>&& _thresholds,
James Feist71d31b22019-01-02 16:57:54 -080046 const double scaleFactor, PowerState readState,
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040047 const std::string& sensorConfiguration,
Jae Hyun Yoo1cbd1c62019-07-29 15:02:43 -070048 std::optional<BridgeGpio>&& bridgeGpio) :
James Feist930fcde2019-05-28 12:58:43 -070049 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
James Feistce3fca42018-11-21 12:58:24 -080050 std::move(_thresholds), sensorConfiguration,
51 "xyz.openbmc_project.Configuration.ADC", maxReading, minReading),
James Feist930fcde2019-05-28 12:58:43 -070052 objServer(objectServer), scaleFactor(scaleFactor), path(path),
James Feist71d31b22019-01-02 16:57:54 -080053 readState(std::move(readState)), inputDev(io, open(path.c_str(), O_RDONLY)),
Jae Hyun Yoo1cbd1c62019-07-29 15:02:43 -070054 waitTimer(io), errCount(0), thresholdTimer(io, this),
55 bridgeGpio(std::move(bridgeGpio))
James Feist6714a252018-09-10 15:26:18 -070056{
James Feist251c7822018-09-12 12:54:15 -070057 sensorInterface = objectServer.add_interface(
58 "/xyz/openbmc_project/sensors/voltage/" + name,
59 "xyz.openbmc_project.Sensor.Value");
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070060 if (thresholds::hasWarningInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070061 {
James Feist251c7822018-09-12 12:54:15 -070062 thresholdInterfaceWarning = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070063 "/xyz/openbmc_project/sensors/voltage/" + name,
64 "xyz.openbmc_project.Sensor.Threshold.Warning");
65 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070066 if (thresholds::hasCriticalInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070067 {
James Feist251c7822018-09-12 12:54:15 -070068 thresholdInterfaceCritical = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070069 "/xyz/openbmc_project/sensors/voltage/" + name,
70 "xyz.openbmc_project.Sensor.Threshold.Critical");
71 }
James Feist078f2322019-03-08 11:09:05 -080072 association = objectServer.add_interface(
James Feist2adc95c2019-09-30 14:55:28 -070073 "/xyz/openbmc_project/sensors/voltage/" + name, association::interface);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070074 setInitialProperties(conn);
75 setupRead();
James Feist71d31b22019-01-02 16:57:54 -080076
77 // setup match
78 setupPowerMatch(conn);
James Feist6714a252018-09-10 15:26:18 -070079}
80
81ADCSensor::~ADCSensor()
82{
83 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070084 inputDev.close();
85 waitTimer.cancel();
James Feist251c7822018-09-12 12:54:15 -070086 objServer.remove_interface(thresholdInterfaceWarning);
87 objServer.remove_interface(thresholdInterfaceCritical);
88 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -080089 objServer.remove_interface(association);
James Feist6714a252018-09-10 15:26:18 -070090}
91
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070092void ADCSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -070093{
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040094 if (bridgeGpio.has_value())
95 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -070096 (*bridgeGpio).set(1);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040097 // In case a channel has a bridge circuit,we have to turn the bridge on
98 // prior to reading a value at least for one scan cycle to get a valid
99 // value. Guarantee that the HW signal can be stable, the HW signal
100 // could be instability.
101 waitTimer.expires_from_now(
102 boost::posix_time::milliseconds(gpioBridgeEnableMs));
103 waitTimer.async_wait([&](const boost::system::error_code& ec) {
104 if (ec == boost::asio::error::operation_aborted)
105 {
106 return; // we're being canceled
107 }
108 boost::asio::async_read_until(
109 inputDev, readBuf, '\n',
110 [&](const boost::system::error_code& ec,
111 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
112 });
113 }
114 else
115 {
116 boost::asio::async_read_until(
117 inputDev, readBuf, '\n',
118 [&](const boost::system::error_code& ec,
119 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
120 }
James Feist6714a252018-09-10 15:26:18 -0700121}
122
James Feistd8705872019-02-08 13:26:09 -0800123void ADCSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700124{
125 if (err == boost::system::errc::bad_file_descriptor)
126 {
127 return; // we're being destroyed
128 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700129 std::istream responseStream(&readBuf);
James Feist6714a252018-09-10 15:26:18 -0700130
131 if (!err)
132 {
133 std::string response;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700134 std::getline(responseStream, response);
James Feist6714a252018-09-10 15:26:18 -0700135
136 // todo read scaling factors from configuration
137 try
138 {
James Feistcc6d4fe2018-11-14 16:38:26 -0800139 double nvalue = std::stof(response);
140
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700141 nvalue = (nvalue / sensorScaleFactor) / scaleFactor;
James Feistcc6d4fe2018-11-14 16:38:26 -0800142 nvalue = std::round(nvalue * roundFactor) / roundFactor;
143
James Feist6714a252018-09-10 15:26:18 -0700144 if (nvalue != value)
145 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700146 updateValue(nvalue);
James Feist6714a252018-09-10 15:26:18 -0700147 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700148 errCount = 0;
James Feist6714a252018-09-10 15:26:18 -0700149 }
James Feistb6c0b912019-07-09 12:21:44 -0700150 catch (std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700151 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700152 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700153 }
154 }
155 else
156 {
James Feist6714a252018-09-10 15:26:18 -0700157
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700158 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700159 }
James Feistdc6c55f2018-10-31 12:53:20 -0700160 // only print once
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700161 if (errCount == warnAfterErrorCount)
James Feist6714a252018-09-10 15:26:18 -0700162 {
James Feistdc6c55f2018-10-31 12:53:20 -0700163 std::cerr << "Failure to read sensor " << name << " at " << path
164 << " ec:" << err << "\n";
165 }
166
167 if (errCount >= warnAfterErrorCount)
168 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700169 updateValue(0);
James Feist6714a252018-09-10 15:26:18 -0700170 }
171
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700172 responseStream.clear();
173 inputDev.close();
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400174 if (bridgeGpio.has_value())
175 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700176 (*bridgeGpio).set(0);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400177 }
James Feist6714a252018-09-10 15:26:18 -0700178 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700179 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700180 {
181 return; // we're no longer valid
182 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700183 inputDev.assign(fd);
184 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
James Feistd8705872019-02-08 13:26:09 -0800185 waitTimer.async_wait([&](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700186 if (ec == boost::asio::error::operation_aborted)
187 {
188 return; // we're being canceled
189 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700190 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700191 });
192}
193
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700194void ADCSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700195{
James Feist71d31b22019-01-02 16:57:54 -0800196 if (readState == PowerState::on && !isPowerOn())
197 {
198 return;
199 }
James Feist46342ec2019-03-06 14:03:41 -0800200
201 thresholds::checkThresholdsPowerDelay(this, thresholdTimer);
James Feist6714a252018-09-10 15:26:18 -0700202}