blob: 657773958a4667d5017854e6270ec17a9ac94760 [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
James Feistce3fca42018-11-21 12:58:24 -080041
James Feist6714a252018-09-10 15:26:18 -070042HwmonTempSensor::HwmonTempSensor(
James Feistd8705872019-02-08 13:26:09 -080043 const std::string& path, const std::string& objectType,
44 sdbusplus::asio::object_server& objectServer,
45 std::shared_ptr<sdbusplus::asio::connection>& conn,
46 boost::asio::io_service& io, const std::string& sensorName,
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050047 std::vector<thresholds::Threshold>&& thresholdsIn,
48 const struct SensorParams& thisSensorParameters, const float pollRate,
James Feistf9b01b62020-01-29 15:21:58 -080049 const std::string& sensorConfiguration, const PowerState powerState) :
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050050 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
51 std::move(thresholdsIn), sensorConfiguration, objectType, false,
52 false, thisSensorParameters.maxValue, thisSensorParameters.minValue,
53 conn, powerState),
Yong Lif3fd1912020-03-25 21:35:23 +080054 std::enable_shared_from_this<HwmonTempSensor>(), objServer(objectServer),
Ed Tanous99c44092022-01-14 09:59:09 -080055 inputDev(io), waitTimer(io), path(path),
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050056 offsetValue(thisSensorParameters.offsetValue),
57 scaleValue(thisSensorParameters.scaleValue),
Ed Tanous8a57ec02020-10-09 12:46:52 -070058 sensorPollMs(static_cast<unsigned int>(pollRate * 1000))
James Feist6714a252018-09-10 15:26:18 -070059{
Ed Tanous99c44092022-01-14 09:59:09 -080060 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
61 int fd = open(path.c_str(), O_RDONLY);
62 if (fd < 0)
63 {
64 std::cerr << "HwmonTempSensor " << sensorName << " failed to open "
65 << path << "\n";
66 }
67 inputDev.assign(fd);
68
James Feist251c7822018-09-12 12:54:15 -070069 sensorInterface = objectServer.add_interface(
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050070 "/xyz/openbmc_project/sensors/" + thisSensorParameters.typeName + "/" +
71 name,
James Feist251c7822018-09-12 12:54:15 -070072 "xyz.openbmc_project.Sensor.Value");
73
Jayashree Dhanapal56678082022-01-04 17:27:20 +053074 for (const auto& threshold : thresholds)
James Feist6714a252018-09-10 15:26:18 -070075 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053076 std::string interface = thresholds::getInterface(threshold.level);
77 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
78 objectServer.add_interface("/xyz/openbmc_project/sensors/" +
79 thisSensorParameters.typeName + "/" +
80 name,
81 interface);
James Feist6714a252018-09-10 15:26:18 -070082 }
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050083 association = objectServer.add_interface("/xyz/openbmc_project/sensors/" +
84 thisSensorParameters.typeName +
85 "/" + name,
86 association::interface);
Andrei Kartashev39287412022-02-04 16:04:47 +030087 setInitialProperties(thisSensorParameters.units);
James Feist6714a252018-09-10 15:26:18 -070088}
89
90HwmonTempSensor::~HwmonTempSensor()
91{
92 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070093 inputDev.close();
94 waitTimer.cancel();
Jayashree Dhanapal56678082022-01-04 17:27:20 +053095 for (const auto& iface : thresholdInterfaces)
96 {
97 objServer.remove_interface(iface);
98 }
James Feist251c7822018-09-12 12:54:15 -070099 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800100 objServer.remove_interface(association);
James Feist6714a252018-09-10 15:26:18 -0700101}
102
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700103void HwmonTempSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -0700104{
Konstantin Aladysheva7345942021-04-28 17:09:02 +0300105 if (!readingStateGood())
106 {
107 markAvailable(false);
108 updateValue(std::numeric_limits<double>::quiet_NaN());
109 restartRead();
110 return;
111 }
Yong Lif3fd1912020-03-25 21:35:23 +0800112
Konstantin Aladysheva7345942021-04-28 17:09:02 +0300113 std::weak_ptr<HwmonTempSensor> weakRef = weak_from_this();
Yong Lif3fd1912020-03-25 21:35:23 +0800114 boost::asio::async_read_until(inputDev, readBuf, '\n',
115 [weakRef](const boost::system::error_code& ec,
116 std::size_t /*bytes_transfered*/) {
117 std::shared_ptr<HwmonTempSensor> self =
118 weakRef.lock();
119 if (self)
120 {
121 self->handleResponse(ec);
122 }
123 });
James Feist6714a252018-09-10 15:26:18 -0700124}
125
Konstantin Aladysheva7345942021-04-28 17:09:02 +0300126void HwmonTempSensor::restartRead()
127{
128 std::weak_ptr<HwmonTempSensor> weakRef = weak_from_this();
129 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
130 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
131 if (ec == boost::asio::error::operation_aborted)
132 {
133 return; // we're being canceled
134 }
135 std::shared_ptr<HwmonTempSensor> self = weakRef.lock();
136 if (!self)
137 {
138 return;
139 }
140 self->setupRead();
141 });
142}
143
James Feistd8705872019-02-08 13:26:09 -0800144void HwmonTempSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700145{
Yong Lif3fd1912020-03-25 21:35:23 +0800146 if ((err == boost::system::errc::bad_file_descriptor) ||
147 (err == boost::asio::error::misc_errors::not_found))
James Feist6714a252018-09-10 15:26:18 -0700148 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700149 std::cerr << "Hwmon temp sensor " << name << " removed " << path
150 << "\n";
James Feist6714a252018-09-10 15:26:18 -0700151 return; // we're being destroyed
152 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700153 std::istream responseStream(&readBuf);
James Feist6714a252018-09-10 15:26:18 -0700154 if (!err)
155 {
156 std::string response;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700157 std::getline(responseStream, response);
James Feist6714a252018-09-10 15:26:18 -0700158 try
159 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700160 rawValue = std::stod(response);
Bruce Mitchell544e7dc2021-07-29 18:05:49 -0500161 double nvalue = (rawValue + offsetValue) * scaleValue;
Josh Lehan833661a2020-03-04 17:35:41 -0800162 updateValue(nvalue);
James Feist6714a252018-09-10 15:26:18 -0700163 }
James Feistd8705872019-02-08 13:26:09 -0800164 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700165 {
James Feist961bf092020-07-01 16:38:12 -0700166 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700167 }
168 }
169 else
170 {
James Feist961bf092020-07-01 16:38:12 -0700171 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700172 }
James Feistdc6c55f2018-10-31 12:53:20 -0700173
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700174 responseStream.clear();
175 inputDev.close();
Ed Tanous99c44092022-01-14 09:59:09 -0800176
177 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
James Feist6714a252018-09-10 15:26:18 -0700178 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700179 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700180 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700181 std::cerr << "Hwmon temp sensor " << name << " not valid " << path
182 << "\n";
James Feist6714a252018-09-10 15:26:18 -0700183 return; // we're no longer valid
184 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700185 inputDev.assign(fd);
Konstantin Aladysheva7345942021-04-28 17:09:02 +0300186 restartRead();
James Feist6714a252018-09-10 15:26:18 -0700187}
188
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700189void HwmonTempSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700190{
James Feist251c7822018-09-12 12:54:15 -0700191 thresholds::checkThresholds(this);
Patrick Ventureca44b2f2019-10-31 11:02:26 -0700192}