blob: c94f2515e6b6047835b80dfd89dcdff265ec88cd [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
19#include <ADCSensor.hpp>
20#include <boost/algorithm/string/predicate.hpp>
21#include <boost/algorithm/string/replace.hpp>
22#include <boost/date_time/posix_time/posix_time.hpp>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040023#include <filesystem>
24#include <fstream>
James Feist6714a252018-09-10 15:26:18 -070025#include <iostream>
26#include <limits>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040027#include <optional>
James Feist6714a252018-09-10 15:26:18 -070028#include <sdbusplus/asio/connection.hpp>
29#include <sdbusplus/asio/object_server.hpp>
30#include <string>
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070031static constexpr unsigned int sensorPollMs = 500;
32static constexpr size_t warnAfterErrorCount = 10;
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040033static constexpr unsigned int gpioBridgeEnableMs = 20;
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
James Feistcc6d4fe2018-11-14 16:38:26 -080037static constexpr double roundFactor = 10000; // 3 decimal places
James Feistce3fca42018-11-21 12:58:24 -080038static constexpr double maxReading = 20;
39static constexpr double minReading = 0;
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040040static constexpr const char* sysGpioPath = "/sys/class/gpio/gpio";
41static constexpr const char* postfixValue = "/value";
42
43void setGpio(int gpioN, int value)
44{
45 std::string device = sysGpioPath + std::to_string(gpioN) + postfixValue;
46 std::fstream gpioFile;
47
48 gpioFile.open(device, std::ios::out);
49
50 if (!gpioFile.good())
51 {
52 std::cerr << "Error opening device " << device << "\n";
53 return;
54 }
55 gpioFile << std::to_string(value);
56 gpioFile.close();
57}
James Feistcc6d4fe2018-11-14 16:38:26 -080058
James Feistd8705872019-02-08 13:26:09 -080059ADCSensor::ADCSensor(const std::string& path,
60 sdbusplus::asio::object_server& objectServer,
61 std::shared_ptr<sdbusplus::asio::connection>& conn,
62 boost::asio::io_service& io, const std::string& sensorName,
63 std::vector<thresholds::Threshold>&& _thresholds,
James Feist71d31b22019-01-02 16:57:54 -080064 const double scaleFactor, PowerState readState,
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040065 const std::string& sensorConfiguration,
66 std::optional<int> bridgeGpio) :
James Feist930fcde2019-05-28 12:58:43 -070067 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
James Feistce3fca42018-11-21 12:58:24 -080068 std::move(_thresholds), sensorConfiguration,
69 "xyz.openbmc_project.Configuration.ADC", maxReading, minReading),
James Feist930fcde2019-05-28 12:58:43 -070070 objServer(objectServer), scaleFactor(scaleFactor), path(path),
James Feist71d31b22019-01-02 16:57:54 -080071 readState(std::move(readState)), inputDev(io, open(path.c_str(), O_RDONLY)),
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040072 waitTimer(io), errCount(0), thresholdTimer(io, this), bridgeGpio(bridgeGpio)
James Feist6714a252018-09-10 15:26:18 -070073{
James Feist251c7822018-09-12 12:54:15 -070074 sensorInterface = objectServer.add_interface(
75 "/xyz/openbmc_project/sensors/voltage/" + name,
76 "xyz.openbmc_project.Sensor.Value");
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070077 if (thresholds::hasWarningInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070078 {
James Feist251c7822018-09-12 12:54:15 -070079 thresholdInterfaceWarning = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070080 "/xyz/openbmc_project/sensors/voltage/" + name,
81 "xyz.openbmc_project.Sensor.Threshold.Warning");
82 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070083 if (thresholds::hasCriticalInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070084 {
James Feist251c7822018-09-12 12:54:15 -070085 thresholdInterfaceCritical = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070086 "/xyz/openbmc_project/sensors/voltage/" + name,
87 "xyz.openbmc_project.Sensor.Threshold.Critical");
88 }
James Feist078f2322019-03-08 11:09:05 -080089 association = objectServer.add_interface(
90 "/xyz/openbmc_project/sensors/voltage/" + name,
91 "org.openbmc.Associations");
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070092 setInitialProperties(conn);
93 setupRead();
James Feist71d31b22019-01-02 16:57:54 -080094
95 // setup match
96 setupPowerMatch(conn);
James Feist6714a252018-09-10 15:26:18 -070097}
98
99ADCSensor::~ADCSensor()
100{
101 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700102 inputDev.close();
103 waitTimer.cancel();
James Feist251c7822018-09-12 12:54:15 -0700104 objServer.remove_interface(thresholdInterfaceWarning);
105 objServer.remove_interface(thresholdInterfaceCritical);
106 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800107 objServer.remove_interface(association);
James Feist6714a252018-09-10 15:26:18 -0700108}
109
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700110void ADCSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -0700111{
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400112 if (bridgeGpio.has_value())
113 {
114 setGpio(*bridgeGpio, 1);
115 // In case a channel has a bridge circuit,we have to turn the bridge on
116 // prior to reading a value at least for one scan cycle to get a valid
117 // value. Guarantee that the HW signal can be stable, the HW signal
118 // could be instability.
119 waitTimer.expires_from_now(
120 boost::posix_time::milliseconds(gpioBridgeEnableMs));
121 waitTimer.async_wait([&](const boost::system::error_code& ec) {
122 if (ec == boost::asio::error::operation_aborted)
123 {
124 return; // we're being canceled
125 }
126 boost::asio::async_read_until(
127 inputDev, readBuf, '\n',
128 [&](const boost::system::error_code& ec,
129 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
130 });
131 }
132 else
133 {
134 boost::asio::async_read_until(
135 inputDev, readBuf, '\n',
136 [&](const boost::system::error_code& ec,
137 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
138 }
James Feist6714a252018-09-10 15:26:18 -0700139}
140
James Feistd8705872019-02-08 13:26:09 -0800141void ADCSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700142{
143 if (err == boost::system::errc::bad_file_descriptor)
144 {
145 return; // we're being destroyed
146 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700147 std::istream responseStream(&readBuf);
James Feist6714a252018-09-10 15:26:18 -0700148
149 if (!err)
150 {
151 std::string response;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700152 std::getline(responseStream, response);
James Feist6714a252018-09-10 15:26:18 -0700153
154 // todo read scaling factors from configuration
155 try
156 {
James Feistcc6d4fe2018-11-14 16:38:26 -0800157 double nvalue = std::stof(response);
158
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700159 nvalue = (nvalue / sensorScaleFactor) / scaleFactor;
James Feistcc6d4fe2018-11-14 16:38:26 -0800160 nvalue = std::round(nvalue * roundFactor) / roundFactor;
161
James Feist6714a252018-09-10 15:26:18 -0700162 if (nvalue != value)
163 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700164 updateValue(nvalue);
James Feist6714a252018-09-10 15:26:18 -0700165 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700166 errCount = 0;
James Feist6714a252018-09-10 15:26:18 -0700167 }
168 catch (std::invalid_argument)
169 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700170 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700171 }
172 }
173 else
174 {
James Feist6714a252018-09-10 15:26:18 -0700175
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700176 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700177 }
James Feistdc6c55f2018-10-31 12:53:20 -0700178 // only print once
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700179 if (errCount == warnAfterErrorCount)
James Feist6714a252018-09-10 15:26:18 -0700180 {
James Feistdc6c55f2018-10-31 12:53:20 -0700181 std::cerr << "Failure to read sensor " << name << " at " << path
182 << " ec:" << err << "\n";
183 }
184
185 if (errCount >= warnAfterErrorCount)
186 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700187 updateValue(0);
James Feist6714a252018-09-10 15:26:18 -0700188 }
189
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700190 responseStream.clear();
191 inputDev.close();
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400192 if (bridgeGpio.has_value())
193 {
194 setGpio(*bridgeGpio, 0);
195 }
James Feist6714a252018-09-10 15:26:18 -0700196 int fd = open(path.c_str(), O_RDONLY);
197 if (fd <= 0)
198 {
199 return; // we're no longer valid
200 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700201 inputDev.assign(fd);
202 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
James Feistd8705872019-02-08 13:26:09 -0800203 waitTimer.async_wait([&](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700204 if (ec == boost::asio::error::operation_aborted)
205 {
206 return; // we're being canceled
207 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700208 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700209 });
210}
211
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700212void ADCSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700213{
James Feist71d31b22019-01-02 16:57:54 -0800214 if (readState == PowerState::on && !isPowerOn())
215 {
216 return;
217 }
James Feist46342ec2019-03-06 14:03:41 -0800218
219 thresholds::checkThresholdsPowerDelay(this, thresholdTimer);
James Feist6714a252018-09-10 15:26:18 -0700220}