blob: aaef76d212fd70cdbc7aeba5a63505a97ac41452 [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),
Yong Li1afda6b2020-04-09 19:24:13 +080056 std::enable_shared_from_this<ADCSensor>(), objServer(objectServer),
57 inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), path(path),
Jeff Lind9cd7042020-11-20 15:49:28 +080058 scaleFactor(scaleFactor),
59 sensorPollMs(static_cast<unsigned int>(pollRate * 1000)),
Zhikui Ren12c2c0e2021-04-28 17:21:21 -070060 bridgeGpio(std::move(bridgeGpio)), thresholdTimer(io)
James Feist6714a252018-09-10 15:26:18 -070061{
James Feist251c7822018-09-12 12:54:15 -070062 sensorInterface = objectServer.add_interface(
63 "/xyz/openbmc_project/sensors/voltage/" + name,
64 "xyz.openbmc_project.Sensor.Value");
Jayashree Dhanapal56678082022-01-04 17:27:20 +053065 for (const auto& threshold : thresholds)
James Feist6714a252018-09-10 15:26:18 -070066 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053067 std::string interface = thresholds::getInterface(threshold.level);
68 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
69 objectServer.add_interface(
70 "/xyz/openbmc_project/sensors/voltage/" + name, interface);
James Feist6714a252018-09-10 15:26:18 -070071 }
James Feist078f2322019-03-08 11:09:05 -080072 association = objectServer.add_interface(
James Feist2adc95c2019-09-30 14:55:28 -070073 "/xyz/openbmc_project/sensors/voltage/" + name, association::interface);
Zev Weiss6b6891c2021-04-22 02:46:21 -050074 setInitialProperties(conn, sensor_paths::unitVolts);
James Feist6714a252018-09-10 15:26:18 -070075}
76
77ADCSensor::~ADCSensor()
78{
79 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070080 inputDev.close();
81 waitTimer.cancel();
Zhikui Ren12c2c0e2021-04-28 17:21:21 -070082
Jayashree Dhanapal56678082022-01-04 17:27:20 +053083 for (const auto& iface : thresholdInterfaces)
84 {
85 objServer.remove_interface(iface);
86 }
James Feist251c7822018-09-12 12:54:15 -070087 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -080088 objServer.remove_interface(association);
James Feist6714a252018-09-10 15:26:18 -070089}
90
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070091void ADCSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -070092{
Yong Li1afda6b2020-04-09 19:24:13 +080093 std::shared_ptr<boost::asio::streambuf> buffer =
94 std::make_shared<boost::asio::streambuf>();
95
96 std::weak_ptr<ADCSensor> weakRef = weak_from_this();
97
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040098 if (bridgeGpio.has_value())
99 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700100 (*bridgeGpio).set(1);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400101 // In case a channel has a bridge circuit,we have to turn the bridge on
102 // prior to reading a value at least for one scan cycle to get a valid
103 // value. Guarantee that the HW signal can be stable, the HW signal
104 // could be instability.
105 waitTimer.expires_from_now(
Zev Weissf72eb832021-06-25 05:55:08 +0000106 boost::posix_time::milliseconds(bridgeGpio->setupTimeMs));
Yong Li1afda6b2020-04-09 19:24:13 +0800107 waitTimer.async_wait(
108 [weakRef, buffer](const boost::system::error_code& ec) {
109 std::shared_ptr<ADCSensor> self = weakRef.lock();
110 if (ec == boost::asio::error::operation_aborted)
111 {
112 return; // we're being canceled
113 }
114
115 if (self)
116 {
117 boost::asio::async_read_until(
118 self->inputDev, *buffer, '\n',
119 [weakRef, buffer](const boost::system::error_code& ec,
120 std::size_t /*bytes_transfered*/) {
121 std::shared_ptr<ADCSensor> self = weakRef.lock();
122 if (self)
123 {
124 self->readBuf = buffer;
125 self->handleResponse(ec);
126 }
127 });
128 }
129 });
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400130 }
131 else
132 {
133 boost::asio::async_read_until(
Yong Li1afda6b2020-04-09 19:24:13 +0800134 inputDev, *buffer, '\n',
135 [weakRef, buffer](const boost::system::error_code& ec,
136 std::size_t /*bytes_transfered*/) {
137 std::shared_ptr<ADCSensor> self = weakRef.lock();
138 if (self)
139 {
140 self->readBuf = buffer;
141 self->handleResponse(ec);
142 }
143 });
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400144 }
James Feist6714a252018-09-10 15:26:18 -0700145}
146
James Feistd8705872019-02-08 13:26:09 -0800147void ADCSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700148{
Yong Li1afda6b2020-04-09 19:24:13 +0800149 std::weak_ptr<ADCSensor> weakRef = weak_from_this();
150
James Feist6714a252018-09-10 15:26:18 -0700151 if (err == boost::system::errc::bad_file_descriptor)
152 {
153 return; // we're being destroyed
154 }
Yong Li1afda6b2020-04-09 19:24:13 +0800155 std::istream responseStream(readBuf.get());
James Feist6714a252018-09-10 15:26:18 -0700156
157 if (!err)
158 {
159 std::string response;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700160 std::getline(responseStream, response);
James Feist6714a252018-09-10 15:26:18 -0700161
162 // todo read scaling factors from configuration
163 try
164 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700165 rawValue = std::stod(response);
166 double nvalue = (rawValue / sensorScaleFactor) / scaleFactor;
James Feistcc6d4fe2018-11-14 16:38:26 -0800167 nvalue = std::round(nvalue * roundFactor) / roundFactor;
Josh Lehan833661a2020-03-04 17:35:41 -0800168 updateValue(nvalue);
James Feist6714a252018-09-10 15:26:18 -0700169 }
Patrick Williams26601e82021-10-06 12:43:25 -0500170 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700171 {
James Feist961bf092020-07-01 16:38:12 -0700172 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700173 }
174 }
175 else
176 {
James Feist961bf092020-07-01 16:38:12 -0700177 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700178 }
179
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700180 responseStream.clear();
181 inputDev.close();
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400182 if (bridgeGpio.has_value())
183 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700184 (*bridgeGpio).set(0);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400185 }
James Feist6714a252018-09-10 15:26:18 -0700186 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700187 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700188 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700189 std::cerr << "adcsensor " << name << " failed to open " << path << "\n";
James Feist6714a252018-09-10 15:26:18 -0700190 return; // we're no longer valid
191 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700192 inputDev.assign(fd);
193 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
Yong Li1afda6b2020-04-09 19:24:13 +0800194 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
195 std::shared_ptr<ADCSensor> self = weakRef.lock();
James Feist6714a252018-09-10 15:26:18 -0700196 if (ec == boost::asio::error::operation_aborted)
197 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700198 if (self)
199 {
200 std::cerr << "adcsensor " << self->name << " read cancelled\n";
201 }
202 else
203 {
204 std::cerr << "adcsensor read cancelled no self\n";
205 }
James Feist6714a252018-09-10 15:26:18 -0700206 return; // we're being canceled
207 }
Yong Li1afda6b2020-04-09 19:24:13 +0800208
209 if (self)
210 {
211 self->setupRead();
212 }
Zhikui Rend3da1282020-09-11 17:02:01 -0700213 else
214 {
215 std::cerr << "adcsensor weakref no self\n";
216 }
James Feist6714a252018-09-10 15:26:18 -0700217 });
218}
219
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700220void ADCSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700221{
James Feist961bf092020-07-01 16:38:12 -0700222 if (!readingStateGood())
James Feist71d31b22019-01-02 16:57:54 -0800223 {
224 return;
225 }
James Feist46342ec2019-03-06 14:03:41 -0800226
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700227 thresholds::checkThresholdsPowerDelay(weak_from_this(), thresholdTimer);
James Feist6714a252018-09-10 15:26:18 -0700228}