blob: 2a429999c2f03fec3d3bd21f86565a8690bc75a6 [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 Feist6714a252018-09-10 15:26:18 -070020#include <boost/algorithm/string/predicate.hpp>
James Feist8086aba2020-08-25 16:00:59 -070021#include <boost/asio/read_until.hpp>
James Feist6714a252018-09-10 15:26:18 -070022#include <boost/date_time/posix_time/posix_time.hpp>
James Feist38fb5982020-05-28 10:09:54 -070023#include <sdbusplus/asio/connection.hpp>
24#include <sdbusplus/asio/object_server.hpp>
25
Patrick Venture96e97db2019-10-31 13:44:38 -070026#include <cmath>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040027#include <filesystem>
28#include <fstream>
James Feist6714a252018-09-10 15:26:18 -070029#include <iostream>
30#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070031#include <memory>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040032#include <optional>
James Feist6714a252018-09-10 15:26:18 -070033#include <string>
Patrick Venture96e97db2019-10-31 13:44:38 -070034#include <vector>
Patrick Venture2da370e2019-11-01 11:51:31 -070035
James Feist6714a252018-09-10 15:26:18 -070036// scaling factor from hwmon
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070037static constexpr unsigned int sensorScaleFactor = 1000;
James Feist6714a252018-09-10 15:26:18 -070038
Zhikui Ren937eb542020-10-12 13:42:08 -070039static constexpr double roundFactor = 10000; // 3 decimal places
40static constexpr double maxVoltageReading = 1.8; // pre sensor scaling
41static constexpr double minVoltageReading = 0;
James Feistcc6d4fe2018-11-14 16:38:26 -080042
James Feistd8705872019-02-08 13:26:09 -080043ADCSensor::ADCSensor(const std::string& path,
44 sdbusplus::asio::object_server& objectServer,
45 std::shared_ptr<sdbusplus::asio::connection>& conn,
46 boost::asio::io_service& io, const std::string& sensorName,
Jeff Lin7b7a9de2021-02-22 11:16:27 +080047 std::vector<thresholds::Threshold>&& thresholdsIn,
Jeff Lind9cd7042020-11-20 15:49:28 +080048 const double scaleFactor, const float pollRate,
49 PowerState readState,
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040050 const std::string& sensorConfiguration,
Jae Hyun Yoo1cbd1c62019-07-29 15:02:43 -070051 std::optional<BridgeGpio>&& bridgeGpio) :
Zhikui Renda98f092021-11-01 09:41:08 -070052 Sensor(escapeName(sensorName), std::move(thresholdsIn), sensorConfiguration,
Jie Yang3291b9c2021-07-29 14:46:51 -070053 "xyz.openbmc_project.Configuration.ADC", false, false,
Zhikui Ren937eb542020-10-12 13:42:08 -070054 maxVoltageReading / scaleFactor, minVoltageReading / scaleFactor,
James Feiste3338522020-09-15 15:40:30 -070055 conn, readState),
Ed Tanous2049bd22022-07-09 07:20:26 -070056 objServer(objectServer), inputDev(io), waitTimer(io), path(path),
57 scaleFactor(scaleFactor),
Jeff Lind9cd7042020-11-20 15:49:28 +080058 sensorPollMs(static_cast<unsigned int>(pollRate * 1000)),
Zhikui Ren12c2c0e2021-04-28 17:21:21 -070059 bridgeGpio(std::move(bridgeGpio)), thresholdTimer(io)
James Feist6714a252018-09-10 15:26:18 -070060{
Ed Tanous99c44092022-01-14 09:59:09 -080061 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
62 int fd = open(path.c_str(), O_RDONLY);
63 if (fd < 0)
64 {
65 std::cerr << "unable to open acd device \n";
66 }
67
68 inputDev.assign(fd);
69
James Feist251c7822018-09-12 12:54:15 -070070 sensorInterface = objectServer.add_interface(
71 "/xyz/openbmc_project/sensors/voltage/" + name,
72 "xyz.openbmc_project.Sensor.Value");
Jayashree Dhanapal56678082022-01-04 17:27:20 +053073 for (const auto& threshold : thresholds)
James Feist6714a252018-09-10 15:26:18 -070074 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053075 std::string interface = thresholds::getInterface(threshold.level);
76 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
77 objectServer.add_interface(
78 "/xyz/openbmc_project/sensors/voltage/" + name, interface);
James Feist6714a252018-09-10 15:26:18 -070079 }
James Feist078f2322019-03-08 11:09:05 -080080 association = objectServer.add_interface(
James Feist2adc95c2019-09-30 14:55:28 -070081 "/xyz/openbmc_project/sensors/voltage/" + name, association::interface);
Andrei Kartashev39287412022-02-04 16:04:47 +030082 setInitialProperties(sensor_paths::unitVolts);
James Feist6714a252018-09-10 15:26:18 -070083}
84
85ADCSensor::~ADCSensor()
86{
87 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070088 inputDev.close();
89 waitTimer.cancel();
Zhikui Ren12c2c0e2021-04-28 17:21:21 -070090
Jayashree Dhanapal56678082022-01-04 17:27:20 +053091 for (const auto& iface : thresholdInterfaces)
92 {
93 objServer.remove_interface(iface);
94 }
James Feist251c7822018-09-12 12:54:15 -070095 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -080096 objServer.remove_interface(association);
James Feist6714a252018-09-10 15:26:18 -070097}
98
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070099void ADCSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -0700100{
Yong Li1afda6b2020-04-09 19:24:13 +0800101 std::shared_ptr<boost::asio::streambuf> buffer =
102 std::make_shared<boost::asio::streambuf>();
103
104 std::weak_ptr<ADCSensor> weakRef = weak_from_this();
105
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400106 if (bridgeGpio.has_value())
107 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700108 (*bridgeGpio).set(1);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400109 // In case a channel has a bridge circuit,we have to turn the bridge on
110 // prior to reading a value at least for one scan cycle to get a valid
111 // value. Guarantee that the HW signal can be stable, the HW signal
112 // could be instability.
113 waitTimer.expires_from_now(
Zev Weissf72eb832021-06-25 05:55:08 +0000114 boost::posix_time::milliseconds(bridgeGpio->setupTimeMs));
Yong Li1afda6b2020-04-09 19:24:13 +0800115 waitTimer.async_wait(
116 [weakRef, buffer](const boost::system::error_code& ec) {
Ed Tanousbb679322022-05-16 16:10:00 -0700117 std::shared_ptr<ADCSensor> self = weakRef.lock();
118 if (ec == boost::asio::error::operation_aborted)
119 {
120 return; // we're being canceled
121 }
Yong Li1afda6b2020-04-09 19:24:13 +0800122
Ed Tanousbb679322022-05-16 16:10:00 -0700123 if (self)
124 {
125 boost::asio::async_read_until(
126 self->inputDev, *buffer, '\n',
127 [weakRef, buffer](const boost::system::error_code& ec,
128 std::size_t /*bytes_transfered*/) {
129 std::shared_ptr<ADCSensor> self = weakRef.lock();
130 if (self)
131 {
132 self->readBuf = buffer;
133 self->handleResponse(ec);
134 }
135 });
136 }
137 });
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400138 }
139 else
140 {
141 boost::asio::async_read_until(
Yong Li1afda6b2020-04-09 19:24:13 +0800142 inputDev, *buffer, '\n',
143 [weakRef, buffer](const boost::system::error_code& ec,
144 std::size_t /*bytes_transfered*/) {
Ed Tanousbb679322022-05-16 16:10:00 -0700145 std::shared_ptr<ADCSensor> self = weakRef.lock();
146 if (self)
147 {
148 self->readBuf = buffer;
149 self->handleResponse(ec);
150 }
Yong Li1afda6b2020-04-09 19:24:13 +0800151 });
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400152 }
James Feist6714a252018-09-10 15:26:18 -0700153}
154
James Feistd8705872019-02-08 13:26:09 -0800155void ADCSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700156{
Yong Li1afda6b2020-04-09 19:24:13 +0800157 std::weak_ptr<ADCSensor> weakRef = weak_from_this();
158
James Feist6714a252018-09-10 15:26:18 -0700159 if (err == boost::system::errc::bad_file_descriptor)
160 {
161 return; // we're being destroyed
162 }
Yong Li1afda6b2020-04-09 19:24:13 +0800163 std::istream responseStream(readBuf.get());
James Feist6714a252018-09-10 15:26:18 -0700164
165 if (!err)
166 {
167 std::string response;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700168 std::getline(responseStream, response);
James Feist6714a252018-09-10 15:26:18 -0700169
170 // todo read scaling factors from configuration
171 try
172 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700173 rawValue = std::stod(response);
174 double nvalue = (rawValue / sensorScaleFactor) / scaleFactor;
James Feistcc6d4fe2018-11-14 16:38:26 -0800175 nvalue = std::round(nvalue * roundFactor) / roundFactor;
Josh Lehan833661a2020-03-04 17:35:41 -0800176 updateValue(nvalue);
James Feist6714a252018-09-10 15:26:18 -0700177 }
Patrick Williams26601e82021-10-06 12:43:25 -0500178 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700179 {
James Feist961bf092020-07-01 16:38:12 -0700180 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700181 }
182 }
183 else
184 {
James Feist961bf092020-07-01 16:38:12 -0700185 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700186 }
187
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700188 responseStream.clear();
189 inputDev.close();
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400190 if (bridgeGpio.has_value())
191 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700192 (*bridgeGpio).set(0);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400193 }
Ed Tanous99c44092022-01-14 09:59:09 -0800194
195 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
James Feist6714a252018-09-10 15:26:18 -0700196 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700197 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700198 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700199 std::cerr << "adcsensor " << name << " failed to open " << path << "\n";
James Feist6714a252018-09-10 15:26:18 -0700200 return; // we're no longer valid
201 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700202 inputDev.assign(fd);
203 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
Yong Li1afda6b2020-04-09 19:24:13 +0800204 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
205 std::shared_ptr<ADCSensor> self = weakRef.lock();
James Feist6714a252018-09-10 15:26:18 -0700206 if (ec == boost::asio::error::operation_aborted)
207 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700208 if (self)
209 {
210 std::cerr << "adcsensor " << self->name << " read cancelled\n";
211 }
212 else
213 {
214 std::cerr << "adcsensor read cancelled no self\n";
215 }
James Feist6714a252018-09-10 15:26:18 -0700216 return; // we're being canceled
217 }
Yong Li1afda6b2020-04-09 19:24:13 +0800218
219 if (self)
220 {
221 self->setupRead();
222 }
Zhikui Rend3da1282020-09-11 17:02:01 -0700223 else
224 {
225 std::cerr << "adcsensor weakref no self\n";
226 }
James Feist6714a252018-09-10 15:26:18 -0700227 });
228}
229
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700230void ADCSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700231{
James Feist961bf092020-07-01 16:38:12 -0700232 if (!readingStateGood())
James Feist71d31b22019-01-02 16:57:54 -0800233 {
234 return;
235 }
James Feist46342ec2019-03-06 14:03:41 -0800236
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700237 thresholds::checkThresholdsPowerDelay(weak_from_this(), thresholdTimer);
James Feist6714a252018-09-10 15:26:18 -0700238}