blob: ec3efdba374fbe5292f2f1bc4458d8e0d49777ba [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(
73 "/xyz/openbmc_project/sensors/voltage/" + name,
74 "org.openbmc.Associations");
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);
180 if (fd <= 0)
181 {
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}