blob: 942cfc30bcaff4c723335c842746c8e13a791212 [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>
Patrick Venture96e97db2019-10-31 13:44:38 -070032#include <vector>
Cheng C Yang209ec562019-03-12 16:37:44 +080033
James Feist690895f2019-04-23 13:01:08 -070034static constexpr const char* sensorPathPrefix = "/xyz/openbmc_project/sensors/";
35
Ed Tanous8a57ec02020-10-09 12:46:52 -070036static constexpr bool debug = false;
Josh Lehan49cfba92019-10-08 16:50:42 -070037
Cheng C Yang209ec562019-03-12 16:37:44 +080038PSUSensor::PSUSensor(const std::string& path, const std::string& objectType,
39 sdbusplus::asio::object_server& objectServer,
40 std::shared_ptr<sdbusplus::asio::connection>& conn,
41 boost::asio::io_service& io, const std::string& sensorName,
Ed Tanous8a57ec02020-10-09 12:46:52 -070042 std::vector<thresholds::Threshold>&& thresholdsIn,
Cheng C Yang209ec562019-03-12 16:37:44 +080043 const std::string& sensorConfiguration,
Zev Weiss6b6891c2021-04-22 02:46:21 -050044 const std::string& sensorUnits, unsigned int factor,
Cheng C Yang6b1247a2020-03-09 23:48:39 +080045 double max, double min, const std::string& label,
46 size_t tSize) :
James Feist930fcde2019-05-28 12:58:43 -070047 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
Bruce Lee1263c3d2021-06-04 15:16:33 +080048 std::move(thresholdsIn), sensorConfiguration, objectType, false, max,
49 min, conn),
Yong Libf8b1da2020-04-15 16:32:50 +080050 std::enable_shared_from_this<PSUSensor>(), objServer(objectServer),
Zbigniew Kurzynski484b9b32020-06-18 18:15:55 +020051 inputDev(io), waitTimer(io), path(path), pathRatedMax(""), pathRatedMin(""),
52 sensorFactor(factor), minMaxReadCounter(0)
Cheng C Yang209ec562019-03-12 16:37:44 +080053{
Zev Weiss6b6891c2021-04-22 02:46:21 -050054 std::string unitPath = sensor_paths::getPathForUnits(sensorUnits);
Ed Tanous8a57ec02020-10-09 12:46:52 -070055 if constexpr (debug)
Josh Lehan49cfba92019-10-08 16:50:42 -070056 {
57 std::cerr << "Constructed sensor: path " << path << " type "
58 << objectType << " config " << sensorConfiguration
Zev Weiss6b6891c2021-04-22 02:46:21 -050059 << " typename " << unitPath << " factor " << factor << " min "
60 << min << " max " << max << " name \"" << sensorName
61 << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -070062 }
63
Cheng C Yanga97f1342020-02-11 15:10:41 +080064 fd = open(path.c_str(), O_RDONLY);
65 if (fd < 0)
66 {
67 std::cerr << "PSU sensor failed to open file\n";
68 return;
69 }
70 inputDev.assign(fd);
71
Zev Weiss6b6891c2021-04-22 02:46:21 -050072 std::string dbusPath = sensorPathPrefix + unitPath + "/" + name;
James Feist690895f2019-04-23 13:01:08 -070073
Cheng C Yang209ec562019-03-12 16:37:44 +080074 sensorInterface = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070075 dbusPath, "xyz.openbmc_project.Sensor.Value");
Cheng C Yang209ec562019-03-12 16:37:44 +080076
77 if (thresholds::hasWarningInterface(thresholds))
78 {
79 thresholdInterfaceWarning = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070080 dbusPath, "xyz.openbmc_project.Sensor.Threshold.Warning");
Cheng C Yang209ec562019-03-12 16:37:44 +080081 }
82 if (thresholds::hasCriticalInterface(thresholds))
83 {
84 thresholdInterfaceCritical = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070085 dbusPath, "xyz.openbmc_project.Sensor.Threshold.Critical");
Cheng C Yang209ec562019-03-12 16:37:44 +080086 }
James Feist690895f2019-04-23 13:01:08 -070087
AppaRao Pulic82213c2020-02-27 01:24:58 +053088 // This should be called before initializing association.
89 // createInventoryAssoc() does add more associations before doing
90 // register and initialize "Associations" property.
Ed Tanous8a57ec02020-10-09 12:46:52 -070091 if (label.empty() || tSize == thresholds.size())
Cheng C Yang6b1247a2020-03-09 23:48:39 +080092 {
Zev Weiss6b6891c2021-04-22 02:46:21 -050093 setInitialProperties(conn, sensorUnits);
Cheng C Yang6b1247a2020-03-09 23:48:39 +080094 }
95 else
96 {
Zev Weiss6b6891c2021-04-22 02:46:21 -050097 setInitialProperties(conn, sensorUnits, label, tSize);
Cheng C Yang6b1247a2020-03-09 23:48:39 +080098 }
Patrick Venture43162182019-10-23 10:44:53 -070099
AppaRao Pulic82213c2020-02-27 01:24:58 +0530100 association = objectServer.add_interface(dbusPath, association::interface);
101
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800102 createInventoryAssoc(conn, association, configurationPath);
Zbigniew Kurzynski484b9b32020-06-18 18:15:55 +0200103
104 if (auto fileParts = splitFileName(path))
105 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200106 auto& [type, nr, item] = *fileParts;
Zbigniew Kurzynski484b9b32020-06-18 18:15:55 +0200107 if (item.compare("input") == 0)
108 {
109 pathRatedMax = boost::replace_all_copy(path, item, "rated_max");
110 pathRatedMin = boost::replace_all_copy(path, item, "rated_min");
111 }
112 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700113 if constexpr (debug)
Zbigniew Kurzynski484b9b32020-06-18 18:15:55 +0200114 {
115 std::cerr << "File: " << pathRatedMax
116 << " will be used to update MaxValue\n";
117 std::cerr << "File: " << pathRatedMin
118 << " will be used to update MinValue\n";
119 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800120}
121
122PSUSensor::~PSUSensor()
123{
Cheng C Yang209ec562019-03-12 16:37:44 +0800124 waitTimer.cancel();
Cheng C Yanga97f1342020-02-11 15:10:41 +0800125 inputDev.close();
Cheng C Yang209ec562019-03-12 16:37:44 +0800126 objServer.remove_interface(sensorInterface);
127 objServer.remove_interface(thresholdInterfaceWarning);
128 objServer.remove_interface(thresholdInterfaceCritical);
AppaRao Pulic82213c2020-02-27 01:24:58 +0530129 objServer.remove_interface(association);
Cheng C Yang209ec562019-03-12 16:37:44 +0800130}
131
132void PSUSensor::setupRead(void)
133{
Yong Libf8b1da2020-04-15 16:32:50 +0800134 std::shared_ptr<boost::asio::streambuf> buffer =
135 std::make_shared<boost::asio::streambuf>();
136 std::weak_ptr<PSUSensor> weakRef = weak_from_this();
Cheng C Yang209ec562019-03-12 16:37:44 +0800137 boost::asio::async_read_until(
Yong Libf8b1da2020-04-15 16:32:50 +0800138 inputDev, *buffer, '\n',
139 [weakRef, buffer](const boost::system::error_code& ec,
140 std::size_t /*bytes_transfered*/) {
141 std::shared_ptr<PSUSensor> self = weakRef.lock();
142 if (self)
143 {
144 self->readBuf = buffer;
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
Cheng C Yang209ec562019-03-12 16:37:44 +0800163void PSUSensor::handleResponse(const boost::system::error_code& err)
164{
Yong Libf8b1da2020-04-15 16:32:50 +0800165 if ((err == boost::system::errc::bad_file_descriptor) ||
166 (err == boost::asio::error::misc_errors::not_found))
Cheng C Yang209ec562019-03-12 16:37:44 +0800167 {
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800168 std::cerr << "Bad file descriptor from\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800169 return;
170 }
Yong Libf8b1da2020-04-15 16:32:50 +0800171 std::istream responseStream(readBuf.get());
Cheng C Yang209ec562019-03-12 16:37:44 +0800172 if (!err)
173 {
174 std::string response;
175 try
176 {
177 std::getline(responseStream, response);
Zhikui Rend3da1282020-09-11 17:02:01 -0700178 rawValue = std::stod(response);
Cheng C Yang209ec562019-03-12 16:37:44 +0800179 responseStream.clear();
Zhikui Rend3da1282020-09-11 17:02:01 -0700180 double nvalue = rawValue / sensorFactor;
Josh Lehan49cfba92019-10-08 16:50:42 -0700181
Josh Lehan833661a2020-03-04 17:35:41 -0800182 updateValue(nvalue);
Zbigniew Kurzynski484b9b32020-06-18 18:15:55 +0200183
184 if (minMaxReadCounter++ % 8 == 0)
185 {
186 updateMinMaxValues();
187 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800188 }
189 catch (const std::invalid_argument&)
190 {
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800191 std::cerr << "Could not parse " << response << "\n";
James Feist961bf092020-07-01 16:38:12 -0700192 incrementError();
Cheng C Yang209ec562019-03-12 16:37:44 +0800193 }
194 }
195 else
196 {
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800197 std::cerr << "System error " << err << "\n";
James Feist961bf092020-07-01 16:38:12 -0700198 incrementError();
Cheng C Yang209ec562019-03-12 16:37:44 +0800199 }
200
Cheng C Yanga97f1342020-02-11 15:10:41 +0800201 lseek(fd, 0, SEEK_SET);
Cheng C Yang209ec562019-03-12 16:37:44 +0800202 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
Yong Libf8b1da2020-04-15 16:32:50 +0800203
204 std::weak_ptr<PSUSensor> weakRef = weak_from_this();
205 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
206 std::shared_ptr<PSUSensor> self = weakRef.lock();
Cheng C Yang209ec562019-03-12 16:37:44 +0800207 if (ec == boost::asio::error::operation_aborted)
208 {
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800209 std::cerr << "Failed to reschedule\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800210 return;
211 }
Yong Libf8b1da2020-04-15 16:32:50 +0800212 if (self)
213 {
214 self->setupRead();
215 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800216 });
217}
218
219void PSUSensor::checkThresholds(void)
220{
221 thresholds::checkThresholds(this);
222}