blob: 2996b6f18779f8d6fcef98c3394600cfbd3a4f97 [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,
Cheng C Yang6b1247a2020-03-09 23:48:39 +080044 double max, double min, const std::string& label,
45 size_t tSize) :
James Feist930fcde2019-05-28 12:58:43 -070046 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
Cheng C Yang209ec562019-03-12 16:37:44 +080047 std::move(_thresholds), sensorConfiguration, objectType, max, min),
Cheng C Yanga97f1342020-02-11 15:10:41 +080048 objServer(objectServer), inputDev(io), waitTimer(io), path(path),
Cheng C Yang6b1247a2020-03-09 23:48:39 +080049 errCount(0), sensorFactor(factor)
Cheng C Yang209ec562019-03-12 16:37:44 +080050{
Josh Lehan49cfba92019-10-08 16:50:42 -070051 if constexpr (DEBUG)
52 {
53 std::cerr << "Constructed sensor: path " << path << " type "
54 << objectType << " config " << sensorConfiguration
55 << " typename " << sensorTypeName << " factor " << factor
56 << " min " << min << " max " << max << " name \""
57 << sensorName << "\"\n";
58 }
59
Cheng C Yanga97f1342020-02-11 15:10:41 +080060 fd = open(path.c_str(), O_RDONLY);
61 if (fd < 0)
62 {
63 std::cerr << "PSU sensor failed to open file\n";
64 return;
65 }
66 inputDev.assign(fd);
67
James Feist690895f2019-04-23 13:01:08 -070068 std::string dbusPath = sensorPathPrefix + sensorTypeName + name;
69
Cheng C Yang209ec562019-03-12 16:37:44 +080070 sensorInterface = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070071 dbusPath, "xyz.openbmc_project.Sensor.Value");
Cheng C Yang209ec562019-03-12 16:37:44 +080072
73 if (thresholds::hasWarningInterface(thresholds))
74 {
75 thresholdInterfaceWarning = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070076 dbusPath, "xyz.openbmc_project.Sensor.Threshold.Warning");
Cheng C Yang209ec562019-03-12 16:37:44 +080077 }
78 if (thresholds::hasCriticalInterface(thresholds))
79 {
80 thresholdInterfaceCritical = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070081 dbusPath, "xyz.openbmc_project.Sensor.Threshold.Critical");
Cheng C Yang209ec562019-03-12 16:37:44 +080082 }
James Feist690895f2019-04-23 13:01:08 -070083
AppaRao Pulic82213c2020-02-27 01:24:58 +053084 // This should be called before initializing association.
85 // createInventoryAssoc() does add more associations before doing
86 // register and initialize "Associations" property.
Cheng C Yang6b1247a2020-03-09 23:48:39 +080087 if (label.empty() || tSize == _thresholds.size())
88 {
89 setInitialProperties(conn);
90 }
91 else
92 {
93 setInitialProperties(conn, label, tSize);
94 }
Patrick Venture43162182019-10-23 10:44:53 -070095
AppaRao Pulic82213c2020-02-27 01:24:58 +053096 association = objectServer.add_interface(dbusPath, association::interface);
97
Cheng C Yang5580f2f2019-09-19 09:01:47 +080098 createInventoryAssoc(conn, association, configurationPath);
Cheng C Yang209ec562019-03-12 16:37:44 +080099 setupRead();
100}
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 Yang6b1247a2020-03-09 23:48:39 +0800106 objServer.remove_interface(association);
Cheng C Yang209ec562019-03-12 16:37:44 +0800107 objServer.remove_interface(sensorInterface);
108 objServer.remove_interface(thresholdInterfaceWarning);
109 objServer.remove_interface(thresholdInterfaceCritical);
AppaRao Pulic82213c2020-02-27 01:24:58 +0530110 objServer.remove_interface(association);
Cheng C Yang209ec562019-03-12 16:37:44 +0800111}
112
113void PSUSensor::setupRead(void)
114{
115 boost::asio::async_read_until(
116 inputDev, readBuf, '\n',
117 [&](const boost::system::error_code& ec,
118 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
119}
120
121void PSUSensor::handleResponse(const boost::system::error_code& err)
122{
123 if (err == boost::system::errc::bad_file_descriptor)
124 {
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800125 std::cerr << "Bad file descriptor from\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800126 return;
127 }
128 std::istream responseStream(&readBuf);
129 if (!err)
130 {
131 std::string response;
132 try
133 {
134 std::getline(responseStream, response);
Josh Lehan833661a2020-03-04 17:35:41 -0800135 double nvalue = std::stod(response);
Cheng C Yang209ec562019-03-12 16:37:44 +0800136 responseStream.clear();
137 nvalue /= sensorFactor;
Josh Lehan49cfba92019-10-08 16:50:42 -0700138
Josh Lehan833661a2020-03-04 17:35:41 -0800139 updateValue(nvalue);
Cheng C Yang209ec562019-03-12 16:37:44 +0800140 errCount = 0;
141 }
142 catch (const std::invalid_argument&)
143 {
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800144 std::cerr << "Could not parse " << response << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800145 errCount++;
146 }
147 }
148 else
149 {
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800150 std::cerr << "System error " << err << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800151 errCount++;
152 }
153
154 if (errCount >= warnAfterErrorCount)
155 {
156 if (errCount == warnAfterErrorCount)
157 {
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800158 std::cerr << "Failure to read sensor " << name << "\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800159 }
160 updateValue(0);
161 errCount++;
162 }
163
Cheng C Yanga97f1342020-02-11 15:10:41 +0800164 lseek(fd, 0, SEEK_SET);
Cheng C Yang209ec562019-03-12 16:37:44 +0800165 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
166 waitTimer.async_wait([&](const boost::system::error_code& ec) {
167 if (ec == boost::asio::error::operation_aborted)
168 {
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800169 std::cerr << "Failed to reschedule\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800170 return;
171 }
172 setupRead();
173 });
174}
175
176void PSUSensor::checkThresholds(void)
177{
178 thresholds::checkThresholds(this);
179}