blob: a025419af866be9465e313b449e4332ddd4baf6f [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
James Feistd8705872019-02-08 13:26:09 -080051ADCSensor::ADCSensor(const std::string& path,
52 sdbusplus::asio::object_server& objectServer,
53 std::shared_ptr<sdbusplus::asio::connection>& conn,
Ed Tanous1f978632023-02-28 18:16:39 -080054 boost::asio::io_context& io, const std::string& sensorName,
Jeff Lin7b7a9de2021-02-22 11:16:27 +080055 std::vector<thresholds::Threshold>&& thresholdsIn,
Jeff Lind9cd7042020-11-20 15:49:28 +080056 const double scaleFactor, const float pollRate,
57 PowerState readState,
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040058 const std::string& sensorConfiguration,
Jae Hyun Yoo1cbd1c62019-07-29 15:02:43 -070059 std::optional<BridgeGpio>&& bridgeGpio) :
Zhikui Renda98f092021-11-01 09:41:08 -070060 Sensor(escapeName(sensorName), std::move(thresholdsIn), sensorConfiguration,
Zev Weiss054aad82022-08-18 01:37:34 -070061 "ADC", false, false, maxVoltageReading / scaleFactor,
62 minVoltageReading / scaleFactor, conn, readState),
Ed Tanous2049bd22022-07-09 07:20:26 -070063 objServer(objectServer), inputDev(io), waitTimer(io), path(path),
64 scaleFactor(scaleFactor),
Jeff Lind9cd7042020-11-20 15:49:28 +080065 sensorPollMs(static_cast<unsigned int>(pollRate * 1000)),
Zhikui Ren12c2c0e2021-04-28 17:21:21 -070066 bridgeGpio(std::move(bridgeGpio)), thresholdTimer(io)
James Feist6714a252018-09-10 15:26:18 -070067{
Ed Tanous99c44092022-01-14 09:59:09 -080068 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
69 int fd = open(path.c_str(), O_RDONLY);
70 if (fd < 0)
71 {
72 std::cerr << "unable to open acd device \n";
73 }
74
75 inputDev.assign(fd);
76
James Feist251c7822018-09-12 12:54:15 -070077 sensorInterface = objectServer.add_interface(
78 "/xyz/openbmc_project/sensors/voltage/" + name,
79 "xyz.openbmc_project.Sensor.Value");
Jayashree Dhanapal56678082022-01-04 17:27:20 +053080 for (const auto& threshold : thresholds)
James Feist6714a252018-09-10 15:26:18 -070081 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053082 std::string interface = thresholds::getInterface(threshold.level);
83 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
84 objectServer.add_interface(
85 "/xyz/openbmc_project/sensors/voltage/" + name, interface);
James Feist6714a252018-09-10 15:26:18 -070086 }
James Feist078f2322019-03-08 11:09:05 -080087 association = objectServer.add_interface(
James Feist2adc95c2019-09-30 14:55:28 -070088 "/xyz/openbmc_project/sensors/voltage/" + name, association::interface);
Andrei Kartashev39287412022-02-04 16:04:47 +030089 setInitialProperties(sensor_paths::unitVolts);
James Feist6714a252018-09-10 15:26:18 -070090}
91
92ADCSensor::~ADCSensor()
93{
94 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070095 inputDev.close();
96 waitTimer.cancel();
Zhikui Ren12c2c0e2021-04-28 17:21:21 -070097
Jayashree Dhanapal56678082022-01-04 17:27:20 +053098 for (const auto& iface : thresholdInterfaces)
99 {
100 objServer.remove_interface(iface);
101 }
James Feist251c7822018-09-12 12:54:15 -0700102 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800103 objServer.remove_interface(association);
James Feist6714a252018-09-10 15:26:18 -0700104}
105
Ed Tanous201a1012024-04-03 18:07:28 -0700106void ADCSensor::setupRead()
James Feist6714a252018-09-10 15:26:18 -0700107{
Yong Li1afda6b2020-04-09 19:24:13 +0800108 std::shared_ptr<boost::asio::streambuf> buffer =
109 std::make_shared<boost::asio::streambuf>();
110
111 std::weak_ptr<ADCSensor> weakRef = weak_from_this();
112
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400113 if (bridgeGpio.has_value())
114 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700115 (*bridgeGpio).set(1);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400116 // In case a channel has a bridge circuit,we have to turn the bridge on
117 // prior to reading a value at least for one scan cycle to get a valid
118 // value. Guarantee that the HW signal can be stable, the HW signal
119 // could be instability.
Ed Tanous83db50c2023-03-01 10:20:24 -0800120 waitTimer.expires_after(
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700121 std::chrono::milliseconds(bridgeGpio->setupTimeMs));
Yong Li1afda6b2020-04-09 19:24:13 +0800122 waitTimer.async_wait(
123 [weakRef, buffer](const boost::system::error_code& ec) {
Ed Tanousbb679322022-05-16 16:10:00 -0700124 std::shared_ptr<ADCSensor> self = weakRef.lock();
125 if (ec == boost::asio::error::operation_aborted)
126 {
127 return; // we're being canceled
128 }
Yong Li1afda6b2020-04-09 19:24:13 +0800129
Ed Tanousbb679322022-05-16 16:10:00 -0700130 if (self)
131 {
132 boost::asio::async_read_until(
133 self->inputDev, *buffer, '\n',
134 [weakRef, buffer](const boost::system::error_code& ec,
135 std::size_t /*bytes_transfered*/) {
136 std::shared_ptr<ADCSensor> self = weakRef.lock();
137 if (self)
138 {
139 self->readBuf = buffer;
140 self->handleResponse(ec);
141 }
Patrick Williams597e8422023-10-20 11:19:01 -0500142 });
Ed Tanousbb679322022-05-16 16:10:00 -0700143 }
144 });
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400145 }
146 else
147 {
148 boost::asio::async_read_until(
Yong Li1afda6b2020-04-09 19:24:13 +0800149 inputDev, *buffer, '\n',
150 [weakRef, buffer](const boost::system::error_code& ec,
151 std::size_t /*bytes_transfered*/) {
Ed Tanousbb679322022-05-16 16:10:00 -0700152 std::shared_ptr<ADCSensor> self = weakRef.lock();
153 if (self)
154 {
155 self->readBuf = buffer;
156 self->handleResponse(ec);
157 }
Patrick Williams597e8422023-10-20 11:19:01 -0500158 });
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400159 }
James Feist6714a252018-09-10 15:26:18 -0700160}
161
James Feistd8705872019-02-08 13:26:09 -0800162void ADCSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700163{
Yong Li1afda6b2020-04-09 19:24:13 +0800164 std::weak_ptr<ADCSensor> weakRef = weak_from_this();
165
James Feist6714a252018-09-10 15:26:18 -0700166 if (err == boost::system::errc::bad_file_descriptor)
167 {
168 return; // we're being destroyed
169 }
Yong Li1afda6b2020-04-09 19:24:13 +0800170 std::istream responseStream(readBuf.get());
James Feist6714a252018-09-10 15:26:18 -0700171
172 if (!err)
173 {
174 std::string response;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700175 std::getline(responseStream, response);
James Feist6714a252018-09-10 15:26:18 -0700176
177 // todo read scaling factors from configuration
178 try
179 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700180 rawValue = std::stod(response);
181 double nvalue = (rawValue / sensorScaleFactor) / scaleFactor;
James Feistcc6d4fe2018-11-14 16:38:26 -0800182 nvalue = std::round(nvalue * roundFactor) / roundFactor;
Josh Lehan833661a2020-03-04 17:35:41 -0800183 updateValue(nvalue);
James Feist6714a252018-09-10 15:26:18 -0700184 }
Patrick Williams26601e82021-10-06 12:43:25 -0500185 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700186 {
James Feist961bf092020-07-01 16:38:12 -0700187 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700188 }
189 }
190 else
191 {
James Feist961bf092020-07-01 16:38:12 -0700192 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700193 }
194
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700195 responseStream.clear();
196 inputDev.close();
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400197 if (bridgeGpio.has_value())
198 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700199 (*bridgeGpio).set(0);
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400200 }
Ed Tanous99c44092022-01-14 09:59:09 -0800201
202 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
James Feist6714a252018-09-10 15:26:18 -0700203 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700204 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700205 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700206 std::cerr << "adcsensor " << name << " failed to open " << path << "\n";
James Feist6714a252018-09-10 15:26:18 -0700207 return; // we're no longer valid
208 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700209 inputDev.assign(fd);
Ed Tanous83db50c2023-03-01 10:20:24 -0800210 waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs));
Yong Li1afda6b2020-04-09 19:24:13 +0800211 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
212 std::shared_ptr<ADCSensor> self = weakRef.lock();
James Feist6714a252018-09-10 15:26:18 -0700213 if (ec == boost::asio::error::operation_aborted)
214 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700215 if (self)
216 {
217 std::cerr << "adcsensor " << self->name << " read cancelled\n";
218 }
219 else
220 {
221 std::cerr << "adcsensor read cancelled no self\n";
222 }
James Feist6714a252018-09-10 15:26:18 -0700223 return; // we're being canceled
224 }
Yong Li1afda6b2020-04-09 19:24:13 +0800225
226 if (self)
227 {
228 self->setupRead();
229 }
Zhikui Rend3da1282020-09-11 17:02:01 -0700230 else
231 {
232 std::cerr << "adcsensor weakref no self\n";
233 }
James Feist6714a252018-09-10 15:26:18 -0700234 });
235}
236
Ed Tanous201a1012024-04-03 18:07:28 -0700237void ADCSensor::checkThresholds()
James Feist6714a252018-09-10 15:26:18 -0700238{
James Feist961bf092020-07-01 16:38:12 -0700239 if (!readingStateGood())
James Feist71d31b22019-01-02 16:57:54 -0800240 {
241 return;
242 }
James Feist46342ec2019-03-06 14:03:41 -0800243
Zhikui Ren12c2c0e2021-04-28 17:21:21 -0700244 thresholds::checkThresholdsPowerDelay(weak_from_this(), thresholdTimer);
James Feist6714a252018-09-10 15:26:18 -0700245}