blob: fe2c8a791b36d82db20c89a0919260b607429695 [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 Feist38fb5982020-05-28 10:09:54 -070021#include <sdbusplus/asio/connection.hpp>
22#include <sdbusplus/asio/object_server.hpp>
23
Patrick Venture96e97db2019-10-31 13:44:38 -070024#include <cmath>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040025#include <filesystem>
26#include <fstream>
James Feist6714a252018-09-10 15:26:18 -070027#include <iostream>
28#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070029#include <memory>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040030#include <optional>
James Feist6714a252018-09-10 15:26:18 -070031#include <string>
Patrick Venture96e97db2019-10-31 13:44:38 -070032#include <vector>
Patrick Venture2da370e2019-11-01 11:51:31 -070033
James Feist6714a252018-09-10 15:26:18 -070034// scaling factor from hwmon
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070035static constexpr unsigned int sensorScaleFactor = 1000;
James Feist6714a252018-09-10 15:26:18 -070036
Zhikui Ren937eb542020-10-12 13:42:08 -070037static constexpr double roundFactor = 10000; // 3 decimal places
38static constexpr double maxVoltageReading = 1.8; // pre sensor scaling
39static constexpr double minVoltageReading = 0;
James Feistcc6d4fe2018-11-14 16:38:26 -080040
James Feistd8705872019-02-08 13:26:09 -080041ADCSensor::ADCSensor(const std::string& path,
42 sdbusplus::asio::object_server& objectServer,
43 std::shared_ptr<sdbusplus::asio::connection>& conn,
44 boost::asio::io_service& io, const std::string& sensorName,
Jeff Lin7b7a9de2021-02-22 11:16:27 +080045 std::vector<thresholds::Threshold>&& thresholdsIn,
Jeff Lind9cd7042020-11-20 15:49:28 +080046 const double scaleFactor, const float pollRate,
47 PowerState readState,
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040048 const std::string& sensorConfiguration,
Jae Hyun Yoo1cbd1c62019-07-29 15:02:43 -070049 std::optional<BridgeGpio>&& bridgeGpio) :
Zhikui Renda98f092021-11-01 09:41:08 -070050 Sensor(escapeName(sensorName), std::move(thresholdsIn), sensorConfiguration,
Zev Weiss054aad82022-08-18 01:37:34 -070051 "ADC", false, false, maxVoltageReading / scaleFactor,
52 minVoltageReading / scaleFactor, conn, readState),
Ed Tanous2049bd22022-07-09 07:20:26 -070053 objServer(objectServer), inputDev(io), waitTimer(io), path(path),
54 scaleFactor(scaleFactor),
Jeff Lind9cd7042020-11-20 15:49:28 +080055 sensorPollMs(static_cast<unsigned int>(pollRate * 1000)),
Zhikui Ren12c2c0e2021-04-28 17:21:21 -070056 bridgeGpio(std::move(bridgeGpio)), thresholdTimer(io)
James Feist6714a252018-09-10 15:26:18 -070057{
Ed Tanous99c44092022-01-14 09:59:09 -080058 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
59 int fd = open(path.c_str(), O_RDONLY);
60 if (fd < 0)
61 {
62 std::cerr << "unable to open acd device \n";
63 }
64
65 inputDev.assign(fd);
66
James Feist251c7822018-09-12 12:54:15 -070067 sensorInterface = objectServer.add_interface(
68 "/xyz/openbmc_project/sensors/voltage/" + name,
69 "xyz.openbmc_project.Sensor.Value");
Jayashree Dhanapal56678082022-01-04 17:27:20 +053070 for (const auto& threshold : thresholds)
James Feist6714a252018-09-10 15:26:18 -070071 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053072 std::string interface = thresholds::getInterface(threshold.level);
73 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
74 objectServer.add_interface(
75 "/xyz/openbmc_project/sensors/voltage/" + name, interface);
James Feist6714a252018-09-10 15:26:18 -070076 }
James Feist078f2322019-03-08 11:09:05 -080077 association = objectServer.add_interface(
James Feist2adc95c2019-09-30 14:55:28 -070078 "/xyz/openbmc_project/sensors/voltage/" + name, association::interface);
Andrei Kartashev39287412022-02-04 16:04:47 +030079 setInitialProperties(sensor_paths::unitVolts);
James Feist6714a252018-09-10 15:26:18 -070080}
81
82ADCSensor::~ADCSensor()
83{
84 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070085 inputDev.close();
86 waitTimer.cancel();
Zhikui Ren12c2c0e2021-04-28 17:21:21 -070087
Jayashree Dhanapal56678082022-01-04 17:27:20 +053088 for (const auto& iface : thresholdInterfaces)
89 {
90 objServer.remove_interface(iface);
91 }
James Feist251c7822018-09-12 12:54:15 -070092 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -080093 objServer.remove_interface(association);
James Feist6714a252018-09-10 15:26:18 -070094}
95
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070096void ADCSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -070097{
Yong Li1afda6b2020-04-09 19:24:13 +080098 std::shared_ptr<boost::asio::streambuf> buffer =
99 std::make_shared<boost::asio::streambuf>();
100
101 std::weak_ptr<ADCSensor> weakRef = weak_from_this();
102
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400103 if (bridgeGpio.has_value())
104 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700105 (*bridgeGpio).set(1);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400106 // In case a channel has a bridge circuit,we have to turn the bridge on
107 // prior to reading a value at least for one scan cycle to get a valid
108 // value. Guarantee that the HW signal can be stable, the HW signal
109 // could be instability.
110 waitTimer.expires_from_now(
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700111 std::chrono::milliseconds(bridgeGpio->setupTimeMs));
Yong Li1afda6b2020-04-09 19:24:13 +0800112 waitTimer.async_wait(
113 [weakRef, buffer](const boost::system::error_code& ec) {
Ed Tanousbb679322022-05-16 16:10:00 -0700114 std::shared_ptr<ADCSensor> self = weakRef.lock();
115 if (ec == boost::asio::error::operation_aborted)
116 {
117 return; // we're being canceled
118 }
Yong Li1afda6b2020-04-09 19:24:13 +0800119
Ed Tanousbb679322022-05-16 16:10:00 -0700120 if (self)
121 {
122 boost::asio::async_read_until(
123 self->inputDev, *buffer, '\n',
124 [weakRef, buffer](const boost::system::error_code& ec,
125 std::size_t /*bytes_transfered*/) {
126 std::shared_ptr<ADCSensor> self = weakRef.lock();
127 if (self)
128 {
129 self->readBuf = buffer;
130 self->handleResponse(ec);
131 }
132 });
133 }
134 });
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400135 }
136 else
137 {
138 boost::asio::async_read_until(
Yong Li1afda6b2020-04-09 19:24:13 +0800139 inputDev, *buffer, '\n',
140 [weakRef, buffer](const boost::system::error_code& ec,
141 std::size_t /*bytes_transfered*/) {
Ed Tanousbb679322022-05-16 16:10:00 -0700142 std::shared_ptr<ADCSensor> self = weakRef.lock();
143 if (self)
144 {
145 self->readBuf = buffer;
146 self->handleResponse(ec);
147 }
Yong Li1afda6b2020-04-09 19:24:13 +0800148 });
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400149 }
James Feist6714a252018-09-10 15:26:18 -0700150}
151
James Feistd8705872019-02-08 13:26:09 -0800152void ADCSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700153{
Yong Li1afda6b2020-04-09 19:24:13 +0800154 std::weak_ptr<ADCSensor> weakRef = weak_from_this();
155
James Feist6714a252018-09-10 15:26:18 -0700156 if (err == boost::system::errc::bad_file_descriptor)
157 {
158 return; // we're being destroyed
159 }
Yong Li1afda6b2020-04-09 19:24:13 +0800160 std::istream responseStream(readBuf.get());
James Feist6714a252018-09-10 15:26:18 -0700161
162 if (!err)
163 {
164 std::string response;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700165 std::getline(responseStream, response);
James Feist6714a252018-09-10 15:26:18 -0700166
167 // todo read scaling factors from configuration
168 try
169 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700170 rawValue = std::stod(response);
171 double nvalue = (rawValue / sensorScaleFactor) / scaleFactor;
James Feistcc6d4fe2018-11-14 16:38:26 -0800172 nvalue = std::round(nvalue * roundFactor) / roundFactor;
Josh Lehan833661a2020-03-04 17:35:41 -0800173 updateValue(nvalue);
James Feist6714a252018-09-10 15:26:18 -0700174 }
Patrick Williams26601e82021-10-06 12:43:25 -0500175 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700176 {
James Feist961bf092020-07-01 16:38:12 -0700177 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700178 }
179 }
180 else
181 {
James Feist961bf092020-07-01 16:38:12 -0700182 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700183 }
184
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700185 responseStream.clear();
186 inputDev.close();
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400187 if (bridgeGpio.has_value())
188 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700189 (*bridgeGpio).set(0);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400190 }
Ed Tanous99c44092022-01-14 09:59:09 -0800191
192 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
James Feist6714a252018-09-10 15:26:18 -0700193 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700194 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700195 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700196 std::cerr << "adcsensor " << name << " failed to open " << path << "\n";
James Feist6714a252018-09-10 15:26:18 -0700197 return; // we're no longer valid
198 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700199 inputDev.assign(fd);
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700200 waitTimer.expires_from_now(std::chrono::milliseconds(sensorPollMs));
Yong Li1afda6b2020-04-09 19:24:13 +0800201 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
202 std::shared_ptr<ADCSensor> self = weakRef.lock();
James Feist6714a252018-09-10 15:26:18 -0700203 if (ec == boost::asio::error::operation_aborted)
204 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700205 if (self)
206 {
207 std::cerr << "adcsensor " << self->name << " read cancelled\n";
208 }
209 else
210 {
211 std::cerr << "adcsensor read cancelled no self\n";
212 }
James Feist6714a252018-09-10 15:26:18 -0700213 return; // we're being canceled
214 }
Yong Li1afda6b2020-04-09 19:24:13 +0800215
216 if (self)
217 {
218 self->setupRead();
219 }
Zhikui Rend3da1282020-09-11 17:02:01 -0700220 else
221 {
222 std::cerr << "adcsensor weakref no self\n";
223 }
James Feist6714a252018-09-10 15:26:18 -0700224 });
225}
226
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700227void ADCSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700228{
James Feist961bf092020-07-01 16:38:12 -0700229 if (!readingStateGood())
James Feist71d31b22019-01-02 16:57:54 -0800230 {
231 return;
232 }
James Feist46342ec2019-03-06 14:03:41 -0800233
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700234 thresholds::checkThresholdsPowerDelay(weak_from_this(), thresholdTimer);
James Feist6714a252018-09-10 15:26:18 -0700235}