blob: 68c7a0e4fae7bd64387f9d85c8e3a3ba3fb17d5c [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
Patrick Ventureca44b2f2019-10-31 11:02:26 -070017#include "PSUSensor.hpp"
18
Cheng C Yang209ec562019-03-12 16:37:44 +080019#include <unistd.h>
20
Cheng C Yang209ec562019-03-12 16:37:44 +080021#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>
25#include <limits>
26#include <sdbusplus/asio/connection.hpp>
27#include <sdbusplus/asio/object_server.hpp>
28#include <string>
29
James Feist690895f2019-04-23 13:01:08 -070030static constexpr const char* sensorPathPrefix = "/xyz/openbmc_project/sensors/";
31
Josh Lehan49cfba92019-10-08 16:50:42 -070032static constexpr bool DEBUG = false;
33
Cheng C Yang209ec562019-03-12 16:37:44 +080034PSUSensor::PSUSensor(const std::string& path, const std::string& objectType,
35 sdbusplus::asio::object_server& objectServer,
36 std::shared_ptr<sdbusplus::asio::connection>& conn,
37 boost::asio::io_service& io, const std::string& sensorName,
38 std::vector<thresholds::Threshold>&& _thresholds,
39 const std::string& sensorConfiguration,
40 std::string& sensorTypeName, unsigned int factor,
41 double max, double min) :
James Feist930fcde2019-05-28 12:58:43 -070042 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
Cheng C Yang209ec562019-03-12 16:37:44 +080043 std::move(_thresholds), sensorConfiguration, objectType, max, min),
James Feist930fcde2019-05-28 12:58:43 -070044 path(path), objServer(objectServer),
45 inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), errCount(0),
46 sensorFactor(factor)
Cheng C Yang209ec562019-03-12 16:37:44 +080047{
Josh Lehan49cfba92019-10-08 16:50:42 -070048 if constexpr (DEBUG)
49 {
50 std::cerr << "Constructed sensor: path " << path << " type "
51 << objectType << " config " << sensorConfiguration
52 << " typename " << sensorTypeName << " factor " << factor
53 << " min " << min << " max " << max << " name \""
54 << sensorName << "\"\n";
55 }
56
James Feist690895f2019-04-23 13:01:08 -070057 std::string dbusPath = sensorPathPrefix + sensorTypeName + name;
58
Cheng C Yang209ec562019-03-12 16:37:44 +080059 sensorInterface = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070060 dbusPath, "xyz.openbmc_project.Sensor.Value");
Cheng C Yang209ec562019-03-12 16:37:44 +080061
62 if (thresholds::hasWarningInterface(thresholds))
63 {
64 thresholdInterfaceWarning = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070065 dbusPath, "xyz.openbmc_project.Sensor.Threshold.Warning");
Cheng C Yang209ec562019-03-12 16:37:44 +080066 }
67 if (thresholds::hasCriticalInterface(thresholds))
68 {
69 thresholdInterfaceCritical = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070070 dbusPath, "xyz.openbmc_project.Sensor.Threshold.Critical");
Cheng C Yang209ec562019-03-12 16:37:44 +080071 }
James Feist2adc95c2019-09-30 14:55:28 -070072 association = objectServer.add_interface(dbusPath, association::interface);
James Feist690895f2019-04-23 13:01:08 -070073
Patrick Venture43162182019-10-23 10:44:53 -070074 setInitialProperties(conn);
75
Cheng C Yang5580f2f2019-09-19 09:01:47 +080076 createInventoryAssoc(conn, association, configurationPath);
Cheng C Yang209ec562019-03-12 16:37:44 +080077 setupRead();
78}
79
80PSUSensor::~PSUSensor()
81{
82 inputDev.close();
83 waitTimer.cancel();
84 objServer.remove_interface(sensorInterface);
85 objServer.remove_interface(thresholdInterfaceWarning);
86 objServer.remove_interface(thresholdInterfaceCritical);
87}
88
89void PSUSensor::setupRead(void)
90{
91 boost::asio::async_read_until(
92 inputDev, readBuf, '\n',
93 [&](const boost::system::error_code& ec,
94 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
95}
96
97void PSUSensor::handleResponse(const boost::system::error_code& err)
98{
99 if (err == boost::system::errc::bad_file_descriptor)
100 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700101 std::cerr << "Bad file descriptor from " << path << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800102 return;
103 }
104 std::istream responseStream(&readBuf);
105 if (!err)
106 {
107 std::string response;
108 try
109 {
110 std::getline(responseStream, response);
111 float nvalue = std::stof(response);
112 responseStream.clear();
113 nvalue /= sensorFactor;
Josh Lehan49cfba92019-10-08 16:50:42 -0700114
115 if constexpr (DEBUG)
116 {
117 std::cerr << "Read " << path << " scale " << sensorFactor
118 << " value " << nvalue << "\n";
119 }
James Feistb6c0b912019-07-09 12:21:44 -0700120 if (static_cast<double>(nvalue) != value)
Cheng C Yang209ec562019-03-12 16:37:44 +0800121 {
Josh Lehan432d1ed2019-10-16 12:23:31 -0700122 if constexpr (DEBUG)
123 {
124 std::cerr << "Update " << path << " from " << value
125 << " to " << nvalue << "\n";
126 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800127 updateValue(nvalue);
128 }
129 errCount = 0;
130 }
131 catch (const std::invalid_argument&)
132 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700133 std::cerr << "Could not parse " << response << " from path " << path
134 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800135 errCount++;
136 }
137 }
138 else
139 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700140 std::cerr << "System error " << err << " from path " << path << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800141 errCount++;
142 }
143
144 if (errCount >= warnAfterErrorCount)
145 {
146 if (errCount == warnAfterErrorCount)
147 {
148 std::cerr << "Failure to read sensor " << name << " at " << path
149 << "\n";
150 }
151 updateValue(0);
152 errCount++;
153 }
154
155 responseStream.clear();
156 inputDev.close();
157 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700158 if (fd < 0)
Cheng C Yang209ec562019-03-12 16:37:44 +0800159 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700160 std::cerr << "Failed to open path " << path << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800161 return;
162 }
163 inputDev.assign(fd);
164 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
165 waitTimer.async_wait([&](const boost::system::error_code& ec) {
166 if (ec == boost::asio::error::operation_aborted)
167 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700168 std::cerr << "Failed to reschedule wait for " << path << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800169 return;
170 }
171 setupRead();
172 });
173}
174
175void PSUSensor::checkThresholds(void)
176{
177 thresholds::checkThresholds(this);
178}