blob: 5410b08fb4239293efdb32f7016432572157e394 [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
29PSUSensor::PSUSensor(const std::string& path, const std::string& objectType,
30 sdbusplus::asio::object_server& objectServer,
31 std::shared_ptr<sdbusplus::asio::connection>& conn,
32 boost::asio::io_service& io, const std::string& sensorName,
33 std::vector<thresholds::Threshold>&& _thresholds,
34 const std::string& sensorConfiguration,
35 std::string& sensorTypeName, unsigned int factor,
36 double max, double min) :
37 Sensor(boost::replace_all_copy(sensorName, " ", "_"), path,
38 std::move(_thresholds), sensorConfiguration, objectType, max, min),
39 objServer(objectServer), inputDev(io, open(path.c_str(), O_RDONLY)),
40 waitTimer(io), errCount(0), sensorFactor(factor)
41{
42 sensorInterface = objectServer.add_interface(
43 "/xyz/openbmc_project/sensors/" + sensorTypeName + name,
44 "xyz.openbmc_project.Sensor.Value");
45
46 if (thresholds::hasWarningInterface(thresholds))
47 {
48 thresholdInterfaceWarning = objectServer.add_interface(
49 "/xyz/openbmc_project/sensors/" + sensorTypeName + name,
50 "xyz.openbmc_project.Sensor.Threshold.Warning");
51 }
52 if (thresholds::hasCriticalInterface(thresholds))
53 {
54 thresholdInterfaceCritical = objectServer.add_interface(
55 "/xyz/openbmc_project/sensors/" + sensorTypeName + name,
56 "xyz.openbmc_project.Sensor.Threshold.Critical");
57 }
58 setInitialProperties(conn);
59 setupRead();
60}
61
62PSUSensor::~PSUSensor()
63{
64 inputDev.close();
65 waitTimer.cancel();
66 objServer.remove_interface(sensorInterface);
67 objServer.remove_interface(thresholdInterfaceWarning);
68 objServer.remove_interface(thresholdInterfaceCritical);
69}
70
71void PSUSensor::setupRead(void)
72{
73 boost::asio::async_read_until(
74 inputDev, readBuf, '\n',
75 [&](const boost::system::error_code& ec,
76 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
77}
78
79void PSUSensor::handleResponse(const boost::system::error_code& err)
80{
81 if (err == boost::system::errc::bad_file_descriptor)
82 {
83 return;
84 }
85 std::istream responseStream(&readBuf);
86 if (!err)
87 {
88 std::string response;
89 try
90 {
91 std::getline(responseStream, response);
92 float nvalue = std::stof(response);
93 responseStream.clear();
94 nvalue /= sensorFactor;
Cheng C Yang209ec562019-03-12 16:37:44 +080095 if (nvalue != value)
96 {
97 updateValue(nvalue);
98 }
99 errCount = 0;
100 }
101 catch (const std::invalid_argument&)
102 {
103 errCount++;
104 }
105 }
106 else
107 {
108 errCount++;
109 }
110
111 if (errCount >= warnAfterErrorCount)
112 {
113 if (errCount == warnAfterErrorCount)
114 {
115 std::cerr << "Failure to read sensor " << name << " at " << path
116 << "\n";
117 }
118 updateValue(0);
119 errCount++;
120 }
121
122 responseStream.clear();
123 inputDev.close();
124 int fd = open(path.c_str(), O_RDONLY);
125 if (fd <= 0)
126 {
127 return;
128 }
129 inputDev.assign(fd);
130 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
131 waitTimer.async_wait([&](const boost::system::error_code& ec) {
132 if (ec == boost::asio::error::operation_aborted)
133 {
134 return;
135 }
136 setupRead();
137 });
138}
139
140void PSUSensor::checkThresholds(void)
141{
142 thresholds::checkThresholds(this);
143}