blob: 71ff4ed08aabdf3ee38af0b332a29dc5dbe6c40c [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
Patrick Ventureca44b2f2019-10-31 11:02:26 -070017#include "HwmonTempSensor.hpp"
18
James Feist6714a252018-09-10 15:26:18 -070019#include <unistd.h>
20
James Feist6714a252018-09-10 15:26:18 -070021#include <boost/algorithm/string/predicate.hpp>
22#include <boost/algorithm/string/replace.hpp>
23#include <boost/date_time/posix_time/posix_time.hpp>
James Feist38fb5982020-05-28 10:09:54 -070024#include <sdbusplus/asio/connection.hpp>
25#include <sdbusplus/asio/object_server.hpp>
26
James Feist6714a252018-09-10 15:26:18 -070027#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070028#include <istream>
James Feist6714a252018-09-10 15:26:18 -070029#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070030#include <memory>
James Feist6714a252018-09-10 15:26:18 -070031#include <string>
Patrick Venture96e97db2019-10-31 13:44:38 -070032#include <vector>
James Feist6714a252018-09-10 15:26:18 -070033
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070034static constexpr unsigned int sensorPollMs = 500;
35static constexpr unsigned int sensorScaleFactor = 1000;
36static constexpr size_t warnAfterErrorCount = 10;
James Feist6714a252018-09-10 15:26:18 -070037
James Feistce3fca42018-11-21 12:58:24 -080038static constexpr double maxReading = 127;
39static constexpr double minReading = -128;
40
James Feist6714a252018-09-10 15:26:18 -070041HwmonTempSensor::HwmonTempSensor(
James Feistd8705872019-02-08 13:26:09 -080042 const std::string& path, const std::string& objectType,
43 sdbusplus::asio::object_server& objectServer,
44 std::shared_ptr<sdbusplus::asio::connection>& conn,
45 boost::asio::io_service& io, const std::string& sensorName,
46 std::vector<thresholds::Threshold>&& _thresholds,
James Feistf9b01b62020-01-29 15:21:58 -080047 const std::string& sensorConfiguration, const PowerState powerState) :
James Feist930fcde2019-05-28 12:58:43 -070048 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
James Feistce3fca42018-11-21 12:58:24 -080049 std::move(_thresholds), sensorConfiguration, objectType, maxReading,
50 minReading),
Yong Lif3fd1912020-03-25 21:35:23 +080051 std::enable_shared_from_this<HwmonTempSensor>(), objServer(objectServer),
52 inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), path(path),
53 errCount(0), readState(powerState)
James Feist6714a252018-09-10 15:26:18 -070054{
James Feist251c7822018-09-12 12:54:15 -070055 sensorInterface = objectServer.add_interface(
56 "/xyz/openbmc_project/sensors/temperature/" + name,
57 "xyz.openbmc_project.Sensor.Value");
58
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070059 if (thresholds::hasWarningInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070060 {
James Feist251c7822018-09-12 12:54:15 -070061 thresholdInterfaceWarning = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070062 "/xyz/openbmc_project/sensors/temperature/" + name,
63 "xyz.openbmc_project.Sensor.Threshold.Warning");
64 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070065 if (thresholds::hasCriticalInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070066 {
James Feist251c7822018-09-12 12:54:15 -070067 thresholdInterfaceCritical = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070068 "/xyz/openbmc_project/sensors/temperature/" + name,
69 "xyz.openbmc_project.Sensor.Threshold.Critical");
70 }
James Feist078f2322019-03-08 11:09:05 -080071 association = objectServer.add_interface(
72 "/xyz/openbmc_project/sensors/temperature/" + name,
James Feist2adc95c2019-09-30 14:55:28 -070073 association::interface);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070074 setInitialProperties(conn);
James Feistf9b01b62020-01-29 15:21:58 -080075 setupPowerMatch(conn);
James Feist6714a252018-09-10 15:26:18 -070076}
77
78HwmonTempSensor::~HwmonTempSensor()
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 HwmonTempSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -070090{
Yong Lif3fd1912020-03-25 21:35:23 +080091 std::weak_ptr<HwmonTempSensor> weakRef = weak_from_this();
92
93 boost::asio::async_read_until(inputDev, readBuf, '\n',
94 [weakRef](const boost::system::error_code& ec,
95 std::size_t /*bytes_transfered*/) {
96 std::shared_ptr<HwmonTempSensor> self =
97 weakRef.lock();
98 if (self)
99 {
100 self->handleResponse(ec);
101 }
102 });
James Feist6714a252018-09-10 15:26:18 -0700103}
104
James Feistd8705872019-02-08 13:26:09 -0800105void HwmonTempSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700106{
Yong Lif3fd1912020-03-25 21:35:23 +0800107 if ((err == boost::system::errc::bad_file_descriptor) ||
108 (err == boost::asio::error::misc_errors::not_found))
James Feist6714a252018-09-10 15:26:18 -0700109 {
110 return; // we're being destroyed
111 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700112 std::istream responseStream(&readBuf);
James Feist6714a252018-09-10 15:26:18 -0700113 if (!err)
114 {
115 std::string response;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700116 std::getline(responseStream, response);
James Feist6714a252018-09-10 15:26:18 -0700117 try
118 {
Josh Lehan833661a2020-03-04 17:35:41 -0800119 double nvalue = std::stod(response);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700120 nvalue /= sensorScaleFactor;
Josh Lehan833661a2020-03-04 17:35:41 -0800121 updateValue(nvalue);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700122 errCount = 0;
James Feist6714a252018-09-10 15:26:18 -0700123 }
James Feistd8705872019-02-08 13:26:09 -0800124 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700125 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700126 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700127 }
128 }
James Feist3459d012020-01-30 13:17:43 -0800129 else if (readState == PowerState::on && !isPowerOn())
130 {
131 errCount = 0;
132 updateValue(std::numeric_limits<double>::quiet_NaN());
133 }
James Feist6714a252018-09-10 15:26:18 -0700134 else
135 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700136 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700137 }
James Feistdc6c55f2018-10-31 12:53:20 -0700138
139 // 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 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700150 responseStream.clear();
151 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700152 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700153 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700154 {
155 return; // we're no longer valid
156 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700157 inputDev.assign(fd);
158 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
Yong Lif3fd1912020-03-25 21:35:23 +0800159 std::weak_ptr<HwmonTempSensor> weakRef = weak_from_this();
160 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
161 std::shared_ptr<HwmonTempSensor> self = weakRef.lock();
James Feist6714a252018-09-10 15:26:18 -0700162 if (ec == boost::asio::error::operation_aborted)
163 {
164 return; // we're being canceled
165 }
Yong Lif3fd1912020-03-25 21:35:23 +0800166 if (self)
167 {
168 self->setupRead();
169 }
James Feist6714a252018-09-10 15:26:18 -0700170 });
171}
172
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700173void HwmonTempSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700174{
James Feistf9b01b62020-01-29 15:21:58 -0800175 if (readState == PowerState::on && !isPowerOn())
176 {
177 return;
178 }
James Feist251c7822018-09-12 12:54:15 -0700179 thresholds::checkThresholds(this);
Patrick Ventureca44b2f2019-10-31 11:02:26 -0700180}