blob: f93846d4ff2608c81baaa3205975ed8530a1b8e4 [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>
James Feist8086aba2020-08-25 16:00:59 -070023#include <boost/asio/read_until.hpp>
Cheng C Yang209ec562019-03-12 16:37:44 +080024#include <boost/date_time/posix_time/posix_time.hpp>
James Feist38fb5982020-05-28 10:09:54 -070025#include <sdbusplus/asio/connection.hpp>
26#include <sdbusplus/asio/object_server.hpp>
27
Cheng C Yang209ec562019-03-12 16:37:44 +080028#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070029#include <istream>
Cheng C Yang209ec562019-03-12 16:37:44 +080030#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070031#include <memory>
Cheng C Yang209ec562019-03-12 16:37:44 +080032#include <string>
Patrick Venture96e97db2019-10-31 13:44:38 -070033#include <vector>
Cheng C Yang209ec562019-03-12 16:37:44 +080034
James Feist690895f2019-04-23 13:01:08 -070035static constexpr const char* sensorPathPrefix = "/xyz/openbmc_project/sensors/";
36
Josh Lehan49cfba92019-10-08 16:50:42 -070037static constexpr bool DEBUG = false;
38
Cheng C Yang209ec562019-03-12 16:37:44 +080039PSUSensor::PSUSensor(const std::string& path, const std::string& objectType,
40 sdbusplus::asio::object_server& objectServer,
41 std::shared_ptr<sdbusplus::asio::connection>& conn,
42 boost::asio::io_service& io, const std::string& sensorName,
43 std::vector<thresholds::Threshold>&& _thresholds,
44 const std::string& sensorConfiguration,
45 std::string& sensorTypeName, unsigned int factor,
Cheng C Yang6b1247a2020-03-09 23:48:39 +080046 double max, double min, const std::string& label,
47 size_t tSize) :
James Feist930fcde2019-05-28 12:58:43 -070048 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
Cheng C Yang209ec562019-03-12 16:37:44 +080049 std::move(_thresholds), sensorConfiguration, objectType, max, min),
Yong Libf8b1da2020-04-15 16:32:50 +080050 std::enable_shared_from_this<PSUSensor>(), objServer(objectServer),
James Feist961bf092020-07-01 16:38:12 -070051 inputDev(io), waitTimer(io), path(path), sensorFactor(factor)
Cheng C Yang209ec562019-03-12 16:37:44 +080052{
Josh Lehan49cfba92019-10-08 16:50:42 -070053 if constexpr (DEBUG)
54 {
55 std::cerr << "Constructed sensor: path " << path << " type "
56 << objectType << " config " << sensorConfiguration
57 << " typename " << sensorTypeName << " factor " << factor
58 << " min " << min << " max " << max << " name \""
59 << sensorName << "\"\n";
60 }
61
Cheng C Yanga97f1342020-02-11 15:10:41 +080062 fd = open(path.c_str(), O_RDONLY);
63 if (fd < 0)
64 {
65 std::cerr << "PSU sensor failed to open file\n";
66 return;
67 }
68 inputDev.assign(fd);
69
James Feist690895f2019-04-23 13:01:08 -070070 std::string dbusPath = sensorPathPrefix + sensorTypeName + name;
71
Cheng C Yang209ec562019-03-12 16:37:44 +080072 sensorInterface = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070073 dbusPath, "xyz.openbmc_project.Sensor.Value");
Cheng C Yang209ec562019-03-12 16:37:44 +080074
75 if (thresholds::hasWarningInterface(thresholds))
76 {
77 thresholdInterfaceWarning = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070078 dbusPath, "xyz.openbmc_project.Sensor.Threshold.Warning");
Cheng C Yang209ec562019-03-12 16:37:44 +080079 }
80 if (thresholds::hasCriticalInterface(thresholds))
81 {
82 thresholdInterfaceCritical = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070083 dbusPath, "xyz.openbmc_project.Sensor.Threshold.Critical");
Cheng C Yang209ec562019-03-12 16:37:44 +080084 }
James Feist690895f2019-04-23 13:01:08 -070085
AppaRao Pulic82213c2020-02-27 01:24:58 +053086 // This should be called before initializing association.
87 // createInventoryAssoc() does add more associations before doing
88 // register and initialize "Associations" property.
Cheng C Yang6b1247a2020-03-09 23:48:39 +080089 if (label.empty() || tSize == _thresholds.size())
90 {
91 setInitialProperties(conn);
92 }
93 else
94 {
95 setInitialProperties(conn, label, tSize);
96 }
Patrick Venture43162182019-10-23 10:44:53 -070097
AppaRao Pulic82213c2020-02-27 01:24:58 +053098 association = objectServer.add_interface(dbusPath, association::interface);
99
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800100 createInventoryAssoc(conn, association, configurationPath);
Cheng C Yang209ec562019-03-12 16:37:44 +0800101}
102
103PSUSensor::~PSUSensor()
104{
Cheng C Yang209ec562019-03-12 16:37:44 +0800105 waitTimer.cancel();
Cheng C Yanga97f1342020-02-11 15:10:41 +0800106 inputDev.close();
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{
Yong Libf8b1da2020-04-15 16:32:50 +0800115 std::shared_ptr<boost::asio::streambuf> buffer =
116 std::make_shared<boost::asio::streambuf>();
117 std::weak_ptr<PSUSensor> weakRef = weak_from_this();
Cheng C Yang209ec562019-03-12 16:37:44 +0800118 boost::asio::async_read_until(
Yong Libf8b1da2020-04-15 16:32:50 +0800119 inputDev, *buffer, '\n',
120 [weakRef, buffer](const boost::system::error_code& ec,
121 std::size_t /*bytes_transfered*/) {
122 std::shared_ptr<PSUSensor> self = weakRef.lock();
123 if (self)
124 {
125 self->readBuf = buffer;
126 self->handleResponse(ec);
127 }
128 });
Cheng C Yang209ec562019-03-12 16:37:44 +0800129}
130
131void PSUSensor::handleResponse(const boost::system::error_code& err)
132{
Yong Libf8b1da2020-04-15 16:32:50 +0800133 if ((err == boost::system::errc::bad_file_descriptor) ||
134 (err == boost::asio::error::misc_errors::not_found))
Cheng C Yang209ec562019-03-12 16:37:44 +0800135 {
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800136 std::cerr << "Bad file descriptor from\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800137 return;
138 }
Yong Libf8b1da2020-04-15 16:32:50 +0800139 std::istream responseStream(readBuf.get());
Cheng C Yang209ec562019-03-12 16:37:44 +0800140 if (!err)
141 {
142 std::string response;
143 try
144 {
145 std::getline(responseStream, response);
Zhikui Ren9bf68042020-08-13 15:31:26 -0700146 rawValue = std::stod(response);
Cheng C Yang209ec562019-03-12 16:37:44 +0800147 responseStream.clear();
Zhikui Ren9bf68042020-08-13 15:31:26 -0700148 double nvalue = rawValue / sensorFactor;
Josh Lehan49cfba92019-10-08 16:50:42 -0700149
Josh Lehan833661a2020-03-04 17:35:41 -0800150 updateValue(nvalue);
Cheng C Yang209ec562019-03-12 16:37:44 +0800151 }
152 catch (const std::invalid_argument&)
153 {
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800154 std::cerr << "Could not parse " << response << "\n";
James Feist961bf092020-07-01 16:38:12 -0700155 incrementError();
Cheng C Yang209ec562019-03-12 16:37:44 +0800156 }
157 }
158 else
159 {
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800160 std::cerr << "System error " << err << "\n";
James Feist961bf092020-07-01 16:38:12 -0700161 incrementError();
Cheng C Yang209ec562019-03-12 16:37:44 +0800162 }
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));
Yong Libf8b1da2020-04-15 16:32:50 +0800166
167 std::weak_ptr<PSUSensor> weakRef = weak_from_this();
168 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
169 std::shared_ptr<PSUSensor> self = weakRef.lock();
Cheng C Yang209ec562019-03-12 16:37:44 +0800170 if (ec == boost::asio::error::operation_aborted)
171 {
Cheng C Yang6b1247a2020-03-09 23:48:39 +0800172 std::cerr << "Failed to reschedule\n";
Cheng C Yang209ec562019-03-12 16:37:44 +0800173 return;
174 }
Yong Libf8b1da2020-04-15 16:32:50 +0800175 if (self)
176 {
177 self->setupRead();
178 }
Cheng C Yang209ec562019-03-12 16:37:44 +0800179 });
180}
181
182void PSUSensor::checkThresholds(void)
183{
184 thresholds::checkThresholds(this);
185}