blob: 0b3fd28cc5f0222016d94112115fe391dc3ccc6e [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>
Patrick Venture96e97db2019-10-31 13:44:38 -070037#include <memory>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040038#include <optional>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070039#include <stdexcept>
James Feist6714a252018-09-10 15:26:18 -070040#include <string>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070041#include <utility>
Patrick Venture96e97db2019-10-31 13:44:38 -070042#include <vector>
Patrick Venture2da370e2019-11-01 11:51:31 -070043
James Feist6714a252018-09-10 15:26:18 -070044// scaling factor from hwmon
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070045static constexpr unsigned int sensorScaleFactor = 1000;
James Feist6714a252018-09-10 15:26:18 -070046
Zhikui Ren937eb542020-10-12 13:42:08 -070047static constexpr double roundFactor = 10000; // 3 decimal places
48static constexpr double maxVoltageReading = 1.8; // pre sensor scaling
49static constexpr double minVoltageReading = 0;
James Feistcc6d4fe2018-11-14 16:38:26 -080050
Patrick Williams2aaf7172024-08-16 15:20:40 -040051ADCSensor::ADCSensor(
52 const std::string& path, sdbusplus::asio::object_server& objectServer,
53 std::shared_ptr<sdbusplus::asio::connection>& conn,
54 boost::asio::io_context& io, const std::string& sensorName,
55 std::vector<thresholds::Threshold>&& thresholdsIn, const double scaleFactor,
56 const float pollRate, PowerState readState,
57 const std::string& sensorConfiguration,
58 std::optional<BridgeGpio>&& bridgeGpio) :
Zhikui Renda98f092021-11-01 09:41:08 -070059 Sensor(escapeName(sensorName), std::move(thresholdsIn), sensorConfiguration,
Zev Weiss054aad82022-08-18 01:37:34 -070060 "ADC", false, false, maxVoltageReading / scaleFactor,
61 minVoltageReading / scaleFactor, conn, readState),
Ed Tanous2049bd22022-07-09 07:20:26 -070062 objServer(objectServer), inputDev(io), waitTimer(io), path(path),
63 scaleFactor(scaleFactor),
Jeff Lind9cd7042020-11-20 15:49:28 +080064 sensorPollMs(static_cast<unsigned int>(pollRate * 1000)),
Zhikui Ren12c2c0e2021-04-28 17:21:21 -070065 bridgeGpio(std::move(bridgeGpio)), thresholdTimer(io)
James Feist6714a252018-09-10 15:26:18 -070066{
Ed Tanous99c44092022-01-14 09:59:09 -080067 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
68 int fd = open(path.c_str(), O_RDONLY);
69 if (fd < 0)
70 {
71 std::cerr << "unable to open acd device \n";
72 }
73
74 inputDev.assign(fd);
75
James Feist251c7822018-09-12 12:54:15 -070076 sensorInterface = objectServer.add_interface(
77 "/xyz/openbmc_project/sensors/voltage/" + name,
78 "xyz.openbmc_project.Sensor.Value");
Jayashree Dhanapal56678082022-01-04 17:27:20 +053079 for (const auto& threshold : thresholds)
James Feist6714a252018-09-10 15:26:18 -070080 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053081 std::string interface = thresholds::getInterface(threshold.level);
82 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
83 objectServer.add_interface(
84 "/xyz/openbmc_project/sensors/voltage/" + name, interface);
James Feist6714a252018-09-10 15:26:18 -070085 }
James Feist078f2322019-03-08 11:09:05 -080086 association = objectServer.add_interface(
James Feist2adc95c2019-09-30 14:55:28 -070087 "/xyz/openbmc_project/sensors/voltage/" + name, association::interface);
Andrei Kartashev39287412022-02-04 16:04:47 +030088 setInitialProperties(sensor_paths::unitVolts);
James Feist6714a252018-09-10 15:26:18 -070089}
90
91ADCSensor::~ADCSensor()
92{
93 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070094 inputDev.close();
95 waitTimer.cancel();
Zhikui Ren12c2c0e2021-04-28 17:21:21 -070096
Jayashree Dhanapal56678082022-01-04 17:27:20 +053097 for (const auto& iface : thresholdInterfaces)
98 {
99 objServer.remove_interface(iface);
100 }
James Feist251c7822018-09-12 12:54:15 -0700101 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800102 objServer.remove_interface(association);
James Feist6714a252018-09-10 15:26:18 -0700103}
104
Ed Tanous201a1012024-04-03 18:07:28 -0700105void ADCSensor::setupRead()
James Feist6714a252018-09-10 15:26:18 -0700106{
Yong Li1afda6b2020-04-09 19:24:13 +0800107 std::shared_ptr<boost::asio::streambuf> buffer =
108 std::make_shared<boost::asio::streambuf>();
109
110 std::weak_ptr<ADCSensor> weakRef = weak_from_this();
111
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400112 if (bridgeGpio.has_value())
113 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700114 (*bridgeGpio).set(1);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400115 // In case a channel has a bridge circuit,we have to turn the bridge on
116 // prior to reading a value at least for one scan cycle to get a valid
117 // value. Guarantee that the HW signal can be stable, the HW signal
118 // could be instability.
Ed Tanous83db50c2023-03-01 10:20:24 -0800119 waitTimer.expires_after(
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700120 std::chrono::milliseconds(bridgeGpio->setupTimeMs));
Yong Li1afda6b2020-04-09 19:24:13 +0800121 waitTimer.async_wait(
122 [weakRef, buffer](const boost::system::error_code& ec) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400123 std::shared_ptr<ADCSensor> self = weakRef.lock();
124 if (ec == boost::asio::error::operation_aborted)
125 {
126 return; // we're being canceled
127 }
Yong Li1afda6b2020-04-09 19:24:13 +0800128
Patrick Williams2aaf7172024-08-16 15:20:40 -0400129 if (self)
130 {
131 boost::asio::async_read_until(
132 self->inputDev, *buffer, '\n',
133 [weakRef, buffer](const boost::system::error_code& ec,
134 std::size_t /*bytes_transfered*/) {
135 std::shared_ptr<ADCSensor> self = weakRef.lock();
136 if (self)
137 {
138 self->readBuf = buffer;
139 self->handleResponse(ec);
140 }
141 });
142 }
143 });
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400144 }
145 else
146 {
147 boost::asio::async_read_until(
Yong Li1afda6b2020-04-09 19:24:13 +0800148 inputDev, *buffer, '\n',
149 [weakRef, buffer](const boost::system::error_code& ec,
150 std::size_t /*bytes_transfered*/) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400151 std::shared_ptr<ADCSensor> self = weakRef.lock();
152 if (self)
153 {
154 self->readBuf = buffer;
155 self->handleResponse(ec);
156 }
157 });
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400158 }
James Feist6714a252018-09-10 15:26:18 -0700159}
160
James Feistd8705872019-02-08 13:26:09 -0800161void ADCSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700162{
Yong Li1afda6b2020-04-09 19:24:13 +0800163 std::weak_ptr<ADCSensor> weakRef = weak_from_this();
164
James Feist6714a252018-09-10 15:26:18 -0700165 if (err == boost::system::errc::bad_file_descriptor)
166 {
167 return; // we're being destroyed
168 }
Yong Li1afda6b2020-04-09 19:24:13 +0800169 std::istream responseStream(readBuf.get());
James Feist6714a252018-09-10 15:26:18 -0700170
171 if (!err)
172 {
173 std::string response;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700174 std::getline(responseStream, response);
James Feist6714a252018-09-10 15:26:18 -0700175
176 // todo read scaling factors from configuration
177 try
178 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700179 rawValue = std::stod(response);
180 double nvalue = (rawValue / sensorScaleFactor) / scaleFactor;
James Feistcc6d4fe2018-11-14 16:38:26 -0800181 nvalue = std::round(nvalue * roundFactor) / roundFactor;
Josh Lehan833661a2020-03-04 17:35:41 -0800182 updateValue(nvalue);
James Feist6714a252018-09-10 15:26:18 -0700183 }
Patrick Williams26601e82021-10-06 12:43:25 -0500184 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700185 {
James Feist961bf092020-07-01 16:38:12 -0700186 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700187 }
188 }
189 else
190 {
James Feist961bf092020-07-01 16:38:12 -0700191 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700192 }
193
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700194 responseStream.clear();
195 inputDev.close();
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400196 if (bridgeGpio.has_value())
197 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700198 (*bridgeGpio).set(0);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400199 }
Ed Tanous99c44092022-01-14 09:59:09 -0800200
201 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
James Feist6714a252018-09-10 15:26:18 -0700202 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700203 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700204 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700205 std::cerr << "adcsensor " << name << " failed to open " << path << "\n";
James Feist6714a252018-09-10 15:26:18 -0700206 return; // we're no longer valid
207 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700208 inputDev.assign(fd);
Ed Tanous83db50c2023-03-01 10:20:24 -0800209 waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs));
Yong Li1afda6b2020-04-09 19:24:13 +0800210 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
211 std::shared_ptr<ADCSensor> self = weakRef.lock();
James Feist6714a252018-09-10 15:26:18 -0700212 if (ec == boost::asio::error::operation_aborted)
213 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700214 if (self)
215 {
216 std::cerr << "adcsensor " << self->name << " read cancelled\n";
217 }
218 else
219 {
220 std::cerr << "adcsensor read cancelled no self\n";
221 }
James Feist6714a252018-09-10 15:26:18 -0700222 return; // we're being canceled
223 }
Yong Li1afda6b2020-04-09 19:24:13 +0800224
225 if (self)
226 {
227 self->setupRead();
228 }
Zhikui Rend3da1282020-09-11 17:02:01 -0700229 else
230 {
231 std::cerr << "adcsensor weakref no self\n";
232 }
James Feist6714a252018-09-10 15:26:18 -0700233 });
234}
235
Ed Tanous201a1012024-04-03 18:07:28 -0700236void ADCSensor::checkThresholds()
James Feist6714a252018-09-10 15:26:18 -0700237{
James Feist961bf092020-07-01 16:38:12 -0700238 if (!readingStateGood())
James Feist71d31b22019-01-02 16:57:54 -0800239 {
240 return;
241 }
James Feist46342ec2019-03-06 14:03:41 -0800242
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700243 thresholds::checkThresholdsPowerDelay(weak_from_this(), thresholdTimer);
James Feist6714a252018-09-10 15:26:18 -0700244}