blob: bf02692e15c4f1d592809d138a26f9f4b2ee9fa9 [file] [log] [blame]
Cheng C Yang209ec562019-03-12 16:37:44 +08001/*
2// Copyright (c) 2019 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 <PSUSensor.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
James Feist690895f2019-04-23 13:01:08 -070029static constexpr const char* sensorPathPrefix = "/xyz/openbmc_project/sensors/";
30
Josh Lehan49cfba92019-10-08 16:50:42 -070031static constexpr bool DEBUG = false;
32
Cheng C Yang209ec562019-03-12 16:37:44 +080033PSUSensor::PSUSensor(const std::string& path, const std::string& objectType,
34 sdbusplus::asio::object_server& objectServer,
35 std::shared_ptr<sdbusplus::asio::connection>& conn,
36 boost::asio::io_service& io, const std::string& sensorName,
37 std::vector<thresholds::Threshold>&& _thresholds,
38 const std::string& sensorConfiguration,
39 std::string& sensorTypeName, unsigned int factor,
40 double max, double min) :
James Feist930fcde2019-05-28 12:58:43 -070041 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
Cheng C Yang209ec562019-03-12 16:37:44 +080042 std::move(_thresholds), sensorConfiguration, objectType, max, min),
James Feist930fcde2019-05-28 12:58:43 -070043 path(path), objServer(objectServer),
44 inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), errCount(0),
45 sensorFactor(factor)
Cheng C Yang209ec562019-03-12 16:37:44 +080046{
Josh Lehan49cfba92019-10-08 16:50:42 -070047 if constexpr (DEBUG)
48 {
49 std::cerr << "Constructed sensor: path " << path << " type "
50 << objectType << " config " << sensorConfiguration
51 << " typename " << sensorTypeName << " factor " << factor
52 << " min " << min << " max " << max << " name \""
53 << sensorName << "\"\n";
54 }
55
James Feist690895f2019-04-23 13:01:08 -070056 std::string dbusPath = sensorPathPrefix + sensorTypeName + name;
57
Cheng C Yang209ec562019-03-12 16:37:44 +080058 sensorInterface = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070059 dbusPath, "xyz.openbmc_project.Sensor.Value");
Cheng C Yang209ec562019-03-12 16:37:44 +080060
61 if (thresholds::hasWarningInterface(thresholds))
62 {
63 thresholdInterfaceWarning = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070064 dbusPath, "xyz.openbmc_project.Sensor.Threshold.Warning");
Cheng C Yang209ec562019-03-12 16:37:44 +080065 }
66 if (thresholds::hasCriticalInterface(thresholds))
67 {
68 thresholdInterfaceCritical = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070069 dbusPath, "xyz.openbmc_project.Sensor.Threshold.Critical");
Cheng C Yang209ec562019-03-12 16:37:44 +080070 }
James Feist2adc95c2019-09-30 14:55:28 -070071 association = objectServer.add_interface(dbusPath, association::interface);
James Feist690895f2019-04-23 13:01:08 -070072
Patrick Venture43162182019-10-23 10:44:53 -070073 setInitialProperties(conn);
74
Cheng C Yang5580f2f2019-09-19 09:01:47 +080075 createInventoryAssoc(conn, association, configurationPath);
Cheng C Yang209ec562019-03-12 16:37:44 +080076 setupRead();
77}
78
79PSUSensor::~PSUSensor()
80{
81 inputDev.close();
82 waitTimer.cancel();
83 objServer.remove_interface(sensorInterface);
84 objServer.remove_interface(thresholdInterfaceWarning);
85 objServer.remove_interface(thresholdInterfaceCritical);
86}
87
88void PSUSensor::setupRead(void)
89{
90 boost::asio::async_read_until(
91 inputDev, readBuf, '\n',
92 [&](const boost::system::error_code& ec,
93 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
94}
95
96void PSUSensor::handleResponse(const boost::system::error_code& err)
97{
98 if (err == boost::system::errc::bad_file_descriptor)
99 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700100 std::cerr << "Bad file descriptor from " << path << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800101 return;
102 }
103 std::istream responseStream(&readBuf);
104 if (!err)
105 {
106 std::string response;
107 try
108 {
109 std::getline(responseStream, response);
110 float nvalue = std::stof(response);
111 responseStream.clear();
112 nvalue /= sensorFactor;
Josh Lehan49cfba92019-10-08 16:50:42 -0700113
114 if constexpr (DEBUG)
115 {
116 std::cerr << "Read " << path << " scale " << sensorFactor
117 << " value " << nvalue << "\n";
118 }
James Feistb6c0b912019-07-09 12:21:44 -0700119 if (static_cast<double>(nvalue) != value)
Cheng C Yang209ec562019-03-12 16:37:44 +0800120 {
Josh Lehan432d1ed2019-10-16 12:23:31 -0700121 if constexpr (DEBUG)
122 {
123 std::cerr << "Update " << path << " from " << value
124 << " to " << nvalue << "\n";
125 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800126 updateValue(nvalue);
127 }
128 errCount = 0;
129 }
130 catch (const std::invalid_argument&)
131 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700132 std::cerr << "Could not parse " << response << " from path " << path
133 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800134 errCount++;
135 }
136 }
137 else
138 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700139 std::cerr << "System error " << err << " from path " << path << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800140 errCount++;
141 }
142
143 if (errCount >= warnAfterErrorCount)
144 {
145 if (errCount == warnAfterErrorCount)
146 {
147 std::cerr << "Failure to read sensor " << name << " at " << path
148 << "\n";
149 }
150 updateValue(0);
151 errCount++;
152 }
153
154 responseStream.clear();
155 inputDev.close();
156 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700157 if (fd < 0)
Cheng C Yang209ec562019-03-12 16:37:44 +0800158 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700159 std::cerr << "Failed to open path " << path << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800160 return;
161 }
162 inputDev.assign(fd);
163 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
164 waitTimer.async_wait([&](const boost::system::error_code& ec) {
165 if (ec == boost::asio::error::operation_aborted)
166 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700167 std::cerr << "Failed to reschedule wait for " << path << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800168 return;
169 }
170 setupRead();
171 });
172}
173
174void PSUSensor::checkThresholds(void)
175{
176 thresholds::checkThresholds(this);
177}