blob: 170e79b986b1e27d7c7ee2e0f01f08eafbb6107b [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>
James Feist38fb5982020-05-28 10:09:54 -070030#include <sdbusplus/asio/connection.hpp>
31#include <sdbusplus/asio/object_server.hpp>
32
Ed Tanouseacbfdd2024-04-04 12:00:24 -070033#include <chrono>
Patrick Venture96e97db2019-10-31 13:44:38 -070034#include <cmath>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070035#include <cstddef>
James Feist6714a252018-09-10 15:26:18 -070036#include <iostream>
Thang Tranaf1724b2024-12-09 13:37:01 +070037#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070038#include <memory>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040039#include <optional>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070040#include <stdexcept>
James Feist6714a252018-09-10 15:26:18 -070041#include <string>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070042#include <utility>
Patrick Venture96e97db2019-10-31 13:44:38 -070043#include <vector>
Patrick Venture2da370e2019-11-01 11:51:31 -070044
James Feist6714a252018-09-10 15:26:18 -070045// scaling factor from hwmon
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070046static constexpr unsigned int sensorScaleFactor = 1000;
James Feist6714a252018-09-10 15:26:18 -070047
Zhikui Ren937eb542020-10-12 13:42:08 -070048static constexpr double roundFactor = 10000; // 3 decimal places
49static constexpr double maxVoltageReading = 1.8; // pre sensor scaling
50static constexpr double minVoltageReading = 0;
James Feistcc6d4fe2018-11-14 16:38:26 -080051
Patrick Williams2aaf7172024-08-16 15:20:40 -040052ADCSensor::ADCSensor(
53 const std::string& path, sdbusplus::asio::object_server& objectServer,
54 std::shared_ptr<sdbusplus::asio::connection>& conn,
55 boost::asio::io_context& io, const std::string& sensorName,
56 std::vector<thresholds::Threshold>&& thresholdsIn, const double scaleFactor,
57 const float pollRate, PowerState readState,
58 const std::string& sensorConfiguration,
59 std::optional<BridgeGpio>&& bridgeGpio) :
Zhikui Renda98f092021-11-01 09:41:08 -070060 Sensor(escapeName(sensorName), std::move(thresholdsIn), sensorConfiguration,
Zev Weiss054aad82022-08-18 01:37:34 -070061 "ADC", false, false, maxVoltageReading / scaleFactor,
62 minVoltageReading / scaleFactor, conn, readState),
Ed Tanous2049bd22022-07-09 07:20:26 -070063 objServer(objectServer), inputDev(io), waitTimer(io), path(path),
64 scaleFactor(scaleFactor),
Jeff Lind9cd7042020-11-20 15:49:28 +080065 sensorPollMs(static_cast<unsigned int>(pollRate * 1000)),
Zhikui Ren12c2c0e2021-04-28 17:21:21 -070066 bridgeGpio(std::move(bridgeGpio)), thresholdTimer(io)
James Feist6714a252018-09-10 15:26:18 -070067{
Ed Tanous99c44092022-01-14 09:59:09 -080068 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
69 int fd = open(path.c_str(), O_RDONLY);
70 if (fd < 0)
71 {
72 std::cerr << "unable to open acd device \n";
73 }
74
75 inputDev.assign(fd);
76
James Feist251c7822018-09-12 12:54:15 -070077 sensorInterface = objectServer.add_interface(
78 "/xyz/openbmc_project/sensors/voltage/" + name,
79 "xyz.openbmc_project.Sensor.Value");
Jayashree Dhanapal56678082022-01-04 17:27:20 +053080 for (const auto& threshold : thresholds)
James Feist6714a252018-09-10 15:26:18 -070081 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053082 std::string interface = thresholds::getInterface(threshold.level);
83 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
84 objectServer.add_interface(
85 "/xyz/openbmc_project/sensors/voltage/" + name, interface);
James Feist6714a252018-09-10 15:26:18 -070086 }
James Feist078f2322019-03-08 11:09:05 -080087 association = objectServer.add_interface(
James Feist2adc95c2019-09-30 14:55:28 -070088 "/xyz/openbmc_project/sensors/voltage/" + name, association::interface);
Andrei Kartashev39287412022-02-04 16:04:47 +030089 setInitialProperties(sensor_paths::unitVolts);
James Feist6714a252018-09-10 15:26:18 -070090}
91
92ADCSensor::~ADCSensor()
93{
94 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070095 inputDev.close();
96 waitTimer.cancel();
Zhikui Ren12c2c0e2021-04-28 17:21:21 -070097
Jayashree Dhanapal56678082022-01-04 17:27:20 +053098 for (const auto& iface : thresholdInterfaces)
99 {
100 objServer.remove_interface(iface);
101 }
James Feist251c7822018-09-12 12:54:15 -0700102 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800103 objServer.remove_interface(association);
James Feist6714a252018-09-10 15:26:18 -0700104}
105
Ed Tanous201a1012024-04-03 18:07:28 -0700106void ADCSensor::setupRead()
James Feist6714a252018-09-10 15:26:18 -0700107{
Thang Tranaf1724b2024-12-09 13:37:01 +0700108 if (!readingStateGood())
109 {
110 markAvailable(false);
111 updateValue(std::numeric_limits<double>::quiet_NaN());
112 restartRead();
113 return;
114 }
115
Yong Li1afda6b2020-04-09 19:24:13 +0800116 std::shared_ptr<boost::asio::streambuf> buffer =
117 std::make_shared<boost::asio::streambuf>();
118
119 std::weak_ptr<ADCSensor> weakRef = weak_from_this();
120
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400121 if (bridgeGpio.has_value())
122 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700123 (*bridgeGpio).set(1);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400124 // In case a channel has a bridge circuit,we have to turn the bridge on
125 // prior to reading a value at least for one scan cycle to get a valid
126 // value. Guarantee that the HW signal can be stable, the HW signal
127 // could be instability.
Ed Tanous83db50c2023-03-01 10:20:24 -0800128 waitTimer.expires_after(
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700129 std::chrono::milliseconds(bridgeGpio->setupTimeMs));
Yong Li1afda6b2020-04-09 19:24:13 +0800130 waitTimer.async_wait(
131 [weakRef, buffer](const boost::system::error_code& ec) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400132 std::shared_ptr<ADCSensor> self = weakRef.lock();
133 if (ec == boost::asio::error::operation_aborted)
134 {
135 return; // we're being canceled
136 }
Yong Li1afda6b2020-04-09 19:24:13 +0800137
Patrick Williams2aaf7172024-08-16 15:20:40 -0400138 if (self)
139 {
140 boost::asio::async_read_until(
141 self->inputDev, *buffer, '\n',
142 [weakRef, buffer](const boost::system::error_code& ec,
143 std::size_t /*bytes_transfered*/) {
144 std::shared_ptr<ADCSensor> self = weakRef.lock();
145 if (self)
146 {
147 self->readBuf = buffer;
148 self->handleResponse(ec);
149 }
150 });
151 }
152 });
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400153 }
154 else
155 {
156 boost::asio::async_read_until(
Yong Li1afda6b2020-04-09 19:24:13 +0800157 inputDev, *buffer, '\n',
158 [weakRef, buffer](const boost::system::error_code& ec,
159 std::size_t /*bytes_transfered*/) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400160 std::shared_ptr<ADCSensor> self = weakRef.lock();
161 if (self)
162 {
163 self->readBuf = buffer;
164 self->handleResponse(ec);
165 }
166 });
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400167 }
James Feist6714a252018-09-10 15:26:18 -0700168}
169
Thang Tranaf1724b2024-12-09 13:37:01 +0700170void ADCSensor::restartRead()
171{
172 std::weak_ptr<ADCSensor> weakRef = weak_from_this();
173 waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs));
174 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
175 std::shared_ptr<ADCSensor> self = weakRef.lock();
176 if (ec == boost::asio::error::operation_aborted)
177 {
178 if (self)
179 {
180 std::cerr << "adcsensor " << self->name << " read cancelled\n";
181 }
182 else
183 {
184 std::cerr << "adcsensor read cancelled no self\n";
185 }
186 return; // we're being canceled
187 }
188
189 if (self)
190 {
191 self->setupRead();
192 }
193 else
194 {
195 std::cerr << "adcsensor weakref no self\n";
196 }
197 });
198}
199
James Feistd8705872019-02-08 13:26:09 -0800200void ADCSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700201{
Yong Li1afda6b2020-04-09 19:24:13 +0800202 std::weak_ptr<ADCSensor> weakRef = weak_from_this();
203
James Feist6714a252018-09-10 15:26:18 -0700204 if (err == boost::system::errc::bad_file_descriptor)
205 {
206 return; // we're being destroyed
207 }
Yong Li1afda6b2020-04-09 19:24:13 +0800208 std::istream responseStream(readBuf.get());
James Feist6714a252018-09-10 15:26:18 -0700209
210 if (!err)
211 {
212 std::string response;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700213 std::getline(responseStream, response);
James Feist6714a252018-09-10 15:26:18 -0700214
215 // todo read scaling factors from configuration
216 try
217 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700218 rawValue = std::stod(response);
219 double nvalue = (rawValue / sensorScaleFactor) / scaleFactor;
James Feistcc6d4fe2018-11-14 16:38:26 -0800220 nvalue = std::round(nvalue * roundFactor) / roundFactor;
Josh Lehan833661a2020-03-04 17:35:41 -0800221 updateValue(nvalue);
James Feist6714a252018-09-10 15:26:18 -0700222 }
Patrick Williams26601e82021-10-06 12:43:25 -0500223 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700224 {
James Feist961bf092020-07-01 16:38:12 -0700225 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700226 }
227 }
228 else
229 {
James Feist961bf092020-07-01 16:38:12 -0700230 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700231 }
232
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700233 responseStream.clear();
234 inputDev.close();
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400235 if (bridgeGpio.has_value())
236 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700237 (*bridgeGpio).set(0);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400238 }
Ed Tanous99c44092022-01-14 09:59:09 -0800239
240 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
James Feist6714a252018-09-10 15:26:18 -0700241 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700242 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700243 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700244 std::cerr << "adcsensor " << name << " failed to open " << path << "\n";
James Feist6714a252018-09-10 15:26:18 -0700245 return; // we're no longer valid
246 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700247 inputDev.assign(fd);
Thang Tranaf1724b2024-12-09 13:37:01 +0700248 restartRead();
James Feist6714a252018-09-10 15:26:18 -0700249}
250
Ed Tanous201a1012024-04-03 18:07:28 -0700251void ADCSensor::checkThresholds()
James Feist6714a252018-09-10 15:26:18 -0700252{
James Feist961bf092020-07-01 16:38:12 -0700253 if (!readingStateGood())
James Feist71d31b22019-01-02 16:57:54 -0800254 {
255 return;
256 }
James Feist46342ec2019-03-06 14:03:41 -0800257
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700258 thresholds::checkThresholdsPowerDelay(weak_from_this(), thresholdTimer);
James Feist6714a252018-09-10 15:26:18 -0700259}