blob: 38bc96b53954fe475718a674e51595145e97d7f1 [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>
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 sensorPollMs = 500;
35static constexpr unsigned int sensorScaleFactor = 1000;
36static constexpr size_t warnAfterErrorCount = 10;
James Feist6714a252018-09-10 15:26:18 -070037
James Feistce3fca42018-11-21 12:58:24 -080038static constexpr double maxReading = 127;
39static constexpr double minReading = -128;
40
James Feist6714a252018-09-10 15:26:18 -070041HwmonTempSensor::HwmonTempSensor(
James Feistd8705872019-02-08 13:26:09 -080042 const std::string& path, const std::string& objectType,
43 sdbusplus::asio::object_server& objectServer,
44 std::shared_ptr<sdbusplus::asio::connection>& conn,
45 boost::asio::io_service& io, const std::string& sensorName,
46 std::vector<thresholds::Threshold>&& _thresholds,
James Feistf9b01b62020-01-29 15:21:58 -080047 const std::string& sensorConfiguration, const PowerState powerState) :
James Feist930fcde2019-05-28 12:58:43 -070048 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
James Feistce3fca42018-11-21 12:58:24 -080049 std::move(_thresholds), sensorConfiguration, objectType, maxReading,
James Feist961bf092020-07-01 16:38:12 -070050 minReading, powerState),
Yong Lif3fd1912020-03-25 21:35:23 +080051 std::enable_shared_from_this<HwmonTempSensor>(), objServer(objectServer),
James Feist961bf092020-07-01 16:38:12 -070052 inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), path(path)
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 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{
Yong Lif3fd1912020-03-25 21:35:23 +080089 std::weak_ptr<HwmonTempSensor> weakRef = weak_from_this();
90
91 boost::asio::async_read_until(inputDev, readBuf, '\n',
92 [weakRef](const boost::system::error_code& ec,
93 std::size_t /*bytes_transfered*/) {
94 std::shared_ptr<HwmonTempSensor> self =
95 weakRef.lock();
96 if (self)
97 {
98 self->handleResponse(ec);
99 }
100 });
James Feist6714a252018-09-10 15:26:18 -0700101}
102
James Feistd8705872019-02-08 13:26:09 -0800103void HwmonTempSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700104{
Yong Lif3fd1912020-03-25 21:35:23 +0800105 if ((err == boost::system::errc::bad_file_descriptor) ||
106 (err == boost::asio::error::misc_errors::not_found))
James Feist6714a252018-09-10 15:26:18 -0700107 {
108 return; // we're being destroyed
109 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700110 std::istream responseStream(&readBuf);
James Feist6714a252018-09-10 15:26:18 -0700111 if (!err)
112 {
113 std::string response;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700114 std::getline(responseStream, response);
James Feist6714a252018-09-10 15:26:18 -0700115 try
116 {
Josh Lehan833661a2020-03-04 17:35:41 -0800117 double nvalue = std::stod(response);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700118 nvalue /= sensorScaleFactor;
Josh Lehan833661a2020-03-04 17:35:41 -0800119 updateValue(nvalue);
James Feist6714a252018-09-10 15:26:18 -0700120 }
James Feistd8705872019-02-08 13:26:09 -0800121 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700122 {
James Feist961bf092020-07-01 16:38:12 -0700123 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700124 }
125 }
126 else
127 {
James Feist961bf092020-07-01 16:38:12 -0700128 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700129 }
James Feistdc6c55f2018-10-31 12:53:20 -0700130
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700131 responseStream.clear();
132 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700133 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700134 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700135 {
136 return; // we're no longer valid
137 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700138 inputDev.assign(fd);
139 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
Yong Lif3fd1912020-03-25 21:35:23 +0800140 std::weak_ptr<HwmonTempSensor> weakRef = weak_from_this();
141 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
142 std::shared_ptr<HwmonTempSensor> self = weakRef.lock();
James Feist6714a252018-09-10 15:26:18 -0700143 if (ec == boost::asio::error::operation_aborted)
144 {
145 return; // we're being canceled
146 }
Yong Lif3fd1912020-03-25 21:35:23 +0800147 if (self)
148 {
149 self->setupRead();
150 }
James Feist6714a252018-09-10 15:26:18 -0700151 });
152}
153
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700154void HwmonTempSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700155{
James Feist251c7822018-09-12 12:54:15 -0700156 thresholds::checkThresholds(this);
Patrick Ventureca44b2f2019-10-31 11:02:26 -0700157}