blob: 58dee54075210c485a917c5c5a701828ddb9b5f4 [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 }
James Feist690895f2019-04-23 13:01:08 -070060 association =
61 objectServer.add_interface(dbusPath, "org.openbmc.Associations");
62
Cheng C Yang209ec562019-03-12 16:37:44 +080063 setInitialProperties(conn);
64 setupRead();
65}
66
67PSUSensor::~PSUSensor()
68{
69 inputDev.close();
70 waitTimer.cancel();
71 objServer.remove_interface(sensorInterface);
72 objServer.remove_interface(thresholdInterfaceWarning);
73 objServer.remove_interface(thresholdInterfaceCritical);
74}
75
76void PSUSensor::setupRead(void)
77{
78 boost::asio::async_read_until(
79 inputDev, readBuf, '\n',
80 [&](const boost::system::error_code& ec,
81 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
82}
83
84void PSUSensor::handleResponse(const boost::system::error_code& err)
85{
86 if (err == boost::system::errc::bad_file_descriptor)
87 {
88 return;
89 }
90 std::istream responseStream(&readBuf);
91 if (!err)
92 {
93 std::string response;
94 try
95 {
96 std::getline(responseStream, response);
97 float nvalue = std::stof(response);
98 responseStream.clear();
99 nvalue /= sensorFactor;
James Feistb6c0b912019-07-09 12:21:44 -0700100 if (static_cast<double>(nvalue) != value)
Cheng C Yang209ec562019-03-12 16:37:44 +0800101 {
102 updateValue(nvalue);
103 }
104 errCount = 0;
105 }
106 catch (const std::invalid_argument&)
107 {
108 errCount++;
109 }
110 }
111 else
112 {
113 errCount++;
114 }
115
116 if (errCount >= warnAfterErrorCount)
117 {
118 if (errCount == warnAfterErrorCount)
119 {
120 std::cerr << "Failure to read sensor " << name << " at " << path
121 << "\n";
122 }
123 updateValue(0);
124 errCount++;
125 }
126
127 responseStream.clear();
128 inputDev.close();
129 int fd = open(path.c_str(), O_RDONLY);
130 if (fd <= 0)
131 {
132 return;
133 }
134 inputDev.assign(fd);
135 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
136 waitTimer.async_wait([&](const boost::system::error_code& ec) {
137 if (ec == boost::asio::error::operation_aborted)
138 {
139 return;
140 }
141 setupRead();
142 });
143}
144
145void PSUSensor::checkThresholds(void)
146{
147 thresholds::checkThresholds(this);
148}