blob: 667e517a6febcf72506b97573fa6d9199f522633 [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
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070034static constexpr unsigned int sensorScaleFactor = 1000;
35static constexpr size_t warnAfterErrorCount = 10;
James Feist6714a252018-09-10 15:26:18 -070036
James Feistce3fca42018-11-21 12:58:24 -080037static constexpr double maxReading = 127;
38static constexpr double minReading = -128;
39
James Feist6714a252018-09-10 15:26:18 -070040HwmonTempSensor::HwmonTempSensor(
James Feistd8705872019-02-08 13:26:09 -080041 const std::string& path, const std::string& objectType,
42 sdbusplus::asio::object_server& objectServer,
43 std::shared_ptr<sdbusplus::asio::connection>& conn,
44 boost::asio::io_service& io, const std::string& sensorName,
Jeff Lin7b7a9de2021-02-22 11:16:27 +080045 std::vector<thresholds::Threshold>&& thresholdsIn, const float pollRate,
James Feistf9b01b62020-01-29 15:21:58 -080046 const std::string& sensorConfiguration, const PowerState powerState) :
Jeff Lin7b7a9de2021-02-22 11:16:27 +080047 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
Bruce Lee1263c3d2021-06-04 15:16:33 +080048 std::move(thresholdsIn), sensorConfiguration, objectType, false,
49 maxReading, minReading, conn, powerState),
Yong Lif3fd1912020-03-25 21:35:23 +080050 std::enable_shared_from_this<HwmonTempSensor>(), objServer(objectServer),
Jeff Lin87bc67f2020-12-04 20:58:01 +080051 inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), path(path),
Ed Tanous8a57ec02020-10-09 12:46:52 -070052 sensorPollMs(static_cast<unsigned int>(pollRate * 1000))
James Feist6714a252018-09-10 15:26:18 -070053{
James Feist251c7822018-09-12 12:54:15 -070054 sensorInterface = objectServer.add_interface(
55 "/xyz/openbmc_project/sensors/temperature/" + name,
56 "xyz.openbmc_project.Sensor.Value");
57
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070058 if (thresholds::hasWarningInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070059 {
James Feist251c7822018-09-12 12:54:15 -070060 thresholdInterfaceWarning = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070061 "/xyz/openbmc_project/sensors/temperature/" + name,
62 "xyz.openbmc_project.Sensor.Threshold.Warning");
63 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070064 if (thresholds::hasCriticalInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070065 {
James Feist251c7822018-09-12 12:54:15 -070066 thresholdInterfaceCritical = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070067 "/xyz/openbmc_project/sensors/temperature/" + name,
68 "xyz.openbmc_project.Sensor.Threshold.Critical");
69 }
James Feist078f2322019-03-08 11:09:05 -080070 association = objectServer.add_interface(
71 "/xyz/openbmc_project/sensors/temperature/" + name,
James Feist2adc95c2019-09-30 14:55:28 -070072 association::interface);
Zev Weiss6b6891c2021-04-22 02:46:21 -050073 setInitialProperties(conn, sensor_paths::unitDegreesC);
James Feist6714a252018-09-10 15:26:18 -070074}
75
76HwmonTempSensor::~HwmonTempSensor()
77{
78 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070079 inputDev.close();
80 waitTimer.cancel();
James Feist251c7822018-09-12 12:54:15 -070081 objServer.remove_interface(thresholdInterfaceWarning);
82 objServer.remove_interface(thresholdInterfaceCritical);
83 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -080084 objServer.remove_interface(association);
James Feist6714a252018-09-10 15:26:18 -070085}
86
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070087void HwmonTempSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -070088{
Konstantin Aladysheva7345942021-04-28 17:09:02 +030089 if (!readingStateGood())
90 {
91 markAvailable(false);
92 updateValue(std::numeric_limits<double>::quiet_NaN());
93 restartRead();
94 return;
95 }
Yong Lif3fd1912020-03-25 21:35:23 +080096
Konstantin Aladysheva7345942021-04-28 17:09:02 +030097 std::weak_ptr<HwmonTempSensor> weakRef = weak_from_this();
Yong Lif3fd1912020-03-25 21:35:23 +080098 boost::asio::async_read_until(inputDev, readBuf, '\n',
99 [weakRef](const boost::system::error_code& ec,
100 std::size_t /*bytes_transfered*/) {
101 std::shared_ptr<HwmonTempSensor> self =
102 weakRef.lock();
103 if (self)
104 {
105 self->handleResponse(ec);
106 }
107 });
James Feist6714a252018-09-10 15:26:18 -0700108}
109
Konstantin Aladysheva7345942021-04-28 17:09:02 +0300110void HwmonTempSensor::restartRead()
111{
112 std::weak_ptr<HwmonTempSensor> weakRef = weak_from_this();
113 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
114 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
115 if (ec == boost::asio::error::operation_aborted)
116 {
117 return; // we're being canceled
118 }
119 std::shared_ptr<HwmonTempSensor> self = weakRef.lock();
120 if (!self)
121 {
122 return;
123 }
124 self->setupRead();
125 });
126}
127
James Feistd8705872019-02-08 13:26:09 -0800128void HwmonTempSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700129{
Yong Lif3fd1912020-03-25 21:35:23 +0800130 if ((err == boost::system::errc::bad_file_descriptor) ||
131 (err == boost::asio::error::misc_errors::not_found))
James Feist6714a252018-09-10 15:26:18 -0700132 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700133 std::cerr << "Hwmon temp sensor " << name << " removed " << path
134 << "\n";
James Feist6714a252018-09-10 15:26:18 -0700135 return; // we're being destroyed
136 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700137 std::istream responseStream(&readBuf);
James Feist6714a252018-09-10 15:26:18 -0700138 if (!err)
139 {
140 std::string response;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700141 std::getline(responseStream, response);
James Feist6714a252018-09-10 15:26:18 -0700142 try
143 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700144 rawValue = std::stod(response);
145 double nvalue = rawValue / sensorScaleFactor;
Josh Lehan833661a2020-03-04 17:35:41 -0800146 updateValue(nvalue);
James Feist6714a252018-09-10 15:26:18 -0700147 }
James Feistd8705872019-02-08 13:26:09 -0800148 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700149 {
James Feist961bf092020-07-01 16:38:12 -0700150 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700151 }
152 }
153 else
154 {
James Feist961bf092020-07-01 16:38:12 -0700155 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700156 }
James Feistdc6c55f2018-10-31 12:53:20 -0700157
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700158 responseStream.clear();
159 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700160 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700161 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700162 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700163 std::cerr << "Hwmon temp sensor " << name << " not valid " << path
164 << "\n";
James Feist6714a252018-09-10 15:26:18 -0700165 return; // we're no longer valid
166 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700167 inputDev.assign(fd);
Konstantin Aladysheva7345942021-04-28 17:09:02 +0300168 restartRead();
James Feist6714a252018-09-10 15:26:18 -0700169}
170
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700171void HwmonTempSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700172{
James Feist251c7822018-09-12 12:54:15 -0700173 thresholds::checkThresholds(this);
Patrick Ventureca44b2f2019-10-31 11:02:26 -0700174}