blob: 1db036e4ce9ad23b4da201ebfab256c5b9dae71a [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>
23#include <iostream>
24#include <limits>
25#include <sdbusplus/asio/connection.hpp>
26#include <sdbusplus/asio/object_server.hpp>
27#include <string>
28
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070029static constexpr unsigned int sensorPollMs = 500;
30static constexpr size_t warnAfterErrorCount = 10;
James Feist6714a252018-09-10 15:26:18 -070031
32// scaling factor from hwmon
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070033static constexpr unsigned int sensorScaleFactor = 1000;
James Feist6714a252018-09-10 15:26:18 -070034
James Feistcc6d4fe2018-11-14 16:38:26 -080035static constexpr double roundFactor = 10000; // 3 decimal places
James Feistce3fca42018-11-21 12:58:24 -080036static constexpr double maxReading = 20;
37static constexpr double minReading = 0;
James Feistcc6d4fe2018-11-14 16:38:26 -080038
James Feistd8705872019-02-08 13:26:09 -080039ADCSensor::ADCSensor(const std::string& path,
40 sdbusplus::asio::object_server& objectServer,
41 std::shared_ptr<sdbusplus::asio::connection>& conn,
42 boost::asio::io_service& io, const std::string& sensorName,
43 std::vector<thresholds::Threshold>&& _thresholds,
James Feist71d31b22019-01-02 16:57:54 -080044 const double scaleFactor, PowerState readState,
James Feistd8705872019-02-08 13:26:09 -080045 const std::string& sensorConfiguration) :
James Feistdc6c55f2018-10-31 12:53:20 -070046 Sensor(boost::replace_all_copy(sensorName, " ", "_"), path,
James Feistce3fca42018-11-21 12:58:24 -080047 std::move(_thresholds), sensorConfiguration,
48 "xyz.openbmc_project.Configuration.ADC", maxReading, minReading),
49 objServer(objectServer), scaleFactor(scaleFactor),
James Feist71d31b22019-01-02 16:57:54 -080050 readState(std::move(readState)), inputDev(io, open(path.c_str(), O_RDONLY)),
James Feist46342ec2019-03-06 14:03:41 -080051 waitTimer(io), errCount(0), thresholdTimer(io, this)
James Feist6714a252018-09-10 15:26:18 -070052{
James Feist251c7822018-09-12 12:54:15 -070053 sensorInterface = objectServer.add_interface(
54 "/xyz/openbmc_project/sensors/voltage/" + name,
55 "xyz.openbmc_project.Sensor.Value");
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070056 if (thresholds::hasWarningInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070057 {
James Feist251c7822018-09-12 12:54:15 -070058 thresholdInterfaceWarning = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070059 "/xyz/openbmc_project/sensors/voltage/" + name,
60 "xyz.openbmc_project.Sensor.Threshold.Warning");
61 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070062 if (thresholds::hasCriticalInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070063 {
James Feist251c7822018-09-12 12:54:15 -070064 thresholdInterfaceCritical = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070065 "/xyz/openbmc_project/sensors/voltage/" + name,
66 "xyz.openbmc_project.Sensor.Threshold.Critical");
67 }
James Feist078f2322019-03-08 11:09:05 -080068 association = objectServer.add_interface(
69 "/xyz/openbmc_project/sensors/voltage/" + name,
70 "org.openbmc.Associations");
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070071 setInitialProperties(conn);
72 setupRead();
James Feist71d31b22019-01-02 16:57:54 -080073
74 // setup match
75 setupPowerMatch(conn);
James Feist6714a252018-09-10 15:26:18 -070076}
77
78ADCSensor::~ADCSensor()
79{
80 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070081 inputDev.close();
82 waitTimer.cancel();
James Feist251c7822018-09-12 12:54:15 -070083 objServer.remove_interface(thresholdInterfaceWarning);
84 objServer.remove_interface(thresholdInterfaceCritical);
85 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -080086 objServer.remove_interface(association);
James Feist6714a252018-09-10 15:26:18 -070087}
88
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070089void ADCSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -070090{
91 boost::asio::async_read_until(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070092 inputDev, readBuf, '\n',
James Feistd8705872019-02-08 13:26:09 -080093 [&](const boost::system::error_code& ec,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070094 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist6714a252018-09-10 15:26:18 -070095}
96
James Feistd8705872019-02-08 13:26:09 -080097void ADCSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -070098{
99 if (err == boost::system::errc::bad_file_descriptor)
100 {
101 return; // we're being destroyed
102 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700103 std::istream responseStream(&readBuf);
James Feist6714a252018-09-10 15:26:18 -0700104
105 if (!err)
106 {
107 std::string response;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700108 std::getline(responseStream, response);
James Feist6714a252018-09-10 15:26:18 -0700109
110 // todo read scaling factors from configuration
111 try
112 {
James Feistcc6d4fe2018-11-14 16:38:26 -0800113 double nvalue = std::stof(response);
114
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700115 nvalue = (nvalue / sensorScaleFactor) / scaleFactor;
James Feistcc6d4fe2018-11-14 16:38:26 -0800116 nvalue = std::round(nvalue * roundFactor) / roundFactor;
117
Richard Marian Thomaiyarb2eb29a2018-12-08 18:26:58 +0530118 if (overridenState)
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530119 {
120 nvalue = overriddenValue;
121 }
James Feistcc6d4fe2018-11-14 16:38:26 -0800122
James Feist6714a252018-09-10 15:26:18 -0700123 if (nvalue != value)
124 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700125 updateValue(nvalue);
James Feist6714a252018-09-10 15:26:18 -0700126 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700127 errCount = 0;
James Feist6714a252018-09-10 15:26:18 -0700128 }
129 catch (std::invalid_argument)
130 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700131 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700132 }
133 }
134 else
135 {
James Feist6714a252018-09-10 15:26:18 -0700136
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700137 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700138 }
James Feistdc6c55f2018-10-31 12:53:20 -0700139 // only print once
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700140 if (errCount == warnAfterErrorCount)
James Feist6714a252018-09-10 15:26:18 -0700141 {
James Feistdc6c55f2018-10-31 12:53:20 -0700142 std::cerr << "Failure to read sensor " << name << " at " << path
143 << " ec:" << err << "\n";
144 }
145
146 if (errCount >= warnAfterErrorCount)
147 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700148 updateValue(0);
James Feist6714a252018-09-10 15:26:18 -0700149 }
150
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700151 responseStream.clear();
152 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700153 int fd = open(path.c_str(), O_RDONLY);
154 if (fd <= 0)
155 {
156 return; // we're no longer valid
157 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700158 inputDev.assign(fd);
159 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
James Feistd8705872019-02-08 13:26:09 -0800160 waitTimer.async_wait([&](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700161 if (ec == boost::asio::error::operation_aborted)
162 {
163 return; // we're being canceled
164 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700165 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700166 });
167}
168
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700169void ADCSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700170{
James Feist71d31b22019-01-02 16:57:54 -0800171 if (readState == PowerState::on && !isPowerOn())
172 {
173 return;
174 }
James Feist46342ec2019-03-06 14:03:41 -0800175
176 thresholds::checkThresholdsPowerDelay(this, thresholdTimer);
James Feist6714a252018-09-10 15:26:18 -0700177}