blob: 7d443ada8370d86496788442809008b40a6ed976 [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
19#include <HwmonTempSensor.hpp>
20#include <boost/algorithm/string/predicate.hpp>
21#include <boost/algorithm/string/replace.hpp>
22#include <boost/date_time/posix_time/posix_time.hpp>
23#include <iostream>
24#include <limits>
25#include <sdbusplus/asio/connection.hpp>
26#include <sdbusplus/asio/object_server.hpp>
27#include <string>
28
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070029static constexpr unsigned int sensorPollMs = 500;
30static constexpr unsigned int sensorScaleFactor = 1000;
31static constexpr size_t warnAfterErrorCount = 10;
James Feist6714a252018-09-10 15:26:18 -070032
James Feistce3fca42018-11-21 12:58:24 -080033static constexpr double maxReading = 127;
34static constexpr double minReading = -128;
35
James Feist6714a252018-09-10 15:26:18 -070036HwmonTempSensor::HwmonTempSensor(
37 const std::string &path, const std::string &objectType,
38 sdbusplus::asio::object_server &objectServer,
39 std::shared_ptr<sdbusplus::asio::connection> &conn,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070040 boost::asio::io_service &io, const std::string &sensorName,
James Feist6714a252018-09-10 15:26:18 -070041 std::vector<thresholds::Threshold> &&_thresholds,
42 const std::string &sensorConfiguration) :
James Feistdc6c55f2018-10-31 12:53:20 -070043 Sensor(boost::replace_all_copy(sensorName, " ", "_"), path,
James Feistce3fca42018-11-21 12:58:24 -080044 std::move(_thresholds), sensorConfiguration, objectType, maxReading,
45 minReading),
James Feistdc6c55f2018-10-31 12:53:20 -070046 objServer(objectServer), inputDev(io, open(path.c_str(), O_RDONLY)),
James Feistce3fca42018-11-21 12:58:24 -080047 waitTimer(io), errCount(0)
James Feist6714a252018-09-10 15:26:18 -070048{
James Feist251c7822018-09-12 12:54:15 -070049 sensorInterface = objectServer.add_interface(
50 "/xyz/openbmc_project/sensors/temperature/" + name,
51 "xyz.openbmc_project.Sensor.Value");
52
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070053 if (thresholds::hasWarningInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070054 {
James Feist251c7822018-09-12 12:54:15 -070055 thresholdInterfaceWarning = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070056 "/xyz/openbmc_project/sensors/temperature/" + name,
57 "xyz.openbmc_project.Sensor.Threshold.Warning");
58 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070059 if (thresholds::hasCriticalInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070060 {
James Feist251c7822018-09-12 12:54:15 -070061 thresholdInterfaceCritical = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070062 "/xyz/openbmc_project/sensors/temperature/" + name,
63 "xyz.openbmc_project.Sensor.Threshold.Critical");
64 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070065 setInitialProperties(conn);
66 setupRead();
James Feist6714a252018-09-10 15:26:18 -070067}
68
69HwmonTempSensor::~HwmonTempSensor()
70{
71 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070072 inputDev.close();
73 waitTimer.cancel();
James Feist251c7822018-09-12 12:54:15 -070074 objServer.remove_interface(thresholdInterfaceWarning);
75 objServer.remove_interface(thresholdInterfaceCritical);
76 objServer.remove_interface(sensorInterface);
James Feist6714a252018-09-10 15:26:18 -070077}
78
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070079void HwmonTempSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -070080{
81 boost::asio::async_read_until(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070082 inputDev, readBuf, '\n',
James Feist6714a252018-09-10 15:26:18 -070083 [&](const boost::system::error_code &ec,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070084 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist6714a252018-09-10 15:26:18 -070085}
86
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070087void HwmonTempSensor::handleResponse(const boost::system::error_code &err)
James Feist6714a252018-09-10 15:26:18 -070088{
89 if (err == boost::system::errc::bad_file_descriptor)
90 {
91 return; // we're being destroyed
92 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070093 std::istream responseStream(&readBuf);
James Feist6714a252018-09-10 15:26:18 -070094 if (!err)
95 {
96 std::string response;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070097 std::getline(responseStream, response);
James Feist6714a252018-09-10 15:26:18 -070098 try
99 {
100 float nvalue = std::stof(response);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700101 nvalue /= sensorScaleFactor;
Richard Marian Thomaiyarb2eb29a2018-12-08 18:26:58 +0530102 if (overridenState)
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530103 {
104 nvalue = overriddenValue;
105 }
James Feist6714a252018-09-10 15:26:18 -0700106 if (nvalue != value)
107 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700108 updateValue(nvalue);
James Feist6714a252018-09-10 15:26:18 -0700109 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700110 errCount = 0;
James Feist6714a252018-09-10 15:26:18 -0700111 }
112 catch (const std::invalid_argument &)
113 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700114 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700115 }
116 }
117 else
118 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700119 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700120 }
James Feistdc6c55f2018-10-31 12:53:20 -0700121
122 // only print once
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700123 if (errCount == warnAfterErrorCount)
James Feist6714a252018-09-10 15:26:18 -0700124 {
James Feistdc6c55f2018-10-31 12:53:20 -0700125 std::cerr << "Failure to read sensor " << name << " at " << path
126 << " ec:" << err << "\n";
127 }
128
129 if (errCount >= warnAfterErrorCount)
130 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700131 updateValue(0);
James Feist6714a252018-09-10 15:26:18 -0700132 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700133 responseStream.clear();
134 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700135 int fd = open(path.c_str(), O_RDONLY);
136 if (fd <= 0)
137 {
138 return; // we're no longer valid
139 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700140 inputDev.assign(fd);
141 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700142 waitTimer.async_wait([&](const boost::system::error_code &ec) {
James Feist6714a252018-09-10 15:26:18 -0700143 if (ec == boost::asio::error::operation_aborted)
144 {
145 return; // we're being canceled
146 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700147 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700148 });
149}
150
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700151void HwmonTempSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700152{
James Feist251c7822018-09-12 12:54:15 -0700153 thresholds::checkThresholds(this);
James Feistce3fca42018-11-21 12:58:24 -0800154}