blob: 3ee453302a7e48edcae8c9e0368c03bd5452020c [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>
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070035static constexpr unsigned int sensorPollMs = 500;
36static constexpr size_t warnAfterErrorCount = 10;
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040037static constexpr unsigned int gpioBridgeEnableMs = 20;
James Feist6714a252018-09-10 15:26:18 -070038// scaling factor from hwmon
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070039static constexpr unsigned int sensorScaleFactor = 1000;
James Feist6714a252018-09-10 15:26:18 -070040
James Feistcc6d4fe2018-11-14 16:38:26 -080041static constexpr double roundFactor = 10000; // 3 decimal places
James Feistce3fca42018-11-21 12:58:24 -080042static constexpr double maxReading = 20;
43static constexpr double minReading = 0;
James Feistcc6d4fe2018-11-14 16:38:26 -080044
James Feistd8705872019-02-08 13:26:09 -080045ADCSensor::ADCSensor(const std::string& path,
46 sdbusplus::asio::object_server& objectServer,
47 std::shared_ptr<sdbusplus::asio::connection>& conn,
48 boost::asio::io_service& io, const std::string& sensorName,
49 std::vector<thresholds::Threshold>&& _thresholds,
James Feist71d31b22019-01-02 16:57:54 -080050 const double scaleFactor, PowerState readState,
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040051 const std::string& sensorConfiguration,
Jae Hyun Yoo1cbd1c62019-07-29 15:02:43 -070052 std::optional<BridgeGpio>&& bridgeGpio) :
James Feist930fcde2019-05-28 12:58:43 -070053 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
James Feistce3fca42018-11-21 12:58:24 -080054 std::move(_thresholds), sensorConfiguration,
55 "xyz.openbmc_project.Configuration.ADC", maxReading, minReading),
James Feist930fcde2019-05-28 12:58:43 -070056 objServer(objectServer), scaleFactor(scaleFactor), path(path),
James Feist71d31b22019-01-02 16:57:54 -080057 readState(std::move(readState)), inputDev(io, open(path.c_str(), O_RDONLY)),
Jae Hyun Yoo1cbd1c62019-07-29 15:02:43 -070058 waitTimer(io), errCount(0), thresholdTimer(io, this),
59 bridgeGpio(std::move(bridgeGpio))
James Feist6714a252018-09-10 15:26:18 -070060{
James Feist251c7822018-09-12 12:54:15 -070061 sensorInterface = objectServer.add_interface(
62 "/xyz/openbmc_project/sensors/voltage/" + name,
63 "xyz.openbmc_project.Sensor.Value");
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070064 if (thresholds::hasWarningInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070065 {
James Feist251c7822018-09-12 12:54:15 -070066 thresholdInterfaceWarning = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070067 "/xyz/openbmc_project/sensors/voltage/" + name,
68 "xyz.openbmc_project.Sensor.Threshold.Warning");
69 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070070 if (thresholds::hasCriticalInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070071 {
James Feist251c7822018-09-12 12:54:15 -070072 thresholdInterfaceCritical = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070073 "/xyz/openbmc_project/sensors/voltage/" + name,
74 "xyz.openbmc_project.Sensor.Threshold.Critical");
75 }
James Feist078f2322019-03-08 11:09:05 -080076 association = objectServer.add_interface(
James Feist2adc95c2019-09-30 14:55:28 -070077 "/xyz/openbmc_project/sensors/voltage/" + name, association::interface);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070078 setInitialProperties(conn);
79 setupRead();
James Feist71d31b22019-01-02 16:57:54 -080080
81 // setup match
82 setupPowerMatch(conn);
James Feist6714a252018-09-10 15:26:18 -070083}
84
85ADCSensor::~ADCSensor()
86{
87 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070088 inputDev.close();
89 waitTimer.cancel();
James Feist251c7822018-09-12 12:54:15 -070090 objServer.remove_interface(thresholdInterfaceWarning);
91 objServer.remove_interface(thresholdInterfaceCritical);
92 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -080093 objServer.remove_interface(association);
James Feist6714a252018-09-10 15:26:18 -070094}
95
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070096void ADCSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -070097{
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040098 if (bridgeGpio.has_value())
99 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700100 (*bridgeGpio).set(1);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400101 // In case a channel has a bridge circuit,we have to turn the bridge on
102 // prior to reading a value at least for one scan cycle to get a valid
103 // value. Guarantee that the HW signal can be stable, the HW signal
104 // could be instability.
105 waitTimer.expires_from_now(
106 boost::posix_time::milliseconds(gpioBridgeEnableMs));
107 waitTimer.async_wait([&](const boost::system::error_code& ec) {
108 if (ec == boost::asio::error::operation_aborted)
109 {
110 return; // we're being canceled
111 }
112 boost::asio::async_read_until(
113 inputDev, readBuf, '\n',
114 [&](const boost::system::error_code& ec,
115 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
116 });
117 }
118 else
119 {
120 boost::asio::async_read_until(
121 inputDev, readBuf, '\n',
122 [&](const boost::system::error_code& ec,
123 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
124 }
James Feist6714a252018-09-10 15:26:18 -0700125}
126
James Feistd8705872019-02-08 13:26:09 -0800127void ADCSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700128{
129 if (err == boost::system::errc::bad_file_descriptor)
130 {
131 return; // we're being destroyed
132 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700133 std::istream responseStream(&readBuf);
James Feist6714a252018-09-10 15:26:18 -0700134
135 if (!err)
136 {
137 std::string response;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700138 std::getline(responseStream, response);
James Feist6714a252018-09-10 15:26:18 -0700139
140 // todo read scaling factors from configuration
141 try
142 {
James Feistcc6d4fe2018-11-14 16:38:26 -0800143 double nvalue = std::stof(response);
144
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700145 nvalue = (nvalue / sensorScaleFactor) / scaleFactor;
James Feistcc6d4fe2018-11-14 16:38:26 -0800146 nvalue = std::round(nvalue * roundFactor) / roundFactor;
147
James Feist6714a252018-09-10 15:26:18 -0700148 if (nvalue != value)
149 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700150 updateValue(nvalue);
James Feist6714a252018-09-10 15:26:18 -0700151 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700152 errCount = 0;
James Feist6714a252018-09-10 15:26:18 -0700153 }
James Feistb6c0b912019-07-09 12:21:44 -0700154 catch (std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700155 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700156 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700157 }
158 }
159 else
160 {
James Feist6714a252018-09-10 15:26:18 -0700161
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700162 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700163 }
James Feistdc6c55f2018-10-31 12:53:20 -0700164 // only print once
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700165 if (errCount == warnAfterErrorCount)
James Feist6714a252018-09-10 15:26:18 -0700166 {
James Feistdc6c55f2018-10-31 12:53:20 -0700167 std::cerr << "Failure to read sensor " << name << " at " << path
168 << " ec:" << err << "\n";
169 }
170
171 if (errCount >= warnAfterErrorCount)
172 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700173 updateValue(0);
James Feist6714a252018-09-10 15:26:18 -0700174 }
175
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700176 responseStream.clear();
177 inputDev.close();
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400178 if (bridgeGpio.has_value())
179 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700180 (*bridgeGpio).set(0);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400181 }
James Feist6714a252018-09-10 15:26:18 -0700182 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700183 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700184 {
185 return; // we're no longer valid
186 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700187 inputDev.assign(fd);
188 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
James Feistd8705872019-02-08 13:26:09 -0800189 waitTimer.async_wait([&](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700190 if (ec == boost::asio::error::operation_aborted)
191 {
192 return; // we're being canceled
193 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700194 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700195 });
196}
197
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700198void ADCSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700199{
James Feist71d31b22019-01-02 16:57:54 -0800200 if (readState == PowerState::on && !isPowerOn())
201 {
202 return;
203 }
James Feist46342ec2019-03-06 14:03:41 -0800204
205 thresholds::checkThresholdsPowerDelay(this, thresholdTimer);
James Feist6714a252018-09-10 15:26:18 -0700206}