blob: 549fff71a3a781cfdbaaa07aa33b70c8c105043a [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>
James Feist8086aba2020-08-25 16:00:59 -070021#include <boost/asio/read_until.hpp>
James Feist6714a252018-09-10 15:26:18 -070022#include <boost/date_time/posix_time/posix_time.hpp>
James Feist38fb5982020-05-28 10:09:54 -070023#include <sdbusplus/asio/connection.hpp>
24#include <sdbusplus/asio/object_server.hpp>
25
James Feist6714a252018-09-10 15:26:18 -070026#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070027#include <istream>
James Feist6714a252018-09-10 15:26:18 -070028#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070029#include <memory>
James Feist6714a252018-09-10 15:26:18 -070030#include <string>
Patrick Venture96e97db2019-10-31 13:44:38 -070031#include <vector>
James Feist6714a252018-09-10 15:26:18 -070032
Bruce Mitchelle5fc3a52021-08-10 11:50:25 -050033// Temperatures are read in milli degrees Celsius, we need degrees Celsius.
34// Pressures are read in kilopascal, we need Pascals. On D-Bus for Open BMC
35// we use the International System of Units without prefixes.
36// Links to the kernel documentation:
37// https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface
38// https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-bus-iio
39// For IIO RAW sensors we get a raw_value, an offset, and scale to compute
40// the value = (raw_value + offset) * scale
41static constexpr double sensorOffset = 0.0;
42static constexpr double sensorScale = 0.001;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070043static constexpr size_t warnAfterErrorCount = 10;
James Feist6714a252018-09-10 15:26:18 -070044
James Feistce3fca42018-11-21 12:58:24 -080045static constexpr double maxReading = 127;
46static constexpr double minReading = -128;
47
James Feist6714a252018-09-10 15:26:18 -070048HwmonTempSensor::HwmonTempSensor(
James Feistd8705872019-02-08 13:26:09 -080049 const std::string& path, const std::string& objectType,
50 sdbusplus::asio::object_server& objectServer,
51 std::shared_ptr<sdbusplus::asio::connection>& conn,
52 boost::asio::io_service& io, const std::string& sensorName,
Jeff Lin7b7a9de2021-02-22 11:16:27 +080053 std::vector<thresholds::Threshold>&& thresholdsIn, const float pollRate,
James Feistf9b01b62020-01-29 15:21:58 -080054 const std::string& sensorConfiguration, const PowerState powerState) :
Zhikui Renda98f092021-11-01 09:41:08 -070055 Sensor(escapeName(sensorName), std::move(thresholdsIn), sensorConfiguration,
56 objectType, false, false, maxReading, minReading, conn, powerState),
Yong Lif3fd1912020-03-25 21:35:23 +080057 std::enable_shared_from_this<HwmonTempSensor>(), objServer(objectServer),
Jeff Lin87bc67f2020-12-04 20:58:01 +080058 inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), path(path),
Ed Tanous8a57ec02020-10-09 12:46:52 -070059 sensorPollMs(static_cast<unsigned int>(pollRate * 1000))
James Feist6714a252018-09-10 15:26:18 -070060{
James Feist251c7822018-09-12 12:54:15 -070061 sensorInterface = objectServer.add_interface(
62 "/xyz/openbmc_project/sensors/temperature/" + name,
63 "xyz.openbmc_project.Sensor.Value");
64
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070065 if (thresholds::hasWarningInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070066 {
James Feist251c7822018-09-12 12:54:15 -070067 thresholdInterfaceWarning = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070068 "/xyz/openbmc_project/sensors/temperature/" + name,
69 "xyz.openbmc_project.Sensor.Threshold.Warning");
70 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070071 if (thresholds::hasCriticalInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070072 {
James Feist251c7822018-09-12 12:54:15 -070073 thresholdInterfaceCritical = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070074 "/xyz/openbmc_project/sensors/temperature/" + name,
75 "xyz.openbmc_project.Sensor.Threshold.Critical");
76 }
James Feist078f2322019-03-08 11:09:05 -080077 association = objectServer.add_interface(
78 "/xyz/openbmc_project/sensors/temperature/" + name,
James Feist2adc95c2019-09-30 14:55:28 -070079 association::interface);
Zev Weiss6b6891c2021-04-22 02:46:21 -050080 setInitialProperties(conn, sensor_paths::unitDegreesC);
James Feist6714a252018-09-10 15:26:18 -070081}
82
83HwmonTempSensor::~HwmonTempSensor()
84{
85 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070086 inputDev.close();
87 waitTimer.cancel();
James Feist251c7822018-09-12 12:54:15 -070088 objServer.remove_interface(thresholdInterfaceWarning);
89 objServer.remove_interface(thresholdInterfaceCritical);
90 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -080091 objServer.remove_interface(association);
James Feist6714a252018-09-10 15:26:18 -070092}
93
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070094void HwmonTempSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -070095{
Konstantin Aladysheva7345942021-04-28 17:09:02 +030096 if (!readingStateGood())
97 {
98 markAvailable(false);
99 updateValue(std::numeric_limits<double>::quiet_NaN());
100 restartRead();
101 return;
102 }
Yong Lif3fd1912020-03-25 21:35:23 +0800103
Konstantin Aladysheva7345942021-04-28 17:09:02 +0300104 std::weak_ptr<HwmonTempSensor> weakRef = weak_from_this();
Yong Lif3fd1912020-03-25 21:35:23 +0800105 boost::asio::async_read_until(inputDev, readBuf, '\n',
106 [weakRef](const boost::system::error_code& ec,
107 std::size_t /*bytes_transfered*/) {
108 std::shared_ptr<HwmonTempSensor> self =
109 weakRef.lock();
110 if (self)
111 {
112 self->handleResponse(ec);
113 }
114 });
James Feist6714a252018-09-10 15:26:18 -0700115}
116
Konstantin Aladysheva7345942021-04-28 17:09:02 +0300117void HwmonTempSensor::restartRead()
118{
119 std::weak_ptr<HwmonTempSensor> weakRef = weak_from_this();
120 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
121 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
122 if (ec == boost::asio::error::operation_aborted)
123 {
124 return; // we're being canceled
125 }
126 std::shared_ptr<HwmonTempSensor> self = weakRef.lock();
127 if (!self)
128 {
129 return;
130 }
131 self->setupRead();
132 });
133}
134
James Feistd8705872019-02-08 13:26:09 -0800135void HwmonTempSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700136{
Yong Lif3fd1912020-03-25 21:35:23 +0800137 if ((err == boost::system::errc::bad_file_descriptor) ||
138 (err == boost::asio::error::misc_errors::not_found))
James Feist6714a252018-09-10 15:26:18 -0700139 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700140 std::cerr << "Hwmon temp sensor " << name << " removed " << path
141 << "\n";
James Feist6714a252018-09-10 15:26:18 -0700142 return; // we're being destroyed
143 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700144 std::istream responseStream(&readBuf);
James Feist6714a252018-09-10 15:26:18 -0700145 if (!err)
146 {
147 std::string response;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700148 std::getline(responseStream, response);
James Feist6714a252018-09-10 15:26:18 -0700149 try
150 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700151 rawValue = std::stod(response);
Bruce Mitchelle5fc3a52021-08-10 11:50:25 -0500152 double nvalue = (rawValue + sensorOffset) * sensorScale;
Josh Lehan833661a2020-03-04 17:35:41 -0800153 updateValue(nvalue);
James Feist6714a252018-09-10 15:26:18 -0700154 }
James Feistd8705872019-02-08 13:26:09 -0800155 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700156 {
James Feist961bf092020-07-01 16:38:12 -0700157 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700158 }
159 }
160 else
161 {
James Feist961bf092020-07-01 16:38:12 -0700162 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700163 }
James Feistdc6c55f2018-10-31 12:53:20 -0700164
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700165 responseStream.clear();
166 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700167 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700168 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700169 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700170 std::cerr << "Hwmon temp sensor " << name << " not valid " << path
171 << "\n";
James Feist6714a252018-09-10 15:26:18 -0700172 return; // we're no longer valid
173 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700174 inputDev.assign(fd);
Konstantin Aladysheva7345942021-04-28 17:09:02 +0300175 restartRead();
James Feist6714a252018-09-10 15:26:18 -0700176}
177
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700178void HwmonTempSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700179{
James Feist251c7822018-09-12 12:54:15 -0700180 thresholds::checkThresholds(this);
Patrick Ventureca44b2f2019-10-31 11:02:26 -0700181}