blob: 9f9448ad4bda6e50a81eed2608a21617a0e0e33b [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>
24#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070025#include <istream>
James Feist6714a252018-09-10 15:26:18 -070026#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070027#include <memory>
James Feist6714a252018-09-10 15:26:18 -070028#include <sdbusplus/asio/connection.hpp>
29#include <sdbusplus/asio/object_server.hpp>
30#include <string>
Patrick Venture96e97db2019-10-31 13:44:38 -070031#include <vector>
James Feist6714a252018-09-10 15:26:18 -070032
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070033static constexpr unsigned int sensorPollMs = 500;
34static 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,
45 std::vector<thresholds::Threshold>&& _thresholds,
James Feistf9b01b62020-01-29 15:21:58 -080046 const std::string& sensorConfiguration, const PowerState powerState) :
James Feist930fcde2019-05-28 12:58:43 -070047 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
James Feistce3fca42018-11-21 12:58:24 -080048 std::move(_thresholds), sensorConfiguration, objectType, maxReading,
49 minReading),
Yong Lif3fd1912020-03-25 21:35:23 +080050 std::enable_shared_from_this<HwmonTempSensor>(), objServer(objectServer),
51 inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), path(path),
52 errCount(0), readState(powerState)
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);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070073 setInitialProperties(conn);
James Feistf9b01b62020-01-29 15:21:58 -080074 setupPowerMatch(conn);
James Feist6714a252018-09-10 15:26:18 -070075}
76
77HwmonTempSensor::~HwmonTempSensor()
78{
79 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070080 inputDev.close();
81 waitTimer.cancel();
James Feist251c7822018-09-12 12:54:15 -070082 objServer.remove_interface(thresholdInterfaceWarning);
83 objServer.remove_interface(thresholdInterfaceCritical);
84 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -080085 objServer.remove_interface(association);
James Feist6714a252018-09-10 15:26:18 -070086}
87
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070088void HwmonTempSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -070089{
Yong Lif3fd1912020-03-25 21:35:23 +080090 std::weak_ptr<HwmonTempSensor> weakRef = weak_from_this();
91
92 boost::asio::async_read_until(inputDev, readBuf, '\n',
93 [weakRef](const boost::system::error_code& ec,
94 std::size_t /*bytes_transfered*/) {
95 std::shared_ptr<HwmonTempSensor> self =
96 weakRef.lock();
97 if (self)
98 {
99 self->handleResponse(ec);
100 }
101 });
James Feist6714a252018-09-10 15:26:18 -0700102}
103
James Feistd8705872019-02-08 13:26:09 -0800104void HwmonTempSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700105{
Yong Lif3fd1912020-03-25 21:35:23 +0800106 if ((err == boost::system::errc::bad_file_descriptor) ||
107 (err == boost::asio::error::misc_errors::not_found))
James Feist6714a252018-09-10 15:26:18 -0700108 {
109 return; // we're being destroyed
110 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700111 std::istream responseStream(&readBuf);
James Feist6714a252018-09-10 15:26:18 -0700112 if (!err)
113 {
114 std::string response;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700115 std::getline(responseStream, response);
James Feist6714a252018-09-10 15:26:18 -0700116 try
117 {
Josh Lehan833661a2020-03-04 17:35:41 -0800118 double nvalue = std::stod(response);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700119 nvalue /= sensorScaleFactor;
Josh Lehan833661a2020-03-04 17:35:41 -0800120 updateValue(nvalue);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700121 errCount = 0;
James Feist6714a252018-09-10 15:26:18 -0700122 }
James Feistd8705872019-02-08 13:26:09 -0800123 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700124 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700125 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700126 }
127 }
James Feist3459d012020-01-30 13:17:43 -0800128 else if (readState == PowerState::on && !isPowerOn())
129 {
130 errCount = 0;
131 updateValue(std::numeric_limits<double>::quiet_NaN());
132 }
James Feist6714a252018-09-10 15:26:18 -0700133 else
134 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700135 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700136 }
James Feistdc6c55f2018-10-31 12:53:20 -0700137
138 // only print once
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700139 if (errCount == warnAfterErrorCount)
James Feist6714a252018-09-10 15:26:18 -0700140 {
James Feistdc6c55f2018-10-31 12:53:20 -0700141 std::cerr << "Failure to read sensor " << name << " at " << path
142 << " ec:" << err << "\n";
143 }
144
145 if (errCount >= warnAfterErrorCount)
146 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700147 updateValue(0);
James Feist6714a252018-09-10 15:26:18 -0700148 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700149 responseStream.clear();
150 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700151 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700152 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700153 {
154 return; // we're no longer valid
155 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700156 inputDev.assign(fd);
157 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
Yong Lif3fd1912020-03-25 21:35:23 +0800158 std::weak_ptr<HwmonTempSensor> weakRef = weak_from_this();
159 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
160 std::shared_ptr<HwmonTempSensor> self = weakRef.lock();
James Feist6714a252018-09-10 15:26:18 -0700161 if (ec == boost::asio::error::operation_aborted)
162 {
163 return; // we're being canceled
164 }
Yong Lif3fd1912020-03-25 21:35:23 +0800165 if (self)
166 {
167 self->setupRead();
168 }
James Feist6714a252018-09-10 15:26:18 -0700169 });
170}
171
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700172void HwmonTempSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700173{
James Feistf9b01b62020-01-29 15:21:58 -0800174 if (readState == PowerState::on && !isPowerOn())
175 {
176 return;
177 }
James Feist251c7822018-09-12 12:54:15 -0700178 thresholds::checkThresholds(this);
Patrick Ventureca44b2f2019-10-31 11:02:26 -0700179}