blob: 81c7b9ecdcef1761c0752e12575bc6256a852235 [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
Ed Tanous8a57ec02020-10-09 12:46:52 -070019#include <PSUSensor.hpp>
Cheng C Yang209ec562019-03-12 16:37:44 +080020#include <boost/algorithm/string/predicate.hpp>
21#include <boost/algorithm/string/replace.hpp>
James Feist8086aba2020-08-25 16:00:59 -070022#include <boost/asio/read_until.hpp>
Cheng C Yang209ec562019-03-12 16:37:44 +080023#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
Cheng C Yang209ec562019-03-12 16:37:44 +080027#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070028#include <istream>
Cheng C Yang209ec562019-03-12 16:37:44 +080029#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070030#include <memory>
Cheng C Yang209ec562019-03-12 16:37:44 +080031#include <string>
Paul Fertser34533542021-07-20 08:29:09 +000032#include <system_error>
Patrick Venture96e97db2019-10-31 13:44:38 -070033#include <vector>
Cheng C Yang209ec562019-03-12 16:37:44 +080034
James Feist690895f2019-04-23 13:01:08 -070035static constexpr const char* sensorPathPrefix = "/xyz/openbmc_project/sensors/";
36
Ed Tanous8a57ec02020-10-09 12:46:52 -070037static constexpr bool debug = false;
Josh Lehan49cfba92019-10-08 16:50:42 -070038
Cheng C Yang209ec562019-03-12 16:37:44 +080039PSUSensor::PSUSensor(const std::string& path, const std::string& objectType,
40 sdbusplus::asio::object_server& objectServer,
41 std::shared_ptr<sdbusplus::asio::connection>& conn,
42 boost::asio::io_service& io, const std::string& sensorName,
Ed Tanous8a57ec02020-10-09 12:46:52 -070043 std::vector<thresholds::Threshold>&& thresholdsIn,
Cheng C Yang209ec562019-03-12 16:37:44 +080044 const std::string& sensorConfiguration,
Zev Weiss6b6891c2021-04-22 02:46:21 -050045 const std::string& sensorUnits, unsigned int factor,
Jeff Line41d52f2021-04-07 19:38:51 +080046 double max, double min, double offset,
47 const std::string& label, size_t tSize, double pollRate) :
James Feist930fcde2019-05-28 12:58:43 -070048 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
Bruce Lee1263c3d2021-06-04 15:16:33 +080049 std::move(thresholdsIn), sensorConfiguration, objectType, false, max,
50 min, conn),
Yong Libf8b1da2020-04-15 16:32:50 +080051 std::enable_shared_from_this<PSUSensor>(), objServer(objectServer),
Zbigniew Kurzynski484b9b32020-06-18 18:15:55 +020052 inputDev(io), waitTimer(io), path(path), pathRatedMax(""), pathRatedMin(""),
Jeff Line41d52f2021-04-07 19:38:51 +080053 sensorFactor(factor), minMaxReadCounter(0), sensorOffset(offset)
Cheng C Yang209ec562019-03-12 16:37:44 +080054{
Zev Weiss6b6891c2021-04-22 02:46:21 -050055 std::string unitPath = sensor_paths::getPathForUnits(sensorUnits);
Ed Tanous8a57ec02020-10-09 12:46:52 -070056 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -070057 {
58 std::cerr << "Constructed sensor: path " << path << " type "
59 << objectType << " config " << sensorConfiguration
Zev Weiss6b6891c2021-04-22 02:46:21 -050060 << " typename " << unitPath << " factor " << factor << " min "
Jeff Line41d52f2021-04-07 19:38:51 +080061 << min << " max " << max << " offset " << offset << " name \""
62 << sensorName << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -070063 }
Lei YU7170a232021-02-04 16:19:27 +080064 if (pollRate > 0.0)
65 {
66 sensorPollMs = static_cast<unsigned int>(pollRate * 1000);
67 }
Josh Lehan49cfba92019-10-08 16:50:42 -070068
Kuiying Wangbcf76712021-03-19 11:17:58 +080069 fd = open(path.c_str(), O_RDONLY | O_NONBLOCK);
Cheng C Yanga97f1342020-02-11 15:10:41 +080070 if (fd < 0)
71 {
72 std::cerr << "PSU sensor failed to open file\n";
73 return;
74 }
75 inputDev.assign(fd);
76
Zev Weiss6b6891c2021-04-22 02:46:21 -050077 std::string dbusPath = sensorPathPrefix + unitPath + "/" + name;
James Feist690895f2019-04-23 13:01:08 -070078
Cheng C Yang209ec562019-03-12 16:37:44 +080079 sensorInterface = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070080 dbusPath, "xyz.openbmc_project.Sensor.Value");
Cheng C Yang209ec562019-03-12 16:37:44 +080081
82 if (thresholds::hasWarningInterface(thresholds))
83 {
84 thresholdInterfaceWarning = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070085 dbusPath, "xyz.openbmc_project.Sensor.Threshold.Warning");
Cheng C Yang209ec562019-03-12 16:37:44 +080086 }
87 if (thresholds::hasCriticalInterface(thresholds))
88 {
89 thresholdInterfaceCritical = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070090 dbusPath, "xyz.openbmc_project.Sensor.Threshold.Critical");
Cheng C Yang209ec562019-03-12 16:37:44 +080091 }
James Feist690895f2019-04-23 13:01:08 -070092
AppaRao Pulic82213c2020-02-27 01:24:58 +053093 // This should be called before initializing association.
94 // createInventoryAssoc() does add more associations before doing
95 // register and initialize "Associations" property.
Ed Tanous8a57ec02020-10-09 12:46:52 -070096 if (label.empty() || tSize == thresholds.size())
Cheng C Yang6b1247a2020-03-09 23:48:39 +080097 {
Zev Weiss6b6891c2021-04-22 02:46:21 -050098 setInitialProperties(conn, sensorUnits);
Cheng C Yang6b1247a2020-03-09 23:48:39 +080099 }
100 else
101 {
Zev Weiss6b6891c2021-04-22 02:46:21 -0500102 setInitialProperties(conn, sensorUnits, label, tSize);
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800103 }
Patrick Venture43162182019-10-23 10:44:53 -0700104
AppaRao Pulic82213c2020-02-27 01:24:58 +0530105 association = objectServer.add_interface(dbusPath, association::interface);
106
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800107 createInventoryAssoc(conn, association, configurationPath);
Zbigniew Kurzynski484b9b32020-06-18 18:15:55 +0200108
109 if (auto fileParts = splitFileName(path))
110 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200111 auto& [type, nr, item] = *fileParts;
Zbigniew Kurzynski484b9b32020-06-18 18:15:55 +0200112 if (item.compare("input") == 0)
113 {
114 pathRatedMax = boost::replace_all_copy(path, item, "rated_max");
115 pathRatedMin = boost::replace_all_copy(path, item, "rated_min");
116 }
117 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700118 if constexpr (debug)
Zbigniew Kurzynski484b9b32020-06-18 18:15:55 +0200119 {
120 std::cerr << "File: " << pathRatedMax
121 << " will be used to update MaxValue\n";
122 std::cerr << "File: " << pathRatedMin
123 << " will be used to update MinValue\n";
124 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800125}
126
127PSUSensor::~PSUSensor()
128{
Cheng C Yang209ec562019-03-12 16:37:44 +0800129 waitTimer.cancel();
Cheng C Yanga97f1342020-02-11 15:10:41 +0800130 inputDev.close();
Cheng C Yang209ec562019-03-12 16:37:44 +0800131 objServer.remove_interface(sensorInterface);
132 objServer.remove_interface(thresholdInterfaceWarning);
133 objServer.remove_interface(thresholdInterfaceCritical);
AppaRao Pulic82213c2020-02-27 01:24:58 +0530134 objServer.remove_interface(association);
Cheng C Yang209ec562019-03-12 16:37:44 +0800135}
136
137void PSUSensor::setupRead(void)
138{
Yong Libf8b1da2020-04-15 16:32:50 +0800139 std::weak_ptr<PSUSensor> weakRef = weak_from_this();
Kuiying Wangbcf76712021-03-19 11:17:58 +0800140 inputDev.async_wait(boost::asio::posix::descriptor_base::wait_read,
141 [weakRef](const boost::system::error_code& ec) {
142 std::shared_ptr<PSUSensor> self = weakRef.lock();
143 if (self)
144 {
145 self->handleResponse(ec);
146 }
147 });
Cheng C Yang209ec562019-03-12 16:37:44 +0800148}
149
Zbigniew Kurzynski484b9b32020-06-18 18:15:55 +0200150void PSUSensor::updateMinMaxValues(void)
151{
152 if (auto newVal = readFile(pathRatedMin, sensorFactor))
153 {
154 updateProperty(sensorInterface, minValue, *newVal, "MinValue");
155 }
156
157 if (auto newVal = readFile(pathRatedMax, sensorFactor))
158 {
159 updateProperty(sensorInterface, maxValue, *newVal, "MaxValue");
160 }
161}
162
Kuiying Wangbcf76712021-03-19 11:17:58 +0800163// Create a buffer expected to be able to hold more characters than will be
164// present in the input file.
165static constexpr uint32_t psuBufLen = 128;
Cheng C Yang209ec562019-03-12 16:37:44 +0800166void PSUSensor::handleResponse(const boost::system::error_code& err)
167{
Yong Libf8b1da2020-04-15 16:32:50 +0800168 if ((err == boost::system::errc::bad_file_descriptor) ||
169 (err == boost::asio::error::misc_errors::not_found))
Cheng C Yang209ec562019-03-12 16:37:44 +0800170 {
Paul Fertser34533542021-07-20 08:29:09 +0000171 std::cerr << "Bad file descriptor for " << path << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800172 return;
173 }
Kuiying Wangbcf76712021-03-19 11:17:58 +0800174
175 std::string buffer;
176 buffer.resize(psuBufLen);
177 lseek(fd, 0, SEEK_SET);
178 int rdLen = read(fd, buffer.data(), psuBufLen);
179
180 if (rdLen > 0)
Cheng C Yang209ec562019-03-12 16:37:44 +0800181 {
Cheng C Yang209ec562019-03-12 16:37:44 +0800182 try
183 {
Kuiying Wangbcf76712021-03-19 11:17:58 +0800184 rawValue = std::stod(buffer);
Jeff Line41d52f2021-04-07 19:38:51 +0800185 updateValue((rawValue / sensorFactor) + sensorOffset);
Zbigniew Kurzynski484b9b32020-06-18 18:15:55 +0200186 if (minMaxReadCounter++ % 8 == 0)
187 {
188 updateMinMaxValues();
189 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800190 }
191 catch (const std::invalid_argument&)
192 {
Kuiying Wangbcf76712021-03-19 11:17:58 +0800193 std::cerr << "Could not parse input from " << path << "\n";
James Feist961bf092020-07-01 16:38:12 -0700194 incrementError();
Cheng C Yang209ec562019-03-12 16:37:44 +0800195 }
196 }
197 else
198 {
Paul Fertser34533542021-07-20 08:29:09 +0000199 std::cerr
200 << "System error " << errno << " ("
201 << std::generic_category().default_error_condition(errno).message()
202 << ") reading from " << path << ", line: " << __LINE__ << "\n";
James Feist961bf092020-07-01 16:38:12 -0700203 incrementError();
Cheng C Yang209ec562019-03-12 16:37:44 +0800204 }
205
Cheng C Yanga97f1342020-02-11 15:10:41 +0800206 lseek(fd, 0, SEEK_SET);
Cheng C Yang209ec562019-03-12 16:37:44 +0800207 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
Yong Libf8b1da2020-04-15 16:32:50 +0800208
209 std::weak_ptr<PSUSensor> weakRef = weak_from_this();
210 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
Cheng C Yang209ec562019-03-12 16:37:44 +0800211 if (ec == boost::asio::error::operation_aborted)
212 {
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800213 std::cerr << "Failed to reschedule\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800214 return;
215 }
Kuiying Wangbcf76712021-03-19 11:17:58 +0800216 std::shared_ptr<PSUSensor> self = weakRef.lock();
Yong Libf8b1da2020-04-15 16:32:50 +0800217 if (self)
218 {
219 self->setupRead();
220 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800221 });
222}
223
224void PSUSensor::checkThresholds(void)
225{
226 thresholds::checkThresholds(this);
227}