blob: bbc0d2f07eabb344db554f4a0104d7c2df39a968 [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>
James Feist8086aba2020-08-25 16:00:59 -070021#include <boost/asio/read_until.hpp>
Cheng C Yang209ec562019-03-12 16:37:44 +080022#include <boost/date_time/posix_time/posix_time.hpp>
James Feist38fb5982020-05-28 10:09:54 -070023#include <sdbusplus/asio/connection.hpp>
24#include <sdbusplus/asio/object_server.hpp>
25
Cheng C Yang209ec562019-03-12 16:37:44 +080026#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070027#include <istream>
Cheng C Yang209ec562019-03-12 16:37:44 +080028#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070029#include <memory>
Cheng C Yang209ec562019-03-12 16:37:44 +080030#include <string>
Paul Fertser34533542021-07-20 08:29:09 +000031#include <system_error>
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,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000044 const PowerState& powerState,
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) :
Zhikui Renda98f092021-11-01 09:41:08 -070048 Sensor(escapeName(sensorName), std::move(thresholdsIn), sensorConfiguration,
49 objectType, false, false, max, min, conn, powerState),
Yong Libf8b1da2020-04-15 16:32:50 +080050 std::enable_shared_from_this<PSUSensor>(), objServer(objectServer),
Agata Wiatrowska3fed6742021-09-29 10:53:52 +020051 inputDev(io), waitTimer(io), path(path), sensorFactor(factor),
52 sensorOffset(offset), thresholdTimer(io)
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 "
Jeff Line41d52f2021-04-07 19:38:51 +080060 << min << " max " << max << " offset " << offset << " name \""
61 << sensorName << "\"\n";
Josh Lehan49cfba92019-10-08 16:50:42 -070062 }
Lei YU7170a232021-02-04 16:19:27 +080063 if (pollRate > 0.0)
64 {
65 sensorPollMs = static_cast<unsigned int>(pollRate * 1000);
66 }
Josh Lehan49cfba92019-10-08 16:50:42 -070067
Ed Tanous99c44092022-01-14 09:59:09 -080068 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
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
Jayashree Dhanapal56678082022-01-04 17:27:20 +053082 for (const auto& threshold : thresholds)
Cheng C Yang209ec562019-03-12 16:37:44 +080083 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053084 std::string interface = thresholds::getInterface(threshold.level);
85 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
86 objectServer.add_interface(dbusPath, interface);
Cheng C Yang209ec562019-03-12 16:37:44 +080087 }
James Feist690895f2019-04-23 13:01:08 -070088
AppaRao Pulic82213c2020-02-27 01:24:58 +053089 // This should be called before initializing association.
90 // createInventoryAssoc() does add more associations before doing
91 // register and initialize "Associations" property.
Ed Tanous8a57ec02020-10-09 12:46:52 -070092 if (label.empty() || tSize == thresholds.size())
Cheng C Yang6b1247a2020-03-09 23:48:39 +080093 {
Andrei Kartashev39287412022-02-04 16:04:47 +030094 setInitialProperties(sensorUnits);
Cheng C Yang6b1247a2020-03-09 23:48:39 +080095 }
96 else
97 {
Andrei Kartashev39287412022-02-04 16:04:47 +030098 setInitialProperties(sensorUnits, label, tSize);
Cheng C Yang6b1247a2020-03-09 23:48:39 +080099 }
Patrick Venture43162182019-10-23 10:44:53 -0700100
AppaRao Pulic82213c2020-02-27 01:24:58 +0530101 association = objectServer.add_interface(dbusPath, association::interface);
102
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800103 createInventoryAssoc(conn, association, configurationPath);
Cheng C Yang209ec562019-03-12 16:37:44 +0800104}
105
106PSUSensor::~PSUSensor()
107{
Cheng C Yang209ec562019-03-12 16:37:44 +0800108 waitTimer.cancel();
Cheng C Yanga97f1342020-02-11 15:10:41 +0800109 inputDev.close();
Cheng C Yang209ec562019-03-12 16:37:44 +0800110 objServer.remove_interface(sensorInterface);
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530111 for (const auto& iface : thresholdInterfaces)
112 {
113 objServer.remove_interface(iface);
114 }
AppaRao Pulic82213c2020-02-27 01:24:58 +0530115 objServer.remove_interface(association);
Cheng C Yang209ec562019-03-12 16:37:44 +0800116}
117
118void PSUSensor::setupRead(void)
119{
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000120 if (!readingStateGood())
121 {
122 markAvailable(false);
123 updateValue(std::numeric_limits<double>::quiet_NaN());
124 restartRead();
125 return;
126 }
127
Yong Libf8b1da2020-04-15 16:32:50 +0800128 std::weak_ptr<PSUSensor> weakRef = weak_from_this();
Kuiying Wangbcf76712021-03-19 11:17:58 +0800129 inputDev.async_wait(boost::asio::posix::descriptor_base::wait_read,
130 [weakRef](const boost::system::error_code& ec) {
131 std::shared_ptr<PSUSensor> self = weakRef.lock();
132 if (self)
133 {
134 self->handleResponse(ec);
135 }
136 });
Cheng C Yang209ec562019-03-12 16:37:44 +0800137}
138
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000139void PSUSensor::restartRead(void)
140{
141 std::weak_ptr<PSUSensor> weakRef = weak_from_this();
142 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
143 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
144 if (ec == boost::asio::error::operation_aborted)
145 {
146 std::cerr << "Failed to reschedule\n";
147 return;
148 }
149 std::shared_ptr<PSUSensor> self = weakRef.lock();
150 if (self)
151 {
152 self->setupRead();
153 }
154 });
155}
156
Kuiying Wangbcf76712021-03-19 11:17:58 +0800157// Create a buffer expected to be able to hold more characters than will be
158// present in the input file.
159static constexpr uint32_t psuBufLen = 128;
Cheng C Yang209ec562019-03-12 16:37:44 +0800160void PSUSensor::handleResponse(const boost::system::error_code& err)
161{
Yong Libf8b1da2020-04-15 16:32:50 +0800162 if ((err == boost::system::errc::bad_file_descriptor) ||
163 (err == boost::asio::error::misc_errors::not_found))
Cheng C Yang209ec562019-03-12 16:37:44 +0800164 {
Paul Fertser34533542021-07-20 08:29:09 +0000165 std::cerr << "Bad file descriptor for " << path << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800166 return;
167 }
Kuiying Wangbcf76712021-03-19 11:17:58 +0800168
169 std::string buffer;
170 buffer.resize(psuBufLen);
171 lseek(fd, 0, SEEK_SET);
172 int rdLen = read(fd, buffer.data(), psuBufLen);
173
174 if (rdLen > 0)
Cheng C Yang209ec562019-03-12 16:37:44 +0800175 {
Cheng C Yang209ec562019-03-12 16:37:44 +0800176 try
177 {
Kuiying Wangbcf76712021-03-19 11:17:58 +0800178 rawValue = std::stod(buffer);
Jeff Line41d52f2021-04-07 19:38:51 +0800179 updateValue((rawValue / sensorFactor) + sensorOffset);
Cheng C Yang209ec562019-03-12 16:37:44 +0800180 }
181 catch (const std::invalid_argument&)
182 {
Kuiying Wangbcf76712021-03-19 11:17:58 +0800183 std::cerr << "Could not parse input from " << path << "\n";
James Feist961bf092020-07-01 16:38:12 -0700184 incrementError();
Cheng C Yang209ec562019-03-12 16:37:44 +0800185 }
186 }
187 else
188 {
Paul Fertser34533542021-07-20 08:29:09 +0000189 std::cerr
190 << "System error " << errno << " ("
191 << std::generic_category().default_error_condition(errno).message()
192 << ") reading from " << path << ", line: " << __LINE__ << "\n";
James Feist961bf092020-07-01 16:38:12 -0700193 incrementError();
Cheng C Yang209ec562019-03-12 16:37:44 +0800194 }
195
Cheng C Yanga97f1342020-02-11 15:10:41 +0800196 lseek(fd, 0, SEEK_SET);
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000197 restartRead();
Cheng C Yang209ec562019-03-12 16:37:44 +0800198}
199
200void PSUSensor::checkThresholds(void)
201{
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000202 if (!readingStateGood())
203 {
204 return;
205 }
206
207 thresholds::checkThresholdsPowerDelay(weak_from_this(), thresholdTimer);
Cheng C Yang209ec562019-03-12 16:37:44 +0800208}