blob: 4d6bc1dd24ce1ab1431c50bbfc14fc5645a16eff [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>
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
Josh Lehan49cfba92019-10-08 16:50:42 -070036static constexpr bool DEBUG = false;
37
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,
42 std::vector<thresholds::Threshold>&& _thresholds,
43 const std::string& sensorConfiguration,
44 std::string& sensorTypeName, 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, " ", "_"),
Cheng C Yang209ec562019-03-12 16:37:44 +080048 std::move(_thresholds), sensorConfiguration, objectType, max, min),
Yong Libf8b1da2020-04-15 16:32:50 +080049 std::enable_shared_from_this<PSUSensor>(), objServer(objectServer),
50 inputDev(io), waitTimer(io), path(path), errCount(0), sensorFactor(factor)
Cheng C Yang209ec562019-03-12 16:37:44 +080051{
Josh Lehan49cfba92019-10-08 16:50:42 -070052 if constexpr (DEBUG)
53 {
54 std::cerr << "Constructed sensor: path " << path << " type "
55 << objectType << " config " << sensorConfiguration
56 << " typename " << sensorTypeName << " factor " << factor
57 << " min " << min << " max " << max << " name \""
58 << sensorName << "\"\n";
59 }
60
Cheng C Yanga97f1342020-02-11 15:10:41 +080061 fd = open(path.c_str(), O_RDONLY);
62 if (fd < 0)
63 {
64 std::cerr << "PSU sensor failed to open file\n";
65 return;
66 }
67 inputDev.assign(fd);
68
James Feist690895f2019-04-23 13:01:08 -070069 std::string dbusPath = sensorPathPrefix + sensorTypeName + name;
70
Cheng C Yang209ec562019-03-12 16:37:44 +080071 sensorInterface = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070072 dbusPath, "xyz.openbmc_project.Sensor.Value");
Cheng C Yang209ec562019-03-12 16:37:44 +080073
74 if (thresholds::hasWarningInterface(thresholds))
75 {
76 thresholdInterfaceWarning = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070077 dbusPath, "xyz.openbmc_project.Sensor.Threshold.Warning");
Cheng C Yang209ec562019-03-12 16:37:44 +080078 }
79 if (thresholds::hasCriticalInterface(thresholds))
80 {
81 thresholdInterfaceCritical = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070082 dbusPath, "xyz.openbmc_project.Sensor.Threshold.Critical");
Cheng C Yang209ec562019-03-12 16:37:44 +080083 }
James Feist690895f2019-04-23 13:01:08 -070084
AppaRao Pulic82213c2020-02-27 01:24:58 +053085 // This should be called before initializing association.
86 // createInventoryAssoc() does add more associations before doing
87 // register and initialize "Associations" property.
Cheng C Yang6b1247a2020-03-09 23:48:39 +080088 if (label.empty() || tSize == _thresholds.size())
89 {
90 setInitialProperties(conn);
91 }
92 else
93 {
94 setInitialProperties(conn, label, tSize);
95 }
Patrick Venture43162182019-10-23 10:44:53 -070096
AppaRao Pulic82213c2020-02-27 01:24:58 +053097 association = objectServer.add_interface(dbusPath, association::interface);
98
Cheng C Yang5580f2f2019-09-19 09:01:47 +080099 createInventoryAssoc(conn, association, configurationPath);
Cheng C Yang209ec562019-03-12 16:37:44 +0800100}
101
102PSUSensor::~PSUSensor()
103{
Cheng C Yang209ec562019-03-12 16:37:44 +0800104 waitTimer.cancel();
Cheng C Yanga97f1342020-02-11 15:10:41 +0800105 inputDev.close();
Cheng C Yang209ec562019-03-12 16:37:44 +0800106 objServer.remove_interface(sensorInterface);
107 objServer.remove_interface(thresholdInterfaceWarning);
108 objServer.remove_interface(thresholdInterfaceCritical);
AppaRao Pulic82213c2020-02-27 01:24:58 +0530109 objServer.remove_interface(association);
Cheng C Yang209ec562019-03-12 16:37:44 +0800110}
111
112void PSUSensor::setupRead(void)
113{
Yong Libf8b1da2020-04-15 16:32:50 +0800114 std::shared_ptr<boost::asio::streambuf> buffer =
115 std::make_shared<boost::asio::streambuf>();
116 std::weak_ptr<PSUSensor> weakRef = weak_from_this();
Cheng C Yang209ec562019-03-12 16:37:44 +0800117 boost::asio::async_read_until(
Yong Libf8b1da2020-04-15 16:32:50 +0800118 inputDev, *buffer, '\n',
119 [weakRef, buffer](const boost::system::error_code& ec,
120 std::size_t /*bytes_transfered*/) {
121 std::shared_ptr<PSUSensor> self = weakRef.lock();
122 if (self)
123 {
124 self->readBuf = buffer;
125 self->handleResponse(ec);
126 }
127 });
Cheng C Yang209ec562019-03-12 16:37:44 +0800128}
129
130void PSUSensor::handleResponse(const boost::system::error_code& err)
131{
Yong Libf8b1da2020-04-15 16:32:50 +0800132 if ((err == boost::system::errc::bad_file_descriptor) ||
133 (err == boost::asio::error::misc_errors::not_found))
Cheng C Yang209ec562019-03-12 16:37:44 +0800134 {
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800135 std::cerr << "Bad file descriptor from\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800136 return;
137 }
Yong Libf8b1da2020-04-15 16:32:50 +0800138 std::istream responseStream(readBuf.get());
Cheng C Yang209ec562019-03-12 16:37:44 +0800139 if (!err)
140 {
141 std::string response;
142 try
143 {
144 std::getline(responseStream, response);
Josh Lehan833661a2020-03-04 17:35:41 -0800145 double nvalue = std::stod(response);
Cheng C Yang209ec562019-03-12 16:37:44 +0800146 responseStream.clear();
147 nvalue /= sensorFactor;
Josh Lehan49cfba92019-10-08 16:50:42 -0700148
Josh Lehan833661a2020-03-04 17:35:41 -0800149 updateValue(nvalue);
Cheng C Yang209ec562019-03-12 16:37:44 +0800150 errCount = 0;
151 }
152 catch (const std::invalid_argument&)
153 {
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800154 std::cerr << "Could not parse " << response << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800155 errCount++;
156 }
157 }
158 else
159 {
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800160 std::cerr << "System error " << err << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800161 errCount++;
162 }
163
164 if (errCount >= warnAfterErrorCount)
165 {
166 if (errCount == warnAfterErrorCount)
167 {
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800168 std::cerr << "Failure to read sensor " << name << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800169 }
170 updateValue(0);
171 errCount++;
172 }
173
Cheng C Yanga97f1342020-02-11 15:10:41 +0800174 lseek(fd, 0, SEEK_SET);
Cheng C Yang209ec562019-03-12 16:37:44 +0800175 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
Yong Libf8b1da2020-04-15 16:32:50 +0800176
177 std::weak_ptr<PSUSensor> weakRef = weak_from_this();
178 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
179 std::shared_ptr<PSUSensor> self = weakRef.lock();
Cheng C Yang209ec562019-03-12 16:37:44 +0800180 if (ec == boost::asio::error::operation_aborted)
181 {
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800182 std::cerr << "Failed to reschedule\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800183 return;
184 }
Yong Libf8b1da2020-04-15 16:32:50 +0800185 if (self)
186 {
187 self->setupRead();
188 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800189 });
190}
191
192void PSUSensor::checkThresholds(void)
193{
194 thresholds::checkThresholds(this);
195}