blob: a7156a76d3708f0f0e874983f160d8f01946d78e [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
19#include <PSUSensor.hpp>
20#include <boost/algorithm/string/predicate.hpp>
21#include <boost/algorithm/string/replace.hpp>
22#include <boost/date_time/posix_time/posix_time.hpp>
23#include <iostream>
24#include <limits>
25#include <sdbusplus/asio/connection.hpp>
26#include <sdbusplus/asio/object_server.hpp>
27#include <string>
28
James Feist690895f2019-04-23 13:01:08 -070029static constexpr const char* sensorPathPrefix = "/xyz/openbmc_project/sensors/";
30
Cheng C Yang209ec562019-03-12 16:37:44 +080031PSUSensor::PSUSensor(const std::string& path, const std::string& objectType,
32 sdbusplus::asio::object_server& objectServer,
33 std::shared_ptr<sdbusplus::asio::connection>& conn,
34 boost::asio::io_service& io, const std::string& sensorName,
35 std::vector<thresholds::Threshold>&& _thresholds,
36 const std::string& sensorConfiguration,
37 std::string& sensorTypeName, unsigned int factor,
38 double max, double min) :
James Feist930fcde2019-05-28 12:58:43 -070039 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
Cheng C Yang209ec562019-03-12 16:37:44 +080040 std::move(_thresholds), sensorConfiguration, objectType, max, min),
James Feist930fcde2019-05-28 12:58:43 -070041 path(path), objServer(objectServer),
42 inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), errCount(0),
43 sensorFactor(factor)
Cheng C Yang209ec562019-03-12 16:37:44 +080044{
James Feist690895f2019-04-23 13:01:08 -070045 std::string dbusPath = sensorPathPrefix + sensorTypeName + name;
46
Cheng C Yang209ec562019-03-12 16:37:44 +080047 sensorInterface = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070048 dbusPath, "xyz.openbmc_project.Sensor.Value");
Cheng C Yang209ec562019-03-12 16:37:44 +080049
50 if (thresholds::hasWarningInterface(thresholds))
51 {
52 thresholdInterfaceWarning = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070053 dbusPath, "xyz.openbmc_project.Sensor.Threshold.Warning");
Cheng C Yang209ec562019-03-12 16:37:44 +080054 }
55 if (thresholds::hasCriticalInterface(thresholds))
56 {
57 thresholdInterfaceCritical = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070058 dbusPath, "xyz.openbmc_project.Sensor.Threshold.Critical");
Cheng C Yang209ec562019-03-12 16:37:44 +080059 }
Cheng C Yang5580f2f2019-09-19 09:01:47 +080060 setInitialProperties(conn);
61
James Feist2adc95c2019-09-30 14:55:28 -070062 association = objectServer.add_interface(dbusPath, association::interface);
James Feist690895f2019-04-23 13:01:08 -070063
Cheng C Yang5580f2f2019-09-19 09:01:47 +080064 createInventoryAssoc(conn, association, configurationPath);
Cheng C Yang209ec562019-03-12 16:37:44 +080065 setupRead();
66}
67
68PSUSensor::~PSUSensor()
69{
70 inputDev.close();
71 waitTimer.cancel();
72 objServer.remove_interface(sensorInterface);
73 objServer.remove_interface(thresholdInterfaceWarning);
74 objServer.remove_interface(thresholdInterfaceCritical);
75}
76
77void PSUSensor::setupRead(void)
78{
79 boost::asio::async_read_until(
80 inputDev, readBuf, '\n',
81 [&](const boost::system::error_code& ec,
82 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
83}
84
85void PSUSensor::handleResponse(const boost::system::error_code& err)
86{
87 if (err == boost::system::errc::bad_file_descriptor)
88 {
89 return;
90 }
91 std::istream responseStream(&readBuf);
92 if (!err)
93 {
94 std::string response;
95 try
96 {
97 std::getline(responseStream, response);
98 float nvalue = std::stof(response);
99 responseStream.clear();
100 nvalue /= sensorFactor;
James Feistb6c0b912019-07-09 12:21:44 -0700101 if (static_cast<double>(nvalue) != value)
Cheng C Yang209ec562019-03-12 16:37:44 +0800102 {
103 updateValue(nvalue);
104 }
105 errCount = 0;
106 }
107 catch (const std::invalid_argument&)
108 {
109 errCount++;
110 }
111 }
112 else
113 {
114 errCount++;
115 }
116
117 if (errCount >= warnAfterErrorCount)
118 {
119 if (errCount == warnAfterErrorCount)
120 {
121 std::cerr << "Failure to read sensor " << name << " at " << path
122 << "\n";
123 }
124 updateValue(0);
125 errCount++;
126 }
127
128 responseStream.clear();
129 inputDev.close();
130 int fd = open(path.c_str(), O_RDONLY);
131 if (fd <= 0)
132 {
133 return;
134 }
135 inputDev.assign(fd);
136 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
137 waitTimer.async_wait([&](const boost::system::error_code& ec) {
138 if (ec == boost::asio::error::operation_aborted)
139 {
140 return;
141 }
142 setupRead();
143 });
144}
145
146void PSUSensor::checkThresholds(void)
147{
148 thresholds::checkThresholds(this);
149}