blob: 86997d3437a285e28c1160bdcb6f921d174fd2e5 [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) :
39 Sensor(boost::replace_all_copy(sensorName, " ", "_"), path,
40 std::move(_thresholds), sensorConfiguration, objectType, max, min),
41 objServer(objectServer), inputDev(io, open(path.c_str(), O_RDONLY)),
42 waitTimer(io), errCount(0), sensorFactor(factor)
43{
James Feist690895f2019-04-23 13:01:08 -070044 std::string dbusPath = sensorPathPrefix + sensorTypeName + name;
45
Cheng C Yang209ec562019-03-12 16:37:44 +080046 sensorInterface = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070047 dbusPath, "xyz.openbmc_project.Sensor.Value");
Cheng C Yang209ec562019-03-12 16:37:44 +080048
49 if (thresholds::hasWarningInterface(thresholds))
50 {
51 thresholdInterfaceWarning = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070052 dbusPath, "xyz.openbmc_project.Sensor.Threshold.Warning");
Cheng C Yang209ec562019-03-12 16:37:44 +080053 }
54 if (thresholds::hasCriticalInterface(thresholds))
55 {
56 thresholdInterfaceCritical = objectServer.add_interface(
James Feist690895f2019-04-23 13:01:08 -070057 dbusPath, "xyz.openbmc_project.Sensor.Threshold.Critical");
Cheng C Yang209ec562019-03-12 16:37:44 +080058 }
James Feist690895f2019-04-23 13:01:08 -070059 association =
60 objectServer.add_interface(dbusPath, "org.openbmc.Associations");
61
Cheng C Yang209ec562019-03-12 16:37:44 +080062 setInitialProperties(conn);
63 setupRead();
64}
65
66PSUSensor::~PSUSensor()
67{
68 inputDev.close();
69 waitTimer.cancel();
70 objServer.remove_interface(sensorInterface);
71 objServer.remove_interface(thresholdInterfaceWarning);
72 objServer.remove_interface(thresholdInterfaceCritical);
73}
74
75void PSUSensor::setupRead(void)
76{
77 boost::asio::async_read_until(
78 inputDev, readBuf, '\n',
79 [&](const boost::system::error_code& ec,
80 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
81}
82
83void PSUSensor::handleResponse(const boost::system::error_code& err)
84{
85 if (err == boost::system::errc::bad_file_descriptor)
86 {
87 return;
88 }
89 std::istream responseStream(&readBuf);
90 if (!err)
91 {
92 std::string response;
93 try
94 {
95 std::getline(responseStream, response);
96 float nvalue = std::stof(response);
97 responseStream.clear();
98 nvalue /= sensorFactor;
Cheng C Yang209ec562019-03-12 16:37:44 +080099 if (nvalue != value)
100 {
101 updateValue(nvalue);
102 }
103 errCount = 0;
104 }
105 catch (const std::invalid_argument&)
106 {
107 errCount++;
108 }
109 }
110 else
111 {
112 errCount++;
113 }
114
115 if (errCount >= warnAfterErrorCount)
116 {
117 if (errCount == warnAfterErrorCount)
118 {
119 std::cerr << "Failure to read sensor " << name << " at " << path
120 << "\n";
121 }
122 updateValue(0);
123 errCount++;
124 }
125
126 responseStream.clear();
127 inputDev.close();
128 int fd = open(path.c_str(), O_RDONLY);
129 if (fd <= 0)
130 {
131 return;
132 }
133 inputDev.assign(fd);
134 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
135 waitTimer.async_wait([&](const boost::system::error_code& ec) {
136 if (ec == boost::asio::error::operation_aborted)
137 {
138 return;
139 }
140 setupRead();
141 });
142}
143
144void PSUSensor::checkThresholds(void)
145{
146 thresholds::checkThresholds(this);
147}