blob: 96b710c3b060d4ddeb3872ec5e8d8f37e4ed6885 [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>
Patrick Venture96e97db2019-10-31 13:44:38 -070025#include <istream>
Cheng C Yang209ec562019-03-12 16:37:44 +080026#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070027#include <memory>
Cheng C Yang209ec562019-03-12 16:37:44 +080028#include <sdbusplus/asio/connection.hpp>
29#include <sdbusplus/asio/object_server.hpp>
30#include <string>
Patrick Venture96e97db2019-10-31 13:44:38 -070031#include <vector>
Cheng C Yang209ec562019-03-12 16:37:44 +080032
James Feist690895f2019-04-23 13:01:08 -070033static constexpr const char* sensorPathPrefix = "/xyz/openbmc_project/sensors/";
34
Josh Lehan49cfba92019-10-08 16:50:42 -070035static constexpr bool DEBUG = false;
36
Cheng C Yang209ec562019-03-12 16:37:44 +080037PSUSensor::PSUSensor(const std::string& path, const std::string& objectType,
38 sdbusplus::asio::object_server& objectServer,
39 std::shared_ptr<sdbusplus::asio::connection>& conn,
40 boost::asio::io_service& io, const std::string& sensorName,
41 std::vector<thresholds::Threshold>&& _thresholds,
42 const std::string& sensorConfiguration,
43 std::string& sensorTypeName, unsigned int factor,
44 double max, double min) :
James Feist930fcde2019-05-28 12:58:43 -070045 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
Cheng C Yang209ec562019-03-12 16:37:44 +080046 std::move(_thresholds), sensorConfiguration, objectType, max, min),
Cheng C Yanga97f1342020-02-11 15:10:41 +080047 objServer(objectServer), inputDev(io), waitTimer(io), path(path),
48 errCount(0),
Brad Bishopfbb44ad2019-11-08 09:42:37 -050049
James Feist930fcde2019-05-28 12:58:43 -070050 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.
Patrick Venture43162182019-10-23 10:44:53 -070088 setInitialProperties(conn);
89
AppaRao Pulic82213c2020-02-27 01:24:58 +053090 association = objectServer.add_interface(dbusPath, association::interface);
91
Cheng C Yang5580f2f2019-09-19 09:01:47 +080092 createInventoryAssoc(conn, association, configurationPath);
Cheng C Yang209ec562019-03-12 16:37:44 +080093 setupRead();
94}
95
96PSUSensor::~PSUSensor()
97{
Cheng C Yang209ec562019-03-12 16:37:44 +080098 waitTimer.cancel();
Cheng C Yanga97f1342020-02-11 15:10:41 +080099 inputDev.close();
Cheng C Yang209ec562019-03-12 16:37:44 +0800100 objServer.remove_interface(sensorInterface);
101 objServer.remove_interface(thresholdInterfaceWarning);
102 objServer.remove_interface(thresholdInterfaceCritical);
AppaRao Pulic82213c2020-02-27 01:24:58 +0530103 objServer.remove_interface(association);
Cheng C Yang209ec562019-03-12 16:37:44 +0800104}
105
106void PSUSensor::setupRead(void)
107{
108 boost::asio::async_read_until(
109 inputDev, readBuf, '\n',
110 [&](const boost::system::error_code& ec,
111 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
112}
113
114void PSUSensor::handleResponse(const boost::system::error_code& err)
115{
116 if (err == boost::system::errc::bad_file_descriptor)
117 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700118 std::cerr << "Bad file descriptor from " << path << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800119 return;
120 }
121 std::istream responseStream(&readBuf);
122 if (!err)
123 {
124 std::string response;
125 try
126 {
127 std::getline(responseStream, response);
Josh Lehan833661a2020-03-04 17:35:41 -0800128 double nvalue = std::stod(response);
Cheng C Yang209ec562019-03-12 16:37:44 +0800129 responseStream.clear();
130 nvalue /= sensorFactor;
Josh Lehan49cfba92019-10-08 16:50:42 -0700131
Josh Lehan833661a2020-03-04 17:35:41 -0800132 updateValue(nvalue);
Cheng C Yang209ec562019-03-12 16:37:44 +0800133 errCount = 0;
134 }
135 catch (const std::invalid_argument&)
136 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700137 std::cerr << "Could not parse " << response << " from path " << path
138 << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800139 errCount++;
140 }
141 }
142 else
143 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700144 std::cerr << "System error " << err << " from path " << path << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800145 errCount++;
146 }
147
148 if (errCount >= warnAfterErrorCount)
149 {
150 if (errCount == warnAfterErrorCount)
151 {
152 std::cerr << "Failure to read sensor " << name << " at " << path
153 << "\n";
154 }
155 updateValue(0);
156 errCount++;
157 }
158
Cheng C Yanga97f1342020-02-11 15:10:41 +0800159 lseek(fd, 0, SEEK_SET);
Cheng C Yang209ec562019-03-12 16:37:44 +0800160 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
161 waitTimer.async_wait([&](const boost::system::error_code& ec) {
162 if (ec == boost::asio::error::operation_aborted)
163 {
Josh Lehan49cfba92019-10-08 16:50:42 -0700164 std::cerr << "Failed to reschedule wait for " << path << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800165 return;
166 }
167 setupRead();
168 });
169}
170
171void PSUSensor::checkThresholds(void)
172{
173 thresholds::checkThresholds(this);
174}