blob: cbf7132a82a2acfdb33d61234968fd2b3cef491d [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
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103017#include "ADCSensor.hpp"
18
Ed Tanouseacbfdd2024-04-04 12:00:24 -070019#include "SensorPaths.hpp"
20#include "Thresholds.hpp"
21#include "Utils.hpp"
22#include "sensor.hpp"
James Feist6714a252018-09-10 15:26:18 -070023
Ed Tanouseacbfdd2024-04-04 12:00:24 -070024#include <fcntl.h>
25
26#include <boost/asio/error.hpp>
27#include <boost/asio/io_context.hpp>
James Feist8086aba2020-08-25 16:00:59 -070028#include <boost/asio/read_until.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070029#include <boost/asio/streambuf.hpp>
George Liud4bc41f2025-02-20 09:21:38 +080030#include <phosphor-logging/lg2.hpp>
James Feist38fb5982020-05-28 10:09:54 -070031#include <sdbusplus/asio/connection.hpp>
32#include <sdbusplus/asio/object_server.hpp>
33
Ed Tanouseacbfdd2024-04-04 12:00:24 -070034#include <chrono>
Patrick Venture96e97db2019-10-31 13:44:38 -070035#include <cmath>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070036#include <cstddef>
George Liud4bc41f2025-02-20 09:21:38 +080037#include <istream>
Thang Tranaf1724b2024-12-09 13:37:01 +070038#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070039#include <memory>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040040#include <optional>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070041#include <stdexcept>
James Feist6714a252018-09-10 15:26:18 -070042#include <string>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070043#include <utility>
Patrick Venture96e97db2019-10-31 13:44:38 -070044#include <vector>
Patrick Venture2da370e2019-11-01 11:51:31 -070045
James Feist6714a252018-09-10 15:26:18 -070046// scaling factor from hwmon
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070047static constexpr unsigned int sensorScaleFactor = 1000;
James Feist6714a252018-09-10 15:26:18 -070048
Zhikui Ren937eb542020-10-12 13:42:08 -070049static constexpr double roundFactor = 10000; // 3 decimal places
50static constexpr double maxVoltageReading = 1.8; // pre sensor scaling
51static constexpr double minVoltageReading = 0;
James Feistcc6d4fe2018-11-14 16:38:26 -080052
Patrick Williams2aaf7172024-08-16 15:20:40 -040053ADCSensor::ADCSensor(
54 const std::string& path, sdbusplus::asio::object_server& objectServer,
55 std::shared_ptr<sdbusplus::asio::connection>& conn,
56 boost::asio::io_context& io, const std::string& sensorName,
57 std::vector<thresholds::Threshold>&& thresholdsIn, const double scaleFactor,
58 const float pollRate, PowerState readState,
59 const std::string& sensorConfiguration,
60 std::optional<BridgeGpio>&& bridgeGpio) :
Zhikui Renda98f092021-11-01 09:41:08 -070061 Sensor(escapeName(sensorName), std::move(thresholdsIn), sensorConfiguration,
Zev Weiss054aad82022-08-18 01:37:34 -070062 "ADC", false, false, maxVoltageReading / scaleFactor,
63 minVoltageReading / scaleFactor, conn, readState),
Ed Tanous2049bd22022-07-09 07:20:26 -070064 objServer(objectServer), inputDev(io), waitTimer(io), path(path),
65 scaleFactor(scaleFactor),
Jeff Lind9cd7042020-11-20 15:49:28 +080066 sensorPollMs(static_cast<unsigned int>(pollRate * 1000)),
Zhikui Ren12c2c0e2021-04-28 17:21:21 -070067 bridgeGpio(std::move(bridgeGpio)), thresholdTimer(io)
James Feist6714a252018-09-10 15:26:18 -070068{
Ed Tanous99c44092022-01-14 09:59:09 -080069 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
70 int fd = open(path.c_str(), O_RDONLY);
71 if (fd < 0)
72 {
George Liud4bc41f2025-02-20 09:21:38 +080073 lg2::error("unable to open acd device");
Ed Tanous99c44092022-01-14 09:59:09 -080074 }
75
76 inputDev.assign(fd);
77
James Feist251c7822018-09-12 12:54:15 -070078 sensorInterface = objectServer.add_interface(
79 "/xyz/openbmc_project/sensors/voltage/" + name,
80 "xyz.openbmc_project.Sensor.Value");
Jayashree Dhanapal56678082022-01-04 17:27:20 +053081 for (const auto& threshold : thresholds)
James Feist6714a252018-09-10 15:26:18 -070082 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053083 std::string interface = thresholds::getInterface(threshold.level);
84 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
85 objectServer.add_interface(
86 "/xyz/openbmc_project/sensors/voltage/" + name, interface);
James Feist6714a252018-09-10 15:26:18 -070087 }
James Feist078f2322019-03-08 11:09:05 -080088 association = objectServer.add_interface(
James Feist2adc95c2019-09-30 14:55:28 -070089 "/xyz/openbmc_project/sensors/voltage/" + name, association::interface);
Andrei Kartashev39287412022-02-04 16:04:47 +030090 setInitialProperties(sensor_paths::unitVolts);
James Feist6714a252018-09-10 15:26:18 -070091}
92
93ADCSensor::~ADCSensor()
94{
95 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070096 inputDev.close();
97 waitTimer.cancel();
Zhikui Ren12c2c0e2021-04-28 17:21:21 -070098
Jayashree Dhanapal56678082022-01-04 17:27:20 +053099 for (const auto& iface : thresholdInterfaces)
100 {
101 objServer.remove_interface(iface);
102 }
James Feist251c7822018-09-12 12:54:15 -0700103 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800104 objServer.remove_interface(association);
James Feist6714a252018-09-10 15:26:18 -0700105}
106
Ed Tanous201a1012024-04-03 18:07:28 -0700107void ADCSensor::setupRead()
James Feist6714a252018-09-10 15:26:18 -0700108{
Thang Tranaf1724b2024-12-09 13:37:01 +0700109 if (!readingStateGood())
110 {
111 markAvailable(false);
112 updateValue(std::numeric_limits<double>::quiet_NaN());
113 restartRead();
114 return;
115 }
116
Yong Li1afda6b2020-04-09 19:24:13 +0800117 std::shared_ptr<boost::asio::streambuf> buffer =
118 std::make_shared<boost::asio::streambuf>();
119
120 std::weak_ptr<ADCSensor> weakRef = weak_from_this();
121
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400122 if (bridgeGpio.has_value())
123 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700124 (*bridgeGpio).set(1);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400125 // In case a channel has a bridge circuit,we have to turn the bridge on
126 // prior to reading a value at least for one scan cycle to get a valid
127 // value. Guarantee that the HW signal can be stable, the HW signal
128 // could be instability.
Ed Tanous83db50c2023-03-01 10:20:24 -0800129 waitTimer.expires_after(
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700130 std::chrono::milliseconds(bridgeGpio->setupTimeMs));
Yong Li1afda6b2020-04-09 19:24:13 +0800131 waitTimer.async_wait(
132 [weakRef, buffer](const boost::system::error_code& ec) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400133 std::shared_ptr<ADCSensor> self = weakRef.lock();
134 if (ec == boost::asio::error::operation_aborted)
135 {
136 return; // we're being canceled
137 }
Yong Li1afda6b2020-04-09 19:24:13 +0800138
Patrick Williams2aaf7172024-08-16 15:20:40 -0400139 if (self)
140 {
141 boost::asio::async_read_until(
142 self->inputDev, *buffer, '\n',
143 [weakRef, buffer](const boost::system::error_code& ec,
144 std::size_t /*bytes_transfered*/) {
145 std::shared_ptr<ADCSensor> self = weakRef.lock();
146 if (self)
147 {
148 self->readBuf = buffer;
149 self->handleResponse(ec);
150 }
151 });
152 }
153 });
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400154 }
155 else
156 {
157 boost::asio::async_read_until(
Yong Li1afda6b2020-04-09 19:24:13 +0800158 inputDev, *buffer, '\n',
159 [weakRef, buffer](const boost::system::error_code& ec,
160 std::size_t /*bytes_transfered*/) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400161 std::shared_ptr<ADCSensor> self = weakRef.lock();
162 if (self)
163 {
164 self->readBuf = buffer;
165 self->handleResponse(ec);
166 }
167 });
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400168 }
James Feist6714a252018-09-10 15:26:18 -0700169}
170
Thang Tranaf1724b2024-12-09 13:37:01 +0700171void ADCSensor::restartRead()
172{
173 std::weak_ptr<ADCSensor> weakRef = weak_from_this();
174 waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs));
175 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
176 std::shared_ptr<ADCSensor> self = weakRef.lock();
177 if (ec == boost::asio::error::operation_aborted)
178 {
179 if (self)
180 {
George Liud4bc41f2025-02-20 09:21:38 +0800181 lg2::error("adcsensor '{NAME}' read cancelled", "NAME",
182 self->name);
Thang Tranaf1724b2024-12-09 13:37:01 +0700183 }
184 else
185 {
George Liud4bc41f2025-02-20 09:21:38 +0800186 lg2::error("adcsensor read cancelled no self");
Thang Tranaf1724b2024-12-09 13:37:01 +0700187 }
188 return; // we're being canceled
189 }
190
191 if (self)
192 {
193 self->setupRead();
194 }
195 else
196 {
George Liud4bc41f2025-02-20 09:21:38 +0800197 lg2::error("adcsensor weakref no self");
Thang Tranaf1724b2024-12-09 13:37:01 +0700198 }
199 });
200}
201
James Feistd8705872019-02-08 13:26:09 -0800202void ADCSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700203{
Yong Li1afda6b2020-04-09 19:24:13 +0800204 std::weak_ptr<ADCSensor> weakRef = weak_from_this();
205
James Feist6714a252018-09-10 15:26:18 -0700206 if (err == boost::system::errc::bad_file_descriptor)
207 {
208 return; // we're being destroyed
209 }
Yong Li1afda6b2020-04-09 19:24:13 +0800210 std::istream responseStream(readBuf.get());
James Feist6714a252018-09-10 15:26:18 -0700211
212 if (!err)
213 {
214 std::string response;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700215 std::getline(responseStream, response);
James Feist6714a252018-09-10 15:26:18 -0700216
217 // todo read scaling factors from configuration
218 try
219 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700220 rawValue = std::stod(response);
221 double nvalue = (rawValue / sensorScaleFactor) / scaleFactor;
James Feistcc6d4fe2018-11-14 16:38:26 -0800222 nvalue = std::round(nvalue * roundFactor) / roundFactor;
Josh Lehan833661a2020-03-04 17:35:41 -0800223 updateValue(nvalue);
James Feist6714a252018-09-10 15:26:18 -0700224 }
Patrick Williams26601e82021-10-06 12:43:25 -0500225 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700226 {
James Feist961bf092020-07-01 16:38:12 -0700227 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700228 }
229 }
230 else
231 {
James Feist961bf092020-07-01 16:38:12 -0700232 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700233 }
234
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700235 responseStream.clear();
236 inputDev.close();
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400237 if (bridgeGpio.has_value())
238 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700239 (*bridgeGpio).set(0);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400240 }
Ed Tanous99c44092022-01-14 09:59:09 -0800241
242 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
James Feist6714a252018-09-10 15:26:18 -0700243 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700244 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700245 {
George Liud4bc41f2025-02-20 09:21:38 +0800246 lg2::error("adcsensor '{NAME}' failed to open '{PATH}'", "NAME", name,
247 "PATH", path);
James Feist6714a252018-09-10 15:26:18 -0700248 return; // we're no longer valid
249 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700250 inputDev.assign(fd);
Thang Tranaf1724b2024-12-09 13:37:01 +0700251 restartRead();
James Feist6714a252018-09-10 15:26:18 -0700252}
253
Ed Tanous201a1012024-04-03 18:07:28 -0700254void ADCSensor::checkThresholds()
James Feist6714a252018-09-10 15:26:18 -0700255{
James Feist961bf092020-07-01 16:38:12 -0700256 if (!readingStateGood())
James Feist71d31b22019-01-02 16:57:54 -0800257 {
258 return;
259 }
James Feist46342ec2019-03-06 14:03:41 -0800260
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700261 thresholds::checkThresholdsPowerDelay(weak_from_this(), thresholdTimer);
James Feist6714a252018-09-10 15:26:18 -0700262}