blob: d6dcdca528a5fc4ec371dc78e291bda4d220874c [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
James Feist6714a252018-09-10 15:26:18 -070019#include <unistd.h>
20
James Feist8086aba2020-08-25 16:00:59 -070021#include <boost/asio/read_until.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,
Ed Tanous1f978632023-02-28 18:16:39 -080045 boost::asio::io_context& 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,
Zev Weiss054aad82022-08-18 01:37:34 -070052 "ADC", false, false, maxVoltageReading / scaleFactor,
53 minVoltageReading / scaleFactor, conn, readState),
Ed Tanous2049bd22022-07-09 07:20:26 -070054 objServer(objectServer), inputDev(io), waitTimer(io), path(path),
55 scaleFactor(scaleFactor),
Jeff Lind9cd7042020-11-20 15:49:28 +080056 sensorPollMs(static_cast<unsigned int>(pollRate * 1000)),
Zhikui Ren12c2c0e2021-04-28 17:21:21 -070057 bridgeGpio(std::move(bridgeGpio)), thresholdTimer(io)
James Feist6714a252018-09-10 15:26:18 -070058{
Ed Tanous99c44092022-01-14 09:59:09 -080059 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
60 int fd = open(path.c_str(), O_RDONLY);
61 if (fd < 0)
62 {
63 std::cerr << "unable to open acd device \n";
64 }
65
66 inputDev.assign(fd);
67
James Feist251c7822018-09-12 12:54:15 -070068 sensorInterface = objectServer.add_interface(
69 "/xyz/openbmc_project/sensors/voltage/" + name,
70 "xyz.openbmc_project.Sensor.Value");
Jayashree Dhanapal56678082022-01-04 17:27:20 +053071 for (const auto& threshold : thresholds)
James Feist6714a252018-09-10 15:26:18 -070072 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053073 std::string interface = thresholds::getInterface(threshold.level);
74 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
75 objectServer.add_interface(
76 "/xyz/openbmc_project/sensors/voltage/" + name, interface);
James Feist6714a252018-09-10 15:26:18 -070077 }
James Feist078f2322019-03-08 11:09:05 -080078 association = objectServer.add_interface(
James Feist2adc95c2019-09-30 14:55:28 -070079 "/xyz/openbmc_project/sensors/voltage/" + name, association::interface);
Andrei Kartashev39287412022-02-04 16:04:47 +030080 setInitialProperties(sensor_paths::unitVolts);
James Feist6714a252018-09-10 15:26:18 -070081}
82
83ADCSensor::~ADCSensor()
84{
85 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070086 inputDev.close();
87 waitTimer.cancel();
Zhikui Ren12c2c0e2021-04-28 17:21:21 -070088
Jayashree Dhanapal56678082022-01-04 17:27:20 +053089 for (const auto& iface : thresholdInterfaces)
90 {
91 objServer.remove_interface(iface);
92 }
James Feist251c7822018-09-12 12:54:15 -070093 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -080094 objServer.remove_interface(association);
James Feist6714a252018-09-10 15:26:18 -070095}
96
Ed Tanous201a1012024-04-03 18:07:28 -070097void ADCSensor::setupRead()
James Feist6714a252018-09-10 15:26:18 -070098{
Yong Li1afda6b2020-04-09 19:24:13 +080099 std::shared_ptr<boost::asio::streambuf> buffer =
100 std::make_shared<boost::asio::streambuf>();
101
102 std::weak_ptr<ADCSensor> weakRef = weak_from_this();
103
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400104 if (bridgeGpio.has_value())
105 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700106 (*bridgeGpio).set(1);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400107 // In case a channel has a bridge circuit,we have to turn the bridge on
108 // prior to reading a value at least for one scan cycle to get a valid
109 // value. Guarantee that the HW signal can be stable, the HW signal
110 // could be instability.
Ed Tanous83db50c2023-03-01 10:20:24 -0800111 waitTimer.expires_after(
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700112 std::chrono::milliseconds(bridgeGpio->setupTimeMs));
Yong Li1afda6b2020-04-09 19:24:13 +0800113 waitTimer.async_wait(
114 [weakRef, buffer](const boost::system::error_code& ec) {
Ed Tanousbb679322022-05-16 16:10:00 -0700115 std::shared_ptr<ADCSensor> self = weakRef.lock();
116 if (ec == boost::asio::error::operation_aborted)
117 {
118 return; // we're being canceled
119 }
Yong Li1afda6b2020-04-09 19:24:13 +0800120
Ed Tanousbb679322022-05-16 16:10:00 -0700121 if (self)
122 {
123 boost::asio::async_read_until(
124 self->inputDev, *buffer, '\n',
125 [weakRef, buffer](const boost::system::error_code& ec,
126 std::size_t /*bytes_transfered*/) {
127 std::shared_ptr<ADCSensor> self = weakRef.lock();
128 if (self)
129 {
130 self->readBuf = buffer;
131 self->handleResponse(ec);
132 }
Patrick Williams597e8422023-10-20 11:19:01 -0500133 });
Ed Tanousbb679322022-05-16 16:10:00 -0700134 }
135 });
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400136 }
137 else
138 {
139 boost::asio::async_read_until(
Yong Li1afda6b2020-04-09 19:24:13 +0800140 inputDev, *buffer, '\n',
141 [weakRef, buffer](const boost::system::error_code& ec,
142 std::size_t /*bytes_transfered*/) {
Ed Tanousbb679322022-05-16 16:10:00 -0700143 std::shared_ptr<ADCSensor> self = weakRef.lock();
144 if (self)
145 {
146 self->readBuf = buffer;
147 self->handleResponse(ec);
148 }
Patrick Williams597e8422023-10-20 11:19:01 -0500149 });
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400150 }
James Feist6714a252018-09-10 15:26:18 -0700151}
152
James Feistd8705872019-02-08 13:26:09 -0800153void ADCSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700154{
Yong Li1afda6b2020-04-09 19:24:13 +0800155 std::weak_ptr<ADCSensor> weakRef = weak_from_this();
156
James Feist6714a252018-09-10 15:26:18 -0700157 if (err == boost::system::errc::bad_file_descriptor)
158 {
159 return; // we're being destroyed
160 }
Yong Li1afda6b2020-04-09 19:24:13 +0800161 std::istream responseStream(readBuf.get());
James Feist6714a252018-09-10 15:26:18 -0700162
163 if (!err)
164 {
165 std::string response;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700166 std::getline(responseStream, response);
James Feist6714a252018-09-10 15:26:18 -0700167
168 // todo read scaling factors from configuration
169 try
170 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700171 rawValue = std::stod(response);
172 double nvalue = (rawValue / sensorScaleFactor) / scaleFactor;
James Feistcc6d4fe2018-11-14 16:38:26 -0800173 nvalue = std::round(nvalue * roundFactor) / roundFactor;
Josh Lehan833661a2020-03-04 17:35:41 -0800174 updateValue(nvalue);
James Feist6714a252018-09-10 15:26:18 -0700175 }
Patrick Williams26601e82021-10-06 12:43:25 -0500176 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700177 {
James Feist961bf092020-07-01 16:38:12 -0700178 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700179 }
180 }
181 else
182 {
James Feist961bf092020-07-01 16:38:12 -0700183 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700184 }
185
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700186 responseStream.clear();
187 inputDev.close();
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400188 if (bridgeGpio.has_value())
189 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700190 (*bridgeGpio).set(0);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400191 }
Ed Tanous99c44092022-01-14 09:59:09 -0800192
193 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
James Feist6714a252018-09-10 15:26:18 -0700194 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700195 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700196 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700197 std::cerr << "adcsensor " << name << " failed to open " << path << "\n";
James Feist6714a252018-09-10 15:26:18 -0700198 return; // we're no longer valid
199 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700200 inputDev.assign(fd);
Ed Tanous83db50c2023-03-01 10:20:24 -0800201 waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs));
Yong Li1afda6b2020-04-09 19:24:13 +0800202 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
203 std::shared_ptr<ADCSensor> self = weakRef.lock();
James Feist6714a252018-09-10 15:26:18 -0700204 if (ec == boost::asio::error::operation_aborted)
205 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700206 if (self)
207 {
208 std::cerr << "adcsensor " << self->name << " read cancelled\n";
209 }
210 else
211 {
212 std::cerr << "adcsensor read cancelled no self\n";
213 }
James Feist6714a252018-09-10 15:26:18 -0700214 return; // we're being canceled
215 }
Yong Li1afda6b2020-04-09 19:24:13 +0800216
217 if (self)
218 {
219 self->setupRead();
220 }
Zhikui Rend3da1282020-09-11 17:02:01 -0700221 else
222 {
223 std::cerr << "adcsensor weakref no self\n";
224 }
James Feist6714a252018-09-10 15:26:18 -0700225 });
226}
227
Ed Tanous201a1012024-04-03 18:07:28 -0700228void ADCSensor::checkThresholds()
James Feist6714a252018-09-10 15:26:18 -0700229{
James Feist961bf092020-07-01 16:38:12 -0700230 if (!readingStateGood())
James Feist71d31b22019-01-02 16:57:54 -0800231 {
232 return;
233 }
James Feist46342ec2019-03-06 14:03:41 -0800234
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700235 thresholds::checkThresholdsPowerDelay(weak_from_this(), thresholdTimer);
James Feist6714a252018-09-10 15:26:18 -0700236}