blob: 9e2d845e7843b89b67e9774fd08cae6bc8c03bf6 [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 <HwmonTempSensor.hpp>
James Feist6714a252018-09-10 15:26:18 -070020#include <boost/algorithm/string/predicate.hpp>
21#include <boost/algorithm/string/replace.hpp>
James Feist8086aba2020-08-25 16:00:59 -070022#include <boost/asio/read_until.hpp>
James Feist6714a252018-09-10 15:26:18 -070023#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
Bruce Mitchelle5fc3a52021-08-10 11:50:25 -050034// Temperatures are read in milli degrees Celsius, we need degrees Celsius.
35// Pressures are read in kilopascal, we need Pascals. On D-Bus for Open BMC
36// we use the International System of Units without prefixes.
37// Links to the kernel documentation:
38// https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface
39// https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-bus-iio
40// For IIO RAW sensors we get a raw_value, an offset, and scale to compute
41// the value = (raw_value + offset) * scale
42static constexpr double sensorOffset = 0.0;
43static constexpr double sensorScale = 0.001;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070044static constexpr size_t warnAfterErrorCount = 10;
James Feist6714a252018-09-10 15:26:18 -070045
James Feistce3fca42018-11-21 12:58:24 -080046static constexpr double maxReading = 127;
47static constexpr double minReading = -128;
48
James Feist6714a252018-09-10 15:26:18 -070049HwmonTempSensor::HwmonTempSensor(
James Feistd8705872019-02-08 13:26:09 -080050 const std::string& path, const std::string& objectType,
51 sdbusplus::asio::object_server& objectServer,
52 std::shared_ptr<sdbusplus::asio::connection>& conn,
53 boost::asio::io_service& io, const std::string& sensorName,
Jeff Lin7b7a9de2021-02-22 11:16:27 +080054 std::vector<thresholds::Threshold>&& thresholdsIn, const float pollRate,
James Feistf9b01b62020-01-29 15:21:58 -080055 const std::string& sensorConfiguration, const PowerState powerState) :
Jeff Lin7b7a9de2021-02-22 11:16:27 +080056 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
Bruce Lee1263c3d2021-06-04 15:16:33 +080057 std::move(thresholdsIn), sensorConfiguration, objectType, false,
58 maxReading, minReading, conn, powerState),
Yong Lif3fd1912020-03-25 21:35:23 +080059 std::enable_shared_from_this<HwmonTempSensor>(), objServer(objectServer),
Jeff Lin87bc67f2020-12-04 20:58:01 +080060 inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), path(path),
Ed Tanous8a57ec02020-10-09 12:46:52 -070061 sensorPollMs(static_cast<unsigned int>(pollRate * 1000))
James Feist6714a252018-09-10 15:26:18 -070062{
James Feist251c7822018-09-12 12:54:15 -070063 sensorInterface = objectServer.add_interface(
64 "/xyz/openbmc_project/sensors/temperature/" + name,
65 "xyz.openbmc_project.Sensor.Value");
66
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070067 if (thresholds::hasWarningInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070068 {
James Feist251c7822018-09-12 12:54:15 -070069 thresholdInterfaceWarning = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070070 "/xyz/openbmc_project/sensors/temperature/" + name,
71 "xyz.openbmc_project.Sensor.Threshold.Warning");
72 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070073 if (thresholds::hasCriticalInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070074 {
James Feist251c7822018-09-12 12:54:15 -070075 thresholdInterfaceCritical = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070076 "/xyz/openbmc_project/sensors/temperature/" + name,
77 "xyz.openbmc_project.Sensor.Threshold.Critical");
78 }
James Feist078f2322019-03-08 11:09:05 -080079 association = objectServer.add_interface(
80 "/xyz/openbmc_project/sensors/temperature/" + name,
James Feist2adc95c2019-09-30 14:55:28 -070081 association::interface);
Zev Weiss6b6891c2021-04-22 02:46:21 -050082 setInitialProperties(conn, sensor_paths::unitDegreesC);
James Feist6714a252018-09-10 15:26:18 -070083}
84
85HwmonTempSensor::~HwmonTempSensor()
86{
87 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070088 inputDev.close();
89 waitTimer.cancel();
James Feist251c7822018-09-12 12:54:15 -070090 objServer.remove_interface(thresholdInterfaceWarning);
91 objServer.remove_interface(thresholdInterfaceCritical);
92 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -080093 objServer.remove_interface(association);
James Feist6714a252018-09-10 15:26:18 -070094}
95
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070096void HwmonTempSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -070097{
Konstantin Aladysheva7345942021-04-28 17:09:02 +030098 if (!readingStateGood())
99 {
100 markAvailable(false);
101 updateValue(std::numeric_limits<double>::quiet_NaN());
102 restartRead();
103 return;
104 }
Yong Lif3fd1912020-03-25 21:35:23 +0800105
Konstantin Aladysheva7345942021-04-28 17:09:02 +0300106 std::weak_ptr<HwmonTempSensor> weakRef = weak_from_this();
Yong Lif3fd1912020-03-25 21:35:23 +0800107 boost::asio::async_read_until(inputDev, readBuf, '\n',
108 [weakRef](const boost::system::error_code& ec,
109 std::size_t /*bytes_transfered*/) {
110 std::shared_ptr<HwmonTempSensor> self =
111 weakRef.lock();
112 if (self)
113 {
114 self->handleResponse(ec);
115 }
116 });
James Feist6714a252018-09-10 15:26:18 -0700117}
118
Konstantin Aladysheva7345942021-04-28 17:09:02 +0300119void HwmonTempSensor::restartRead()
120{
121 std::weak_ptr<HwmonTempSensor> weakRef = weak_from_this();
122 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
123 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
124 if (ec == boost::asio::error::operation_aborted)
125 {
126 return; // we're being canceled
127 }
128 std::shared_ptr<HwmonTempSensor> self = weakRef.lock();
129 if (!self)
130 {
131 return;
132 }
133 self->setupRead();
134 });
135}
136
James Feistd8705872019-02-08 13:26:09 -0800137void HwmonTempSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700138{
Yong Lif3fd1912020-03-25 21:35:23 +0800139 if ((err == boost::system::errc::bad_file_descriptor) ||
140 (err == boost::asio::error::misc_errors::not_found))
James Feist6714a252018-09-10 15:26:18 -0700141 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700142 std::cerr << "Hwmon temp sensor " << name << " removed " << path
143 << "\n";
James Feist6714a252018-09-10 15:26:18 -0700144 return; // we're being destroyed
145 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700146 std::istream responseStream(&readBuf);
James Feist6714a252018-09-10 15:26:18 -0700147 if (!err)
148 {
149 std::string response;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700150 std::getline(responseStream, response);
James Feist6714a252018-09-10 15:26:18 -0700151 try
152 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700153 rawValue = std::stod(response);
Bruce Mitchelle5fc3a52021-08-10 11:50:25 -0500154 double nvalue = (rawValue + sensorOffset) * sensorScale;
Josh Lehan833661a2020-03-04 17:35:41 -0800155 updateValue(nvalue);
James Feist6714a252018-09-10 15:26:18 -0700156 }
James Feistd8705872019-02-08 13:26:09 -0800157 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700158 {
James Feist961bf092020-07-01 16:38:12 -0700159 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700160 }
161 }
162 else
163 {
James Feist961bf092020-07-01 16:38:12 -0700164 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700165 }
James Feistdc6c55f2018-10-31 12:53:20 -0700166
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700167 responseStream.clear();
168 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700169 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700170 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700171 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700172 std::cerr << "Hwmon temp sensor " << name << " not valid " << path
173 << "\n";
James Feist6714a252018-09-10 15:26:18 -0700174 return; // we're no longer valid
175 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700176 inputDev.assign(fd);
Konstantin Aladysheva7345942021-04-28 17:09:02 +0300177 restartRead();
James Feist6714a252018-09-10 15:26:18 -0700178}
179
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700180void HwmonTempSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700181{
James Feist251c7822018-09-12 12:54:15 -0700182 thresholds::checkThresholds(this);
Patrick Ventureca44b2f2019-10-31 11:02:26 -0700183}