blob: 4d47762e85a3da35131a3f524611c62d38fe6a9b [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 Feist8086aba2020-08-25 16:00:59 -070020#include <boost/asio/read_until.hpp>
James Feist6714a252018-09-10 15:26:18 -070021#include <boost/date_time/posix_time/posix_time.hpp>
James Feist38fb5982020-05-28 10:09:54 -070022#include <sdbusplus/asio/connection.hpp>
23#include <sdbusplus/asio/object_server.hpp>
24
Patrick Venture96e97db2019-10-31 13:44:38 -070025#include <cmath>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040026#include <filesystem>
27#include <fstream>
James Feist6714a252018-09-10 15:26:18 -070028#include <iostream>
29#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070030#include <memory>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040031#include <optional>
James Feist6714a252018-09-10 15:26:18 -070032#include <string>
Patrick Venture96e97db2019-10-31 13:44:38 -070033#include <vector>
Patrick Venture2da370e2019-11-01 11:51:31 -070034
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
Zhikui Ren937eb542020-10-12 13:42:08 -070038static constexpr double roundFactor = 10000; // 3 decimal places
39static constexpr double maxVoltageReading = 1.8; // pre sensor scaling
40static constexpr double minVoltageReading = 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,
Jeff Lin7b7a9de2021-02-22 11:16:27 +080046 std::vector<thresholds::Threshold>&& thresholdsIn,
Jeff Lind9cd7042020-11-20 15:49:28 +080047 const double scaleFactor, const float pollRate,
48 PowerState readState,
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040049 const std::string& sensorConfiguration,
Jae Hyun Yoo1cbd1c62019-07-29 15:02:43 -070050 std::optional<BridgeGpio>&& bridgeGpio) :
Zhikui Renda98f092021-11-01 09:41:08 -070051 Sensor(escapeName(sensorName), std::move(thresholdsIn), sensorConfiguration,
Jie Yang3291b9c2021-07-29 14:46:51 -070052 "xyz.openbmc_project.Configuration.ADC", false, false,
Zhikui Ren937eb542020-10-12 13:42:08 -070053 maxVoltageReading / scaleFactor, minVoltageReading / scaleFactor,
James Feiste3338522020-09-15 15:40:30 -070054 conn, readState),
Ed Tanous2049bd22022-07-09 07:20:26 -070055 objServer(objectServer), inputDev(io), waitTimer(io), path(path),
56 scaleFactor(scaleFactor),
Jeff Lind9cd7042020-11-20 15:49:28 +080057 sensorPollMs(static_cast<unsigned int>(pollRate * 1000)),
Zhikui Ren12c2c0e2021-04-28 17:21:21 -070058 bridgeGpio(std::move(bridgeGpio)), thresholdTimer(io)
James Feist6714a252018-09-10 15:26:18 -070059{
Ed Tanous99c44092022-01-14 09:59:09 -080060 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
61 int fd = open(path.c_str(), O_RDONLY);
62 if (fd < 0)
63 {
64 std::cerr << "unable to open acd device \n";
65 }
66
67 inputDev.assign(fd);
68
James Feist251c7822018-09-12 12:54:15 -070069 sensorInterface = objectServer.add_interface(
70 "/xyz/openbmc_project/sensors/voltage/" + name,
71 "xyz.openbmc_project.Sensor.Value");
Jayashree Dhanapal56678082022-01-04 17:27:20 +053072 for (const auto& threshold : thresholds)
James Feist6714a252018-09-10 15:26:18 -070073 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053074 std::string interface = thresholds::getInterface(threshold.level);
75 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
76 objectServer.add_interface(
77 "/xyz/openbmc_project/sensors/voltage/" + name, interface);
James Feist6714a252018-09-10 15:26:18 -070078 }
James Feist078f2322019-03-08 11:09:05 -080079 association = objectServer.add_interface(
James Feist2adc95c2019-09-30 14:55:28 -070080 "/xyz/openbmc_project/sensors/voltage/" + name, association::interface);
Andrei Kartashev39287412022-02-04 16:04:47 +030081 setInitialProperties(sensor_paths::unitVolts);
James Feist6714a252018-09-10 15:26:18 -070082}
83
84ADCSensor::~ADCSensor()
85{
86 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070087 inputDev.close();
88 waitTimer.cancel();
Zhikui Ren12c2c0e2021-04-28 17:21:21 -070089
Jayashree Dhanapal56678082022-01-04 17:27:20 +053090 for (const auto& iface : thresholdInterfaces)
91 {
92 objServer.remove_interface(iface);
93 }
James Feist251c7822018-09-12 12:54:15 -070094 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -080095 objServer.remove_interface(association);
James Feist6714a252018-09-10 15:26:18 -070096}
97
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070098void ADCSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -070099{
Yong Li1afda6b2020-04-09 19:24:13 +0800100 std::shared_ptr<boost::asio::streambuf> buffer =
101 std::make_shared<boost::asio::streambuf>();
102
103 std::weak_ptr<ADCSensor> weakRef = weak_from_this();
104
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400105 if (bridgeGpio.has_value())
106 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700107 (*bridgeGpio).set(1);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400108 // In case a channel has a bridge circuit,we have to turn the bridge on
109 // prior to reading a value at least for one scan cycle to get a valid
110 // value. Guarantee that the HW signal can be stable, the HW signal
111 // could be instability.
112 waitTimer.expires_from_now(
Zev Weissf72eb832021-06-25 05:55:08 +0000113 boost::posix_time::milliseconds(bridgeGpio->setupTimeMs));
Yong Li1afda6b2020-04-09 19:24:13 +0800114 waitTimer.async_wait(
115 [weakRef, buffer](const boost::system::error_code& ec) {
Ed Tanousbb679322022-05-16 16:10:00 -0700116 std::shared_ptr<ADCSensor> self = weakRef.lock();
117 if (ec == boost::asio::error::operation_aborted)
118 {
119 return; // we're being canceled
120 }
Yong Li1afda6b2020-04-09 19:24:13 +0800121
Ed Tanousbb679322022-05-16 16:10:00 -0700122 if (self)
123 {
124 boost::asio::async_read_until(
125 self->inputDev, *buffer, '\n',
126 [weakRef, buffer](const boost::system::error_code& ec,
127 std::size_t /*bytes_transfered*/) {
128 std::shared_ptr<ADCSensor> self = weakRef.lock();
129 if (self)
130 {
131 self->readBuf = buffer;
132 self->handleResponse(ec);
133 }
134 });
135 }
136 });
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400137 }
138 else
139 {
140 boost::asio::async_read_until(
Yong Li1afda6b2020-04-09 19:24:13 +0800141 inputDev, *buffer, '\n',
142 [weakRef, buffer](const boost::system::error_code& ec,
143 std::size_t /*bytes_transfered*/) {
Ed Tanousbb679322022-05-16 16:10:00 -0700144 std::shared_ptr<ADCSensor> self = weakRef.lock();
145 if (self)
146 {
147 self->readBuf = buffer;
148 self->handleResponse(ec);
149 }
Yong Li1afda6b2020-04-09 19:24:13 +0800150 });
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400151 }
James Feist6714a252018-09-10 15:26:18 -0700152}
153
James Feistd8705872019-02-08 13:26:09 -0800154void ADCSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700155{
Yong Li1afda6b2020-04-09 19:24:13 +0800156 std::weak_ptr<ADCSensor> weakRef = weak_from_this();
157
James Feist6714a252018-09-10 15:26:18 -0700158 if (err == boost::system::errc::bad_file_descriptor)
159 {
160 return; // we're being destroyed
161 }
Yong Li1afda6b2020-04-09 19:24:13 +0800162 std::istream responseStream(readBuf.get());
James Feist6714a252018-09-10 15:26:18 -0700163
164 if (!err)
165 {
166 std::string response;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700167 std::getline(responseStream, response);
James Feist6714a252018-09-10 15:26:18 -0700168
169 // todo read scaling factors from configuration
170 try
171 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700172 rawValue = std::stod(response);
173 double nvalue = (rawValue / sensorScaleFactor) / scaleFactor;
James Feistcc6d4fe2018-11-14 16:38:26 -0800174 nvalue = std::round(nvalue * roundFactor) / roundFactor;
Josh Lehan833661a2020-03-04 17:35:41 -0800175 updateValue(nvalue);
James Feist6714a252018-09-10 15:26:18 -0700176 }
Patrick Williams26601e82021-10-06 12:43:25 -0500177 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700178 {
James Feist961bf092020-07-01 16:38:12 -0700179 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700180 }
181 }
182 else
183 {
James Feist961bf092020-07-01 16:38:12 -0700184 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700185 }
186
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700187 responseStream.clear();
188 inputDev.close();
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400189 if (bridgeGpio.has_value())
190 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700191 (*bridgeGpio).set(0);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400192 }
Ed Tanous99c44092022-01-14 09:59:09 -0800193
194 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
James Feist6714a252018-09-10 15:26:18 -0700195 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700196 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700197 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700198 std::cerr << "adcsensor " << name << " failed to open " << path << "\n";
James Feist6714a252018-09-10 15:26:18 -0700199 return; // we're no longer valid
200 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700201 inputDev.assign(fd);
202 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
Yong Li1afda6b2020-04-09 19:24:13 +0800203 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
204 std::shared_ptr<ADCSensor> self = weakRef.lock();
James Feist6714a252018-09-10 15:26:18 -0700205 if (ec == boost::asio::error::operation_aborted)
206 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700207 if (self)
208 {
209 std::cerr << "adcsensor " << self->name << " read cancelled\n";
210 }
211 else
212 {
213 std::cerr << "adcsensor read cancelled no self\n";
214 }
James Feist6714a252018-09-10 15:26:18 -0700215 return; // we're being canceled
216 }
Yong Li1afda6b2020-04-09 19:24:13 +0800217
218 if (self)
219 {
220 self->setupRead();
221 }
Zhikui Rend3da1282020-09-11 17:02:01 -0700222 else
223 {
224 std::cerr << "adcsensor weakref no self\n";
225 }
James Feist6714a252018-09-10 15:26:18 -0700226 });
227}
228
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700229void ADCSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700230{
James Feist961bf092020-07-01 16:38:12 -0700231 if (!readingStateGood())
James Feist71d31b22019-01-02 16:57:54 -0800232 {
233 return;
234 }
James Feist46342ec2019-03-06 14:03:41 -0800235
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700236 thresholds::checkThresholdsPowerDelay(weak_from_this(), thresholdTimer);
James Feist6714a252018-09-10 15:26:18 -0700237}