blob: 9758168c20fb834b9e12f2e6cbfc36423944c008 [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
Ed Tanous8a57ec02020-10-09 12:46:52 -070019#include <ADCSensor.hpp>
James Feist6714a252018-09-10 15:26:18 -070020#include <boost/algorithm/string/predicate.hpp>
James Feist8086aba2020-08-25 16:00:59 -070021#include <boost/asio/read_until.hpp>
James Feist6714a252018-09-10 15:26:18 -070022#include <boost/date_time/posix_time/posix_time.hpp>
James Feist38fb5982020-05-28 10:09:54 -070023#include <sdbusplus/asio/connection.hpp>
24#include <sdbusplus/asio/object_server.hpp>
25
Patrick Venture96e97db2019-10-31 13:44:38 -070026#include <cmath>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040027#include <filesystem>
28#include <fstream>
James Feist6714a252018-09-10 15:26:18 -070029#include <iostream>
30#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070031#include <memory>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040032#include <optional>
James Feist6714a252018-09-10 15:26:18 -070033#include <string>
Patrick Venture96e97db2019-10-31 13:44:38 -070034#include <vector>
Patrick Venture2da370e2019-11-01 11:51:31 -070035
James Feist6714a252018-09-10 15:26:18 -070036// scaling factor from hwmon
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070037static constexpr unsigned int sensorScaleFactor = 1000;
James Feist6714a252018-09-10 15:26:18 -070038
Zhikui Ren937eb542020-10-12 13:42:08 -070039static constexpr double roundFactor = 10000; // 3 decimal places
40static constexpr double maxVoltageReading = 1.8; // pre sensor scaling
41static constexpr double minVoltageReading = 0;
James Feistcc6d4fe2018-11-14 16:38:26 -080042
James Feistd8705872019-02-08 13:26:09 -080043ADCSensor::ADCSensor(const std::string& path,
44 sdbusplus::asio::object_server& objectServer,
45 std::shared_ptr<sdbusplus::asio::connection>& conn,
46 boost::asio::io_service& io, const std::string& sensorName,
Jeff Lin7b7a9de2021-02-22 11:16:27 +080047 std::vector<thresholds::Threshold>&& thresholdsIn,
Jeff Lind9cd7042020-11-20 15:49:28 +080048 const double scaleFactor, const float pollRate,
49 PowerState readState,
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040050 const std::string& sensorConfiguration,
Jae Hyun Yoo1cbd1c62019-07-29 15:02:43 -070051 std::optional<BridgeGpio>&& bridgeGpio) :
Zhikui Renda98f092021-11-01 09:41:08 -070052 Sensor(escapeName(sensorName), std::move(thresholdsIn), sensorConfiguration,
Jie Yang3291b9c2021-07-29 14:46:51 -070053 "xyz.openbmc_project.Configuration.ADC", false, false,
Zhikui Ren937eb542020-10-12 13:42:08 -070054 maxVoltageReading / scaleFactor, minVoltageReading / scaleFactor,
James Feiste3338522020-09-15 15:40:30 -070055 conn, readState),
Yong Li1afda6b2020-04-09 19:24:13 +080056 std::enable_shared_from_this<ADCSensor>(), objServer(objectServer),
57 inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), path(path),
Jeff Lind9cd7042020-11-20 15:49:28 +080058 scaleFactor(scaleFactor),
59 sensorPollMs(static_cast<unsigned int>(pollRate * 1000)),
Zhikui Ren12c2c0e2021-04-28 17:21:21 -070060 bridgeGpio(std::move(bridgeGpio)), thresholdTimer(io)
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);
Zev Weiss6b6891c2021-04-22 02:46:21 -050079 setInitialProperties(conn, sensor_paths::unitVolts);
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();
Zhikui Ren12c2c0e2021-04-28 17:21:21 -070087
James Feist251c7822018-09-12 12:54:15 -070088 objServer.remove_interface(thresholdInterfaceWarning);
89 objServer.remove_interface(thresholdInterfaceCritical);
90 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -080091 objServer.remove_interface(association);
James Feist6714a252018-09-10 15:26:18 -070092}
93
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070094void ADCSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -070095{
Yong Li1afda6b2020-04-09 19:24:13 +080096 std::shared_ptr<boost::asio::streambuf> buffer =
97 std::make_shared<boost::asio::streambuf>();
98
99 std::weak_ptr<ADCSensor> weakRef = weak_from_this();
100
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400101 if (bridgeGpio.has_value())
102 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700103 (*bridgeGpio).set(1);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400104 // In case a channel has a bridge circuit,we have to turn the bridge on
105 // prior to reading a value at least for one scan cycle to get a valid
106 // value. Guarantee that the HW signal can be stable, the HW signal
107 // could be instability.
108 waitTimer.expires_from_now(
Zev Weissf72eb832021-06-25 05:55:08 +0000109 boost::posix_time::milliseconds(bridgeGpio->setupTimeMs));
Yong Li1afda6b2020-04-09 19:24:13 +0800110 waitTimer.async_wait(
111 [weakRef, buffer](const boost::system::error_code& ec) {
112 std::shared_ptr<ADCSensor> self = weakRef.lock();
113 if (ec == boost::asio::error::operation_aborted)
114 {
115 return; // we're being canceled
116 }
117
118 if (self)
119 {
120 boost::asio::async_read_until(
121 self->inputDev, *buffer, '\n',
122 [weakRef, buffer](const boost::system::error_code& ec,
123 std::size_t /*bytes_transfered*/) {
124 std::shared_ptr<ADCSensor> self = weakRef.lock();
125 if (self)
126 {
127 self->readBuf = buffer;
128 self->handleResponse(ec);
129 }
130 });
131 }
132 });
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400133 }
134 else
135 {
136 boost::asio::async_read_until(
Yong Li1afda6b2020-04-09 19:24:13 +0800137 inputDev, *buffer, '\n',
138 [weakRef, buffer](const boost::system::error_code& ec,
139 std::size_t /*bytes_transfered*/) {
140 std::shared_ptr<ADCSensor> self = weakRef.lock();
141 if (self)
142 {
143 self->readBuf = buffer;
144 self->handleResponse(ec);
145 }
146 });
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400147 }
James Feist6714a252018-09-10 15:26:18 -0700148}
149
James Feistd8705872019-02-08 13:26:09 -0800150void ADCSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700151{
Yong Li1afda6b2020-04-09 19:24:13 +0800152 std::weak_ptr<ADCSensor> weakRef = weak_from_this();
153
James Feist6714a252018-09-10 15:26:18 -0700154 if (err == boost::system::errc::bad_file_descriptor)
155 {
156 return; // we're being destroyed
157 }
Yong Li1afda6b2020-04-09 19:24:13 +0800158 std::istream responseStream(readBuf.get());
James Feist6714a252018-09-10 15:26:18 -0700159
160 if (!err)
161 {
162 std::string response;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700163 std::getline(responseStream, response);
James Feist6714a252018-09-10 15:26:18 -0700164
165 // todo read scaling factors from configuration
166 try
167 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700168 rawValue = std::stod(response);
169 double nvalue = (rawValue / sensorScaleFactor) / scaleFactor;
James Feistcc6d4fe2018-11-14 16:38:26 -0800170 nvalue = std::round(nvalue * roundFactor) / roundFactor;
Josh Lehan833661a2020-03-04 17:35:41 -0800171 updateValue(nvalue);
James Feist6714a252018-09-10 15:26:18 -0700172 }
Patrick Williams26601e82021-10-06 12:43:25 -0500173 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700174 {
James Feist961bf092020-07-01 16:38:12 -0700175 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700176 }
177 }
178 else
179 {
James Feist961bf092020-07-01 16:38:12 -0700180 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700181 }
182
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700183 responseStream.clear();
184 inputDev.close();
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400185 if (bridgeGpio.has_value())
186 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700187 (*bridgeGpio).set(0);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400188 }
James Feist6714a252018-09-10 15:26:18 -0700189 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700190 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700191 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700192 std::cerr << "adcsensor " << name << " failed to open " << path << "\n";
James Feist6714a252018-09-10 15:26:18 -0700193 return; // we're no longer valid
194 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700195 inputDev.assign(fd);
196 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
Yong Li1afda6b2020-04-09 19:24:13 +0800197 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
198 std::shared_ptr<ADCSensor> self = weakRef.lock();
James Feist6714a252018-09-10 15:26:18 -0700199 if (ec == boost::asio::error::operation_aborted)
200 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700201 if (self)
202 {
203 std::cerr << "adcsensor " << self->name << " read cancelled\n";
204 }
205 else
206 {
207 std::cerr << "adcsensor read cancelled no self\n";
208 }
James Feist6714a252018-09-10 15:26:18 -0700209 return; // we're being canceled
210 }
Yong Li1afda6b2020-04-09 19:24:13 +0800211
212 if (self)
213 {
214 self->setupRead();
215 }
Zhikui Rend3da1282020-09-11 17:02:01 -0700216 else
217 {
218 std::cerr << "adcsensor weakref no self\n";
219 }
James Feist6714a252018-09-10 15:26:18 -0700220 });
221}
222
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700223void ADCSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700224{
James Feist961bf092020-07-01 16:38:12 -0700225 if (!readingStateGood())
James Feist71d31b22019-01-02 16:57:54 -0800226 {
227 return;
228 }
James Feist46342ec2019-03-06 14:03:41 -0800229
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700230 thresholds::checkThresholdsPowerDelay(weak_from_this(), thresholdTimer);
James Feist6714a252018-09-10 15:26:18 -0700231}