blob: 5222b04a505350a1e84cfaf89259a2705b857874 [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),
Brad Bishopfbb44ad2019-11-08 09:42:37 -050050 objServer(objectServer), inputDev(io, open(path.c_str(), O_RDONLY)),
James Feistf9b01b62020-01-29 15:21:58 -080051 waitTimer(io), path(path), errCount(0), readState(powerState)
James Feist6714a252018-09-10 15:26:18 -070052{
James Feist251c7822018-09-12 12:54:15 -070053 sensorInterface = objectServer.add_interface(
54 "/xyz/openbmc_project/sensors/temperature/" + name,
55 "xyz.openbmc_project.Sensor.Value");
56
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070057 if (thresholds::hasWarningInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070058 {
James Feist251c7822018-09-12 12:54:15 -070059 thresholdInterfaceWarning = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070060 "/xyz/openbmc_project/sensors/temperature/" + name,
61 "xyz.openbmc_project.Sensor.Threshold.Warning");
62 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070063 if (thresholds::hasCriticalInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070064 {
James Feist251c7822018-09-12 12:54:15 -070065 thresholdInterfaceCritical = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070066 "/xyz/openbmc_project/sensors/temperature/" + name,
67 "xyz.openbmc_project.Sensor.Threshold.Critical");
68 }
James Feist078f2322019-03-08 11:09:05 -080069 association = objectServer.add_interface(
70 "/xyz/openbmc_project/sensors/temperature/" + name,
James Feist2adc95c2019-09-30 14:55:28 -070071 association::interface);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070072 setInitialProperties(conn);
James Feistf9b01b62020-01-29 15:21:58 -080073 setupPowerMatch(conn);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070074 setupRead();
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{
90 boost::asio::async_read_until(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070091 inputDev, readBuf, '\n',
James Feistd8705872019-02-08 13:26:09 -080092 [&](const boost::system::error_code& ec,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070093 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist6714a252018-09-10 15:26:18 -070094}
95
James Feistd8705872019-02-08 13:26:09 -080096void HwmonTempSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -070097{
98 if (err == boost::system::errc::bad_file_descriptor)
99 {
100 return; // we're being destroyed
101 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700102 std::istream responseStream(&readBuf);
James Feist6714a252018-09-10 15:26:18 -0700103 if (!err)
104 {
105 std::string response;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700106 std::getline(responseStream, response);
James Feist6714a252018-09-10 15:26:18 -0700107 try
108 {
109 float nvalue = std::stof(response);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700110 nvalue /= sensorScaleFactor;
James Feistb6c0b912019-07-09 12:21:44 -0700111 if (static_cast<double>(nvalue) != value)
James Feist6714a252018-09-10 15:26:18 -0700112 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700113 updateValue(nvalue);
James Feist6714a252018-09-10 15:26:18 -0700114 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700115 errCount = 0;
James Feist6714a252018-09-10 15:26:18 -0700116 }
James Feistd8705872019-02-08 13:26:09 -0800117 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700118 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700119 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700120 }
121 }
122 else
123 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700124 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700125 }
James Feistdc6c55f2018-10-31 12:53:20 -0700126
127 // only print once
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700128 if (errCount == warnAfterErrorCount)
James Feist6714a252018-09-10 15:26:18 -0700129 {
James Feistdc6c55f2018-10-31 12:53:20 -0700130 std::cerr << "Failure to read sensor " << name << " at " << path
131 << " ec:" << err << "\n";
132 }
133
134 if (errCount >= warnAfterErrorCount)
135 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700136 updateValue(0);
James Feist6714a252018-09-10 15:26:18 -0700137 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700138 responseStream.clear();
139 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700140 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700141 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700142 {
143 return; // we're no longer valid
144 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700145 inputDev.assign(fd);
146 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
James Feistd8705872019-02-08 13:26:09 -0800147 waitTimer.async_wait([&](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700148 if (ec == boost::asio::error::operation_aborted)
149 {
150 return; // we're being canceled
151 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700152 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700153 });
154}
155
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700156void HwmonTempSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700157{
James Feistf9b01b62020-01-29 15:21:58 -0800158 if (readState == PowerState::on && !isPowerOn())
159 {
160 return;
161 }
James Feist251c7822018-09-12 12:54:15 -0700162 thresholds::checkThresholds(this);
Patrick Ventureca44b2f2019-10-31 11:02:26 -0700163}